- Introduction—Types of Menus
- Pull-Down Menus
- Hierarchical Menus
- Pop-Up Menus
- Menu Objects, Menu IDs and Item Numbers, Command IDs, and Menu Lists
- Creating Your Application's Menus
- Providing Help Balloons (Mac OS 8/9)
- Changing Menu Item Appearance
- Adding Items to a Menu
- Associating Data with Menu Items
- Handling Menu Choices
- Hiding and Showing the Menu Bar
- Accessing Menus from Alerts and Dialogs
- Main Menu Manager Constants, Data Types, and Functions
- Demonstration Program Menus1 Listing
- Demonstration Program Menus1 Comments
- Demonstration Program Menus2 Listing
- Demonstration Program Menus2 Comments
Demonstration Program Menus1 Comments
When this program is run, the user should choose items from all menus, including the Apple menu. Selections should be made using the mouse and, where appropriate, the Command key equivalents. The user should also note the effects on the menu bar of clicking outside, then inside, the program's window, that is, of sending the program to the background and returning it to the foreground.
defines
Constants are established for the pull-down and submenu menu IDs and associated resource IDs, menu item numbers and subitem numbers.
The Menu Manager function CreateStandardFontMenu will be used to create a hierarchical Font menu and mFirstFontSubMenu establishes the ID of the first Font menu submenu to be created.
The last line establishes a constant for the resource ID of the 'WIND' resource.
Global Variables
gFontMenuHierMenuCount will be assigned a value representing the number of submenus created by the Menu Manager function CreateStandardFontMenu.
GCurrentFontSizeItem will be assigned the menu item number of the chosen font size.
gCurrentFontMenuItem and gCurrentFontSubMenuItem will be used in the Font menu handling function to specify which menu and submenu items are to have a checkmark added or cleared. gCurrentFontSubMenuRef will be assigned a reference to the menu object for the currently chosen Font menu submenu.
main
The main() function creates a window and makes its graphics port the current port, calls doGetMenus to set up the menus, shows the window and enters the main event loop.
doPreliminaries
The large number of master pointers created by MoreMasterPointers in this program allows for the likely creation of a large number of submenus by the Menu Manager function CreateStandardFontMenu.
When the program is run on Mac OS X, the Quit item will be in the Application menu. The system informs the program of the user's choice of this item via a high-level event known as an Apple event, more specifically, an Apple event known as the Quit Application event. The call to AEInstallEventHandler installs quitAppEventHandler as the handler for this high-level event. (Apple events and Apple event handlers are explained in Chapter 10.)
quitAppEventHandler
quitAppEventHandler is the handler for the Quit Application event installed in doPreliminaries. Basically, it sets the global variable gDone to true, which causes the program to terminate from the main event loop.
doGetMenus
doGetMenus sets up the menu bar and the various menus.
At the first block, GetNewMBar reads in the 'MENU' resources for each menu specified in the 'MBAR' resource and creates a menu object for each of those menus. (Note that the error handling here and in other areas of this program is somewhat rudimentary: the program simply terminates.) The call to SetMenuBar makes the newly created menu list the current list.
The call to Gestalt determines whether the application is running on Mac OS 8/9 or Mac OS X. If the application is running on Mac OS X, GetMenuRef is called to get a reference to the menu object for the File menu and DeleteMenuItem is called to delete the Quit item and its preceding divider from the menu.
The third block utilizes the relatively new Menu Manager function CreateStandardFontMenu to create a hierarchical font menu. A reference to the Font menu object is passed in the first parameter. The third parameter specifies the menu ID for the first submenu to be created. The fourth parameter specifies that the Font menu be created as a hierarchical menu. The fifth parameter receives a value representing the number of submenus created. CreateStandardFontMenu itself inserts these submenus into the submenu portion of the menu list.
The fourth block checkmarks the Font menu item containing the name of the small system font. GetFontName gets the name of the small system font and CountMenuItems counts the number of items in the Font menu. The for loop then walks the items in the Font menu looking for a match. When it finds a match, CheckMenuItem is called to checkmark the item, the global variable which keeps track of the currently selected font is assigned the number of that item, and the for loop is exited.
The fifth block creates the Window menu using the Window Manager function CreateStandardWindowMenu. The accessor function SetMenuID sets the menu's ID and InsertMenu inserts the menu into the menu list. (Setting the menu ID is for illustrative purposes only because the ID is not used in this demonstration. Since the system handles the standard Window menu automatically, an ID is ordinarily only required for menu adjustment purposes when the menu has been customised.)
The sixth block inserts a further single submenu (to be attached to the second item in the Special menu) into the submenu portion of the menu list. GetNewMBar does not read in the resource descriptions of submenus, so the first step is to read in the 'MENU' resource with GetMenu. InsertMenu inserts a menu reference for this menu into the menu list at the location specified in the second parameter to this call. Using the constant hierMenu (-1) as the second parameter causes the menu to be installed in the submenu portion of the menu list.
The last line causes a checkmark to be set against the Size menu item corresponding to the initialised value of the global variable gCurrentFontSizeItem.
DrawMenuBar draws the menu bar.
Note that, in Carbon, the contents of the Apple Menu Items folder are automatically added to the Apple menu.
doEvents
doEvents switches according to the type of low-level or Operating System event received. Further processing is called for in the case of mouse-down or Command key equivalents, these being central to the matter of menu handling.
At the keyDown case, a check is made of the modifiers field of the event structure to establish whether the Command key was also pressed at the time. If so, menu enabling/disabling is attended to before the call to MenuEvent establishes whether the character code is associated with a currently enabled menu or submenu item in the menu list. If a match is found, MenuEvent returns a long integer containing the menu ID in the high word and the item number in the low word, otherwise it returns 0 in the high word. This long integer is then passed to the function doMenuChoice.
doMouseDown
doMouseDown first establishes the window and window part in which the mouse-down event occurred, and switches accordingly. This demonstration program is specifically concerned with mouse-downs in the menu bar and the content region of the window.
If the event occurred in this program's menu bar, menu enabling/disabling is attended to before the call to MenuSelect. MenuSelect tracks the user's actions until the mouse button is released, at which time it returns a long integer. If the user actually chose a menu item, this long integer contains the menu ID in the high word and the item number in the low word, otherwise it contains 0 in the high word. This long integer is passed to the function doMenuChoice.
If the mouse-down event occurred in the content region of the window, and if the window to which the mouse-down refers is not the front window, SelectWindow is called to effect basic window activation/deactivation.
doAdjustMenus
doAdjustMenus is called when a mouse-down occurs in the menu bar and when examination of a key-down event reveals that a menu item's keyboard equivalent has been pressed. No action is required in this simple program.
Later demonstration programs contain examples of menu adjustment functions.
doMenuChoice
doMenuChoice takes the long integer returned by the MenuSelect and MenuEvent calls, extracts the high word (the menu ID) and the low word (the menu item number) and switches according to the menu ID.
At the first two lines, the menu ID and the menu item number are extracted from the long integer. The next two lines will cause an immediate return if the high word equals 0, (meaning that either the mouse button was released when the pointer was outside the menu box or MenuEvent found no menu list match for the key pressed in conjunction with the Command key).
If the menu ID represents either the Font menu or one of the Font menu's submenus, the menu handling function doFontMenu is called. Otherwise, the function switches on the menu ID so that the appropriate individual menu handling function is called. Note the handling of the submenu attached to the second item in the Special menu (case mSubMenu).
The Window menu is handled automatically by the system.
MenuEvent and MenuSelect leave the menu title highlighted if an item was actually chosen. Accordingly, the last line unhighlights the menu title when the action associated with the user's drop-down menu choice is complete.
doAppleApplicationMenu
doAppleApplicationMenu takes the short integer representing the menu item. If this value represents the first item in the Mac OS 8/9 Apple menu or Mac OS X Application menu (the inserted "About..." item), text representing this item is drawn in the scrolling display.
If other items in the Mac OS 8/9 Apple menu are chosen, the system automatically opens the chosen object and passes control that object.
doFileMenu
doFileMenu handles choices from the File menu when the program is run on Mac OS 8/9. In this demonstration, only the Quit item is enabled, all other items having been disabled in the File menu's 'MENU' resource. When this item is chosen, the global variable gDone is set to true, causing termination of the program.
doEditMenu
doEditMenu switches according to the menu item number, drawing text representing the chosen item in the window.
doFontMenu
doFontMenu first gets a reference to the Font menu object. This, together with the menu item number, is passed in a call to the function GetFontFamilyFromMenuSelection. This function returns a reference to the font family and a value representing the font style. (A font family reference represents a collection of fonts with the same design characteristics, e.g., Arial, Arial Bold, Arial Italic, and Arial Bold Italic. Font style values are, for example, 0 = plain, 1 = bold, 2 = italic, 3 = bold italic).
The font family reference and the font style value are passed in calls to TextFont and TextFace, which will cause subsequent text drawing to be conducted in the specified font and style. The call to GetFontName gets the font's name from the font family reference and the function drawItemString draws that name in the window.
The remaining code is mainly concerned with checkmarking the newly chosen Font menu item and submenu item, and unchecking the previously chosen items.
If the menu ID represents the Font menu (meaning that a menu item without an attached submenu was chosen), the previously chosen item is unchecked, a global variable stores the item number of the newly-chosen item preparatory to the next call to doFontmenu, and the newly chosen item is checked. If a submenu item has previously been chosen, and thus checked, it is unchecked.
If, on the other hand, the menuID represents one of the Font menu's submenus:
If a submenu item has previously been chosen, that item is unchecked. A reference to the submenu object is assigned to a global variable, the menu item number is stored in another global variable preparatory to the next call to doFontmenu, and the newly chosen submenu item is checked.
The next two lines uncheck the previously checked Font menu item.
The for loop walks the Font menu looking for a match between item names and the font name previously extracted from the font family reference. When a match is found, the loop exits, the loop variable containing the item number where the match was found. This is stored in a global variable preparatory to the next call to doFontMenu, and is also passed in the call to CheckMenuItem to check that item.
The last block gets the style name from the menu object and draws that next to the font name in the window.
doSizeMenu
doSizeMenu switches according to the menu item chosen in the Size menu, sets the text size for all text drawing to that size, unchecks the current size item, and checks the newly chosen item. gCurrentSize is then set to the chosen menu item number before the function returns.
doSpecialMenu
doSpecialMenu handles a choice of the first item in the Special menu. Since the second item is the title of a submenu, only the first item is attended to in this function.
doSubMenus
doSubMenus switches according to the chosen item in the submenu attached to the second menu item in the Special menu.
drawItemString
The function drawItemString is incidental to the demonstration, being called by the menu selection handling functions to draw text in the application's window to reflect the user's menu choices.