#!/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]"
}}
if {[info procs KeysymBox] == ""} {
  catch {source "$XF_Dir/templates/Procedures/KeysymBox.t"}
}


# Help text.

set Help "" ; append Help {Busyth -- Go busy while doing some command

This program teaches another program how to indicate it is busy with some
action. The program does this by changing the cursor to a watch and adding the
word 'Busy' to its window title. 


Widgets of Busyth

Event Entry

This entry contains a binding, like Key-Return or Button1 which when executed in
a remote widget, should go busy when doing its code.


'Bind' button

This button brings up XF's keysym dialog box, which will help you in selecting a
binding for the Event entry.


The Teach Menu

  Busy

Select the "Busy" entry in the Teach menu to select a widget. While teaching,
the cursor changes to a cross, and all mouseclicks get shunted to busyth. Busyth
takes a widget, and also some kind of event as input. This event can be an X
event, such as Key-Return, or Any-Button1, or it can be left blank.

If an X event is given, then the binding code is found for that event over the
widget given, and if a binding exists there, then busyth teaches the app to go
'busy' while executing that binding. If no binding code is found for that event
over the widget, binding code is next searched for over that widget's class,
then over 'all', then over the widget with the current focus in that app, and
finally the focus widget's class. So you can elect to set an application busy
whenever you hit <Return> or press <Button-1> over some widget, for example.

If no event is given, then the widget is assumed to have a '-command'
configuration entry, which performs a command when the widget is activated.
(Commands are used whenever a button is pressed, or a scrollbar or scale is
moved.) If the widget is a menu, the menuentry the mouse was pressed over yields
its command. The code associated with a command is modified to use the busy
function. In this way, you can set an application busy whenver you press a
button, move a scrollbar, or select a menu option.

} $TH_Help


## A slight modification of the focus code.

# Teach a widget (like a button) to go busy while doing its command.
proc teach_busy {binding} {
  global App Widget Class
  if {![get_widget]} {bell ; return}
  if {$binding == ""} {
    if {$Class == "Menu"} {
      global Y
      set root_y [send $App winfo rooty $Widget]
      set entry [send $App $Widget index @[expr $Y - $root_y]]
      if {[set command [send $App $Widget entrycget $entry -command]] == ""} {
	bell ; return}
      set w $Widget
      while {[send $App winfo class $w] == "Menu"} {
        set w [send $App winfo parent $w]}
      set cmd "$Widget entryconfigure $entry -command \{th_busy $w $command\}\n"
    } elseif {![catch {send "$App" $Widget cget -command} command]} {
      if {$command == ""} {bell ; return}
      set cmd "$Widget configure -command \{th_busy $Widget $command\}\n"
    } else {bell ; return}
  } else {
    set not_bound 1
    foreach w [concat [send $App bindtags $Widget] \
			[send $App bindtags \[focus\]]] {
      if {[set command [send $App bind $w <$binding>]] != ""} {
        set not_bound 0 ; break}}
    if $not_bound {bell ; return}
    set cmd "bind $w <$binding> \{th_busy $Widget $command\}\n"
  }
  clear_output
  include_files {busy.tcl th_busy}
  do_cmd $cmd 0
}

set Busy_Event ""
create_form_entry .event "Event" Busy_Event
button .event.keysym -text {Binding} \
	-command {.event.e insert insert [KeysymBox $XF_Dir/lib/Keysyms]}
pack .event.keysym -before .event.e -side right
if {[info procs KeysymBox] == ""} {.event.keysym configure -state disabled}
.buttons.teach.m add command -label "Busy" -com {teach_busy $Busy_Event}
