Home > Articles > Home & Office Computing > Mac OS X

This chapter is from the book

This chapter is from the book

Handling Menu Choices

Determining the Menu ID and Menu Item—MenuSelect and MenuEvent

The first action your application should take when the user presses the mouse button while the cursor is in the menu bar is menu adjustment (that is, enabling or disabling menu items and adding or removing marks as required). Your application should then call MenuSelect. MenuSelect tracks the mouse, displays menus, highlights menu titles, displays and highlights enabled menu items, handles all user activity until the user releases the mouse button, and returns a long integer as its function result. The long integer contains the menu ID in the high word and the item number in the low word.

If some of your menu items have keyboard equivalents, your application should detect such key-down events. If an examination of the modifiers field of the event structure reveals that the Command key was down, your application should first adjust its menus and then call MenuEvent. MenuEvent scans the current menu list for a menu item that has a matching keyboard equivalent. Like MenuSelect, MenuEvent returns a long integer containing the menu ID and the item number.

If the user did not actually choose a menu command with the mouse, or if the user pressed a keyboard combination that did not map to a keyboard equivalent, MenuSelect and MenuEvent return 0 in the high word, the value in the low word being undefined.

Further Handling—Command IDs Not Used

The long integer returned by MenuSelect and MenuEvent should be passed as a parameter to a function that switches according to the menu ID in the high word and passes the low word to other functions that respond appropriately to that menu command.

Further Handling—Command IDs Used

Mac OS 8 introduced an alternative method of identifying, for the purposes of further handling, the menu item chosen by the user. This method assumes that you have previously assigned a unique value to your individual menu items via the command ID field of the 'xmnu' resource (or, programmatically, via calls to SetMenuItemCommandID).

Using this method, the menu ID and item number should be extracted from the long integer returned by MenuSelect and MenuEvent in the usual way. The menu ID should then be used in a call to GetMenuRef to get the reference to the menu's menu object. This reference and the menu item should then be used in a call to GetMenuItemCommandID, which returns the unique value that you previously assigned to the item (that is, the item's command ID). Your application should then switch according to that command ID, calling the other functions that respond appropriately to that menu command.

Unhighlighting the Menu Title

Recall that one of the actions of MenuSelect and MenuEvent is to highlight the menu title. Ordinarily, your application should not unhighlight the menu title (using HiliteMenu) until it performs the action associated with the menu command chosen by the user. However, if, in response to a menu command, your application displays a modal dialog containing an edit text item, you should unhighlight the menu title immediately so that the user can access the Edit menu.

Adjusting Menus

Menu adjustment should be on the basis of the type of window that is currently the frontmost window, for example, a text window, a modeless dialog, and so on. Accordingly, the menu adjustment function should first determine which window is the front window. The following are examples of menu adjustment functions:

void doAdjustMenus(void)
{
 WindowRef windowRef;
 SInt16 windowType;

 windowRef = FrontWindow();
 windowType = doGetWindowType(windowRef);

 switch windowType
 {
 case kMyDocWindow:
  doAdjustFileMenuForDocWindow();
  doAdjustEditMenuForDocWindow();
  // Adjust others.
  break;

 case kMyModelessDialogWindow:
  doAdjustMenusForModelessDialogs();
  break;

 case kNil:
  doAdjustMenusNoWindows();
  break;
 };

 DrawMenuBar;
}

void doAdjustFileMenuForDocWindow(void)
{
 MenuRef menuRef;

 menuRef = GetMenuRef(mFile);

 EnableMenuItem (menuRef,iNew);
 EnableMenuItem (menuRef,iOpen);
 DisableMenuItem(menuRef,iClose);
 DisableMenuItem(menuRef,iSave);
 DisableMenuItem(menuRef,iSaveAs);
 DisableMenuItem(menuRef,iPageSetup);
 DisableMenuItem(menuRef,iPrint);
 EnableMenuItem (menuRef,iQuit);
}

Handling Mac OS 8/9 Apple Menu Choices

When the user chooses an item in the Mac OS 8/9 Apple menu, MenuSelect returns the menu ID of your application's Apple menu in the high word and the item number in the low word.

If your application provides an About command as the first menu item in the Apple menu, and the user chooses this item, you should display the About dialog/alert. Other choices from the Mac OS 8/9 Apple menu are handled automatically by the system.

Handling Mac OS X Application Menu Choices

About Command

When the user chooses the About command in the Mac OS X Application menu, MenuSelect returns the menu ID of your application's Application menu in the high word and 1 in the low word. Thus the code that handles the user's choice of the Apple menu's About item on Mac OS 8/9 will also handle the user's choice of the Application menu's About item on Mac OS X.

Quit Command

When the user chooses the Quit command from the Mac OS X Application menu, a high-level event (more specifically, an Apple event) known as a Quit Application event is sent to your application. Your application must support the Quit Application event in order to respond to the user choosing the Quit command (see Chapter 10).

Preferences... Command

An Apple Event, known as the Show Preferences event, is sent to your application when the user chooses the Preferences... command from the Mac OS X Application menu. Your application must support the Show Preferences event in order to respond to the user choosing the Preferences...command. (See the demonstration program in Chapter 19.)

Handling a Size Menu

Preamble

On Mac OS 8/9, font sizes in Size menus should be outlined to indicate which sizes are directly provided by the current font. For bitmapped fonts, you should outline only those sizes that exist in the Fonts folder. For TrueType fonts, all sizes supported by that font should be outlined. The current font size should be indicated with a checkmark. If the current selection contains more than one font size, a dash should be placed next to each font size in the selection.

Size menus should, in addition to displaying available font sizes, provide an Other command to enable the user to specify a size not currently listed in the menu. When the user chooses the Other command, the current font size should be displayed in a dialog which allows the user to enter the desired font size. If the user chooses a size not already in the menu, a checkmark should be added to the Other menu item and the chosen size should be added in parenthesis to the text of the Other command.

Handling the Menu Choice

The following is an example function that handles a user's choice of an item in a Size menu:

void doHandleSizeCommand(SInt16 menuItem)
{
 SInt16 numItems;
 Boolean addItem;
 SInt32 sizeChosen;

 numItems = CountMenuItems(GetMenuHandle(mSize));
 if(menuItem == numItems)   // If user chose Other, display dialog. If the
 {         // user-specified size is not in the menu, add a
 doDisplayOtherBox(sizeChosen); // checkmark to the Other command and add the new
 }         // font size to the text of the Other command.
 else        // Return sizeChosen.
 {         // User chose a size.
 doRemoveMarksFromSizeMenu(); // Remove marks from item/s showing previous size.
 CheckMenuItem(GetMenuHandle(mSize),menuItem,true); // Add mark to chosen item.
 sizeChosen = doItemToSize(menuItem);  // Convert item number to font size.
 }
 doResizeSelection(sizeChosen);    // Update document state or user selection.
}
  • + Share This
  • 🔖 Save To Your Account