| Framework | Carbon/Carbon.h |
| Declared in | Menus.h |
You can use the Menu Manager to create and manage the menus in your application. Menus allow the user to view or choose from a list of choices and commands that your application provides. All Mac OS applications should provide these standard menus: the Application menu, the File menu, and the Edit menu.
Carbon supports the Menu Manager, with the following changes:
Your application must use the functions defined by the Menu Manager whenever it creates and disposes of Menu Manager data structures. Some applications, for example, create menus by using the Resource Manager function GetResource (instead of the Menu Manager function GetMenu) and dispose of them by calling ReleaseResource instead of DisposeMenu. This practice is not supported in Carbon.
The MenuInfo structure is opaque in Carbon. You must revise your application so that it accesses Menu Manager data structures only through accessor functions.
Menu color information tables are not recommended in Carbon.
You are strongly encouraged to adopt the standard Mac OS menu definition functions (also known as MDEFs) in your application. Your menus will then inherit the systemwide appearance and, furthermore, take advantage of other Menu Manager enhancements planned for the future. If you decide to customize the appearance of a menu and you are deploying your application for Mac OS X v10.3 and later, you should use a custom HIView instead of a custom menu definition function.
Function descriptions are grouped by the tasks for which you use the functions. For an alphabetical list of functions, go to the API index at the end of the document.
CreateNewMenu
DuplicateMenu
DisposeMenu
CreateCustomMenu
RegisterMenuDefinition
SetMenuDefinition
GetMenuDefinition
CreateStandardFontMenu
UpdateStandardFontMenu
ReleaseMenu Deprecated in Mac OS X v10.5
RetainMenu Deprecated in Mac OS X v10.5
HideMenuBar
ShowMenuBar
IsMenuBarVisible
FlashMenuBar
SetMenuBar
GetMenuBar
DrawMenuBar
InvalMenuBar
GetMBarHeight
DuplicateMenuBar
DisposeMenuBar
InsertMenu
DeleteMenu
ClearMenuBar
GetMenuRetainCount Deprecated in Mac OS X v10.5
GetMenuRef
GetMenuHandle
SetMenuTitleWithCFString
CopyMenuTitleAsCFString
GetMenuType
SetMenuTitleIcon
GetMenuTitleIcon
SetMenuID
GetMenuID
LMGetTheMenu
SetMenuHeight
GetMenuHeight
SetMenuWidth
GetMenuWidth
CalcMenuSize
IsMenuSizeInvalid
GetMenuAttributes
ChangeMenuAttributes
IsValidMenu
SetMenuFlashCount Deprecated in Mac OS X v10.5
HiliteMenu
UpdateInvalidMenuItems
InvalidateMenuEnabling
InvalidateMenuItems
InvalidateMenuSize
IsMenuBarInvalid
IsMenuItemInvalid
EraseMenuBackground Deprecated in Mac OS X v10.5
ScrollMenuImage Deprecated in Mac OS X v10.5
SetMenuItemProperty
SetMenuCommandProperty
GetMenuItemProperty
GetMenuCommandProperty
GetMenuItemPropertySize
GetMenuCommandPropertySize
RemoveMenuItemProperty
RemoveMenuCommandProperty
GetMenuItemPropertyAttributes
ChangeMenuItemPropertyAttributes
SetMenuItemRefCon
GetMenuItemRefCon
CountMenuItems
CountMenuItemsWithCommandID
EnableMenuItem
EnableMenuCommand
DisableMenuItem
DisableMenuCommand
EnableAllMenuItems
DisableAllMenuItems
IsMenuItemEnabled
IsMenuCommandEnabled
MenuHasEnabledItems
EnableMenuItemIcon
DisableMenuItemIcon
IsMenuItemIconEnabled
SetMenuItemTextWithCFString
CopyMenuItemTextAsCFString
AppendMenuItemTextWithCFString
InsertMenuItemTextWithCFString
GetMenuFont
SetMenuFont
GetMenuItemFontID
SetMenuItemFontID
GetFontFamilyFromMenuSelection
SetItemStyle
GetItemStyle
GetMenuItemTextEncoding Deprecated in Mac OS X v10.5
SetMenuItemTextEncoding Deprecated in Mac OS X v10.5
SetMenuItemIconHandle
GetMenuItemIconHandle
SetItemMark
SetMenuCommandMark
GetItemMark
GetMenuCommandMark
CheckMenuItem
SetMenuItemIndent
GetMenuItemIndent
SetMenuItemKeyGlyph
GetMenuItemKeyGlyph
SetMenuItemModifiers
GetMenuItemModifiers
SetMenuItemCommandID
GetMenuItemCommandID
SetMenuItemHierarchicalMenu
GetMenuItemHierarchicalMenu
SetMenuItemCommandKey
GetMenuItemCommandKey
GetIndMenuItemWithCommandID
SetMenuItemData
CopyMenuItemData
GetMenuItemAttributes
ChangeMenuItemAttributes
GetItemIcon Deprecated in Mac OS X v10.5
GetMenuItemHierarchicalID Deprecated in Mac OS X v10.5
SetItemIcon Deprecated in Mac OS X v10.5
SetMenuItemHierarchicalID Deprecated in Mac OS X v10.5
IsMenuKeyEvent
GetMenuTrackingData
CancelMenuTracking
MenuSelect
MenuChoice
IsShowContextualMenuEvent
ContextualMenuSelect
PopUpMenuSelect
GetItemCmd
GetMenu
GetMenuExcludesMarkColumn
GetNewMBar
IsShowContextualMenuClick
MenuEvent
SetItemCmd
SetMenuExcludesMarkColumn
AppendMenu Deprecated in Mac OS X v10.5
AppendMenuItemText Deprecated in Mac OS X v10.5
AppendResMenu Deprecated in Mac OS X v10.5
DeleteMCEntries Deprecated in Mac OS X v10.5
DisposeMCInfo Deprecated in Mac OS X v10.5
DisposeMenuDefUPP Deprecated in Mac OS X v10.5
GetMCEntry Deprecated in Mac OS X v10.5
GetMCInfo Deprecated in Mac OS X v10.5
GetMenuItemText Deprecated in Mac OS X v10.5
GetMenuTitle Deprecated in Mac OS X v10.5
InitContextualMenus Deprecated in Mac OS X v10.5
InsertFontResMenu Deprecated in Mac OS X v10.5
InsertIntlResMenu Deprecated in Mac OS X v10.5
InsertMenuItem Deprecated in Mac OS X v10.5
InsertMenuItemText Deprecated in Mac OS X v10.5
InsertResMenu Deprecated in Mac OS X v10.5
InvokeMenuDefUPP Deprecated in Mac OS X v10.5
MenuKey Deprecated in Mac OS X v10.5
NewMenu Deprecated in Mac OS X v10.5
NewMenuDefUPP Deprecated in Mac OS X v10.5
ProcessIsContextualMenuClient Deprecated in Mac OS X v10.5
SetMCEntries Deprecated in Mac OS X v10.5
SetMCInfo Deprecated in Mac OS X v10.5
SetMenuItemText Deprecated in Mac OS X v10.5
SetMenuTitle Deprecated in Mac OS X v10.5
Get the menu whose contents are displayed in the menubar.
MenuRef AcquireRootMenu ( void );
The current root menu. See page for a description of the MenuRef data type. If no root menu currently exists, the Menu Manager creates one and returns its menu reference.
This function increments the reference count of the root menu. The caller should call ReleaseMenu when done with the menu.
Menus.h
Appends a new menu item with text from a CFString.
OSStatus AppendMenuItemTextWithCFString ( MenuRef inMenu, CFStringRef inString, MenuItemAttributes inAttributes, MenuCommand inCommandID, MenuItemIndex *outNewItem );
The menu to which to append the new item.
The text of the new item.
The attributes of the new item.
The command ID of the new item.
On exit, the index of the new item. May be NULL if the caller does not need this information.
A result code. See “Menu Manager Result Codes.”
The Menu Manager will make its own copy of the CFString before returning from this function. Modifying the string after calling AppendMenuItemTextWithCFString will have no effect on the menu item's actual text. The caller may release the string after the call.
Menus.h
Recalculates the horizontal and vertical dimensions of a menu.
void CalcMenuSize ( MenuRef theMenu );
The menu whose dimensions need recalculating.
The CalcMenuSize function uses the menu definition function of the specified menu to calculate the dimensions of the menu. In most cases, your application does not need to use the CalcMenuSize function.
Menus.hCancels menu tracking.
OSStatus CancelMenuTracking ( MenuRef inRootMenu, Boolean inImmediate, UInt32 inDismissalReason );
The root menu of the tracking session to dismiss. For menu bar tracking, pass the result from AcquireRootMenu; for popup menu tracking, pass the menu that was passed to PopUpMenuSelect.
Pass true if you want the open menus to disappear immediately, false if you want them to fade out.
Why the menu is being dismissed. This value is passed in the kEventMenuEndTracking event. You can pass the constants in “Menu Dismissal Constants.” If you pass zero here, the kEventMenuEndTracking event contains kMenuDismissedByCancelMenuTracking.
A result code. See “Menu Manager Result Codes.”
Menus.h
Changes the attributes of a menu.
OSStatus ChangeMenuAttributes ( MenuRef menu, MenuAttributes setTheseAttributes, MenuAttributes clearTheseAttributes );
The menu whose attributes you want to change.
The attributes to add to the menu. See “Menu Attribute Constants” for a list of possible values.
The attributes to remove from the menu. See “Menu Attribute Constants” for a list of possible values.
A result code. See “Menu Manager Result Codes.”
Menus.h
Changes the attributes of a menu item.
OSStatus ChangeMenuItemAttributes ( MenuRef menu, MenuItemIndex item, MenuItemAttributes setTheseAttributes, MenuItemAttributes clearTheseAttributes );
The menu.
The index of the menu item.
The attributes to add to the menu item. See “Menu Item Attribute Constants” for a list of possible values.
The attributes to remove from the menu item.
A result code. See “Menu Manager Result Codes.”
Menus.h
Changes the attributes of a menu item property.
OSStatus ChangeMenuItemPropertyAttributes ( MenuRef menu, MenuItemIndex item, OSType propertyCreator, OSType propertyTag, OptionBits attributesToSet, OptionBits attributesToClear );
The menu containing the menu item whose properties you want ot change.
The index of the menu item.
The creator code of the property.
The property tag.
The attributes to add to the menu item property.
The attributes to remove from the menu item property.
A result code. See “Menu Manager Result Codes.”
Currently you can only specify the kMenuPropertyPersistent attribute (See “Menu Item Property Attribute Constant”), which currently has no effect on Mac OS X. Therefore, SetMenuItemPropertyAttributes is not useful at this time.
Menus.h
Adds or removes a check mark from a menu item.
void CheckMenuItem ( MenuRef theMenu, short item, Boolean checked );
The menu containing the menu item to check or uncheck.
The menu index of the item to check or uncheck.
Pass true to add a check, false to remove it.
You can also add or remove a check mark using the SetItemMark function.
Menus.h
Deletes all menus from the current menu list.
void ClearMenuBar ( void );
The ClearMenuBar function deletes all menus from the current menu list. ClearMenuBar decrements the reference count of each menu in the menu list; if the reference count reaches zero, the memory associated with the menu is released. To explicitly release the memory occupied by a menu, use DisposeMenu.
Menus.h
An application-defined callback function that examines the context in a contextual menu CFPlugin.
OSStatus CMPluginExamineContext ( void *thisInstance, const AEDesc *inContext, AEDescList *outCommandPairs );
The instance of this plugin.
The Apple event descriptor describing the selection that invoked the contextual menu.
On return, outCommandPairs points to an array of Apple event descriptors, each of which contains information about a menu item to display in the contextual menu.
A result code. See “Menu Manager Result Codes.”
Your contextual menu plugin must implement this function to determine what menu items to display given the user selection. The Apple event descriptor for each menu item contains the information to display in the item (such as text) and a command ID.
Menus.h
An application-defined callback function that handles menu item selection in a contextual menu CFPlugin.
OSStatus CMPluginHandleSelection ( void *thisInstance, AEDesc *inContext, SInt32 inCommandID );
The instance of this plugin.
The Apple event descriptor describing the selection that invoked the contextual menu.
The command ID associated with the user’s menu item selection.
A result code. See “Menu Manager Result Codes.”
Your contextual menu plugin must implement this function to handle a user’s selection of an item in the contextual menu. The command ID indicates which of the choices you gave in CMPluginExamineContext the user selected, so you can use that and the user selection stored in the inContext parameter to execute the conxtexual menu selection appropriately.
Menus.h
An application-defined callback function that handles any post-selection cleanup in a contextual menu CFPlugin.
void CMPluginPostMenuCleanup ( void *thisInstance );
The instance of this plugin.
Your contextual menu plugin must implement this function to handle any necessary cleanup required after displaying the contextual menu. If no cleanup is needed, you can simply return from this function.
Menus.h
Displays a contextual menu.
OSStatus ContextualMenuSelect ( MenuRef inMenu, Point inGlobalLocation, Boolean inReserved, UInt32 inHelpType, ConstStr255Param inHelpItemString, const AEDesc *inSelection, UInt32 *outUserSelectionType, MenuID *outMenuID, MenuItemIndex *outMenuItem );
The menu containing application commands to display. The caller creates this menu based on the current context, the mouse location, and the current selection (if it was the target of the mouse). If you pass NULL, only system commands are displayed.
The location (in global coordinates) of the mouse near which the menu is to be displayed.
Reserved for future use. Pass false for this parameter.
An identifier specifying the type of help provided by the application; see “Contextual Menu Help Type Constants.”
The string containing the text to be displayed for the help menu item. This string is unused unless you also pass the constant kCMOtherHelp in the inHelpType parameter.
A pointer to an object specifier for the current selection. This allows the system to examine the selection and add special system commands accordingly. Passing a value of NULL indicates that no selection should be examined, and most likely, no special system actions will be included.
A pointer to an unsigned 32-bit value. On return, the value indicates what the user selected from the contextual menu; see “Contextual Menu Selection Type Constants” for a list of possible values.
A pointer to a signed 16-bit value. On return, if outUserSelectionType is set to kCMMenuItemSelected, the value is set to the menu ID of the chosen item.
A pointer to an unsigned 16-bit value. On return, if outUserSelectionType is set to kCMMenuItemSelected, the value is set to the index of the menu item chosen.
A result code. See “Menu Manager Result Codes.” Returns userCanceledErr and sets outUserSelectionType to kCMNothingSelected if the user selects an item that requires no additional actions on your part.
If your application uses the standard window handler, you may want to install handlers for the kEventWindowContextualMenuSelect or kEventControlContextualMenuClick events and call ContextualMenuSelect from within your handler. The standard window handler automatically detects contextual menu clicks and sends the kEventWindowContextualMenuSelect and kEventControlContextualMenuClick events.
If the IsShowContextualMenuEvent function returns true or you receive the appropriate contextual menu Carbon event, you should call the ContextualMenuSelect function after generating your own menu and preparing an Apple Event descriptor ( AEDesc) that describes the item for which your application is displaying a contextual menu. This descriptor may contain an object specifier or raw data and will be passed to all contextual menu plug-ins.
The system will add other items before displaying the contextual menu, and it will remove those items before returning, leaving the menu in its original state.
After all the system commands are added, the contextual menu is displayed and tracked. If the user selects one of the system items, it is handled by the system and the call returns as though the user didn’t select anything from the menu. If the user selects any other item (or no item at all), the Menu Manager passes back appropriate values in the parameters outUserSelectionType, outMenuID, and outMenuItem.
Your application should provide visual feedback indicating the item that was clicked upon. For example, a click on an icon should highlight the icon, while a click on editable text should not eliminate the current selection.
If the outUserSelectionType parameter contains kCMMenuItemSelected, you should look at the outMenuID and outMenuItem parameters to determine what menu item the user chose and handle it appropriately. If the outUserSelectionType parameter contains kCMShowHelpSelected, you should open the proper help sequence.
Menus.h
Obtains multiple menu item attributes at once.
OSStatus CopyMenuItemData ( MenuRef inMenu, MenuItemID inItem, Boolean inIsCommandID, MenuItemDataPtr ioData );
The menu whose attributes you want to get. Note that if you pass true for the inIsCommandID parameter, you can pass NULL here, in which case the Menu Manager searches the root menu for the first menu that matches the specified command ID.
The menu item index or the command ID of the menu item.
A Boolean value indicating whether the value passed for the inItem parameter is a command ID or a menu item index. Pass true to indicate a command ID, false to indicate that it is a menu item index. If you pass true, the Menu Manager returns the data for the first menu item that matches the specified command ID.
A pointer to a MenuItemDataRec structure. Before calling, you should set the whichData field to indicate what data you want to obtain. (Individual fields may also require initialization before calling.) On return, the structure contains the data you requested. For more details on the types of data you can obtain, see “Menu Item Data Flags.”
A result code. See “Menu Manager Result Codes.”
You can use this function to obtain multiple menu item attributes simultaneously, which is often more efficient than making several different calls. For example, a menu definition function could use CopyMenuItemData to obtain all the individual attributes necessary for drawing a menu all at once.
This function returns copies of the data in the menu, so you should release any data in the MenuItemDataRec structure that was allocated dynamically (such as the CFString item text).
Menus.h
Copies menu items from one menu to another.
OSStatus CopyMenuItems ( MenuRef inSourceMenu, MenuItemIndex inFirstItem, ItemCount inNumItems, MenuRef inDestMenu, MenuItemIndex inInsertAfter );
The menu from which to copy items.
The first item to copy.
The number of items to copy.
The menu to which to copy items.
The menu item in the destination menu after which to insert the copied items. Pass zero to insert items at the beginning of the menu. Note that you cannot specify an index value greater than the number of items in the destination menu.
A result code. See “Menu Manager Result Codes.”
Menus.h
Returns a CFString containing the text of a menu item.
OSStatus CopyMenuItemTextAsCFString ( MenuRef inMenu, MenuItemIndex inItem, CFStringRef *outString );
The menu containing the item.
The item whose text you want to copy.
On exit, a CFString containing the item's text. The caller must release this string when it is no longer needed.
A result code. See “Menu Manager Result Codes.”
Menus.h
Returns a CFString containing the title of a menu.
OSStatus CopyMenuTitleAsCFString ( MenuRef inMenu, CFStringRef *outString );
The menu whose title you want to obtain.
On exit, a CFString containing the menu's title. This string must be released by the caller.
A result code. See “Menu Manager Result Codes.”
Menus.h
Obtains the number of menu items in a menu
UInt16 CountMenuItems ( MenuRef theMenu );
The menu whose items you want to count.
The number of menu items in the menu.
Menus.h