PP Commanders
Volume Number: 12
Issue Number: 2
Column Tag: Getting Started
PowerPlant and Commanders 
By Dave Mark, MacTech Magazine Regular Contributing Author
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
This month, we’re going to explore a brand new aspect of PowerPlant: the concept of
commands, commanders, and the LCommander PowerPlant class. PowerPlant
commands are similar to messages. You’ve already seen how messages are sent from a
broadcaster (such as a button) to all listeners registered to listen to that broadcaster.
The command model is slightly different.
Commands are associated with menus and keyDown events. To respond to menu
selections and user keystrokes, you’ll need to create a class derived (at least in part)
from the LCommander class. There are three key functions you’ll inherit and override
from the LCommander base class.
• HandleKeyPress() - receives an EventRecord containing a character typed by
the user.
• ObeyCommand() - receives a command number associated with a specific menu
command. When we create this month’s project, you’ll see how to associate a
command number with a specific menu item.
• FindCommandStatus() - gives your LCommander subclass a chance to update
(i.e. enable, disable, check, uncheck, change item text) the status of the menu
item associated with a specified command.
Basically, PowerPlant handles the administrative work of keeping track of which
menu items need to be enabled, which pane should receive which event, etc. Every
PowerPlant application has a chain of command. The chain (really a tree) starts
with the LApplication object and flows downward through other objects that handle
commands to the panes that will become the targets of the commands. Think of the
current target as the application’s current focus. If a keystroke is entered, the
corresponding keyDown event will be sent to the current target pane (perhaps a
window, perhaps a textEdit pane within the window).
As you’ll see in this month’s program, you’ll have a little setup work to do, and
then you’ll override the three LCommander functions described above. That’s pretty
much it. Of course, as you get deeper into PowerPlant you’ll discover that there is
much more you can do, but for now, concentrate on understanding the basics.
A Sneak Preview of BeepCommander
This month’s program is called BeepCommander. It features a single window type
that responds to keyDowns by displaying the typed character in the window. Figure 1
shows a BeepCommander window.
Figure 1. A BeepCommander window.
BeepCommander also features a menu named Special with a single item named Beep. When you select Beep, your computer beeps (gasp!). The sneaky thing is, the
Beep item is only enabled when the letter ‘x’ is typed. I know, I know, weird user
interface. That’s fine. The point is to show the relationship between menus,
keystrokes and the LPane and LCommander functions you’ll be overriding.
Let’s get started.
Create a New Project
The first thing you’ll need to do is create a new project, based on the PowerPlant
stationery.
• Create a new folder called BeepCommander.
• Launch CodeWarrior and create a new project named BeepCommander.µ.
• In the project window, double-click on the file .rsrc.