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

  1. //========================================================================================
  2. //
  3. //    File:                FWGXUtil.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1995 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. #if FW_LIB_EXPORT_PRAGMAS
  77. #pragma lib_export on
  78. #endif
  79.  
  80. #ifdef FW_BUILD_MAC
  81. #pragma segment FW_GXUtilities
  82. #endif
  83.  
  84. //----------------------------------------------------------------------------------------
  85. // FW_IsGXInstalled
  86. //----------------------------------------------------------------------------------------
  87.  
  88. FW_FUNC_ATTR     FW_Boolean
  89.                 FW_IsGXInstalled()
  90. {
  91.     ODSLong    version;
  92.     static    FW_Boolean alreadyChecked = FALSE, answer = FALSE;
  93.     
  94.     // since this may be called repeatedly, check Gestalt only
  95.     // once and cache the answer.  We'll assume for now that GX
  96.     // doesn't go on an off line during a single session.
  97.     if (alreadyChecked)                // have we already checked Gestalt?        
  98.     {
  99.         return answer;                // just return the same answer
  100.     }
  101.     
  102.     // go and check Gestalt
  103.     alreadyChecked    = true;
  104.     if (Gestalt(gestaltGraphicsVersion, &version) == noErr)
  105.     {
  106.         answer    = true;
  107.         return answer;
  108.     }
  109.     
  110.     answer    = false;
  111.     return answer;
  112. }
  113.  
  114. //----------------------------------------------------------------------------------------
  115. // FW_GX_Initialize
  116. //----------------------------------------------------------------------------------------
  117.  
  118. void            FW_GX_Initialize()
  119. {
  120.     if (FW_IsGXInstalled())
  121.     {
  122.         ::GXEnterGraphics();
  123.         ::GXInitPrinting();
  124.     }
  125. }
  126.  
  127. //----------------------------------------------------------------------------------------
  128. // FW_GX_Terminate
  129. //----------------------------------------------------------------------------------------
  130.  
  131. void            FW_GX_Terminate()
  132. {
  133.     if (FW_IsGXInstalled())
  134.     {
  135.         ::GXExitPrinting();
  136.         ::GXExitGraphics();
  137.     }
  138. }
  139.  
  140. //========================================================================================
  141. // class FW_CGraphicContextGX
  142. //========================================================================================
  143.  
  144. //----------------------------------------------------------------------------------------
  145. // FW_CGraphicContextGX::FW_CGraphicContextGX
  146. //----------------------------------------------------------------------------------------
  147.  
  148. FW_CGraphicContextGX::FW_CGraphicContextGX(
  149.         Environment* ev,
  150.         ODFacet* facet,
  151.         ODShape* invalidShape)
  152. {
  153.     ODCanvas* canvas = facet->GetCanvas(ev);
  154.  
  155.     gxGraphicsError err, errSticky;
  156.     err = ::GXGetGraphicsError(&errSticky); // +++
  157.  
  158.     // ----- Get / create the GX objects that we will need
  159.     fGXTransform = ::GXNewTransform();
  160.     fGXViewPort = (gxViewPort) canvas->GetGXViewport(ev); 
  161.  
  162.     err = ::GXGetGraphicsError(&errSticky); // +++
  163.  
  164.     // ----- Set up transform mapping
  165.     SetupTransformMapping(ev, facet);
  166.  
  167.     err = ::GXGetGraphicsError(&errSticky); // +++
  168.  
  169.     // ----- Set up viewport clipping
  170.     SetupViewPortClipping(ev, facet, invalidShape);
  171.  
  172.     err = ::GXGetGraphicsError(&errSticky); // +++
  173.  
  174.     // ----- Set up transform clipping
  175.     SetupTransformClipping(ev, facet);
  176.  
  177.     err = ::GXGetGraphicsError(&errSticky); // +++
  178.  
  179.     // ----- Plug the viewport into the frame's transform
  180.     ::GXSetTransformViewPorts(fGXTransform, 1, &fGXViewPort);
  181.     
  182.     err = ::GXGetGraphicsError(&errSticky); // +++
  183.  
  184.     FW_END_CONSTRUCTOR
  185. }
  186.  
  187. //----------------------------------------------------------------------------------------
  188. // FW_CGraphicContextGX::~FW_CGraphicContextGX
  189. //----------------------------------------------------------------------------------------
  190.  
  191. FW_CGraphicContextGX::~FW_CGraphicContextGX()
  192. {
  193.     FW_START_DESTRUCTOR
  194.     
  195.     gxGraphicsError err, errSticky;
  196.     err = ::GXGetGraphicsError(&errSticky); // +++
  197.  
  198.     ::GXSetTransformViewPorts(fGXTransform, 0, NULL);
  199.  
  200.     err = ::GXGetGraphicsError(&errSticky); // +++
  201.  
  202.     RestoreTransformClipping();
  203.  
  204.     err = ::GXGetGraphicsError(&errSticky); // +++
  205.  
  206.     RestoreViewPortClipping();
  207.  
  208.     err = ::GXGetGraphicsError(&errSticky); // +++
  209.  
  210.     RestoreTransformMapping();
  211.  
  212.     err = ::GXGetGraphicsError(&errSticky); // +++
  213.     
  214.     ::GXDisposeTransform(fGXTransform);
  215.  
  216.     err = ::GXGetGraphicsError(&errSticky); // +++
  217. }
  218.  
  219. //----------------------------------------------------------------------------------------
  220. // FW_CGraphicContextGX::SetupTransformMapping
  221. //----------------------------------------------------------------------------------------
  222.  
  223. void FW_CGraphicContextGX::SetupTransformMapping(
  224.         Environment* ev,
  225.         ODFacet* facet)
  226. {
  227.     ODCanvas* canvas = facet->GetCanvas(ev);
  228.     FW_CAcquiredODTransform aqFacetXForm = facet->AcquireFrameTransform(ev, canvas);
  229.  
  230.     aqFacetXForm->GetMatrix(ev, (ODMatrix*) &fMapping);
  231.  
  232.     ::GXGetTransformMapping(fGXTransform, &fOldMapping);
  233.  
  234.     ::MapMapping(&fMapping, &fOldMapping);
  235.     ::GXSetTransformMapping(fGXTransform, &fMapping);
  236. }
  237.  
  238. //----------------------------------------------------------------------------------------
  239. // FW_CGraphicContextGX::RestoreTransformMapping
  240. //----------------------------------------------------------------------------------------
  241.  
  242. void FW_CGraphicContextGX::RestoreTransformMapping()
  243. {
  244.     ::GXSetTransformMapping(fGXTransform, &fOldMapping);
  245. }
  246.  
  247. //----------------------------------------------------------------------------------------
  248. // FW_CGraphicContextGX::SetupTransformClipping
  249. //----------------------------------------------------------------------------------------
  250.  
  251. void FW_CGraphicContextGX::SetupTransformClipping(
  252.         Environment* ev,
  253.         ODFacet* facet)
  254. {
  255.     gxShape transformClip = NULL;
  256.  
  257.     ODFrame* frame = facet->GetFrame(ev);
  258.     ODCanvas* canvas = facet->GetCanvas(ev);
  259.  
  260.     gxGraphicsError err, errSticky;
  261.     err = ::GXGetGraphicsError(&errSticky); // +++
  262.  
  263.     if (canvas->HasPlatformPrintJob(ev, kODQuickDrawGX))
  264.     {
  265.         // Printing
  266.         FW_CPoint extent;
  267.         frame->GetContentExtent(ev, (ODPoint*)&extent);
  268.         
  269.         FW_CRect extentRect(FW_kZeroPoint, extent);
  270.         transformClip = GXNewRectangle((gxRectangle*)&extentRect); 
  271.     }
  272.     else
  273.     {
  274.         // Drawing to the screen
  275.         FW_CAcquiredODShape aqFrameShape = frame->AcquireFrameShape(ev, NULL);
  276.         transformClip = aqFrameShape->GetGXShape(ev);
  277.     }
  278.  
  279.     err = ::GXGetGraphicsError(&errSticky); // +++
  280.  
  281.     fTransformClipOld = ::GXGetTransformClip(fGXTransform);
  282.     ::GXSetTransformClip(fGXTransform, transformClip);
  283.  
  284.     err = ::GXGetGraphicsError(&errSticky); // +++
  285.  
  286. #ifdef FW_DEBUG
  287.     gxRectangle boundsTransformClip;
  288.     ::GXGetShapeBounds(transformClip, 0, &boundsTransformClip);
  289.  
  290.     gxRectangle boundsTransformClipOld;
  291.     ::GXGetShapeBounds(fTransformClipOld, 0, &boundsTransformClipOld);
  292. #endif
  293.  
  294.     ::GXDisposeShape(transformClip);
  295.  
  296.     err = ::GXGetGraphicsError(&errSticky); // +++
  297. }
  298.  
  299. //----------------------------------------------------------------------------------------
  300. // FW_CGraphicContextGX::RestoreTransformClipping
  301. //----------------------------------------------------------------------------------------
  302.  
  303. void FW_CGraphicContextGX::RestoreTransformClipping()
  304. {
  305.     ::GXSetTransformClip(fGXTransform, fTransformClipOld);
  306.     ::GXDisposeShape(fTransformClipOld);
  307. }
  308.  
  309. //----------------------------------------------------------------------------------------
  310. // FW_CGraphicContextGX::SetupViewPortClipping
  311. //----------------------------------------------------------------------------------------
  312.  
  313. void FW_CGraphicContextGX::SetupViewPortClipping(
  314.         Environment* ev,
  315.         ODFacet* facet,
  316.         ODShape* invalidShape)
  317. {
  318.     ODCanvas* canvas = facet->GetCanvas(ev);
  319.     FW_CAcquiredODShape aqFacetClipShape = facet->AcquireAggregateClipShape(ev, canvas);
  320.  
  321. #ifdef FW_DEBUG
  322.     FW_CRect boundsFacetClipShape;
  323.     aqFacetClipShape->GetBoundingBox(ev, (ODRect*) &boundsFacetClipShape);
  324. #endif
  325.  
  326.     // ----- Map the clip shape's copy back to local coordinates
  327.     gxShape    facetGXShape = aqFacetClipShape->GetGXShape(ev);
  328.     gxShape    viewPortClip = ::GXCopyToShape(NULL, facetGXShape);
  329.     ::GXDisposeShape(facetGXShape);
  330.  
  331.     ::GXSetShapeAttributes(viewPortClip, GXGetShapeAttributes(viewPortClip) & ~gxMapTransformShape);
  332.     ::GXMapShape(viewPortClip, &fMapping);
  333.  
  334.     // ----- Intersect the clip shape with the invalid  shape
  335.     if (invalidShape != NULL)
  336.     {
  337.         gxShape invalidGXShape = invalidShape->GetGXShape(ev);
  338.         ::GXIntersectShape(viewPortClip, invalidGXShape);
  339.         ::GXDisposeShape(invalidGXShape);
  340.     }
  341.  
  342.     // ----- Save the current clip and set the new old 
  343.     fViewPortClipOld = ::GXGetViewPortClip(fGXViewPort);
  344.  
  345. #ifdef FW_DEBUG
  346.     gxRectangle boundsViewPortClip;
  347.     ::GXGetShapeBounds(viewPortClip, 0, &boundsViewPortClip);
  348.  
  349.     gxRectangle boundsViewPortClipOld;
  350.     ::GXGetShapeBounds(fViewPortClipOld, 0, &boundsViewPortClipOld);
  351. #endif
  352.  
  353.     ::GXSetViewPortClip(fGXViewPort, viewPortClip);
  354.     ::GXDisposeShape(viewPortClip);
  355. }
  356.  
  357. //----------------------------------------------------------------------------------------
  358. // FW_CGraphicContextGX::RestoreViewPortClipping
  359. //----------------------------------------------------------------------------------------
  360.  
  361. void FW_CGraphicContextGX::RestoreViewPortClipping()
  362. {
  363.     ::GXSetViewPortClip(fGXViewPort, fViewPortClipOld);
  364.     ::GXDisposeShape(fViewPortClipOld);
  365. }
  366.  
  367. //----------------------------------------------------------------------------------------
  368. // FW_CGraphicContextGX::DrawShape
  369. //----------------------------------------------------------------------------------------
  370.  
  371. void FW_CGraphicContextGX::DrawShape(gxShape shape)
  372. {
  373.     gxGraphicsError err, errSticky;
  374.     err = ::GXGetGraphicsError(&errSticky); // +++
  375.  
  376.     gxTransform oldTransform = ::GXGetShapeTransform(shape);
  377.     ::GXSetShapeTransform(shape, fGXTransform);
  378.  
  379.     err = ::GXGetGraphicsError(&errSticky); // +++
  380.     
  381.     ::GXDrawShape(shape);
  382.  
  383.     err = ::GXGetGraphicsError(&errSticky); // +++
  384.     
  385.     ::GXSetShapeTransform(shape, oldTransform);
  386.  
  387.     err = ::GXGetGraphicsError(&errSticky); // +++
  388. }
  389.  
  390. //----------------------------------------------------------------------------------------
  391. // FW_CGraphicContextGX::GetGXTransform
  392. //----------------------------------------------------------------------------------------
  393.  
  394. gxTransform FW_CGraphicContextGX::GetGXTransform() const
  395. {
  396.     return fGXTransform;
  397. }
  398.  
  399. #endif    // FW_SUPPORT_GX
  400.