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


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


# Help text.

set Help "" ; append Help {Cycleth -- Execute some command periodically

This program teaches another program to execute some command at periodic
intervals. It can optionally attatch a checkbutton or checkbutton menu entry to
the cycling command, which the user can use to control the cycle. The cycle can
be paused by turning the checkbutton off, and resumed by turning the button on,
and it deactivates when the button or menu is destroyed. Furthersmore, if a
checkbutton is off, you can execute the command, leaving the cycle paused by
double-clicking on the button, and if the checkbutton is on, you can reset the
cycle by double-clicking on the button.


Widgets of Cycleth

Command Entry

This entry contains a command to be executed periodically. It should be
specified as a valid Tcl command.

Time Entry

This entry gives a number (in milliseconds) of how long to wait before
executing each instance of the command.

'Button Name' Entry

If left empty, no checkbutton is associated with the cycle. Otherwise, a
checkbutton or menuentry-checkbutton is created with this name as text.

The Teach Menu

  Cycle

Select the "Cycle" entry in then Teach menu, then select an application. The
application will begin executing the cycle command at regular intervals. While
teaching, the cursor changes to a cross, and all mouseclicks get shunted to
cycleth. If a checkbutton is being created (i.e. the 'Button Name' entry is not
empty, then clicking on a menu or menuentry creates a menuentry checkbutton in
that menu. Clicking on a frame or toplevel widget creates a checkbutton in that
frame or toplevel, and clicking on any other widget creates a checkbutton in
that widget's parent.

} $TH_Help {

Cycleth is not particularly intelligent when it comes to packing checkbuttons
and menuentries. Use displayth to reorganize your window or menu layout, and
you can use configureth to alter other properties of the checkbutton or
menuentry.}


# Figure out a name for a new widget in parent
proc new_widget_name {parent} {
  global App
  set i 1
  if {$parent == "."} {set p ""} else {set p $parent}
  while {[send $App winfo exists "$p.th$i"]} {incr i}
  return "$p.th$i"
}  

# Teach an application to cycle a command
proc teach_cycle {} {
  global TH_Dir App Cycle_Cmd Cycle_Time Cycle_Name Widget Class
  if {![get_widget]} {bell ; return}
  clear_output
  include_files {cycle.tcl th_cycle}
  if {$Cycle_Name == ""} {
    set handle 1
    while {![catch "send $App set TH(Cycle,Active,$handle)"]} {incr handle}
    do_cmd_out "th_cycle $handle $Cycle_Time $Cycle_Cmd"
  } else {switch $Class {
	"Menu" {
    do_cmd_out "$Widget add checkbutton -label \{$Cycle_Name\}"
    do_cmd_out "th_menucheckbutton_cycle $Widget end $Cycle_Time $Cycle_Cmd"
  } "Menubutton" {
    set menu [send $App $Widget cget -menu]
    do_cmd_out "$menu add checkbutton -label \{$Cycle_Name\}"
    do_cmd_out "th_menucheckbutton_cycle $menu end $Cycle_Time $Cycle_Cmd"
  } "Frame" - "Toplevel" {
    set button [new_widget_name $Widget]
    do_cmd_out "pack \[checkbutton $button -text \{$Cycle_Name\}\]"
    do_cmd_out "th_Checkbutton_cycle $button $Cycle_Time $Cycle_Cmd"
  } default {
    set button [new_widget_name [send $App winfo parent $Widget]]
    do_cmd_out "pack \[checkbutton $button -text \{$Cycle_Name\}\]"
    do_cmd_out "th_Checkbutton_cycle $button $Cycle_Time $Cycle_Cmd"
  }}}
}

.buttons.teach.m add command -label "Cycle" -com {teach_cycle}
set Cycle_Cmd ""
create_form_entry .cmd "Command" Cycle_Cmd
create_form_entry .time "Time" Cycle_Time
foreach foo {100 500 1000 5000 30000 60000} {
  th_history_menu_add .time.e $foo "[expr $foo / 1000.0] seconds"
}
if {$Cycle_Time == ""} {set Cycle_Time 1000}
create_form_entry .widget "Button Name" Cycle_Name
