#!/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/teach_bind.tcl"

# Help text.
set Help "" ; append Help {Taggeth -- Add a menu to go to any tagged text in a text widget

This program adds a menu to a text widget that enumerates all the tags, all the
text or canvas items with those tags, and lets you go to one of them.

} $TH_Bindings_Help {

Widgets of Taggeth

Maximum Menuentry String Length Scale

When creating the menus for tags, the line near the tag boundary is used as the
text for the menu entry. If the text is longer than this value, it gets
truncated to less than this value.


Tag Select Checkbutton

If set, whenever you select a range of text with a tag, the program selects that
range of text in the text widget. If clear, the program merely moves the cursor
to the beginning of the tagged range, just like if this checkbutton was set, but
it does not alter the selection.


'Show Unused Tags' Checkbutton

A tag can be created for a text widget without being assigned any characters.
Sometimes text widgets can have several 'unassigned' tags. If they should not be
included in the tags menu, leave this button empty. If you want them listed in
the tags menu, set this button on.


Maximum Ranges per Menu Scale

Sometimes there will be many ranges of a tag in a text widget. While identifying
them all is quick and easy, creating a menu entry for each one can be slow, and
will certainly result in a humungus and untidy menu. This scale allows you to
limit the number of ranges that get shown on a tag's submenu; if there are more
tag ranges, the excess ones are shunted down to lower cascade menus. You can
disable this artificial limit by specifying 0 on this scale.


The Teach Menu

  Tags

Select this menu option, then click on any text widget. A menu of tags will be
added.

} $TH_Bind_Help {
Does not work on canvas widgets. This is a difficult feature because a canvas
does not provide any means of identifying all the tags it uses. Also how should
one indicate one has 'gone to' a particular canvas item? Still such a beast
would be very useful.

None of the tag or tag range menus can be torn off. This is because tearing off
menus actually creates copies of the menus, and this confuses the tags code.}


# Gives app all the code necessary to do our functions.
proc teach_code {} {
  global TH_Dir Menu_Length Tag_Sel Tag_End Tag_Empty Menu_Max_Entries Class
  if {![file exists "$TH_Dir/lib/tag.$Class.tcl"]} {return}
  include_files [list tag.$Class.tcl "th_[set Class]_make_tags_menu"] \
	[list browse.$Class.tcl "th_[set Class]_goto"]
  do_cmd_set TH(Tag,Menu,Length) $Menu_Length
  do_cmd_set TH(Tag,Entries,Max) $Menu_Max_Entries
  do_cmd_set TH(Tag,Select) $Tag_Sel
  do_cmd_set TH(Tag,Empty) $Tag_Empty
}

# Teach an app the tags menu, and when to invoke it.
proc teach_tags {} {
  global TH_Dir Class
  if {![get_widget]} {bell ; return}
  clear_output
  teach_code
  teach_tag_bindings
}

proc teach_tag_bindings {} {
  global Bindings Widget App Class TH_Dir
  if {![file exists "$TH_Dir/lib/tag.$Class.tcl"]} {return}
  teach_menubindings $Bindings(Tags)
  if {[set i [string last "menu " [.output get 1.0 end]]] == -1} {return}
  set menu_line "1.0 +5c + $i c"
  set tags_menu [.output get $menu_line "$menu_line lineend"]
  do_cmd "$tags_menu configure -postcommand \{th_[set \
	Class]_make_tags_menu $Widget\}\n" 0
}


create_form_scale .tagml "Maximum Menuentry String Length" Menu_Length 30 \
    -from 5 -to 50
frame .tag ; pack .tag -fill x
create_form_checkbutton .tag.sel "Select Tag Range" Tag_Sel 1 left
create_form_checkbutton .tag.empty "Show Unused Tags" Tag_Empty 0 left
create_form_scale .tagme "Maximum Ranges per Menu" Menu_Max_Entries 20 \
    -from 0 -to 50

destroy .buttons.teach.m ; menu .buttons.teach.m
.buttons.teach.m add command -label "Tags" -command "teach_tags"
