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

  1. // @(#) 1.8 com/src/samples/grafpart/iodgrect.cpp, odgrafpart, od96nt 2/3/97 19:26:07 [2/5/97 20:44:57]
  2. /*====START_GENERATED_PROLOG======================================
  3.  */
  4. /*
  5.  *   COMPONENT_NAME: odgrafpart
  6.  *
  7.  *   CLASSES: ContentGrafPartRect
  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:      iodgrect.cpp
  31. //  Contains:  Class implementation for a Rect Object in a 2D-Drawing Container Part
  32. // ************************************************************************
  33.  
  34. #define ContentGrafPartRect_Class_Source
  35.  
  36. #ifndef SOM_Module_GrafRect_Source
  37.     #define SOM_Module_GrafRect_Source
  38. #endif  // SOM_Module_GrafRect_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_ContentGrafPartLine_xh
  81.     #include "iodgline.xh"
  82. #endif // SOM_ContentGrafPartLine_xh
  83.  
  84. #ifndef SOM_ContentGrafPartRect_xih
  85.     #include "iodgrect.xih"
  86. #endif // SOM_ContentGrafPartRect_xih
  87.  
  88.  
  89. // *************************************************************************
  90. //
  91. //  Class implementation:
  92. //      ContentGrafPartRect : ContentGrafPartLine
  93. //
  94. // *************************************************************************
  95.  
  96. // =========================================================================
  97. //
  98. //      Public Override methods: ContentGrafPartRect
  99. //
  100. // =========================================================================
  101.  
  102. // -------------------------------------------------------------------------
  103. // ContentGrafPartRect --> somInit
  104. //
  105. //     Initialization used at registration time
  106. //
  107. //     -> public override method
  108. //     -> calls parent first
  109. // -------------------------------------------------------------------------
  110. SOM_Scope void  SOMLINK ContentGrafPartRectsomInit(ContentGrafPartRect *somSelf )
  111. {
  112.     SQUAWK("> Invoked ContentGrafPartRect::somInit()\n");
  113.  
  114.     /* ContentGrafPartRectData *somThis = ContentGrafPartRectGetData(somSelf); */
  115.     ContentGrafPartRectMethodDebug("ContentGrafPartRect","ContentGrafPartRectsomInit");
  116.  
  117.     // call parent method
  118.     ContentGrafPartRect_parent_ContentGrafPartLine_somInit(somSelf);
  119.  
  120.     // set the objtype
  121.     somSelf->_set_objType(somGetGlobalEnvironment(), COBJ_GRAFPART_RECT);
  122.  
  123.     // set the initial values of the the points for a rect to the origin (0,0;0,0)
  124.     ODPoint pPt = {0,0};
  125.     somSelf->_set_fPt2(somGetGlobalEnvironment(), &pPt);    // set second point to 0,0
  126.  
  127.     SQUAWK("< Leaving ContentGrafPartRect::somInit()\n");
  128.  
  129. }   // somInit()
  130.  
  131.  
  132. // -------------------------------------------------------------------------
  133. //  ContentGrafPartRect --> Draw
  134. //
  135. //      Render a rect on the screen
  136. //
  137. //      -> public override method
  138. //      -> does not call parent
  139. // -------------------------------------------------------------------------
  140. SOM_Scope   ODBoolean   SOMLINK ContentGrafPartRectDraw(ContentGrafPartRect*    somSelf,
  141.                                                         Environment*            ev,
  142.                                                         HDraw                   hDraw )
  143. {
  144.     SQUAWK("> Invoked ContentGrafPartRect::Draw()\n");
  145.  
  146. #ifdef _PLATFORM_OS2_
  147.     // Instance data for this class, in addition to that defined by ContentGrafPartFigr,
  148.     //   only exists (and is used) on the OS/2 platform, so the following function is only
  149.     //   defined in the iodgrect.xih file on the OS/2 platform.
  150.     ContentGrafPartRectData *somThis = ContentGrafPartRectGetData(somSelf);
  151. #endif  // _PLATFORM_OS2_
  152.     ContentGrafPartRectMethodDebug("ContentGrafPartRect","ContentGrafPartRectDraw");
  153.  
  154.     // set local buffers from persistant data
  155.     ODPoint pt1, pt2;               // buffers for the points
  156.     pt1 = somSelf->_get_fPt1(ev);   // get origin from parent figure
  157.     pt2 = somSelf->_get_fPt2(ev);   // get endpoint from local instance data
  158.  
  159. #if defined (_PLATFORM_OS2_)
  160.     SQUAWK("\tContentGrafPartRect::Draw(): Drawing rect object for OS2\n");
  161.     _pPt1.x = ODFixedToInt(pt1.x); _pPt1.y = ODFixedToInt(pt1.y);
  162.     SQUAWK("\tContentGrafPartRect::Draw(): point 1: %lx, %lx\n", pt1.x, pt1.y );
  163.     _pPt2.x = ODFixedToInt(pt2.x); _pPt2.y = ODFixedToInt(pt2.y);
  164.     SQUAWK("\tContentGrafPartRect::Draw(): point 2: %lx, %lx\n", pt2.x, pt2.y );
  165.     GpiSetColor(hDraw, somSelf->_get_fColor(ev));
  166.     GpiMove(hDraw, &_pPt1);
  167.     GpiBox(hDraw, DRO_OUTLINEFILL, &_pPt2, 0, 0);
  168. #elif defined (_PLATFORM_WIN32_)
  169.     SQUAWK("\tContentGrafPartRect::Draw(): Drawing rect object for WIN32\n");
  170.     HBRUSH hbr = CreateSolidBrush( somSelf->_get_fColor(ev) );
  171.     SelectObject( hDraw, hbr);
  172.     HPEN pen = CreatePen(PS_SOLID, 0, somSelf->_get_fColor(ev)) ;
  173.     SelectObject( hDraw, pen) ;
  174.     Rectangle(  hDraw,
  175.         ODFixedToInt(pt1.x), ODFixedToInt(pt1.y),
  176.         ODFixedToInt(pt2.x), ODFixedToInt(pt2.y) );
  177.     DeleteObject(hbr);
  178.     DeleteObject(pen) ;
  179. #endif // _PLATFORM_???_
  180.  
  181.     SQUAWK("< Leaving ContentGrafPartRect::Draw()\n");
  182.     return(kODTrue);
  183.  
  184. }   // ContentGrafPartRect::Draw()
  185.  
  186.  
  187. // -------------------------------------------------------------------------
  188. //  ContentGrafPartRect --> ContainsPoint
  189. //
  190. //      Determines whether a given point is on or near a rect
  191. //
  192. //      -> public override method
  193. //      -> does not call parent
  194. // -------------------------------------------------------------------------
  195. SOM_Scope   ODBoolean   SOMLINK ContentGrafPartRectContainsPoint(   ContentGrafPartRect*    somSelf,
  196.                                                                     Environment*            ev,
  197.                                                                     ODPoint*                point )
  198. {
  199.     SQUAWK("> Invoked ContentGrafPartRect::ContainsPoint()\n");
  200.  
  201.     /* ContentGrafPartRectData *somThis = ContentGrafPartRectGetData(somSelf); */
  202.     ContentGrafPartRectMethodDebug("ContentGrafPartRect","ContentGrafPartRectContainsPoint");
  203.  
  204.     // set local buffers from persistant data
  205.     ODPoint pt1, pt2;               // buffers for the points
  206.     pt1 = somSelf->_get_fPt1(ev);   // get origin from parent figure
  207.     pt2 = somSelf->_get_fPt2(ev);   // get endpoint from local instance data
  208.  
  209.     SQUAWK("\tContentGrafPartRect::ContainsPoint(): received Point: %d,%d\n", ODFixedToInt(point->x), ODFixedToInt(point->y));
  210.     SQUAWK("\tContentGrafPartRect::ContainsPoint(): rectangle extents: %d, %d; %d, %d\n", ODFixedToInt(pt1.x), ODFixedToInt(pt1.y), ODFixedToInt(pt2.x), ODFixedToInt(pt2.y) );
  211.     SQUAWK("\tContentGrafPartRect::ContainsPoint(): X, Y SelectionThresholds: %d, %d\n", kODXSelectionThreshold, kODYSelectionThreshold );
  212.  
  213.     // do all math in double precision
  214.     struct POINTFL
  215.     {
  216.         double x;
  217.         double y;
  218.     }   Dpt1 =  { (double)pt1.x/65536.,     (double)pt1.y/65536.   },
  219.         Dpt2 =  { (double)pt2.x/65536.,     (double)pt2.y/65536.   },
  220.         Dpt3 =  { (double)point->x/65536.,  (double)point->y/65536.};
  221.  
  222.     double Dtx = (double)kODXSelectionThreshold/65536.; // the x selection threshold
  223.     double Dty = (double)kODXSelectionThreshold/65536.; // the y selection threshold
  224.     double Dd;                                          // distance between point and rect
  225.     double dx = (Dpt2.x - Dpt1.x);                      // delta x
  226.     double dy = (Dpt2.y - Dpt1.y);                      // delta y
  227.     double m, intercept_1, intercept_2, x_intersect, y_intersect;
  228.  
  229. #ifdef _PLATFORM_OS2_
  230.     #define TOP(a,b,t) ( max(a,b) + t )
  231.     #define BOT(a,b,t) ( min(a,b) - t )
  232. #else
  233.     #define TOP(a,b,t) ( min(a,b) - t )
  234.     #define BOT(a,b,t) ( max(a,b) + t )
  235. #endif  // _PLATFORM_OS2_
  236.  
  237.     // examine threshold ranges around each line of the rectangle
  238.     if ( ( ( Dpt3.x >= Dpt1.x - Dtx )                   &&  // point in x range of left vertical
  239.            ( Dpt3.x <= Dpt1.x + Dtx )                   &&  //   and
  240.            ( Dpt3.y >= TOP( Dpt1.y, Dpt2.y,   Dty ) )   &&  // point in y range of left vertical
  241.            ( Dpt3.y <= BOT( Dpt1.y, Dpt2.y,   Dty ) ) ) ||  //     or
  242.          ( ( Dpt3.x >= Dpt2.x - Dtx )                   &&  // point in x range of right vertical
  243.            ( Dpt3.x <= Dpt2.x + Dtx )                   &&  //   and
  244.            ( Dpt3.y >= TOP( Dpt1.y, Dpt2.y,   Dty ) )   &&  // point in y range of right vertical
  245.            ( Dpt3.y <= BOT( Dpt1.y, Dpt2.y,   Dty ) ) ) ||  //     or
  246.          ( ( Dpt3.x >= min( Dpt1.x, Dpt2.x) - Dtx )     &&  // point in x range of top horizontal
  247.            ( Dpt3.x <= max( Dpt1.x, Dpt2.x) + Dtx )     &&  //   and
  248.            ( Dpt3.y >= min( Dpt1.y, Dpt2.y) - Dty )     &&  // point in y range of top horizontal
  249.            ( Dpt3.y <= max( Dpt1.y, Dpt2.y) + Dty ) )   ||  //     or
  250.          ( ( Dpt3.x >= min( Dpt1.x, Dpt2.x) - Dtx )     &&  // point in x range of bottom horizontal
  251.            ( Dpt3.x <= max( Dpt1.x, Dpt2.x) + Dtx )     &&  //   and
  252.            ( Dpt3.y >= max( Dpt1.y, Dpt2.y) + Dty )     &&  // point in y range of bottom horizontal
  253.            ( Dpt3.y <= min( Dpt1.y, Dpt2.y) - Dty ) ) )
  254.     {
  255.         SQUAWK("< Leaving ContentGrafPartRect::ContainsPoint(), returning True (edges)\n");
  256.         return(kODTrue);
  257.     }   // if: edges
  258.     else                                                // determine distance of point from rect
  259.     {
  260.         SQUAWK("< Leaving ContentGrafPartRect::ContainsPoint(), returning False\n");
  261.         return(kODFalse);
  262.     }   // else: edges
  263.  
  264. }   // ContentGrafPartRect::ContainsPoint()
  265.  
  266.  
  267. // =========================================================================
  268. // End of file: GrafRect.cpp
  269. // =========================================================================
  270.