home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / ColorSync 2.5.1 SDK / Sample Code / CSDemo 2.5 / ShellSources / appMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-09  |  13.1 KB  |  462 lines  |  [TEXT/CWIE]

  1. // Simple framework for Macintosh sample code
  2. //
  3. // David Hayward and Nick Thompson 
  4. // Developer Technical Support
  5. // AppleLink: DEVSUPPORT
  6. //
  7. // Copyrite 1994, Apple Computer,Inc
  8. //
  9. // Application event dispatching code.
  10. // Most of the stuff in here will not need altering from app to app.
  11. // 
  12. // 9/13/94    nick    first cut
  13. // 12/13/94    david    several modifications
  14.  
  15.  
  16. #include <LowMem.h>
  17. #include <Windows.h>
  18. #include <ToolUtils.h>
  19. #include <Errors.h>
  20. #include <AppleEvents.h>
  21. #include <QDOffscreen.h>
  22. #include <DiskInit.h>
  23. #include <Dialogs.h>
  24.  
  25. #include "aeUtils.h"
  26. #include "dragUtils.h"
  27.  
  28. #include "appGlobals.h"
  29. #include "appMain.h"
  30. #include "appMenus.h"
  31. #include "appAEvts.h"
  32. #include "appErrors.h"
  33.  
  34. #include "win.h"
  35. #include "winTables.h"
  36.  
  37. #ifndef PIGS_SHELL_NOGX
  38. #include "gxGlobals.h"
  39. #include "gxUtils.h"
  40. #endif
  41.  
  42.  
  43. /**\
  44. |**| ==============================================================================
  45. |**| PRIVATE FUNCTION PROTOTYPES
  46. |**| ==============================================================================
  47. \**/
  48. void    InitToolbox            ( void ) ;
  49. void    InitMenuBar            ( void ) ;
  50. void    DoEventLoop            ( void ) ;
  51. void    DoMouseDownEvent    ( EventRecord *event ) ;
  52. void    DoKeyDownEvent        ( EventRecord *event ) ;
  53. void    DoUpdateEvent        ( EventRecord *event ) ;
  54. void    DoDiskEvent            ( EventRecord *event ) ;
  55. void    DoActivateEvent        ( EventRecord *event ) ;
  56. void    DoOSEvent            ( EventRecord *event ) ;
  57. void    DoNullEvent            ( EventRecord *event ) ;
  58.  
  59.  
  60. /**\
  61. |**| ==============================================================================
  62. |**| PUBLIC FUNCTIONS
  63. |**| ==============================================================================
  64. \**/
  65.  
  66.  
  67. /*------------------------------------------------------------------------------*\
  68.     DoEvent
  69.  *------------------------------------------------------------------------------*
  70.         This routine dispaches one event record.
  71.         This routine is called by DoEventLoop() and also by the
  72.         GX Printing Event Handler.
  73. \*------------------------------------------------------------------------------*/
  74. void DoEvent ( EventRecord *event )
  75. {
  76.     switch (event->what)
  77.     {
  78.         case nullEvent :                        /* handle null events */
  79.             DoNullEvent( event ) ;
  80.             break ;
  81.         
  82.         case mouseDown :                        /* handle mouse clicks */
  83.             DoMouseDownEvent( event ) ;
  84.             break ;
  85.         
  86.         case mouseUp :
  87.             break ;
  88.         
  89.         case keyDown :                            /* handle key hits */
  90.         case autoKey :
  91.             DoKeyDownEvent( event ) ;
  92.             break ;
  93.             
  94.         case updateEvt :                        /* handle update events */
  95.             DoUpdateEvent( event ) ;
  96.             break ;
  97.         
  98.         case diskEvt:                            /* handle disk events */
  99.             DoDiskEvent( event ) ;
  100.             break ;
  101.  
  102.         case activateEvt:                        /* handle activate events */
  103.             DoActivateEvent( event ) ;
  104.             break ;
  105.  
  106.         case osEvt:                                /* handle os events */
  107.             DoOSEvent( event ) ;
  108.             break ;
  109.  
  110.         case kHighLevelEvent:                    /* handle Apple Events */
  111.             AEProcessAppleEvent( event ) ;
  112.             break ;
  113.     }
  114. }
  115.  
  116.  
  117. /*------------------------------------------------------------------------------*\
  118.     main
  119.  *------------------------------------------------------------------------------*
  120.         the main routine which initialized the various managers and
  121.         starts the main event loop.
  122. \*------------------------------------------------------------------------------*/
  123. void main ( void )
  124. {
  125.     InitToolbox() ;
  126.     InitMenuBar() ;
  127.     
  128.     gAppResRefNum = CurResFile() ;
  129.  
  130.     // check for AppleEvents
  131.     if ( AE_initialize() != noErr )
  132.         FailIfErr( eFatalNeedsAEVTMgr ) ;
  133.     FailIfErr( RegisterAppleEvents() ) ;
  134.     
  135. #ifndef PIGS_SHELL_NOGX
  136.     // check for GX
  137.     (void) QDGX_initialize() ;
  138. #endif
  139.  
  140. #ifndef PIGS_SHELL_NODRAG
  141.     // check for Drag&Drop
  142.     (void) Drag_initialize() ;
  143. #endif
  144.  
  145.     // run
  146.     DoEventLoop() ;
  147.  
  148. #ifndef PIGS_SHELL_NOGX
  149.     // if necessary, clean up after GX
  150.     DisposeGXStuff() ;
  151. #endif
  152. }
  153.  
  154.  
  155. /**\
  156. |**| ==============================================================================
  157. |**| PRIVATE FUNCTIONS
  158. |**| ==============================================================================
  159. \**/
  160.  
  161.  
  162. /*------------------------------------------------------------------------------*\
  163.     InitToolbox
  164.  *------------------------------------------------------------------------------*
  165.         This routine initializes the standard managers.
  166.         This routine is called by main().
  167. \*------------------------------------------------------------------------------*/
  168. static void InitToolbox ( void )
  169. {
  170.     MaxApplZone() ;
  171.  
  172.     InitGraf( (Ptr)&qd.thePort ) ;
  173.     InitFonts() ;
  174.     InitWindows() ;
  175.     InitMenus() ;
  176.     FlushEvents( everyEvent, 0 ) ;
  177.     TEInit() ;
  178.     InitDialogs(0) ;
  179.     InitCursor() ;
  180.  
  181.     MoreMasters() ; MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  182.     MoreMasters() ; MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  183.     MoreMasters() ; MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  184. }
  185.  
  186.  
  187. /*------------------------------------------------------------------------------*\
  188.     InitMenuBar
  189.  *------------------------------------------------------------------------------*
  190.         This routine reads in the MBAR resource, adds the DA to the apple 
  191.         menu, and sets up all the menu states by calling DoAppAdjustMenus.
  192.         This routine is called by main().
  193. \*------------------------------------------------------------------------------*/
  194. static void InitMenuBar ( void )
  195. {
  196.     OSErr         retCode = noErr ;
  197.     Handle        menuBar = nil;
  198.     short        i ;
  199.     
  200.     menuBar = GetNewMBar( mMenuBar ) ;        // Read menus into menu bar, MBAR res id is 128
  201.     if ( menuBar == nil )                    // if we dont have it then quit 
  202.     {                                        // your app needs a dialog here, maybe.
  203.         SysError( dsSysErr ) ;                 // Thing is, if it couldn't get the menu bar,
  204.         ExitToShell() ;                     // it may not get the dialog resource.  
  205.     }                                         // I call Syserror here.  This way I
  206.                                              // know there has been a problem
  207.  
  208.     SetMenuBar( menuBar ) ;                    // Install menus
  209.     DisposeHandle( menuBar ) ;
  210.     
  211.     if (mFirstSubMenu && mLastSubMenu)
  212.         for (i=mFirstSubMenu; i<=mLastSubMenu; i++)
  213.             InsertMenu( GetMenu(i), hierMenu );
  214.  
  215.     AppendResMenu( GetMenuHandle(mApple),'DRVR');    // Add DA names to Apple menu, ID 128
  216.  
  217.     DoAppAdjustMenus() ;
  218. }
  219.  
  220.  
  221. /*------------------------------------------------------------------------------*\
  222.     DoEventLoop
  223.  *------------------------------------------------------------------------------*
  224.         This routine processes events until gQuitFlag gets set.
  225.         This routine is called by main().
  226. \*------------------------------------------------------------------------------*/
  227. static void DoEventLoop ( void )
  228. {
  229.     EventRecord event ;
  230.  
  231.     while ( !gQuitFlag ) 
  232.     {
  233.         if ( WaitNextEvent( everyEvent, &event, gAppSleepTicks, nil ) )
  234.             DoEvent( &event ) ;
  235.         else
  236.             DoNullEvent( &event ) ;
  237.     }
  238. }
  239.  
  240.  
  241. /*------------------------------------------------------------------------------*\
  242.     DoMouseDownEvent
  243.  *------------------------------------------------------------------------------*
  244.         This routine handles mouse down events.
  245.             inDrag clicks are handled here,
  246.             inContent clicks are sent to the window's ClickProcPtr,
  247.             inGoAway clicks are sent to the window's CloseProcPtr
  248.             inMenuBar clicks are handled by calling HandleMenuCommand(),
  249.             inGrow clicks are handled by resizing the window and calling its ResizeProcPtr,
  250.             inZoom clicks are handled by zooming the window and calling its ResizeProcPtr,
  251.         This routine is called by DoEvent().
  252. \*------------------------------------------------------------------------------*/
  253. static void DoMouseDownEvent ( EventRecord *event )
  254. {
  255.     WindowRef        window ;
  256.     winHandle        win ;
  257.     short            clickArea ;
  258.     Rect            screenRect ;
  259.     Rect            sizeRect ;
  260.     long            sizeHV ;
  261.     RgnHandle        grayRgn ;
  262.  
  263.     clickArea = FindWindow( event->where, &window ) ;
  264.     win = GetWindowWinHandle( window ) ;
  265.     
  266.     switch ( clickArea )
  267.     {
  268.         case inDrag:
  269.             grayRgn = LMGetGrayRgn() ;
  270.             screenRect = (**grayRgn).rgnBBox ;
  271.             SelectWindow( window ) ;
  272.             DragWindow( window, event->where, &screenRect ) ;
  273.             break ;
  274.  
  275.         case inContent:
  276.             if ( window == FrontWindow() )
  277.                 CallWinClickProc( win, event ) ;
  278.             else
  279.                 SelectWindow( window ) ;
  280.             break ;
  281.         
  282.         case inGoAway:
  283.             if ( TrackGoAway( window, event->where ) )
  284.             {
  285.                 CallWinCloseProc( win ) ;
  286.                 DoAppAdjustMenus() ;
  287.             }
  288.             break;
  289.  
  290.         case inMenuBar: 
  291.             HandleMenuCommand( MenuSelect(event->where) ) ;
  292.             break;
  293.  
  294.         case inGrow:
  295.             if ( win != nil )
  296.             {
  297.                 sizeRect = GetWinSizeRect( win ) ;
  298.                  sizeHV = GrowWindow( window, event->where, &sizeRect ) ;
  299.                 if ( sizeHV != 0L )            // if the window need to be resized
  300.                 {
  301.                     SizeWindow( window, LoWord(sizeHV), HiWord(sizeHV), true ) ;
  302.                     CallWinResizeProc( win ) ;
  303.                 }
  304.             }
  305.             break;
  306.  
  307.         case inZoomIn: 
  308.         case inZoomOut: 
  309.             if ( TrackBox( window,event->where,clickArea) )
  310.             {
  311.                 SetPort( (GrafPtr)window ) ;
  312.                 EraseRect( &(((CGrafPtr)window)->portRect) ) ;
  313.                 ZoomWindow( window, clickArea, true) ;
  314.                 CallWinResizeProc( win ) ;
  315.             }
  316.             break;
  317.  
  318.         default:
  319.             break ;
  320.     }
  321. }
  322.  
  323.  
  324. /*------------------------------------------------------------------------------*\
  325.     DoKeyDownEvent
  326.  *------------------------------------------------------------------------------*
  327.         This routine handles key down events.
  328.         If the command key is down then the event is handled by calling HandleMenuCommand(),
  329.         Otherwize, the event is sent to the frontmost window by calling its KeyProcPtr,
  330.         This routine is called by DoEvent().
  331. \*------------------------------------------------------------------------------*/
  332. static void DoKeyDownEvent ( EventRecord *event )
  333. {
  334.     char            key ;
  335.     winHandle        win ;
  336.  
  337.     key = event->message & charCodeMask ;
  338.     
  339.     if ( event->modifiers & cmdKey )             /* Command key down? */
  340.         HandleMenuCommand( MenuKey(key) ) ;
  341.     else
  342.     {
  343.         // see if the frontmost window can handle the event
  344.         win = GetWindowWinHandle( FrontWindow() ) ;
  345.         CallWinKeyProc( win, event ) ;
  346.     }
  347. }
  348.  
  349.  
  350. /*------------------------------------------------------------------------------*\
  351.     DoUpdateEvent
  352.  *------------------------------------------------------------------------------*
  353.         This routine handles update events.
  354.         The event is sent to the appropriate window by calling its UpdateProcPtr,
  355.         This routine is called by DoEvent().
  356. \*------------------------------------------------------------------------------*/
  357. static void DoUpdateEvent ( EventRecord *event )
  358. {
  359.     WindowRef       window ;
  360.     winHandle        win  ;
  361.  
  362.     window = (WindowRef) event->message ;
  363.     win = GetWindowWinHandle( window ) ;
  364.     CallWinUpdateProc( win, event ) ;
  365. }
  366.  
  367.  
  368. /*------------------------------------------------------------------------------*\
  369.     DoDiskEvent
  370.  *------------------------------------------------------------------------------*
  371.         This routine handles disk events.
  372.         It is called by DoEvent().
  373. \*------------------------------------------------------------------------------*/
  374. static void DoDiskEvent ( EventRecord *event )
  375. {
  376.     Point aPoint = {100, 100} ;
  377.     if ( HiWrd(event->message) != noErr ) 
  378.         (void) DIBadMount(aPoint, event->message) ;
  379. }
  380.  
  381.  
  382. /*------------------------------------------------------------------------------*\
  383.     DoActivateEvent
  384.  *------------------------------------------------------------------------------*
  385.         This routine handles activate events.  Whenever a window becomes active,
  386.         it sets up all the menu states by calling DoAppAdjustMenus.  The update
  387.         event is then forwarded to the appropriate window by calling its
  388.         ActivateProcPtr.
  389.         This routine is called by DoEvent().
  390. \*------------------------------------------------------------------------------*/
  391. static void DoActivateEvent ( EventRecord *event )
  392. {
  393.     WindowRef       window ;
  394.     winHandle        win ;
  395.     Boolean            becomingActive ;
  396.  
  397.     becomingActive = (event->modifiers) & activeFlag ;
  398.     window = (WindowRef)event->message ;
  399.     win = GetWindowWinHandle( window ) ;
  400.  
  401.     if (becomingActive)
  402.     {
  403.         DoAppAdjustMenus() ;                    // undim menu items
  404.         SetGWorld( (CGrafPtr)window, nil ) ;    // set the window to be the current port
  405.     }
  406.     CallWinActivateProc( win, becomingActive ) ;
  407. }
  408.  
  409.  
  410. /*------------------------------------------------------------------------------*\
  411.     DoOSEvent
  412.  *------------------------------------------------------------------------------*
  413.         This routine handles os events such as Suspend/Resume and MouseMoved
  414.         On Suspend/Resume the gInBackground global and the GXJob are updated. 
  415.         Also, the frontmost window is notified by calling its ActivateProcPtr
  416.         This routine is called by DoEvent().
  417. \*------------------------------------------------------------------------------*/
  418. static void DoOSEvent ( EventRecord *event )
  419. {
  420.     WindowRef       window ;
  421.     winHandle        win ;
  422.     Boolean            becomingActive ;
  423.  
  424.     switch ((event->message >> 24) & 0x00FF)         // High byte of message
  425.     {
  426.         case suspendResumeMessage:
  427.  
  428.             becomingActive = (event->message & resumeFlag ) ;
  429.             gInBackground = !becomingActive ;
  430.  
  431.             (void) CallAllWinResumeProcs( becomingActive ) ;
  432.  
  433.             window = FrontWindow() ;
  434.             win = GetWindowWinHandle( window ) ;
  435.             CallWinActivateProc( win, becomingActive ) ;
  436.             if (becomingActive)
  437.             {
  438.                 DoAppAdjustMenus() ;                    // undim menu items
  439.                 if ( window )
  440.                     SetGWorld( (CGrafPtr)window, nil ) ;// set the window to be the current port
  441.             }
  442.  
  443.             break ;
  444.     }
  445. }
  446.  
  447.  
  448. /*------------------------------------------------------------------------------*\
  449.     DoNullEvent
  450.  *------------------------------------------------------------------------------*
  451.         This routine handles null events.
  452.         The event is sent to all windows by calling each NullProcPtr.
  453.         This routine is called by DoEvent().
  454. \*------------------------------------------------------------------------------*/
  455. static void DoNullEvent ( EventRecord *event )
  456. {
  457.     (void) CallAllWinNullProcs( event );
  458. }
  459.  
  460.  
  461.  
  462.