TweetFollow Us on Twitter

PP Modeless Child Wins
Volume Number:12
Issue Number:1
Column Tag:Getting Started

PowerPlant and Modeless Child Windows

By Dave Mark, MacTech Magazine Regular Contributing Author

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

In last month’s column, we built an application that featured windows with two push buttons and a scrolling picture pane. The first button beeped when you clicked it. The second button was disabled. This month, we’ll extend the PictScroller program. We’ll enable the second button so that when you click it, a new window appears, allowing you to select a new picture for the scrolling picture pane.

Thanks once again to Greg Dow for all his PowerPlant help. Greg is a real friend to this column and has never been too busy to lend a hand.

Copy Last Month’s
PictScroller Project

Start off by making a copy of last month’s project folder. Rename it PictScroller2 or something like that. This way, if things get a little screwy, you don’t have to start over from scratch. At the very least, you’ll be able to start from where we left off last month.

Once your old folder is tucked safely away, open up the PictScroller2 folder. Our first step will be to add two more PICT resources to the Constructor file PictScroller.rsrc.

• Open up PictScroller.rsrc using your favorite resource editor.

• Change the resource ID of the existing PICT resource from 128 to 2001.

• Add two more PICT resources to the file and change their resource IDs to 2002 and 2003.

• Save your changes and quit your resource editor.

In addition to the sun PICT from last month’s column, I added the moon and red car pictures from my ScrapBook.

Our next step is to edit PictScroller.rsrc using Constructor.

• Double-click on the file PictScroller.rsrc.

Constructor will open the file PictScroller.rsrc and display a window listing all the views in this file. At this point, we’ve got a single view, an LWindow with an id of 1 and the name PictWindow. Our first goal is to make a few changes to our existing LWindow view. Our second goal is to create a new view, an LWindow with 3 radio buttons and a mini-PICT frame.

Let’s start by editing the existing LWindow.

• Double-click on the LWindow with the id of 1 (it should be the only view listed in the master view list).

The view editing window for the PictScroller LWindow will appear.

• Double-click on the titlebar of the window embedded in the view editing window (the title bar says PictScroller).

An info window for the PictScroller window will appear. This next step is incredibly important:

• Change the Class ID field from wind to CpsW.

• Close the PictScroller info window.

The four letter (case sensitive!) code tells PowerPlant what type of object we are creating. The code 'wind' corresponds to the class LWindow. That’s the class we used last month. This month, we’ll be subclassing LWindow with a class named CPictScrollerWindow. When you enter the CPictScrollerWindow class definition (later in the column), you’ll see that we create an enum constant with the name class_ID and the value 'CpsW'. Each time you create a class that implements a PowerPlant view, you’ll enter the class’ class_ID code in the Class ID field in the view’s info window.

• Double-click on the LPicture pane (it has a pane id of 1003).

• When the pane info window appears, change the PICT Resource ID to 2001.

The first of your three PICT resources (the one with the resource ID 2001) should now be displayed in the scrolling pane.

• Close the LPicture pane info window.

• Double-click the Dialog button (the right button).

• Click on the Enabled check box (so that it is checked).

• Change the Value Message to 1001.

• Change the Button Title to Picture...

• Close the button’s pane info window.

The button will now say Picture... and will no longer be disabled. Also, when it is clicked in your application, it will broadcast a message with a value of 1001 to any listeners.

Now let’s add a new view.

• Close the view editing window for the PictWindow LWindow.

• Select New Resource from the Edit menu.

• When the view naming dialog appears, make sure LWindow is selected from the popup menu, type Pict Selector (Child) in the edit field, and click the OK button.

A new view editing window will appear. Before we add any items to the new view, change the view’s ID to 2000.

• Close the view editing window.

• Select LWindow 128 in the master view list, then select Resource Info from the Edit menu.

• Change the Resource ID from 128 to 2000.

• Close the resource info window.

• Double-click on the Pict Selector view in the master view list.

A view editing window for view 2000 will appear.

Note: Greg Dow uses a numbering convention that I’ll try to stick to from now on. He numbers all his new views by thousands. So his views have IDs like 1000, 2000, 3000, etc. The items within a view start at one plus the view ID. That means that the items in this new view will be numbered 2001, 2002, 2003, etc. If you have groups of items (like radio buttons, for example), you might want to leave holes in your numbering scheme. For example, you might number your radio buttons 2001, 2002, and 2003, then start the next set of items with 2010, 2011, 2012. As always, pick a scheme you like and try to be consistent.

• Double-click on the window inside the view editing window.

• When the info window appears, change its settings to match those shown in Figure 1.

• Close the info window.

Figure 1. The info window for the Picture Selector window.

Next, you’ll create the four items that make up this new view: three radio buttons and a mini-picture frame.

• Drag an LStdRadioButton from the palette window onto the Picture Selector window.

• Double-click the new radio button.

• When the info window appears, change its settings to match those shown in Figure 2.

• Close the info window.

Figure 2. The info window for the Sun radio button.

• Drag a second LStdRadioButton from the palette window onto the Picture Selector window.

• Double-click the new radio button.

• When the info window appears, change its settings to match those shown in Figure 3.

• Close the info window.

Figure 3. The info window for the Moon radio button.

• Drag a third LStdRadioButton from the palette window onto the Picture Selector window.

• Double-click the new radio button.

• When the info window appears, change its settings to match those shown in Figure 4.

• Close the info window.

Figure 4. The info window for the Red car radio button.

As is, these three radio buttons will act independently. That is, if you click on one to turn it on or off, it will have no effect on the others. To fix this problem, we need to group the three buttons into a radio button group.

• Hold down the shift key and select all three of the radio buttons.

• Select Make Radio Group from the Arrange menu.

Next, we’ll create the mini-picture frame.

• Drag an LPicture from the palette window onto the Picture Selector window.

• Double-click the new LPicture pane.

• When the info window appears, change its settings to match those shown in Figure 5.

• Close the info window.

• Quit Constructor. Be sure to save your changes.

Figure 5. The info window for the LPicture min-picture frame.

That’s about it for your Constructor session. Figure 6 shows my finished Pict Selector view. Notice that none of the radio buttons is turned on and that the mini-picture frame is just a grey square. We’ll set up the buttons and display the right picture in our source code, which we’ll get to next.

Figure 6. The finished Pict Selector view.

Entering the Source Code

We’ll modify two source code files and add two new source code files. We’ll start with some changes to PictScroller.cp.

• Open the project file PictScroller.µ.

• Open the file PictScroller.cp.

• Add the line:

#include "CPictScrollerWindow.h"

immediately after the #include of PictScroller.h.

CPictScrollerWindow.h contains a brand new class definition, which we’ll get to in a bit. This is the class with the class_ID constant 'CpsW' mentioned earlier in the column. Last month, we embedded our message handling code in PictScroller.cp. This month, we’ll create a subclass of LWindow and handle the messages (like 1001 from the Beep button and 1002 from the Picture... button) in our new class.

• In the function CPPStarterApp()::CPPStarterApp(), add this code at the end:

 URegistrar::RegisterClass(CPictScrollerWindow::class_ID,
 CPictScrollerWindow::CreatePictScrollerWindowStream);

This code registers our new class, passing in the class_ID code and a pointer to the member function that creates a new object.

• In CPPStarterApp::ObeyCommand(), comment out these two lines in the cmdNew case of the switch statement:

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

Since we will no longer be handling any messages in this class, we no longer have to add ourselves as a listener. For the same reason, we can delete the function CPPStarterApp::ListenToMessage():

• Delete the function CPPStarterApp::ListenToMessage().

• Save your changes and close the source code file.

• Now open the file PictScroller.h.

• Comment out the reference to public LListener in the first line of the CPPStarterApp class definition:

class CPPStarterApp : public Lapplication
 /*, public LListener*/ {

• Comment out the declaration of the member function CPPStarterApp::ListenToMessage().

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

Your next job is to create new source code files to hold your new class definition and your new class source code.

• Create a new source code file.

• Type in this source code:

#include <LWindow.h>
#include <LListener.h>

class CPictScrollerWindow : 
 public LWindow,
 public LListener {

public:
 enum { class_ID = 'CpsW' };

 static CPictScrollerWindow*
 CreatePictScrollerWindowStream(LStream *inStream);
 CPictScrollerWindow(LStream *inStream);
 virtual void ListenToMessage(MessageT inMessage,
 void *ioParam);
 
 virtual Boolean AllowSubRemoval(
 Lcommander *inSub);

protected:
 Lwindow *mChildWindow;

 virtual void FinishCreateSelf();
 void DisplayPictSelector();
};

• Save the source code file as CPictScrollerWindow.h.

• Close the file CPictScrollerWindow.h.

There are several important things to remember about CPictScrollerWindow.h. We are subclassing LWindow and LListener. We want to behave like a window and support listening to messages (in our case, we want a window that responds to control clicks). We’ve removed the listening behaviour from the LApplication subclass CPPStarterApp and added it to CPictScrollerWindow.

Notice the enum constant class_ID with its corresponding four byte code 'CpsW'. If you define your own class and will be adding listeners to a specific view, be sure the class defines a class_ID and be sure that class_ID code is entered in the class ID field for that view in Constructor.

You might be wondering why we modified the class ID field in the PictScroller view (the view with the two push buttons), but not in the Pict Selector view (the view with the three radio buttons). That’s because the class ID only matters if your controls generate messages and if you add a listener to listen to those messages. We won’t install listeners for any of the radio buttons. See the call UReanimator:: LinkListenerToControls() in CPictScrollerWindow:: DisplayPictSelector(). The class ID acts as a kind of linkage, linking message senders to the class that will receive those messages. As you’ll see, we’ll handle the radio buttons without using listeners, so we didn’t need to modify the class ID field in the Pict Selector view.

Let’s get to the CPictScrollerWindow.cp source code.

• Create another new source code file.

• Type in this source code (you can ignore the comments):

#include "CPictScrollerWindow.h"
#include <LStdControl.h>
#include <LPicture.h>
#include <UReanimator.h>

CPictScrollerWindow*
 CPictScrollerWindow::CreatePictScrollerWindowStream(
 LStream *inStream)
// This function gets called to create a new
// CPictScrollerWindow object.
{
 return (new CPictScrollerWindow(inStream));
}

 
CPictScrollerWindow::CPictScrollerWindow(
 LStream *inStream):LWindow(inStream)
// Here’s the constructor. Notice that we just pass the input parameter on to the             //               LWindow 
constructor. We have added a data member to our LWindow subclass              //               and initialize it here. 
mChildWindow is a pointer to a Pict Selector window (aka, a                 //               child window) if one exists. 
If you click in the Picture... button, if a child window   //    exists, it is brought to the front. If the child window 
does not exist, it is created.
{
 mChildWindow = nil;
}


void CPictScrollerWindow::FinishCreateSelf()
// FinishCreateSelf() overrides its LWindow counterpart and is called automatically at  //               object construction 
time. We’ll add our two listeners: one for the Beep button and              //               one for the Picture... button.
{
 LStdButton *theButton = (LStdButton*) FindPaneByID(1000);
 theButton->AddListener(this);
 theButton = (LStdButton*) FindPaneByID(1001);
 theButton->AddListener(this);
}


void CPictScrollerWindow::ListenToMessage(
 MessageT inMessage, void *ioParam)
{
 switch (inMessage) {
 case 1000:
// Beep if the Beep button is pressed.
 SysBeep(10);
 break;
 case 1001:
// Open the Pict Selector window if the Picture... button is pressed.
 DisplayPictSelector();
 break;
 case 2001:
 case 2002:
 case 2003: {
 if ((*(Int32*)ioParam) == Button_On) {
// When ListenToMessage() gets called in response to a message from a control, it      //               puts the value 
of the control in the param ioParam. We only take an action when a  //               radio button is being turned 
on. We don't do anything in response to a button  //    being turned off.
 LPicture *thePicture =
 (LPicture*) mChildWindow->FindPaneByID(2010);
 if (inMessage != thePicture->GetPictureID()) {
 thePicture->SetPictureID(inMessage);
 Rect pictFrame;
 thePicture->CalcLocalFrameRect(pictFrame);
 thePicture->ResizeImageTo(
 pictFrame.right - pictFrame.left,
 pictFrame.bottom - pictFrame.top, false);
 thePicture->Refresh();
 thePicture = (LPicture*) FindPaneByID(1003);
 thePicture->SetPictureID(inMessage);
 thePicture->Refresh();
 }
 }
 break;
 }
 }
}


void
CPictScrollerWindow::DisplayPictSelector()
{
// If the Pict Selector window doesn’t exist, we'll create it.
 if (mChildWindow == nil) {
 mChildWindow = LWindow::CreateWindow(2000, this);
// This call searches the specified RidL resource (in this case, 2000) and calls                //               AddListener 
for each item in the RidL. A RidL resource is much like a DITL              //               resource. It is a list of broadcasting 
pane IDs. Each of our radio buttons is a      //    broadcasting pane (broadcasts a message). When we grouped 
the three radio  //    buttons, Constructor created a RidL resource listing the three radio button pane 
    //    IDs. Call LinkListenerToControls() if you have a RidL, or AddListener() for each   //               broadcasting 
control if you don't have a RidL.
 UReanimator::LinkListenerToControls(
 this, mChildWindow, 2000);
// Gets the pane ID of the picture pane in the scroller window
 LPicture *scrollerPicture =
 (LPicture*) FindPaneByID(1003);
 LStdRadioButton *theRadioButton =
 (LStdRadioButton*)mChildWindow->FindPaneByID(
 scrollerPicture->GetPictureID() );
// Our Pict scaling code is executed when a message is sent to ListenToMessage(),    //               above. This 
code also calculates which picture should be drawn in the miniframe.                //               To be sure that this 
code gets executed, we first turn off the appropriate radio    //             button then turn it back on again.
 theRadioButton->SetValue( Button_Off );
 theRadioButton->SetValue( Button_On );
 mChildWindow->Show();
 } else {
 mChildWindow->Select();
 }
}


Boolean CPictScrollerWindow::AllowSubRemoval(
 LCommander *inSub)
// AllowSubRemoval() is called by PowerPlant (by LWindow::AttemptClose() and            //               LWindow::DoClose()) 
when an LWindow is closed. This is the hook where you  //    might put up a “Save Changes” dialog and not 
close the window if Cancel was   //    clicked. In our case, we won't put up this kind of dialog. Instead, we'll 
just mark      //    the mChildWindow as nil and return true, saying it is OK to delete the child           //
    window. It should be noted that AllowSubRemoval() is called when any window            //               that has a 
super-commander is closed (-n general, all windows will have a super-              //               commander - perhaps 
the LApplication object).  When you close the parent        //   window, the AllowSubRemoval() of the parent 
window’s supercommander (the  //    LApplication object's AllowSubRemoval()) gets called. We’ll learn about 
sub- and       //    supercommanders in a future column.
{
 if (inSub == mChildWindow) {
 mChildWindow = nil;
 }
 return true;
}

• Save the source code file as CPictScrollerWindow.cp.

• Select Add Window from the Project menu to add the file to the project.

• Close the file CPictScrollerWindow.cp.

Running the Project

That’s about it. Run the project. If you removed objects before you copied last month’s project, you’ll have a minute or two to wait for the PowerPlant classes to recompile. Once your program runs, you’ll see a window similar to the one in Figure 7.

Figure 7. The main, PictScroller window, showing two
push buttons and a scrolling picture.

Click on the Beep button. You should hear a beep. Click on the Picture... button. A Picture Selector window should appear (Figure 8). Click on a few radio buttons and verify that the picture changes in both the mini-frame and in the PictScroller window. Click on the PictScroller window to bring it to the front. Click on the Picture... button again. Notice that the Picture Selector window came to the front, as opposed to the program creating a new one.

Figure 8. The Picture Selector window.

Select New from the File menu and create a second PictScroller window. Click on that window’s Picture... button to bring up a second Picture Selector window. Click on radio buttons in each Picture Selector window to convince yourself that each is tied only to its own parent window. Now close one of the PictScroller windows. Notice that that window’s Picture Selector window also disappears!!! As I said in the program comments, this all has to do with commanders, subcommanders, and supercommanders. We’ll get into all that in future columns.

Till Next Month...

Well, that’s about it for PictScroller. I think we’ll start next month’s column with a brand new program. See you then...

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Dropbox 193.4.5594 - Cloud backup and sy...
Dropbox is a file hosting service that provides cloud storage, file synchronization, personal cloud, and client software. It is a modern workspace that allows you to get to all of your files, manage... Read more
Google Chrome 122.0.6261.57 - Modern and...
Google Chrome is a Web browser by Google, created to be a modern platform for Web pages and applications. It utilizes very fast loading of Web pages and has a V8 engine, which is a custom built... Read more
Skype 8.113.0.210 - Voice-over-internet...
Skype is a telecommunications app that provides HD video calls, instant messaging, calling to any phone number or landline, and Skype for Business for productive cooperation on the projects. This... Read more
Tor Browser 13.0.10 - Anonymize Web brow...
Using Tor Browser you can protect yourself against tracking, surveillance, and censorship. Tor was originally designed, implemented, and deployed as a third-generation onion-routing project of the U.... Read more
Deeper 3.0.4 - Enable hidden features in...
Deeper is a personalization utility for macOS which allows you to enable and disable the hidden functions of the Finder, Dock, QuickTime, Safari, iTunes, login window, Spotlight, and many of Apple's... Read more
OnyX 4.5.5 - Maintenance and optimizatio...
OnyX is a multifunction utility that you can use to verify the startup disk and the structure of its system files, to run miscellaneous maintenance and cleaning tasks, to configure parameters in the... Read more

Latest Forum Discussions

See All

Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »
Live, Playdate, Live! – The TouchArcade...
In this week’s episode of The TouchArcade Show we kick things off by talking about all the games I splurged on during the recent Playdate Catalog one-year anniversary sale, including the new Lucas Pope jam Mars After Midnight. We haven’t played any... | Read more »
TouchArcade Game of the Week: ‘Vroomies’
So here’s a thing: Vroomies from developer Alex Taber aka Unordered Games is the Game of the Week! Except… Vroomies came out an entire month ago. It wasn’t on my radar until this week, which is why I included it in our weekly new games round-up, but... | Read more »
SwitchArcade Round-Up: ‘MLB The Show 24’...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for March 15th, 2024. We’re closing out the week with a bunch of new games, with Sony’s baseball franchise MLB The Show up to bat yet again. There are several other interesting games to... | Read more »
Steam Deck Weekly: WWE 2K24 and Summerho...
Welcome to this week’s edition of the Steam Deck Weekly. The busy season has begun with games we’ve been looking forward to playing including Dragon’s Dogma 2, Horizon Forbidden West Complete Edition, and also console exclusives like Rise of the... | Read more »
Steam Spring Sale 2024 – The 10 Best Ste...
The Steam Spring Sale 2024 began last night, and while it isn’t as big of a deal as say the Steam Winter Sale, you may as well take advantage of it to save money on some games you were planning to buy. I obviously recommend checking out your own... | Read more »
New ‘SaGa Emerald Beyond’ Gameplay Showc...
Last month, Square Enix posted a Let’s Play video featuring SaGa Localization Director Neil Broadley who showcased the worlds, companions, and more from the upcoming and highly-anticipated RPG SaGa Emerald Beyond. | Read more »
Choose Your Side in the Latest ‘Marvel S...
Last month, Marvel Snap (Free) held its very first “imbalance" event in honor of Valentine’s Day. For a limited time, certain well-known couples were given special boosts when conditions were right. It must have gone over well, because we’ve got a... | Read more »
Warframe welcomes the arrival of a new s...
As a Warframe player one of the best things about it launching on iOS, despite it being arguably the best way to play the game if you have a controller, is that I can now be paid to talk about it. To whit, we are gearing up to receive the first... | Read more »
Apple Arcade Weekly Round-Up: Updates an...
Following the new releases earlier in the month and April 2024’s games being revealed by Apple, this week has seen some notable game updates and events go live for Apple Arcade. What The Golf? has an April Fool’s Day celebration event going live “... | Read more »

Price Scanner via MacPrices.net

Apple Education is offering $100 discounts on...
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 $100 off the price of a new M3 MacBook Air.... Read more
Apple Watch Ultra 2 with Blood Oxygen feature...
Best Buy is offering Apple Watch Ultra 2 models for $50 off MSRP on their online store this week. Sale prices available for online orders only, in-store prices may vary. Order online, and choose... Read more
New promo at Sams Club: Apple HomePods for $2...
Sams Club has Apple HomePods on sale for $259 through March 31, 2024. Their price is $40 off Apple’s MSRP, and both Space Gray and White colors are available. Sale price is for online orders only, in... Read more
Get Apple’s 2nd generation Apple Pencil for $...
Apple’s Pencil (2nd generation) works with the 12″ iPad Pro (3rd, 4th, 5th, and 6th generation), 11″ iPad Pro (1st, 2nd, 3rd, and 4th generation), iPad Air (4th and 5th generation), and iPad mini (... Read more
10th generation Apple iPads on sale for $100...
Best Buy has Apple’s 10th-generation WiFi iPads back on sale for $100 off MSRP on their online store, starting at only $349. With the discount, Best Buy’s prices are the lowest currently available... Read more
iPad Airs on sale again starting at $449 on B...
Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices again for $150 off Apple’s MSRP, starting at $449. Sale prices for online orders only, in-store price may vary. Order online, and choose... Read more
Best Buy is blowing out clearance 13-inch M1...
Best Buy is blowing out clearance Apple 13″ M1 MacBook Airs this weekend for only $649.99, or $350 off Apple’s original MSRP. Sale prices for online orders only, in-store prices may vary. Order... Read more
Low price alert! You can now get a 13-inch M1...
Walmart has, for the first time, begun offering new Apple MacBooks for sale on their online store, albeit clearance previous-generation models. They now have the 13″ M1 MacBook Air (8GB RAM, 256GB... Read more
Best Apple MacBook deal this weekend: Get the...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New 15-inch M3 MacBook Air (Midnight) on sale...
Amazon has the new 15″ M3 MacBook Air (8GB RAM/256GB SSD/Midnight) in stock and on sale today for $1249.99 including free shipping. Their price is $50 off MSRP, and it’s the lowest price currently... Read more

Jobs Board

Early Preschool Teacher - Glenda Drive/ *Appl...
Early Preschool Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Read more
Senior Software Engineer - *Apple* Fundamen...
…center of Microsoft's efforts to empower our users to do more. The Apple Fundamentals team focused on defining and improving the end-to-end developer experience in Read more
Relationship Banker *Apple* Valley Main - W...
…Alcohol Policy to learn more. **Company:** WELLS FARGO BANK **Req Number:** R-350696 **Updated:** Mon Mar 11 00:00:00 UTC 2024 **Location:** APPLE VALLEY,California Read more
Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill WellSpan Medical Group, York, PA | Nursing | Nursing Support | FTE: 1 | Regular | Tracking Code: 200555 Apply Now Read more
Early Preschool Teacher - Glenda Drive/ *Appl...
Early Preschool Teacher - Glenda Drive/ Apple ValleyTeacher Share by Email Share on LinkedIn Share on Twitter Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.