TweetFollow Us on Twitter

Window Related Events
Volume Number:9
Issue Number:1
Column Tag:Getting Started

Related Info: Event Manager Window Manager

Window Related Events

Handling update, activate and suspend/resume events

By Dave Mark, MacTech Magazine Regular Contributing Author

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

In last month’s column, we covered the basics of event loop programming, focusing on four specific events: the mouseDown, mouseUp, keyDown, and autoKey events. If you haven’t already, be sure to read through the Event Manager chapters in Inside Macintosh, Volumes I and VI. Pay specific attention to the sections that describe the mouseDown, mouseUp, keyDown, and autoKey events in detail. Finally, go back to last month’s program and flesh it out a little. When a mouseDown occurs, draw a string in the event window that describes where and when the mouseDown occurred. For a keyDown, draw the character and key codes embedded in the EventRecord.

In this month’s column, we’re going to expand our event handling repertoire, focusing on activate, update, and suspend/resume events.

Activate Events

Every time your application creates a window, that window is added to a list maintained by the Window Manager. The windows in this list are in the order that they appear on the screen, from the frontmost to the rearmost. The frontmost window is also known as the active window. In Figure 1, Window #1 starts off as the active window. Notice the difference between the title bars of the active and non-active window.

When the mouse is clicked in the rear window, Window #1 is made inactive, then the rear window, Window #2, is made active. Since the front window is made inactive before another window is brought to the front, there will never be more than one active window at a time!

The Window Manager accomplishes this by sending your application a series of activate events. If a rear window is being brought to the front, your application will receive two activate events. The first tells you that the frontmost window is becoming inactive. This event is also known as a deactivate event. The second event tells your application that a window is becoming active. Both of these events set EventRecord.what to activateEvt.

If a window is created, and there are no currently open windows, the Window Manager only generates a single activate event, indicating that the newly created window is becoming active. In this case, no deactivate event is sent.

Figure 1. A mouse click in Window #2 brings it to the front.

In general, you’ll use activate events if you treat window contents differently for an active window than for an inactive window. For example, your application might highlight selected text in the active window, but not in an inactive window. Activate events are provided for your benefit. Use them as you see fit. You’ll see how we discriminate between an activate and deactivate event when we get to our program later in the column.

Figure 2. When Window #1 is dragged to the left, more of Window #2 is exposed, causing an update event.

Update Events

Next on our list of events is an event that tells your program to update the contents of a window. The updateEvt is generated when a window is created (after the activateEvt is generated) and when a new portion of a window is revealed. Figure two shows a typical sequence that generates an updateEvt. The first picture shows Window #1 partially obscuring Window #2. Next, Window #1 is dragged to the left, revealing a previously hidden section of the Window #2. The Window Manager sends an updateEvt to your application, telling it to update the contents of Window #2. The third picture shows the window, after the program responded to the update event.

Figure 3 shows a slightly different sequence, involving update and activate events. This time, the mouse is clicked in Window #2, bringing it to the front. First, a deactivate event is generated for Window #1. Next, an activate event is generated for Window #2. Finally, an update event is generated, asking the program to update the contents of Window #2.

Once again, you’ll see update and activate events in action in the program later in the column.

Figure 3. When Window #2 is moved to the front, two activate events and an update event are generated.

Suspend/Resume Events

Under MultiFinder (System 6) and under System 7, more than one application is allowed to run at a time. When you click in a window belonging to a background application, the active application receives a suspend event, and the background application receives a resume event and is moved to the foreground.

You might use suspend and resume events to determine the actions taken by your program. For example, in the foreground, you might display a special tool palette, or perhaps run an animation. When your application moves into the background, you’ll receive a suspend event and you might hide the tool palette, or discontinue the animation until you get a resume event.

It’s important to note that your application can continue running, even if it receives a suspend event. It will still continue to receive update (and other appropriate events) while in the background. At the very least, it’s a good idea to treat a suspend event as you would a deactivate event when it comes to the contents of your windows. For example, if you usually deselect any selected text when a window is deactivated, do the same thing for the active window when you receive a suspend event.

At Last! The Program!

As promised, here’s a program that incorporates all the events described in this column. WindowMaster creates a single window and handles all the usual events relating to windows. Figure 4 shows the WindowMaster window in all its glory. Notice that this window sports a close box, a zoom box, a drag region (the title bar), and a grow box. Though this window doesn’t support scroll bars, there is room for them. In most cases, if a window has a grow box, it has room for scroll bars. We’ll get to scroll bars in a later column.

Figure 4. WindowMaster in action.

Creating the WindowMaster Resources

Create a folder in your development folder named WindowMaster. Launch ResEdit, then create a resource file named WindowMaster.Π.rsrc in your WindowMaster folder. You’ll create two resources in this file.

First, create a WIND resource according to the specifications in Figure 5. Next, select Set 'WIND' Characteristics... from the WIND menu and set the window’s title to PICT 128. Next, copy a graphic from your scrapbook into the clipboard. If you don’t have anything interesting in your scrapbook, go draw something. I’ll wait.

Figure 5. Specifications for WindowMaster’s WIND resource.

Once you’ve got a graphic in the clipboard, return to ResEdit and select Paste from the Edit menu. ResEdit will place your picture in a PICT resource. Make sure the PICT resource has a resource ID of 128. Well, that’s it for ResEdit. Quit, making sure you save your changes.

Creating the WindowMaster Project

Once you’re out of ResEdit, launch THINK C and create a new project file named WindowMaster.Π in the WindowMaster folder. Add MacTraps to the project file. Next, select New from the File menu and type this source code into the window that appears.

/* 1 */
#include <Values.h>

#define kBaseResID 128
#define kMoveToFront (WindowPtr)-1L
#define kSleep   MAXLONG

#define kScrollBarAdjust  (16-1)
#define kLeaveWhereItIs   false
#define kNormalUpdates    true

#define kMinWindowHeight  50
#define kMinWindowWidth   80

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

Boolean gDone;

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

void  ToolBoxInit( void );
void  WindowInit( void );
void  EventLoop( void );
void  DoEvent( EventRecord *eventPtr );
void  HandleMouseDown( EventRecord *eventPtr );
void  DoUpdate( EventRecord *eventPtr );
void  DoPicture( WindowPtr window, PicHandle picture );
void  DoActivate( WindowPtr window, Boolean becomingActive );
void  DoSuspendResume( Boolean resuming );
void  CenterPict( PicHandle picture, Rect *srcRectPtr,
 Rect *destRectPtr );


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

void  main( void )
{
 ToolBoxInit();
 WindowInit();
 
 EventLoop();
}

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

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

/******************************** WindowInit *********/

void  WindowInit( void )
{
 WindowPtrwindow;
 
 window = GetNewWindow( kBaseResID, nil, kMoveToFront );
 
 if ( window == nil )
 {
 SysBeep( 10 );  /*  Couldn't load the WIND resource!!!  */
 ExitToShell();
 }
 
 ShowWindow( window );
 SetPort( window );
}
/******************************** EventLoop *********/

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

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

void  DoEvent( EventRecord *eventPtr )
{
 BooleanbecomingActive, resuming;
 
 switch ( eventPtr->what )
 {
 case mouseDown: 
 HandleMouseDown( eventPtr );
 break;
 case updateEvt:
 DoUpdate( eventPtr );
 break;
 case activateEvt:
 becomingActive = ( (eventPtr->modifiers & activeFlag)
 == activeFlag );
 DoActivate( (WindowPtr)eventPtr->message, 
 becomingActive );
 break;
 case osEvt:
 resuming = ( eventPtr->message & suspendResumeMessage )
 == resumeFlag;
 DoSuspendResume( resuming );
 break;
 }
}

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

void  HandleMouseDown( EventRecord *eventPtr )
{
 WindowPtrwindow;
 short  thePart;
 GrafPtroldPort;
 long   windSize;
 Rect   growRect;
 
 thePart = FindWindow( eventPtr->where, &window );
 
 switch ( thePart )
 {
 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 ) )
 gDone = true;
 break;
 case inGrow:
 growRect.top = kMinWindowHeight;
 growRect.left = kMinWindowWidth;
 growRect.bottom = MAXSHORT;
 growRect.right = MAXSHORT;
 
 windSize = GrowWindow( window, eventPtr->where, 
 &growRect );
 if ( windSize != 0 )
 {
 GetPort( &oldPort );
 SetPort( window );
 EraseRect( &window->portRect );
 SizeWindow( window, LoWord( windSize ),
 HiWord( windSize ), kNormalUpdates );
 InvalRect( &window->portRect );
 SetPort( oldPort );
 }
 break;
 case inZoomIn:
 case inZoomOut:
 if ( TrackBox( window, eventPtr->where, thePart ) )
 {
 GetPort( &oldPort );
 SetPort( window );
 EraseRect( &window->portRect );
 ZoomWindow( window, thePart, kLeaveWhereItIs );
 InvalRect( &window->portRect );
 SetPort( oldPort );
 }
 break;
 }
}

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

void  DoUpdate( EventRecord *eventPtr )
{
 short  pictureID;
 PicHandlepicture;
 WindowPtrwindow;
 
 window = (WindowPtr)eventPtr->message;
 
 BeginUpdate( window );
 
 picture = GetPicture( kBaseResID );
 
 if ( picture == nil )
 {
 SysBeep( 10 );  /*  Couldn't load the PICT resource!!!  */
 ExitToShell();
 }
 
 DoPicture( window, picture );
 
 EndUpdate( window );
}

/******************************** DoPicture *********/

void  DoPicture( WindowPtr window, PicHandle picture )
{
 Rect   drawingClipRect, destRect;
 RgnHandletempRgn;
 
 SetPort( window );
 
 EraseRect( &window->portRect );
 
 tempRgn = NewRgn();
 GetClip( tempRgn );

 drawingClipRect = window->portRect;
 drawingClipRect.right -= kScrollBarAdjust;
 drawingClipRect.bottom -= kScrollBarAdjust;
 
 ClipRect( &drawingClipRect );
 
 CenterPict( picture, &drawingClipRect, &destRect );
 DrawPicture( picture, &destRect );
 
 SetClip( tempRgn );
 DisposeRgn( tempRgn );
 
 DrawGrowIcon( window );
}

/************************************* DoActivate   */



void  DoActivate( WindowPtr window, Boolean becomingActive )
{
 DrawGrowIcon( window );
}

/************************************* DoSuspendResume    */

void  DoSuspendResume( Boolean resuming )
{
 WindowPtrwindow;
 
 window = FrontWindow();
 
 DrawGrowIcon( window );
}

/****************** CenterPict ********************/

void  CenterPict( PicHandle picture, Rect *srcRectPtr,
 Rect *destRectPtr )
{
 Rect pictRect;
 
 pictRect = (**( picture )).picFrame;
 
 OffsetRect( &pictRect, srcRectPtr->left - pictRect.left,
    srcRectPtr->top - pictRect.top);
 OffsetRect( &pictRect,
 (srcRectPtr->right - pictRect.right)/2,
 (srcRectPtr->bottom - pictRect.bottom)/2);
 
 *destRectPtr = pictRect;
}

Once your code is typed in, save it under the name WindowMaster.c, then add the window to the project by selecting Add from the Project menu.

Running WindowMaster

Select Run from the Project menu to run WindowMaster. A single window, like the one shown in Figure 4 should appear. Click the mouse in the title bar and drag the window around on the screen. Next, click in the grow box and resize the window. Try making it tall and skinny, then short and fat. Notice that there is a limit to how small you can make the window. Next, click on the zoom box in the upper-right corner of the window. The window should expand to fill the main screen. Click the zoom-box again to return the window to its previous size.

Next, drag the window downwards, so it is halfway off the screen, obscuring part of the picture. Now drag it back up. The picture will be redrawn in full. Finally, click in the window’s close box to exit the program.

Walking Through the Source Code

Just like last month’s program, WindowMaster is based on an event loop architecture. Also, like last month’s column, the program starts off by including <Values.h> to get the constant MAXLONG, used for the #define kSleep. The #defines will be explained as they are used.

/* 2 */
#include <Values.h>

#define kBaseResID 128
#define kMoveToFront (WindowPtr)-1L
#define kSleep   MAXLONG

#define kScrollBarAdjust  (16-1)
#define kLeaveWhereItIs   false
#define kNormalUpdates    true

#define kMinWindowHeight  50
#define kMinWindowWidth   80

gDone is initialized to false, then set to true when a click occurs in the window’s close box. Note that this is not the way Mac applications normally exit, but we haven’t got to menus yet, so a click in the close box will have to do for now.

/* 3 */
Boolean gDone;

As always, the code includes prototypes for all functions.

/* 4 */
void  ToolBoxInit( void );
void  WindowInit( void );
void  EventLoop( void );
void  DoEvent( EventRecord *eventPtr );
void  HandleMouseDown( EventRecord *eventPtr );
void  DoUpdate( EventRecord *eventPtr );
void  DoPicture( WindowPtr window, PicHandle picture );
void  DoActivate( WindowPtr window, Boolean becomingActive );
void  DoSuspendResume( Boolean resuming );
void  CenterPict( PicHandle picture, Rect *srcRectPtr,
 Rect *destRectPtr );

main() initializes the Toolbox, creates a window, then enters the main event loop.

/* 5*/
/******************************** main *********/

void  main( void )
{
 ToolBoxInit();
 WindowInit();
 
 EventLoop();
}

ToolBoxInit() is the same as it ever was.

/* 6 */
/*********************************** ToolBoxInit */

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

WindowInit() calls GetNewWindow() to load the WIND resource from the resource file.

/* 7 */
/******************************** WindowInit *********/

void  WindowInit( void )
{
 WindowPtrwindow;
 
 window = GetNewWindow( kBaseResID, nil, kMoveToFront );

If the resource wasn’t found, beep once, then exit.

/* 8 */
 if ( window == nil )
 {
 SysBeep( 10 );  /*  Couldn't load the WIND resource!!!  */
 ExitToShell();
 }

Once the window is created, make it visible, then make it the current port. Notice that this routine does not do any drawing. We’ll draw our picture in response to an update event.

/* 9 */
 ShowWindow( window );
 SetPort( window );
}

EventLoop() sets gDone to false, then loops around a call to WaitNextEvent(). If WaitNextEvent() returns true, the event is passed to DoEvent().

/* 10 */
/******************************** EventLoop *********/

void  EventLoop( void )
{
 EventRecordevent;
 
 gDone = false;
 while ( gDone == false )
 {
 if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
 DoEvent( &event );
 }
}
DoEvent() switches on the event’s what field.

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

void  DoEvent( EventRecord *eventPtr )
{
 BooleanbecomingActive, resuming;
 
 switch ( eventPtr->what )
 {

A mouseDown event is passed to HandleMouseDown(). An updateEvt is passed on to DoUpdate().

/* 11 */
 case mouseDown: 
 HandleMouseDown( eventPtr );
 break;
 case updateEvt:
 DoUpdate( eventPtr );
 break;

In the case of an activate event, the event’s modifiers field holds the key to whether the event is an activate or deactivate event. activeFlag is a mask that designates one of the bits in the modifiers field. If the bit is set, the event is an activate event. If the bit is clear, the event is a deactivate event. The Boolean becomingActive is true if the event is an activate event. Once becomingActive is set, it is passed on to DoActivate(). The event’s message field holds a pointer to the window being activated or deactivated.

/* 12 */
 case activateEvt:
 becomingActive = ( (eventPtr->modifiers & activeFlag)
 == activeFlag );
 DoActivate( (WindowPtr)eventPtr->message,
 becomingActive );
 break;

Similarly, an osEvt is used to indicate either a suspend or resume event. suspendResumeMessage is a predefined constant that designates the suspend/resume bit in the message field. resuming is set to true if the event is a resume event. Once set, resuming is passed on to DoSuspendResume().

/* 13 */
 case osEvt:
 resuming = ( eventPtr->message & suspendResumeMessage )
 == resumeFlag;
 DoSuspendResume( resuming );
 break;
 }
}

HandleMouseDown() handles the mouseDown event.

/* 14 */
/************************************* HandleMouseDown */

void  HandleMouseDown( EventRecord *eventPtr )
{
 WindowPtrwindow;
 short  thePart;
 GrafPtroldPort;
 long   windSize;
 Rect   growRect;

FindWindow() takes the event’s where field and returns the window at those coordinates. thePart indicates the part of the window the mouse click occurred in.

/* 15 */
 thePart = FindWindow( eventPtr->where, &window );

If the click was in a portion of the screen not belonging to our application (like a desk accessory window), thePart is set to inSysWindow and we’ll pass the event back to the system with SystemClick().

/* 16 */
 switch ( thePart )
 {
 case inSysWindow : 
 SystemClick( eventPtr, window );
 break;

If the event was in the content region of the window, we’ll typically call SelectWindow() to bring the window to the front. Since we only have one window, this line isn’t particularly useful. Later on though, you’ll add another window to this program and you’ll want to keep this code in here.

As your windows get more complex, you’ll want to do more with inContent clicks than just select the window. You might have a button or scroll bar in the window that needs action, or you might have some text that needs selection. This is the jumping off point for all clicks that occur in the window. Eventually, we’ll add a routine named DoContentClick() to process clicks in a window’s content region.

/* 17 */
 case inContent:
 SelectWindow( window );
 break;

If the click was in the window’s drag region (title bar), we’ll pass the window (retrieved by FindWindow()), and the mouse click coordinates to the Toolbox routine DragWindow(). The third parameter is a bounding rectangle that determines where on the screen the window may be dragged. screenBits.bounds is a System global variable that defines the boundaries of the main display.

If you have two monitors, this code works fine, however. DragWindow() checks for screenBits.bounds as a parameter and, if it finds it, allows dragging anywhere on any monitor attached to the system.

/* 18 */
 case inDrag : 
 DragWindow( window, eventPtr->where, 
 &screenBits.bounds );
 break;

If the mouseDown was in the close box, we’ll call TrackGoAway() to see if the mouse was released while still inside the close box. If so, gDone is set to true. TrackGoAway() is the routine that does the little animation in the close box.

/* 19 */
 case inGoAway :
 if ( TrackGoAway( window, eventPtr->where ) )
 gDone = true;
 break;

If the click was in the grow box, we set up a rectangle that defines how large and how small the window is allowed to grow [See Inside Macintosh, Vol. 1 - Tech Ed.]. Basically, it’s good policy to put a limit on how small a window can get, but, unless you’ve got a pressing reason, you should allow windows to get as large as the user wants.

/* 20 */
 case inGrow:
 growRect.top = kMinWindowHeight;
 growRect.left = kMinWindowWidth;
 growRect.bottom = MAXSHORT;
 growRect.right = MAXSHORT;

Next, this rectangle is passed on to GrowWindow(), which tracks the mouse, allowing the user to specify the new window size.

/* 21 */
 windSize = GrowWindow( window, eventPtr->where, &growRect );

The return value contains two 2-byte values, indicating the new height and width of the window [Both zero means no change - Tech Ed.].

/* 22 */
 if ( windSize != 0 )
 {

First, we’ll save away the current port (in case it’s not this window), then make this window the current port. Next, we erase the entire window and change the window’s size by calling SizeWindow(). The last parameter tells the system we want this resizing to generate an update event.

/* 23 */
 GetPort( &oldPort );
 SetPort( window );
 EraseRect( &window->portRect );
 SizeWindow( window, LoWord( windSize ),
 HiWord( windSize ), kNormalUpdates );

Next, we call InvalRect() to tell the system that the entire window should be redrawn with the next update event, not just the new area revealed by the grow box. In fact, this call will force an update even if the window was shrunk.

/* 24 */
 InvalRect( &window->portRect );

Because we center our picture in the window, we need to redraw the window contents whenever the window changes size. To see why this is true, try commenting out the previous line of code.

Update events are tricky. It is definitely worth reading the section of Inside Macintosh that covers the Window Manager. You might also check out Chapter 4’s Updater program in Volume I of the Mac Primer.

Once the window is resized, the port is set back to its old value. Since InvalRect() applies to the current port, it was important that we make the window being grown the current port.

/* 25 */
 SetPort( oldPort );
 }
 break;

Finally, a click in the zoom box follows a similar strategy. Again, TrackBox() is called to verify that the mouse was released inside the zoom box. If so, ZoomWindow() is called to zoom the window, or return it to its old position, depending on its current state.

/* 26 */
 case inZoomIn:
 case inZoomOut:
 if ( TrackBox( window, eventPtr->where, thePart ) )
 {
 GetPort( &oldPort );
 SetPort( window );
 EraseRect( &window->portRect );
 ZoomWindow( window, thePart, kLeaveWhereItIs );
 InvalRect( &window->portRect );
 SetPort( oldPort );
 }
 break;
 }
}

DoUpdate() is called whenever an update event occurs. First, the window is retrieved from the event’s message field. Next, BeginUpdate() is called, telling the Window Manager that we’re handling updates for this window. BeginUpdate() will restrict drawing to the region of the window that needs updating, also known as the update region. If you call InvalRect() on the whole window, the whole window is available for drawing.

/* 27 */
/************************************* DoUpdate     */

void  DoUpdate( EventRecord *eventPtr )
{
 short  pictureID;
 PicHandlepicture;
 WindowPtrwindow;
 
 window = (WindowPtr)eventPtr->message;
 
 BeginUpdate( window );

Next, we load the picture and pass it on to DoPicture().

/* 28 */
 picture = GetPicture( kBaseResID );
 
 if ( picture == nil )
 {
 SysBeep( 10 );  /*  Couldn't load the PICT resource!!!  */
 ExitToShell();
 }
 
 DoPicture( window, picture );
 
 EndUpdate( window );
}

DoPicture() makes the window the current port, then erases the entire window. Note that this will already have been done if the update event was caused by a resize or zoom, but there’s no harm in this minor duplication of code.

/* 29 */

/******************************** DoPicture *********/

void  DoPicture( WindowPtr window, PicHandle picture )
{
 Rect   drawingClipRect, destRect;
 RgnHandletempRgn;
 
 SetPort( window );
 
 EraseRect( &window->portRect );

Next, a new region is created. We pass this region handle on to GetClip(), which places a copy of the window’s clipping region in tempRgn. The clipping region defines which parts of the window can be drawn in and which parts can’t. Again, it is a good idea to read the entire Window Manager chapter in Inside Macintosh, as there’s not enough space to cover all of it in this column.

/* 30 */
 tempRgn = NewRgn();
 GetClip( tempRgn );

Since we want to center the picture in the part of the window not covered by the grow region and scroll bar areas, we’ll subtract those areas from the window’s portRect. Next, we’ll pass this rectangle on to ClipRect() making it the new clip region.

/* 31 */
 drawingClipRect = window->portRect;
 drawingClipRect.right -= kScrollBarAdjust;
 drawingClipRect.bottom -= kScrollBarAdjust;
 
 ClipRect( &drawingClipRect );

Next, we’ll call CenterPict() to center the picture in drawingClipRect, returning the rectangle the picture should be drawn in as its third parameter. We then draw the picture with DrawPicture().

/* 32 */
 CenterPict( picture, &drawingClipRect, &destRect );
 DrawPicture( picture, &destRect );

Next, the clipping region is set back to its old value and the grow icon is redrawn, completing our update.

/* 33 */
 SetClip( tempRgn );
 DisposeRgn( tempRgn );
 
 DrawGrowIcon( window );
}

It’s important to remember that your application can receive update events even if it’s running in the background. If a foreground application’s window covers your application’s windows and is then moved, the Window Manager will send your application update events asking you to redraw the effected windows.

DoActivate() is pretty simple. It redraws the grow icon in the specified window. DrawGrowIcon() will draw a different grow icon, depending on whether your window is frontmost or not. When your application starts handling more than one window, you’ll start making use of the second parameter, becomingActive.

/* 34 */

/************************************* DoActivate   */

void  DoActivate( WindowPtr window, Boolean becomingActive )
{
 DrawGrowIcon( window );
}

DoSuspendResume() is also pretty simple. This time the frontmost window’s grow icon is redrawn. A front window, which has been suspended gets the same grow icon as any other window. DrawGrowIcon() is smart enough to know the state of your application.

Once again, as your programs get more complex, you’ll make use of the second parameter, resuming.

/* 35 */
/************************************* DoSuspendResume    */

void  DoSuspendResume( Boolean resuming )
{
 WindowPtrwindow;
 
 window = FrontWindow();
 
 DrawGrowIcon( window );
}

CenterPict() centers the picture in the rectangle pointed to by srcRectPtr, returning the result in the rectangle pointed to by destRectPtr.


/* 36 */
/****************** CenterPict ********************/

void  CenterPict( PicHandle picture, Rect *srcRectPtr,
 Rect *destRectPtr )
{
 Rect pictRect;
 
 pictRect = (**( picture )).picFrame;
 
 OffsetRect( &pictRect, srcRectPtr->left - pictRect.left,
    srcRectPtr->top - pictRect.top);
 OffsetRect( &pictRect,
 (srcRectPtr->right - pictRect.right)/2,
 (srcRectPtr->bottom - pictRect.bottom)/2);
   
 *destRectPtr = pictRect;
}

Till Next Month

There’s a lot going on in this program. As I’ve said before, do yourself a favor and read the Window Manager chapter in Inside Macintosh. If a concept in this program seems fuzzy, try commenting out some of the code to see what happens. This is especially useful with the code that effects or handles update events. You might also try commenting out the calls to DrawGrowIcon() just to see what happens.

Finally, try adding another window to the program. Add a second WIND and a second PICT resource to the resource file, then create the new window in WindowInit(). You’ll be amazed how easy it is to add a second window. Go ahead, try it!

By the way, you may have noticed that this month’s program comes in C only. Sorry Pascal fans, but the programs (and this column) are just getting too big and I had to choose one language or the other. Hopefully, you now have enough examples of C and Pascal that you’ll be able to either come up to speed on C (my preference) or translate the code to Pascal yourself.

By the way, for those of you following Daniel’s career, he’s getting big (would you believe he’s already 16 pounds, and he’s only 12 weeks old!), and he smiles an awful lot. Till next month, keep programming and Happy Holidays!!!

 
AAPL
$98.15
Apple Inc.
-0.23
MSFT
$43.58
Microsoft Corpora
-0.31
GOOG
$587.42
Google Inc.
+1.81

MacTech Search:
Community Search:

Software Updates via MacUpdate

Knock 1.1.7 - Unlock your Mac by knockin...
Knock is a faster, safer way to sign in. You keep your iPhone with you all the time. Now you can use it as a password. You never have to open the app -- just knock on your phone twice, even when it's... Read more
Mellel 3.3.6 - Powerful word processor w...
Mellel is the leading word processor for OS X and has been widely considered the industry standard since its inception. Mellel focuses on writers and scholars for technical writing and multilingual... Read more
LibreOffice 4.3.0.4 - Free Open Source o...
LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more
Freeway Pro 7.0 - Drag-and-drop Web desi...
Freeway Pro lets you build websites with speed and precision... without writing a line of code! With it's user-oriented drag-and-drop interface, Freeway Pro helps you piece together the website of... Read more
Drive Genius 3.2.4 - Powerful system uti...
Drive Genius is an OS X utility designed to provide unsurpassed storage management. Featuring an easy-to-use interface, Drive Genius is packed with powerful tools such as a drive optimizer, a... Read more
Vitamin-R 2.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
Toast Titanium 12.0 - The ultimate media...
Toast Titanium goes way beyond the very basic burning in the Mac OS and iLife software, and sets the standard for burning CDs, DVDs, and now Blu-ray discs on the Mac. Create superior sounding audio... Read more
OS X Yosemite Wallpaper 1.0 - Desktop im...
OS X Yosemite Wallpaper is the gorgeous new background image for Apple's upcoming OS X 10.10 Yosemite. This wallpaper is available for all screen resolutions with a source file that measures 5,418... Read more
Acorn 4.4 - Bitmap image editor. (Demo)
Acorn is a new image editor built with one goal in mind - simplicity. Fast, easy, and fluid, Acorn provides the options you'll need without any overhead. Acorn feels right, and won't drain your bank... Read more
Bartender 1.2.20 - Organize your menu ba...
Bartender lets you organize your menu bar apps. Features: Lets you tidy your menu bar apps how you want. See your menu bar apps when you want. Hide the apps you need to run, but do not need to... Read more

Latest Forum Discussions

See All

Super Heavy Sword (Games)
Super Heavy Sword 0.0.1 Device: iOS Universal Category: Games Price: $.99, Version: 0.0.1 (iTunes) Description: Get Ready to Get HEAVY! Monster Robot Studios presents SUPER Heavy Sword! The sequel to the smash hit HEAVY sword which... | Read more »
Angels In The Sky (Games)
Angels In The Sky 1.00 Device: iOS Universal Category: Games Price: $6.99, Version: 1.00 (iTunes) Description: - This game is only for the iPhone 5s. please do not use the iPad, iPhone 5 or earlier devices.- Just touch or holding... | Read more »
80 Days (Games)
80 Days 1.0.2 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.2 (iTunes) Description: 1872, with a steampunk twist. Phileas Fogg has wagered he can circumnavigate the world in just eighty days. Choose your own route... | Read more »
Micromon (Games)
Micromon 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: 130+ Animated Monsters to Catch & Battle! No waiting, play at your own pace! Embark on an epic monster capture RPG like none... | Read more »
Empire Manager (Games)
Empire Manager 1.0 Device: iOS iPhone Category: Games Price: $3.99, Version: 1.0 (iTunes) Description: Become ruler of an empire. Manage your economy, develop technology, hire an army and conquer the world in this addictive turn-... | Read more »
Empire Manager HD (Games)
Empire Manager HD 1.0 Device: iOS Universal Category: Games Price: $7.99, Version: 1.0 (iTunes) Description: Become ruler of an empire. Manage your economy, develop technology, hire an army and conquer the world in this addictive... | Read more »
Star Admiral Review
Star Admiral Review By Rob Thomas on July 30th, 2014 Our Rating: :: ADMIRABLE ADMIRALSUniversal App - Designed for iPhone and iPad While this new digital CCG may feel a bit familiar, Star Admiral offers a sci-fi twist and galaxy’s... | Read more »
Zap! Pow! Become a Badass Wizard in Phan...
Zap! Pow! | Read more »
Urban Trial Freestyle Review
Urban Trial Freestyle Review By Blake Grundman on July 30th, 2014 Our Rating: :: RIDIN' DIRTYUniversal App - Designed for iPhone and iPad A rough ride that has trouble keeping its wheels on the track.   | Read more »
Take Note! Noteshelf Has Recieved a Big...
Take Note! Noteshelf Has Recieved a Big Update. Posted by Jessica Fisher on July 30th, 2014 [ permalink ] iPad Only App - Designed for the iPad | Read more »

Price Scanner via MacPrices.net

Save $50 on the 2.5GHz Mac mini, plus free sh...
B&H Photo has the 2.5GHz Mac mini on sale for $549.99 including free shipping. That’s $50 off MSRP, and B&H will also include a free copy of Parallels Desktop software. NY sales tax only. Read more
Save up to $140 on an iPad Air with Apple ref...
Apple is offering Certified Refurbished iPad Airs for up to $140 off MSRP. Apple’s one-year warranty is included with each model, and shipping is free. Stock tends to come and go with some of these... Read more
$250 price drop on leftover 15-inch Retina Ma...
B&H Photo has dropped prices on 2013 15″ Retina MacBook Pros by $250 off original MSRP. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.3GHz Retina MacBook Pro: $2249, $250 off... Read more
More iPad Upgrade Musings – The ‘Book Mystiqu...
Much discussed recently, what with Apple reporting iPad sales shrinkage over two consecutive quarters, is that it had apparently been widely assumed that tablet users would follow a two-year hardware... Read more
13-inch 2.5GHz MacBook Pro on sale for $999,...
Best Buy has the 13″ 2.5GHz MacBook Pro available for $999.99 on their online store. Choose free shipping or free instant local store pickup (if available). Their price is $100 off MSRP. Price is... Read more
Save up to $300 on an iMac with Apple refurbi...
The Apple Store has Apple Certified Refurbished iMacs available for up to $300 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free. These are the best prices on... Read more
WaterField Unveils 15″ Outback Solo & 13″...
Hard on the heels of Apple’s refreshed MacBook Pro Retina laptops announcement, WaterField Designs has unveiled a 15-inch version of the waxed-canvas and leather Outback Solo and a 13-inch version of... Read more
New Roxio Toast 12 Delivers Digital Media Pow...
Roxio Toast 12 is a hub for sharing digital media to virtually any platform or device. has introduced two new additions to its Roxio Toast product family – Roxio Toast 12 Titanium and Roxio Toast 12... Read more
The lowest prices on leftover Retina MacBook...
Best Buy has dropped prices on leftover 13″ and 15″ Retina MacBook Pros by up to $300 off original MSRP on their online store for a limited time. Choose free local store pickup (if available) or free... Read more
Apple Updates MacBook Pro with Retina Display...
Apple today updated its MacBook Pro with Retina display with faster processors and double the amount of memory in both entry-level configurations. MacBook Pro with Retina display features a Retina... Read more

Jobs Board

Sr Software Lead Engineer, *Apple* Online S...
Sr Software Lead Engineer, Apple Online Store Publishing Systems Keywords: Company: Apple Job Code: E3PCAK8MgYYkw Location (City or ZIP): Santa Clara Status: Full Read more
Sr Software Lead Engineer, *Apple* Online S...
Sr Software Lead Engineer, Apple Online Store Publishing Systems Keywords: Company: Apple Job Code: E3PCAK8MgYYkw Location (City or ZIP): Santa Clara Status: Full Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Sr. Product Leader, *Apple* Store Apps - Ap...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.