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 / FWGC.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  14.2 KB  |  463 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWGC.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 FWGC_H
  13. #include "FWGC.h"
  14. #endif
  15.  
  16. #ifndef FWGRUTIL_H
  17. #include "FWGrUtil.h"
  18. #endif
  19.  
  20. #ifndef FWGRGLOB_H
  21. #include "FWGrGlob.h"
  22. #endif
  23.  
  24. #ifndef FWFXMATH_H
  25. #include "FWFxMath.h"
  26. #endif
  27.  
  28. #ifndef FWREGION_H
  29. #include "FWRegion.h"
  30. #endif
  31.  
  32. #ifndef FWGDEV_H
  33. #include "FWGDev.h"
  34. #endif
  35.  
  36. #ifndef FWRASTER_H
  37. #include "FWRaster.h"
  38. #endif
  39.  
  40. #ifndef FWMAPING_H
  41. #include "FWMaping.h"
  42. #endif
  43.  
  44. #ifndef FWODGEOM_H
  45. #include "FWODGeom.h"
  46. #endif
  47.  
  48. // ----- OpenDoc Includes -----
  49.  
  50. #ifndef SOM_ODTransform_xh
  51. #include <Trnsform.xh>
  52. #endif
  53.  
  54. #ifndef SOM_ODShape_xh
  55. #include <Shape.xh>
  56. #endif
  57.  
  58. #ifndef SOM_ODCanvas_xh
  59. #include <Canvas.xh>
  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 FW_GRTransformContextSegment
  72. #endif
  73.  
  74. //========================================================================================
  75. //    class FW_CGraphicContext
  76. //========================================================================================
  77.  
  78. //----------------------------------------------------------------------------------------
  79. //    FW_CGraphicContext::FW_CGraphicContext
  80. //----------------------------------------------------------------------------------------
  81.  
  82. FW_CGraphicContext::FW_CGraphicContext(Environment* ev) :
  83.     fInitialized(FALSE),
  84.     fPreviousGraphicContext(FW_gLastGC),
  85.     fRasterizer(NULL),
  86.     fDevicePreviousState(NULL),
  87.     fDeviceSuspendState(NULL),
  88.     fEnvironment(ev),
  89.     fTransform(NULL)
  90. {
  91.     FW_END_CONSTRUCTOR
  92. }
  93.  
  94. //----------------------------------------------------------------------------------------
  95. //    FW_CGraphicContext::InitGraphicContext
  96. //
  97. //    Exception Handling Notes
  98. //    Don't forget kids, if we fail during construction then our destructor *won't*
  99. //    get called - i.e. CloseDevice won't be called to match the OpenDevice, etc.
  100. //    We need to protect ourselves from things like SetClip failing (and yes, even stupid 
  101. //     little clip operations *do* fail).
  102. //    We could use helper objects, but it seems easier to use nested try/catch blocks in
  103. //    this case since we'd have no opportunity to reuse the helper classes.
  104. //----------------------------------------------------------------------------------------
  105. //    clipShape is in local coordinate
  106.  
  107. FW_DECLARE_THROW_POINT (FW_CGraphicContext_InitGraphicContext);
  108.  
  109. void FW_CGraphicContext::InitGraphicContext(FW_CGraphicDevice* graphicDevice,
  110.                                             ODTransform* transform,
  111.                                             ODShape* clipShape)
  112. {
  113.     FW_ASSERT(graphicDevice != NULL);
  114.     
  115.     if (fPreviousGraphicContext)
  116.         fPreviousGraphicContext->PrivSuspend();
  117.     
  118.     FW_TRY
  119.     {
  120.         // ----- Save the transform ----
  121.         fTransform = transform;
  122.         if(fTransform != NULL)
  123.         {
  124. #ifdef FW_BUILD_WIN
  125.             fTransform->IncrementRefCount(fEnvironment);
  126. #endif
  127. #ifdef FW_BUILD_MAC
  128.             fTransform->Acquire(fEnvironment);
  129. #endif
  130.         }
  131.         
  132.         FW_TRY
  133.         {
  134.             // ------ Use the default rasterizer -----
  135.             fRasterizer = FW_gRasterizer;
  136.             
  137.             // ----- Set the graphic device -----
  138.             fGraphicDevice = graphicDevice;
  139.             fGraphicDevice->Acquire();
  140.  
  141.             FW_TRY
  142.             {
  143.                 // ----- Open the Device -----
  144.                 fDevicePreviousState = fGraphicDevice->OpenDevice(fEnvironment, this);
  145.                 
  146.                 FW_TRY
  147.                 {
  148.                     FW_CHECK_THROW_POINT (FW_CGraphicContext_InitGraphicContext);
  149.                     
  150.                     // ----- Set the clip -----
  151.                     SetClip(clipShape);
  152.                 }
  153.                 FW_CATCH_BEGIN
  154.                 FW_CATCH_EVERYTHING ()
  155.                 {
  156.                     fGraphicDevice->CloseDevice (fEnvironment, fDevicePreviousState);
  157.                     FW_THROW_SAME ();
  158.                 }
  159.                 FW_CATCH_END
  160.             }
  161.             FW_CATCH_BEGIN
  162.             FW_CATCH_EVERYTHING ()
  163.             {
  164.                 fGraphicDevice->Release ();
  165.                 
  166.                 FW_THROW_SAME ();
  167.             }
  168.             FW_CATCH_END
  169.         }
  170.         FW_CATCH_BEGIN
  171.         FW_CATCH_EVERYTHING ()
  172.         {
  173.             if (fTransform != NULL)
  174.                 fTransform->Release(fEnvironment);
  175.             FW_THROW_SAME ();
  176.         }
  177.         FW_CATCH_END
  178.  
  179.     }
  180.     FW_CATCH_BEGIN
  181.     FW_CATCH_EVERYTHING ()
  182.     {
  183.         if (fPreviousGraphicContext)
  184.             fPreviousGraphicContext->PrivResume();
  185.         FW_THROW_SAME ();
  186.     }
  187.     FW_CATCH_END
  188.  
  189.     FW_gLastGC = this;
  190.     fInitialized = TRUE;
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. //    FW_CGraphicContext::TerminateGraphicContext
  195. //----------------------------------------------------------------------------------------
  196.  
  197. void FW_CGraphicContext::TerminateGraphicContext()
  198. {
  199.     if(fInitialized)
  200.     {
  201.         // ----- Close the Device -----
  202.         if (fDevicePreviousState)
  203.             fGraphicDevice->CloseDevice(fEnvironment, fDevicePreviousState);
  204.     
  205.         // ----- Now we can release the device -----
  206.         fGraphicDevice->Release();
  207.         fGraphicDevice = NULL;
  208.         
  209.         // ----- and the transform -----
  210.         if(fTransform != NULL)
  211.             fTransform->Release(fEnvironment);
  212.             
  213.         // ----- Reset the last Graphic Context Global -----
  214.         FW_gLastGC = fPreviousGraphicContext;
  215.         
  216.         if (fPreviousGraphicContext)
  217.             fPreviousGraphicContext->PrivResume();
  218.             
  219.         fInitialized = FALSE;
  220.     }
  221. }
  222.  
  223. //----------------------------------------------------------------------------------------
  224. //    FW_CGraphicContext::~FW_CGraphicContext
  225. //----------------------------------------------------------------------------------------
  226.  
  227. FW_CGraphicContext::~FW_CGraphicContext()
  228. {
  229.     FW_START_DESTRUCTOR
  230.  
  231.     TerminateGraphicContext();
  232. }
  233.  
  234. //----------------------------------------------------------------------------------------
  235. //    FW_CGraphicContext::PrivSuspend
  236. //----------------------------------------------------------------------------------------
  237.  
  238. ODPlatformCanvas FW_CGraphicContext::GetPlatformCanvas() const
  239. {
  240.     return fGraphicDevice->GetPlatformCanvas();
  241. }
  242.  
  243. //----------------------------------------------------------------------------------------
  244. //    FW_CGraphicContext::PrivSuspend
  245. //----------------------------------------------------------------------------------------
  246.  
  247. void FW_CGraphicContext::PrivSuspend()
  248. {
  249.     fDeviceSuspendState = fGraphicDevice->Suspend();
  250. }
  251.  
  252. //----------------------------------------------------------------------------------------
  253. //    FW_CGraphicContext::PrivResume
  254. //----------------------------------------------------------------------------------------
  255.  
  256. void FW_CGraphicContext::PrivResume()
  257. {
  258.     fGraphicDevice->Resume(fDeviceSuspendState);
  259.     fDeviceSuspendState = NULL;
  260. }
  261.  
  262. //----------------------------------------------------------------------------------------
  263. //    FW_CGraphicContext::SetMapping
  264. //----------------------------------------------------------------------------------------
  265.  
  266. void FW_CGraphicContext::SetMapping(const FW_CMapping& newMapping)
  267. {
  268.     fMapping = newMapping;
  269.     fGraphicDevice->MappingChanged();
  270. }
  271.  
  272. //----------------------------------------------------------------------------------------
  273. //    FW_CGraphicContext::GetMapping
  274. //----------------------------------------------------------------------------------------
  275. void FW_CGraphicContext::GetMapping(FW_CMapping& mapping) const
  276. {
  277.     mapping = fMapping;
  278. }
  279.  
  280. //----------------------------------------------------------------------------------------
  281. //    FW_CGraphicContext::GetClip
  282. //----------------------------------------------------------------------------------------
  283.  
  284. ODShape* FW_CGraphicContext::GetClip() const
  285. {
  286.     ODRgnHandle clipRegion = fGraphicDevice->GetClip();
  287.     ODShape* clipShape = ::FW_NewODShape(fEnvironment, clipRegion);
  288.     ODShape* clipShapeCopy = DeviceToLogical(clipShape);
  289.     clipShape->Release(fEnvironment);        // Disposes the region, too
  290.     return clipShapeCopy;
  291. }
  292.  
  293. //----------------------------------------------------------------------------------------
  294. //    FW_CGraphicContext::SetClip
  295. //----------------------------------------------------------------------------------------
  296. //    a copy of clipShape is used
  297.  
  298. void FW_CGraphicContext::SetClip(ODShape* clipShape)
  299. {
  300.     FW_ASSERT(clipShape != NULL);
  301.     
  302.     ODShape* clipShapeCopy = LogicalToDevice(clipShape);
  303.         // fMapping.ContentToDevice(fEnvironment, clipShape, fGraphicDevice, fTransform);
  304.     ODRgnHandle clipRegion = ::FW_GetShapeRegion(fEnvironment, clipShapeCopy);
  305.     fGraphicDevice->SetClip(clipRegion);    // Uses a copy
  306.     clipShapeCopy->Release(fEnvironment);    // Disposes the region, too
  307. }
  308.  
  309. //----------------------------------------------------------------------------------------
  310. //    FW_CGraphicContext::GetClipRect
  311. //----------------------------------------------------------------------------------------
  312.  
  313. void FW_CGraphicContext::GetClipRect(FW_CRect& clipRect) const
  314. {
  315.     FW_SPlatformRect plfmClipRect;
  316.     fGraphicDevice->GetClipRect(plfmClipRect);
  317.     clipRect = DeviceToLogical(plfmClipRect);
  318. }
  319.  
  320. //----------------------------------------------------------------------------------------
  321. //    FW_CGraphicContext::SetClipRect
  322. //----------------------------------------------------------------------------------------
  323.  
  324. void FW_CGraphicContext::SetClipRect(const FW_CRect& clipRect)
  325. {
  326.     FW_SPlatformRect plfmClipRect = LogicalToDevice(clipRect);    
  327.     fGraphicDevice->SetClipRect(plfmClipRect);
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. //    FW_CGraphicContext::SetRasterizer
  332. //----------------------------------------------------------------------------------------
  333.  
  334. void FW_CGraphicContext::SetRasterizer(FW_CRasterizer* rasterizer)
  335. {
  336.     fRasterizer = rasterizer;
  337. }
  338.  
  339. //----------------------------------------------------------------------------------------
  340. //    FW_CGraphicContext::LogicalToDevice
  341. //----------------------------------------------------------------------------------------
  342.  
  343. FW_SPlatformPoint FW_CGraphicContext::LogicalToDevice(FW_CFixed xSize, FW_CFixed ySize) const
  344. {
  345.     FW_CRect rect(FW_IntToFixed(0), FW_IntToFixed(0), xSize, ySize);
  346.     FW_SPlatformRect plfmRect = LogicalToDevice(rect);
  347.     return FW_SPlatformPoint(plfmRect.right - plfmRect.left, plfmRect.bottom - plfmRect.top);
  348. }
  349.  
  350. //----------------------------------------------------------------------------------------
  351. //    FW_CGraphicContext::LogicalToDevice
  352. //----------------------------------------------------------------------------------------
  353.  
  354. FW_SPlatformPoint FW_CGraphicContext::LogicalToDevice(const FW_CPoint& point) const
  355. {
  356.     FW_SPlatformPoint plfmPoint;
  357.     fMapping.LogicalToDevice(fEnvironment, point, plfmPoint, fGraphicDevice, fTransform);
  358.     return plfmPoint;
  359. }
  360.  
  361. //----------------------------------------------------------------------------------------
  362. //    FW_CGraphicContext::LogicalToDevice
  363. //----------------------------------------------------------------------------------------
  364.  
  365. FW_SPlatformRect FW_CGraphicContext::LogicalToDevice(const FW_CRect& rect) const
  366. {
  367.     FW_SPlatformRect plfmRect;
  368.     fMapping.LogicalToDevice(fEnvironment, rect, plfmRect, fGraphicDevice, fTransform);
  369.     return plfmRect;
  370. }
  371.  
  372. //----------------------------------------------------------------------------------------
  373. //    FW_CGraphicContext::LogicalToDevice
  374. //----------------------------------------------------------------------------------------
  375.  
  376. ODShape* FW_CGraphicContext::LogicalToDevice(ODShape* shape) const
  377. {
  378.     return fMapping.LogicalToDevice(fEnvironment, shape, fGraphicDevice, fTransform);
  379. }
  380.  
  381. //----------------------------------------------------------------------------------------
  382. //    FW_CGraphicContext::DeviceToLogical
  383. //----------------------------------------------------------------------------------------
  384.  
  385. FW_CPoint FW_CGraphicContext::DeviceToLogical(short xSize, short ySize) const
  386. {
  387.     FW_SPlatformRect plfmRect(0, 0, xSize, ySize);
  388.     FW_CRect rect = DeviceToLogical(plfmRect);
  389.     return FW_CPoint(rect.right - rect.left, rect.bottom - rect.top);
  390. }
  391.  
  392. //----------------------------------------------------------------------------------------
  393. //    FW_CGraphicContext::DeviceToLogical
  394. //----------------------------------------------------------------------------------------
  395.  
  396. FW_CPoint FW_CGraphicContext::DeviceToLogical(const FW_SPlatformPoint& point) const
  397. {
  398.     FW_CPoint pt;
  399.     fMapping.DeviceToLogical(fEnvironment, point, pt, fGraphicDevice, fTransform);
  400.     return pt;
  401. }
  402.  
  403. //----------------------------------------------------------------------------------------
  404. //    FW_CGraphicContext::DeviceToLogical
  405. //----------------------------------------------------------------------------------------
  406.  
  407. FW_CRect FW_CGraphicContext::DeviceToLogical(const FW_SPlatformRect& rect) const
  408. {
  409.     FW_CRect odRect;
  410.     fMapping.DeviceToLogical(fEnvironment, rect, odRect, fGraphicDevice, fTransform);
  411.     return odRect;
  412. }
  413.  
  414. //----------------------------------------------------------------------------------------
  415. //    FW_CGraphicContext::DeviceToLogical
  416. //----------------------------------------------------------------------------------------
  417.  
  418. ODShape* FW_CGraphicContext::DeviceToLogical(ODShape* shape) const
  419. {
  420.     return     fMapping.DeviceToLogical(fEnvironment, shape, fGraphicDevice, fTransform);
  421. }
  422.  
  423. //----------------------------------------------------------------------------------------
  424. //    FW_CGraphicContext::GetOriginOffset
  425. //----------------------------------------------------------------------------------------
  426.  
  427. FW_SPlatformPoint FW_CGraphicContext::GetOriginOffset() const
  428. {
  429.     return fMapping.GetOriginOffset(fEnvironment, fGraphicDevice, fTransform);
  430. }
  431.  
  432. //========================================================================================
  433. //    class FW_CSaveRestoreContext
  434. //========================================================================================
  435.  
  436. //----------------------------------------------------------------------------------------
  437. //    FW_CSaveRestoreContext::FW_CSaveRestoreContext
  438. //----------------------------------------------------------------------------------------
  439.  
  440. FW_CSaveRestoreContext::FW_CSaveRestoreContext(FW_CGraphicContext& gc) :
  441.     fGC(gc),
  442.     fSuspendResume(NULL)
  443. {
  444.     FW_ASSERT(&gc == FW_gLastGC);    
  445.  
  446.     fSuspendResume = gc.GetGraphicDevice()->Suspend();
  447.  
  448.     FW_END_CONSTRUCTOR
  449. }
  450.  
  451. //----------------------------------------------------------------------------------------
  452. //    FW_CSaveRestoreContext::FW_CSaveRestoreContext
  453. //----------------------------------------------------------------------------------------
  454.  
  455. FW_CSaveRestoreContext::~FW_CSaveRestoreContext()
  456. {
  457.     FW_START_DESTRUCTOR
  458.     
  459.     // ----- ATTENTION: I don't owned fSuspendResume so I don't delete it -----
  460.     if (fSuspendResume)
  461.         fGC.GetGraphicDevice()->Resume(fSuspendResume);
  462. }
  463.