Window Decorators

Tycho provides a set of classes that are added to top-level windows to perform certain well-defined functions. There are currently three of these "decorator" classes.

("Decorator" is perhaps a poor choice for this set of classes, since the meaning is very different from the Decorator object-oriented design pattern.)

Contents

MenuBar Class

The MenuBar class is a complete application menu bar. (MenuBar is a subclass of the abstract class MenuSupport, which implements most of the menu functionality.) The Displayer top-level window contains a MenuBar object for each View packed into it. Views are responsible for maintaining their own menu bars: they each have a method called menubar which directs commands to its menu bar; each view subclass adds appropriate entries to the menubar.

To illustrate the workings of the menu bar, we will just create a menu bar and pack it into a top-level window. To understand the Displayer-View machinery, see the documentation for the Displayer and View classes. Create a top-level window and and an empty menubar:

set f [::tycho::autoName .dechtml]
::tycho::TopLevel $f
::tycho::MenuBar $f.mb
pack $f.mb -fill x -expand on
$f centerOnScreen
(The window will appear as a very small rectangle in the center of your screen.) Now, we'll add a couple of menus to the menu bar:
$f.mb addMenu File
$f.mb addMenu Help -side right -underline 0
set bogus ""
(Ignore the last line: that is needed to prevent a window with the result of the call -- which we don't want to see -- from popping up.) Note that the Help menu is packed to the right of the menu bar: stretch the window with the mouse to see this. The -underline option places an underline under the indicated letter of the label and enables keyboard navigation through menus. (Sorry, this is currently broken...)

The menu bar accesses all menus and menu entries by label -- that is, by the string displayed in the menu bar or in the menu. To add entries to a menu, call add with the entry label, the name of the menu to place the entry into, and additional options. For example, we'll add a Close entry to the File menu and a Tycho Home Page entry to the Help menu:

$f.mb add Close File -command {delete object $f}
$f.mb add {Tycho Home Page} Help \
	-command {::tycho::File::openContext \
	$TYCHO/doc/index.html} \
	-underline 0 -accelerator "C-x h"
(Click on the File and Help menu buttons to bring down the menus.) Note that labels containing spaces must be bracketed as a list, or the menu bar will get confused -- as in {Tycho Home Page}.

The add command takes any options acceptable to the entries of the Tk menu widget. Three commonly-used ones are illustrated above:

You can add a separator to a menu:

$f.mb addSeparator Help
$f.mb add Foo Help -command {::tycho::inform Foo!}

Individual menu entries can be disabled and enabled:

$f.mb disable {Tycho Home Page}
(Pop down the Help menu: the Tycho Home Page entry is greyed out and cannot be invoked.) Whole menus can be disabled in exactly the same way:
$f.mb disable File
To re-enable disabled menus or entries, just call enable:
$f.mb enable {Tycho Home Page}
$f.mb enable File
For more detailed information on menu bars, see the MenuBar and MenuSupport
class documentation.

StatusBar Class

The StatusBar is a bar designed to be placed along the bottom of top-level windows. It contains a button to close the window, an optional field indicating file status (writable, read-only, or modified), and an area in which short but informative status messages can be displayed. Usually, the status bar is accessed from a View class, which has the method statusbar to direct commands to its status bar.

To illustrate the operation of the status bar, we will create one and pack it into a top-level window with a empty frame to give the window some size:

set fr [::tycho::autoName .dechtml]
::tycho::TopLevel $fr
frame $fr.f -width 300
pack $fr.f -fill x -expand on
::tycho::StatusBar $fr.sb -closecommand {delete object $fr}
pack $fr.sb -fill x -expand on
$fr centerOnScreen
The status bar just created has a command given to it via the -closecommand option, which deletes the object when the close button is pressed. The status bar has a number of other options that control its appearance. To add a file status display, set the -filestatus to "readonly," "writeable," or "modified":
$fr.sb configure -filestatus readonly
Another one:
$fr.sb configure -filestatus modified
To change the displayed text in the bar:
$fr.sb configure -closetext Quit
To leave some space at the right side of the close button to match a scrollbar (presumably in some other window in the parent window):
$fr.sb configure -scrollbarpad 1
There is really only one method of interest in the status bar: puts prints a string to the status region:
$fr.sb puts "A brief but informative message"
To clear the display:
$fr.sb puts ""
Often, printing a string and clearing it are bound to Tk <Enter> and <Leave> events -- that way the status bar can be used as a simple help mechanism. The toolbar, for example, to print information about a button when the mouse is moved over that button.

ToolBar Class

The ToolBar is a bar designed to be placed along the top of top-level windows. It contains a row of buttons that make commonly-used functions highly visible to the inexperienced user. It can also display entry widgets below the buttons -- these are typically used when the temporary mature of the StatusBar display is not sufficient, or when direct user input is desired without popping up a dialog box (or both!).

We will place the toolbar in an empty top-level window to illustrate its operation. Usually, the toolbar will be accessed through the toolbar method of the View class. We will use the same window as for the status bar -- if you have closed it, go bak and create another. Then:

::tycho::ToolBar $fr.tb -statusbar $fr.sb
pack $fr.tb -fill x -expand on -before $fr.f
At first, the toolbar is blank, so you won't actually see anything. Let's add a couple of buttons to it:
$fr.tb button foo "The first button" -text "Push Me" \
		-command {::tycho::post Thanks!}
$fr.tb button bar "The second button"   -text "Push Me Too" \
		-command {::tycho::post {Thanks again!}}
set bogus ""
Notice that the button description is displayed in the status bar when the mouse is moved over the button. Buttons can be disabled:
$fr.tb disable foo
And enabled:
$fr.tb enable foo
To add entry widgets to the toolbar, do this:
$fr.tb entry thing "Enter something here" "Start with" {::tycho::post}
set bogus ""
If you press the Return key while in the entry widget, the contents of the widget are appended to the command (the last argument in the above call) and the command is executed. In this case, a window will display the entry widget's contents. To get all of the data in a toolbar, you can use get:
$fr.tb get
The entry widgets can be disabled if you need to prevent the user from altering the data (why does the appearance not change, though???):
$fr.tb disable thing
And enabled:
$fr.tb enable thing

MenuBar class documentation
MenuSupport class documentation
ToolBar class documentation
StatusBar class documentation
Tycho Home Page


Copyright © 1996, The Regents of the University of California. All rights reserved.
Last updated: 96/12/01, comments to: johnr@eecs.berkeley.edu