home *** CD-ROM | disk | FTP | other *** search
- // Simple framework for Macintosh sample code
- //
- // David Hayward and Nick Thompson
- // Developer Technical Support
- // AppleLink: DEVSUPPORT
- //
- // Copyrite 1994, Apple Computer,Inc
- //
- // Application event dispatching code.
- // Most of the stuff in here will not need altering from app to app.
- //
- // 9/13/94 nick first cut
- // 12/13/94 david several modifications
-
-
- #include <LowMem.h>
- #include <Windows.h>
- #include <ToolUtils.h>
- #include <Errors.h>
- #include <AppleEvents.h>
- #include <QDOffscreen.h>
- #include <DiskInit.h>
- #include <Dialogs.h>
-
- #include "aeUtils.h"
- #include "dragUtils.h"
-
- #include "appGlobals.h"
- #include "appMain.h"
- #include "appMenus.h"
- #include "appAEvts.h"
- #include "appErrors.h"
-
- #include "win.h"
- #include "winTables.h"
-
- #ifndef PIGS_SHELL_NOGX
- #include "gxGlobals.h"
- #include "gxUtils.h"
- #endif
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTION PROTOTYPES
- |**| ==============================================================================
- \**/
- void InitToolbox ( void ) ;
- void InitMenuBar ( void ) ;
- void DoEventLoop ( void ) ;
- void DoMouseDownEvent ( EventRecord *event ) ;
- void DoKeyDownEvent ( EventRecord *event ) ;
- void DoUpdateEvent ( EventRecord *event ) ;
- void DoDiskEvent ( EventRecord *event ) ;
- void DoActivateEvent ( EventRecord *event ) ;
- void DoOSEvent ( EventRecord *event ) ;
- void DoNullEvent ( EventRecord *event ) ;
-
-
- /**\
- |**| ==============================================================================
- |**| PUBLIC FUNCTIONS
- |**| ==============================================================================
- \**/
-
-
- /*------------------------------------------------------------------------------*\
- DoEvent
- *------------------------------------------------------------------------------*
- This routine dispaches one event record.
- This routine is called by DoEventLoop() and also by the
- GX Printing Event Handler.
- \*------------------------------------------------------------------------------*/
- void DoEvent ( EventRecord *event )
- {
- switch (event->what)
- {
- case nullEvent : /* handle null events */
- DoNullEvent( event ) ;
- break ;
-
- case mouseDown : /* handle mouse clicks */
- DoMouseDownEvent( event ) ;
- break ;
-
- case mouseUp :
- break ;
-
- case keyDown : /* handle key hits */
- case autoKey :
- DoKeyDownEvent( event ) ;
- break ;
-
- case updateEvt : /* handle update events */
- DoUpdateEvent( event ) ;
- break ;
-
- case diskEvt: /* handle disk events */
- DoDiskEvent( event ) ;
- break ;
-
- case activateEvt: /* handle activate events */
- DoActivateEvent( event ) ;
- break ;
-
- case osEvt: /* handle os events */
- DoOSEvent( event ) ;
- break ;
-
- case kHighLevelEvent: /* handle Apple Events */
- AEProcessAppleEvent( event ) ;
- break ;
- }
- }
-
-
- /*------------------------------------------------------------------------------*\
- main
- *------------------------------------------------------------------------------*
- the main routine which initialized the various managers and
- starts the main event loop.
- \*------------------------------------------------------------------------------*/
- void main ( void )
- {
- InitToolbox() ;
- InitMenuBar() ;
-
- gAppResRefNum = CurResFile() ;
-
- // check for AppleEvents
- if ( AE_initialize() != noErr )
- FailIfErr( eFatalNeedsAEVTMgr ) ;
- FailIfErr( RegisterAppleEvents() ) ;
-
- #ifndef PIGS_SHELL_NOGX
- // check for GX
- (void) QDGX_initialize() ;
- #endif
-
- #ifndef PIGS_SHELL_NODRAG
- // check for Drag&Drop
- (void) Drag_initialize() ;
- #endif
-
- // run
- DoEventLoop() ;
-
- #ifndef PIGS_SHELL_NOGX
- // if necessary, clean up after GX
- DisposeGXStuff() ;
- #endif
- }
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTIONS
- |**| ==============================================================================
- \**/
-
-
- /*------------------------------------------------------------------------------*\
- InitToolbox
- *------------------------------------------------------------------------------*
- This routine initializes the standard managers.
- This routine is called by main().
- \*------------------------------------------------------------------------------*/
- static void InitToolbox ( void )
- {
- MaxApplZone() ;
-
- InitGraf( (Ptr)&qd.thePort ) ;
- InitFonts() ;
- InitWindows() ;
- InitMenus() ;
- FlushEvents( everyEvent, 0 ) ;
- TEInit() ;
- InitDialogs(0) ;
- InitCursor() ;
-
- MoreMasters() ; MoreMasters() ; MoreMasters() ; MoreMasters() ;
- MoreMasters() ; MoreMasters() ; MoreMasters() ; MoreMasters() ;
- MoreMasters() ; MoreMasters() ; MoreMasters() ; MoreMasters() ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- InitMenuBar
- *------------------------------------------------------------------------------*
- This routine reads in the MBAR resource, adds the DA to the apple
- menu, and sets up all the menu states by calling DoAppAdjustMenus.
- This routine is called by main().
- \*------------------------------------------------------------------------------*/
- static void InitMenuBar ( void )
- {
- OSErr retCode = noErr ;
- Handle menuBar = nil;
- short i ;
-
- menuBar = GetNewMBar( mMenuBar ) ; // Read menus into menu bar, MBAR res id is 128
- if ( menuBar == nil ) // if we dont have it then quit
- { // your app needs a dialog here, maybe.
- SysError( dsSysErr ) ; // Thing is, if it couldn't get the menu bar,
- ExitToShell() ; // it may not get the dialog resource.
- } // I call Syserror here. This way I
- // know there has been a problem
-
- SetMenuBar( menuBar ) ; // Install menus
- DisposeHandle( menuBar ) ;
-
- if (mFirstSubMenu && mLastSubMenu)
- for (i=mFirstSubMenu; i<=mLastSubMenu; i++)
- InsertMenu( GetMenu(i), hierMenu );
-
- AppendResMenu( GetMenuHandle(mApple),'DRVR'); // Add DA names to Apple menu, ID 128
-
- DoAppAdjustMenus() ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoEventLoop
- *------------------------------------------------------------------------------*
- This routine processes events until gQuitFlag gets set.
- This routine is called by main().
- \*------------------------------------------------------------------------------*/
- static void DoEventLoop ( void )
- {
- EventRecord event ;
-
- while ( !gQuitFlag )
- {
- if ( WaitNextEvent( everyEvent, &event, gAppSleepTicks, nil ) )
- DoEvent( &event ) ;
- else
- DoNullEvent( &event ) ;
- }
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoMouseDownEvent
- *------------------------------------------------------------------------------*
- This routine handles mouse down events.
- inDrag clicks are handled here,
- inContent clicks are sent to the window's ClickProcPtr,
- inGoAway clicks are sent to the window's CloseProcPtr
- inMenuBar clicks are handled by calling HandleMenuCommand(),
- inGrow clicks are handled by resizing the window and calling its ResizeProcPtr,
- inZoom clicks are handled by zooming the window and calling its ResizeProcPtr,
- This routine is called by DoEvent().
- \*------------------------------------------------------------------------------*/
- static void DoMouseDownEvent ( EventRecord *event )
- {
- WindowRef window ;
- winHandle win ;
- short clickArea ;
- Rect screenRect ;
- Rect sizeRect ;
- long sizeHV ;
- RgnHandle grayRgn ;
-
- clickArea = FindWindow( event->where, &window ) ;
- win = GetWindowWinHandle( window ) ;
-
- switch ( clickArea )
- {
- case inDrag:
- grayRgn = LMGetGrayRgn() ;
- screenRect = (**grayRgn).rgnBBox ;
- SelectWindow( window ) ;
- DragWindow( window, event->where, &screenRect ) ;
- break ;
-
- case inContent:
- if ( window == FrontWindow() )
- CallWinClickProc( win, event ) ;
- else
- SelectWindow( window ) ;
- break ;
-
- case inGoAway:
- if ( TrackGoAway( window, event->where ) )
- {
- CallWinCloseProc( win ) ;
- DoAppAdjustMenus() ;
- }
- break;
-
- case inMenuBar:
- HandleMenuCommand( MenuSelect(event->where) ) ;
- break;
-
- case inGrow:
- if ( win != nil )
- {
- sizeRect = GetWinSizeRect( win ) ;
- sizeHV = GrowWindow( window, event->where, &sizeRect ) ;
- if ( sizeHV != 0L ) // if the window need to be resized
- {
- SizeWindow( window, LoWord(sizeHV), HiWord(sizeHV), true ) ;
- CallWinResizeProc( win ) ;
- }
- }
- break;
-
- case inZoomIn:
- case inZoomOut:
- if ( TrackBox( window,event->where,clickArea) )
- {
- SetPort( (GrafPtr)window ) ;
- EraseRect( &(((CGrafPtr)window)->portRect) ) ;
- ZoomWindow( window, clickArea, true) ;
- CallWinResizeProc( win ) ;
- }
- break;
-
- default:
- break ;
- }
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoKeyDownEvent
- *------------------------------------------------------------------------------*
- This routine handles key down events.
- If the command key is down then the event is handled by calling HandleMenuCommand(),
- Otherwize, the event is sent to the frontmost window by calling its KeyProcPtr,
- This routine is called by DoEvent().
- \*------------------------------------------------------------------------------*/
- static void DoKeyDownEvent ( EventRecord *event )
- {
- char key ;
- winHandle win ;
-
- key = event->message & charCodeMask ;
-
- if ( event->modifiers & cmdKey ) /* Command key down? */
- HandleMenuCommand( MenuKey(key) ) ;
- else
- {
- // see if the frontmost window can handle the event
- win = GetWindowWinHandle( FrontWindow() ) ;
- CallWinKeyProc( win, event ) ;
- }
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoUpdateEvent
- *------------------------------------------------------------------------------*
- This routine handles update events.
- The event is sent to the appropriate window by calling its UpdateProcPtr,
- This routine is called by DoEvent().
- \*------------------------------------------------------------------------------*/
- static void DoUpdateEvent ( EventRecord *event )
- {
- WindowRef window ;
- winHandle win ;
-
- window = (WindowRef) event->message ;
- win = GetWindowWinHandle( window ) ;
- CallWinUpdateProc( win, event ) ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoDiskEvent
- *------------------------------------------------------------------------------*
- This routine handles disk events.
- It is called by DoEvent().
- \*------------------------------------------------------------------------------*/
- static void DoDiskEvent ( EventRecord *event )
- {
- Point aPoint = {100, 100} ;
- if ( HiWrd(event->message) != noErr )
- (void) DIBadMount(aPoint, event->message) ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoActivateEvent
- *------------------------------------------------------------------------------*
- This routine handles activate events. Whenever a window becomes active,
- it sets up all the menu states by calling DoAppAdjustMenus. The update
- event is then forwarded to the appropriate window by calling its
- ActivateProcPtr.
- This routine is called by DoEvent().
- \*------------------------------------------------------------------------------*/
- static void DoActivateEvent ( EventRecord *event )
- {
- WindowRef window ;
- winHandle win ;
- Boolean becomingActive ;
-
- becomingActive = (event->modifiers) & activeFlag ;
- window = (WindowRef)event->message ;
- win = GetWindowWinHandle( window ) ;
-
- if (becomingActive)
- {
- DoAppAdjustMenus() ; // undim menu items
- SetGWorld( (CGrafPtr)window, nil ) ; // set the window to be the current port
- }
- CallWinActivateProc( win, becomingActive ) ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoOSEvent
- *------------------------------------------------------------------------------*
- This routine handles os events such as Suspend/Resume and MouseMoved
- On Suspend/Resume the gInBackground global and the GXJob are updated.
- Also, the frontmost window is notified by calling its ActivateProcPtr
- This routine is called by DoEvent().
- \*------------------------------------------------------------------------------*/
- static void DoOSEvent ( EventRecord *event )
- {
- WindowRef window ;
- winHandle win ;
- Boolean becomingActive ;
-
- switch ((event->message >> 24) & 0x00FF) // High byte of message
- {
- case suspendResumeMessage:
-
- becomingActive = (event->message & resumeFlag ) ;
- gInBackground = !becomingActive ;
-
- (void) CallAllWinResumeProcs( becomingActive ) ;
-
- window = FrontWindow() ;
- win = GetWindowWinHandle( window ) ;
- CallWinActivateProc( win, becomingActive ) ;
- if (becomingActive)
- {
- DoAppAdjustMenus() ; // undim menu items
- if ( window )
- SetGWorld( (CGrafPtr)window, nil ) ;// set the window to be the current port
- }
-
- break ;
- }
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoNullEvent
- *------------------------------------------------------------------------------*
- This routine handles null events.
- The event is sent to all windows by calling each NullProcPtr.
- This routine is called by DoEvent().
- \*------------------------------------------------------------------------------*/
- static void DoNullEvent ( EventRecord *event )
- {
- (void) CallAllWinNullProcs( event );
- }
-
-
-
-