#!/afs/ece/usr/tcl/bin/wish -f

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.


Text Tag Start / End Checkbuttons

If the start checkbutton is set, an entry is created for the start of each range
of tagged text. If the end checkbutton is set, another entry is created for the
end of each range of tagged text. 


Tag Empty 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.

It is possible for you to indicate not to put beginning-of-range or end-of-range
menu options for tags, but this is rather useless.
}

# Gives app all the code necessary to do our functions.
proc teach_code {} {
  global TH_Dir Menu_Length Tag_Start Tag_End Tag_Empty Menu_Max_Entries Class
  if {![file exists "$TH_Dir/lib/tag.$Class.tcl"]} {th_beep ; 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\n" 0
  do_cmd "set TH(Tag,Entries,Max) $Menu_Max_Entries\n" 0
  do_cmd "set TH(Tag,Start) $Tag_Start\n" 0
  do_cmd "set TH(Tag,End) $Tag_End\n" 0
  do_cmd "set TH(Tag,Empty) $Tag_Empty\n" 0
}

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

proc teach_tag_bindings {} {
  global Bindings Widget App Class
  teach_menubindings $Bindings(Tags)
  set string [.output get "end -4l" end]
  set tags_menu [string range $string 0 [expr [string first " " $string] -1]]
  set tags_parent [send $App winfo parent $tags_menu]
  do_cmd "$tags_parent entryconfigure last -command \{th_[set \
	Class]_make_tags_menu $Widget $tags_menu\}\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.start "Text Tag Start" Tag_Start 1 left
create_form_checkbutton .tag.end "Text Tag End" Tag_End 0 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"


