home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / pbClock / EventLoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-29  |  3.6 KB  |  136 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        EventLoop.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  26. #include "App.protos.h"        /* Get the prototypes for application.            */
  27.  
  28.  
  29.  
  30. /*****************************************************************************/
  31.  
  32.  
  33.  
  34. extern RgnHandle    gCursorRgn;                /* DTS.Lib..framework global. */
  35.     /* The current cursor region.  The initial cursor region is an empty region,
  36.     ** which will cause WaitNextEvent to generate a mouse-moved event, which will
  37.     ** cause us to set the cursor for the first time. */
  38.  
  39. extern Boolean        gQuitApplication;        /* DTS.Lib..framework global. */
  40.     /* This is set to false by Initialize.  When the user selects Quit
  41.     ** (and does not abort the quit), then this boolean is set true. */
  42.  
  43. Boolean        gLowOnMem;
  44. static void    ManageLowMem(void);
  45.  
  46. #define kRamReserve 0x10000L
  47. #define kRamSlop    0x08000L
  48.  
  49.  
  50.  
  51. /*****************************************************************************/
  52. /*****************************************************************************/
  53.  
  54.  
  55.  
  56. /* Get events forever, and handle them by calling DoEvent.  Get the events by
  57. ** calling WaitNextEvent. */
  58.  
  59. #pragma segment Main
  60. void    EventLoop(void)
  61. {
  62.     EventRecord        event;
  63.  
  64.     while (!gQuitApplication) {
  65.         ManageLowMem();
  66.         if (gCursorRgn) SetEmptyRgn(gCursorRgn);
  67.         if (!WaitNextEvent(everyEvent, &event, 15, gCursorRgn))
  68.             event.what = nullEvent;
  69.         DoEvent(&event);
  70.     };
  71. }
  72.  
  73.  
  74.  
  75. /*****************************************************************************/
  76.  
  77.  
  78.  
  79. /* This is called to determine if there is enough ram available for various
  80. ** application operations.  Typically, if there is not enough, certain menu
  81. ** items are dimmed, thus preventing further use of memory. */
  82.  
  83. #pragma segment Main
  84. static void    ManageLowMem(void)
  85. {
  86.     static Handle    reserveMemHndl;
  87.  
  88.     if (!reserveMemHndl) {
  89.         reserveMemHndl = NewHandle(0);
  90.         EmptyHandle(reserveMemHndl);
  91.     }
  92.  
  93.     if (gLowOnMem) {
  94.         ReallocateHandle(reserveMemHndl, kRamReserve + kRamSlop);
  95.         if (*reserveMemHndl) {
  96.             SetHandleSize(reserveMemHndl, kRamReserve);
  97.             HPurge(reserveMemHndl);
  98.             gLowOnMem = false;
  99.         }
  100.     }
  101.     else {
  102.         if (!*reserveMemHndl) {
  103.             ReallocateHandle(reserveMemHndl, kRamReserve);
  104.             if (*reserveMemHndl) HPurge(reserveMemHndl);
  105.             else {
  106.                 NewDocumentWindow(nil, 'LMEM', false);
  107.                 gLowOnMem = true;
  108.             }
  109.         }
  110.     }
  111. }
  112.  
  113.  
  114.  
  115. /*****************************************************************************/
  116. /*****************************************************************************/
  117. /*****************************************************************************/
  118.  
  119.  
  120.  
  121. /* •• Called by DTS.Lib..framework. •• */
  122.  
  123. /* Handle the cursor changes.  Most of the work is done by the application
  124. ** framework.  Each window has its own cursor calculation proc, so you
  125. ** shouldn't have to add any code here.  You may wish to do some special
  126. ** stuff prior to the DoWindowCursor call if you have modeless dialogs. */
  127.  
  128. #pragma segment Main
  129. void    DoCursor(void)
  130. {
  131.     DoWindowCursor();
  132. }
  133.  
  134.  
  135.  
  136.