home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Snippets / QuickDraw / ScreenDump / RubberBand.c < prev   
Encoding:
Text File  |  1994-10-28  |  2.5 KB  |  108 lines  |  [TEXT/KAHL]

  1. //   Rubber Bandit is a simple
  2. //   srcXor rubber-banding example.
  3. //   - DMH, MacDTS, 3/5/91
  4. //
  5. // Modification History: 
  6. //
  7. //    9/23/94        nick        make RubberBandIt return the bounding rect, 
  8. //                            cleaned up discarded some fns
  9. //
  10. //    Copyright:    © 1991-94 by Apple Computer, Inc., all rights reserved.
  11.  
  12.  
  13. // function prototypes:
  14.  
  15. Rect     RubberBandIt(Point anchorPt);
  16. Rect    *CheckRect(Rect *theRect);
  17. void    DrawStuff(Rect *rubberRect);
  18.  
  19.  
  20.  
  21. /*    ================================================
  22.     RubberBandIt follows the mouse and rubber-bands
  23.     a design based on its position.  It retains
  24.     control until the mouse button is released.
  25.     ================================================    */
  26.  
  27. Rect RubberBandIt(Point    anchorPt)
  28. {
  29.     Point    curMousePt = {0, 0};
  30.     Rect    rubberRect;
  31.  
  32.     PenMode(srcXor);                                /* Set pen mode to exclusive or.*/
  33.     PenPat(&gray);
  34.  
  35.     GetMouse(&curMousePt);                            /* Get current mouse pos.        */
  36.  
  37.     rubberRect.top = anchorPt.v;                    /* Draw initial rectangle.        */
  38.     rubberRect.left = anchorPt.h;
  39.     rubberRect.bottom = curMousePt.v;
  40.     rubberRect.right = curMousePt.h;
  41.     DrawStuff(&rubberRect);
  42.  
  43.     while (Button())
  44.     {
  45.         GetMouse(&curMousePt);                        /* Get current mouse pos…        */
  46.  
  47.         if (curMousePt.h != rubberRect.right ||        /* If mouse moved…                */
  48.             curMousePt.v != rubberRect.bottom)
  49.         {
  50.             DrawStuff(&rubberRect);                    /* Erase old…                    */
  51.             rubberRect.bottom = curMousePt.v;
  52.             rubberRect.right = curMousePt.h;
  53.             DrawStuff(&rubberRect);                    /* and draw new.                */
  54.         }
  55.     }
  56.     
  57.     DrawStuff(&rubberRect);                            // Erase old at end.            */
  58.     return rubberRect ;
  59. }
  60.  
  61.  
  62. /*    ================================================
  63.     DrawStuff draws designs in the rectangle
  64.     passed, using the current pen mode, etc.
  65.     ================================================    */
  66.  
  67. void DrawStuff(rubberRect)
  68. Rect    *rubberRect;
  69. {
  70.     Rect    curRect;
  71.     
  72.     curRect = *rubberRect;
  73.     FrameRect(CheckRect(&curRect));
  74. }
  75.  
  76.  
  77.  
  78.  
  79. /*    ================================================
  80.     CheckRect makes sure that the top of the
  81.     rectangle passed is ≤ the bottom and the left is
  82.     ≤ the right.  FrameRect needs things this way.
  83.     ================================================    */
  84.  
  85. Rect *CheckRect(theRect)
  86. Rect    *theRect;
  87. {
  88.     short    temp;
  89.  
  90.     if (theRect->top > theRect->bottom)        /* Need to reverse top and bottom? */
  91.     {
  92.         temp = theRect->top;
  93.         theRect->top = theRect->bottom;
  94.         theRect->bottom = temp;
  95.     }
  96.  
  97.     if (theRect->left > theRect->right)        /* Need to reverse left and right? */
  98.     {
  99.         temp = theRect->left;
  100.         theRect->left = theRect->right;
  101.         theRect->right = temp;
  102.     }
  103.     
  104.     return theRect;                            /* This makes nested calls easier.    */
  105. }
  106.  
  107.  
  108.