Menu

The GUI library simplifies creation of Menu objects.  A display has a menu bar at the top.  This is initially empty.  Menu items may be added to it (e.g., “File”, “Edit”, etc.).  Menus can also be added to a display (or any other graphical object, for that matter), as pop-up menus (i.e., menus that came up when you press the right mouse button).

Menu objects are created using the following function:

Function Description
Menu(menuName) Creates a new Menu with the specified name.

Once a menu has been created, it can be populated with items using the following functions:

Function Description
menu.addItem(item, functionName) Adds item (string) to the menu, and specifies which function to call when item is selected.
menu.addItemList(itemList, functionNameList) Adds a list of items (a list of strings) to the menu, and specifies the corresponding functions to call (one function per item).  The two lists are parallel (and thus need to be of equal length).
menu.addSeparator() Adds a separator line to the menu.
menu.addSubmenu(menu) Adds a submenu to the menu.  Used for creating hierarchical menus.
menu.enable() Enables the menu (active).
menu.disable() Disables/grays out the menu (inactive).

Once a menu has been created, it can be added to a display as follows:

Function Description
d.addMenu(menu) Adds menu to the menu bar (left to right) of display d (e.g., “File”, “Edit”, etc.)
d.addPopupMenu(menu) Adds a popup menu (e.g. a right-click menu) on display d.

Again, the second function is also available for every GUI object (e.g., a circle).  This opens the door for many interesting GUI applications.

The following outlines the process of creating menu objects:

Drop-down Menus

Every display has its own menu bar.  To add menus to it, follow these steps:

1.  Create a Menu, as follows:

where name is a string.

2.  Add menu items to a menu, as follows:

where name is a string, and function is a function to be called when the user selects this menu item.  This function should expect no parameters.

3.  Finally, add the menu to a Display’s menu bar:

Pop-up Menus

Pop-up menus are menus displayed when the user right-clicks on a display or other GUI object.

Pop-up menus are created the same way as drop-down menus.  The only difference is that pop-up menus are added to a GUI object using the object’s addPopupMenu() function, as follows: