TweetFollow Us on Twitter

PowerPlant
Volume Number:11
Issue Number:9
Column Tag:Getting Started

PowerPlant

By Dave Mark, MacTech Magazine Regular Contributing Author

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

This month, we’re going to take a look at PowerPlant, the framework included on every CodeWarrior CD. If you own Symantec C++, take heart: we’ll get to the TCL in a future column. In the meantime, follow along anyway. Who knows, you might find yourself making the switch to PowerPlant someday.

This Month’s Program

I don’t know about you but, after four month’s straight of menus, I’m ready for something else. So instead of talking about PowerPlant and menu handling (we’ll do that in a future column), this month’s program will introduce PowerPlant’s messaging system.

As you design your PowerPlant programs, you’ll work with objects called broadcasters and listeners. A broadcaster sends a message and a listener receives that message. In this month’s program, we’ll create a window containing a button. Each time the button is clicked, it will send a message to any objects registered as listeners to it. Basically, this means that the broadcasting objects’ BroadcastMessage() member function calls the listening object’s ListenToMessage() member function.

This mechanism is simpler than it sounds. As we build our project, just remember that the button is a broadcaster, and that the class CDashboardApp will be the listener.

If you have the CW6 documentation, you might want to read the section named LBroadcaster & LListener in the PowerPlant manual (chapter 8, page 93).

As is usually the case when working with a framework, we’ll take an existing PowerPlant example and modify it to suit our needs.

• Duplicate the folder “CodeWarrior6:Metrowerks PowerPlant:More PowerPlant Examples:Dashboard Starter”.

If you run one of the projects in this folder (either Dashboard68K.µ or DashboardPPC.µ), you’ll see the window shown in Figure 1. To quit the program, select Quit from the File menu. As you can see, Quit is the only item in the File menu. In a future column, we’ll add some menus and items to a PowerPlant program. For now, let’s add a button to the Dashboard window.

Figure 1. The Dashboard Starter window, before our modifications.

Editing Dashboard.PPob

CodeWarrior comes with a ResEdit-like program, named Contructor, that lets you create and edit PowerPlant-specific resources. The resource we’re interested in in this column is the PPob resource. A PPob resource is like a combination of a DLOG and DITL, but for any PowerPlant view including windows and dialogs. We’ll use Constructor to add a button to the Dashboard window.

CodeWarrior 6 ships with two different versions of Constructor. Though both will do the job, Constructor 2.0a16 is far newer than Constructor 1.0.1 and seems pretty stable for an alpha release. The screen shots and instructions in this column were all based on 2.0a16. You’ll find both versions in the CodeWarrior 6 folder, inside the Metrowerks PowerPlant subfolder.

• Launch Constructor 2.0a16 and open the file Dashboard.PPob (it’s in the same folder as the two Dashboard project files).

As you can see by the Constructor window shown in Figure 2, the file Dashboard.PPob already contains a resource. This PPob resource represents the main Dashboard window. It has a resource ID of 200 and represents an object belonging to the class LWindow.

Figure 2. The Constructor window listing the view resources in Dashboard.PPob.

Our next step is to edit this PPob resource.

• Double-click on the LWindow PPob with an id of 200.

When you double-click on PPob 200, a PPob editing window will appear (see Figure 3) showing the object view hierarchy defined by this PPob. Right now, the PPob consists of a single window.

Figure 3. A Constructor window showing PPob 200.

If you click on the window view and select Pane Information... from the Pane menu (or just double-click on the window view), a pane info window will appear (see Figure 4) allowing you to edit the selected view. In this case, the selected view describes a document window with a zoom box, no close box, positioned automatically in the Alert position on the main screen, etc. Feel free to edit this view if you like. To test your changes, quit Constructor, saving your changes, then use CodeWarrior to rerun the project.

Figure 4. The Pane Info window describing the Dashboard window.

Figure 5. The items you can place in a window using Constructor.

With the PPob 200 window in front, you should notice a palette window listing all the items you can place in a PPob view. The palette, shown in Figure 5, works just like ResEdit’s DITL palette. To add an item to a view, drag the item off the palette into the PPob window.

Here’s where we’ll add the button to the window.

• Drag an LStdButton off of the palette into the window view in the PPob 200 window.

• Double-click on the button that appears and edit the info window to match the one shown in Figure 6.

Figure 6. The info window for the LStdButton we added to our window view.

There are three important changes to make in the LStdButton info window. First, change the Pane ID: field to read 1000 (be sure the Text ID checkbox is unchecked before you change the ID). The Pane ID serves to identify the button pane from all the other panes in the PPob resource. By convention, number your panes starting at 1000 and moving upwards from there. For example, if you added three items to the Dashboard window represented by PPob 200, you’d set their Pane IDs to 1000, 1001, and 1002.

The second change to make to the LStdButton info window is to fill in the Button Title: field. Since we want our button to beep, the word Beep will make a fine button title.

The third change is to the Value Message: field. This field contains the message that will get sent when the button is clicked. The message is an integer constant that will be passed as a parameter to any objects registered as listeners to the broadcasting button. Again, by convention, we’ll number our messages starting at 1000.

That’s it. Save your changes and quit Constructor.

Dashboard68K.µ or DashboardPPC.µ

If you haven’t already, open up one of the Dashboard projects (either Dashboard68K.µ or DashboardPPC.µ). Since we want to add a button to our window, we’ll need to add the files that contain the PowerPlant classes that implement push buttons: LControl.cp and LStdControl.cp.

• In the project window, click on the triangle to the left of the group named Pane. When we add the two files, we want to add them to this group.

• Add the files LControl.cp and LStdControl.cp to the project. You’ll find them in the folder “CodeWarrior 6:Metrowerks C/C++:PowerPlant Libraries:Pane Classes”

CDashboardApp.h

• Open the file CDashboardApp.h.

• Add this line after the #include of <LApplication.h>:

#include <LListener.h>

• Change the first line of the CDashboardApp class to look like this:

class CDashboardApp : public LApplication, public LListener {

To convert the CDashboardApp class into a listener, we have to make sure it is derived from the class LListener. Deriving a class from more than one class is perfectly acceptable in C++ and is known as multiple inheritence.

Another step in making the CDashboardApp class a listener is to add a member function named ListenToMessage(). ListenToMessage() will get called when any broadcaster it is listening to broadcasts a message.

• Add this line after the definition of the member function FindCommandStatus():

 virtual void    ListenToMessage(MessageT inMessage, void *ioParam);

• Close CDashboardApp.h and save your changes.

CDashboardApp.cp

Here’s where all the action is. Take some time to look through the file and read all the comments (don’t worry, the file isn’t that long). Notice that main() defines a CDashboardApp object and then calls the member function Run() which was inherited from the LApplication class.

• Add this line after all the other #include files:

#include <LStdControl.h>

Notice that even though we added the files LStdControl.cp and LControl.cp to the project, we don’t include the file <LControl.h>. <LControl.h> is included by <LStdControl.h>.

• In the constructor, find the call of the static function URegister::RegisterClass() and add this line right below it:

 URegistrar::RegisterClass(LStdButton::class_ID,
 LStdButton::CreateStdButtonStream);

This line tells PowerPlant which function to call (LStdButton::CreateStdButtonStream()) when an object of type LStdButton is created based on a PPob resource. Before we came along, this program only needed to register the LWindow class. Since we added an LStdButton object to the PPob resource, we’ll need to register that class as well.

• Also in the constructor, just before the call of mDisplayWindow->Show(), add these lines:

 LStdButton *theButton =
 (LStdButton *)mDisplayWindow->FindPaneByID( 1000 );
 theButton->AddListener( this );

The first line searches the window mDisplayWindow for the pane with the id 1000. Now you know why we entered the number 1000 in the LStdButton’s Pane ID: field.

The second line registers the current object (the CDashboardApp object, known here as this) as a listener of the button we just found.

• At the end of the file, add the member function CDashboardApp::ListenToMessage():

// ----------------------------------------------------------
// • ListenToMessage
// ----------------------------------------------------------
// Respond to message 1000 broadcast by pushbutton

void
CDashboardApp::ListenToMessage(MessageT inMessage, void *ioParam)
{
 if ( inMessage == 1000 )
 SysBeep( 20 );
}

This function will get called whenever the button is clicked. The message 1000 will be passed in as the parameter inMessage.

Figure 7. The Dashboard window. This time it has a button in it.

Running the Program

That’s about it. Save your changes and run the program. The usual Dashboard window will appear, but this time with a button smack-dab in the middle of it. Press the button and, guess what, your Mac will beep at you.

Till Next Month

Obviously, this month’s program gives you only a brief glimpse into the PowerPlant framework. On the other hand, it’s a pretty solid glimpse. Try your hand at adding some other panes to the Dashboard window containment hierarchy. Start by adding a second button with its own pane ID and its own message ID. Next, try to add some other controls.

What part of PowerPlant would you like to learn about next? Should I get into other control types? How about (shudder) menus? Send email. I’ll be waiting to hear from you. See you next month...

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »

Price Scanner via MacPrices.net

May 2024 Apple Education discounts on MacBook...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take up to $300 off the purchase of a new MacBook... Read more
Clearance 16-inch M2 Pro MacBook Pros in stoc...
Apple has clearance 16″ M2 Pro MacBook Pros available in their Certified Refurbished store starting at $2049 and ranging up to $450 off original MSRP. Each model features a new outer case, shipping... Read more
Save $300 at Apple on 14-inch M3 MacBook Pros...
Apple has 14″ M3 MacBook Pros with 16GB of RAM, Certified Refurbished, available for $270-$300 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year warranty is... Read more
Apple continues to offer 14-inch M3 MacBook P...
Apple has 14″ M3 MacBook Pros, Certified Refurbished, available starting at only $1359 and ranging up to $270 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year... Read more
Apple AirPods Pro with USB-C return to all-ti...
Amazon has Apple’s AirPods Pro with USB-C in stock and on sale for $179.99 including free shipping. Their price is $70 (28%) off MSRP, and it’s currently the lowest price available for new AirPods... Read more
Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more
Best Buy is clearing out iPad Airs for up to...
In advance of next week’s probably release of new and updated iPad Airs, Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices for up to $200 off Apple’s MSRP, starting at $399. Sale prices... Read more
Every version of Apple Pencil is on sale toda...
Best Buy has all Apple Pencils on sale today for $79, ranging up to 39% off MSRP for some models. Sale prices for online orders only, in-store prices may vary. Order online and choose free shipping... Read more
Sunday Sale: Apple Studio Display with Standa...
Amazon has the standard-glass Apple Studio Display on sale for $300 off MSRP for a limited time. Shipping is free: – Studio Display (Standard glass): $1299.97 $300 off MSRP For the latest prices and... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.