home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff386.lzh / XLispStat / src3.lzh / Mac / macresizebrush.c < prev    next >
C/C++ Source or Header  |  1990-07-30  |  4KB  |  178 lines

  1. #define _IVIEWWINDOW_
  2. #include "iview.h"
  3.  
  4. #ifdef MPWC
  5. #include <OSUtils.h>
  6. #include <TextEdit.h>
  7. #include <OSEvents.h>
  8. #define TRUE 1
  9. #define FALSE 0
  10. #define gray qd.gray
  11. #endif MPWC
  12.  
  13. #define ResizeBoxHeight 10
  14. #define ResizeBoxLeft 95
  15. #define ResizeBoxTop 50
  16.     
  17. static Rect ResizeMessageRect;
  18. static Rect ResizeBox = { ResizeBoxTop, ResizeBoxLeft,  
  19.                             ResizeBoxTop + ResizeBoxHeight,
  20.                             ResizeBoxLeft + ResizeBoxHeight},
  21.               windowRect = { 50, 150, 200, 350},
  22.               OKRect = { 120, 10, 140, 90 },
  23.               CancelRect = { 120, 110, 140, 190 };
  24.  
  25. static char ResizeMessage[] = "To resize brush click in this window and drag";
  26.  
  27. static Point mouseLoc;
  28. static Rect oldBrush, brush;
  29.  
  30. static ControlHandle OKButton, CancelButton;
  31. static WindowPtr wind;
  32. static int cancelled;
  33.  
  34. static Boolean DoContent();
  35.  
  36. IViewGetNewBrushSize(w, new_width, new_height)
  37.     IVIEW_WINDOW w;
  38.     int *new_width, *new_height;
  39. {
  40.   int left, top, width, height;
  41.   
  42.   IViewGetBrush(w, &left, &top, &width, &height);
  43.   left -= width; top -= height;
  44.   
  45.   SetRect(&oldBrush, left, top, left + width, top + height);
  46.   brush = oldBrush;
  47.  
  48.   setup_window();
  49.   SetPt(&mouseLoc, brush.right, brush.bottom);
  50.   DrawBrush();
  51.   event_loop();
  52.   DisposeWindow(wind);
  53.   if (new_width != nil) *new_width = brush.right - brush.left;
  54.   if (new_height != nil) *new_height = brush.bottom - brush.top;
  55.   return(! cancelled);
  56. }
  57.  
  58. static setup_window()
  59. {
  60.   wind = NewWindow(nil,&windowRect,"\p", true, 
  61.                   dBoxProc, (WindowPtr) -1, true, 0L);
  62.                           
  63.   OKButton = NewControl(wind,&OKRect,"\pOK", true, 0, 0, 0, 0, 0L);
  64.  
  65.   CancelButton = NewControl(wind,&CancelRect,"\pCancel", true, 0, 0, 0, 0, 0L);
  66.  
  67.   SetPort(wind);
  68.  
  69.   ResizeMessageRect = ResizeBox;
  70.   InsetRect(&ResizeMessageRect, -50, -20);
  71.   OffsetRect(&ResizeMessageRect, 0, -30);
  72.  
  73.   TextBox(ResizeMessage, (long) strlen(ResizeMessage), 
  74.           &ResizeMessageRect, 1);
  75. /*  FrameRect(&ResizeBox);*/
  76. }
  77.  
  78. static event_loop()
  79. {
  80.   EventRecord myEvent;
  81.   WindowPtr whichWindow;
  82.   Boolean done = false;
  83.   
  84.   cancelled = FALSE;
  85.   FlushEvents( everyEvent, 0 );
  86.   do {
  87.     if (GetNextEvent(everyEvent, &myEvent)) {
  88.       switch (myEvent.what) {
  89.       case mouseDown:
  90.         switch (FindWindow( myEvent.where, &whichWindow )) {
  91.         case inContent:
  92.           if (whichWindow == wind) done = DoContent(&myEvent);
  93.           else SysBeep(10);
  94.           break;
  95.         default:
  96.           SysBeep(10);
  97.           break;
  98.         } /* end switch FindWindow */
  99.         break;
  100.       default:; 
  101.       } /* end switch myEvent.what) */
  102.     }
  103.     MoveBrush();
  104.   } while (! done);
  105.   FlushEvents( everyEvent, 0 );
  106. }
  107.  
  108. static Boolean DoContent(theEvent)
  109.     EventRecord    *theEvent;
  110. {
  111.   int cntlCode;
  112.   ControlHandle theControl;
  113.  
  114.   GlobalToLocal( &(*theEvent).where );
  115.   switch (cntlCode = FindControl((*theEvent).where, wind, &theControl)) {
  116.   case inButton:
  117.     DrawBrush(); /* to erase */
  118.     if (theControl==OKButton) 
  119.       if (TrackControl(theControl, (*theEvent).where, nil)) return(true);
  120.     if (theControl==CancelButton) 
  121.       if (TrackControl(theControl, (*theEvent).where, nil)) {
  122.         brush = oldBrush;
  123.         cancelled = TRUE;
  124.         return(true);
  125.       }
  126.     DrawBrush(); /* to redraw */
  127.     break;
  128.   default:
  129. /*    if (PtInRect((*theEvent).where, &ResizeBox)) */
  130.       ResizeBrush((*theEvent).where);
  131.   }
  132.   return(false);
  133. }
  134.  
  135. static ResizeBrush(p)
  136.     Point p;
  137. {
  138.     Point BottomLeft, lastBottomLeft;
  139.  
  140.     BottomLeft = p;
  141.     DrawBrush();                  /* to erase */
  142.     Pt2Rect(p, BottomLeft, &brush);
  143.     mouseLoc = BottomLeft;
  144.     DrawBrush();                  /* to redraw */
  145.     while (Button()) {
  146.               lastBottomLeft = BottomLeft;
  147.         GetMouse(&BottomLeft);
  148.         if (! EqualPt(lastBottomLeft, BottomLeft)) {
  149.             DrawBrush();          /* to erase */
  150.             Pt2Rect(p, BottomLeft, &brush);
  151.             mouseLoc = BottomLeft;
  152.             DrawBrush();          /* to redraw */
  153.         }
  154.     }
  155. }
  156.  
  157. static DrawBrush()
  158. {
  159.   PenMode(patXor);
  160.   PenPat(gray);
  161.   FrameRect(&brush);  
  162.   PenNormal();
  163. }
  164.  
  165. static MoveBrush()
  166. {
  167.   Point oldMouseLoc;
  168.  
  169.   oldMouseLoc = mouseLoc;
  170.   GetMouse(&mouseLoc);
  171.   if ((oldMouseLoc.h != mouseLoc.h) || (oldMouseLoc.v != mouseLoc.v)) {
  172.     DrawBrush();                         /* to erase */
  173.     OffsetRect(&brush, mouseLoc.h - oldMouseLoc.h,
  174.                mouseLoc.v - oldMouseLoc.v);
  175.     DrawBrush();                         /* to redraw */
  176.   }
  177. }
  178.