home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / Sample Code / AppsToGo / DTS.Draw / TLineObj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-24  |  8.3 KB  |  328 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        TLineObj.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 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. static OSErr    LineLayerProc(LayerObj theLayer, short message);
  67.  
  68.  
  69.  
  70. /*****************************************************************************/
  71.  
  72.  
  73.  
  74. #pragma segment DrawObjects
  75. long    TLineObj(TreeObjHndl hndl, short message, long data)
  76. {
  77.     Rect        rct, drct, grabber;
  78.     RgnHandle    rgn, accumRgn;
  79.     short        orn, h, w, i;
  80.     LayerObj    lineLayer;
  81.     Rect        *rptr;
  82.     Boolean        change;
  83.     ClickInfo    *click;
  84.     RGBColor    rgb, oldRgb;
  85. #if VH_VERSION
  86.     char        *cptr;
  87. #endif
  88.  
  89.     switch (message) {
  90.         case INITMESSAGE:
  91.         case FREEMESSAGE:
  92.         case COPYMESSAGE:
  93.         case UNDOMESSAGE:
  94.         case CONVERTMESSAGE:
  95.         case FREADMESSAGE:
  96.         case FWRITEMESSAGE:
  97.         case HREADMESSAGE:
  98.         case HWRITEMESSAGE:
  99.         case HITTESTMESSAGE:
  100.         case SETOBJRECTMESSAGE:
  101.         case SECTOBJRECTMESSAGE:
  102.         case CLICKMESSAGE:
  103.         case KEYMESSAGE:
  104.         case SETSELECTMESSAGE:
  105.         case GETSELECTMESSAGE:
  106.         case COMPAREMESSAGE:
  107.             return(TRectObj(hndl, message, data));
  108.             break;
  109.  
  110.         case GETBBOXMESSAGE:
  111.             TRectObj(hndl, message, data);
  112.             h = (mDerefCommon(hndl)->penHeight / 2) + 1;
  113.             w = (mDerefCommon(hndl)->penWidth  / 2) + 1;
  114.             rptr = (Rect *)data;
  115.             rptr->left   -= w;
  116.             rptr->right  += w;
  117.             rptr->top    -= h;
  118.             rptr->bottom += h;
  119.             break;
  120.  
  121.         case GETOBJRECTMESSAGE:
  122.             TRectObj(hndl, message, data);
  123.             rptr = (Rect *)data;
  124.             if (rptr->left == rptr->right ) rptr->right++;
  125.             if (rptr->top  == rptr->bottom) rptr->bottom++;
  126.             break;
  127.  
  128.         case GETRGNMESSAGE:
  129.             rgn      = NewRgn();
  130.             accumRgn = (RgnHandle)data;
  131.             if (accumRgn)
  132.                 if (GetHandleSize((Handle)accumRgn) > 10000)
  133.                     return((long)rgn);
  134.             NewLayer(&lineLayer, nil, LineLayerProc, nil, 0, (long)hndl);
  135.             if (lineLayer) {
  136.                 SetLayerWorld(lineLayer);
  137.                 TLineObj(hndl, DRAWMESSAGE, DRAWMASK);
  138.                 BitMapToRegion(rgn, (BitMapPtr)(*((CGrafPtr)(*lineLayer)->layerPort)->portPixMap));
  139.                 ResetLayerWorld(lineLayer);
  140.                 DisposeLayer(lineLayer);
  141.                 if (!data)
  142.                     InsetRgn(rgn, -1, -1);
  143.             }
  144.             if (accumRgn)
  145.                 UnionRgn(rgn, accumRgn, accumRgn);
  146.             return((long)rgn);
  147.             break;
  148.  
  149.         case DRAWMESSAGE:
  150.             rct  = mDerefLine(hndl)->rect;
  151.             orn  = mDerefLine(hndl)->flip;
  152.             drct = rct;
  153.             switch (orn) {
  154.                 case 1:
  155.                     drct.top    = rct.bottom;
  156.                     drct.bottom = rct.top;
  157.                     break;
  158.                 case 2:
  159.                     drct.left  = rct.right;
  160.                     drct.right = rct.left;
  161.                     break;
  162.                 case 3:
  163.                     drct.top    = rct.bottom;
  164.                     drct.bottom = rct.top;
  165.                     drct.left   = rct.right;
  166.                     drct.right  = rct.left;
  167.                     break;
  168.             }
  169.             h = mDerefCommon(hndl)->penHeight;
  170.             w = mDerefCommon(hndl)->penWidth;
  171.             PenSize(w, h);
  172.             switch (data) {
  173.                 case DRAWOBJ:
  174.                     if (gQDVersion) {
  175.                         GetForeColor(&oldRgb);
  176.                         rgb = mDerefLine(hndl)->borderColor;
  177.                         RGBForeColor(&rgb);
  178.                     }
  179.                     MoveTo(drct.left - (w / 2), drct.top - (h / 2));
  180.                     LineTo(drct.right - (w / 2), drct.bottom - (h / 2));
  181.                     if (gQDVersion)
  182.                         RGBForeColor(&oldRgb);
  183.                     break;
  184.                 case ERASEOBJ:
  185.                     break;
  186.                 case DRAWSELECT:
  187.                     if (mDerefCommon(hndl)->selected) {
  188.                         for (i = 0; i < 2; ++i) {
  189.                             grabber.top  = i ? drct.top  : drct.bottom;
  190.                             grabber.left = i ? drct.left : drct.right;
  191.                             grabber.bottom = (grabber.top  -= 3) + 6;
  192.                             grabber.right  = (grabber.left -= 3) + 6;
  193.                             InvertRect(&grabber);
  194.                         }
  195.                     }
  196.                     break;
  197.                 case DRAWGHOST:
  198.                     PenMode(patXor);
  199.                     TLineObj(hndl, DRAWMESSAGE, DRAWOBJ);
  200.                     break;
  201.                 case DRAWMASK:
  202.                     ForeColor(blackColor);
  203.                     MoveTo(drct.left - (w / 2), drct.top - (h / 2));
  204.                     LineTo(drct.right - (w / 2), drct.bottom - (h / 2));
  205.                     break;
  206.             }
  207.             PenNormal();
  208.             break;
  209.  
  210.         case PRINTMESSAGE:
  211.             TLineObj(hndl, DRAWMESSAGE, DRAWOBJ);
  212.             break;
  213.  
  214. #if VH_VERSION
  215.         case VHMESSAGE:
  216.             cptr = ((VHFormatDataPtr)data)->data;
  217.             ccatchr(cptr, 13, 2);
  218.             ccat   (cptr, "$10: TRectObj:");
  219.             ccatchr(cptr, 13, 1);
  220.             ccat   (cptr, "  $00: selected     = ");
  221.             ccatdec(cptr, mDerefLine(hndl)->selected);
  222.             ccatchr(cptr, 13, 1);
  223.             rct = mDerefLine(hndl)->rect;
  224.             ccat   (cptr, "  $02: rect         = ($");
  225.             ccathex(cptr, 0, 4, 4, rct.top);
  226.             ccat   (cptr, ",$");
  227.             ccathex(cptr, 0, 4, 4, rct.left);
  228.             ccat   (cptr, ",$");
  229.             ccathex(cptr, 0, 4, 4, rct.bottom);
  230.             ccat   (cptr, ",$");
  231.             ccathex(cptr, 0, 4, 4, rct.right);
  232.             ccat   (cptr, ")");
  233.             ccatchr(cptr, 13, 1);
  234.             ccat   (cptr, "  $0A: penHeight    = ");
  235.             ccatdec(cptr, mDerefLine(hndl)->penHeight);
  236.             ccatchr(cptr, 13, 1);
  237.             ccat   (cptr, "  $0A: penWidth     = ");
  238.             ccatdec(cptr, mDerefLine(hndl)->penWidth);
  239.             ccatchr(cptr, 13, 1);
  240.             ccat   (cptr, "  $0E: borderColor  = ($");
  241.             ccathex(cptr, 0, 4, 4, mDerefLine(hndl)->borderColor.red);
  242.             ccat   (cptr, ",$");
  243.             ccathex(cptr, 0, 4, 4, mDerefLine(hndl)->borderColor.green);
  244.             ccat   (cptr, ",$");
  245.             ccathex(cptr, 0, 4, 4, mDerefLine(hndl)->borderColor.blue);
  246.             ccat   (cptr, ")");
  247.             ccatchr(cptr, 13, 1);
  248.             ccat   (cptr, "  $14: content      = ($");
  249.             ccatdec(cptr, mDerefLine(hndl)->content);
  250.             ccatchr(cptr, 13, 1);
  251.             ccat   (cptr, "  $16: contentColor = ($");
  252.             ccathex(cptr, 0, 4, 4, mDerefLine(hndl)->contentColor.red);
  253.             ccat   (cptr, ",$");
  254.             ccathex(cptr, 0, 4, 4, mDerefLine(hndl)->contentColor.green);
  255.             ccat   (cptr, ",$");
  256.             ccathex(cptr, 0, 4, 4, mDerefLine(hndl)->contentColor.blue);
  257.             ccat   (cptr, ")");
  258.             ccatchr(cptr, 13, 1);
  259.             ccat   (cptr, "  $1A: flip         = ($");
  260.             ccatdec(cptr, mDerefLine(hndl)->flip);
  261.             return(true);
  262.             break;
  263. #endif
  264.  
  265.         case SIZEMESSAGE:
  266.             change = TRectObj(hndl, message, data);
  267.             if (change) {
  268.                 click = (ClickInfo *)data;
  269.                 mDerefLine(hndl)->flip = click->newFlip;
  270.             }
  271.             return(change);
  272.             break;
  273.  
  274.         default:
  275.             break;
  276.     }
  277.  
  278.     return(noErr);
  279. }
  280.  
  281.  
  282.  
  283. /*****************************************************************************/
  284.  
  285.  
  286.  
  287. static OSErr    LineLayerProc(LayerObj theLayer, short message)
  288. {
  289.     OSErr        err;
  290.     TreeObjHndl    line;
  291.     Rect        rct;
  292.     CGrafPtr    keepPort;
  293.     GDHandle    keepGDevice;
  294.     GWorldPtr    layerWorld;
  295.  
  296.     switch (message) {
  297.         case kLayerInit:
  298.             err = noErr;
  299.             if (theLayer) {
  300.                 if (!(*theLayer)->layerPort) {
  301.                     line = (TreeObjHndl)(*theLayer)->layerData;
  302.                     TLineObj(line, GETBBOXMESSAGE, (long)&rct);
  303.                     GetGWorld(&keepPort, &keepGDevice);        /* Keep the GWorld. */
  304.                     err = NewGWorld(&layerWorld, 1, &rct, nil, nil, 0);
  305.                     if (err == noErr) {
  306.                         (*theLayer)->layerOwnsPort = true;
  307.                         SetPort((*theLayer)->layerPort = (GrafPtr)layerWorld);
  308.                         SetOrigin(rct.left, rct.top);
  309.                         EraseRect(&rct);
  310.                     }
  311.                     SetGWorld(keepPort, keepGDevice);        /* Restore the kept GWorld. */
  312.                 }
  313.             }
  314.             else err = paramErr;
  315.             break;
  316.  
  317.         default:
  318.             err = DefaultLayerProc(theLayer, message);
  319.                 /* Default behavior for everything else. */
  320.             break;
  321.     }
  322.  
  323.     return(err);
  324. }
  325.  
  326.  
  327.  
  328.