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

This chapter is from the book

This chapter is from the book

Creating Your Application's Menus

'MBAR', 'MENU', and 'xmnu' Resources

As stated in Chapter 1, you can provide a textual, formal description of resources in a file and then use a resource compiler such as Rez to compile the description into a resource, or you can create resource descriptions using a resource editor such as Resorcerer. This book assumes the use of Resorcerer.

When creating resources using Resorcerer, it is advisable that you refer to a diagram and description of the structure of the resource and relate that to the various items in the Resorcerer editing windows. Accordingly, the following describes the structure of those resources associated with the creation of menus.

Structure of a Compiled 'MBAR' Resource

Fig 5 shows the structure of a compiled 'MBAR' resource. The number of menu resource IDs should match the number of menus declared in the first two bytes.

Figure 3.5 Structure of a compliled MBAR resource.

Structure of a Compiled 'MENU' Resource

Fig 6 shows the structure of a compiled 'MENU' resource (and its variable length data) and how it "feeds" the menu object.

Figure 3.6 Structue of a compiled menu ("menu") resource and its variable length data.

The following describes the main fields of the 'MENU' resource:

Field

Description

MENU ID

The menu's unique identification number. Note that the number assigned to the menu ID and the resource ID do not have to be identical, though it is advisable that these numbers be the same. A menu ID from 1 to 235 indicates a menu (or submenu) of an application. Apple reserves the menu ID of 0.

PLACEHOLDER FOR MENU WIDTH PLACEHOLDER FOR MENU HEIGHT

After reading in the resource data, the Menu Manager requests the menu's MDEF to calculate the width and height of the menu and store these values in the menu object.

RESOURCE ID OF MENU DEFINITION FUNCTION

If the integer 63 appears here, the standard MDEF will be used. The MDEF is read in after the menu's resource data is read in. The Menu Manager stores a handle to the MDEF in the menu object.

INITIAL ENABLED STATE OF THE MENU AND MENU ITEMS

A value whose bits indicate if the corresponding menu item is enabled or disabled, with bit 0 indicating whether the menu as a whole is enabled or disabled.

VARIABLE LENGTH DATA THAT DEFINES THE MENU ITEMS

The Menu Manager stores the variable length data for each menu item at the end of the menu object (see Fig 6).


The following describes the main fields of the variable length data for each menu item. Note that various alternatives apply to the icon number, keyboard equivalent, and marking character fields. For example, a menu item can have a keyboard equivalent or a submenu, but not both.

Field

Description

ICON NUMBER, SCRIPTCODE, OR 0

ICON NUMBER A number from 1 to 255 (or from 1 to 254 for small or reduced icons). If the menu item specifies an icon, you must provide a 'cicn' (color icon) or 'ICON' resource with a resource ID equal to the icon number plus 256. If you want an 'ICON' resource to be reduced to the size of a small icon ('SICN'), or if you want the size of a 'cicn' resource reduced by half, assign the value 0x1D to the keyboard equivalent field. If you want a 'SICN' resource, assign the value 0x1E. The Menu Manager looks first for a 'cicn' resource with the calculated resource ID. In the Carbon era, color icons are much to be preferred. SCRIPTCODE (Not used when the 'MENU' resource is extended with an 'xmnu' resource) Specify the script code here if you want the item's text to be drawn in a script other than the system script, and also provide 0x1C in the keyboard equivalent field. When the 'MENU' resource is extended by an 'xmnu' resource, the script code should be set in the text encoding field of the 'xmnu' resource. 0 Specifies that the menu item does not contain an icon and uses the system script.

KEYBOARD EQUIVALENT, 0X1B, 0X1C,

KEYBOARD EQUIVALENT 0X1D, OX1E, OR 0 Specified as a one-byte character and, actually, a Command-key equivalent only. The Command-key equivalent can be extended with modifier key (Shift, Option, Control) constants in the modifier keys field of the extended menu ('xmnu' ) resource (see below). 0x1B Specifies that the menu item has a submenu. (The menu ID of the submenu should be assigned to the marking character field (see below).) 0x1C (Not used when the 'MENU' resource is extended with an 'xmnu' resource) Specifies that the item uses a script other than the system script. (The script code should be assigned to the icon number field (see above).) When the 'MENU' resource is extended by an 'xmnu' resource, the script code should be set in the text encoding field of the 'xmnu' resource. 0x1D For menu items containing icons, causes 'ICON' resources to be reduced to the size of a small icon, or 'cicn' resources to be reduced by half. 0x1E Specifies that you want the Menu Manager to use a small icon ('SICN') resource for the item's icon. (The small icon's resource ID should be assigned to the icon number field (see above).) 0 Specifies that the menu item has neither a keyboard equivalent nor a submenu and uses the system script.

MARKING CHARACTER, MENU ID OF

MARKING CHARACTER SUBMENU, OR 0 Special characters, such as the checkmark and diamond characters are available to indicate the marks associated with a menu item. MENU ID OF SUBMENU Submenus of an application must have menu IDs from 1 to 235. Submenus of a driver must have menu IDs from 236 to 255. 0 Specifies that the item has neither a mark nor a submenu.

FONT STYLE OF THE MENU ITEM

Specifies whether the font style of the menu item should be plain, or any combination of bold, italic, outline, and shadow.


NOTE

As the user traverses menu items, if an item has a submenu, the MenuSelect function looks in the submenu portion of the menu list for the submenu. It then searches for a menu with a defined menu ID that matches the menu ID specified by the hierarchical menu item. If it finds a match, it attaches the submenu to the menu item.

Structure of a Compiled 'xmnu' Resource

The 'xmnu' resource provides for the additional features, for example, support for extended modifier keys, command IDs, and so on. Fig 7 shows the structure of a compiled 'xmnu' resource and an individual menu item entry in that resource.

The following describes the fields of a compiled 'xmnu' resource:

Field

Description

VERSION NUMBER

Version of the resource.

NUMBER OF ENTRIES

Number of entries (extended menu item structures) in the resource.

FIRST EXTENDED MENU ENTRY

A number of extended menu item structures (see fig 7)

.... LAST EXTENDED MENU ENTRY

 


Figure 3.7 Structure of a compiled extended menu ("xmnu") resource and an extended menu item entry.

Each entry in a 'xmnu' resource corresponds to a menu item. The following describes the main fields of an extended menu item entry.

Field

Description

TYPE

Specifies whether there is extended information for the item. 1 indicates that there is extended information for the item, causing the rest of the entry to be read in. 0 indicates that there is no information for the item, causing the Menu Manager to skip the rest of the entry.

COMMAND ID

A unique value used to identify the menu item (as opposed to referring to the item using the menu ID and item number). This value may be ascertained via a call to GetMenuItemCommandID. A command ID may be assigned to a menu item programmatically via a call to SetMenuItemCommandID.

MODIFIER KEYS

Specifies the modifier keys used in a keyboard equivalent to select a menu item. The current modifier keys may be ascertained via a call to GetMenuItemModifiers. Modifier keys may be assigned to a menu item programmatically via a call to SetMenuItemModifiers.

ICON TYPE PLACEHOLDER

(Reserved. Set to 0.)

ICON HANDLE PLACEHOLDER

(Reserved. Set to 0.)

TEXT ENCODING

Specifies the text encoding for the menu item text. This field of the 'xmnu' resource should be used instead of setting the keyboard equivalent field in the 'MENU' resource to 0x1C and the icon number field to the script code. If you want to use the system script, assign -1. If you want to use the current script, assign -2. The current text encoding may be ascertained via a call to GetMenuItemTextEncoding. Text encoding may be assigned to a menu item programmatically via a call to SetMenuItemTextEncoding.

REFERENCE CONSTANT

Any value an application wants to store. The current value may be ascertained via a call to GetMenuItemRefCon. Reference constants may be assigned to a menu item programmatically via a call to SetMenuItemRefCon.

REFERENCE CONSTANT

Any additional value an application wants to store. The getter and setter functions relating to this second reference constant are not available in Carbon. If you wish to associate data with a menu item you should use the functions which are available for that purpose (see "Associating Data With Menu Items").

MENU ID OF SUBMENU

A value between 1 and 235, identifying the submenu. The current submenu ID may be acertained via a call to GetMenuItemHierarchicalID. The menu ID of a submenu may be assigned to a menu item programmatically via a call to SetMenuItemHierarchicalID. This, in effect, attaches a submenu to the menu item.

FONT ID

The ID of the font family. If this value is 0, then the large system font ID (Mac OS 8/9) or system font (Mac OS X) is used. The current font ID may be acertained via a call to GetMenuItemFontID. The font ID of a menu item may be set programmatically via a call to SetMenuItemFontID.

KEYBOARD GLYPH

A symbol representing a menu item's modifier key. The current keyboard glyph may be ascertained via a call to GetMenuItemGlyph. If the value in this field is zero, the keyboard glyph uses the keyboard font. (A glyph is a visual representation of a character.) You can override the character code to be displayed with a substitute glyph by assigning a non-zero value to this field. The keyboard glyph of a menu item may be set programmatically via a call to SetMenuItemKeyGlyph.


The information in an 'xmnu' resource is set for specified menu items; it is not necessary to create an extended menu entry for all menu items in a menu.

It is not necessary to provide 'xmnu' resources if your application's menus do not require the additional features provided by this resource.

Creating 'MBAR', 'MENU', and 'xmnu' Resources Using Resorcerer

As previously stated, when creating resources using Resorcerer, it is advisable that you refer to a diagram and description of the structure of the resource and relate that to the various items in the Resorcerer editing windows. The following assumes that approach.

Creating 'MBAR' Resources

Fig 8 shows an 'MBAR' resource containing seven menus being created with Resorcerer. The first three entries would be, respectively, the Apple, File, and Edit menus.

Figure 3.8 Creating an 'MBAR' resource using resorcerer

Creating 'MENU' Resources

Fig 9 shows an imaginary View menu with the Full Screen menu item being edited. This menu item has been assigned a keyboard equivalent (more specifically, a Command-key equivalent); accordingly, the Key Equiv: radio button has been clicked and the character F has been entered as the Command-key equivalent. The menu item has also been assigned a marking character (a checkmark).

Figure 3.9 Editing a 'MENU' resource using resorcerer

Fig 10 shows the same View menu with the Floating Palettes menu item being edited. This item has a submenu; accordingly, the Sub-menu ID radio button has been clicked and the resource ID of the submenu's 'MENU' resource has been entered. The item also has an icon provided by a 'CICN' or 'cicn' resource with a resource ID of 257.

Figure 3.10 Further editing of a 'MENU' resource using resorcerer.

Creating 'MENU' Resources for Submenus

Fig 11 shows the Line and Fill submenu item in the submenu attached to the Floating Palettes menu item being edited. This item has a marking character (a checkmark), an icon provided by an 'ICON' or 'cicn' resource with resource ID 258, and a Command-key equivalent.

Figure 3.11 Editing a 'MENU' resource for a submenu using resourcerer.

Creating 'xmnu' Resources

Fig 12 shows an 'xmnu' resource being created using Resorcerer. This 'xmnu' resource extends the 'MENU' resource with resource ID 133 (the View menu, above). Menu item 4 has been assigned a command ID, and the Command-key equivalent assigned to this item in the 'MENU' resource (Command-F) has been extended to the keyboard equivalent Command-Shift-F by specifying the Shift key as an extended modifier.

Figure 3.12 Creating an 'xmnu' resource using resorcerer.

Creating the Menu Bar and Pull-Down Menus

The function GetNewMBar, which itself calls GetMenu, should be used to read in the 'MBAR' resource and its associated 'MENU' resources. After reading in a 'MENU' resource, GetMenu looks for an 'xmnu' resource with the same resource ID and reads it in if found. GetNewMBar creates a menu object for each menu and inserts each menu into the menu list.

SetMenuBar should then be used to set the current menu list as the menu list created by your application. A call to DrawMenuBar completes the process by drawing the menu bar, displaying all the menu titles in the current menu list.

Deleting the Quit Command

As previously stated, your application must include a Quit command in the File menu when your application is run on Mac OS 8/9 but not when it is run on Mac OS X. Accordingly, you must conditionalize your code so as to ensure that the Quit command and its preceding divider are deleted when your application is run on Mac OS X. The methodology recommended by Apple is as follows:

SInt32 response;
MenuRef menuRef;

Gestalt(gestaltMenuMgrAttr,&response);
if(response & gestaltMenuMgrAquaLayoutMask)
{
 menuRef = GetMenuRef(mFile);
 if(menuRef != NULL)
 {
 DeleteMenuItem(menuRef,iQuit);
 DeleteMenuItem(menuRef,iQuit - 1);
 }
}

Creating a Hierarchical Menu

GetNewMBar does not read in the resource descriptions of submenus but simply records the menu ID of any submenu in the menu object. Submenu descriptions are read in with GetMenu and the submenu is inserted in the current menu list using InsertMenu, with the constant hierMenu passed as the second parameter to that call.

NOTE

As the user traverses menu items, if an item has a submenu, the MenuSelect function looks in the submenu portion of the menu list for the submenu. It then searches for a menu with a defined menu ID that matches the menu ID specified by the hierarchical menu item. If it finds a match, it attaches the submenu to the menu item.

Carbon Note

In Carbon, calling GetMenu twice on the same resource ID will create two independent, unique menus. (In Classic, the second call to GetMenu returns the same MenuHandle as the first call.) Thus, to prevent memory leaks in Carbon, GetMenu should not be called a second time on the same resource ID without an intervening call to DisposeMenu.

Adding Menus to the Menu List

A menu may be added to the current menu list using one of the following procedures:

  • Read the relevant 'MENU' resource in with GetMenu, add it to the current menu list with InsertMenu, and update the menu bar with DrawMenuBar.

  • Use NewMenu to create a new empty menu, use AppendMenu, InsertMenuItem, InsertResMenu, or AppendResMenu to fill the menu with menu items, add the menu to the current menu list using InsertMenu, and update the menu bar using DrawMenuBar.

Note that GetMenuRef may be used to obtain a reference to the menu object of any menu in the current menu list.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020