home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / Skel 3.0i / Skel nextevent.p < prev    next >
Encoding:
Text File  |  1994-04-18  |  4.9 KB  |  145 lines  |  [TEXT/PJMM]

  1. unit NextEvent;
  2. interface
  3.     uses
  4.         Globals, DoCommand, ReSize, UpdateWindow, HighLevelEvents, AppleEvents;
  5.  
  6.     procedure MainEventLoop;
  7.  
  8. implementation
  9.  
  10. {the main loop that handles events}
  11. {############################   MainEventLoop  ##########################}
  12.  
  13. {  Brace yourself: here's where the action is.  Most Mac programs just}
  14. {  wait for events (as do we all), and then process them.  There are}
  15. {  two sorts of events: those directly initiated by the user, like key}
  16. {  presses and mouse-downs, and those consequent events posted by the}
  17. {  Event Manager, like update and activate events.  The latter MUST be}
  18. {  handled correctly and carefully.  In particular, it's important for}
  19. {  all events to make sure that the event occurred in or for the window}
  20. {  you expect -- it's possible to get events which are not for one of}
  21. {  our windows, despite GetNextEvent's return value.  Similarly,}
  22. {  be sure to check that the window it occured in is the active one,}
  23. {  if it matters.}
  24.  
  25. {  A common mistake in handling update and activate events is in finding}
  26. {  out which window they are for.  It is "(WindowPtr)myevent.message "}
  27. {  that gives this information, NOT "whichwindow" (the WindowPtr returned}
  28. {  by FindWindow).  The latter pointer merely tells you what window}
  29. {  the mouse was in at the time the event was posted -- completely}
  30. {  irrelevant for Update and Activate events.  Think it through carefully.}
  31.  
  32.     procedure MainEventLoop;
  33.         const
  34.             kSleep = 5; {Should be a variable, the sleep time for WaitNextEvent.}
  35.         var
  36.             diskInitPt: Point;
  37.             it: Longint;
  38.             hasEvent: Boolean;
  39.     begin
  40. (* body of MainEventLoop *)
  41.  
  42.         FlushEvents(everyEvent, 0);(* discard leftover events *)
  43.  
  44. (* get next event, and Handle it appropriately, until user QUITs *)
  45.  
  46.         userdone := false;
  47.         repeat
  48. {Get next event, with WNE if we can /LIR}
  49.             if gWNEImplemented then
  50.                 hasEvent := WaitNextEvent(everyEvent, myevent, kSleep, nil)
  51.             else
  52.                 begin
  53.                     SystemTask;    (* Handle desk accessories *)
  54.                     hasEvent := GetNextEvent(everyEvent, myevent);
  55.                 end;
  56.  
  57.             if hasEvent then
  58.                 begin (* get event; if for us... *)
  59.                     case (myevent.what) of
  60. (* Handle each kind of event *)
  61.  
  62.                         mouseDown:
  63. (* find out what window the mouse went down in, and where in it *)
  64.                             begin
  65.                                 windowcode := FindWindow(myevent.where, whichwindow);
  66.                                 case (windowcode) of
  67.                 (* Handle mouse-down for each place *)
  68.                                     inSysWindow:
  69.                 (* Handle the desk accessories *)
  70.                                         SystemClick(myevent, whichwindow);
  71.                 (* inSysWindow *)
  72.                                     inMenuBar: (* Handle the command *)
  73.                                         begin
  74.                                             it := MenuSelect(myevent.where);
  75.                                             userdone := DoCommand(it);
  76. {userdone := DoCommand(MenuSelect(myevent.where));}
  77.                                         end;
  78.                 (* inMenuBar *)
  79.                                     inDrag: (* drag the window *)
  80.                                         DragWindow(whichwindow, myevent.where, dragrect);
  81.                                     inContent: 
  82.                                         begin
  83.                 (* includes inGrow if window inactive.}
  84. {                   Activate window *)
  85.                                             if (whichwindow = mywindow) then
  86.                 (* make sure it's for mine *)
  87.                                                 if (whichwindow <> FrontWindow) then
  88.                                                     SelectWindow(whichwindow);
  89.                 (* make it active *)
  90.                                         end;
  91.                                     inGrow:
  92.                 (* window is already active; change its size *)
  93.                                         if (whichwindow = mywindow) then
  94.                 (* make sure it's for mine *)
  95.                                             ReSize(mywindow, myevent.where);
  96.                 (* inGrow *)
  97.                                     inGoAway: 
  98.                                         ;
  99.                 (* we don't have a GoAway Region *)
  100.                                 end; (* case *)
  101.                             end; {mouseDown}
  102.  
  103.                         keyDown, autoKey:     (* if command key, pass the char to MenuKey *)
  104.                             if BitAnd(myevent.modifiers, cmdKey) <> 0 then
  105.                                 userdone := DoCommand(MenuKey(char(BitAnd(myevent.message, charcodemask))));
  106.                         updateEvt: (* if it's for our window, update it *)
  107.                             if (WindowPtr(myevent.message) = mywindow) then
  108.                                 UpdateWindow(mywindow);
  109.                 (* redraw the window contents *)
  110.                         activateEvt:
  111.                 (* if for our window, set port as nec. *)
  112.                             if (WindowPtr(myevent.message) = mywindow) then
  113.                                 begin
  114.                 (* my window *)
  115.                                     DrawGrowIcon(mywindow);
  116.                 (* redraw grow icon to reflect new state }
  117.                                     if BitAnd(myevent.modifiers, 1) = 1 then
  118.                 (* odd means an activate event *)
  119.                                         SetPort(mywindow)
  120.                 (* activate evt: work in our own port *)
  121.                                     else
  122.                                         SetPort(screenport);
  123.                 (* deactivate evt: our port is gone; keep port from dangling *)
  124.                                 end; {activateEvt}
  125.                         diskEvt:  {Handle uninitialized disks}
  126.                             if (HiWord(myevent.message) <> noErr) then
  127.                                 begin
  128.                                     DILoad;
  129.                                     diskInitPt.v := 120;
  130.                                     diskInitPt.h := 100;
  131.                                     if DIBadMount(diskInitPt, myevent.message) <> noErr then
  132.                                         ;
  133.                                     DIUnload;
  134.                                 end;{diskEvt, if}
  135.                         OSevt: 
  136.                             DoHighLevelEvent(myevent); {Added by LIR}
  137.                         kHighLevelEvent: 
  138.                             DoAppleEvent(myEvent); {Added by LIR}
  139.                         otherwise
  140.                     end; {case}
  141.                 end; {if GetNextEvent}
  142.         until userdone;
  143.     end;
  144.  
  145. end.