- 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 Menus2 Comments
When this program is run, the user should choose Show Balloons from the Help menu and make menu choices from all menus, including the Apple menu. Choices should be made using the mouse and, where appropriate, the keyboard equivalents. The user should note:
The extended modifier keys assigned to the last two items in the Style menus.
The Command-key equivalents assigned to the items in the Size menu. (These are, in order, delete-to-the-left key, delete-to-the-right key, page-up key, and page-down key.)
That the Font menu is WYSIWYG.
That the marking character column has been deleted from the Special menu and the menu items in this menu are drawn in the Gadget font (assuming it is available).
That the items in the submenu attached to the second item in the Special menu have color icons.
The balloon help provided for all menus and menu items.
The Menus2 demonstration program package also includes a demonstration of Apple Help, including the methodology used to create an item in the Mac OS 8/9 Help menu. The Apple Guide file titled "Menus Guide", which will cause a "Menus Help" item to be created in the Mac OS 8/9 Help menu, should be retained in the same folder as the Menus2 application. An alias of the folder titled "Menus Help" should be placed in the Help folder in the System Folder (Mac OS 8/9) and in the user's Help folder (~/Library/Documentation/Help) (Mac OS X). You will then be able to access the help content by choosing Menus Help from the Help menu.
The help content does not provide user assistance for Menus2 programs as such. Rather, it provides a brief description of how to provide user assistance for your application using Apple Help.
Because this demonstration program is based on Menus1, the following comments exclude those for the functions that remain unchanged.
The calls to RGBBackColor and RGBForeColor set the window background and foreground colors to, respectively, dark blue and white.
doGetMenus sets up the menu bar and the various menus.
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.) SetMenuBar makes the newly created menu list the current list.
The next block utilizes the Menu Manager function CreateStandardFontMenu in the creation of a non-hierarchical Font menu. Following the call to CreateStandardFontMenu, the process of making the menu WYSIWYG begins. The call to CountMenuItems returns the number of items in the menu. Then, for each of these items, GetMenuItemText gets the font's name, GetFNum gets the font number associated with the font name, and SetMenuItemFontID sets the font for the menu item. In the following if block, the current item is checkmarked if the item name equals the name of the small system font, and the global variable which keeps track of the currently selected font is assigned the item number.
The next block programmatically assigns extended modifier keys to the Outline and Shadow items in the Style (Programmatic) menu. The SetMenuItemModifiers calls assign Shift-Option-Control to the Outline item and Shift-Option to the Shadow item. (The extended modifier keys for the same two items in the Style ('xmnu') menu are assigned in the associated 'xmnu' resources.)
The next block inserts the application's single submenu into the submenu portion of the menu list and programmatically attaches it to the Special menu's second menu item. 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 object 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 call to GetMenuRef gets a reference to the Special menu, which is used in the following call to SetMenuHierarchicalID to attach the submenu to the second item in the Special menu.
The following rather large block programmatically assigns command IDs to all items in the Style (Programmatic), Size, and Special menus and the submenu. (Command IDs for the File and Style ('xmnu') menus are assigned in the associated 'xmnu' resources. It is not possible to assign command IDs to the items in the Font menu.) The Command IDs are defined in the four-character-code format, which packs four one-byte characters together in a 32-bit value. For example, 'plai' expressed as hexadecimal is 0x706C6169. 70 is the ASCII code for p, 6C is the ASCII code for l, and 69 is the ASCII code for i.
The following block programmatically assigns a color icon to the second item in the submenu. The call to GetCIcon creates a CIcon data structure and initializes it from data read in from the specified 'cicn' resource. The handle to this structure is then passed as the last parameter in the SetMenuItemIconHandle, the third parameter specifying that the type of icon is a color icon. (The color icon for the first item in the submenu is assigned in the associated 'xmnu' resource.)
The next block programmatically assigns command-key equivalents to the items of the Size menu. (Because the keys assigned are the two delete keys and the page-up and page-down keys, it is not possible to make these assignments within the 'MENU' resource.) Also, a substitute glyph must be assigned, otherwise the correct glyphs will not be displayed. The calls to SetItemCmd assign the specified key to the menu item, and a substitute glyph is assigned via calls to SetMenuItemGlyph. If this is not done, the glyphs displayed will not be the correct visual representations of the keys. (These substitute glyphs could also have been specified in the keyboard glyph fields for these items in the menu's 'xmnu' resource.)
In the next block, SetMenuExcludesMarkColumn is called to delete the marking character column from the Special menu and SetMenuFont is called to set the font for the menu items in this menu to Gadget (assuming that font is present).
In the next block, and only if the program is running on Mac OS X, HMGetHelpMenu is called to create a Help menu, InsertMenuItem is called to insert a single item in that menu, and SetMenuItemCommandID assigns a command ID to that item.
The next block sets checkmarks against the appropriate font, style and size menu items according to the initialized values of the associated global variables.
The call to DrawMenuBar draws the menu bar
Note that, in Carbon, the contents of the Apple Menu Items folder are automatically added to the Apple menu.
doMenuChoice extracts the menu ID and menu item number from the long integer returned by the MenuSelect and MenuEvent calls. An immediate return is made if the high word equals 0. The function "special cases" the Font menus, calling the function for handling choices from that menu. Otherwise, GetMenuItemCommandID is called. GetMenuItemCommandID returns zero as the function result if the call is successful, and a pointer to an integer representing the value of the item's command ID will be returned in the third parameter. If the call is successful, and if a zero is not returned in the third parameter, a command ID exists for the item. Accordingly, the command ID is passed in a call to the function doCommand.
MenuSelect and MenuEvent 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.
doCommand handles choices from those menus whose items have command IDs.
Note that the initial handling of all of the remaining menu items, regardless of which menu they belong to, is attended to within the one switch in the one function. The responses to the user choosing the various menu items is the same as in Menus1, except that the code relating to checkmarking the Style menu items has been added and the code for checkmarking the Size menu items and storing the current size has been divided between this function a further handling function (doCheckSizeMenuItem).
At the block titled Style ('xmnu') and Style (Programmatic) menu, bits in the global variable gCurrentStyle are set or unset according to the font styles selected. The code reflects the fact that Bold, Italic, Underline, Outline and Shadow style selections are additive, not mutually exclusive, and that a selection of Plain must unset all bits in gCurrentStyle. The code also reflects the requirement that, except in the case of the Plain item, the selection of a checked item must cause that item to be unchecked, and vice versa. With gCurrentStyle set, the function doCheckStyleMenuItem is called to check/uncheck the relevant menu items as appropriate.
Note that the handling of the two submenu items has been changed to make the items mutually exclusive.
The 'help' command ID case applies only when the program is run on Mac OS X. The function AHGoToPage is called to deliver a request to load the specified HTML file in the specified Help book folder to the Help Viewer application.
doCheckStyleMenuItem is called from doMenuChoice when an item in the Style menu is chosen. With the appropriate bit settings of gCurrentStyle attended to within doMenuChoice, a reference to the Style menu object is obtained. This is required for the six CheckMenuItem calls, which check or uncheck the individual menu items according to whether the third parameter evaluates to, respectively, true or false.
The call to TextFace sets the style for subsequent text drawing. The last line draws some text to prove that the desired effect was achieved.
1 -Applicable only to menus without 'xmnu' resources. When 'xmnu' resources are used, use SetMenuItemHierarchicalID to attach a submenu to a menu item.
2 -When 'xmnu' resources are used, use SetMenuItemModifiers to set the extended modifier keys (Shift, Option, Control).
3 -Applicable only to menus without 'xmnu' resources. When 'xmnu' resources are used, do not use SetItemCmd to specify a script code. Use SetMenuItemTextEncoding.