home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / lang / tml_doub.src < prev    next >
Internet Message Format  |  1986-03-11  |  4KB

  1. Return-Path: <oster%ucblapis@BERKELEY.EDU>
  2. Received: from ucbvax.berkeley.edu by SUMEX-AIM.ARPA with TCP; Mon 10 Mar 86 00:45:05-PST
  3. Received: by ucbvax.berkeley.edu (5.45/1.9)
  4.     id AA19777; Mon, 10 Mar 86 00:10:49 PST
  5. Received: from ucblapis.Berkeley.Edu (ucblapis.ARPA)
  6.     by ucbjade.Berkeley.Edu (4.19/4.41.3)
  7.     id AA00206; Sun, 9 Mar 86 23:18:45 pst
  8. Received: by ucblapis.Berkeley.Edu (4.19/4.41)
  9.     id AA09220; Sun, 9 Mar 86 17:13:31 pst
  10. Date: Sun, 9 Mar 86 17:13:31 pst
  11. From: oster%ucblapis@BERKELEY.EDU (David Phillip Oster)
  12. Message-Id: <8603100113.AA09220@ucblapis.Berkeley.Edu>
  13. To: info-mac@sumex-aim.arpa
  14. Subject: How to tell a double click.
  15.  
  16. Here is the T.M.L. Pascal procedure I've written to tell if a given
  17. mouseDown eventrecord is actually the first click of a double click.
  18. There are a couple of tricky points:
  19.  
  20. 1.) The user gets to set the DoubleClick time threshold from the control
  21. panel.  It is up to us to respect it.
  22.  
  23. 2.) You must arrange to return as soon as you know that a given
  24. eventRecord is NOT the first click of a double click - if you always
  25. wait for the doubleClick time to expire, the application will seem sluggish.
  26.  
  27. 3.) You have to allow for the mouse to move a little while the user is 
  28. doubleClicking (but not too much.)
  29.  
  30. { --- Cut Here --- }
  31. { ******************************************************************************* }
  32. {+++ 
  33. Wrap a toolbox procedure in a layer to make it
  34.   a function returning a useful value: LocalToGlobal
  35. Convert a point from the current window's coordinates to screen coordinates.
  36. Author: David Phillip Oster
  37. Date: 1/6/86
  38. +++}
  39. FUNCTION LocToGlob(p : Point) : Point;
  40. BEGIN
  41.   LocalToGlobal(p);
  42.   LocToGlob := p;
  43. END;
  44. { ******************************************************************************* }
  45. {+++ 
  46. Wrap a toolbox procedure in a layer to make it
  47.   a function returning a useful value: GetMouse
  48. return the mouse's position in local coordinates
  49. Author: David Phillip Oster
  50. Date: 1/6/86
  51. +++}
  52. FUNCTION MousePt : Point;
  53. VAR p : Point;
  54. BEGIN
  55.   GetMouse(p);
  56.   MousePt := p;
  57. END;
  58. { ******************************************************************************* }
  59. {+++ Calaculates on rectangles and points 
  60. CenterRectPt takes a rectangle and a pt and centers the rect 
  61.     with respect to the pt
  62. Author: David Phillip Oster
  63. Date: 1/6/86
  64. +++}
  65. FUNCTION CenterRectPt(theRect : Rect; thePt : Point ) : Rect;
  66. VAR newCorn : Point;
  67.   i : VHSelect;
  68. BEGIN
  69.   FOR i := v TO h DO 
  70.     newCorn.vh[i] := thePt.vh[i] -
  71.         ((theRect.botRight.vh[i] - theRect.topleft.vh[i]) div 2);
  72.   OffsetRect(theRect, newCorn.h, newCorn.v);
  73.   CenterRectPt := theRect;
  74. END;
  75. { ******************************************************************************* }
  76. {+++ WasDoubleClick - call with the event record from a mouseDown event.
  77. check for a second mouse down within DblTime time, and within 
  78.     8 pixels.  If such an event exists, remove it and 
  79.     return true otherwise, return false 
  80. Author: David Phillip Oster
  81. Date: 1/6/86
  82. +++}
  83. FUNCTION WasDoubleClick(VAR myEvent : EventRecord) : Boolean;
  84. TYPE
  85.   LongPtr = ^ LongInt;
  86. VAR
  87.   DblTime : LongPtr;
  88.   clickRect : Rect;
  89.   dnTime : LongInt;
  90.   dnEvent : EventRecord;
  91.   dontCare : Boolean;
  92. BEGIN
  93.   WasDoubleClick := FALSE;
  94.   DblTime := LongPtr(POINTER($2F0));    { get the user's doubletime threshold }
  95.   dnTime := myEvent.when + DblTime^;    { only wait until dnTime for second event }    
  96.   SetRect(clickRect, 0,0,12,12);
  97.   clickRect := CenterRectPt(clickRect, myEvent.where);
  98.       { +++ wait for the second event, leave loop as soon as we can +++ }
  99.   WHILE (TickCount < dnTime) AND
  100.       PtInRect(LocToGlob(MousePt), clickRect) AND
  101.       NOT EventAvail(mDownMask,dnEvent) DO 
  102.     { nothing } ;
  103.   { +++ waited long enough, is there a second mouse down? +++ }
  104.   IF EventAvail(mDownMask,dnEvent) THEN BEGIN
  105.     IF PtInRect(dnEvent.where, clickRect) THEN BEGIN
  106.         { +++ we know GetNextEvent will return true +++ }
  107.       WasDoubleClick := GetNextEvent(mDownMask,dnEvent);
  108.     END;
  109.   END;
  110. END;
  111. { --- Cut Here --- }
  112. This code would be much more elegant in C, because I could return 0; as
  113. soon as I knew that this particular event wasn't a doubleCLick, but it is
  114. okay in Pascal.
  115.  
  116. I hope this helps.  (I also do a much nicer double-click on title-bar of
  117. window goes to full screen and back than the new Mac Roms do.)
  118. ---David Phillip Oster
  119. ---------------------  ``What do you look like when you aren't visualizing?''
  120. Arpa: oster@lapis.berkeley.edu
  121. Uucp: ucbvax!ucblapis!oster
  122.