home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / DTS.Draw / TRRectObj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-17  |  7.8 KB  |  293 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        TRRectObj.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1992-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19. /* See the files "=How to write your app" and "=Using TreeObj.c" for information
  20. ** on this function. */
  21.  
  22. /* This file implements the messages for the round-rect object.  Many of the messages
  23. ** can be handled by the rect object, as they deal with a rect structure.  Only
  24. ** a few of them are round-rect-specific. */
  25.  
  26. /* It would seem that you would want a custom hit-test message handler here, but
  27. ** the rect object first checks to see if the hit is within the bounding box of
  28. ** the object, and if so, it then calls the object to return the region.  This
  29. ** allows the rect object to generically handle hit-testing. */
  30.  
  31.  
  32.  
  33. /*****************************************************************************/
  34.  
  35.  
  36.  
  37. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  38. #include "App.protos.h"        /* Get the prototypes for the application.        */
  39.  
  40. #ifndef __OSEVENTS__
  41. #include <OSEvents.h>
  42. #endif
  43.  
  44. #ifndef __OSUTILS__
  45. #include <OSUtils.h>
  46. #endif
  47.  
  48. #ifndef __QUICKDRAW__
  49. #include <Quickdraw.h>
  50. #endif
  51.  
  52. #ifndef __STRING__
  53. #include <String.h>
  54. #endif
  55.  
  56. #ifndef __TREEOBJ2__
  57. #include "TreeObj2.h"
  58. #endif
  59.  
  60. #ifndef __UTILITIES__
  61. #include "Utilities.h"
  62. #endif
  63.  
  64.  
  65.  
  66. #pragma segment DrawObjects
  67. long    TRRectObj(TreeObjHndl hndl, short message, long data)
  68. {
  69.     Rect        rct, grabber, *rptr;
  70.     RgnHandle    rgn, accumRgn;
  71.     short        w, h, oldh, oldw;
  72.     ClickInfo    *click;
  73.     TreeObjHndl    hhndl;
  74.     Point        where, pt, curMouse;
  75.     EventRecord    option;
  76.     RGBColor    rgb, rgb2;
  77. #if VH_VERSION
  78.     char        *cptr;
  79. #endif
  80.  
  81.     switch (message) {
  82.         case INITMESSAGE:
  83.             TRectObj(hndl, message, data);
  84.             mDerefRRect(hndl)->width = mDerefRRect(hndl)->height = 20;
  85.             break;
  86.  
  87.         case FREEMESSAGE:
  88.         case COPYMESSAGE:
  89.         case UNDOMESSAGE:
  90.         case CONVERTMESSAGE:
  91.         case FREADMESSAGE:
  92.         case FWRITEMESSAGE:
  93.         case HREADMESSAGE:
  94.         case HWRITEMESSAGE:
  95.         case GETOBJRECTMESSAGE:
  96.         case SETOBJRECTMESSAGE:
  97.         case SECTOBJRECTMESSAGE:
  98.         case CLICKMESSAGE:
  99.         case KEYMESSAGE:
  100.         case SETSELECTMESSAGE:
  101.         case GETSELECTMESSAGE:
  102.         case COMPAREMESSAGE:
  103.             return(TRectObj(hndl, message, data));
  104.             break;
  105.  
  106.         case GETBBOXMESSAGE:
  107.             TRectObj(hndl, message, data);
  108.             if (mDerefCommon(hndl)->selected) {
  109.                 rct = mDerefRRect(hndl)->rect;
  110.                 w   = mDerefRRect(hndl)->width;
  111.                 h   = mDerefRRect(hndl)->height;
  112.                 grabber.top  = rct.top  + (h / 2);
  113.                 grabber.left = rct.left + (w / 2);
  114.                 grabber.bottom = (grabber.top  -= 3) + 6;
  115.                 grabber.right  = (grabber.left -= 3) + 6;
  116.                 rptr = (Rect *)data;
  117.                 if (rptr->right < grabber.right)
  118.                     rptr->right = grabber.right;
  119.                 if (rptr->bottom < grabber.bottom)
  120.                     rptr->bottom = grabber.bottom;
  121.             }
  122.             break;
  123.  
  124.         case HITTESTMESSAGE:
  125.             click = (ClickInfo *)data;
  126.             hhndl = (TreeObjHndl)TRectObj(hndl, message, data);
  127.                 /* We must call TRectObj::HITTESTMESSAGE to set some static
  128.                 ** variables that TRectObj::SELECTOBJMESSAGE uses. */
  129.             if ((!hhndl) && (click->message == HITTESTGRABBER)) {
  130.                 if (mDerefRoot(GetRootHndl(hndl))->numSelected == 1) {
  131.                     if (mDerefRRect(hndl)->selected) {
  132.                         rct   = mDerefRRect(hndl)->rect;
  133.                         where = click->localEvent.where;
  134.                         GetMouse(&curMouse);
  135.                         grabber.top  = pt.v = (rct.top  + (mDerefRRect(hndl)->height / 2));
  136.                         grabber.left = pt.h = (rct.left + (mDerefRRect(hndl)->width / 2));
  137.                         grabber.bottom = (grabber.top  -= 3) + 6;
  138.                         grabber.right  = (grabber.left -= 3) + 6;
  139.                         if (PtInRect(where, &grabber)) {
  140.                             click->localEvent.where = pt;
  141.                             click->offset.h         = pt.h - curMouse.h;
  142.                             click->offset.v         = pt.v - curMouse.v;
  143.                             click->oldFlip          = 0;
  144.                             click->newFlip          = 0;
  145.                             click->grabber          = 7;
  146.                             hhndl = hndl;
  147.                         }
  148.                     }
  149.                 }
  150.             }
  151.             return((long)hhndl);
  152.             break;
  153.  
  154.         case GETRGNMESSAGE:
  155.             rgn      = NewRgn();
  156.             accumRgn = (RgnHandle)data;
  157.             if (accumRgn)
  158.                 if (GetHandleSize((Handle)accumRgn) > 10000)
  159.                     return((long)rgn);
  160.             OpenRgn();
  161.             rct = mDerefRRect(hndl)->rect;
  162.             FrameRoundRect(&rct, mDerefRRect(hndl)->width, mDerefRRect(hndl)->height);
  163.             CloseRgn(rgn);
  164.             if (accumRgn)
  165.                 UnionRgn(rgn, accumRgn, accumRgn);
  166.             return((long)rgn);
  167.             break;
  168.  
  169.         case DRAWMESSAGE:
  170.             rct = mDerefRRect(hndl)->rect;
  171.             h   = mDerefCommon(hndl)->penHeight;
  172.             w   = mDerefCommon(hndl)->penWidth;
  173.             PenSize(w, h);
  174.             w = mDerefRRect(hndl)->width;
  175.             h = mDerefRRect(hndl)->height;
  176.             switch (data) {
  177.                 case DRAWOBJ:
  178.                     if (gQDVersion)
  179.                         GetForeColor(&rgb);
  180.                     ForeColor(whiteColor);
  181.                     if (gQDVersion) {
  182.                         rgb2 = mDerefRRect(hndl)->contentColor;
  183.                         RGBForeColor(&rgb2);
  184.                     }
  185.                     PaintRoundRect(&rct, w, h);
  186.                     ForeColor(blackColor);
  187.                     if (gQDVersion) {
  188.                         rgb2 = mDerefRRect(hndl)->borderColor;
  189.                         RGBForeColor(&rgb2);
  190.                     }
  191.                     FrameRoundRect(&rct, w, h);
  192.                     if (gQDVersion)
  193.                         RGBForeColor(&rgb);
  194.                     break;
  195.                 case ERASEOBJ:
  196.                     EraseRoundRect(&rct, w, h);
  197.                     break;
  198.                 case DRAWSELECT:
  199.                     TRectObj(hndl, message, data);
  200.                     if (mDerefCommon(hndl)->selected) {
  201.                         grabber.top  = rct.top  + (h / 2);
  202.                         grabber.left = rct.left + (w / 2);
  203.                         grabber.bottom = (grabber.top  -= 3) + 6;
  204.                         grabber.right  = (grabber.left -= 3) + 6;
  205.                         InvertRect(&grabber);
  206.                     }
  207.                     break;
  208.                 case DRAWGHOST:
  209.                     PenMode(patXor);
  210.                     FrameRoundRect(&rct, w, h);
  211.                     break;
  212.                 case DRAWMASK:
  213.                     FillRoundRect(&rct, w, h, (ConstPatternParam)&qd.black);
  214.                     break;
  215.             }
  216.             PenNormal();
  217.             break;
  218.  
  219.         case PRINTMESSAGE:
  220.             TRRectObj(hndl, DRAWMESSAGE, DRAWOBJ);
  221.             break;
  222.  
  223. #if VH_VERSION
  224.         case VHMESSAGE:
  225.             cptr = ((VHFormatDataPtr)data)->data;
  226.             ccatchr(cptr, 13, 2);
  227.             ccat   (cptr, "$10: TRRectObj:");
  228.             ccatchr(cptr, 13, 1);
  229.             ccat   (cptr, "  $00: selected = ");
  230.             ccatdec(cptr, mDerefRRect(hndl)->selected);
  231.             ccatchr(cptr, 13, 1);
  232.             rct = mDerefRRect(hndl)->rect;
  233.             ccat      (cptr, "  $02: rect     = ($");
  234.             ccatpadhex(cptr, 0, 4, 4, rct.top);
  235.             ccat      (cptr, ",$");
  236.             ccatpadhex(cptr, 0, 4, 4, rct.left);
  237.             ccat      (cptr, ",$");
  238.             ccatpadhex(cptr, 0, 4, 4, rct.bottom);
  239.             ccat      (cptr, ",$");
  240.             ccatpadhex(cptr, 0, 4, 4, rct.right);
  241.             ccat      (cptr, ")");
  242.             ccatchr   (cptr, 13, 1);
  243.             ccat      (cptr, "  $0A: width    = ");
  244.             ccatdec   (cptr, mDerefRRect(hndl)->width);
  245.             ccatchr   (cptr, 13, 1);
  246.             ccat      (cptr, "  $0C: height   = ");
  247.             ccatdec   (cptr, mDerefRRect(hndl)->height);
  248.             return(true);
  249.             break;
  250. #endif
  251.  
  252.         case SIZEMESSAGE:
  253.             click = (ClickInfo *)data;
  254.             if (click->grabber != 7)
  255.                 return(TRectObj(hndl, message, data));
  256.  
  257.             DoTreeObjMethod(hndl, GETOBJRECTMESSAGE, (long)&rct);
  258.             oldw = mDerefRRect(hndl)->width;
  259.             oldh = mDerefRRect(hndl)->height;
  260.  
  261.             GetMouse(&curMouse);
  262.             curMouse.h += click->offset.h;
  263.             curMouse.v += click->offset.v;
  264.             w = (curMouse.h - rct.left) * 2;
  265.             h = (curMouse.v - rct.top) * 2;
  266.             if (w < 10) w = 10;
  267.             if (h < 10) h = 10;
  268.             if (w > (rct.right  - rct.left)) w = rct.right  - rct.left;
  269.             if (h > (rct.bottom - rct.top))  h = rct.bottom - rct.top;
  270.  
  271.             OSEventAvail(nullEvent, &option);
  272.             if (option.modifiers & shiftKey) {
  273.                 if (w > h) w = h;
  274.                 if (h > w) h = w;
  275.             }
  276.             if ((w != oldw) || (h != oldh)) {
  277.                 mDerefRRect(hndl)->width  = w;
  278.                 mDerefRRect(hndl)->height = h;
  279.                 return(true);
  280.             }
  281.             return(false);
  282.             break;
  283.  
  284.         default:
  285.             break;
  286.     }
  287.  
  288.     return(noErr);
  289. }
  290.  
  291.  
  292.  
  293.