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


foreach pair {{get_widget aux/teach.tcl}} {
  if {[info procs [lindex $pair 0]] == ""} {
    source "[file dirname [info script]]/../[lindex $pair 1]"
}}


# Help text.
set Help "" ; append Help {Historyth -- Extend Entries with History Menus

Perhaps you've encountered an entry you use often, but only give a handful of
values to, and you wish it would remember those values?

Historyth's duty is to give entries a 'memory', so you can save values you liked
in the entry to a menu. When you select those items in the menu, they reappear
in the entry. A '+' button allows you to add whatever is in the entry to the
menu. After hitting the 'Teach' button, you click on some remote widget, and
this program endows the remote widget(s) with these special features.


Widgets of Historyth

The Teach Menu

  History

Select the History entry in the Teach menu to teach an entry widget. You can
only teach one entry widget at a time. After selecting the History entry, you
may do one of two things.

1. Click on an entry widget, which will then bring up its own History menu and
'+' button, or

2. Click on a label that describes the entry (if there is one), then click on
the entry. The label will be replaced by the History menu and '+' button.

} $TH_Help


# Extends the remote widget
proc extend_widget {} {
  global App Widget Class TH_Dir
  set potential_menu ""
  set potential_app ""
  if {![get_widget]} {bell ; return}
  if {$Class != "Entry"} {
    set potential_menu $Widget
    set potential_app $App
    if {![get_widget]} {bell ; return}
    if {$Class != "Entry"} {bell ; return}
  }
  if {$potential_menu == ""} {
    if {[send $App "winfo exists \"\[winfo parent $Widget\].thistory\""]} {bell ; return}
  } else {
    if {$App != $potential_app} {bell ; return}
    if {[send $App winfo exists $potential_menu] &&
	([send $App winfo class $potential_menu] == "Menubutton")} {bell ; return}}

  clear_output
  include_files {history.tcl th_make_history_menu}
  do_cmd "th_make_history_menu $Widget $potential_menu" 0
}


.buttons.teach.m add command -label "History" -command {extend_widget}
