home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / FWGXUtil.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  9.6 KB  |  356 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWGXUtil.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWGXCFG_H
  13. #include "FWGXCfg.h"
  14. #endif
  15.  
  16. #ifdef FW_SUPPORT_GX
  17.  
  18. #ifndef FWGXUTIL_H
  19. #include "FWGXUtil.h"
  20. #endif
  21.  
  22. #ifndef FWACQUIR_H
  23. #include "FWAcquir.h"
  24. #endif
  25.  
  26. #ifndef FWDEBUG_H
  27. #include "FWDebug.h"
  28. #endif
  29.  
  30. #ifndef FWPOINT_H
  31. #include "FWPoint.h"
  32. #endif
  33.  
  34. #ifndef FWRECT_H
  35. #include "FWRect.h"
  36. #endif
  37.  
  38. // ----- Platform includes
  39.  
  40. #ifndef __GESTALT__
  41. #include <Gestalt.h>
  42. #endif
  43.  
  44. #ifndef __GXGRAPHICS__
  45. #include <GXGraphics.h>
  46. #endif
  47.  
  48. #ifndef __GXPRINTING__
  49. #include <GXPrinting.h>
  50. #endif
  51.  
  52. // ----- OpenDoc includes
  53.  
  54. #ifndef SOM_ODFrame_xh
  55. #include <Frame.xh>
  56. #endif
  57.  
  58. #ifndef SOM_ODFacet_xh
  59. #include <Facet.xh>
  60. #endif
  61.  
  62. #ifndef SOM_ODCanvas_xh
  63. #include <Canvas.xh>
  64. #endif
  65.  
  66. #ifndef SOM_ODShape_xh
  67. #include <Shape.xh>
  68. #endif
  69.  
  70. // ------ Standard includes
  71.  
  72. // #include <string.h>
  73.  
  74. //----------------------------------------------------------------------------------------
  75.  
  76. #ifdef FW_BUILD_MAC
  77. #pragma segment FW_GXUtilities
  78. #endif
  79.  
  80. //----------------------------------------------------------------------------------------
  81. // FW_IsGXInstalled
  82. //----------------------------------------------------------------------------------------
  83.  
  84. FW_Boolean FW_IsGXInstalled()
  85. {
  86.     static FW_Boolean gAlreadyChecked = FALSE, gAnswer = FALSE;
  87.     
  88.     // since this may be called repeatedly, check Gestalt only
  89.     // once and cache the answer.  We'll assume for now that GX
  90.     // doesn't go on an off line during a single session.
  91.     if (!gAlreadyChecked)            // have we already checked Gestalt?        
  92.     {    
  93.         ODSLong    version;
  94.         
  95.         // go and check Gestalt
  96.         gAlreadyChecked    = true;
  97.         gAnswer = (Gestalt(gestaltGraphicsVersion, &version) == noErr);
  98.         
  99.         // To be sure that the code fragment is here
  100.         if (gAnswer)
  101.             gAnswer = ((void*)::GXEnterGraphics != (void*)kUnresolvedCFragSymbolAddress);
  102.     }
  103.         
  104.     return gAnswer;
  105. }
  106.  
  107. //----------------------------------------------------------------------------------------
  108. // FW_GX_Initialize
  109. //----------------------------------------------------------------------------------------
  110.  
  111. void FW_Priv_GX_Initialize()
  112. {
  113.     if (FW_IsGXInstalled())
  114.     {
  115.         ::GXEnterGraphics();
  116.         ::GXInitPrinting();
  117.     }
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. // FW_GX_Terminate
  122. //----------------------------------------------------------------------------------------
  123.  
  124. void FW_Priv_GX_Terminate()
  125. {
  126.     if (FW_IsGXInstalled())
  127.     {
  128.         ::GXExitPrinting();
  129.         ::GXExitGraphics();
  130.     }
  131. }
  132.  
  133. //========================================================================================
  134. // class FW_CGraphicContextGX
  135. //========================================================================================
  136.  
  137. FW_DEFINE_AUTO(FW_CGraphicContextGX)
  138.  
  139. //----------------------------------------------------------------------------------------
  140. // FW_CGraphicContextGX::FW_CGraphicContextGX
  141. //----------------------------------------------------------------------------------------
  142.  
  143. FW_CGraphicContextGX::FW_CGraphicContextGX(
  144.         Environment* ev,
  145.         ODFacet* facet,
  146.         ODShape* invalidShape)
  147. {
  148.     FW_REQUIRE(FW_IsGXInstalled());
  149.  
  150.     ODCanvas* canvas = facet->GetCanvas(ev);
  151.  
  152.     // ----- Get / create the GX objects that we will need
  153.     fGXTransform = ::GXNewTransform();
  154.     fGXViewPort = (gxViewPort) canvas->GetGXViewport(ev); 
  155.  
  156.     // ----- Set up transform mapping
  157.     SetupTransformMapping(ev, facet);
  158.  
  159.     // ----- Set up viewport clipping
  160.     SetupViewPortClipping(ev, facet, invalidShape);
  161.  
  162.     // ----- Set up transform clipping
  163.     SetupTransformClipping(ev, facet);
  164.  
  165.     // ----- Plug the viewport into the frame's transform
  166.     ::GXSetTransformViewPorts(fGXTransform, 1, &fGXViewPort);
  167.  
  168.     FW_END_CONSTRUCTOR
  169. }
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // FW_CGraphicContextGX::~FW_CGraphicContextGX
  173. //----------------------------------------------------------------------------------------
  174.  
  175. FW_CGraphicContextGX::~FW_CGraphicContextGX()
  176. {
  177.     FW_START_DESTRUCTOR
  178.     
  179.     ::GXSetTransformViewPorts(fGXTransform, 0, NULL);
  180.  
  181.     RestoreTransformClipping();
  182.  
  183.     RestoreViewPortClipping();
  184.  
  185.     RestoreTransformMapping();
  186.     
  187.     ::GXDisposeTransform(fGXTransform);
  188. }
  189.  
  190. //----------------------------------------------------------------------------------------
  191. // FW_CGraphicContextGX::SetupTransformMapping
  192. //----------------------------------------------------------------------------------------
  193.  
  194. void FW_CGraphicContextGX::SetupTransformMapping(
  195.         Environment* ev,
  196.         ODFacet* facet)
  197. {
  198.     ODCanvas* canvas = facet->GetCanvas(ev);
  199.     FW_CAcquiredODTransform aqFacetXForm = facet->AcquireFrameTransform(ev, canvas);
  200.  
  201.     aqFacetXForm->GetMatrix(ev, (ODMatrix*) &fMapping);
  202.  
  203.     ::GXGetTransformMapping(fGXTransform, &fOldMapping);
  204.  
  205.     ::MapMapping(&fMapping, &fOldMapping);
  206.     ::GXSetTransformMapping(fGXTransform, &fMapping);
  207. }
  208.  
  209. //----------------------------------------------------------------------------------------
  210. // FW_CGraphicContextGX::RestoreTransformMapping
  211. //----------------------------------------------------------------------------------------
  212.  
  213. void FW_CGraphicContextGX::RestoreTransformMapping()
  214. {
  215.     ::GXSetTransformMapping(fGXTransform, &fOldMapping);
  216. }
  217.  
  218. //----------------------------------------------------------------------------------------
  219. // FW_CGraphicContextGX::SetupTransformClipping
  220. //----------------------------------------------------------------------------------------
  221.  
  222. void FW_CGraphicContextGX::SetupTransformClipping(
  223.         Environment* ev,
  224.         ODFacet* facet)
  225. {
  226.     gxShape transformClip = NULL;
  227.  
  228.     ODFrame* frame = facet->GetFrame(ev);
  229.     ODCanvas* canvas = facet->GetCanvas(ev);
  230.  
  231.     if (canvas->HasPlatformPrintJob(ev, kODQuickDrawGX))
  232.     {
  233.         // Printing
  234.         FW_CPoint extent;
  235.         frame->GetContentExtent(ev, (ODPoint*)&extent);
  236.         
  237.         FW_CRect extentRect(FW_kZeroPoint, extent);
  238.         transformClip = GXNewRectangle((gxRectangle*)&extentRect); 
  239.     }
  240.     else
  241.     {
  242.         // Drawing to the screen
  243.         FW_CAcquiredODShape aqFrameShape = frame->AcquireFrameShape(ev, NULL);
  244.         transformClip = aqFrameShape->GetGXShape(ev);
  245.     }
  246.  
  247.     fTransformClipOld = ::GXGetTransformClip(fGXTransform);
  248.     ::GXSetTransformClip(fGXTransform, transformClip);
  249.  
  250. #ifdef FW_DEBUG
  251.     gxRectangle boundsTransformClip;
  252.     ::GXGetShapeBounds(transformClip, 0, &boundsTransformClip);
  253.  
  254.     gxRectangle boundsTransformClipOld;
  255.     ::GXGetShapeBounds(fTransformClipOld, 0, &boundsTransformClipOld);
  256. #endif
  257.  
  258.     ::GXDisposeShape(transformClip);
  259. }
  260.  
  261. //----------------------------------------------------------------------------------------
  262. // FW_CGraphicContextGX::RestoreTransformClipping
  263. //----------------------------------------------------------------------------------------
  264.  
  265. void FW_CGraphicContextGX::RestoreTransformClipping()
  266. {
  267.     ::GXSetTransformClip(fGXTransform, fTransformClipOld);
  268.     ::GXDisposeShape(fTransformClipOld);
  269. }
  270.  
  271. //----------------------------------------------------------------------------------------
  272. // FW_CGraphicContextGX::SetupViewPortClipping
  273. //----------------------------------------------------------------------------------------
  274.  
  275. void FW_CGraphicContextGX::SetupViewPortClipping(
  276.         Environment* ev,
  277.         ODFacet* facet,
  278.         ODShape* invalidShape)
  279. {
  280.     ODCanvas* canvas = facet->GetCanvas(ev);
  281.     FW_CAcquiredODShape aqFacetClipShape = facet->AcquireAggregateClipShape(ev, canvas);
  282.  
  283. #ifdef FW_DEBUG
  284.     FW_CRect boundsFacetClipShape;
  285.     aqFacetClipShape->GetBoundingBox(ev, (ODRect*) &boundsFacetClipShape);
  286. #endif
  287.  
  288.     // ----- Map the clip shape's copy back to local coordinates
  289.     gxShape    facetGXShape = aqFacetClipShape->GetGXShape(ev);
  290.     gxShape    viewPortClip = ::GXCopyToShape(NULL, facetGXShape);
  291.     ::GXDisposeShape(facetGXShape);
  292.  
  293.     ::GXSetShapeAttributes(viewPortClip, GXGetShapeAttributes(viewPortClip) & ~gxMapTransformShape);
  294. //    ::GXMapShape(viewPortClip, &fMapping);
  295.  
  296.     // ----- Intersect the clip shape with the invalid  shape
  297.     if (invalidShape != NULL)
  298.     {
  299.         gxShape invalidGXShape = invalidShape->GetGXShape(ev);
  300.         ::GXIntersectShape(viewPortClip, invalidGXShape);
  301.         ::GXDisposeShape(invalidGXShape);
  302.     }
  303.     
  304.     //    [HLX] move here because of bug #1306478
  305.     ::GXMapShape(viewPortClip, &fMapping);
  306.  
  307.     // ----- Save the current clip and set the new old 
  308.     fViewPortClipOld = ::GXGetViewPortClip(fGXViewPort);
  309.  
  310. #ifdef FW_DEBUG
  311.     gxRectangle boundsViewPortClip;
  312.     ::GXGetShapeBounds(viewPortClip, 0, &boundsViewPortClip);
  313.  
  314.     gxRectangle boundsViewPortClipOld;
  315.     ::GXGetShapeBounds(fViewPortClipOld, 0, &boundsViewPortClipOld);
  316. #endif
  317.  
  318.     ::GXSetViewPortClip(fGXViewPort, viewPortClip);
  319.     ::GXDisposeShape(viewPortClip);
  320. }
  321.  
  322. //----------------------------------------------------------------------------------------
  323. // FW_CGraphicContextGX::RestoreViewPortClipping
  324. //----------------------------------------------------------------------------------------
  325.  
  326. void FW_CGraphicContextGX::RestoreViewPortClipping()
  327. {
  328.     ::GXSetViewPortClip(fGXViewPort, fViewPortClipOld);
  329.     ::GXDisposeShape(fViewPortClipOld);
  330. }
  331.  
  332. //----------------------------------------------------------------------------------------
  333. // FW_CGraphicContextGX::DrawShape
  334. //----------------------------------------------------------------------------------------
  335.  
  336. void FW_CGraphicContextGX::DrawShape(gxShape shape)
  337. {
  338.     gxTransform oldTransform = ::GXGetShapeTransform(shape);
  339.     ::GXSetShapeTransform(shape, fGXTransform);
  340.     
  341.     ::GXDrawShape(shape);
  342.  
  343.     ::GXSetShapeTransform(shape, oldTransform);
  344. }
  345.  
  346. //----------------------------------------------------------------------------------------
  347. // FW_CGraphicContextGX::GetGXTransform
  348. //----------------------------------------------------------------------------------------
  349.  
  350. gxTransform FW_CGraphicContextGX::GetGXTransform() const
  351. {
  352.     return fGXTransform;
  353. }
  354.  
  355. #endif    // FW_SUPPORT_GX
  356.