TweetFollow Us on Twitter

Deeper Dialogs
Volume Number:9
Issue Number:5
Column Tag:Getting Started

Related Info: Dialog Manager

Looking Deeper into the Dialog Manager

Handling Modeless Dialogs

By Dave Mark, MacTech Magazine Regular Contributing Author

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

Over the last few columns, we’ve explored the mysteries of the Dialog Manager via a program called Dialogger. As promised, the next few columns will go deeper into the Dialog Manager, presenting a program called Modeless. Modeless implements a modeless dialog, a dialog that looks and acts like a regular window, but retains the benefits of being managed by the Dialog Manager. Because of Modeless’ size, we’ll create the Modeless resources and enter the Modeless source code this month, and put off a detailed discussion of the program till next month’s column.

Creating the Modeless Resources

Most of the resources we’ll need for this month’s column can be copied from last month’s resource file, Dialogger.Π.rsrc. Start by creating a new folder in your Development folder named Modeless ƒ. Next, duplicate the file Dialogger.Π.rsrc and drag the copy into the Modeless ƒ folder. Change the name of this file to Modeless.Π.rsrc.

Next, launch ResEdit by double-clicking on the file Modeless.Π.rsrc. The Modeless.Π.rsrc window should look similar to the one shown in Figure 1.

Figure 1. Modeless.Π.rsrc, before surgery.

Your first mission is to edit the ALRT, replacing any references to Dialogger with the appropriate reference to Modeless. Double-click on the ALRT icon. When the ALRT-picker window appears, double-click on the only ALRT listed, the one with a resource ID of 129. When the ALRT editor appears, double-click on the ALRT window so the DITL editor appears. Finally, double-click on the static text item and, when the static text editor appears, make your changes. Mine are shown in Figure 2.

Figure 2. A new version of the About... ALRT.

When you’re happy with your About... message, close all the windows until you are back at the main window. Next, you’ll change the About Dialogger... item in the • menu to read About Modeless....

Double-click on the MENU icon to bring up the MENU picker window. Next, double-click on the • MENU, bringing up the MENU editing window. Click on the About Dialogger... item and change it so it says About Modeless..., as shown in Figure 3.

Figure 3. A new version of the • MENU resource.

Close the • MENU editing window, then double-click on the File menu, bringing up the File MENU editing window. Click on the Settings... item and then click in the Cmd-key: field. Type the letter S in the Cmd-key: field. This ties the command-key equivalent S to the Settings... item in the File menu. My File MENU editing window is shown in Figure 4.

Figure 4. A new version of the File MENU resource.

Once you are done, close all the windows leaving just the main window open. Our final chore is to edit the DLOG and DITL resources that made up Dialogger’s modal dialog, changing them to reflect the look of a modeless dialog.

Double-click on the DLOG icon to bring up the DLOG-picker window. Next, double-click on the only DLOG listed, the one with the resource id of 128. The Settings... DLOG editing window should appear. First, click on the Initially visible check box, making sure it is unchecked.

Figure 5. The DLOG editing window, at the half-way point.

Next, click on the second window icon from the left, at the top of the editing window. The DLOG window should change to reflect your new selection. Modeless dialogs don’t have the traditional double-border of their modal cousins. Instead, they look like regular windows. In this case, we want to give our dialog window a close box, so make sure the Close box check box is checked.

We also want to give our dialog window a title. Select Set 'DLOG' Characteristics... from the DLOG menu. When the 'DLOG' Characteristics dialog appears, type the text Fred Settings in the Window title: field and click OK. Your new title should be reflected in the DLOG editing window. Figure 5 shows my DLOG editing window at this point in the process.

Next, we'll edit the DITL associated with this DLOG. Double click on the DLOG window (within the editing window) so the DITL editing window appears. You are now going to delete all the items in the DITL except the Pick one: static text item and the three radio buttons.

Click on the OK button and press the delete key. Click on the Cancel button and press the delete key. Click on the Show preview check box and press the delete key. Finally, click on the user item (the grey rectangle) and press delete.

Next, you'll change the positioning of the remaining four items. Double-click on the Pick one: static text item and change its coordinates to the ones shown in Figure 6. Close the window when you are done.

Figure 6. New coordinates for the Pick one: static text item.

Double-click on the Afghan radio button and change its coordinates to the ones shown in Figure 7. Close the window when you are done.

Figure 7. New coordinates for the Afghan radio button.

Double-click on the Elephant radio button and change its coordinates to the ones shown in Figure 8. Close the window when you are done.

Figure 8. New coordinates for the Elephant radio button.

Double-click on the Squirrel radio button and change its coordinates to the ones shown in Figure 9. Close the window when you are done.

Figure 9. New coordinates for the Squirrel radio button.

Now we're almost done. Close the DITL editing window, leaving you back in the DLOG editing window. The last thing left to do is to resize the DLOG to reflect its slimmed down and rearranged DITL. Change the Bottom: to 123 and the Right: to 234. The final version of our DLOG is shown in Figure 10.

Figure 10. The final version of our DLOG.

Quit ResEdit, being sure to save your changes. Now we're ready to enter our source code.

Creating the Modeless Project

Launch THINK C and create a new project named Modeless.Π in the Modeless ƒ folder. Add MacTraps to the project. Next, create a new source code window and type in the following:

/* 1 */

#define kBaseResID 128
#define kAboutALRTid 129
#define kDialogResID 128

#define kVisible true
#define kMoveToBackNULL
#define kMoveToFront (WindowPtr)-1L
#define kNoGoAwayfalse
#define kSleep   60L

#define kOn 1
#define kOff0

#define iAfghan  1
#define iElephant2
#define iSquirrel3

#define kLeftMargin5
#define kTopMargin 40

#define kFirstRadio1
#define kLastRadio 3

#define mApple   kBaseResID
#define iAbout   1

#define mFile    kBaseResID+1
#define iSettings1
#define iQuit    3


/*************/
/*  Globals  */
/*************/

Boolean gDone;
short   gCurrentPICT = kBaseResID;
DialogPtr gSettingsDLOG = NULL;
WindowPtr gFredWindow = NULL;


/***************/
/*  Functions  */
/***************/

void    ToolBoxInit( void );
PicHandle LoadPICT( short picID );
void    CreateWindow( void );
void    MenuBarInit( void );
void    EventLoop( void );
void    DoEvent( EventRecord *eventPtr );
void    DoDialogEvent( EventRecord *eventPtr );
void    HandleMouseDown( EventRecord *eventPtr );
void    HandleMenuChoice( long menuChoice );
void    HandleAppleChoice( short item );
void    HandleFileChoice( short item );
void    DoUpdate( EventRecord *eventPtr );
void    CreateDialog( void );
void    FlipControl( ControlHandle control );
void    SwitchPICT( void );
    

/******** main *********/

void  main( void )
{
 ToolBoxInit();
 MenuBarInit();
 
 CreateWindow();
 
 EventLoop();
}


/******* ToolBoxInit *******/

void  ToolBoxInit( void )
{
 InitGraf( &thePort );
 InitFonts();
 InitWindows();
 InitMenus();
 TEInit();
 InitDialogs( NULL );
 InitCursor();
}


/******** LoadPICT *********/

PicHandle LoadPICT( short picID )
{
 PicHandlepic;
 
 pic = GetPicture( picID );
 
 if ( pic == NULL )
 {
 SysBeep( 10 );  /*  Couldn't load the PICT resource!!!  */
 ExitToShell();
 }
 return( pic );
}


/******** CreateWindow *********/

void  CreateWindow( void )
{
 PicHandlepic;
 Rect   r;
 
 pic = LoadPICT( gCurrentPICT );
 
 r = (**pic).picFrame;
 
 OffsetRect( &r, kLeftMargin - r.left,
 kTopMargin - r.top );
 
 gFredWindow = NewWindow( NULL, &r, "\pMy Pet Fred",
 kVisible, noGrowDocProc, kMoveToBack, kNoGoAway, 0L );
 
 if ( gFredWindow == NULL )
 {
 SysBeep( 10 );  /*  Couldn't load the WIND resource!!!  */
 ExitToShell();
 }
 
 ShowWindow( gFredWindow );
 SetPort( gFredWindow );
}


/******* MenuBarInit ********/

void  MenuBarInit( void )
{
 Handle menuBar;
 MenuHandle menu;
 
 menuBar = GetNewMBar( kBaseResID );
 SetMenuBar( menuBar );

 menu = GetMHandle( mApple );
 AddResMenu( menu, 'DRVR' );
 
 DrawMenuBar();
}


/******** EventLoop *********/

void  EventLoop( void )
{
 EventRecordevent;
 
 gDone = false;
 while ( gDone == false )
 {
 if ( WaitNextEvent( everyEvent, &event, kSleep, NULL ) )
 DoEvent( &event );
 }
}


/********* DoEvent **********/

void  DoEvent( EventRecord *eventPtr )
{
 char   theChar;
 
 if ( IsDialogEvent( eventPtr ) )
 {
 DoDialogEvent( eventPtr );
 }
 else
 {
 switch ( eventPtr->what )
 {
 case mouseDown: 
 HandleMouseDown( eventPtr );
 break;
 case keyDown:
 case autoKey:
 theChar = eventPtr->message & charCodeMask;
 if ( (eventPtr->modifiers & cmdKey) != 0 ) 
 HandleMenuChoice( MenuKey( theChar ) );
 break;
 case updateEvt:
 DoUpdate( eventPtr );
 break;
 }
 }
}


/********** DoDialogEvent  **********/

void  DoDialogEvent( EventRecord *eventPtr )
{
 short  itemHit;
 short  itemType;
 Handle itemHandle;
 Rect   itemRect;
 short  curRadioButton, i;
 char   theChar;
 BooleanbecomingActive;
 MenuHandle menu;
 DialogPtrdialog;
 
 menu = GetMHandle( mFile );

 switch ( eventPtr->what )
 {
 case keyDown:
 case autoKey:
 theChar = eventPtr->message & charCodeMask;
 if ( (eventPtr->modifiers & cmdKey) != 0 ) 
 HandleMenuChoice( MenuKey( theChar ) );
 break;
 case activateEvt:
 becomingActive =
 ( (eventPtr->modifiers & activeFlag) == activeFlag );
 
 if ( becomingActive )
 {
 for ( i=kFirstRadio; i<=kLastRadio; i++ )
 {
 GetDItem( gSettingsDLOG, i, &itemType, 
 &itemHandle, &itemRect );
 HiliteControl( (ControlHandle)itemHandle, 0 );
 }
 DisableItem( menu, iSettings );
 }
 else
 {
 for ( i=kFirstRadio; i<=kLastRadio; i++ )
 {
 GetDItem( gSettingsDLOG, i, &itemType, 
 &itemHandle, &itemRect );
 HiliteControl( (ControlHandle)itemHandle, 255 );
 }
 EnableItem( menu, iSettings );
 }
 break;
 }
 
 if ( DialogSelect( eventPtr, &dialog, &itemHit ) )
 {
 switch ( itemHit )
 {
 case iAfghan:
 case iElephant:
 case iSquirrel:
 curRadioButton = gCurrentPICT - 
 kBaseResID + kFirstRadio;
 
 if ( curRadioButton != itemHit )
 {
 GetDItem( dialog, curRadioButton, &itemType,
 &itemHandle, &itemRect );
 FlipControl( (ControlHandle)itemHandle );
 
 GetDItem( dialog, itemHit, &itemType,
 &itemHandle, &itemRect );
 FlipControl( (ControlHandle)itemHandle );
 
 curRadioButton = itemHit;
 
 if ( gCurrentPICT != curRadioButton +
 kBaseResID - kFirstRadio )
 {
 gCurrentPICT = curRadioButton +
 kBaseResID - kFirstRadio;
 SwitchPICT();
 }
 }
 break;
 }
 }
}


/********* HandleMouseDown ********/

void  HandleMouseDown( EventRecord *eventPtr )
{
 WindowPtrwindow;
 short  thePart;
 long   menuChoice;
 GrafPtroldPort;
 long   windSize;
 Rect   growRect;
 MenuHandle menu;
 
 thePart = FindWindow( eventPtr->where, &window );
 
 switch ( thePart )
 {
 case inMenuBar:
 menuChoice = MenuSelect( eventPtr->where );
 HandleMenuChoice( menuChoice );
 break;
 case inSysWindow : 
 SystemClick( eventPtr, window );
 break;
 case inContent:
 SelectWindow( window );
 break;
 case inDrag : 
 DragWindow( window, eventPtr->where, 
 &screenBits.bounds );
 break;
 case inGoAway:
 if ( TrackGoAway( window, eventPtr->where ) )
 if ( window == gSettingsDLOG )
 {
 HideWindow( window );
 menu = GetMHandle( mFile );
 EnableItem( menu, iSettings );
 }
 break;
 }
}


/******** HandleMenuChoice **********/

void  HandleMenuChoice( long menuChoice )
{
 short  menu;
 short  item;
 
 if ( menuChoice != 0 )
 {
 menu = HiWord( menuChoice );
 item = LoWord( menuChoice );
 
 switch ( menu )
 {
 case mApple:
 HandleAppleChoice( item );
 break;
 case mFile:
 HandleFileChoice( item );
 break;
 }
 HiliteMenu( 0 );
 }
}


/******* HandleAppleChoice **********/

void  HandleAppleChoice( short item )
{
 MenuHandle appleMenu;
 Str255 accName;
 short  accNumber;
 
 switch ( item )
 {
 case iAbout:
 NoteAlert( kAboutALRTid, NULL );
 break;
 default:
 appleMenu = GetMHandle( mApple );
 GetItem( appleMenu, item, accName );
 accNumber = OpenDeskAcc( accName );
 break;
 }
}


/******** HandleFileChoice *********/

void  HandleFileChoice( short item )
{
 short  newPICTid;
 
 switch ( item )
 {
 case iSettings:
 if ( gSettingsDLOG == NULL )
 CreateDialog();
 else
 {
 ShowWindow( gSettingsDLOG );
 SelectWindow( gSettingsDLOG );
 }
 break;
 case iQuit:
 gDone = true;
 break;
 }
}


/********* DoUpdate ***********/

void  DoUpdate( EventRecord *eventPtr )
{
 PicHandlepic;
 WindowPtrwindow;
 Rect   r;
 
 window = (WindowPtr)eventPtr->message;
 
 pic = LoadPICT( gCurrentPICT );
 
 SetPort( window );
 
 BeginUpdate( window );
 
 r = window->portRect;
 DrawPicture( pic, &r );
 
 EndUpdate( window );
}


/******** CreateDialog  *********/

void  CreateDialog( void )
{
 short  itemType;
 Handle itemHandle;
 Rect   itemRect;
 short  curRadioButton;

 gSettingsDLOG = GetNewDialog( kDialogResID, 
 NULL, kMoveToFront );

 if ( gSettingsDLOG == NULL )
 {
 SysBeep( 10 );  /*  Couldn't load the DLOG resource!!!  */
 ExitToShell();
 }
 
 ShowWindow( gSettingsDLOG );
 SetPort( gSettingsDLOG );
 
 curRadioButton = gCurrentPICT - kBaseResID + kFirstRadio;
 GetDItem( gSettingsDLOG, curRadioButton, &itemType, 
 &itemHandle, &itemRect );
 SetCtlValue( (ControlHandle)itemHandle, kOn );
}


/******** FlipControl *********/

void  FlipControl( ControlHandle control )
{
 SetCtlValue( control, ! GetCtlValue( control ) );
}


/****** SwitchPICT  *********/

void  SwitchPICT( void )
{
 DisposeWindow( gFredWindow );
 
 CreateWindow();
}

Running Modeless

Save your source code as Modeless.c and add it to the project. Select Run from the Project menu to compile and run your program. When the program runs, the My Pet Fred window will appear, showing PICT 128. Pull down the • menu and verify that the first item reads About Modeless.... Select About Modeless... from the • menu and check out your About alert. Are your changes all there? Click the OK button to dismiss the About alert.

Hold down the mouse in the File menu and verify that the S command-key equivalent was added to the Settings... item. Select Settings.... The Settings... modal dialog box should appear (Figure 11).

Figure 11. The Settings... modal dialog box.

Click on the My Pet Fred window, bringing it to the front and sending the modeless dialog to the back. Notice that the radio buttons are dimmed when the dialog is no longer the front-most window (Figure 12).

Figure 12. The radio buttons are dimmed when the dialog box is not in front.

Now type the command-key equivalent S to bring the Settings... dialog to the front again. Once again, the radio buttons should be enabled. Click on the Elephant radio button. Notice that the My Pet Fred window changes appropriately, leaving the Settings...window in front. Click on a few more radio buttons. While you are at it, Click on the File menu. Notice that the Settings... item has been dimmed. Though this doesn't help us much in this program, it's important to be able to disable and enable certain menu items when a modeless dialog is in front.

When you are satisfied with your pet selection, drag the Settings... window to another part of your screen. Now click the close box. The Settings... window disappears. Select Settings... from the File menu. The Settings... window reappears at the position it was in when it disappeared and with the same radio button settings.

Finally, type the command-key equivalent Q to exit the program.

Till Next Month...

Modeless dialogs require a change in the usual modal programming strategy. Modal dialog handling code is usually pretty self-contained. Modeless code tends to be larger and more spread out. Next month, we'll look at the Modeless source code and talk about basic strategies for handling your own modeless dialogs.

Till then, I'm going to test drive the latest addition to my Mac: a Procom Mac-CD-MX CD-ROM drive. If you are in the market for a CD-ROM drive, do yourself a favor and check this one out. It's fast, well-designed, and is Photo-CD compatible. Now if only I could find a Photo-CD to test it with! Hmmm... I do have that roll of undeveloped film sitting on the kitchen counter. Anyone know where I can go to get it turned into a Photo-CD?

OOPS!

Correction to Getting Started, March, 1993.

Notice anything funny about the LoadPict() function in Dialogger and in Modeless? The idea of LoadPICT() was to load a PICT from the resource fork, then return a handle to the PICT. Trouble is, the routine doesn’t return a value. Ooops! Add the line:

return ( pic ) ;

to the end of the function. That should fix things. By the way, if your version of LoadPict() works without this line, you’ve discovered something interesting about the stack. Any guesses?

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »
Explore some of BBCs' most iconic s...
Despite your personal opinion on the BBC at a managerial level, it is undeniable that it has overseen some fantastic British shows in the past, and now thanks to a partnership with Roblox, players will be able to interact with some of these... | Read more »

Price Scanner via MacPrices.net

You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more
Multiple Apple retailers are offering 13-inch...
Several 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... Read more
Roundup of Verizon’s April Apple iPhone Promo...
Verizon is offering a number of iPhone deals for the month of April. Switch, and open a new of service, and you can qualify for a free iPhone 15 or heavy monthly discounts on other models: – 128GB... Read more

Jobs Board

Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.