home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / ZoomRecter / Source / main.c
Encoding:
C/C++ Source or Header  |  1996-09-17  |  4.3 KB  |  207 lines  |  [TEXT/CWIE]

  1. /*
  2.     Zoom-Rect-er
  3.     DTS Code Snippet
  4.     
  5.     1/6/92    Steve Falkenburg
  6.     
  7.     This snippet shows how to do "Finder" style zooming between two rectangles.
  8.     The boolean flag "kZoomLarger" controls the proportional direction of the zooming.
  9.     
  10.     To get the two rectangles, you drag them out rubberbanded, and the zoom occurs between
  11.     them.  To quit, click the close box.
  12.     
  13.     If you want to do zooms between windows, open up a port with the dimensions of the desktop
  14.     (from GetGrayRgn()).
  15.     
  16.     DON'T use this as a sample of how to do rubberband drawing!!!  It's sort of hacked
  17.     together bypassing the event mechanism and just using Button().
  18. */
  19.  
  20. #include <Dialogs.h>
  21. #include <Fonts.h>    
  22.  
  23. #define    kNumSteps        14
  24. #define    kRectsVisible    4
  25. #define    kZoomRatio        .7
  26. #define    kDelayTicks        1
  27.  
  28. #define    kZoomLarger        true        // change this to zoom "inward"
  29.  
  30.  
  31. void InitStuff(void);
  32. void ZoomRect(Boolean zoomOut,Rect *smallRect, Rect *bigRect);
  33. void CalcRect(Rect *theRect,Rect *smallRect,Rect *bigRect,double stepValue);
  34. Boolean GetRects(Rect *zoomFrom,Rect *zoomTo);
  35. void FixRect(Rect *theRect,Rect *rightRect);
  36.  
  37. Boolean gDone;
  38.  
  39. void main(void)
  40. {
  41.     WindowPtr window;
  42.     Rect bounds = {44,12,330,500},zoomFrom,zoomTo;
  43.         
  44.     InitStuff();
  45.     window = NewWindow(nil,&bounds,"\pDrag Two Rects to Zoom",true,documentProc,(WindowPtr)-1L,true,0);
  46.     SetPort(window);
  47.     
  48.     do {
  49.         if (GetRects(&zoomFrom,&zoomTo))
  50.             ZoomRect(kZoomLarger,&zoomFrom,&zoomTo);
  51.         EraseRect(&window->portRect);
  52.     }
  53.     while (!gDone);
  54.     
  55.     FlushEvents(everyEvent,0);
  56. }
  57.  
  58.     
  59. void InitStuff(void)
  60. {
  61.     InitGraf(&qd.thePort);
  62.     InitFonts();
  63.     InitWindows();
  64.     InitMenus();
  65.     TEInit();
  66.     InitDialogs(nil);
  67.     InitCursor();
  68.     FlushEvents(everyEvent,0);
  69. }
  70.  
  71.  
  72. void ZoomRect(Boolean zoomLarger,Rect *smallRect, Rect *bigRect)
  73. {
  74.     double firstStep,stepValue,trailer,zoomRatio;
  75.     short i,step;
  76.     Rect curRect;
  77.     long ticks;
  78.     
  79.     PenPat(&qd.gray);
  80.     PenMode(patXor);
  81.     
  82.     
  83.     firstStep=kZoomRatio;
  84.     for (i=0; i<kNumSteps; i++) {
  85.         firstStep *= kZoomRatio;
  86.     }
  87.  
  88.     if (!zoomLarger) {
  89.         zoomRatio = 1.0/kZoomRatio;
  90.         firstStep = 1.0-firstStep;
  91.     }
  92.     else
  93.         zoomRatio = kZoomRatio;
  94.         
  95.     trailer = firstStep;
  96.     stepValue = firstStep;
  97.     for (step=0; step<(kNumSteps+kRectsVisible); step++) {
  98.     
  99.         // draw new frame
  100.         
  101.         if (step<kNumSteps) {
  102.             stepValue /= zoomRatio;
  103.             CalcRect(&curRect,smallRect,bigRect,stepValue);
  104.             FrameRect(&curRect);
  105.         }
  106.         
  107.         // erase old frame
  108.         
  109.         if (step>=kRectsVisible) {
  110.             trailer /= zoomRatio;
  111.             CalcRect(&curRect,smallRect,bigRect,trailer);
  112.             FrameRect(&curRect);
  113.         }
  114.  
  115.         Delay(kDelayTicks,&ticks);
  116.     }
  117.  
  118.     PenNormal();
  119. }
  120.  
  121.  
  122. void CalcRect(Rect *theRect,Rect *smallRect,Rect *bigRect,double stepValue)
  123. {
  124.     theRect->left = smallRect->left + (short)((double)(bigRect->left-smallRect->left)*stepValue);
  125.     theRect->top = smallRect->top + (short)((double)(bigRect->top-smallRect->top)*stepValue);
  126.     theRect->right = smallRect->right + (short)((double)(bigRect->right-smallRect->right)*stepValue);
  127.     theRect->bottom = smallRect->bottom + (short)((double)(bigRect->bottom-smallRect->bottom)*stepValue);
  128. }
  129.  
  130.  
  131. Boolean GetRects(Rect *zoomFrom,Rect *zoomTo)
  132. {
  133.     short numRects = 0;
  134.     /*EventRecord ev;*/
  135.     Rect theRect,drawRect;
  136.     Point firstPt,curPt,oldPt,globalPt;
  137.     /*KeyMap theKeys;*/
  138.     WindowPtr window;
  139.     Boolean result = true;
  140.     
  141.     PenMode(patXor);
  142.     
  143.     do {
  144.         while (!Button()){
  145.         
  146.         GetMouse(&globalPt);
  147.         LocalToGlobal(&globalPt);
  148.         if ((FindWindow(globalPt,&window)==inGoAway) && window==FrontWindow()) {
  149.             gDone = true;
  150.             result = false;
  151.         }
  152.         
  153.         GetMouse(&firstPt);
  154.         oldPt = firstPt;
  155.         SetRect(&theRect,firstPt.h,firstPt.v,firstPt.h,firstPt.v);
  156.         }
  157.         while (Button()) {
  158.             GetMouse(&curPt);
  159.             if (!EqualPt(curPt,oldPt)) {
  160.                 FixRect(&theRect,&drawRect);
  161.                 FrameRect(&drawRect);
  162.                 oldPt = curPt;
  163.                 theRect.right = curPt.h;
  164.                 theRect.bottom = curPt.v;
  165.                 FixRect(&theRect,&drawRect);
  166.                 FrameRect(&drawRect);
  167.             }
  168.         }
  169.         
  170.         FixRect(&theRect,&drawRect);
  171.         if (numRects==0)
  172.             *zoomFrom = drawRect;
  173.         else
  174.             *zoomTo = drawRect;
  175.             
  176.         numRects++;
  177.  
  178.     } while (numRects<2);
  179.     
  180.     PenNormal();
  181.     
  182.     return result;
  183. }
  184.  
  185.  
  186. void FixRect(Rect *theRect,Rect *rightRect)
  187. {
  188.     if (theRect->right > theRect->left) {
  189.         rightRect->right = theRect->right;
  190.         rightRect->left = theRect->left;
  191.     }
  192.     else {
  193.         rightRect->right = theRect->left;
  194.         rightRect->left = theRect->right;
  195.     }
  196.     
  197.     if (theRect->bottom > theRect->top) {
  198.         rightRect->bottom = theRect->bottom;
  199.         rightRect->top = theRect->top;
  200.     }
  201.     else {
  202.         rightRect->bottom = theRect->top;
  203.         rightRect->top = theRect->bottom;
  204.     }
  205.     
  206. }
  207.