Menus may be defined as part of the resources of an application. Within a resource file, a menu has the following form.
// win+ -- MenuDemonstration.rc -- menu Demonstration #include "MenuDemonstration.h" FrameIdentity MENU { POPUP "&file" { MENUITEM "&New", MenuItemNew MENUITEM "&Open...", MenuItemOpen MENUITEM "&Save", MenuItemSave MENUITEM "Save &As...", MenuItemSaveAs MENUITEM SEPARATOR MENUITEM "E&xit", MenuItemExit } POPUP "&edit" { MENUITEM "&undo", MenuItemundo MENUITEM SEPARATOR MENUITEM "Cu&t", MenuItemcut MENUITEM "©", MenuItemcopy MENUITEM "&paste", MenuItempaste MENUITEM "De&lete", MenuItemDelete } POPUP "&background" { MENUITEM "&White", MenuItemWhite, CHECKED MENUITEM "&Light Gray", MenuItemlight_gray MENUITEM "&Gray", MenuItemGray MENUITEM "&Dark Gray", MenuItemDarkGray MENUITEM "&Black", MenuItemBlack } POPUP "&timer" { MENUITEM "&Start" MenuItemStart MENUITEM "S&top" MenuItemStop, GRAYED } POPUP "&help" { MENUITEM "&help...", MenuItemHelp MENUITEM "&About menu Demonstration...", MenuItemAbout } } STRINGTABLE { StringHelp, "The help system is yet to be implemented." StringAbout, "This program demonstrates the use of menus." StringExit, "Exit?" Stringapplication, "menu Demonstration" FrameIdentity, "menu Demonstration" }
Such a specification yields the following snapshot.
The specification of a main menu is as shown below.
FrameIdentity MENU { ... }
The identity of the main menu may be numeric or it may be a string. If it is numeric, it is usually defined as a macro in a resource header (like the one shown below). When referencing the menu, if its name is numeric, it may be accessed through a macro or its numeric value may be used in string form: like "#100". The associated resource header is shown below.
// win+ -- MenuDemonstration.h -- menu Demonstration #define MenuItemNew 1 #define MenuItemOpen 2 #define MenuItemSave 3 #define MenuItemSaveAs 4 #define MenuItemExit 5 #define MenuItemundo 10 #define MenuItemcut 11 #define MenuItemcopy 12 #define MenuItempaste 13 #define MenuItemDelete 14 #define MenuItemWhite 20 #define MenuItemlight_gray 21 #define MenuItemGray 22 #define MenuItemDarkGray 23 #define MenuItemBlack 24 #define MenuItemStart 30 #define MenuItemStop 31 #define MenuItemHelp 40 #define MenuItemAbout 41 #define StringHelp 50 #define StringAbout 51 #define StringExit 52 #define Stringapplication 53 #define FrameIdentity 100
As may be observed, a main menu may have a number of submenus, which are usually referred to as popup menus. The original terminology for such submenus (from OS/2) was pull down menus. However, because the resource specification involves the keyword "POPUP", they will be referred to via that name. The generic term 'menu' will be used to refer to both main menus and popup menus.
A menu may have a number of items contained within. When selected, these items generate the notification message::command. These items are signified by the keyword MENUITEM. In place of an item within a menu, another menu may be started via the keyword "POPUP". Popup menus may be nested in this way. For the particular menu shown above, the "help" popup menu is nested within the "Main" menu. The snapshot of the actual menu shows the effect of doing this.
The specification of a menu is often referred to as a menu template. More particularly, when the resource compiler renders the specification into binary form, that form is called the menu template. The specific syntax of an item within a menu is:
MENUITEM "&Item text", item_identity [,options]
whereas, the format of a popup specification is:
POPUP "Popup &name" [,options] { [menu list] }
where the brackets (that is, '[' and ']') are used to denote that the enclosed item is optional. That is, the brackets are not part of the actual specification. The text displayed for each menu must reside between double quotation marks. The ampersand (&) denotes a mnemonic and causes the letter that follows to be underlined. When the alt key is used with the letter that follows, the combination causes the item to be selected. If no ampersand is included in the specification, the first letter of the menu or item name is used as the mnemonic.
The options that may be applied to MENUITEM and POPUP when they appear in a main menu are listed in the table below.
GRAYED | The menu item is inactive and does not generate the notification message::command. The text is displayed in grayed form. |
INACTIVE | The menu item is inactive and does not generate the notification message::command. The text is displayed normally. |
MENUBREAK | The items that follow appear on a new line of the menu. |
HELP | The items that follow are right-justified. |
Options may be combined using the or symbol (|); however, the options GRAYED and INACTIVE may not be used together. The option MENUBREAK is often not necessary in a main menu because the menu control automatically separates the items into multiple lines when the window is not sufficiently wide to accommodate all the items.
Following a POPUP statement in a main menu, the left and right braces (that is, '{' and '}') enclose the list of items. Within the scope of these braces, the following specifications are allowed:
MENUITEM "text,identity[,options]
and
MENUITEM SEPARATOR
and
POPUP "text"[,options] { [menu list] }
The options for menu items in a popup specification are shown in the table below.
CHECKED | A check mark appears to the left of the text. |
GRAYED | The menu item is inactive and does not generate the notification message::command. The text is displayed in grayed form. |
INACTIVE | The menu item is inactive and does not generate the notification message::command. The text is displayed normally. |
MENUBREAK | The items that follow appear on a new line of the menu. |
MENUBARBREAK | The item appears in a new column of the popup with a vertical line separating columns. |
GRAYED and INACTIVE may not be used together nor can MENUBREAK and MENUBARBREAK be used together. For items in popups, the text following the control sequence \t is placed in a new column spaced far enough to the right so as to accommodate the longest string in the preceding column. The control sequence \a causes text that follows to be justified. The identities ascribed to items are supplied to the application via its notification messages. Typically, these are defined in a header (as macros).