#!/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]]/../util/frame.tcl"

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

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

} $TH_Bindings_Help {

Widgets of Fileth

'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.

'Autocheck Time' entry

Generally, edith's edit commands, and fileth's file commands keep track of
changes made to the widget's contents. But they don't notice when the file is
simultaneously updated by some other program. This entry allows you to
ooptionally check the file periodically to see if someone other than you has
modified it since you last read or saved it. It can also optionally read or
write to the file, depending on some settings the program learns. You can
specify here a time (in milliseconds) to check the file. The application will
check the file periodically (using this time interval), and then maybe reread
the file, write a backup copy of its contents, or warn you if a change has
occurred. If you leave this entry blank, then no checking is done.

'Auto-Save Suffix' entry

When autochecking is enabled, and periodic auto-saving is turned on, this entry
specifies a suffix to add to the backup file, to distinguish it from the
original file. You can always leave this blank, in which case auto-saves are
done to the original file. Which means that no backup files are involved; the
file is implicitly saved soon after any modifications are done. If the
'Autocheck Time' entry is empty, this entry is ignored.

} $TH_Frame_Help {

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 Auto_Suffix
  global Auto_Time
  if {[lsearch "Entry Text Canvas Listbox" $Class] < 0} {return ""}

  set f [teach_frame_code]
  include_files {file.Misc.tcl th_insert_file} \
		{cycle.tcl th_cycle}
  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
  if {$Auto_Time > 0} {
    do_cmd_set TH(File,Auto,Warn,$Widget) 1
    do_cmd_set TH(File,Auto,Type,$Widget) 1
    do_cmd_set TH(File,Auto,Suffix,$Widget) $Auto_Suffix
    if {[catch "send $App set TH(Cycle,Active,$Widget)"]} {
      do_cmd_out "th_cycle $Widget $Auto_Time th_file_auto $Widget"
  }}
  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 Class Auto_Time

  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 $Allow_Pipe { set bindings [concat $bindings $Bindings(File,Pipe)]}
  if {$Auto_Time > 0} {set bindings [concat $bindings $Bindings(File,Auto)]}
  return [widget_frame_bindings $bindings]
}


pack [frame .file] -fill x -side top
create_form_checkbutton .file.pipe "Allow Pipe I/O" Allow_Pipe 1 left
pack [frame .show] -fill y -side left
pack [label .show.frame -text "In frame, show"] -side top
create_form_checkbutton .show.labelp "Path" Show(path) 0 top
create_form_checkbutton .show.labelf "Name" Show(name) 0 top
pack .show.labelp .show.labelf -expand yes -fill both
pack [frame .check] -fill x
create_form_entry .check.mtimetime "Autocheck Time" Auto_Time
create_form_entry .check.auto "Auto-Save Suffix" Auto_Suffix
foreach foo {".CKP" ".BAK" "#" "~"} {
  th_history_menu_add .check.auto.e $foo
}
