Menus are modelled by the type Menu.html#Menu.MenuMenu. Rather surprisingly, Menus need not be packed. Menus can either be the normal things which appear in a menu bar (typically at the top of a window), or attached to a single button, or pop-up menus which pop up out of nowhere. Actually, these are typically considered bad interface design, since they rarely confirm to user expectations (i.e. the user has no way of knowing wether a pop-up menu will appear). Try to use pop-up Menus only as optional, convenient short-cuts. While we're at it, try also to use one menu bar only, at the top of the window, since several menu bars in one window will be confusing, and try not to change the menus while the user isn't looking.
Menus in HTKcan contain:
Checkbuttons and radio buttons have a state, which can be access by attaching a Tk variable to them (class HTk.html#HTk.HasVariableHasVariable; a TkVariable is created with HTk.html#HTk.createTkVariablecreateTkVariable).
Here, we construct a typical menu. We start with creating a window and
all that:
Now, we create the element holding the menu, and attach it as a
menubar to the window. The second parameter to createMenu is
a boolean determining wether this is a tear-off menu
(i.e. a
menu which you can tear off from its menu button, and keep open).
Each pull-down menu in the menubar is in fact a submenu; we first
create such a submenu, and then attach a menu to it:
The utility function createPulldownMenu has been provided,
since creating a pull-down menu is such a common task; it does
exactly what the previous three lines have done, and will be used in
the rest of this wee example. Now we create the three simple menu
items, followed by a separator and a submenu of two more items:
Now we create a second pulldown menu with the a check button, and a
group of three radio buttons. Note that Tk variables can hold all
types which are instances of the class GUIValue, in
particular strings and characters (and note how we have to
disambiguate the overloaded numerals).
This code in itself defines the menu. To query the state of the
variables, we bind the check button, and spawn a thread which reads
the variables. However, you do not need to bind anything to a button
if you do not want to react on it being pressed, and just want to read
the value of the variable at some point. A menu command is an instance
of class HasCommand, so you can use clicked to bind
a simple event to it (see
Sect. ssec:teenage-clicks).
More exciting examples of using menus-- in particular pop-up menus-- can be found in htk/examples/simple/Mainmenu.hs.
The reader will probably agree that this is a lot of code for such a simple task, and quite unnecessarily so. For the convenient design of menus, we recommend the menu modules from the toolkit (see Sect. 10.4), which are at an abstraction level more suitable for a functional language.