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 / SLGC.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  14.7 KB  |  478 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLGC.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 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.             {
  122.     #ifdef FW_BUILD_WIN
  123.                 gc.fTransform->IncrementRefCount(ev);
  124.     #endif
  125.     #ifdef FW_BUILD_MAC
  126.                 gc.fTransform->Acquire(ev);
  127.     #endif
  128.             }
  129.             
  130.             // ----- Set the graphic device -----
  131.             level = 1;
  132.             gc.fGraphicDevice = graphicDevice;
  133.             gc.fGraphicDevice->Acquire();
  134.     
  135.             // ----- Open the Device -----
  136.             level = 2;
  137.             gc.fDevicePreviousState = gc.fGraphicDevice->OpenDevice(ev, &gc);
  138.                     
  139.             // ----- Set the clip -----
  140.             level = 3;
  141.             FW_CHECK_THROW_POINT (FW_CGraphicContext_InitGraphicContext);                        
  142.             FW_PrivGC_SetClip(ev, gc, clipShape);
  143.             FW_FailOnEvError(ev);
  144.         }
  145.         FW_CATCH_BEGIN
  146.         FW_CATCH_EVERYTHING ()
  147.         {
  148.             if (level >= 3)
  149.                 gc.fGraphicDevice->CloseDevice(ev, gc.fDevicePreviousState);
  150.  
  151.             if (level >= 2)
  152.                 gc.fGraphicDevice->Release();
  153.  
  154.             if (level >= 1)
  155.             {
  156.                 if (gc.fTransform != NULL)
  157.                     gc.fTransform->Release(ev);
  158.             }
  159.             
  160.             if (gc.fPreviousGraphicContext)
  161.                 PrivResume(gc.fPreviousGraphicContext);
  162.             FW_THROW_SAME ();
  163.         }
  164.         FW_CATCH_END
  165.     
  166.         FW_gLastGC = &gc;
  167.         gc.fInitialized = TRUE;
  168.     }
  169.     FW_SOM_CATCH
  170. }
  171.  
  172. //----------------------------------------------------------------------------------------
  173. // FW_PrivGC_ChangeClipAndTransform
  174. //----------------------------------------------------------------------------------------
  175.  
  176. void SL_API FW_PrivGC_ChangeClipAndTransform(Environment* ev,
  177.                                             FW_SGraphicContext& gc,
  178.                                             ODTransform* transform,
  179.                                             ODShape* clipShape)
  180. {
  181.     FW_SOM_TRY
  182.     {
  183.         if (gc.fTransform != NULL)
  184.             gc.fTransform->Release(ev);
  185.         
  186.         // ----- Save the transform ----
  187.         gc.fTransform = transform;
  188.         if(gc.fTransform != NULL)
  189.         {
  190. #ifdef FW_BUILD_WIN
  191.             gc.fTransform->IncrementRefCount(ev);
  192. #endif
  193. #ifdef FW_BUILD_MAC
  194.             gc.fTransform->Acquire(ev);
  195. #endif
  196.         }
  197.         
  198.         // ----- Set the origin offset -----
  199.         gc.fGraphicDevice->UpdateOriginForContext(ev);
  200.  
  201.         // ----- Set the clip -----
  202.         FW_PrivGC_SetClip(ev, gc, clipShape);
  203.         FW_FailOnEvError(ev);
  204.     }
  205.     FW_SOM_CATCH
  206. }
  207.  
  208. //----------------------------------------------------------------------------------------
  209. // FW_PrivGC_Terminate
  210. //----------------------------------------------------------------------------------------
  211.  
  212. void SL_API FW_PrivGC_Terminate(Environment* ev, FW_SGraphicContext& gc)
  213. {
  214.     FW_SOM_TRY
  215.     {
  216.         if(gc.fInitialized)
  217.         {
  218.             // ----- Close the Device -----
  219.             if (gc.fDevicePreviousState)
  220.                 gc.fGraphicDevice->CloseDevice(ev, gc.fDevicePreviousState);
  221.         
  222.             // ----- Now we can release the device -----
  223.             gc.fGraphicDevice->Release();
  224.             gc.fGraphicDevice = NULL;
  225.             
  226.             // ----- and the transform -----
  227.             if(gc.fTransform != NULL)
  228.                 gc.fTransform->Release(ev);
  229.                 
  230.             // ----- Reset the last Graphic Context Global -----
  231.             FW_gLastGC = gc.fPreviousGraphicContext;
  232.             
  233.             if (gc.fPreviousGraphicContext)
  234.                 PrivResume(gc.fPreviousGraphicContext);
  235.                 
  236.             gc.fInitialized = FALSE;
  237.         }
  238.         
  239.         FW_PrivMapping_Term(gc.fMapping, ev);
  240.     }
  241.     FW_SOM_CATCH
  242. }
  243.  
  244. //----------------------------------------------------------------------------------------
  245. // FW_PrivGC_GetClip
  246. //----------------------------------------------------------------------------------------
  247.  
  248. ODShape* SL_API FW_PrivGC_GetClip(Environment* ev, const FW_SGraphicContext& gc)
  249. {
  250.     FW_SOM_TRY
  251.     {    
  252.         ODRgnHandle clipRegion = gc.fGraphicDevice->GetClip();
  253.         ODShape* clipShape = ::FW_NewODShape(ev, clipRegion);
  254.         ODShape* clipShapeCopy = FW_PrivGC_DeviceToLogicalShape(ev, gc, clipShape);
  255.         FW_FailOnEvError(ev);
  256.         clipShape->Release(ev);        // Disposes the region, too
  257.         return clipShapeCopy;
  258.     }
  259.     FW_SOM_CATCH
  260.     
  261.     return NULL;
  262. }
  263.  
  264. //----------------------------------------------------------------------------------------
  265. // FW_PrivGC_SetClip
  266. //----------------------------------------------------------------------------------------
  267.  
  268. void SL_API FW_PrivGC_SetClip(Environment* ev, FW_SGraphicContext& gc, ODShape* clipShape)
  269. {
  270.     FW_ASSERT(clipShape != NULL);
  271.     
  272.     FW_SOM_TRY
  273.     {    
  274.         ODShape* clipShapeCopy = FW_PrivGC_LogicalToDeviceShape(ev, gc, clipShape); 
  275.         FW_FailOnEvError(ev);
  276.         ODRgnHandle clipRegion = ::FW_GetShapeRegion(ev, clipShapeCopy);
  277.         gc.fGraphicDevice->SetClip(ev, clipRegion);        // Uses a copy
  278.         clipShapeCopy->Release(ev);    // Disposes the region, too
  279.     }
  280.     FW_SOM_CATCH
  281. }
  282.  
  283. //----------------------------------------------------------------------------------------
  284. // FW_PrivGC_GetClipRect
  285. //----------------------------------------------------------------------------------------
  286.  
  287. void SL_API FW_PrivGC_GetClipRect(Environment* ev, const FW_SGraphicContext& gc, FW_SRect& clipRect)
  288. {
  289.     FW_SOM_TRY
  290.     {    
  291.         FW_CPlatformRect plfmClipRect;
  292.         gc.fGraphicDevice->GetClipRect(plfmClipRect);
  293.         FW_PrivGC_DeviceToLogicalRect(ev, gc, plfmClipRect, clipRect);
  294.         FW_FailOnEvError(ev);
  295.     }
  296.     FW_SOM_CATCH
  297. }
  298.  
  299. //----------------------------------------------------------------------------------------
  300. // FW_PrivGC_SetClipRect
  301. //----------------------------------------------------------------------------------------
  302.  
  303. void SL_API FW_PrivGC_SetClipRect(Environment* ev, FW_SGraphicContext& gc, const FW_SRect& clipRect)
  304. {
  305.     FW_SOM_TRY
  306.     {    
  307.         FW_CPlatformRect plfmClipRect;
  308.         FW_PrivGC_LogicalToDeviceRect(ev, gc, clipRect, plfmClipRect);
  309.         FW_FailOnEvError(ev);
  310.         gc.fGraphicDevice->SetClipRect(plfmClipRect);
  311.     }
  312.     FW_SOM_CATCH
  313. }
  314.  
  315. //----------------------------------------------------------------------------------------
  316. // FW_PrivGC_SetMapping
  317. //----------------------------------------------------------------------------------------
  318.  
  319. void SL_API FW_PrivGC_SetMapping(Environment* ev, FW_SGraphicContext& gc, const FW_SMapping& newMapping)
  320. {
  321.     FW_SOM_TRY
  322.     {    
  323.         FW_PrivMapping_Term(gc.fMapping, ev);
  324.         FW_PrivMapping_InitFromCopy(gc.fMapping, newMapping);
  325.         gc.fGraphicDevice->MappingChanged(ev);
  326.     }
  327.     FW_SOM_CATCH
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. // FW_PrivGC_GetOriginOffset
  332. //----------------------------------------------------------------------------------------
  333.  
  334. void SL_API FW_PrivGC_GetOriginOffset(Environment* ev, const FW_SGraphicContext& gc, FW_PlatformPoint& offset)
  335. {
  336.     // No try block necessary - Do not throw
  337.     FW_PrivMapping_GetOriginOffset(gc.fMapping, ev, offset, gc.fGraphicDevice, gc.fTransform);
  338. }
  339.  
  340. //----------------------------------------------------------------------------------------
  341. // FW_PrivGC_LogicalToDeviceSize
  342. //----------------------------------------------------------------------------------------
  343.  
  344. void SL_API FW_PrivGC_LogicalToDeviceSize(Environment* ev,
  345.                                         const FW_SGraphicContext& gc,
  346.                                         FW_Fixed xSize,
  347.                                         FW_Fixed ySize,
  348.                                         FW_PlatformPoint& ptResult)
  349. {
  350.     // No try block necessary - Do not throw
  351.     FW_CRect rect(FW_IntToFixed(0), FW_IntToFixed(0), xSize, ySize);
  352.     FW_CPlatformRect plfmRect;
  353.     
  354.     FW_PrivGC_LogicalToDeviceRect(ev, gc, rect, plfmRect);
  355.     // even if error let it go
  356.         
  357.     FW_SetXY(ptResult, plfmRect.right - plfmRect.left, plfmRect.bottom - plfmRect.top);
  358. }
  359.  
  360. //----------------------------------------------------------------------------------------
  361. // FW_PrivGC_LogicalToDevicePoint
  362. //----------------------------------------------------------------------------------------
  363.  
  364. void SL_API FW_PrivGC_LogicalToDevicePoint(Environment* ev,
  365.                                 const FW_SGraphicContext& gc,
  366.                                 const FW_SPoint& point,
  367.                                 FW_PlatformPoint& ptResult)
  368. {
  369.     // No try block necessary - Do not throw
  370.     FW_PrivMapping_LogicalToDevicePoint(gc.fMapping, ev, point, ptResult, gc.fGraphicDevice, gc.fTransform);
  371. }
  372.  
  373. //----------------------------------------------------------------------------------------
  374. // FW_PrivGC_LogicalToDeviceRect
  375. //----------------------------------------------------------------------------------------
  376.  
  377. void SL_API FW_PrivGC_LogicalToDeviceRect(Environment* ev,
  378.                                 const FW_SGraphicContext& gc,                    
  379.                                 const FW_SRect& rect,
  380.                                 FW_PlatformRect& rectResult)
  381. {
  382.     // No try block necessary - Do not throw
  383.     FW_PrivMapping_LogicalToDeviceRect(gc.fMapping, ev, rect, rectResult, gc.fGraphicDevice, gc.fTransform);
  384. }
  385.  
  386. //----------------------------------------------------------------------------------------
  387. // FW_PrivGC_LogicalToDeviceShape
  388. //----------------------------------------------------------------------------------------
  389.  
  390. ODShape* SL_API FW_PrivGC_LogicalToDeviceShape(Environment* ev,
  391.                                                 const FW_SGraphicContext& gc, 
  392.                                                 ODShape* shape)
  393. {
  394.     // No try block necessary - Do not throw
  395.     return FW_PrivMapping_LogicalToDeviceShape(gc.fMapping, ev, shape, gc.fGraphicDevice, gc.fTransform);
  396. }
  397.  
  398. //----------------------------------------------------------------------------------------
  399. // FW_PrivGC_DeviceToLogicalSize
  400. //----------------------------------------------------------------------------------------
  401.  
  402. void SL_API FW_PrivGC_DeviceToLogicalSize(Environment* ev,
  403.                                             const FW_SGraphicContext& gc,
  404.                                             FW_PlatformCoordinate xSize,
  405.                                             FW_PlatformCoordinate ySize,
  406.                                             FW_SPoint& ptResult)
  407. {
  408.     // No try block necessary - Do not throw
  409.     FW_CPlatformRect plfmRect(0, 0, xSize, ySize);
  410.     FW_SRect rect;
  411.     FW_PrivGC_DeviceToLogicalRect(ev, gc, plfmRect, rect);
  412.     // even if error let it go
  413.     ptResult.x = rect.right - rect.left;
  414.     ptResult.y = rect.bottom - rect.top;
  415. }
  416.  
  417. //----------------------------------------------------------------------------------------
  418. // FW_PrivGC_DeviceToLogicalPoint
  419. //----------------------------------------------------------------------------------------
  420.  
  421. void SL_API FW_PrivGC_DeviceToLogicalPoint(Environment* ev,
  422.                                             const FW_SGraphicContext& gc,
  423.                                             const FW_PlatformPoint& point,
  424.                                             FW_SPoint& ptResult)
  425. {
  426.     // No try block necessary - Do not throw
  427.     FW_PrivMapping_DeviceToLogicalPoint(gc.fMapping, ev, point, ptResult, gc.fGraphicDevice, gc.fTransform);
  428. }
  429.                                             
  430. //----------------------------------------------------------------------------------------
  431. // FW_PrivGC_DeviceToLogicalRect
  432. //----------------------------------------------------------------------------------------
  433.  
  434. void SL_API FW_PrivGC_DeviceToLogicalRect(Environment* ev,
  435.                                             const FW_SGraphicContext& gc,
  436.                                             const FW_PlatformRect& rect,
  437.                                             FW_SRect& rectResult)
  438. {
  439.     // No try block necessary - Do not throw
  440.     FW_PrivMapping_DeviceToLogicalRect(gc.fMapping, ev, rect, rectResult, gc.fGraphicDevice, gc.fTransform);
  441. }
  442.                                         
  443. //----------------------------------------------------------------------------------------
  444. // FW_PrivGC_DeviceToLogicalShape
  445. //----------------------------------------------------------------------------------------
  446.  
  447. ODShape* SL_API FW_PrivGC_DeviceToLogicalShape(Environment* ev, const FW_SGraphicContext& gc, ODShape* shape)
  448. {
  449.     // No try block necessary - Do not throw
  450.     return FW_PrivMapping_DeviceToLogicalShape(gc.fMapping, ev, shape, gc.fGraphicDevice, gc.fTransform);
  451. }
  452.  
  453. //========================================================================================
  454. //    Local helpers
  455. //========================================================================================
  456.  
  457. //----------------------------------------------------------------------------------------
  458. // PrivSuspend
  459. //----------------------------------------------------------------------------------------
  460.  
  461. static void    PrivSuspend(FW_SGraphicContext* gc)
  462. {
  463.     FW_ASSERT(gc->fDeviceSuspendState == NULL);
  464.     gc->fDeviceSuspendState = gc->fGraphicDevice->Suspend();
  465. }
  466.  
  467. //----------------------------------------------------------------------------------------
  468. // PrivResume
  469. //----------------------------------------------------------------------------------------
  470.  
  471. static void PrivResume(FW_SGraphicContext* gc)
  472. {
  473.     gc->fGraphicDevice->Resume(gc->fDeviceSuspendState);
  474.     gc->fDeviceSuspendState = NULL;
  475. }
  476.  
  477.  
  478.