home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opendc12.zip / od124os2.exe / od12osp1.exe / src / grafpart / iodgline.cpp < prev    next >
Text File  |  1997-04-02  |  32KB  |  744 lines

  1. // @(#) 1.26 com/src/samples/grafpart/iodgline.cpp, odgrafpart, od96os2, odos29712d 2/7/97 17:41:48 [3/21/97 17:45:36]
  2. /*====START_GENERATED_PROLOG======================================
  3.  */
  4. /*
  5.  *   COMPONENT_NAME: odgrafpart
  6.  *
  7.  *   CLASSES: ContentGrafPartLine
  8.  *
  9.  *   ORIGINS: 27
  10.  *
  11.  *
  12.  *   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  13.  *   All Rights Reserved
  14.  *   Licensed Materials - Property of IBM
  15.  *   US Government Users Restricted Rights - Use, duplication or
  16.  *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  17.  *
  18.  *   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19.  *   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20.  *   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21.  *   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  22.  *   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  23.  *   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  24.  *   OR PERFORMANCE OF THIS SOFTWARE.
  25.  */
  26. /*====END_GENERATED_PROLOG========================================
  27.  */
  28.  
  29. // *********************************************************************
  30. //  File:      iodgline.cpp
  31. //  Contains:  Class implementation for a Line Object in a 2D-Drawing Container Part
  32. // ************************************************************************
  33.  
  34. #define ContentGrafPartLine_Class_Source
  35.  
  36. #ifndef SOM_Module_GrafLine_Source
  37.     #define SOM_Module_GrafLine_Source
  38. #endif  // SOM_Module_GrafLine_Source
  39.  
  40. // GrafPart's includes
  41. #ifndef _IODGCOMM_CPP_
  42.    #include <iodgcomm.cpp>
  43. #endif   // _IODGCOMM_CPP_
  44.  
  45. // GrafPart's constants & defines
  46. #ifndef _GRAFPART_HPP_
  47.    #include <iodgraph.hpp>
  48. #endif   // _GRAFPART_HPP_
  49.  
  50. // support use of temporary objects (Initialize & Uninitialize)
  51. #ifndef _TEMPOBJ_
  52.     #include <TempObj.h>
  53. #endif // _TEMPOBJ_
  54.  
  55. // support StorageUnit function
  56. #ifndef _STORUTIL_
  57.     #include <StorUtil.h>
  58. #endif  // _STORUTIL_
  59.  
  60. // support math function (ContainsPoint)
  61. #ifndef __math_h
  62.     #include <math.h>
  63. #endif  // __math_h
  64.  
  65. // includes for native drawing functions
  66. #if defined(_PLATFORM_WIN32_)
  67.     #include <windows.h>
  68.     #include <winuser.h>
  69.     #include <wingdi.h>
  70. #elif defined(_PLATFORM_OS2_)
  71.     #include <ODos2.h>
  72. #endif // _PLATFORM_???_
  73.  
  74.  
  75. // local function support includes
  76. #ifndef SOM_GrafPart_xh
  77.     #include "iodgraph.xh"
  78. #endif // SOM_GrafPart_xh
  79.  
  80. #ifndef SOM_ContentGrafPartFigr_xh
  81.     #include "iodgfigr.xh"
  82. #endif // SOM_ContentGrafPartFigr_xh
  83.  
  84. #ifndef SOM_ContentGrafPartLine_xih
  85.     #include "iodgline.xih"
  86. #endif // SOM_ContentGrafPartLine_xih
  87.  
  88.  
  89. // *************************************************************************
  90. //
  91. //  Class implementation:
  92. //      ContentGrafPartLine : ContentGrafPartFigr
  93. //
  94. // *************************************************************************
  95.  
  96. // =========================================================================
  97. //
  98. //      Public Override methods: ContentGrafPartLine
  99. //
  100. // =========================================================================
  101.  
  102. // -------------------------------------------------------------------------
  103. // ContentGrafPartLine --> somInit
  104. //
  105. //     Initialization used at registration time
  106. //
  107. //     -> public override method
  108. //     -> calls parent first
  109. // -------------------------------------------------------------------------
  110. SOM_Scope void  SOMLINK ContentGrafPartLinesomInit(ContentGrafPartLine *somSelf )
  111. {
  112.     SQUAWK("> Invoked ContentGrafPartLine::somInit()\n");
  113.  
  114.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  115.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLinesomInit");
  116.  
  117.     // call parent method
  118.     ContentGrafPartLine_parent_ContentGrafPartFigr_somInit(somSelf);
  119.  
  120.     // set the objtype
  121.     somSelf->_set_objType(somGetGlobalEnvironment(), COBJ_GRAFPART_LINE);
  122.  
  123.     // set the initial values of the the points for a line to the origin (0,0;0,0)
  124.     memset(&_fPt2, 0, sizeof(ODPoint) );            // set second point to 0,0
  125.  
  126.     SQUAWK("< Leaving ContentGrafPartLine::somInit()\n");
  127.  
  128. }   // somInit()
  129.  
  130.  
  131. // -------------------------------------------------------------------------
  132. // ContentGrafPartLine --> somUninit
  133. //
  134. //     Uninitialization used at registration time
  135. //
  136. //     -> public override method
  137. //     -> calls parent only
  138. // -------------------------------------------------------------------------
  139. SOM_Scope void  SOMLINK ContentGrafPartLinesomUninit(ContentGrafPartLine *somSelf)
  140. {
  141.     SQUAWK("> Invoked ContentGrafPartLine::somUninit()\n");
  142.  
  143.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  144.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLinesomUninit");
  145.  
  146.     ContentGrafPartLine_parent_ContentGrafPartFigr_somUninit(somSelf);
  147.  
  148.     SQUAWK("< Leaving ContentGrafPartLine::somUninit()\n");
  149.  
  150. }   // somUninit()
  151.  
  152.  
  153. // -------------------------------------------------------------------------
  154. // ContentGrafPartLine --> MoveBy
  155. //
  156. //      Override ContentObject::MoveBy to update points
  157. //
  158. //      -> public override method
  159. //      -> does not call parent
  160. //      -> the 'offset' is a set of deltas from the origin of the
  161. //          line's bounding rect, expressed as coordinates (not a point)
  162. // -------------------------------------------------------------------------
  163. SOM_Scope void  SOMLINK ContentGrafPartLineMoveBy(  ContentGrafPartLine*    somSelf,
  164.                                                     Environment*            ev,
  165.                                                     ODPoint*                offset )
  166. {
  167.     SQUAWK("> Invoked ContentGrafPartLine::MoveBy(ODPoint*)\n");
  168.     SQUAWK("\tContentGrafPartLine::MoveBy(ODPoint*): received offset: %d, %d\n", ODFixedToInt(offset->x), ODFixedToInt(offset->y) );
  169.  
  170.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  171.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLineMoveBy");
  172.  
  173.     // set local buffers from persistant data
  174.     ODPoint pt1, pt2;   // buffers for the points
  175.     pt1 = somSelf->_get_fPt1(ev);   // get origin from parent figure
  176.     pt2 = _fPt2;                    // get endpoint from local instance data
  177.  
  178. /*
  179.     // calculate deltas relative to origin point
  180.     ODRect bbox = somSelf->_get_boundingRect(ev);               // get the current bounding box to a buffer
  181.     SQUAWK("\tContentGrafPartLine::MoveBy(ODPoint*): boundingRect: %d, %d; %d, %d\n", ODFixedToInt(bbox.left), ODFixedToInt(bbox.top), ODFixedToInt(bbox.right), ODFixedToInt(bbox.bottom) );
  182.  
  183.     ODFixed deltaX = pt1.x - min(pt1.x, pt2.x) + offset->x;     // add the difference between the first
  184.                                                                 //   point and the bbox origin
  185. #ifdef _PLATFORM_OS2_                                           // OS2 origin is LLC
  186.     ODFixed deltaY = pt1.y - max(pt1.y, pt2.y) + offset->y;
  187. #else                                                           // everyone else's origin is ULC
  188.     ODFixed deltaY = pt1.y - min(pt1.x, pt2.x) + offset->y;
  189. #endif  // _PLATFORM_OS2_
  190.     SQUAWK("\tContentGrafPartLine::MoveBy(ODPoint*): calculated deltas are: %d, %d\n", ODFixedToInt(deltaX), ODFixedToInt(deltaY) );
  191.  
  192.     // apply the offset to the current points
  193.     SQUAWK("\tContentGrafPartLine::MoveBy(ODPoint*): coordinates before move: %d, %d; %d, %d\n", ODFixedToInt(_fPt1.x), ODFixedToInt(_fPt1.y), ODFixedToInt(_fPt2.x), ODFixedToInt(_fPt2.y) );
  194.     pt1.x += deltaX;
  195.     pt1.y += deltaY;
  196.     pt2.x += deltaX;
  197.     pt2.y += deltaY;
  198. */
  199.     SQUAWK("\tContentGrafPartLine::MoveBy(ODPoint*): coordinates before move: %d, %d; %d, %d\n", ODFixedToInt(pt1.x), ODFixedToInt(pt1.y), ODFixedToInt(pt2.x), ODFixedToInt(pt2.y) );
  200.  
  201.     // simple-minded move
  202.     pt1.x += offset->x;
  203.     pt1.y += offset->y;
  204.     pt2.x += offset->x;
  205.     pt2.y += offset->y;
  206.  
  207.     SQUAWK("\tContentGrafPartLine::MoveBy(ODPoint*): coordinates  after move: %d, %d; %d, %d\n", ODFixedToInt(pt1.x), ODFixedToInt(pt1.y), ODFixedToInt(pt2.x), ODFixedToInt(pt2.y) );
  208.  
  209.     // update the persistant data from the buffers
  210.     somSelf->_set_fPt1(ev, &pt1);       // set origin in parent figure
  211.     _fPt2.x = pt2.x; _fPt2.y = pt2.y;   // set endpoint in local instance data
  212.  
  213.     // adjust the bounding rect for the new points
  214.     somSelf->AdjustBoundingRect(ev);
  215.  
  216.     SQUAWK("< Leaving ContentGrafPartLine::MoveBy(ODPoint*)\n");
  217.  
  218. }   // ContentGrafPartLine::MoveBy(ODPoint)
  219.  
  220.  
  221. // -------------------------------------------------------------------------
  222. // ContentGrafPartLine --> Resize
  223. //
  224. //      Override ContentObject::Resize to update points
  225. //
  226. //     -> public override method
  227. //     -> does not call parent
  228. // -------------------------------------------------------------------------
  229. SOM_Scope void  SOMLINK ContentGrafPartLineResize(  ContentGrafPartLine*    somSelf,
  230.                                                     Environment*            ev,
  231.                                                     ODPoint*                point,
  232.                                                     ODULong                 flags )
  233. {
  234.     SQUAWK("> Invoked ContentGrafPartLine::Resize()\n");
  235.     SQUAWK("\tContentGrafPartLine::Resize(): received endpoint: %d, %d\n", ODFixedToInt(point->x), ODFixedToInt(point->y) );
  236.  
  237.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  238.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLineResize");
  239.  
  240. #ifdef _PLATFORM_OS2_
  241.     #define TOP(a,b) ( max(a,b) )
  242.     #define BOT(a,b) ( min(a,b) )
  243. #else
  244.     #define TOP(a,b) ( min(a,b) )
  245.     #define BOT(a,b) ( max(a,b) )
  246. #endif  // _PLATFORM_OS2_
  247.  
  248.     // set local buffers from persistant data
  249.     ODPoint pt1, pt2;   // buffers for the points
  250.     pt1 = somSelf->_get_fPt1(ev);   // get origin from parent figure
  251.     pt2 = _fPt2;                    // get endpoint from local instance data
  252.  
  253.     // reset last point to new endpoint (resize is relative to first point)
  254.     SQUAWK("\tContentGrafPartLine::Resize(): coordinates before resize: %d, %d; %d, %d\n", ODFixedToInt(pt1.x), ODFixedToInt(pt1.y), ODFixedToInt(pt2.x), ODFixedToInt(pt2.y) );
  255.  
  256.     if (flags & COBJ_RESIZE_LEFT)
  257.     {
  258.         SQUAWK("\tContentGrafPartLine::Resize(): resizing from Left\n" );
  259.         if (pt1.x < pt2.x)
  260.         {
  261.             SQUAWK("\tContentGrafPartLine::Resize(): resizing point 1\n" );
  262.             pt1.x = point->x;
  263.         }   // if: pt1 on left
  264.         else
  265.         {
  266.             SQUAWK("\tContentGrafPartLine::Resize(): resizing point 2\n" );
  267.             pt2.x = point->x;
  268.         }   // else: pt2 on left
  269.     }   // if: resize from left
  270.     else if (flags & COBJ_RESIZE_RIGHT)
  271.     {
  272.         SQUAWK("\tContentGrafPartLine::Resize(): resizing from Right\n" );
  273.         if (pt1.x > pt2.x)
  274.         {
  275.             SQUAWK("\tContentGrafPartLine::Resize(): resizing point 1\n" );
  276.             pt1.x = point->x;
  277.         }   // if: pt1 on right
  278.         else
  279.         {
  280.             SQUAWK("\tContentGrafPartLine::Resize(): resizing point 2\n" );
  281.             pt2.x = point->x;
  282.         }   // else: pt2 on right
  283.     }   // if: resize from right
  284.  
  285.     if (flags & COBJ_RESIZE_TOP)
  286.     {
  287.         SQUAWK("\tContentGrafPartLine::Resize(): resizing from Top\n" );
  288.         if (pt1.y == TOP(pt1.y, pt2.y) )
  289.         {
  290.             SQUAWK("\tContentGrafPartLine::Resize(): resizing point 1\n" );
  291.             pt1.y = point->y;
  292.         }   // if: pt1 on top
  293.         else
  294.         {
  295.             SQUAWK("\tContentGrafPartLine::Resize(): resizing point 2\n" );
  296.             pt2.y = point->y;
  297.         }   // else: pt2 on top
  298.     }   // if: resize from top
  299.     else if (flags & COBJ_RESIZE_BOTTOM)
  300.     {
  301.         SQUAWK("\tContentGrafPartLine::Resize(): resizing from Bottom\n" );
  302.         if (pt1.y == BOT(pt1.y, pt2.y) )
  303.         {
  304.             SQUAWK("\tContentGrafPartLine::Resize(): resizing point 1\n" );
  305.             pt1.y = point->y;
  306.         }   // if: pt1 on right
  307.         else
  308.         {
  309.             SQUAWK("\tContentGrafPartLine::Resize(): resizing point 2\n" );
  310.             pt2.y = point->y;
  311.         }   // else: pt2 on bottom
  312.     }   // if: resize from bottom
  313.  
  314.     SQUAWK("\tContentGrafPartLine::Resize(): coordinates  after resize: %d, %d; %d, %d\n", ODFixedToInt(pt1.x), ODFixedToInt(pt1.y), ODFixedToInt(pt2.x), ODFixedToInt(pt2.y) );
  315.  
  316.     // update the persistant data from the buffers
  317.     somSelf->_set_fPt1(ev, &pt1);       // set origin in parent figure data
  318.     _fPt2.x = pt2.x; _fPt2.y = pt2.y;   // set endpoint in local instance data
  319.  
  320.     // adjust the bounding rect for the new points
  321.     somSelf->AdjustBoundingRect(ev);
  322.  
  323.     SQUAWK("< Leaving ContentGrafPartLine::Resize()\n");
  324.  
  325. }   // ContentGrafPartLine::Resize()
  326.  
  327.  
  328. // -------------------------------------------------------------------------
  329. // ContentGrafPartLine --> WriteContents
  330. //
  331. //      Override ContentObject::WriteContents to write local data to a StorageUnit
  332. //
  333. //     -> public override method
  334. //     -> calls parent first
  335. // -------------------------------------------------------------------------
  336. SOM_Scope void  SOMLINK ContentGrafPartLineWriteContents(   ContentGrafPartLine*    somSelf,
  337.                                                             Environment*            ev,
  338.                                                             ODStorageUnit*          toSU,
  339.                                                             BCTransferKind          writeKind,
  340.                                                             BCCloneInfo*            cloneInfo )
  341. {
  342.     SQUAWK("> Invoked ContentGrafPartLine::WriteContents()\n");
  343.  
  344.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  345.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLineWriteContents");
  346.  
  347.     // call parent first
  348.     ContentGrafPartLine_parent_ContentGrafPartFigr_WriteContents( somSelf,
  349.                                                             ev,
  350.                                                             toSU,
  351.                                                             writeKind,
  352.                                                             cloneInfo);
  353.  
  354.     // adjust point for containment
  355.     ODPoint fPt;                            // temporary buffer
  356.     if (cloneInfo->refPoint)                // if a reference point was by defined by BC parent
  357.     {
  358.         fPt.x = _fPt2.x - cloneInfo->refPoint->x;
  359.         fPt.y = _fPt2.y - cloneInfo->refPoint->y;
  360.     }   // if: refPoint defined
  361.     else
  362.     {
  363.         fPt.x = _fPt2.x;
  364.         fPt.y = _fPt2.y;
  365.     }   // else: refPoint not defined
  366.  
  367.     // Write the local data
  368.     StorageUnitSetValue(toSU, ev, sizeof(ODPoint), &fPt);       // write the end point
  369.  
  370.     SQUAWK("< Leaving ContentGrafPartLine::WriteContents()\n");
  371.  
  372. }   // ContentGrafPartLine::WriteContents()
  373.  
  374.  
  375. // -------------------------------------------------------------------------
  376. // ContentGrafPartLine --> ReadContents
  377. //
  378. //      Override ContentObject::ReadContents to read local data from a StorageUnit
  379. //
  380. //     -> public override method
  381. //     -> calls parent first
  382. // -------------------------------------------------------------------------
  383. SOM_Scope void  SOMLINK ContentGrafPartLineReadContents(    ContentGrafPartLine*    somSelf,
  384.                                                             Environment*            ev,
  385.                                                             ODStorageUnit*          fromSU,
  386.                                                             BCTransferKind          readKind,
  387.                                                             BCCloneInfo*            cloneInfo )
  388. {
  389.     SQUAWK("> Invoked ContentGrafPartLine::ReadContents()\n");
  390.  
  391.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  392.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLineReadContents");
  393.  
  394.     // call parent first
  395.     ContentGrafPartLine_parent_ContentGrafPartFigr_ReadContents(  somSelf,
  396.                                                             ev,
  397.                                                             fromSU,
  398.                                                             readKind,
  399.                                                             cloneInfo);
  400.     // Read the local data
  401.     StorageUnitGetValue(fromSU, ev, sizeof(ODPoint), &_fPt2);   // read the end point
  402.     if (cloneInfo->refPoint)                                    // adjust point for containment
  403.     {
  404.         _fPt2.x += cloneInfo->refPoint->x;
  405.         _fPt2.y += cloneInfo->refPoint->y;
  406.     }   // if: refPoint defined
  407.  
  408.     SQUAWK("< Leaving ContentGrafPartLine::ReadContents()\n");
  409.  
  410. }   // ContentGrafPartLine::ReadContents()
  411.  
  412.  
  413. // -------------------------------------------------------------------------
  414. // ContentGrafPartLine --> DeleteContents
  415. //
  416. //      Override ContentObject::DeleteContents to wipe out local data
  417. //
  418. //     -> public override method
  419. //     -> calls parent first
  420. // -------------------------------------------------------------------------
  421. SOM_Scope void  SOMLINK ContentGrafPartLineDeleteContents(  ContentGrafPartLine *somSelf,
  422.                                                             Environment         *ev,
  423.                                                             ODULong             type )
  424. {
  425.     SQUAWK("> Invoked ContentGrafPartLine::DeleteContents()\n");
  426.  
  427.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  428.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLineDeleteContents");
  429.  
  430.     // call parent first
  431.     ContentGrafPartLine_parent_ContentGrafPartFigr_DeleteContents(somSelf,
  432.                                                                   ev,
  433.                                                                   type);
  434.  
  435.     // Wipe the local data
  436.     memset(&_fPt2, 0, sizeof(ODPoint));                          // wipe the end point
  437.  
  438.     SQUAWK("< Leaving ContentGrafPartLine::DeleteContents()\n");
  439.  
  440. }   // ContentGrafPartLine::DeleteContents()
  441.  
  442.  
  443. // -------------------------------------------------------------------------
  444. //  ContentGrafPartLine --> Initialize
  445. //
  446. //      Surrogate constructor for line object, calls all necessary initializations
  447. //          because SOM doesn't allow parms to the real constructor (somInit).
  448. //
  449. //      -> public override method
  450. //      -> calls parent first
  451. // -------------------------------------------------------------------------
  452. SOM_Scope   GrafPart*
  453. SOMLINK     ContentGrafPartLineInitialize(  ContentGrafPartLine*    somSelf,
  454.                                             Environment*            ev,
  455.                                             ODFrame*                frame,
  456.                                             ODPoint*                point )
  457. {
  458.     SQUAWK("> Invoked ContentGrafPartLine::Initialize()\n");
  459.  
  460.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  461.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLineInitialize");
  462.  
  463.     TempODPart      tPart = frame->AcquirePart(ev);     // get a temporary wrapper object
  464.     GrafPart*       tGP;                                // declare a buffer for the calling GrafPart
  465.  
  466.     // call parent with first (origin) point
  467.     SQUAWK("\tContentGrafPartLine::Initialize(): calling parent\n");
  468.     tGP = ContentGrafPartLine_parent_ContentGrafPartFigr_Initialize(   somSelf,
  469.                                                                         ev,
  470.                                                                         frame,
  471.                                                                         point );
  472.  
  473.     // set local value for endpoint (second point)
  474.     _fPt2.x = point->x; _fPt2.y = point->y;         // set the end point
  475.     somSelf->AdjustBoundingRect(ev);                // adjust the bounding rectangle to new points
  476.  
  477.     SQUAWK("< Leaving ContentGrafPartLine::Initialize()\n");
  478.     return(tGP);                                    // return the pointer to the calling GrafPart
  479.  
  480. }   // ContentGrafPartLine::Initialize()
  481.  
  482.  
  483. // -------------------------------------------------------------------------
  484. //  ContentGrafPartLine --> Uninitialize
  485. //
  486. //      Surrogate destructor for line object, calls all necessary uninitializations
  487. //          just to encapsulate them, and balance the Initialize.
  488. //
  489. //      -> public override method
  490. //      -> calls parent only
  491. // -------------------------------------------------------------------------
  492. SOM_Scope   void
  493. SOMLINK     ContentGrafPartLineUninitialize(  ContentGrafPartLine*    somSelf,
  494.                                             Environment*            ev)
  495. {
  496.     SQUAWK("> Invoked ContentGrafPartLine::Uninitialize()\n");
  497.  
  498.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  499.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLineUninitialize");
  500.  
  501.     // call parent
  502.     ContentGrafPartLine_parent_ContentGrafPartFigr_Uninitialize( somSelf,
  503.                                                                ev );
  504.  
  505.     SQUAWK("< Leaving ContentGrafPartLine::Uninitialize()\n");
  506.  
  507. }   // ContentGrafPartLine::Uninitialize()
  508.  
  509.  
  510. // -------------------------------------------------------------------------
  511. //  ContentGrafPartLine --> Update
  512. //
  513. //      Surrogate constructor for figr object, calls all necessary initializations
  514. //          because SOM generates the real constructor.
  515. //
  516. //      -> public local method
  517. // -------------------------------------------------------------------------
  518. SOM_Scope   void
  519. SOMLINK     ContentGrafPartLineUpdate(  ContentGrafPartLine*    somSelf,
  520.                                         Environment*            ev,
  521.                                         ODPoint*                point)
  522. {
  523.     SQUAWK("> Invoked ContentGrafPartLine::Update()\n");
  524.  
  525.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  526.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLineUpdate");
  527.  
  528.     // reset the endpoint to the passed point
  529.     somSelf->_set_fPt2(ev, point);                  // set the endpoint
  530.  
  531.     SQUAWK("< Leaving ContentGrafPartLine::Update()\n");
  532.     return;
  533. }   // ContentGrafPartLine::Update()
  534.  
  535.  
  536. // -------------------------------------------------------------------------
  537. //  ContentGrafPartLine --> AdjustBoundingRect
  538. //
  539. //      Adjust a line's bounding rect to fit it's endpoints
  540. //
  541. //      -> public override method
  542. //      -> does not call parent
  543. // -------------------------------------------------------------------------
  544. SOM_Scope   void    SOMLINK ContentGrafPartLineAdjustBoundingRect(  ContentGrafPartLine*    somSelf,
  545.                                                                     Environment*            ev )
  546. {
  547.     SQUAWK("> Invoked ContentGrafPartLine::AdjustBoundingRect()\n");
  548.  
  549.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  550.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLineAdjustBoundingRect");
  551.  
  552.     // set local buffers from persistant data
  553.     ODPoint pt1, pt2;               // buffers for the points
  554.     pt1 = somSelf->_get_fPt1(ev);   // get origin from parent figure
  555.     pt2 = _fPt2;                    // get endpoint from local instance data
  556.  
  557.     // reset the bounding box from the new points, keeping orientation
  558.     ODRect bbox;                                    // declare a buffer for bounding rectangle
  559.  
  560.     // set the buffer points to the new extents
  561. #ifdef _PLATFORM_OS2_                               // OS2 origin is LLC
  562.     bbox.top    = max( pt1.y, pt2.y);
  563.     bbox.bottom = min( pt1.y, pt2.y);
  564. #else                                               // every one else's origin in ULC
  565.     bbox.top    = min( pt1.y, pt2.y);
  566.     bbox.bottom = max( pt1.y, pt2.y);
  567. #endif  // _PLATFORM_OS2_
  568.     bbox.left   = min( pt1.x, pt2.x);
  569.     bbox.right  = max( pt1.x, pt2.x);
  570.  
  571.     somSelf->_set_boundingRect(ev, &bbox);          // set the bounding rectangle from the buffer
  572.  
  573.     SQUAWK("< Leaving ContentGrafPartLine::AdjustBoundingRect()\n");
  574.  
  575. }   // ContentGrafPartLine::AdjustBoundingRect()
  576.  
  577.  
  578. // -------------------------------------------------------------------------
  579. //  ContentGrafPartLine --> Draw
  580. //
  581. //      Render a line on the screen
  582. //
  583. //      -> public override method
  584. //      -> does not call parent
  585. // -------------------------------------------------------------------------
  586. SOM_Scope   ODBoolean   SOMLINK ContentGrafPartLineDraw(ContentGrafPartLine*    somSelf,
  587.                                                         Environment*            ev,
  588.                                                         HDraw                   hDraw )
  589. {
  590.     SQUAWK("> Invoked ContentGrafPartLine::Draw()\n");
  591.  
  592.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  593.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLineDraw");
  594.  
  595.     // set local buffers from persistant data
  596.     ODPoint pt1, pt2;               // buffers for the points
  597.     pt1 = somSelf->_get_fPt1(ev);   // get origin from parent figure
  598.     pt2 = _fPt2;                    // get endpoint from local instance data
  599.  
  600. #if defined (_PLATFORM_OS2_)
  601.     SQUAWK("\tContentGrafPartLine::Draw(): Drawing line object for OS2\n");
  602.     _pPt1.x = ODFixedToInt(pt1.x); _pPt1.y = ODFixedToInt(pt1.y);
  603.     SQUAWK("\tContentGrafPartLine::Draw(): point 1: %lx, %lx\n", pt1.x, pt1.y );
  604.     _pPt2.x = ODFixedToInt(pt2.x); _pPt2.y = ODFixedToInt(pt2.y);
  605.     SQUAWK("\tContentGrafPartLine::Draw(): point 2: %lx, %lx\n", pt2.x, pt2.y );
  606.     GpiSetColor(hDraw, somSelf->_get_fColor(ev));
  607.     GpiMove(hDraw, &_pPt1);
  608.     GpiLine(hDraw, &_pPt2);
  609. #elif defined (_PLATFORM_WIN32_)
  610.     SQUAWK("\tContentGrafPartLine::Draw(): Drawing line object for WIN32\n");
  611.     HPEN pen = CreatePen(PS_SOLID, 0, somSelf->_get_fColor(ev)) ;
  612.     SelectObject(hDraw, pen) ;
  613.     MoveToEx(hDraw, ODFixedToInt(pt1.x), ODFixedToInt(pt1.y), NULL);
  614.     LineTo(  hDraw, ODFixedToInt(pt2.x), ODFixedToInt(pt2.y) );
  615.     DeleteObject(pen) ;
  616. #endif // _PLATFORM_???_
  617.  
  618.     SQUAWK("< Leaving ContentGrafPartLine::Draw()\n");
  619.     return(kODTrue);
  620.  
  621. }   // ContentGrafPartLine::Draw()
  622.  
  623.  
  624. // -------------------------------------------------------------------------
  625. //  ContentGrafPartLine --> ContainsPoint
  626. //
  627. //      Determines whether a given point is on or near a line
  628. //
  629. //      -> public override method
  630. //      -> does not call parent
  631. // -------------------------------------------------------------------------
  632. SOM_Scope   ODBoolean   SOMLINK ContentGrafPartLineContainsPoint(   ContentGrafPartLine*    somSelf,
  633.                                                                     Environment*            ev,
  634.                                                                     ODPoint*                point )
  635. {
  636.     long x1, y1, x2, y2, x3, y3, x4, y4 ;
  637.     long dx, dy, dex, dey, dx2, dy2, dxdy, dt2 ;
  638.  
  639.     SQUAWK("> Invoked ContentGrafPartLine::ContainsPoint()\n");
  640.  
  641.     ContentGrafPartLineData *somThis = ContentGrafPartLineGetData(somSelf);
  642.     ContentGrafPartLineMethodDebug("ContentGrafPartLine","ContentGrafPartLineContainsPoint");
  643.  
  644.     // set local buffers from persistant data
  645.     ODPoint pt1, pt2;               // buffers for the points
  646.     pt1 = somSelf->_get_fPt1(ev);   // get origin from parent figure
  647.     pt2 = _fPt2;                    // get endpoint from local instance data
  648.  
  649.     SQUAWK("\tContentGrafPartLine::ContainsPoint(): received Point: %d,%d\n", ODFixedToInt(point->x), ODFixedToInt(point->y));
  650.     SQUAWK("\tContentGrafPartLine::ContainsPoint(): line endpoints: %d, %d; %d, %d\n", ODFixedToInt(pt1.x), ODFixedToInt(pt1.y), ODFixedToInt(pt2.x), ODFixedToInt(pt2.y) );
  651.     SQUAWK("\tContentGrafPartLine::ContainsPoint(): X, Y SelectionThresholds: %d, %d\n", kODXSelectionThreshold, kODYSelectionThreshold );
  652.  
  653.     x1 = FixedToInt(pt1.x)   ;  y1 = FixedToInt(pt1.y)    ;
  654.     x2 = FixedToInt(pt2.x)   ;  y2 = FixedToInt(pt2.y)    ;
  655.     x3 = FixedToInt(point->x);  y3 = FixedToInt(point->y) ;
  656.  
  657.     dt2 = kODXSelectionThreshold * kODYSelectionThreshold / 4 ;
  658.  
  659.     // First check if within delta of the end points, check square of distance
  660.     // against square of the delta.
  661.  
  662.     // First End check.
  663.     dex = x1 - x3 ;  dey = y1 - y3 ;
  664.     if ( dex * dex + dey * dey < dt2 )
  665.     {
  666.         SQUAWK("< Leaving ContentGrafPartLine::ContainsPoint(), returning True (first point)\n");
  667.         return kODTrue ;
  668.     }
  669.  
  670.     // Second End Check.
  671.     dex = x2 - x3 ;  dey = y2 - y3 ;
  672.     if ( dex * dex + dey * dey < dt2 )
  673.     {
  674.         SQUAWK("< Leaving ContentGrafPartLine::ContainsPoint(), returning True (second point)\n");
  675.         return kODTrue ;
  676.     }
  677.  
  678.     // compute delta X and delta y for the line
  679.     dx  = x1 - x2 ;  dy  = y1 - y2 ;
  680.  
  681.     // if line is vertical, quick answer
  682.     if ( dx == 0 )
  683.     {
  684.         x4 = x1 ;  y4 = y3 ;
  685.     }
  686.  
  687.     // if line is horizontal, quick answer
  688.     if ( dy == 0 )
  689.     {
  690.         x4 = x3 ;  y4 = y1 ;
  691.     }
  692.  
  693.     // Answer is a little more complex if line is oblique.
  694.     // really just a bit of algrbra to find the common solution to the
  695.     // two lines.
  696.     // (x-x2)/(y-y2) = (x1-x2)/(y1.y2)   i.e. the real line
  697.  
  698.     // (x-xy)/(y-y3) = -(y1-y2)/(x1-x2)      and perpendicular line thru x3,y3
  699.  
  700.     if ( dx != 0 && dy != 0)
  701.     {
  702.         dx2 = dx * dx ;  dy2 = dy * dy ;
  703.         dxdy = dx * dy ;
  704.         y4 = ((x3 - x2)*dxdy + y2 * dx2 + y3 * dy2) / (dx2 + dy2) ;
  705.         // using long arithmetic, we can get some truncation errors.  We choose
  706.         // the expression for x4 that minimizes those errors.
  707.         if (dy2 > dx2)
  708.         {
  709.             x4 = (y4 - y2) * dx / dy + x2 ;
  710.         }
  711.         else
  712.         {
  713.             x4 = (y3 - y4) * dy / dx + x3 ;
  714.         }
  715.     }
  716.  
  717.     // x4,y4 is the point where a perpendicular from the point to the line
  718.     // would intercept the line (posibly extended).
  719.  
  720.     // First check if x4,y4 is within the bounding rectangle of the line
  721.     if ((((x1 <= x2) && (x1 <= x4) && (x4 <= x2))  ||   // x1 <= x4 <= x2 OR
  722.          ((x1 >= x2) && (x1 >= x4) && (x4 >= x2))) &&   // x1 >= x4 >= x2  AND
  723.         (((y1 <= y2) && (y1 <= y4) && (y4 <= y2))  ||   // y1 <= y4 <= y2 OR
  724.          ((y1 >= y2) && (y1 >= y4) && (y4 >= y2))))     // t1 >= y4 >= y2
  725.     {
  726.         // then compute length (squared) of line segment from point to x4,y4
  727.         dex = x3 - x4 ;  dey = y3 - y4 ;
  728.         if ( dex * dex + dey * dey < dt2 )
  729.         {
  730.             SQUAWK("< Leaving ContentGrafPartLine::ContainsPoint(), returning True (line proximity)\n");
  731.             return kODTrue ;                // in box and close enough, so return true
  732.         }
  733.     }
  734.  
  735.     SQUAWK("< Leaving ContentGrafPartLine::ContainsPoint(), returning False\n");
  736.     return kODFalse ;                   // not close enough, so return false.
  737.  
  738. }   // ContentGrafPartLine::ContainsPoint()
  739.  
  740.  
  741. // =========================================================================
  742. // End of file: iodgline.cpp
  743. // =========================================================================
  744.