home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Papers / C++ Exceptions / µShell / Event Handling / EventLoop.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-15  |  5.7 KB  |  196 lines  |  [TEXT/CWIE]

  1. #ifndef __EVENTLOOP__
  2. #define __EVENTLOOP__
  3. #pragma once
  4.  
  5. #include "Module.h"
  6. #include "EventLoop.h"
  7. #include "ToolboxEvent.h"
  8. #include "EventFilterChain.h"
  9.  
  10. #include <AppleEvents.h>
  11. #include <Dialogs.h>
  12. #include <stdarg.h>
  13.  
  14.  
  15. class EventLoop;
  16.  
  17. class EventLoop : public TModule, public EventFilterChain
  18. {
  19.     DeclareClassSingle(EventLoop, TModule);
  20.  
  21. public:
  22.     enum AppPhase
  23.     {
  24.         kAppInitializing,
  25.         kAppInitialized,
  26.         kAppActive,            // We've just recieved a non-null event
  27.         kAppSleepy,            // Received our first null event -- calc sleep time
  28.         kAppSleeping,        // We're idle
  29.         kAppDone
  30.     };
  31.     
  32. private:
  33.     //
  34.     //    Toolbox entry points - access with GetXXXProc()
  35.     //
  36.     static pascal Boolean    xAEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn);
  37.     static pascal Boolean    xAEFilterProc(EventRecord *theEvent, long returnID, long transactionID, const AEAddressDesc *sender);
  38.     static pascal Boolean    xModalFilterProc(DialogPtr theDialog, EventRecord *theEvent, DialogItemIndex *itemHit);
  39.     static pascal OSErr        xAEEventHandlerProc(const AppleEvent *theAppleEvent, AppleEvent *reply, UInt32 handlerRefcon);
  40.     static pascal OSErr        xAEEventHandlerProc1(const AppleEvent *theAppleEvent, AppleEvent *reply, UInt32 handlerRefcon);
  41.     
  42.     //
  43.     //    va_arg equivalents of above
  44.     //
  45.     static OSStatus vAEIdleProc(va_list args);
  46.     static OSStatus vAEFilterProc(va_list args);
  47.     static OSStatus vModalFilterProc(va_list args);
  48.     static OSStatus vAEEventHandlerProc(va_list args);
  49.     
  50. #if GENERATINGCFM
  51.         enum ProcSelector
  52.         {
  53.             kAEIdleProc,
  54.             kAEFilterProc,
  55.             kAEModalFilterProc,
  56.             kAEEventHandlerProc,
  57.             kAEEventHandlerProc1,
  58.             kNumProcSelectors
  59.         };
  60.  
  61.         static RoutineDescriptor    gProcDescriptors[kNumProcSelectors];
  62.  
  63.     public:
  64.         static inline AEIdleUPP            GetAEIdleProc()                { return &gProcDescriptors[kAEIdleProc];            }
  65.         static inline AEFilterUPP        GetAEFilterProc()            { return &gProcDescriptors[kAEFilterProc];            }
  66.         static inline ModalFilterUPP    GetModalFilterProc()        { return &gProcDescriptors[kAEModalFilterProc];        }
  67.         static inline AEEventHandlerUPP    GetAEEventHandlerProc()        { return &gProcDescriptors[kAEEventHandlerProc];    }
  68.         static inline AEEventHandlerUPP    GetAEEventHandlerProc1()    { return &gProcDescriptors[kAEEventHandlerProc1];    }
  69. #else
  70.  
  71.     public:
  72.         static inline AEIdleUPP            GetAEIdleProc()                { return &xAEIdleProc;                               }
  73.         static inline AEFilterUPP        GetAEFilterProc()            { return &xAEFilterProc;                          }
  74.         static inline ModalFilterUPP    GetModalFilterProc()        { return &xModalFilterProc;                          }
  75.         static inline AEEventHandlerUPP    GetAEEventHandlerProc()        { return &xAEEventHandlerProc;                    }
  76.         static inline AEEventHandlerUPP    GetAEEventHandlerProc1()    { return &xAEEventHandlerProc1;                    }
  77.  
  78. #endif
  79. protected:
  80.     static    EventLoop*    gEventLoop;
  81.  
  82. public:
  83.     static bool ProcessEvent(EventRecord& theEvent, long *sleepTime, RgnHandle *mouseRgn);
  84.     static void    Idle();
  85.     static void    Yield();
  86.  
  87.     static void* InstallAEHandler(
  88.             AEEventClass             theAEEventClass,
  89.             AEEventID                 theAEEventID,
  90.             AEEventHandlerProcPtr    handler,
  91.             Size                    dataSize    = 0);
  92.  
  93. protected:
  94.     UInt16                fNestLevel; // number of times TBEventProc has been entered
  95.     EventMask            fEventMask;    // mask for WNE
  96.     UInt32                fSleepTime;    // sleep value for WNE
  97.     UInt32                fDreamTime;    // 
  98.     RgnHandle            fMouseRgn;    // mouse region for WNE
  99.     AppPhase            fAppPhase;
  100.     ToolboxEvent        fLastEvent;
  101.     ToolboxEvent        fLastMouseUp;
  102.     EventFilterChain    fFilterChain;
  103.     
  104. public:
  105.     EventLoop(EventMask mask = everyEvent);
  106.     virtual ~EventLoop();
  107.     
  108.     virtual void    Initialize(void);    // Do your thing
  109.     virtual void    Finalize(void);
  110.  
  111.     static    void Run();
  112.     static    void Quit();
  113.     
  114.     static    void    PushEventFilter(EventFilter& filter);
  115.  
  116.     
  117.     inline    UInt32        GetSleepTime()        { return fSleepTime;    }
  118.     inline    RgnHandle    GetMouseRgn()        { return fMouseRgn;        }
  119.  
  120. protected:
  121.     //
  122.     //    Primative entry points -- 
  123.     //
  124.     virtual bool HandleEventProc(EventRecord& theEvent, long *sleepTime, RgnHandle *mouseRgn);
  125.     virtual bool AEIdleProc(EventRecord& theEvent, long& sleepTime, RgnHandle& mouseRgn);
  126.     virtual bool AEFilterProc(EventRecord& theEvent, long returnID, long transactionID, const AEAddressDesc& sender);
  127.     virtual bool ModalFilterProc(DialogPtr theDialog, EventRecord& theEvent, DialogItemIndex& itemHit);
  128.  
  129.     static void InteractWithUser(long                     timeOutInTicks    = kAEDefaultTimeout,
  130.                          NMRecPtr                 nmReqPtr        = nil);
  131.  
  132.     virtual bool PollForEvent(EventRecord& theEvent);
  133.     virtual void DoRun();
  134.     virtual void DoQuit();
  135.     virtual bool DoIdle();
  136.  
  137.     virtual    void BeginEvent(ToolboxEvent& theEvent);
  138.     virtual    bool FilterEvent(ToolboxEvent& theEvent);
  139.     virtual    bool HandleEvent(ToolboxEvent& theEvent);
  140.     virtual    bool HandleNullEvent(ToolboxEvent& theEvent);
  141.     virtual    bool HandleHighLevelEvent(ToolboxEvent& theEvent);
  142.     virtual    void EndEvent(ToolboxEvent& theEvent);
  143.  
  144.     virtual    void BeforeEventLoop();
  145.     virtual    void AfterEventLoop();
  146.     
  147.     friend class WithNewEvent : public ToolboxEvent
  148.     {
  149.     protected:
  150.         EventLoop&         fLoop;
  151.         long*            fSleepTime;
  152.         RgnHandle*        fMouseRgn;
  153.     
  154.     public:
  155.         WithNewEvent(EventLoop& loop, EventRecord& theEvent, long *sleepTime = nil, RgnHandle *mouseRgn = nil);
  156.         ~WithNewEvent();
  157.     };
  158.  
  159. };
  160.  
  161. extern EventLoop& gEventLoop;
  162.  
  163. inline EventLoop::EventLoop
  164.     (EventMask mask)
  165. :    fNestLevel(0),
  166.     fEventMask(mask),
  167.     fSleepTime(0),
  168.     fDreamTime(6),
  169.     fMouseRgn(nil),
  170.     fAppPhase(kAppInitializing)
  171. {
  172.     gEventLoop = this;
  173. }
  174.  
  175. class FGEventLoop : public EventLoop
  176. {
  177. public:
  178.     inline FGEventLoop(EventMask mask)
  179.     : EventLoop(mask) {}
  180.     virtual ~FGEventLoop();
  181.     
  182.     virtual    bool HandleEvent(ToolboxEvent& theEvent);
  183.  
  184. protected:
  185.     virtual    bool HandleMouseEvent(ToolboxEvent& theEvent);
  186.     virtual    bool HandleKeyboardEvent(ToolboxEvent& theEvent);
  187.     virtual    bool HandleWindowEvent(ToolboxEvent& theEvent);
  188.     
  189.     virtual bool HandleMenuSelection(long what);
  190.     
  191. private:
  192.     typedef EventLoop Inherited;
  193. };
  194.  
  195. #endif __EVENTLOOP__
  196.