home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / DTS.Draw / TLineObj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-17  |  8.5 KB  |  331 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.                 if (gQDVersion)
  139.                     BitMapToRegion(rgn, (BitMapPtr)(*((CGrafPtr)(*lineLayer)->layerPort)->portPixMap));
  140.                 else
  141.                     BitMapToRegion(rgn, &(*lineLayer)->layerPort->portBits);
  142.                 ResetLayerWorld(lineLayer);
  143.                 DisposeLayer(lineLayer);
  144.                 if (!data)
  145.                     InsetRgn(rgn, -1, -1);
  146.             }
  147.             if (accumRgn)
  148.                 UnionRgn(rgn, accumRgn, accumRgn);
  149.             return((long)rgn);
  150.             break;
  151.  
  152.         case DRAWMESSAGE:
  153.             rct  = mDerefLine(hndl)->rect;
  154.             orn  = mDerefLine(hndl)->flip;
  155.             drct = rct;
  156.             switch (orn) {
  157.                 case 1:
  158.                     drct.top    = rct.bottom;
  159.                     drct.bottom = rct.top;
  160.                     break;
  161.                 case 2:
  162.                     drct.left  = rct.right;
  163.                     drct.right = rct.left;
  164.                     break;
  165.                 case 3:
  166.                     drct.top    = rct.bottom;
  167.                     drct.bottom = rct.top;
  168.                     drct.left   = rct.right;
  169.                     drct.right  = rct.left;
  170.                     break;
  171.             }
  172.             h = mDerefCommon(hndl)->penHeight;
  173.             w = mDerefCommon(hndl)->penWidth;
  174.             PenSize(w, h);
  175.             switch (data) {
  176.                 case DRAWOBJ:
  177.                     if (gQDVersion) {
  178.                         GetForeColor(&oldRgb);
  179.                         rgb = mDerefLine(hndl)->borderColor;
  180.                         RGBForeColor(&rgb);
  181.                     }
  182.                     MoveTo(drct.left - (w / 2), drct.top - (h / 2));
  183.                     LineTo(drct.right - (w / 2), drct.bottom - (h / 2));
  184.                     if (gQDVersion)
  185.                         RGBForeColor(&oldRgb);
  186.                     break;
  187.                 case ERASEOBJ:
  188.                     break;
  189.                 case DRAWSELECT:
  190.                     if (mDerefCommon(hndl)->selected) {
  191.                         for (i = 0; i < 2; ++i) {
  192.                             grabber.top  = i ? drct.top  : drct.bottom;
  193.                             grabber.left = i ? drct.left : drct.right;
  194.                             grabber.bottom = (grabber.top  -= 3) + 6;
  195.                             grabber.right  = (grabber.left -= 3) + 6;
  196.                             InvertRect(&grabber);
  197.                         }
  198.                     }
  199.                     break;
  200.                 case DRAWGHOST:
  201.                     PenMode(patXor);
  202.                     TLineObj(hndl, DRAWMESSAGE, DRAWOBJ);
  203.                     break;
  204.                 case DRAWMASK:
  205.                     ForeColor(blackColor);
  206.                     MoveTo(drct.left - (w / 2), drct.top - (h / 2));
  207.                     LineTo(drct.right - (w / 2), drct.bottom - (h / 2));
  208.                     break;
  209.             }
  210.             PenNormal();
  211.             break;
  212.  
  213.         case PRINTMESSAGE:
  214.             TLineObj(hndl, DRAWMESSAGE, DRAWOBJ);
  215.             break;
  216.  
  217. #if VH_VERSION
  218.         case VHMESSAGE:
  219.             cptr = ((VHFormatDataPtr)data)->data;
  220.             ccatchr(cptr, 13, 2);
  221.             ccat   (cptr, "$10: TRectObj:");
  222.             ccatchr(cptr, 13, 1);
  223.             ccat   (cptr, "  $00: selected     = ");
  224.             ccatdec(cptr, mDerefLine(hndl)->selected);
  225.             ccatchr(cptr, 13, 1);
  226.             rct = mDerefLine(hndl)->rect;
  227.             ccat      (cptr, "  $02: rect         = ($");
  228.             ccatpadhex(cptr, 0, 4, 4, rct.top);
  229.             ccat      (cptr, ",$");
  230.             ccatpadhex(cptr, 0, 4, 4, rct.left);
  231.             ccat      (cptr, ",$");
  232.             ccatpadhex(cptr, 0, 4, 4, rct.bottom);
  233.             ccat      (cptr, ",$");
  234.             ccatpadhex(cptr, 0, 4, 4, rct.right);
  235.             ccat      (cptr, ")");
  236.             ccatchr   (cptr, 13, 1);
  237.             ccat      (cptr, "  $0A: penHeight    = ");
  238.             ccatdec   (cptr, mDerefLine(hndl)->penHeight);
  239.             ccatchr   (cptr, 13, 1);
  240.             ccat      (cptr, "  $0A: penWidth     = ");
  241.             ccatdec   (cptr, mDerefLine(hndl)->penWidth);
  242.             ccatchr   (cptr, 13, 1);
  243.             ccat      (cptr, "  $0E: borderColor  = ($");
  244.             ccatpadhex(cptr, 0, 4, 4, mDerefLine(hndl)->borderColor.red);
  245.             ccat      (cptr, ",$");
  246.             ccatpadhex(cptr, 0, 4, 4, mDerefLine(hndl)->borderColor.green);
  247.             ccat      (cptr, ",$");
  248.             ccatpadhex(cptr, 0, 4, 4, mDerefLine(hndl)->borderColor.blue);
  249.             ccat      (cptr, ")");
  250.             ccatchr   (cptr, 13, 1);
  251.             ccat      (cptr, "  $14: content      = ($");
  252.             ccatdec   (cptr, mDerefLine(hndl)->content);
  253.             ccatchr   (cptr, 13, 1);
  254.             ccat      (cptr, "  $16: contentColor = ($");
  255.             ccatpadhex(cptr, 0, 4, 4, mDerefLine(hndl)->contentColor.red);
  256.             ccat      (cptr, ",$");
  257.             ccatpadhex(cptr, 0, 4, 4, mDerefLine(hndl)->contentColor.green);
  258.             ccat      (cptr, ",$");
  259.             ccatpadhex(cptr, 0, 4, 4, mDerefLine(hndl)->contentColor.blue);
  260.             ccat      (cptr, ")");
  261.             ccatchr   (cptr, 13, 1);
  262.             ccat      (cptr, "  $1A: flip         = ($");
  263.             ccatdec   (cptr, mDerefLine(hndl)->flip);
  264.             return(true);
  265.             break;
  266. #endif
  267.  
  268.         case SIZEMESSAGE:
  269.             change = TRectObj(hndl, message, data);
  270.             if (change) {
  271.                 click = (ClickInfo *)data;
  272.                 mDerefLine(hndl)->flip = click->newFlip;
  273.             }
  274.             return(change);
  275.             break;
  276.  
  277.         default:
  278.             break;
  279.     }
  280.  
  281.     return(noErr);
  282. }
  283.  
  284.  
  285.  
  286. /*****************************************************************************/
  287.  
  288.  
  289.  
  290. static OSErr    LineLayerProc(LayerObj theLayer, short message)
  291. {
  292.     OSErr        err;
  293.     TreeObjHndl    line;
  294.     Rect        rct;
  295.     CGrafPtr    keepPort;
  296.     GDHandle    keepGDevice;
  297.     GWorldPtr    layerWorld;
  298.  
  299.     switch (message) {
  300.         case kLayerInit:
  301.             err = noErr;
  302.             if (theLayer) {
  303.                 if (!(*theLayer)->layerPort) {
  304.                     line = (TreeObjHndl)(*theLayer)->layerData;
  305.                     TLineObj(line, GETBBOXMESSAGE, (long)&rct);
  306.                     GetGWorld(&keepPort, &keepGDevice);        /* Keep the GWorld. */
  307.                     err = NewGWorld(&layerWorld, 1, &rct, nil, nil, 0);
  308.                     if (err == noErr) {
  309.                         (*theLayer)->layerOwnsPort = true;
  310.                         SetPort((*theLayer)->layerPort = (GrafPtr)layerWorld);
  311.                         SetOrigin(rct.left, rct.top);
  312.                         EraseRect(&rct);
  313.                     }
  314.                     SetGWorld(keepPort, keepGDevice);        /* Restore the kept GWorld. */
  315.                 }
  316.             }
  317.             else err = paramErr;
  318.             break;
  319.  
  320.         default:
  321.             err = DefaultLayerProc(theLayer, message);
  322.                 /* Default behavior for everything else. */
  323.             break;
  324.     }
  325.  
  326.     return(err);
  327. }
  328.  
  329.  
  330.  
  331.