Moon Travel Planner: Modifying the Menu Bar

The procedures for modifying the menu bar in Interface Builder are similar to those you used to modify the main window in Chapter 4:

  • Drag an object from the Menus palette to the menu bar to create new menus and menu items.

  • Use the Info window to change menu item attributes and controls.

  • Double-click an item and type to change its name.

In this section, you’ll modify the menu bar so it’s customized for the Moon Travel Planner application. You’ll do the following:

  1. Disable the items you won’t need.

  2. Set up the items the application needs.

  3. Add an application-specific menu to the menu bar.

  4. Add code to your project to create the menu bar.

  5. Test the menus.

Disable the Items You Won’t Need

Let’s take a look at the default menu bar and disable any items you won’t need for the Moon Travel Planner application. The application won’t provide the ability for users to create new files, so you can disable the New menu item in the File menu. Users will be able to choose Open, Close, and Save As commands from the File menu. But you’ll disable Revert and Save. The Moon Travel Planner won’t provide any editing capabilities, so you’ll disable the items in the Edit menu.

Open the menu bar by clicking main.nib in the Resources group of the Moon Travel Planner project, then double-click the MainMenu icon in the Instances pane.

Disable each unneeded item by following these steps:

  1. Click a menu (such as File) to open it.

  2. Click the item in the menu you want to disable.

  3. Choose Show Info from the Tools menu.

  4. Choose Attributes from the pop-up menu.

  5. Make sure Enabled is not selected, as shown in Figure 7.12. When an item is not enabled, it appears dimmed to the user.

    Enabled is not selected for unneeded menu items

    Figure 7-12. Enabled is not selected for unneeded menu items

Set Up the Items You Know You’ll Need

You must assign a command to every menu item your application makes available to the user. A command must:

  • Consist of four characters

  • Have at least one uppercase character (Apple reserves the use of commands that are all lowercase characters.)

  • Be unique within your application

When a user chooses a menu item from a menu, the Menu Manager issues the command you assigned to it. The Carbon Event Manager passes the command to your application to handle. Then, your application calls the function that handles it.

It takes two steps to get a menu item working for your application: assigning the command and writing the code to handle it. In this section you’ll add commands for each menu item you’ll use in the Moon Travel Planner. You’ll write the code to process commands in other parts of this book.

Note

As you assign a command to a menu item, make a note of it. You’ll need to refer to a command when you write the code to handle it.

Add an About command

The About command will open a window that displays version and copyright information for the application.

  1. Click the NewApplication menu, then double-click the About NewApplication menu item.

  2. Type About Moon Travel Planner.

  3. Choose Show Info from the Tools menu.

  4. Choose Attributes from the pop-up menu.

  5. Assign aBtb as the command. In the Command section, type aBtb in the text field (as shown in Figure 7.13) and press Return.

    A command for the About menu item

    Figure 7-13. A command for the About menu item

Add an Open Itinerary command

The Open command will allow users to open a file that contains a suggested itinerary for a trip to the moon. Because the user will only be able to open files that have a special itinerary type, you should first change the name of the Open menu item to Open Itinerary, then add a command.

  1. Click the File menu, then double-click the Open menu item.

  2. Type Open Itinerary... and press Return. The ellipsis indicates to the user that more information is required to carry out the command. In this case, the ellipsis indicates a dialog will appear.

  3. Assign oPit as the command. From the pop-up menu at the top of the Menu Item Info window, choose Attributes. In the Command section, type oPit in the text field, and press Return.

Add Commands for Close and Save As

Users should be able to close and open an itinerary, or save an itinerary under a different file name. Add these commands to the Close and Save As menu items:

  • Enter cLse as the Close menu item’s command.

  • Enter sAit as the Save As menu item’s command.

Add Commands for Page Setup and Print

In Chapter 8 you’ll create a window (Facts for the Traveler) that displays facts for those considering travel to the moon. You should add page setup and print commands to allow users to print the facts. Add these commands to the Page Setup and Print menu items:

  • Enter pGsu as the Page Setup menu item’s command.

  • Enter pRnt as the Print menu item’s command.

Delete the Help Menu

In Chapter 12 you’ll add a help book to the Moon Travel Planner application, write code that registers the help book, and set properties that specify the help book name and folder. Mac OS X automatically sets up a Help menu and names the Help menu item based on the properties you set. In our case, the Help menu item will be named Moon Travel Planner Help. The operating system also handles the command issued when the user chooses Moon Travel Planner Help from the Help menu. So there is no need for us to use the Help menu provided by Interface Builder. You must delete it, otherwise your application will have two Help menus:

  1. Click the Help menu.

  2. Press Delete.

Add an Application-Specific Menu to the Menu Bar

Not everything an application needs is in the default menu bar; you usually need to add at least one application-specific menu. You can find help for designing an application-specific menu in Inside Mac OS X: Aqua Human Interface Guidelines.

You already know from the Moon Travel Planner specification (see Chapter 2) that you need to add an application-specific menu called Moon. The Moon menu should have two menu items—Compute Travel Time and Show Facts. The Compute Travel Time menu item will issue the same command issued by the Compute Travel Time button in the main window. The Show Facts menu item will issue a command that opens a window to display enticing facts about the moon as a tourist destination.

You’ll use the Menus palette to add a new menu to the default menu bar:

  1. In Interface Builder, choose Palettes from the Tools menu, then click the Menus icon (the circled icon in Figure 7.14) in the palette’s toolbar.

    The Menus palette

    Figure 7-14. The Menus palette

  2. Drag a Submenu object from the Menu palette to the space between the Edit and Window menus, as shown in Figure 7.15.

    A submenu item inserted in the menu bar

    Figure 7-15. A submenu item inserted in the menu bar

  3. Name the menu Moon. Double-click the word Submenu, type Moon, and press Return.

  4. Name the Item Compute Travel Time. Double-click the Item menu item, type Compute Travel Time, and press Return.

  5. Assign Command-K as the Compute Travel Time menu item’s keyboard equivalent. Click the menu item and choose Show Info from the Tools menu, then type K in the Key text field of the Menu Shortcut box as shown in Figure 7.16.

    The Compute Travel Time menu item and Info window

    Figure 7-16. The Compute Travel Time menu item and Info window

  6. Enter tRav as the menu item’s command. Type tRav in the Command text field of the Menu Item Info window and press Return. When the user chooses Compute Travel Time, the Carbon Event Manager sends the tRav command to your application. In Chapter 6 you wrote the handler that takes care of the tRav command issued by the Compute Travel Time button in the main window. Note the menu item uses the same command. As long as the main window is active when the user chooses the Compute Travel Time item from the Moon menu, the handler takes care of the command. It doesn’t matter whether the menu or the button issues the command.

  7. Add a new item to the Moon menu. Drag an Item object from the Menu palette to the space below the Compute Travel Time menu item, as shown in Figure 7.17. When the Item is over a “legal” spot, the pointer changes to show a small + sign and a line appears at the location where the Item is placed.

    Adding a new menu item to a menu

    Figure 7-17. Adding a new menu item to a menu

  8. Name the menu item Show Facts. Double-click the Item menu item, type Show Facts, and press Return. In most cases, a menu item should be written as an action so it’s clear to the user what happens when the item is chosen. Although the window’s title is Facts for the Traveler, we’ve named the menu item Show Facts.

  9. Assign Shift-Command-F as the Show Facts menu item’s keyboard equivalent. Click the Show Facts menu item, then type Shift-Command-F in the Key text field of the Menu Shortcut box.

    Note

    You should assign Shift-Command-F rather than Command-F, because in some applications, Command-F is used for the Find command. Although you could use this equivalent here because the Moon Travel Planner application doesn’t have a Find command, it’s not recommended. Using common keyboard equivalents in an uncommon way could make your application less intuitive for users accustomed to the commonly used combinations.

  10. Enter fAct as the menu item’s command. Type fAct in the Command text field of the Menu Item Info window and press Return.

  11. Save the nib file. Make sure your changes are saved before you add code to the Moon Travel Planner project.

Add Code to Your Project

You need to add code to the main function of the Moon Travel Planner project to create the menu bar from the nib file you modified and to handle the commands issued by menu items.

Create the menu bar

Project Builder already provides code that creates the menu bar from the nib file when your application launches. Let’s take a look at that code:

  1. Make Project Builder active by clicking its icon in the Dock.

  2. Click main.c in the Groups & Files list.

  3. Look at the two lines of code in the main function in the main.c file, just after the CreateNibReference statement. The Interface Builder Services function SetMenuBarFromNib takes two parameters, a nib reference and a CFString object. You pass the nib reference returned from the function CreateNibReference (see Chapter 3). You use the function CFSTR to convert the string “MainMenu” to a CFString object. The string you provide must match exactly the name of the menu object in the Instances pane of Interface Builder. SetMenuBarFromNib reads the menu bar definition from the nib file, allocates memory for it, creates it, and sets it as the current menu bar. The function require_noerr exits the application if there is an error that prevents the menu bar from being created from the nib file.

Handle menu commands

Although there are a number of commands that need to be handled by your application, you won’t write the code for them in this chapter. You’ll do that throughout the book, as follows:

  • About Moon Travel Planner (aBtb). See Chapter 10.

  • Open Itinerary (oPit). See Chapter 11.

  • Close (cLse). You’ll write code to close the itinerary window in Chapter 11, the Facts for the Traveler window in Chapter 8, and the About window in Chapter 10.

  • Save Itinerary As (sAit). See Chapter 11.

  • Page Setup (pGsu). You already set up handling this command in Chapter 6 but in Chapter 9 you’ll write the functions that do page setup.

  • Print (pRnt). You already set up handling this command in Chapter 6 but in Chapter 9 you’ll write the functions that do the printing.

  • Compute Travel Time (tRav). You already set this up in Chapter 6.

  • Show Facts (fAct). See Chapter 8.

In the meantime, you can build and run the application, but the commands that aren’t yet handled won’t do anything.

Test the Menus

Let’s build and run the application to make sure the Moon menu looks okay and the Compute Travel Time menu item works:

  1. Click the Build button in the upper-left corner of the Moon Travel Planner project window.

  2. Click the Run button in the upper-left corner of the project window.

  3. Click a mode of transportation.

  4. Choose Compute Travel Time from the Moon menu. Does the value in the Travel Time field change? If so, you are ready to move on!

  5. Choose Quit Moon Travel Planner from the Moon Travel Planner menu.

Recap

You’ve taken a look at the default menu bar provided by Interface Builder and at the items in each of the menu bar’s menus. You’ve seen that some items, such as edit items, come “prewired” with commands, while other items you need to set up for your application. If an application needs menus or menu items other than the default ones, you can create new ones using the Menu palette. It’s also possible to disable menu items your application doesn’t need.

You created a new menu and menu items and adjusted the default menu items for the Moon Travel Planner application. You also saw that a command issued by a button in the interface can also be issued from a menu item (Compute Travel Time). Next you’ll take a look at localization and, in particular, how to set up an application so it’s easy to make available in multiple languages or for multiple locations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.181.47