home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWPart / Sources / FWContxt.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  12.7 KB  |  413 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWContxt.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWCONTXT_H
  13. #include "FWContxt.h"
  14. #endif
  15.  
  16. #ifndef FWVIEW_H
  17. #include "FWView.h"
  18. #endif
  19.  
  20. #ifndef FWFRAME_H
  21. #include "FWFrame.h"
  22. #endif
  23.  
  24. #ifndef FWPRIDEB_H
  25. #include "FWPriDeb.h"
  26. #endif
  27.  
  28. #ifndef FWACQUIR_H
  29. #include "FWAcquir.h"
  30. #endif
  31.  
  32. #ifndef FWFCTINF_H
  33. #include "FWFctInf.h"
  34. #endif
  35.  
  36. #ifndef FWODGEOM_H
  37. #include "FWODGeom.h"
  38. #endif
  39.  
  40. #ifndef FWUTIL_H
  41. #include "FWUtil.h"
  42. #endif
  43.  
  44. // ----- OpenDoc Includes -----
  45.  
  46. #ifndef SOM_ODFacet_xh
  47. #include <Facet.xh>
  48. #endif
  49.  
  50. #if defined(__MWERKS__) && GENERATING68K
  51. // A hack to work around a bug
  52. #pragma import list somGetGlobalEnvironment
  53. #endif
  54.  
  55. //========================================================================================
  56. // Runtime Informations
  57. //========================================================================================
  58.  
  59. #ifdef FW_BUILD_MAC
  60. #pragma segment framework
  61. #endif
  62.  
  63. //========================================================================================
  64. //    class FW_CWindowContext
  65. //========================================================================================
  66.  
  67. FW_DEFINE_AUTO(FW_CWindowContext)
  68.  
  69. //----------------------------------------------------------------------------------------
  70. //    FW_CWindowContext::FW_CWindowContext
  71. //----------------------------------------------------------------------------------------
  72.  
  73. FW_CWindowContext::FW_CWindowContext(Environment* ev, ODFacet* odFacet) :
  74.     FW_CGraphicContext(ev),
  75.     fTempDevice(NULL),
  76.     fODWindow(NULL)
  77. {
  78.     InitWindowContext(ev, odFacet->GetWindow(ev));
  79. }
  80.  
  81. //----------------------------------------------------------------------------------------
  82. //    FW_CWindowContext::FW_CWindowContext
  83. //----------------------------------------------------------------------------------------
  84.  
  85. FW_CWindowContext::FW_CWindowContext(Environment* ev, ODWindow *odWindow) :
  86.     FW_CGraphicContext(ev),
  87.     fTempDevice(NULL),
  88.     fODWindow(NULL)
  89. {
  90.     InitWindowContext(ev, odWindow);
  91. }
  92.  
  93. //----------------------------------------------------------------------------------------
  94. //    FW_CWindowContext::InitWindowContext
  95. //----------------------------------------------------------------------------------------
  96.  
  97. void FW_CWindowContext::InitWindowContext(Environment* ev, ODWindow *odWindow)
  98. {
  99.     FW_ASSERT(odWindow != NULL);
  100.     fODWindow = odWindow;
  101.     fODWindow->Acquire(ev);
  102.     
  103.     ODFacet *facet = fODWindow->GetRootFacet(ev);
  104.  
  105.     FW_CAcquiredODShape aqClipShape = AcquireClipShape(ev);
  106.  
  107. /*
  108.     [HLX] there is a problem trying to reuse the graphic device of the root frame. 
  109.     First of all if the graphic device has a fContext, it should be suspended. Also
  110.     now that we are running static part I don't know what the effect is. We need to 
  111.     look at that. Might be able to put it back when we move the graphic to a SOM DLL.
  112.     
  113.     FW_CGraphicDevice* device = FW_GetFacetGraphicDevice(ev, facet);
  114.     if (device == NULL)
  115.     {
  116.         fTempDevice = new FW_CGraphicDevice(ev, facet->GetCanvas(ev));
  117.         device = fTempDevice;
  118.     }
  119.  
  120.     InitGraphicContext(device,
  121.                         NULL,
  122.                         aqClipShape);
  123. */
  124.     
  125.     fTempDevice = FW_PrivGDev_CreateFromODCanvas(ev, facet->GetCanvas(ev));
  126.     FW_FailOnEvError(ev);
  127.     
  128.     InitGraphicContext(fTempDevice,
  129.                         NULL,
  130.                         aqClipShape);
  131. }
  132.  
  133. //----------------------------------------------------------------------------------------
  134. //    FW_CWindowContext::~FW_CWindowContext
  135. //----------------------------------------------------------------------------------------
  136.  
  137. FW_CWindowContext::~FW_CWindowContext()
  138. {
  139.     if (fODWindow)
  140.         fODWindow->Release(somGetGlobalEnvironment());
  141.         
  142.     if (fTempDevice)
  143.         FW_PrivGDev_Release(fTempDevice);
  144. }
  145.  
  146. //----------------------------------------------------------------------------------------
  147. //    FW_CWindowContext::AcquireClipShape
  148. //----------------------------------------------------------------------------------------
  149.  
  150. ODShape* FW_CWindowContext::AcquireClipShape(Environment* ev)
  151. {
  152. #ifdef FW_BUILD_MAC
  153.     FW_CRect rect(fODWindow->GetPlatformWindow(ev)->portRect);
  154. #endif
  155. #ifdef FW_BUILD_WIN
  156.     FW_CPlatformRect clientRect;
  157.     ::GetClientRect(fODWindow->GetPlatformWindow(ev), &clientRect);
  158.     FW_CRect rect(clientRect);
  159. #endif
  160.  
  161.     return ::FW_NewODShape(ev, rect);
  162. }
  163.  
  164. //----------------------------------------------------------------------------------------
  165. //    FW_CWindowContext::Reset
  166. //----------------------------------------------------------------------------------------
  167.  
  168. void FW_CWindowContext::Reset()
  169. {
  170.     FW_CAcquiredODShape aqClipShape = AcquireClipShape(fEnvironment);    
  171.     PrivReset(NULL, aqClipShape);
  172. }
  173.  
  174. //========================================================================================
  175. //    class FW_CFrameContext
  176. //========================================================================================
  177.  
  178. FW_DEFINE_AUTO(FW_CFrameContext)
  179.  
  180. //----------------------------------------------------------------------------------------
  181. // FW_CFrameContext::FW_CFrameContext
  182. //----------------------------------------------------------------------------------------
  183. // invalidShape is in Frame coordinate
  184.  
  185. FW_CFrameContext::FW_CFrameContext(Environment* ev, ODFacet *facet, ODShape* invalidShape) :
  186.     FW_CGraphicContext(ev),
  187.     fFacet(facet)
  188. {
  189.     FW_ASSERT(facet != NULL);
  190.     
  191.     FW_CAcquiredODTransform aqFrameTransform = facet->AcquireFrameTransform(ev, NULL);
  192.     
  193.     FW_CAcquiredODShape aqClipShape = FW_CopyAndRelease(ev, fFacet->AcquireAggregateClipShape(ev, NULL));
  194.     if (invalidShape != NULL)
  195.         aqClipShape->Intersect(ev, invalidShape);
  196.  
  197.     InitGraphicContext(FW_GetFacetGraphicDevice(ev, fFacet),
  198.                         aqFrameTransform,
  199.                         aqClipShape);
  200. }
  201.  
  202. //----------------------------------------------------------------------------------------
  203. //    FW_CFrameContext::~FW_CFrameContext
  204. //----------------------------------------------------------------------------------------
  205.  
  206. FW_CFrameContext::~FW_CFrameContext()
  207. {
  208. }
  209.  
  210. //----------------------------------------------------------------------------------------
  211. //    FW_CFrameContext::Reset
  212. //----------------------------------------------------------------------------------------
  213.  
  214. void FW_CFrameContext::Reset()
  215. {
  216.     FW_CAcquiredODTransform aqContentTransform = fFacet->AcquireFrameTransform(fEnvironment, NULL);
  217.     FW_CAcquiredODShape aqClipShape = FW_CopyAndRelease(fEnvironment, fFacet->AcquireAggregateClipShape(fEnvironment, NULL));
  218.     
  219.     PrivReset(aqContentTransform, aqClipShape);
  220. }
  221.  
  222. //========================================================================================
  223. //    class FW_CPlatformGraphicContext
  224. //========================================================================================
  225.  
  226. FW_DEFINE_AUTO(FW_CPlatformGraphicContext)
  227.  
  228. //----------------------------------------------------------------------------------------
  229. // FW_CPlatformGraphicContext::FW_CPlatformGraphicContext
  230. //----------------------------------------------------------------------------------------
  231.  
  232. FW_CPlatformGraphicContext::FW_CPlatformGraphicContext(Environment* ev, ODFacet *facet, ODShape* invalidShape) :
  233.     FW_CGraphicContext(ev),
  234.     fFacet(facet)
  235. #ifdef FW_BUILD_MAC
  236.     ,fMacPort(NULL)
  237. #endif
  238. {
  239.     FW_ASSERT(facet != NULL);
  240.  
  241.     // InitGraphicContext expect clip shape in content coordinates
  242.     FW_CAcquiredODShape aqClipShape(FW_CopyAndRelease(ev, fFacet->AcquireAggregateClipShape(ev, NULL)));
  243.     FW_FrameToContent(ev, fFacet->GetFrame(ev), aqClipShape);
  244.     
  245.     if (invalidShape != NULL)
  246.         aqClipShape->Intersect(ev, invalidShape);
  247.  
  248.     InitGraphicContext(FW_GetFacetGraphicDevice(ev, fFacet),
  249.                         NULL,
  250.                         aqClipShape);
  251.                         
  252. #ifdef FW_BUILD_MAC
  253.     PrivMacAlignOrigin(ev);
  254. #endif
  255. }
  256.  
  257. //----------------------------------------------------------------------------------------
  258. // FW_CPlatformGraphicContext::~FW_CPlatformGraphicContext
  259. //----------------------------------------------------------------------------------------
  260.  
  261. FW_CPlatformGraphicContext::~FW_CPlatformGraphicContext()
  262. {
  263. #ifdef FW_BUILD_MAC
  264.     if (fMacPort != NULL)
  265.     {
  266.         GrafPtr port;
  267.         ::GetPort(&port);    
  268.         ::SetPort(fMacPort);    
  269.         ::SetOrigin(-fMacOrigin.h, -fMacOrigin.v);
  270.         ::SetPort(port);
  271.     }
  272. #endif
  273. }
  274.  
  275. //----------------------------------------------------------------------------------------
  276. //    FW_CPlatformGraphicContext::Reset
  277. //----------------------------------------------------------------------------------------
  278.  
  279. void FW_CPlatformGraphicContext::Reset()
  280. {
  281.     FW_CAcquiredODShape aqClipShape(FW_CopyAndRelease(fEnvironment, fFacet->AcquireAggregateClipShape(fEnvironment, NULL)));
  282.     
  283.     PrivReset(NULL, aqClipShape);
  284.     
  285. #ifdef FW_BUILD_MAC
  286.     PrivMacAlignOrigin(fEnvironment);
  287. #endif
  288. }
  289.  
  290. #ifdef FW_BUILD_MAC
  291. //----------------------------------------------------------------------------------------
  292. // FW_CPlatformGraphicContext::PrivMacAlignOrigin
  293. //----------------------------------------------------------------------------------------
  294.  
  295. void FW_CPlatformGraphicContext::PrivMacAlignOrigin(Environment* ev)
  296. {
  297.     FW_CAcquiredODTransform aqContentTransform = fFacet->AcquireContentTransform(ev, NULL);
  298.     FW_CPoint origin(FW_kZeroPoint);
  299.     origin.Transform(ev, aqContentTransform);
  300.     
  301.     ::GetPort(&fMacPort);    
  302.     fMacOrigin.h = fMacPort->portRect.left;
  303.     fMacOrigin.v = fMacPort->portRect.top;
  304.  
  305.     ::SetOrigin(- FW_FixedToInt(origin.x), - FW_FixedToInt(origin.y));
  306. }
  307. #endif
  308.  
  309. //========================================================================================
  310. // CLASS FW_CViewContext
  311. //========================================================================================
  312.  
  313. FW_DEFINE_AUTO(FW_CViewContext)
  314.  
  315. //----------------------------------------------------------------------------------------
  316. // FW_CViewContext::FW_CViewContext
  317. //----------------------------------------------------------------------------------------
  318. // invalidShape is in view content coordinates
  319.  
  320. FW_CViewContext::FW_CViewContext(Environment* ev, FW_CView *view, ODFacet* odFacet, ODShape* invalidShape) :
  321.     FW_CGraphicContext(ev),
  322.     fView(view),
  323.     fFacet(odFacet)
  324. {
  325.     FW_ASSERT(fFacet);
  326.     
  327.     if (fView == NULL)
  328.     {
  329.         FW_CFrame* frame = FW_CFrame::ODtoFWFrame(ev, odFacet->GetFrame(ev));
  330.         fView = frame->GetContentView(ev);
  331.     }
  332.     
  333.     FW_CAcquiredODTransform aqTransform = PrivAcquireTransform(ev);
  334.     FW_CAcquiredODShape aqClipShape = PrivAcquireClipShape(ev);
  335.     
  336.     // Intersect it with the invalidShape (already in view content coordinates)
  337.     if (invalidShape != NULL)
  338.         aqClipShape->Intersect(ev, invalidShape);
  339.     
  340.     InitGraphicContext(FW_GetFacetGraphicDevice(ev, fFacet), 
  341.                         aqTransform, 
  342.                         aqClipShape);
  343.  
  344.     FW_END_CONSTRUCTOR
  345. }
  346.  
  347. //----------------------------------------------------------------------------------------
  348. // FW_CViewContext::~FW_CViewContext
  349. //----------------------------------------------------------------------------------------
  350.  
  351. FW_CViewContext::~FW_CViewContext()
  352. {
  353.     FW_START_DESTRUCTOR
  354. }
  355.  
  356. //----------------------------------------------------------------------------------------
  357. // FW_CViewContext::PrivAcquireTransform
  358. //----------------------------------------------------------------------------------------
  359.  
  360. ODTransform* FW_CViewContext::PrivAcquireTransform(Environment* ev)
  361. {
  362.     ODTransform* transform = FW_CopyAndRelease(ev, fView->AcquireViewContentToFrameTransform(ev));
  363.     
  364.     // compute transform to convert view to canvas coordinates 
  365.     FW_CAcquiredODTransform aqFrameTransform = fFacet->AcquireFrameTransform(ev, NULL);
  366.     transform->PostCompose(ev, aqFrameTransform);
  367.     
  368.     return transform;
  369. }
  370.  
  371. //----------------------------------------------------------------------------------------
  372. // FW_CViewContext::PrivAcquireClipShape
  373. //----------------------------------------------------------------------------------------
  374.  
  375. ODShape* FW_CViewContext::PrivAcquireClipShape(Environment* ev)
  376. {
  377.     ODShape* clipShape;
  378.     if (fView->PrivIsTopPrintedView(ev))
  379.     {
  380.         // If I am the top printed view I need to use my extent not my visible bounds
  381.         FW_CRect clipRect(FW_kZeroPoint, fView->GetExtent(ev));    
  382.         clipShape = ::FW_NewODShape(ev, clipRect);
  383.         clipShape->SetGeometryMode(ev, kODLoseGeometry);
  384.     }
  385.     else
  386.     {
  387.         FW_CAcquiredODShape aqAggregateClipShape = fFacet->AcquireAggregateClipShape(ev, NULL);
  388.         
  389.         clipShape = ::FW_NewODShape(ev, fView->GetVisibleBounds(ev));  // visible bounds is in frame coordinates too
  390.         clipShape->SetGeometryMode(ev, kODLoseGeometry);
  391.  
  392.         clipShape->Intersect(ev, aqAggregateClipShape);
  393.  
  394.         //    convert it to view content coordinates
  395.         fView->FrameToViewContent(ev, clipShape);
  396.     }
  397.     
  398.     return clipShape;
  399. }
  400.  
  401. //----------------------------------------------------------------------------------------
  402. //    FW_CViewContext::Reset
  403. //----------------------------------------------------------------------------------------
  404.  
  405. void FW_CViewContext::Reset()
  406. {
  407.     FW_CAcquiredODTransform aqTransform = PrivAcquireTransform(fEnvironment);
  408.     FW_CAcquiredODShape aqClipShape = PrivAcquireClipShape(fEnvironment);
  409.     
  410.     PrivReset(aqTransform, aqClipShape);
  411. }
  412.  
  413.