home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / CustomDialog Demo / Source / EventHandler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-31  |  7.4 KB  |  305 lines  |  [TEXT/MMCC]

  1. /**********************************************************************
  2.  *
  3.  *                        CustomGetFile Dialog Demo
  4.  *
  5.  *    EventHandler.c
  6.  *
  7.  *    Written in CodeWarrior Gold 5.5
  8.  *    August 31, 1995
  9.  *
  10.  *    Copyright © 1995 Carl B. Constantine
  11.  *    Some portions Copyright © 1995 MetroWerks, Inc.
  12.  *    Some portions Copyright © 1995 Apple Computer, Inc.
  13.  *
  14.  **********************************************************************/
  15.  
  16.  /*------------------------------------------------------------------
  17.   #
  18.   #                            File History
  19.   #
  20.   #        Date                Description of Change
  21.   #        ----                ---------------------
  22.   #        Aug 31/93            — Original creation of file
  23.   #
  24.   #
  25.   #
  26.   -------------------------------------------------------------------*/
  27.  
  28.  
  29. /*============================ Include Files ============================*/
  30.  
  31.  
  32. #include "AppConstants.h"
  33. #include "AppGlobals.h"
  34. #include "EventHandler.h"
  35. #include "ErrUtils.h"
  36.  
  37.  
  38. /*======================== Procedures & Functions =======================*/
  39.  
  40. void EventLoop( void )
  41. {
  42.     EventRecord        event;
  43.     Boolean            gotEvent;
  44.     long            sleepTime;
  45.     
  46.     gCursorRgn = NewRgn();    // Pass WNE an empty region 1st time through loop
  47.     
  48.     while ( !gQuitting )
  49.     {
  50.         sleepTime = GetDblTime();    // if from window has TE record
  51.         if ( gInBackground ) sleepTime = -1L;    // set approp. background value
  52.         
  53.         gotEvent = WaitNextEvent( everyEvent, &event, sleepTime, gCursorRgn );
  54.         
  55.         if ( gotEvent )
  56.         {
  57.             DoEvent( &event );
  58.         }
  59.         else
  60.         {
  61.             // Don't do anything.  You should put some sort of Idle function here.
  62.         }
  63.     }    // end while loop
  64.     
  65. }
  66.  
  67.  
  68. /*----------------------------------------------------------------------------*/
  69.  
  70. void DoEvent( EventRecord *event )
  71. {
  72.     short            myError;
  73.     short            windowPart;
  74.     WindowPtr        window=NIL;
  75.     char            key;
  76.     Point            mountPoint;
  77.     
  78.     switch ( event->what ) 
  79.     {
  80.         case mouseDown:
  81.             windowPart = FindWindow( event->where, &window );
  82.             switch ( windowPart ) 
  83.             {
  84.                 case inSysWindow:        /* let the system handle the mouseDown */
  85.                     SystemClick( event, window );
  86.                     break;
  87.                 case inMenuBar:
  88.                     AdjustMenus();        /* prepare menu items first */
  89.                     DoMenuCommand( MenuSelect( event->where ) );
  90.                     break;
  91.                 case inContent:
  92.                     /* No contentRgn to click in */
  93.                     break;
  94.                 case inDrag:
  95.                     /* No DragRgn to drag */
  96.                     break;
  97.             }
  98.             break;    /* MouseDown */
  99.         case keyDown:
  100.         case autoKey:        /* Check for menukey equivalents */
  101.             DoKeyPress( window, event );
  102.             break;
  103.         case activateEvt:
  104.             break;
  105.         case updateEvt:
  106.             break;
  107.         case diskEvt:        /* Allow user to format disks if need be */
  108.             if ( HiWrd(event->message) != noErr ) 
  109.             {
  110.                 mountPoint.h = kDILeft;
  111.                 mountPoint.v = kDITop;
  112.                 myError = DIBadMount( mountPoint, event->message );
  113.             }
  114.             break;
  115.         case osEvt:
  116.             DoOSEvent( event );
  117.             break;
  118.     }
  119.     
  120. }    /* doEvent */
  121.  
  122. /*----------------------------------------------------------------------------*/
  123.  
  124. /*    Handle Suspend & Resume Events */
  125.  
  126. void DoOSEvent( EventRecord *event )
  127. {
  128.     switch ( (event->message >> 24) &0x0FF ) 
  129.     {
  130.         case suspendResumeMessage:
  131.             if ( ( event->message & resumeFlag ) == 0)     /* suspend */
  132.             {
  133.                 gInBackground = true;
  134.                 ZeroScrap();
  135.                 TEToScrap();
  136.             //    Don't need to deactiveate anything as we don't have any windows
  137.             } 
  138.             else    /* resuming */
  139.             {
  140.                 gInBackground = false;
  141.                 if ( event->message & convertClipboardFlag)
  142.                     TEFromScrap();
  143.             //    Don't need to activeate anything as we don't have any windows
  144.             }
  145.             break;
  146.         case mouseMovedMessage:
  147.             DisposeRgn( gCursorRgn );    /* get rid of old region */
  148.             gCursorRgn = NewRgn();
  149.             SetRectRgn( gCursorRgn, -32768, -32768, 32766, 32766 );
  150.             break;
  151.     }                /* suspend/Resume */
  152. }
  153.  
  154. /*----------------------------------------------------------------------------*/
  155.  
  156. void DoMenuCommand( long menuResult )
  157. {
  158.     short                menuID;            /* ID of selected menu */
  159.     short                menuItem;        /* item number in selected menu */
  160.     short                itemHit;
  161.     Str255                daName;
  162.     short                daRefNum;
  163.     
  164.     menuID = menuResult >> 16;
  165.     menuItem = menuResult & 0x0000ffff;
  166.     
  167.     switch ( menuID ) 
  168.     {
  169.         case mApple:
  170.             switch ( menuItem ) 
  171.             {
  172.                 case iAbout:    /* display the About box */
  173.                     itemHit = Alert( rAboutBox, 0L );
  174.                     break;
  175.                 default:
  176.                     GetItem( GetMHandle( mApple ), menuItem, daName );
  177.                     daRefNum = OpenDeskAcc( daName );
  178.                     break;
  179.             }
  180.             break;
  181.         case mFile:
  182.             switch ( menuItem ) 
  183.             {
  184.                 case iQuit:
  185.                     gQuitting = true;
  186.                     break;                
  187.             }
  188.             break;
  189.         case mEdit:
  190.             break;
  191.     }
  192.     HiliteMenu( 0 );
  193.     
  194. }    /* DoMenuCommand */
  195.  
  196.  
  197. /*----------------------------------------------------------------------------*/
  198.  
  199. void AdjustMenus( void )
  200. {
  201.     WindowPtr        wp = FrontWindow();
  202.     MenuHandle        fileMenu, editMenu;
  203.         
  204.     if ( !wp )
  205.     {
  206.         fileMenu = GetMHandle( mFile );
  207.         editMenu = GetMHandle( mEdit );
  208.         
  209.         DisableItem( editMenu, iUndo );
  210.         DisableItem( editMenu, iCut );
  211.         DisableItem( editMenu, iCopy );
  212.         DisableItem( editMenu, iPaste );
  213.         DisableItem( editMenu, iClear );
  214.         
  215.         EnableItem( fileMenu, iQuit );
  216.     }
  217.     
  218. }
  219.  
  220. /*----------------------------------------------------------------------------*/
  221.  
  222. /* Handle keyDown events in windows & Dialogs */
  223.  
  224. void DoKeyPress( WindowPtr wp, EventRecord *event )
  225. {
  226.     char    key;
  227.     
  228.     key = event->message & charCodeMask;
  229.     if ( event->modifiers & cmdKey) 
  230.     {    /* is command key down? */
  231.         if ( event->what == keyDown ) 
  232.         {
  233.             AdjustMenus();    /* prepare menu items first */
  234.             DoMenuCommand( MenuKey( key ) );
  235.         }
  236.     }
  237.  
  238. }
  239.  
  240. /*----------------------------------------------------------------------------*/
  241. /*    All this code comes directly from Tom Thompson's book "Power Macintosh
  242.     Programmers Starter Kit"
  243. */
  244.  
  245. /* Build high-level event dispatch table and add our handlers to it. Must use static */
  246. /*    declaration so that the dispatch table has file scope. */
  247. Boolean Init_AE_Events( void )
  248. {
  249. OSErr    err;
  250. short    i;
  251. static AEinstalls HandlersToInstall[] =      /* The 4 required Apple Events */
  252.     {
  253.         {kCoreEventClass, kAEOpenApplication, Core_AE_Open_Handler},
  254.         {kCoreEventClass, kAEOpenDocuments, Core_AE_OpenDoc_Handler},
  255.         {kCoreEventClass, kAEQuitApplication, Core_AE_Quit_Handler},
  256.         {kCoreEventClass, kAEPrintDocuments, Core_AE_Print_Handler}, 
  257.     };
  258.  
  259.     for ( i = 0; i < LAST_HANDLER; i++ )
  260.         {        /* Install each handler in application dispatch table, with a routine descriptor */
  261.         err = AEInstallEventHandler( HandlersToInstall[i].theClass, HandlersToInstall[i].theEvent,
  262.                                     NewAEEventHandlerProc( HandlersToInstall[i].theProc ), 0, FALSE );
  263.         if (err)        /* If there was a problem, bail out */
  264.             {
  265.             GenAlert( errAEHandler );
  266.             return FALSE;
  267.             } /* end if */
  268.         } /* end for */
  269.  
  270.     return TRUE;
  271. } /* end Init_AE_Events() */
  272.  
  273. /* High-level open application event.  */
  274. pascal OSErr Core_AE_Open_Handler( AppleEvent *messagein, AppleEvent *reply, long refIn )
  275. {
  276.     return( noErr );
  277. } /* end Core_AE_Open_Handler() */
  278.  
  279. /* High-level open document event */
  280. pascal OSErr Core_AE_OpenDoc_Handler( AppleEvent *messagein, AppleEvent *reply, long refIn )
  281. {
  282.  
  283.     return( noErr );
  284.     
  285. } /* end Core_AE_OpenDoc_Handler() */
  286.  
  287. /* High-level print event */
  288. pascal OSErr Core_AE_Print_Handler( AppleEvent *messagein, AppleEvent *reply, long refIn )
  289. {
  290.     return errAEEventNotHandled;    /* No printing done here, so no print handler */
  291. } /* end Core_AE_Print_Handler() */
  292.  
  293. /* High-level quit event  */
  294. pascal OSErr Core_AE_Quit_Handler( AppleEvent *messagein, AppleEvent *reply, long refIn )
  295. {
  296.     gQuitting = TRUE;                /* Tell main event loop we want to stop */
  297.     return noErr;
  298. } /* Core_AE_Quit_Handler() */
  299.  
  300. void Do_High_Level( EventRecord *AERecord )
  301. {
  302.     AEProcessAppleEvent( AERecord );
  303. } /* end Do_High_Level() */
  304.  
  305. /*============================= End of File ==============================*/