TweetFollow Us on Twitter

Starting with Dialogs
Volume Number:9
Issue Number:3
Column Tag:Getting Started

Related Info: Dialog Manager

Having a Dialog with Your Mac

Dialogs and alerts via the Macintosh Dialog Manager

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 explored the inner workings of the Menu Manager. This month, we’ll build on that experience and create a program with menus, windows, and, for the first time, dialogs and alerts. Since this program is the biggest one we’ve tackled so far, we’ll break our work down into two parts. This month, we’ll create the resources we’ll need, type in the source code, then take ‘er out for a spin. Next month, we’ll get into the details of the Dialog Manager and explore the elements that make the program work.

Creating the Dialogger Resources

We’ll start off just as we did in last month’s column, by creating our MBAR and MENU resources. Create a folder inside your Development folder named Dialogger ƒ. Next, launch ResEdit and create a new file called Dialogger.Π.rsrc inside this new folder.

Now select Create New Resource from the Resource menu and create a new MBAR resource according to the specifications in Figure 1. As you can see, our program will feature three differents menus.

Figure 1. Specifications for Dialogger’s MBAR resource.

Close the MBAR and MBAR picker windows and again select Create New Resource from the Resource menu. Create a MENU resource according to the specifications in Figure 2. This MENU represents the menu. Be sure that the MENU’s resource id is 128.

Figure 2. Specifications for the MENU resource.

Now create another MENU resource according to the specifications shown in Figure 3. This MENU represents the File menu. Be sure the MENU’s resource id is set to 129.

Figure 3. Specifications for the File MENU resource.

Now create one final MENU resource according to the specifications shown in Figure 4. This MENU represents the Edit menu. Be sure this MENU’s resource id is set to 130.

Figure 4. Specifications for the Edit MENU resource.

OK. Close up your MENU and MENU picker windows. This next part might be a little tricky. You’ll need three PICT images, one of a dog, one of an elephant, and one of a squirrel. If you don’t have any of these handy, drop into your favorite graphics program and do the best you can. If you’re really desparate, just create pictures with the words Afghan, Elephant, and Squirrel in them. Paste all three pictures into your scrapbook.

Back in ResEdit, Paste the pictures into three PICT resources. Use Figure 5 as your guide. Be sure your afghan is PICT 128, your elephant is PICT 129, and that your squirrel is PICT 130.

Figure 5. Three PICT resources. Note which resource id goes with which PICT.

Actually, you can use any PICT images that strike your fancy. Once you understand what this program does and how it works, feel free to customize it to your own desires.

Close the PICT picker window. Once again, select Create New Resource from the Resource menu. This time, create a DLOG resource. A miniature version of your desktop will appear with a window somewhere in the middle. Depending on the size of your monitor, your DLOG window may appear somewhat scaled. When the DLOG editor appears, customize it according to the specifications in Figure 6.

Be sure that you click on the dialog window type (8th from the left) at the top of the editing window. Also, be sure you uncheck the Initially visible and Close box check boxes on the right side of the window. This resource controls the appearance of a dialog box’s window. Now we’ll create a resource that defines the items that appear in this DLOG.

Figure 6. Specifications for the DLOG resource.

Double-click on the DLOG window (not the editing window, but the window that appears in the middle of the editing window, on the mini-desktop) and a DITL editing window will appear. At the same time, a DITL item palette will appear (Figure 7). This palette contains a list of all the items you can add to your dialog.

Figure 7. The DITL item palette.

When you start constructing a DITL (Dialog ITem List), you always start with the OK and Cancel buttons. The OK button is always item number one, and the Cancel button (if it exists) is always item number two. As you’ll learn, these item numbers have special significance to the Dialog Manager.

Click on the Button palette, dragging to the left, on to your DITL window. The outline of a button will appear (Figure 8).

Figure 8. Dragging a button from the palette window.

When you release the mouse button, a new button item will appear. Double click on this button item and a specifications window will appear. Fill it in according to the specs shown in Figure 9. Notice that the Enabled check box is checked. This tells the Dialog Manager to respond to this item when it is clicked on in a dialog box. If the item were not enabled, clicks in it would be ignored.

Figure 9. Specifications for DITL item #1.

Close the spec window and drag a second button off the palette. Double-click on it and enter the specs shown in Figure 10. Close the specs window.

Figure 10. Specifications for DITL item #2.

This time, drag a Radio Button off the palette. Double-click on it and make it reflect the specs shown in Figure 11. Close the specs window.

Figure 11. Specifications for DITL item #3.

Drag a second Radio Button off the palette and make it reflect the specs shown in Figure 12.

Figure 12. Specifications for DITL item #4.

Drag a third Radio Button off the palette and make it reflect the specs shown in Figure 13.

Figure 13. Specifications for DITL item #5.

Next, drag a Static Text item off the palette and make it reflect the specs in Figure 14. Notice that the Enabled check box is unchecked. Normally, clicking in static text is ignored.

Figure 14. Specifications for DITL item #6.

Next, drag a Check Box item off the palette and make it reflect the specs in Figure 15.

Figure 15. Specifications for DITL item #7.

Finally, drag a User Item off the palette and make it reflect the specs in Figure 16. As was the case with Static Text, notice that the Enabled check box is unchecked. A User Item just acts as a marking rectangle. We’ll use it as a guide for drawing a picture, later in the program. There may be times when you want your User Items enabled. For now leave it as is.

Figure 16. Specifications for DITL item #8.

When you close your last item spec window, your DITL editing window should look like the one shown in Figure 17. If it doesn’t, go back and check out your specs. If you need to make changes, you can use the tools in the DITL menu, such as Show Item Numbers, Set Item Number..., and Renumber Items....

Figure 17. The completed DITL resource.

OK, we’re almost done. Close the DITL and DLOG windows till you’re back down to your main window. You might want to save at this point. I’ll wait...

Select Create New Resource from the Resource menu and create an ALRT resource. The ALRT editor looks just like the DLOG editor. It should. Alerts are basically simplified dialogs, easier to use and easier to build. (Top: 40, Left: 40, Bottom: 145, Right: 350).

Select Get Resource Info from the Resource menu and change the ALRT’s resource id to 129. Next, close the Info window and change the DITL ID: field in the ALRT window to 129. We’ll need to create a DITL for the items in the alert and, since we’ve already got a DITL 128, we’ll use 129 for the alert.

In general, it’s a good idea to keep the ALRT and DLOG resource ids in sync with their respective DITL resource ids. DLOG 128 goes with DITL 128 and ALRT 129 goes with DITL 129.

Double-click on the ALRT window inside the mini-desktop. A new DITL resource will appear. You’ll add two items to the DITL. First, create a Button according to the specs in Figure 18.

Figure 18. Specifications for the alert’s OK button.

Next, create a Static Text item item according to the specs in Figure 19. Be sure the Enabled check box is unchecked.

Figure 19. Specifications for DITL item #2.

Whew! That’s it. Quit ResEdit, being sure to save your changes. Let’s get to the project.

Creating the Dialogger Project

Launch THINK C and create a new project file named Dialogger.Π, inside the Dialogger ƒ 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 kMoveToFront (WindowPtr)-1L
#define kNoGoAwayfalse
#define kSleep   60L

#define kFirstRadio3

#define kOn 1
#define kOff0

#define iAfghan  3
#define iElephant4
#define iSquirrel5

#define iShowPreview 7
#define iUserItem8

#define kLeftMargin5
#define kTopMargin 40

#define mApple   kBaseResID
#define iAbout   1

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

/*  Globals  */

Boolean gDone, gShowPreview = true;
short   gCurrentPICT = kBaseResID;

/*  Functions  */

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

void    DrawPreview( DialogPtr dialog, short picID );
void    SwitchPICT( void );

/* see tech note 304 */
pascal OSErr SetDialogDefaultItem(DialogPtr theDialog, 
 short newItem) 
   = { 0x303C, 0x0304, 0xAA68 };        
pascal OSErr SetDialogCancelItem(DialogPtr theDialog, 
 short newItem)
   = { 0x303C, 0x0305, 0xAA68 };

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

void  main( void )

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

void  ToolBoxInit( void )
 InitGraf( &thePort );
 InitDialogs( NULL );

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

PicHandle LoadPICT( short picID )
 pic = GetPicture( picID );
 if ( pic == NULL )
 SysBeep( 10 );  /*  Couldn’t load the PICT resource!!!  */

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

void  CreateWindow( void )
 Rect   r;
 pic = LoadPICT( gCurrentPICT );
 r = (**pic).picFrame;
 OffsetRect( &r, kLeftMargin - r.left,
 kTopMargin - );
 window = NewWindow( NULL, &r, “\pMy Pet Fred”, kVisible,
 noGrowDocProc, kMoveToFront, kNoGoAway, 0L );
 if ( window == NULL )
 SysBeep( 10 );  /*  Couldn’t load the WIND resource!!!  */
 ShowWindow( window );
 SetPort( window );

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

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

 menu = GetMHandle( mApple );
 AddResMenu( menu, ‘DRVR’ );

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

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

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

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

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

void  HandleMouseDown( EventRecord *eventPtr )
 short  thePart;
 long   menuChoice;
 long   windSize;
 Rect   growRect;
 thePart = FindWindow( eventPtr->where, &window );
 switch ( thePart )
 case inMenuBar:
 menuChoice = MenuSelect( eventPtr->where );
 HandleMenuChoice( menuChoice );
 case inSysWindow : 
 SystemClick( eventPtr, window );
 case inContent:
 SelectWindow( window );
 case inDrag : 
 DragWindow( window, eventPtr->where, 
 &screenBits.bounds );

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

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

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

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

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

void  HandleFileChoice( short item )
 short  newPICTid;
 switch ( item )
 case iSettings:
 case iQuit:
 gDone = true;

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

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

/************************************* DoDialog     */

void  DoDialog( void )
 BooleandialogDone = false;
 short  itemHit, itemType;
 Handle itemHandle;
 Rect   itemRect;
 short  curRadioButton;
 dialog = GetNewDialog( kDialogResID, NULL, kMoveToFront );

 ShowWindow( dialog );
 SetPort( dialog );
 SetDialogDefaultItem( dialog, ok );
 SetDialogCancelItem( dialog, cancel );
 curRadioButton = gCurrentPICT - kBaseResID + kFirstRadio;
 GetDItem( dialog, curRadioButton, &itemType, 
 &itemHandle, &itemRect );
 SetCtlValue( (ControlHandle)itemHandle, kOn );
 if ( gShowPreview )
 GetDItem( dialog, iShowPreview, &itemType, 
 &itemHandle, &itemRect );
 SetCtlValue( (ControlHandle)itemHandle, kOn );
 DrawPreview( dialog, curRadioButton + 
 kBaseResID - kFirstRadio );

 while ( ! dialogDone )
 ModalDialog( NULL, &itemHit );
 switch( itemHit )
 case ok:
 case cancel:
 dialogDone = true;
 case iShowPreview:
 GetDItem( dialog, iShowPreview, &itemType,
 &itemHandle, &itemRect );
 FlipControl( (ControlHandle)itemHandle );
 DrawPreview( dialog, curRadioButton + 
 kBaseResID - kFirstRadio );
 case iAfghan:
 case iElephant:
 case iSquirrel:
 if ( curRadioButton != itemHit )
 GetDItem( dialog, curRadioButton, &itemType,
 &itemHandle, &itemRect );
 FlipControl( (ControlHandle)itemHandle );
 GetDItem( dialog, itemHit, &itemType,
 &itemHandle, &itemRect );
 FlipControl( (ControlHandle)itemHandle );
 curRadioButton = itemHit;
 DrawPreview( dialog, curRadioButton + 
 kBaseResID - kFirstRadio );
 HideWindow( dialog );
 if ( itemHit == ok )
 GetDItem( dialog, iShowPreview, &itemType,
 &itemHandle, &itemRect );
 if ( GetCtlValue( (ControlHandle)itemHandle ) == kOn )
 gShowPreview = true;
 gShowPreview = false;
 if ( gCurrentPICT != curRadioButton +
 kBaseResID - kFirstRadio )
 gCurrentPICT = curRadioButton +
 kBaseResID - kFirstRadio;
 DisposDialog( dialog );

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

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

/************************************* DrawPreview  */

void  DrawPreview( DialogPtr dialog, short picID )
 short  itemHit, itemType;
 Handle itemHandle;
 Rect   itemRect;
 GetDItem( dialog, iShowPreview, &itemType, 
 &itemHandle, &itemRect );
 if ( GetCtlValue( (ControlHandle)itemHandle ) == kOff )
 GetDItem( dialog, iUserItem, &itemType, 
 &itemHandle, &itemRect );
 EraseRect( &itemRect );
 pic = LoadPICT( picID );
 GetDItem( dialog, iUserItem, &itemType, 
 &itemHandle, &itemRect );
 FrameRect( &itemRect );
 InsetRect( &itemRect, 1, 1 );
 DrawPicture( pic, &itemRect );

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

void  SwitchPICT( void )
 window = FrontWindow();
 DisposeWindow( window );

Running Dialogger

Save your source code as Dialogger.c and add it to the project. When you run the project, a window should appear, just big enough to hold PICT 128. If you made your PICTs too small, go back and fix them. Figure 20 shows my window.

Figure 20. My Pet Fred.

Go to the File menu and select Settings. Play with all the dialog items. Change some things, then select Cancel, verifying that things stay the same the next time you go into the dialog box. Uncheck the Show preview check box to see what happens. Play, play play!

Figure 21. The Settings... dialog box.

Till Next Month

I think I could have written another twenty pages on this topic, but my wicked editor has threatened bodily harm if I keep rambling on. I’ll get into all the details next month. See you then... [Insert the sound of whips here. -Ed.]


Published in Getting Started, May 1993:

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:

/* correction needs to be added to the bottom of LoadPICT() */

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

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 »
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 »

Price Scanner via

Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
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

Jobs Board

*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
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
Nurse Anesthetist - *Apple* Hill Surgery Ce...
Nurse Anesthetist - Apple Hill Surgery Center Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
Housekeeper, *Apple* Valley Village - Cassi...
Apple Valley Village Health Care Center, a senior care campus, is hiring a Part-Time Housekeeper to join our team! We will train you for this position! In this role, Read more
Sublease Associate Optometrist- *Apple* Val...
Sublease Associate Optometrist- Apple Valley, CA- Target Optical Date: Apr 20, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92307 **Requisition Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.