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.)
Open MathPaper.pbproj
in Project Builder.
Double-click on MainMenu.nib
in the Groups &
Files pane of PB’s main window to open this nib file
in IB.
Choose IB’s File → New menu command, select Cocoa → Empty in the Starting Point panel, and then click the New button.
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.
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:
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.)
Choose IB’s Classes → Subclass
NSApplication menu command, and you’ll see the new
subclass, called MyApplication, in the
MainMenu.nib
window.
Change the subclass’s name from “MyApplication” to “MathApplication”.
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.
Choose IB’s Classes → Create Files
for MathApplication menu command and browse to your
~/MathPaper
folder in the resulting Save panel.
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.
Back in PB, click the Targets vertical tab, the MathPaper target, and finally the Application Settings tab.
Under the Cocoa-Specific section, change the Principal class from NSApplication to MathApplication, as shown in Figure 14-5.
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.
Back in IB, click the File’s Owner icon under the
Instances tab in the MathPaper.nib
window.
Type Command-5 and change the File’s Owner’s class from NSApplication to MathApplication (two classes above NSApplication).
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:
Click anywhere in the AboutPanel.nib
window to
select the nib.
Go out to the Finder and select the
MathApplication.h
file icon in your
~/MathPaper
folder.
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.
Click the Instances tab and then select the File’s
Owner icon in the AboutPanel.nib
window.
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:
Choose IB’s Tools → Palettes → Show Palettes menu command to make sure that the Palettes window is visible.
Click the Cocoa-Windows button in the Palettes window toolbar to select the Cocoa-Windows palette (which can be seen in Figure 14-6).
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.
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.
With the new panel selected, type Command-1 to display the Window Info inspector.
Change the title of the window from “Panel” to “About MathPaper”.
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 ).
Double-click the aboutPanel
outlet to complete the
connection, as shown in Figure 14-6.
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
Build and run MathPaper. Save all files when prompted.
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).
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.
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:
Back in IB, select the NSView class under the Classes tab in the
AboutPanel.nib
window.
Choose IB’s Classes → Subclass NSView menu command to create a new subclass called “MyView”.
Change the name of “MyView” to “MathAnimation”.
Type Command-1 and add the new action method called tick: to the MathAnimation class, as shown in Figure 14-7.
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.
Still in IB, click the Cocoa-Containers button in the Palettes window toolbar to select the Cocoa-Containers palette (shown in Figure 14-8).
Drag the CustomView icon from the Cocoa-Containers palette and drop it inside the About MathPaper panel.
Resize the CustomView so that it fills the About MathPaper panel.
Type Command-1 and change the class of the CustomView from NSView to MathAnimation, as shown in Figure 14-8.
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!)
52.15.80.101