home *** CD-ROM | disk | FTP | other *** search
- /********************************************************/
- /* */
- /* MDEF Tester Code from Chapter Three of */
- /* */
- /* The Macintosh Programming Primer, Volume II, */
- /* Second Edition */
- /* */
- /* Copyright 1993, Dave Mark */
- /* */
- /* This program demonstrates specific Mac programming */
- /* techniques. */
- /* */
- /********************************************************/
-
- #define kWindowResID 128
- #define kMBARResID 128
- #define kNULLStorage 0L
- #define kMoveToFront (WindowPtr)-1L
- #define kSleep 60L
-
- #define mApple 128
- #define iAbout 1
-
- #define mFile 129
- #define iQuit 1
-
- #define mPICT 131
-
-
- /*************/
- /* Globals */
- /*************/
-
- Boolean gDone;
- short gCurPICTid;
-
-
- /***************/
- /* Functions */
- /***************/
-
- void ToolboxInit( void );
- void MenuBarInit( void );
- void WindowInit( 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 HandlePICTChoice( short item );
- void DoUpdate( WindowPtr window );
- void DrawPictInWindow( PicHandle pic,
- WindowPtr window );
- short GetBasePICTid( short menuID );
-
-
- /*********************************** main */
-
- void main( void )
- {
- ToolboxInit();
- MenuBarInit();
- WindowInit();
-
- gCurPICTid = GetBasePICTid( mPICT );
-
- EventLoop();
- }
-
-
- /*********************************** ToolboxInit */
-
- void ToolboxInit( void )
- {
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- InitCursor();
- }
-
-
- /*********************************** MenuBarInit */
-
- void MenuBarInit( void )
- {
- Handle menuBar;
- MenuHandle menu;
-
- menuBar = GetNewMBar( kMBARResID );
- SetMenuBar( menuBar );
-
- menu = GetMHandle( mApple );
- AddResMenu( menu, 'DRVR' );
-
- DrawMenuBar();
- }
-
-
- /*********************************** WindowInit */
-
- void WindowInit( void )
- {
- WindowPtr window;
-
- window = GetNewWindow( kWindowResID, kNULLStorage, kMoveToFront );
-
- if ( window == NULL )
- {
- SysBeep( 20 ); /* Couldn't load WIND */
- ExitToShell();
- }
-
- SetPort( window );
- ShowWindow( window );
- }
-
-
- /*********************************** EventLoop */
-
- void EventLoop( void )
- {
- EventRecord event;
-
- gDone = false;
- while ( gDone == false )
- {
- if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
- DoEvent( &event );
- }
- }
-
-
- /************************************* DoEvent */
-
- void DoEvent( EventRecord *eventPtr )
- {
- char theChar;
-
- 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( (WindowPtr)eventPtr->message );
- break;
- }
- }
-
-
- /************************************* HandleMouseDown */
-
- void HandleMouseDown( EventRecord *eventPtr )
- {
- WindowPtr window;
- short thePart;
- long menuChoice;
-
- thePart = FindWindow( eventPtr->where, &window );
-
- switch ( thePart )
- {
- case inMenuBar:
- menuChoice = MenuSelect( eventPtr->where );
- HandleMenuChoice( menuChoice );
- break;
- case inSysWindow:
- SystemClick( eventPtr, window );
- break;
- case inDrag :
- DragWindow( window, eventPtr->where, &(screenBits.bounds) );
- 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;
- case mPICT:
- HandlePICTChoice( item );
- break;
- }
- HiliteMenu( 0 );
- }
- }
-
-
- /************************************* HandleAppleChoice */
-
- void HandleAppleChoice( short item )
- {
- MenuHandle appleMenu;
- Str255 accName;
- short accNumber;
-
- switch ( item )
- {
- case iAbout:
- SysBeep( 20 );
- break;
- default:
- appleMenu = GetMHandle( mApple );
- GetItem( appleMenu, item, accName );
- accNumber = OpenDeskAcc( accName );
- break;
- }
- }
-
-
- /************************************* HandleFileChoice */
-
- void HandleFileChoice( short item )
- {
- switch ( item )
- {
- case iQuit :
- gDone = true;
- break;
- }
- }
-
-
- /************************************* HandlePICTChoice */
-
- void HandlePICTChoice( short item )
- {
- WindowPtr window;
-
- window = FrontWindow();
-
- EraseRect( &window->portRect );
- InvalRect( &window->portRect );
-
- gCurPICTid = GetBasePICTid( mPICT ) + item - 1;
- }
-
-
- /************************************* DoUpdate */
-
- void DoUpdate( WindowPtr window )
- {
- PicHandle pic;
-
- BeginUpdate( window );
-
- pic = GetPicture( gCurPICTid );
-
- if ( pic == NULL )
- {
- SysBeep( 20 ); /* Couldn't load PICT */
- ExitToShell();
- }
-
- DrawPictInWindow( pic, FrontWindow() );
-
- EndUpdate( window );
- }
-
-
- /*********************************** DrawPictInWindow */
-
- void DrawPictInWindow( PicHandle pic,
- WindowPtr window )
- {
- Rect pictRect, windRect;
-
- pictRect = (**pic).picFrame;
-
- windRect = window->portRect;
-
- OffsetRect( &pictRect, windRect.left - pictRect.left,
- windRect.top - pictRect.top);
- OffsetRect( &pictRect,(windRect.right - pictRect.right)/2,
- (windRect.bottom - pictRect.bottom)/2);
-
- DrawPicture( pic, &pictRect );
- }
-
-
- /*********************************** GetBasePICTid */
-
- short GetBasePICTid( short menuID )
- {
- Handle longHandle;
- long retrievedLong;
-
- longHandle = GetResource( 'long', menuID );
-
- retrievedLong = (*((long *)(*longHandle)));
-
- return( HiWord( retrievedLong ) );
- }