#!/afs/ece/usr/tcl/bin/wish -f
# The next line is executed by most shells, but not Tcl \
wish $0 $*


set Bind_Keyword [file tail [info script]]
source "[file dirname [info script]]/../aux/frame.tcl"

# Help text.
set Help "" ; append Help {Fileth -- Add keybindings to save/load contents of various widgets

This program teaches widgets how to save or load their contents to files or I/O
pipelines

} $TH_Bindings_Help {

Widgets of Fileth

'Pad File Entry' checkbutton

Normally, when a program requests a file, it creates an entry, fills the entry
with a directory name, then prompts you for the filename. Since Tk entries are
constrained to show their contents when nonempty, the entry gets shown with the
cursor at the far right of the entry, after the directory text. But, if this
button is checked, then the entry gets 'padded' with spaces; enough to fill the
entry, therefore scrolling the directory name out of the view of the
entry...this provides a more Emacs-like interface to the file entry.

'Monitor Changes to File' checkbutton

Generally, edith's edit commands, and fileth's file commands keep track of
changes made to the widget's contents, and if the contents have been changed
since the last Save or Read command, fileth's code will prompt the user to save
the widget's contents before doing a Read. One can also monitor the file
perodically to see if it changes by means other than fileth's. If this button is
on, then the file will be compared to the widget every time the mouse enters the
widget's window. (Actually the file's last-modified-time field will be compared
to the time the file was last saved/read). If this button is off, then no
changes in the file will be noticed.


'Allow Pipe I/O' checkbutton

Tcl's open command can accept command pipelines, as well as files. This is very
powerful, but can cause problems for security if used improperly. If this
checkbutton is left off, then fileth won't allow reading or writing to pipes,
otherwise pipeline I/O is OK.


Show Path and Name checkbuttons

These checkbuttons indicate whether the file's path and name should be displayed
in the frame below the widget associated with the file. (The path for pipelines
is the current directory.) For either checkbutton to be turned on, a checkbutton
will be created in the frame that contains the path or name. One can click on
the checkbutton and change its text to a "-". Clicking on it again will bring
back the path or name.

} $TH_Frame_Help {

The Padding done over the file entry will not work well if the entry has a
variable-width font.

The canvas postscript command only writes out the canvas currently visible, so a
scrollable canvas bigger than its view will get clipped.}


# Gives app all the code necessary to do our functions.
proc teach_code {} {
  global TH_Dir Allow_Pipe Show Class App Widget Pad
  if {[lsearch "Entry Text Canvas Listbox" $Class] < 0} {return ""}

  set f [teach_frame_code]
  include_files {file.Misc.tcl th_insert_file}
  if {[file exists "$TH_Dir/lib/file.[set Class].tcl"]} {
    include_files [list file.$Class.tcl "th_[set Class]_write_file"]
  }

  do_cmd_set TH(Pipe,Enabled) $Allow_Pipe
  do_cmd_set TH(File,Pad) $Pad
  if {$Show(path) && ![send $App winfo exists $f.fpl]} {
    do_cmd "pack \[checkbutton $f.fpl -textvariable TH(File,fpl,$Widget) -indicatoron 0 -variable TH(File,fpl,$Widget) -anchor e -onvalue {} -offvalue {-}\] -side left\n" 0
    do_cmd_set TH(File,fpl,$Widget) {}
  }
  if {$Show(name) && ![send $App winfo exists $f.fnl]} {
    do_cmd "pack \[checkbutton $f.fnl -textvariable TH(File,fnl,$Widget) -indicatoron 0 -variable TH(File,fnl,$Widget) -anchor e -onvalue {} -offvalue {-}\] -side left\n" 0
    do_cmd_set TH(File,fnl,$Widget) {}
}}

# For a widget, returns the appropriate bindings. (They will depend on the
# widget)
proc widget_bindings {} {
  global Bindings Allow_Pipe Mtime Class

  if {[lsearch "Entry Text Canvas Listbox" $Class] < 0} {return ""}
  set bindings ""

  set bindings [concat $bindings $Bindings(File,Write)]
  if {[lsearch "Entry Text" $Class] >= 0} {
    set bindings [concat $bindings $Bindings(File,Read)]
  }

  if $Mtime { set bindings [concat $bindings $Bindings(File,Mtime)] }
  if $Allow_Pipe { set bindings [concat $bindings $Bindings(File,Pipe)]}
  return [widget_frame_bindings $bindings]
}


pack [frame .file] -fill x
create_form_checkbutton .file.pad "Pad File Entry" Pad 0 left
create_form_checkbutton .file.mtime "Monitor Changes to File" Mtime 1 left
create_form_checkbutton .file.pipe "Allow Pipe I/O" Allow_Pipe 1 left
pack [frame .show] -fill x
pack [label .show.frame -text "In frame, show"] -side left
create_form_checkbutton .show.labelp "Path" Show(path) 0 left
create_form_checkbutton .show.labelf "Name" Show(name) 0 left

# This cancels the check_mtime binding; since it makes the entry grab the focus
# whenever the mouse passes over it.
bind .buttons.bind <Enter> ""
