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

f.lux 42.1 - Adjusts the color of your d...
f.lux makes the color of your computer's display adapt to the time of day, warm at night and like sunlight during the day. Ever notice how people texting at night have that eerie blue glow? Or wake... Read more
Spotify 1.1.94.872 - Stream music, creat...
Spotify is a streaming music service that gives you on-demand access to millions of songs. Whether you like driving rock, silky R&B, or grandiose classical music, Spotify's massive catalogue puts... Read more
Vitamin-R 4.15 - Personal productivity t...
Vitamin-R creates the optimal conditions for your brain to work at its best by structuring your work into short bursts of distraction-free, highly focused activity alternating with opportunities for... Read more
OfficeTime 2.0.628 - Easy time and expen...
OfficeTime is time and expense tracking that is easy, elegant and focused. Other time keepers are clumsy or oversimplified. OfficeTime balances features and ease of use, allowing you to easily track... Read more
Slack 4.28.182 - Collaborative communica...
Slack brings team communication and collaboration into one place so you can get more work done, whether you belong to a large enterprise or a small business. Check off your to-do list and move your... Read more
DEVONthink Pro 3.8.6 - Knowledge base, i...
DEVONthink is DEVONtechnologies' document and information management solution. It supports a large variety of file formats and stores them in a database enhanced by artificial intelligence (AI). Many... Read more
FileMaker Pro 19.5.4 - Quickly build cus...
FileMaker Pro is the tool you use to create a custom app. You also use FileMaker Pro to access your app on a computer. Start by importing data from a spreadsheet or using a built-in Starter app to... Read more
Backblaze 8.5.0.628 - Online backup serv...
Backblaze is an online backup service designed from the ground-up for the Mac. With unlimited storage available for $6 per month, as well as a free 15-day trial, peace of mind is within reach with... Read more
Day One 7.16 - Maintain a daily journal.
Day One is an easy, great-looking way to use a journal / diary / text-logging application. Day One is well designed and extremely focused to encourage you to write more through quick Menu Bar entry,... Read more
Garmin Express 7.14.0.0 - Manage your Ga...
Garmin Express is your essential tool for managing your Garmin devices. Update maps, golf courses and device software. You can even register your device. Update maps Update software Register your... Read more

Latest Forum Discussions

See All

We’re Digging ‘Shovel Knight Dig’ – The...
We spend the bulk of this week’s podcast talking about the new iPhone 14. Specifically, the iPhone 14 Pro Max which both Eli and myself picked up. The consensus seems to be: They’re great! They’re iPhones! We do lay down our hot takes on all the new... | Read more »
TouchArcade Game of the Week: ‘Loose Noz...
There aren’t a lot of stories like that of the development of Loose Nozzles, and of those games that do have an interesting development story, even fewer are actually decent games to play. Loose Nozzles nails both, though. The way it was created is... | Read more »
SwitchArcade Round-Up: ‘Shovel Knight Di...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 23rd, 2022. In today’s article, we’ve got the rest of this week’s releases to look at. There are actually a few big games today, including the hot-hot-hot Shovel Knight Dig... | Read more »
‘Gubbins’ is a Way Too Adorable Word Gam...
There are games whose art style, sounds, and overall vibe just make me smile ear to ear. Games like Hidden Folks, Krispee Street, or Tiny Wings. There’s just something so cool about being able to literally feel the heart that goes into a game. Now... | Read more »
Based on the Baking Reality Show, ‘Naile...
Fans of Netflix’s reality baking show Nailed It! have a new holiday-themed season to look forward to next month when Nailed It! Halloween launches on October 5th, but the fun doesn’t stop there because the show is also arriving as a mobile game the... | Read more »
Cookie Run: Kingdom announces collaborat...
In news sure to excite fans of biscuits or K-Pop music, the Korean sensations BTS have teamed up with Cookie Run: Kingdom for a series of events. After some warm-up episodes, the collaboration will culminate in a BTS in-game concert, so if anyone'... | Read more »
‘Shovel Knight Dig’ From Nitrome and Yac...
Shovel Knight Dig () from Nitrome and Yacht Club Games is this week’s new Apple Arcade release. It is definitely one of my favorite additions to the service ever, and a fantastic game overall. I played it a few hours ago when it started rolling out... | Read more »
SwitchArcade Round-Up: ‘Mario Strikers’...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 22nd, 2022. Hunh, lots of twos in the date today. Nifty. As those who read yesterday’s article may remember, I got a vaccine shot about twenty four hours ago and it is... | Read more »
Rogue-Like Platformer ‘Tallowmere 2’ Lau...
The original Tallowmere from developer Chris McFarland launched on mobile way back in 2015, and to be honest it did not leave a good first impression with me. For lack of a better term, it just seemed… janky, and right from the start the game sort... | Read more »
Alchemy Stars newest event launches and...
Alchemy Stars has introduced its latest event, entitled Farewell, My Wonderland, bringing with it new characters and a bevvy of rewards. The event will reportedly focus on the underlying message that even after tragic events there is still light,... | Read more »

Price Scanner via MacPrices.net

Use our exclusive Apple Price Trackers to fin...
Our Apple award-winning price trackers are the best place to look for the lowest prices and latest sales on all the latest Apple gear this season. Scan our price trackers for the latest information... Read more
New promo at Verizon: Get Apple Watch Series...
Purchase a new iPhone 14 at Verizon, and get an Apple Watch Series 8 for as low as $5 per month. $120 in promo credits for the Watch are spread over a 36 month term, reducing the price of the Watch... Read more
Visible drops prices on Apple iPhone 13 model...
Verizon’s low-cost wireless cell service, Visible has dropped prices on iPhone 13 models to new low prices starting at $599: – iPhone 13 Pro Max: starting at $980 + free $200 gift card – iPhone 13... Read more
Back in stock! 14″ MacBook Pros with Apple M1...
Amazon has restocked 14″ MacBook Pros M1 Pro CPUs for $400 off MSRP, starting at only $1599. Shipping is free. Be sure to make your purchase from Amazon rather than a third-party seller. Their prices... Read more
This is the final week to take advantage of A...
Apple’s Back to School promotion for 2022 ends on September 26, 2022. As part of this promotion, Apple will include a free $150 Apple Gift Card with the purchase of any MacBook Air, MacBook Pro, or... Read more
Mac Studio with M1 Max CPU back in stock toda...
Apple has the base standard-configuration Mac Studio available again in their Certified Refurbished section for $1799, and it’s in stock today. Each Mac Studio comes with Apple’s one-year warranty,... Read more
Apple MagSafe iPhone battery on sale for $84,...
Amazon has Apple’s MagSafe Battery on sale for $84 today. Shipping is free. That’s $15 off Apple’s MSRP, and it’s the lowest price for one of these MagSafe batteries among the Apple retailers we... Read more
24-inch M1-powered iMacs available today at A...
Apple has a full range of 24-inch M1 iMacs available today in their Certified Refurbished store. Models are available starting at only $1099 and range up to $260 off original MSRP. Each iMac is in... Read more
Verizon offers free Apple iPhone 14 models to...
Verizon is offering a $800-$1000 discounts on Apple’s new iPhone 14 models for new and existing customers with a qualified trade-in. Price of the iPhone 14 will be spread over 36 months of payments,... Read more
Gazelle drops prices on iPhone 13 models to a...
Gazelle has a full line of discounted, refurbished, unlocked Apple iPhone 13 models now available starting at $469. iPhones are offered in Fair, Good, and Excellent conditions, and multiple colors... Read more

Jobs Board

Physician Assistant, Primary Care, *Apple*...
Physician Assistant, Primary Care, Apple Valley (1.07FTE) + Job ID: 65766 + Department: AV Primary Care + City: Apple Valley, MN + Location: HP - Apple Read more
Operations Manager - Mac/ *Apple* Engineerin...
…Responsible for the day-to-day activities relating to the engineering of Apple Macs in a complex, multi-platform environment. Demonstrates strong leadership, Read more
Lead Developer - *Apple* tvOS - Rumble (Uni...
…earnings, and positive sentiment About the role: We are looking for a Lead Apple tvOS Developer to join our application engineering team to expand our video centric Read more
Systems Administrator - *Apple* Devices / J...
…Administration **Duties and Responsibilities** + Configure and maintain the client's Apple Device Management (ADM) solution. The current solution is JAMF supporting Read more
Sr Product Manager, *Apple* TV Platforms -...
…an experienced senior product manager to drive the strategy and requirements for our Apple TV devices, acting as the champion and owner of the holistic experience in Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.