Implementing the About Panel in MathPaper

In this section, we’ll start implementing the animated About panel that we discussed earlier in the MathPaper application. The first thing we’ll do is to add a new module for the About panel’s nib file and arrange for the nib to be loaded automatically the first time the user chooses the MathPaper About MathPaper menu command. (Recall that it’s more efficient to use a separate nib module for an About panel, as we did in our four-function Calculator in Chapter 6. Then the nib needs to be loaded into memory only if it’s used, and About panels aren’t used very often.)

  1. Open MathPaper.pbproj in Project Builder.

  2. Double-click on MainMenu.nib in the Groups & Files pane of PB’s main window to open this nib file in IB.

  3. Choose IB’s File New menu command, select Cocoa Empty in the Starting Point panel, and then click the New button.

  4. Choose IB’s File Save menu command and save the new nib with the name AboutPanel.nib in your ~/MathPaper/English.lproj folder (not the ~/MathPaper folder). When prompted, add this file to the MathPaper project, as shown in Figure 14-2.

Adding AboutPanel.nib to the MathPaper project

Figure 14-2. Adding AboutPanel.nib to the MathPaper project

Note that the File’s Owner of the MainMenu.nib file is an instance of the NSApplication class (to see this in IB, click MainMenu.nib’s File’s Owner icon and type Command-1). Also note that by default, the MathPaper About MathPaper menu command sends the orderFrontStandardAboutPanel: action message to the File’s Owner — that is, the NSApplication object. (To see this in IB, choose the MathPaper About MathPaper menu item, type Command-2, and then single-click the orderFrontStandardAboutPanel: method with the dimple.)

NSApplication’s orderFrontStandardAboutPanel: method actually invokes the orderFrontStandardAboutPanelWithOptions: method. This causes the standard About panel to appear and display the following: credits taken from the file called Credits.rtf; the ApplicationName and ApplicationIcon taken from the application’s main bundle; and the version, copyright, and other information taken from the application property list.

This is all useful behavior for a default About panel. To customize it, however, we’ll need to create our own subclass of the NSApplication class, change the application configuration so that MathPaper is built using our subclass, and override the orderFrontStandardAboutPanel: method.

First we’ll create the NSApplication subclass called MathApplication. Then we’ll add an outlet called aboutPanel that will eventually be set to point to our customized About panel:

  1. Click the File’s Owner icon in the MainMenu.nib window in IB, and then click the Classes tab in the same window. The NSApplication class should be selected. (If it is not selected, use the Search field to find it and then select it.)

  2. Choose IB’s Classes Subclass NSApplication menu command, and you’ll see the new subclass, called MyApplication, in the MainMenu.nib window.

  3. Change the subclass’s name from “MyApplication” to “MathApplication”.

  4. Type Command-1 to bring up the MathApplication Class Info dialog and add an outlet called aboutPanel to the class, as shown in Figure 14-3.

MathApplication subclass of NSApplication with new aboutPanel outlet

Figure 14-3. MathApplication subclass of NSApplication with new aboutPanel outlet

  1. Choose IB’s Classes Create Files for MathApplication menu command and browse to your ~/MathPaper folder in the resulting Save panel.

  2. Make sure that all three checkboxes are selected so that IB will save the MathApplication.h and MathApplication.m files in the ~/MathPaper folder and add them to the MathPaper target (see Figure 14-4). Click the Choose button.

Creating MathApplication class files and adding them to the MathPaper target

Figure 14-4. Creating MathApplication class files and adding them to the MathPaper target

  1. Back in PB, click the Targets vertical tab, the MathPaper target, and finally the Application Settings tab.

  2. Under the Cocoa-Specific section, change the Principal class from NSApplication to MathApplication, as shown in Figure 14-5.

Changing MathPaper’s principal class to MathApplication

Figure 14-5. Changing MathPaper’s principal class to MathApplication

Warning

If you don’t set the class of the File’s Owner to MathApplication, the Application Kit will not instantiate a MathApplication object, and you will see the standard Cocoa About panel.

Now let’s make sure that both the MainMenu.nib and AboutPanel.nib files know about the new MathApplication class.

  1. Back in IB, click the File’s Owner icon under the Instances tab in the MathPaper.nib window.

  2. Type Command-5 and change the File’s Owner’s class from NSApplication to MathApplication (two classes above NSApplication).

  3. Type Command-S to save the MathPaper.nib file.

Because our MathApplication subclass was created in MainMenu.nib, it isn’t yet “known” to AboutPanel.nib. We’ll change that in the next two steps:

  1. Click anywhere in the AboutPanel.nib window to select the nib.

  2. Go out to the Finder and select the MathApplication.h file icon in your ~/MathPaper folder.

  3. Drag the MathApplication.h file icon from the Finder and drop it in the AboutPanel.nib window. (This is an alternative to choosing Classes Read MathApplication.h.) The MathApplication class should now show up as a subclass of NSApplication in the AboutPanel.nib window.

  4. Click the Instances tab and then select the File’s Owner icon in the AboutPanel.nib window.

  5. As you did in MainMenu.nib, change the class of the File’s Owner icon to MathApplication.

Now we need to create the empty About panel itself:

  1. Choose IB’s Tools Palettes Show Palettes menu command to make sure that the Palettes window is visible.

  2. Click the Cocoa-Windows button in the Palettes window toolbar to select the Cocoa-Windows palette (which can be seen in Figure 14-6).

Connecting the File’s Owner to the About MathPaper panel

Figure 14-6. Connecting the File’s Owner to the About MathPaper panel

  1. Drag the Panel icon from the Cocoa-Windows palette and drop it on the desktop. You should see a new (big) panel where you dropped the icon and a new Panel icon in the AboutPanel.nib window.

  2. Move this new panel to the center of the screen (where About panels should open) and resize it so that it’s about the same size as the panel with the title “About MathPaper” in Figure 14-6.

  3. With the new panel selected, type Command-1 to display the Window Info inspector.

  4. Change the title of the window from “Panel” to “About MathPaper”.

  5. Now Control-drag a connection from the File’s Owner icon to the Panel icon in the AboutPanel.nib window (or drag directly to the About MathPaper panel itself ).

  6. Double-click the aboutPanel outlet to complete the connection, as shown in Figure 14-6.

  7. Back in PB, double-click the MathApplication.m file under the Classes group and add the orderFrontStandardAboutPanel: method (shown here in bold) that we discussed earlier:

    #import "MathApplication.h"
    
    @implementation MathApplication
    - (void)orderFrontStandardAboutPanel:(id)sender
                         {
                             if (aboutPanel == nil) {
                                 [NSBundle loadNibNamed:@"AboutPanel" owner:self];
                             }
                             [aboutPanel makeKeyAndOrderFront:self];
                         }
    
    @end
  8. Build and run MathPaper. Save all files when prompted.

  9. With MathPaper running, choose MathPaper About MathPaper and make sure that the new About panel appears in the middle of the screen (where you left it in IB).

  10. Quit MathPaper.

It is very important that your MathPaper application display an empty About panel before you go further, because our animation will get complicated. If your application does not display an About panel, go back and check your work.

Creating the MathAnimation View

MathPaper’s animation subsystem will consist of four parts:

  • An initialization method that sets up the whole thing

  • A timer that “ticks” every 30th of a second and causes the animation to advance to the next frame

  • A method that knows how to advance the state variables used to keep track of the animation

  • A method that knows how to draw the current state of the MathPaper animation based on the values of the state variables

All of this logic will be encapsulated within a new subclass of the NSView class, which we’ll call MathAnimation:

  1. Back in IB, select the NSView class under the Classes tab in the AboutPanel.nib window.

  2. Choose IB’s Classes Subclass NSView menu command to create a new subclass called “MyView”.

  3. Change the name of “MyView” to “MathAnimation”.

  4. Type Command-1 and add the new action method called tick: to the MathAnimation class, as shown in Figure 14-7.

Adding the tick: action method to the MathAnimation class

Figure 14-7. Adding the tick: action method to the MathAnimation class

  1. Choose IB’s Classes Create Files for MathAnimation menu command to create the two MathAnimation class files. Insert them to the MathPaper target, and click Choose.

  2. Still in IB, click the Cocoa-Containers button in the Palettes window toolbar to select the Cocoa-Containers palette (shown in Figure 14-8).

  3. Drag the CustomView icon from the Cocoa-Containers palette and drop it inside the About MathPaper panel.

  4. Resize the CustomView so that it fills the About MathPaper panel.

  5. Type Command-1 and change the class of the CustomView from NSView to MathAnimation, as shown in Figure 14-8.

Changing the CustomView class to MathAnimation

Figure 14-8. Changing the CustomView class to MathAnimation

  1. Type Command-S to save the AboutPanel.nib file.

The MathAnimation view in the About panel will draw the four distinct elements shown in Figure 14-1:

  • The application’s name, MathPaper

  • The five lines underneath the application’s name

  • The pulsating icon

  • The spinning star

Drawing each of these elements requires use of a different part of the Quartz API. To do all this, we’ll first need to learn more about Quartz. (Quartz, as you probably know, is also the name of a very hard mineral — fortunately, using Mac OS X’s Quartz isn’t hard at all!)

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

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