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

This chapter is from the book

This chapter is from the book

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.

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