home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / ODF / OS / FWGraphx / SLGC.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  14.5 KB  |  466 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLGC.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef SLGC_H
  13. #include "SLGC.h"
  14. #endif
  15.  
  16. #ifndef SLMAPING_H
  17. #include "SLMaping.h"
  18. #endif
  19.  
  20. #ifndef PRGRDEV_H
  21. #include "PRGDev.h"
  22. #endif
  23.  
  24. #ifndef FWRECT_H
  25. #include "FWRect.h"
  26. #endif
  27.  
  28. #ifndef FWODGEOM_H
  29. #include "FWODGeom.h"
  30. #endif
  31.  
  32. #ifndef SLGRGLOB_H
  33. #include "SLGrGlob.h"
  34. #endif
  35.  
  36. #ifndef SOM_ODShape_xh
  37. #include <Shape.xh>
  38. #endif
  39.  
  40. #ifdef FW_BUILD_MAC
  41. #pragma segment FW_GraphicsContext
  42. #endif
  43.  
  44. //========================================================================================
  45. //    Local helper prototypes
  46. //========================================================================================
  47.  
  48. static void    PrivSuspend(FW_SGraphicContext* gc);
  49. static void PrivResume(FW_SGraphicContext* gc);
  50.  
  51. //========================================================================================
  52. //    struct FW_SGraphicContext
  53. //========================================================================================
  54.  
  55. // The one and only current GC
  56.  
  57. static FW_SGraphicContext*        FW_gLastGC = NULL;
  58.  
  59. //----------------------------------------------------------------------------------------
  60. // FW_PrivGC_GetCurrent
  61. //----------------------------------------------------------------------------------------
  62.  
  63. FW_SGraphicContext* SL_API FW_PrivGC_GetCurrent(Environment* ev)
  64. {
  65. FW_UNUSED(ev);
  66.     // No try block necessary - Do not throw
  67.     return FW_gLastGC;
  68. }
  69.  
  70. //----------------------------------------------------------------------------------------
  71. // FW_PrivGC_Clear
  72. //----------------------------------------------------------------------------------------
  73.  
  74. void SL_API FW_PrivGC_Clear(Environment* ev, FW_SGraphicContext& gc)
  75. {
  76.     // No try block necessary - Do not throw
  77.     gc.fInitialized                =     FALSE;
  78.     gc.fPreviousGraphicContext    =    FW_gLastGC;
  79.     gc.fDevicePreviousState        =    NULL;
  80.     gc.fDeviceSuspendState        =    NULL;
  81.     gc.fEnvironment                =    ev;
  82.     gc.fTransform                =    NULL;
  83.     
  84.     FW_PrivMapping_Init(gc.fMapping, FW_kPoint);
  85. }
  86.  
  87. //----------------------------------------------------------------------------------------
  88. // FW_PrivGC_Initialize
  89. //
  90. //    Exception Handling Notes
  91. //    Don't forget kids, if we fail during construction then our destructor *won't*
  92. //    get called - i.e. CloseDevice won't be called to match the OpenDevice, etc.
  93. //    We need to protect ourselves from things like SetClip failing (and yes, even stupid 
  94. //     little clip operations *do* fail).
  95. //    We could use helper objects, but it seems easier to use nested try/catch blocks in
  96. //    this case since we'd have no opportunity to reuse the helper classes.
  97. //----------------------------------------------------------------------------------------
  98.  
  99. void SL_API FW_PrivGC_Initialize(Environment* ev, 
  100.                                 FW_SGraphicContext& gc,
  101.                                 FW_HGDevice graphicDevice,
  102.                                 ODTransform* transform,
  103.                                 ODShape* clipShape)
  104. {
  105.     FW_SOM_TRY
  106.     {
  107.         FW_ASSERT(graphicDevice != NULL);
  108.         
  109.         if (gc.fPreviousGraphicContext)
  110.             PrivSuspend(gc.fPreviousGraphicContext);
  111.  
  112.         short level;
  113.         FW_VOLATILE(level);
  114.         
  115.         FW_TRY
  116.         {
  117.             // ----- Save the transform ----
  118.             level = 0;
  119.             gc.fTransform = transform;
  120.             if (gc.fTransform != NULL)
  121.                 gc.fTransform->Acquire(ev);
  122.             
  123.             // ----- Set the graphic device -----
  124.             level = 1;
  125.             gc.fGraphicDevice = graphicDevice;
  126.             gc.fGraphicDevice->Acquire();
  127.     
  128.             // ----- Open the Device -----
  129.             level = 2;
  130.             gc.fDevicePreviousState = gc.fGraphicDevice->OpenDevice(ev, &gc);
  131.                     
  132.             // ----- Set the clip -----
  133.             level = 3;
  134.             FW_CHECK_THROW_POINT (FW_CGraphicContext_InitGraphicContext);                        
  135.             FW_PrivGC_SetClip(ev, gc, clipShape);
  136.             FW_FailOnEvError(ev);
  137.         }
  138.         FW_CATCH_BEGIN
  139.         FW_CATCH_EVERYTHING ()
  140.         {
  141.             if (level >= 3)
  142.                 gc.fGraphicDevice->CloseDevice(ev, gc.fDevicePreviousState);
  143.  
  144.             if (level >= 2)
  145.                 gc.fGraphicDevice->Release();
  146.  
  147.             if (level >= 1)
  148.             {
  149.                 if (gc.fTransform != NULL)
  150.                     gc.fTransform->Release(ev);
  151.             }
  152.             
  153.             if (gc.fPreviousGraphicContext)
  154.                 PrivResume(gc.fPreviousGraphicContext);
  155.             FW_THROW_SAME ();
  156.         }
  157.         FW_CATCH_END
  158.     
  159.         FW_gLastGC = &gc;
  160.         gc.fInitialized = TRUE;
  161.     }
  162.     FW_SOM_CATCH
  163. }
  164.  
  165. //----------------------------------------------------------------------------------------
  166. // FW_PrivGC_ChangeClipAndTransform
  167. //----------------------------------------------------------------------------------------
  168.  
  169. void SL_API FW_PrivGC_ChangeClipAndTransform(Environment* ev,
  170.                                             FW_SGraphicContext& gc,
  171.                                             ODTransform* transform,
  172.                                             ODShape* clipShape)
  173. {
  174.     FW_SOM_TRY
  175.     {
  176.         // ----- Save the transform ----
  177.         // (Acquire new, release old, then assign)
  178.         if (transform != NULL)
  179.             transform->Acquire(ev);
  180.         
  181.         if (gc.fTransform != NULL)
  182.             gc.fTransform->Release(ev);
  183.         
  184.         gc.fTransform = transform;
  185.  
  186.         // ----- Set the origin offset -----
  187.         gc.fGraphicDevice->UpdateOriginForContext(ev);
  188.  
  189.         // ----- Set the clip -----
  190.         FW_PrivGC_SetClip(ev, gc, clipShape);
  191.         FW_FailOnEvError(ev);
  192.     }
  193.     FW_SOM_CATCH
  194. }
  195.  
  196. //----------------------------------------------------------------------------------------
  197. // FW_PrivGC_Terminate
  198. //----------------------------------------------------------------------------------------
  199.  
  200. void SL_API FW_PrivGC_Terminate(Environment* ev, FW_SGraphicContext& gc)
  201. {
  202.     FW_SOM_TRY
  203.     {
  204.         if(gc.fInitialized)
  205.         {
  206.             // ----- Close the Device -----
  207.             if (gc.fDevicePreviousState)
  208.                 gc.fGraphicDevice->CloseDevice(ev, gc.fDevicePreviousState);
  209.         
  210.             // ----- Now we can release the device -----
  211.             gc.fGraphicDevice->Release();
  212.             gc.fGraphicDevice = NULL;
  213.             
  214.             // ----- and the transform -----
  215.             if (gc.fTransform != NULL)
  216.                 gc.fTransform->Release(ev);
  217.                 
  218.             // ----- Reset the last Graphic Context Global -----
  219.             FW_gLastGC = gc.fPreviousGraphicContext;
  220.             
  221.             if (gc.fPreviousGraphicContext)
  222.                 PrivResume(gc.fPreviousGraphicContext);
  223.                 
  224.             gc.fInitialized = FALSE;
  225.         }
  226.         
  227.         FW_PrivMapping_Term(gc.fMapping, ev);
  228.     }
  229.     FW_SOM_CATCH
  230. }
  231.  
  232. //----------------------------------------------------------------------------------------
  233. // FW_PrivGC_GetClip
  234. //----------------------------------------------------------------------------------------
  235.  
  236. ODShape* SL_API FW_PrivGC_GetClip(Environment* ev, const FW_SGraphicContext& gc)
  237. {
  238.     FW_SOM_TRY
  239.     {    
  240.         ODRgnHandle clipRegion = gc.fGraphicDevice->GetClip();
  241.         ODShape* clipShape = ::FW_NewODShape(ev, clipRegion);
  242.         ODShape* clipShapeCopy = FW_PrivGC_DeviceToLogicalShape(ev, gc, clipShape);
  243.         FW_FailOnEvError(ev);
  244.         clipShape->Release(ev);        // Disposes the region, too
  245.         return clipShapeCopy;
  246.     }
  247.     FW_SOM_CATCH
  248.     
  249.     return NULL;
  250. }
  251.  
  252. //----------------------------------------------------------------------------------------
  253. // FW_PrivGC_SetClip
  254. //----------------------------------------------------------------------------------------
  255.  
  256. void SL_API FW_PrivGC_SetClip(Environment* ev, FW_SGraphicContext& gc, ODShape* clipShape)
  257. {
  258.     FW_ASSERT(clipShape != NULL);
  259.     
  260.     FW_SOM_TRY
  261.     {    
  262.         ODShape* clipShapeCopy = FW_PrivGC_LogicalToDeviceShape(ev, gc, clipShape); 
  263.         FW_FailOnEvError(ev);
  264.         ODRgnHandle clipRegion = ::FW_GetShapeRegion(ev, clipShapeCopy);
  265.         gc.fGraphicDevice->SetClip(ev, clipRegion);        // Uses a copy
  266.         clipShapeCopy->Release(ev);    // Disposes the region, too
  267.     }
  268.     FW_SOM_CATCH
  269. }
  270.  
  271. //----------------------------------------------------------------------------------------
  272. // FW_PrivGC_GetClipRect
  273. //----------------------------------------------------------------------------------------
  274.  
  275. void SL_API FW_PrivGC_GetClipRect(Environment* ev, const FW_SGraphicContext& gc, FW_SRect& clipRect)
  276. {
  277.     FW_SOM_TRY
  278.     {    
  279.         FW_CPlatformRect plfmClipRect;
  280.         gc.fGraphicDevice->GetClipRect(plfmClipRect);
  281.         FW_PrivGC_DeviceToLogicalRect(ev, gc, plfmClipRect, clipRect);
  282.         FW_FailOnEvError(ev);
  283.     }
  284.     FW_SOM_CATCH
  285. }
  286.  
  287. //----------------------------------------------------------------------------------------
  288. // FW_PrivGC_SetClipRect
  289. //----------------------------------------------------------------------------------------
  290.  
  291. void SL_API FW_PrivGC_SetClipRect(Environment* ev, FW_SGraphicContext& gc, const FW_SRect& clipRect)
  292. {
  293.     FW_SOM_TRY
  294.     {    
  295.         FW_CPlatformRect plfmClipRect;
  296.         FW_PrivGC_LogicalToDeviceRect(ev, gc, clipRect, plfmClipRect);
  297.         FW_FailOnEvError(ev);
  298.         gc.fGraphicDevice->SetClipRect(plfmClipRect);
  299.     }
  300.     FW_SOM_CATCH
  301. }
  302.  
  303. //----------------------------------------------------------------------------------------
  304. // FW_PrivGC_SetMapping
  305. //----------------------------------------------------------------------------------------
  306.  
  307. void SL_API FW_PrivGC_SetMapping(Environment* ev, FW_SGraphicContext& gc, const FW_SMapping& newMapping)
  308. {
  309.     FW_SOM_TRY
  310.     {    
  311.         FW_PrivMapping_Term(gc.fMapping, ev);
  312.         FW_PrivMapping_InitFromCopy(gc.fMapping, newMapping);
  313.         gc.fGraphicDevice->MappingChanged(ev);
  314.     }
  315.     FW_SOM_CATCH
  316. }
  317.  
  318. //----------------------------------------------------------------------------------------
  319. // FW_PrivGC_GetOriginOffset
  320. //----------------------------------------------------------------------------------------
  321.  
  322. void SL_API FW_PrivGC_GetOriginOffset(Environment* ev, const FW_SGraphicContext& gc, FW_PlatformPoint& offset)
  323. {
  324.     // No try block necessary - Do not throw
  325.     FW_PrivMapping_GetOriginOffset(gc.fMapping, ev, offset, gc.fGraphicDevice, gc.fTransform);
  326. }
  327.  
  328. //----------------------------------------------------------------------------------------
  329. // FW_PrivGC_LogicalToDeviceSize
  330. //----------------------------------------------------------------------------------------
  331.  
  332. void SL_API FW_PrivGC_LogicalToDeviceSize(Environment* ev,
  333.                                         const FW_SGraphicContext& gc,
  334.                                         FW_Fixed xSize,
  335.                                         FW_Fixed ySize,
  336.                                         FW_PlatformPoint& ptResult)
  337. {
  338.     // No try block necessary - Do not throw
  339.     FW_CRect rect(FW_IntToFixed(0), FW_IntToFixed(0), xSize, ySize);
  340.     FW_CPlatformRect plfmRect;
  341.     
  342.     FW_PrivGC_LogicalToDeviceRect(ev, gc, rect, plfmRect);
  343.     // even if error let it go
  344.         
  345.     FW_SetXY(ptResult, plfmRect.right - plfmRect.left, plfmRect.bottom - plfmRect.top);
  346. }
  347.  
  348. //----------------------------------------------------------------------------------------
  349. // FW_PrivGC_LogicalToDevicePoint
  350. //----------------------------------------------------------------------------------------
  351.  
  352. void SL_API FW_PrivGC_LogicalToDevicePoint(Environment* ev,
  353.                                 const FW_SGraphicContext& gc,
  354.                                 const FW_SPoint& point,
  355.                                 FW_PlatformPoint& ptResult)
  356. {
  357.     // No try block necessary - Do not throw
  358.     FW_PrivMapping_LogicalToDevicePoint(gc.fMapping, ev, point, ptResult, gc.fGraphicDevice, gc.fTransform);
  359. }
  360.  
  361. //----------------------------------------------------------------------------------------
  362. // FW_PrivGC_LogicalToDeviceRect
  363. //----------------------------------------------------------------------------------------
  364.  
  365. void SL_API FW_PrivGC_LogicalToDeviceRect(Environment* ev,
  366.                                 const FW_SGraphicContext& gc,                    
  367.                                 const FW_SRect& rect,
  368.                                 FW_PlatformRect& rectResult)
  369. {
  370.     // No try block necessary - Do not throw
  371.     FW_PrivMapping_LogicalToDeviceRect(gc.fMapping, ev, rect, rectResult, gc.fGraphicDevice, gc.fTransform);
  372. }
  373.  
  374. //----------------------------------------------------------------------------------------
  375. // FW_PrivGC_LogicalToDeviceShape
  376. //----------------------------------------------------------------------------------------
  377.  
  378. ODShape* SL_API FW_PrivGC_LogicalToDeviceShape(Environment* ev,
  379.                                                 const FW_SGraphicContext& gc, 
  380.                                                 ODShape* shape)
  381. {
  382.     // No try block necessary - Do not throw
  383.     return FW_PrivMapping_LogicalToDeviceShape(gc.fMapping, ev, shape, gc.fGraphicDevice, gc.fTransform);
  384. }
  385.  
  386. //----------------------------------------------------------------------------------------
  387. // FW_PrivGC_DeviceToLogicalSize
  388. //----------------------------------------------------------------------------------------
  389.  
  390. void SL_API FW_PrivGC_DeviceToLogicalSize(Environment* ev,
  391.                                             const FW_SGraphicContext& gc,
  392.                                             FW_PlatformCoordinate xSize,
  393.                                             FW_PlatformCoordinate ySize,
  394.                                             FW_SPoint& ptResult)
  395. {
  396.     // No try block necessary - Do not throw
  397.     FW_CPlatformRect plfmRect(0, 0, xSize, ySize);
  398.     FW_SRect rect;
  399.     FW_PrivGC_DeviceToLogicalRect(ev, gc, plfmRect, rect);
  400.     // even if error let it go
  401.     ptResult.x = rect.right - rect.left;
  402.     ptResult.y = rect.bottom - rect.top;
  403. }
  404.  
  405. //----------------------------------------------------------------------------------------
  406. // FW_PrivGC_DeviceToLogicalPoint
  407. //----------------------------------------------------------------------------------------
  408.  
  409. void SL_API FW_PrivGC_DeviceToLogicalPoint(Environment* ev,
  410.                                             const FW_SGraphicContext& gc,
  411.                                             const FW_PlatformPoint& point,
  412.                                             FW_SPoint& ptResult)
  413. {
  414.     // No try block necessary - Do not throw
  415.     FW_PrivMapping_DeviceToLogicalPoint(gc.fMapping, ev, point, ptResult, gc.fGraphicDevice, gc.fTransform);
  416. }
  417.                                             
  418. //----------------------------------------------------------------------------------------
  419. // FW_PrivGC_DeviceToLogicalRect
  420. //----------------------------------------------------------------------------------------
  421.  
  422. void SL_API FW_PrivGC_DeviceToLogicalRect(Environment* ev,
  423.                                             const FW_SGraphicContext& gc,
  424.                                             const FW_PlatformRect& rect,
  425.                                             FW_SRect& rectResult)
  426. {
  427.     // No try block necessary - Do not throw
  428.     FW_PrivMapping_DeviceToLogicalRect(gc.fMapping, ev, rect, rectResult, gc.fGraphicDevice, gc.fTransform);
  429. }
  430.                                         
  431. //----------------------------------------------------------------------------------------
  432. // FW_PrivGC_DeviceToLogicalShape
  433. //----------------------------------------------------------------------------------------
  434.  
  435. ODShape* SL_API FW_PrivGC_DeviceToLogicalShape(Environment* ev, const FW_SGraphicContext& gc, ODShape* shape)
  436. {
  437.     // No try block necessary - Do not throw
  438.     return FW_PrivMapping_DeviceToLogicalShape(gc.fMapping, ev, shape, gc.fGraphicDevice, gc.fTransform);
  439. }
  440.  
  441. //========================================================================================
  442. //    Local helpers
  443. //========================================================================================
  444.  
  445. //----------------------------------------------------------------------------------------
  446. // PrivSuspend
  447. //----------------------------------------------------------------------------------------
  448.  
  449. static void    PrivSuspend(FW_SGraphicContext* gc)
  450. {
  451.     FW_ASSERT(gc->fDeviceSuspendState == NULL);
  452.     gc->fDeviceSuspendState = gc->fGraphicDevice->Suspend();
  453. }
  454.  
  455. //----------------------------------------------------------------------------------------
  456. // PrivResume
  457. //----------------------------------------------------------------------------------------
  458.  
  459. static void PrivResume(FW_SGraphicContext* gc)
  460. {
  461.     gc->fGraphicDevice->Resume(gc->fDeviceSuspendState);
  462.     gc->fDeviceSuspendState = NULL;
  463. }
  464.  
  465.  
  466.