home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / macintosh-c / macc-carbon-demos-nonbinhex.sit / macc-carbon-demos-nonbinhex / chap02-demo / LowEvents.c < prev    next >
C/C++ Source or Header  |  2001-05-04  |  7KB  |  271 lines

  1. // *******************************************************************************************
  2. // LowEvents.c                                                             CLASSIC EVENT MODEL
  3. // *******************************************************************************************
  4. // 
  5. // This program contains a main event loop function, together with subsidiary functions which 
  6. // perform nominal handling only of low-level and Operating System events.  It opens a window 
  7. // in which the types of all received low-level and Operating System events are displayed.  It 
  8. // terminates when the user clicks the window's close box.
  9. //
  10. // Event handling is only nominal in this program because its main purpose is to demonstrate
  11. // the basics of an application's main event loop.  Programs in later chapters demonstrate
  12. // the full gamut of individual event handling. 
  13. //
  14. // The program utilises the following resources:
  15. //
  16. // •    A 'plst' resource.
  17. //
  18. // •    A 'WIND' resource (purgeable).
  19. //
  20. // •    A 'SIZE' resource with the acceptSuspendResumeEvents, canBackground,
  21. //        doesActivateOnFGSwitch, and isHighLevelEventAware flags set.
  22. //  
  23. // *******************************************************************************************
  24.  
  25. // ………………………………………………………………………………………………………………………………………………………………………………………………………………………… includes
  26.  
  27. #include <Carbon.h>
  28.  
  29. // …………………………………………………………………………………………………………………………………………………………………………………………………………………………… defines
  30.  
  31. #define rWindowResource    128
  32.  
  33. #define topLeft(r)    (((Point *) &(r))[0])
  34. #define botRight(r)    (((Point *) &(r))[1])
  35.  
  36. // …………………………………………………………………………………………………………………………………………………………………………………………………… global variables
  37.  
  38. Boolean        gDone;
  39. RgnHandle    gCursorRegionHdl;
  40.  
  41. // …………………………………………………………………………………………………………………………………………………………………………………………… function prototypes
  42.  
  43. void    main                        (void);
  44. void    doPreliminaries    (void);
  45. void    doNewWindow            (void);
  46. void    eventLoop                (void);
  47. void    doEvents                (EventRecord *);
  48. void    doMouseDown            (EventRecord *);
  49. void    doUpdate                (EventRecord *);
  50. void    doOSEvent                (EventRecord *);
  51. void    drawEventString    (Str255);
  52. void    doAdjustCursor    (WindowRef);
  53.  
  54. // ************************************************************************************** main
  55.  
  56. void  main(void)
  57. {
  58.     doPreliminaries();
  59.     doNewWindow();
  60.     eventLoop();
  61. }
  62.  
  63. // *************************************************************************** doPreliminaries
  64.  
  65. void  doPreliminaries(void)
  66. {
  67.     MoreMasterPointers(48);
  68.  
  69.     InitCursor();
  70.     FlushEvents(everyEvent,0);
  71. }
  72.  
  73. // ******************************************************************************* doNewWindow
  74.  
  75. void  doNewWindow(void)
  76. {
  77.     WindowRef    windowRef;
  78.  
  79.     if(!(windowRef = GetNewCWindow(rWindowResource,NULL,(WindowRef) -1)))
  80.     {
  81.         SysBeep(10);
  82.         ExitToShell();
  83.     }
  84.  
  85.     SetPortWindowPort(windowRef);
  86.     TextSize(10);
  87. }
  88.  
  89. // ********************************************************************************* eventLoop
  90.  
  91. void  eventLoop(void)
  92. {
  93.     EventRecord    eventStructure;
  94.     Boolean            gotEvent;
  95.     
  96.     gDone = false;
  97.     gCursorRegionHdl = NewRgn();
  98.     doAdjustCursor(FrontWindow());
  99.     
  100.     while(!gDone)
  101.     {
  102.         gotEvent = WaitNextEvent(everyEvent,&eventStructure,180,gCursorRegionHdl);
  103.         if(gotEvent)
  104.             doEvents(&eventStructure);
  105.         else
  106.         {
  107.             if(eventStructure.what == nullEvent)
  108.                 drawEventString("\p   • nullEvent");
  109.         }
  110.     }
  111. }
  112.  
  113. // ********************************************************************************** doEvents
  114.  
  115. void    doEvents(EventRecord *eventStrucPtr)
  116. {
  117.     switch(eventStrucPtr->what)
  118.     {
  119.         case mouseDown:
  120.             drawEventString("\p   • mouseDown");
  121.             doMouseDown(eventStrucPtr);
  122.             break;
  123.  
  124.         case mouseUp:
  125.             drawEventString("\p   • mouseUp");
  126.             break;
  127.  
  128.         case keyDown:
  129.             drawEventString("\p   • keyDown");
  130.             break;
  131.  
  132.         case autoKey:
  133.             drawEventString("\p   • autoKey");
  134.             break;
  135.  
  136.         case updateEvt:
  137.             drawEventString("\p   • updateEvt");
  138.             doUpdate(eventStrucPtr);
  139.             break;
  140.  
  141.         case activateEvt:
  142.             drawEventString("\p   • activateEvt");
  143.             break;
  144.  
  145.         case osEvt:
  146.             drawEventString("\p   • osEvt - ");
  147.             doOSEvent(eventStrucPtr);
  148.             break;
  149.     }
  150. }
  151.     
  152. // ******************************************************************************* doMouseDown
  153.  
  154. void    doMouseDown(EventRecord *eventStrucPtr)
  155. {
  156.     WindowPartCode    partCode;
  157.     WindowRef                windowRef;
  158.     
  159.     partCode = FindWindow(eventStrucPtr->where,&windowRef);
  160.     
  161.     switch(partCode)
  162.     {
  163.         case inContent:
  164.             if(windowRef != FrontWindow())
  165.                 SelectWindow(windowRef);
  166.             break;
  167.  
  168.         case inDrag:
  169.             DragWindow(windowRef,eventStrucPtr->where,NULL);
  170.             doAdjustCursor(windowRef);
  171.             break;
  172.  
  173.         case inGoAway:
  174.             if(TrackGoAway(windowRef,eventStrucPtr->where))
  175.                 gDone = true;
  176.             break;
  177.     }
  178. }
  179.  
  180. // ********************************************************************************** doUpdate
  181.  
  182. void    doUpdate(EventRecord *eventStrucPtr)
  183. {
  184.     BeginUpdate((WindowRef) eventStrucPtr->message);
  185.     EndUpdate((WindowRef) eventStrucPtr->message);
  186. }
  187.  
  188. // ********************************************************************************* doOSEvent
  189.  
  190. void    doOSEvent(EventRecord *eventStrucPtr)
  191. {
  192.     Cursor    arrow;
  193.  
  194.     switch((eventStrucPtr->message >> 24) & 0x000000FF)
  195.     {
  196.         case suspendResumeMessage:
  197.             if((eventStrucPtr->message & resumeFlag) == 1)
  198.             {
  199.                 SetCursor(GetQDGlobalsArrow(&arrow));
  200.                 DrawString("\pResume");
  201.             }
  202.             else
  203.                 DrawString("\pSuspend");
  204.             break;
  205.  
  206.         case mouseMovedMessage:
  207.             doAdjustCursor(FrontWindow());
  208.             DrawString("\pMouse-moved");
  209.             break;
  210.     }
  211. }
  212.  
  213. // *************************************************************************** drawEventString
  214.  
  215. void    drawEventString(Str255 eventString)
  216. {
  217.     WindowRef    windowRef;
  218.     RgnHandle    tempRegion;
  219.     Rect            portRect;
  220.  
  221.     windowRef = FrontWindow();
  222.     tempRegion = NewRgn();
  223.  
  224.     GetWindowPortBounds(windowRef,&portRect);
  225.     ScrollRect(&portRect,0,-15,tempRegion);
  226.     DisposeRgn(tempRegion);
  227.     
  228.     MoveTo(8,340);
  229.     DrawString(eventString);
  230. }
  231.  
  232. // **************************************************************************** doAdjustCursor
  233.  
  234. void  doAdjustCursor(WindowRef windowRef)
  235. {
  236.     RgnHandle    myArrowRegion;
  237.     RgnHandle    myIBeamRegion;
  238.     Rect            cursorRect;
  239.     Point            mousePt;
  240.     Cursor        arrow;
  241.     
  242.     myArrowRegion = NewRgn();
  243.     myIBeamRegion = NewRgn();
  244.     SetRectRgn(myArrowRegion,-32768,-32768,32767,32767);
  245.     
  246.     GetWindowPortBounds(windowRef,&cursorRect);
  247.     SetPortWindowPort(windowRef);
  248.     LocalToGlobal(&topLeft(cursorRect));
  249.     LocalToGlobal(&botRight(cursorRect));    
  250.  
  251.     RectRgn(myIBeamRegion,&cursorRect);
  252.     DiffRgn(myArrowRegion,myIBeamRegion,myArrowRegion);
  253.     
  254.     GetGlobalMouse(&mousePt);
  255.     if(PtInRgn(mousePt,myIBeamRegion))
  256.     {
  257.         SetCursor(*(GetCursor(iBeamCursor)));
  258.         CopyRgn(myIBeamRegion,gCursorRegionHdl);
  259.     }
  260.     else
  261.     {
  262.         SetCursor(GetQDGlobalsArrow(&arrow));
  263.         CopyRgn(myArrowRegion,gCursorRegionHdl);
  264.     }
  265.     
  266.     DisposeRgn(myArrowRegion);
  267.     DisposeRgn(myIBeamRegion);
  268. }
  269.  
  270. // *******************************************************************************************
  271.