home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / Sources / FWRgnShp.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  11.4 KB  |  405 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWRgnShp.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWRGNSHP_H
  13. #include "FWRgnShp.h"
  14. #endif
  15.  
  16. #ifndef FWGRGLOB_H
  17. #include "FWGrGlob.h"
  18. #endif
  19.  
  20. #ifndef FWGC_H
  21. #include "FWGC.h"
  22. #endif
  23.  
  24. #ifndef FWGRUTIL_H
  25. #include "FWGrUtil.h"
  26. #endif
  27.  
  28. #ifndef FWFXMATH_H
  29. #include "FWFxMath.h"
  30. #endif
  31.  
  32. #ifndef FWRASTER_H
  33. #include "FWRaster.h"
  34. #endif
  35.  
  36. #ifndef FWREGION_H
  37. #include "FWRegion.h"
  38. #endif
  39.  
  40. #ifndef FWODGEOM_H
  41. #include "FWODGeom.h"
  42. #endif
  43.  
  44. // ----- Foundation Includes -----
  45.  
  46. #ifndef FWSTREAM_H
  47. #include "FWStream.h"
  48. #endif
  49.  
  50. // ----- OpenDoc Includes -----
  51.  
  52. #ifndef SOM_ODTransform_xh
  53. #include <Trnsform.xh>
  54. #endif
  55.  
  56. #ifndef FW_BUILD_WIN
  57. #ifndef SOM_Module_OpenDoc_Polygon_defined
  58. #include <Polygon.xh>
  59. #endif
  60. #endif
  61.  
  62. //========================================================================================
  63. //    RunTime Info
  64. //========================================================================================
  65.  
  66. #if FW_LIB_EXPORT_PRAGMAS
  67. #pragma lib_export on
  68. #endif
  69.  
  70. #ifdef FW_BUILD_MAC
  71. #pragma segment FWGraphics_RegionShape
  72. #endif
  73.  
  74. FW_DEFINE_CLASS_M1(FW_CRegionShape, FW_CShape)
  75.  
  76. FW_REGISTER_ARCHIVABLE_CLASS(FW_LRegionShape, FW_CRegionShape, FW_CRegionShape::Read, FW_CShape::Write)
  77.  
  78. //========================================================================================
  79. //    class FW_CRegionShape
  80. //========================================================================================
  81.  
  82. //----------------------------------------------------------------------------------------
  83. //    FW_CRegionShape::FW_CRegionShape
  84. //----------------------------------------------------------------------------------------
  85.  
  86. FW_CRegionShape::FW_CRegionShape(ODShape* odShape,
  87.                                  FW_ERenderVerbs renderVerb,
  88.                                  const FW_PInk& ink,
  89.                                  const FW_PStyle& style) :
  90.     FW_CShape(renderVerb, ink, style, FW_kNormalFont),
  91.     fODShape(odShape)
  92. {
  93.     FW_END_CONSTRUCTOR
  94. }
  95.  
  96. //----------------------------------------------------------------------------------------
  97. //    FW_CRegionShape::FW_CRegionShape
  98. //----------------------------------------------------------------------------------------
  99.  
  100. FW_CRegionShape::FW_CRegionShape(const FW_CRegionShape& other) :
  101.     FW_CShape(other),
  102.     fODShape(NULL)
  103. {
  104.     if (other.fODShape)
  105.         fODShape = other.fODShape->Copy(somGetGlobalEnvironment());        // [HLX] for now
  106.         
  107.     FW_END_CONSTRUCTOR
  108. }
  109.  
  110. //----------------------------------------------------------------------------------------
  111. //    FW_CRegionShape::FW_CRegionShape
  112. //----------------------------------------------------------------------------------------
  113.  
  114. FW_CRegionShape::FW_CRegionShape(FW_CReadableStream& archive) :
  115.     FW_CShape(archive),
  116.     fODShape(NULL)
  117. {
  118. #ifndef FW_BUILD_WIN
  119.     unsigned long dataSize;
  120.     archive >> dataSize;
  121.     ODPolygon* polygon = (ODPolygon*)::operator new(dataSize);
  122.     archive.Read((char*)polygon, dataSize);
  123.     
  124.     Environment* ev = somGetGlobalEnvironment();    // [HLX] for now
  125.     // [B1 Conversion] Should diseapear when region shape doesn't use ODShape
  126.     fODShape = new ODShape;
  127.     fODShape->SetPolygon(ev, polygon);
  128. #else
  129.     FW_DEBUG_MESSAGE("Not Yet Implemented");
  130. #endif
  131.  
  132.     FW_END_CONSTRUCTOR
  133. }
  134.  
  135. //----------------------------------------------------------------------------------------
  136. //    FW_CRegionShape::~FW_CRegionShape
  137. //----------------------------------------------------------------------------------------
  138.  
  139. FW_CRegionShape::~FW_CRegionShape()
  140. {
  141.     FW_START_DESTRUCTOR
  142.     Environment* ev = somGetGlobalEnvironment();    // [HLX] for now
  143.     DisposeODShape(ev);
  144. }
  145.  
  146. //----------------------------------------------------------------------------------------
  147. //    FW_CRegionShape::operator=
  148. //----------------------------------------------------------------------------------------
  149.  
  150. FW_CRegionShape& FW_CRegionShape::operator=(const FW_CRegionShape& other)
  151. {
  152.     if (this != &other)
  153.     {
  154.         FW_CShape::operator=(other);
  155.         fODShape = other.fODShape->Copy(somGetGlobalEnvironment());
  156.     }
  157.     
  158.     return *this;
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. //    FW_CRegionShape::Render
  163. //----------------------------------------------------------------------------------------
  164.  
  165. void FW_CRegionShape::Render(FW_CGraphicContext& gc) const
  166. {
  167.     if (fODShape == NULL)
  168.         return;
  169.  
  170.     gc.GetRasterizer()->RenderRegion(gc,
  171.                                     fODShape,
  172.                                     GetRenderVerb(),
  173.                                     fInk,
  174.                                     fStyle);
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. //    FW_CRegionShape::Render
  179. //----------------------------------------------------------------------------------------
  180.  
  181. void FW_CRegionShape::RenderRegion(FW_CGraphicContext& gc,
  182.                                     ODShape* odShape, 
  183.                                     FW_ERenderVerbs renderVerb, 
  184.                                     const FW_PInk& ink,
  185.                                     const FW_PStyle& style)
  186. {
  187.     FW_ASSERT(odShape != NULL);
  188.     
  189.     gc.GetRasterizer()->RenderRegion(gc,
  190.                                     odShape,
  191.                                     renderVerb,
  192.                                     ink,
  193.                                     style);
  194. }
  195.  
  196. //----------------------------------------------------------------------------------------
  197. //    FW_CRegionShape::AdoptODShape
  198. //----------------------------------------------------------------------------------------
  199.  
  200. void FW_CRegionShape::AdoptODShape(ODShape* odShape)
  201. {
  202.     FW_ASSERT(odShape != NULL);
  203.     
  204.     Environment* ev = somGetGlobalEnvironment();    // [HLX] for now
  205.     DisposeODShape(ev);
  206.     fODShape = odShape;    
  207. }
  208.  
  209. //----------------------------------------------------------------------------------------
  210. //    FW_CRegionShape::OrphanODShape
  211. //----------------------------------------------------------------------------------------
  212.  
  213. ODShape* FW_CRegionShape::OrphanODShape()
  214. {
  215.     ODShape* odTempShape = fODShape;
  216.     fODShape = NULL;
  217.     return odTempShape;
  218. }
  219.  
  220. //----------------------------------------------------------------------------------------
  221. //    FW_CRegionShape::SetGeometry
  222. //----------------------------------------------------------------------------------------
  223.  
  224. void FW_CRegionShape::SetGeometry(ODShape* odShape)
  225. {
  226.     Environment* ev = somGetGlobalEnvironment();    // [HLX] for now
  227.     DisposeODShape(ev);
  228.     fODShape = odShape->Copy(ev);
  229. }
  230.  
  231. //----------------------------------------------------------------------------------------
  232. //    FW_CRegionShape::DisposeODShape
  233. //----------------------------------------------------------------------------------------
  234.  
  235. void FW_CRegionShape::DisposeODShape(Environment* ev)
  236. {
  237.     fODShape->Release(ev);
  238.     fODShape = NULL;
  239. }
  240.  
  241. //----------------------------------------------------------------------------------------
  242. //    FW_CRegionShape::HitTest
  243. //----------------------------------------------------------------------------------------
  244.  
  245. FW_Boolean FW_CRegionShape::HitTest(FW_CGraphicContext& gc,
  246.                                     const FW_CPoint& test,
  247.                                     FW_CFixed tolerance) const
  248. {
  249.     if (fODShape == NULL)
  250.         return FALSE;
  251.  
  252.     if(fRenderVerb == FW_kNoRendering)
  253.         return FALSE;
  254.  
  255.     Environment* ev = gc.GetEnvironment();
  256.  
  257.     ODRgnHandle rgn = ::FW_GetShapeRegion(ev, fODShape);
  258.  
  259.     FW_CRect testRect(test.x - tolerance, test.y - tolerance,
  260.                       test.x + tolerance, test.y + tolerance);
  261.     
  262.     if (!::FW_RectInRegion(rgn, testRect))
  263.         return FALSE;
  264.  
  265.     if (fRenderVerb == FW_kFrame)
  266.     {
  267.         ODShape* insideShape = fODShape->Copy(ev);
  268.         ::FW_OutlineODShape(ev, insideShape, GetPenSize());
  269.  
  270.         ODRgnHandle insideRgn = ::FW_GetShapeRegion(ev, insideShape);
  271.         
  272.         FW_Boolean isInside = ::FW_RectInRegion(insideRgn, testRect);
  273.  
  274.         insideShape->Release(ev);
  275.         
  276.         return !isInside;
  277.     }
  278.     
  279.     return TRUE;
  280. }
  281.  
  282. //----------------------------------------------------------------------------------------
  283. //    FW_CRegionShape::Copy
  284. //----------------------------------------------------------------------------------------
  285.  
  286. FW_CShape* FW_CRegionShape::Copy() const
  287. {
  288.     return FW_NEW(FW_CRegionShape, (*this));
  289. }
  290.  
  291. //----------------------------------------------------------------------------------------
  292. //    FW_CRegionShape::Flatten
  293. //----------------------------------------------------------------------------------------
  294.  
  295. void FW_CRegionShape::Flatten(FW_CWritableStream& archive) const
  296. {
  297.     if (fODShape == NULL)
  298.         return;
  299.  
  300.     FW_DEBUG_MESSAGE("Not Yet Implemented");
  301.  
  302. /*
  303. #ifndef FW_BUILD_WIN
  304.     ODPolygon* polygon = fODShape->CopyPolygon(somGetGlobalEnvironment());
  305.     
  306.     unsigned long dataSize = sizeof(ODSLong) + polygon->nContours * sizeof(ODSLong);
  307.     x
  308.     const ODContour *c = &polygon->firstContour;
  309.     for( ODSLong i = polygon->nContours; i>0; i-- ) {
  310.         dataSize += c->nVertices * sizeof(ODPoint);
  311.         c = (ODContour*)&c->vertex[c->nVertices];    // give the next contour
  312.     }
  313.  
  314.     archive << dataSize;
  315.     archive.Write((char*)polygon, dataSize);
  316.     
  317.     delete polygon;    // [HLX] can I delete that
  318. #else    
  319. FW_UNUSED(archive);
  320.     FW_DEBUG_MESSAGE("Not Yet Implemented");
  321. #endif
  322. */
  323. }
  324.  
  325. //----------------------------------------------------------------------------------------
  326. //    FW_CRegionShape::Read
  327. //----------------------------------------------------------------------------------------
  328.  
  329. void* FW_CRegionShape::Read(FW_CReadableStream& archive)
  330. {
  331.     return FW_NEW(FW_CRegionShape, (archive));
  332. }
  333.  
  334. //----------------------------------------------------------------------------------------
  335. //    FW_CRegionShape::GetBounds
  336. //----------------------------------------------------------------------------------------
  337.  
  338. void FW_CRegionShape::GetBounds(FW_CGraphicContext& gc, FW_CRect& rect) const
  339. {
  340. FW_UNUSED(gc);
  341.  
  342.     fODShape->GetBoundingBox(gc.GetEnvironment(), (ODRect*) &rect);
  343. }
  344.  
  345. //----------------------------------------------------------------------------------------
  346. //    FW_CRegionShape::Transform
  347. //----------------------------------------------------------------------------------------
  348.  
  349. void FW_CRegionShape::Transform(Environment* ev, ODTransform* odTransform)
  350. {
  351.     fODShape->Transform(ev, odTransform);
  352. }
  353.  
  354. //----------------------------------------------------------------------------------------
  355. //    FW_CRegionShape::InverseTransform
  356. //----------------------------------------------------------------------------------------
  357.  
  358. void FW_CRegionShape::InverseTransform(Environment* ev, ODTransform* odTransform)
  359. {
  360.     fODShape->InverseTransform(ev, odTransform);
  361. }
  362.  
  363. //----------------------------------------------------------------------------------------
  364. //    FW_CRegionShape::MoveShape
  365. //----------------------------------------------------------------------------------------
  366.  
  367. void FW_CRegionShape::MoveShape(FW_CFixed deltaX, FW_CFixed deltaY)
  368. {
  369.     Environment* ev = somGetGlobalEnvironment();    // [HLX] for now
  370.         
  371.     ODTransform* odTransform = ::FW_NewODTransform(ev, FW_CPoint(deltaX, deltaY));
  372.     fODShape->Transform(ev, odTransform);
  373.     odTransform->Release(ev);
  374. }
  375.  
  376. //----------------------------------------------------------------------------------------
  377. //    FW_CRegionShape::MoveShapeTo
  378. //----------------------------------------------------------------------------------------
  379.  
  380. void FW_CRegionShape::MoveShapeTo(FW_CFixed x, FW_CFixed y)
  381. {
  382.     Environment* ev = somGetGlobalEnvironment();    // [HLX] for now
  383.  
  384.     FW_CRect box;
  385.     fODShape->GetBoundingBox(ev, (ODRect*) &box);
  386.     MoveShape(x - box.left, y - box.top);
  387. }
  388.  
  389. //----------------------------------------------------------------------------------------
  390. //    FW_CRegionShape::Inset
  391. //----------------------------------------------------------------------------------------
  392.  
  393. void FW_CRegionShape::Inset(FW_CFixed x, FW_CFixed y)
  394. {
  395.     Environment* ev = somGetGlobalEnvironment();    // [HLX] for now
  396.  
  397.     ODRgnHandle plfmRgn = ::FW_CopyRegion(::FW_GetShapeRegion(ev, fODShape));
  398.     
  399.     ::FW_InsetRegion(plfmRgn, x, y);
  400.  
  401.     ::FW_SetShapeRegion(ev, fODShape, plfmRgn);
  402.     ::FW_DisposeRegion(plfmRgn);
  403. }
  404.  
  405.