Tcl/Tk Cookbook - Text Editor

Step 2: Add menubar, menus and register callbacks

Script

Menubutton

The following script appended to ed.tcl will create three menubuttons which are associated with three pulldown menus.


#fill the  top menu
menubutton .menubar.file -text File -underline 0 -menu .menubar.file.menu
menubutton .menubar.edit -text Edit -underline 0 -menu .menubar.edit.menu
menubutton .menubar.find -text Find -underline 0 -menu .menubar.find.menu
pack .menubar.file .menubar.edit .menubar.find -side left
menubutton .menubar.help -text Help -underline 0 
pack .menubar.help -side right


The Tk command menubutton creates a menubutton as the child of a menubar. The options -text, -menu and -underline are specified for each button. The -underline option enables the menu to be invoked without using the mouse and from the keyboard (by holding the Alt down and typing the underlined character within the window). The -menu option associates a menu with the menubutton.

Executing this script will now give the following:

Pulldown Menus

You now need to attach menu entries to each of these menubuttons and create pulldown menus.

Append to the script the following:



#create pulldown menus

menu .menubar.file.menu
.menubar.file.menu add command -label Open -command {OpenFile}
.menubar.file.menu add command -label Save -command "SaveFile"
.menubar.file.menu add command -label "Save As" -command {SaveAsFile}
.menubar.file.menu add command -label Quit -command exit

menu .menubar.edit.menu
.menubar.edit.menu add command -label Cut -com CutSelection
.menubar.edit.menu add command -label Paste -com PasteSelection
.menubar.edit.menu add command -label Copy -com CopySelection
.menubar.edit.menu add command -label Clear -com {.ed delete 1.0 end}


In Tk, each menu entry can be either a command, toggle or check button. Each menu entry has -command option to associate the action to be invoked if that entry is selected. A menu entry is selected, by pressing the left mouse button on the top menubutton, traversing the pulldown menu while holding the button down and releasing it on the entry. When an entry is selected the action is invoked and the menu will be unposted.

The script above creates the pulldown menus for "File (menu entires Open, Save, SaveAs and Quit) and "Edit" (menu entries Cut, Paste, Copy and Clear ).

Cascading Menus

A "cascade button is added to the menu (instead of a command or a toggle or a check button) to make another level of subservient (walk-through) menu. The cascade button appears with an arrow pointing left to indicate the additional level of choices.

The script below attaches a cascade button for the menu entry labelled "Find Selection". The menu attached to this cascade allows the user to specify whether the serach should be carried out "Forward" or "Backward" from the current insertion point.



#Find menu
menu .menubar.find.menu
.menubar.find.menu add cascade -label "Find Selection" \
	-menu .menubar.find.menu.fmenu


where the fmenu in this case is specified as:

menu .menubar.find.menu.fmenu
.menubar.find.menu.fmenu add radiobutton -label Forward \
	 -com {FindSelection -forwards}
.menubar.find.menu.fmenu add radiobutton -label Backward \
	-com {FindSelection -backwards}

The rest of the entries are completed by appending the script:



.menubar.find.menu add command -label "Find and Replace" -com FindValue
.menubar.find.menu add command -label "Find Selection and Tag" \
				-com TagSelection

Input Focus to the menubar

Append the following two lines of script to complete the menu system for this application:


tk_menuBar .menubar .menubar.file .menubar.edit .menubar.find .menubar.help
focus .menubar

The ordering of the menubuttons associated with a menubar is necessary for the Tk command tk_menuBar which identifies the menus associated with each menubutton as well as the order of the menus for use with right and left arrow-keys.

Input focus is set to the menubar by the command focus. This is needed for making keystrokes and keyboard traversal recognised.

You can add separators to group the entries. Tk menus are tear-off by default and clicking on the dotted lines achieves it. This is used to capture the menus for the picture below: