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 / Draw / Sources / LineShp.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  9.9 KB  |  378 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                LineShp.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef BASESHP_H
  15. #include "BaseShp.h"
  16. #endif
  17.  
  18. #ifndef LINESHP_H
  19. #include "LineShp.h"
  20. #endif
  21.  
  22. #ifndef UTILS_H
  23. #include "Utils.h"
  24. #endif
  25.  
  26. #ifndef CONSTANT_H
  27. #include "Constant.h"
  28. #endif
  29.  
  30. #ifndef DRAWPART_H
  31. #include "DrawPart.h"
  32. #endif
  33.  
  34. #ifndef DRAWPART_H
  35. #include "DrawPart.h"
  36. #endif
  37.  
  38. #ifndef DRAWFRM_H
  39. #include "DrawFrm.h"
  40. #endif
  41.  
  42. #ifndef DRAWPRXY_H
  43. #include "DrawPrxy.h"
  44. #endif
  45.  
  46. #ifndef DRAWLINK_H
  47. #include "DrawLink.h"
  48. #endif
  49.  
  50. #ifndef DRAWCLIP_H
  51. #include "DrawClip.h"
  52. #endif
  53.  
  54. // ----- Part Layer -----
  55.  
  56. #ifndef FWFRMING_H
  57. #include "FWFrming.h"
  58. #endif
  59.  
  60. #ifndef FWUTIL_H
  61. #include "FWUtil.h"
  62. #endif
  63.  
  64. #ifndef FWSELECT_H
  65. #include "FWSelect.h"
  66. #endif
  67.  
  68. #ifndef FWITERS_H
  69. #include "FWIters.h"
  70. #endif
  71.  
  72. // ----- OS Layer -----
  73.  
  74. #ifndef FWEVENT_H
  75. #include "FWEvent.h"
  76. #endif
  77.  
  78. #ifndef FWRECSHP_H
  79. #include "FWRecShp.h"
  80. #endif
  81.  
  82. #ifndef FWTXTBOX_H
  83. #include "FWTxtBox.h"
  84. #endif
  85.  
  86. #ifndef FWLINSHP_H
  87. #include "FWLinShp.h"
  88. #endif
  89.  
  90. #ifndef FWOVLSHP_H
  91. #include "FWOvlShp.h"
  92. #endif
  93.  
  94. #ifndef FWRRCSHP_H
  95. #include "FWRRcShp.h"
  96. #endif
  97.  
  98. #ifndef FWODGEOM_H
  99. #include "FWODGeom.h"
  100. #endif
  101.  
  102. // ----- Foundation Includes -----
  103.  
  104. #ifndef FWSTREAM_H
  105. #include "FWStream.h"
  106. #endif
  107.  
  108. // ----- OpenDoc Includes -----
  109.  
  110. #ifndef SOM_ODTransform_xh
  111. #include <Trnsform.xh>
  112. #endif
  113.  
  114. //========================================================================================
  115. // Runtime Information
  116. //========================================================================================
  117.  
  118. #ifdef FW_BUILD_MAC
  119. #pragma segment odfdrawshapes
  120. #endif
  121.  
  122. //========================================================================================
  123. // RunTime Info
  124. //========================================================================================
  125.  
  126. FW_DEFINE_AUTO(CLineShape)
  127. FW_DEFINE_CLASS_M1(CLineShape, CBaseShape)
  128.  
  129. const FW_ClassTypeConstant LLineShape = FW_TYPE_CONSTANT('s','h','l','n');
  130. FW_REGISTER_ARCHIVABLE_CLASS(LLineShape, CLineShape, CLineShape::Read, 0, 0, CBaseShape::Write)
  131.  
  132. //========================================================================================
  133. // class CLineShape
  134. //========================================================================================
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // CLineShape::CLineShape
  138. //----------------------------------------------------------------------------------------
  139.  
  140. CLineShape::CLineShape() :
  141.     CBaseShape(2, kLineShape, kFrameOnly)
  142. {
  143.     FW_END_CONSTRUCTOR
  144. }
  145.  
  146. //----------------------------------------------------------------------------------------
  147. // CLineShape::CLineShape
  148. //----------------------------------------------------------------------------------------
  149.  
  150. CLineShape::CLineShape(FW_CReadableStream& archive) :
  151.     CBaseShape(archive)
  152. {    
  153.     archive >> fStart;
  154.     archive >> fEnd;
  155.  
  156.     FW_END_CONSTRUCTOR
  157. }
  158.  
  159. //----------------------------------------------------------------------------------------
  160. // CLineShape::~CLineShape
  161. //----------------------------------------------------------------------------------------
  162.  
  163. CLineShape::~CLineShape()
  164. {
  165.     FW_START_DESTRUCTOR
  166. }
  167.  
  168. //----------------------------------------------------------------------------------------
  169. // CLineShape::RenderShape
  170. //----------------------------------------------------------------------------------------
  171.  
  172. void CLineShape::RenderShape(Environment* ev, ODFacet* facet, FW_CGraphicContext& gc)
  173. {    
  174.     FW_CLineShape::RenderLine(gc, fStart, fEnd, fFrameInk, fFrameStyle);        
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. // CLineShape::OutlineShape
  179. //----------------------------------------------------------------------------------------
  180.  
  181. void CLineShape::OutlineShape(FW_CGraphicContext& gc, const FW_CInk& ink, const FW_CStyle& style, 
  182.                               const FW_CPoint& pt1, const FW_CPoint& pt2)
  183. {
  184.     FW_CLineShape::RenderLine(gc, pt1, pt2, ink, style);        
  185. }
  186.  
  187. //----------------------------------------------------------------------------------------
  188. // CLineShape::HitTest
  189. //----------------------------------------------------------------------------------------
  190.  
  191. FW_Boolean CLineShape::HitTest(Environment *ev, FW_CGraphicContext& gc, const FW_CMouseEvent& theMouseEvent) const
  192. {
  193.     // We should convert the point to shape coordinate
  194.     FW_CPoint mouse = theMouseEvent.GetMousePosition(ev, FW_CMouseEvent::kFrame);
  195.     ODFacet* facet = theMouseEvent.GetFacet(ev);
  196.     FW_CFrame* frame = FW_CFrame::ODtoFWFrame(ev, facet->GetFrame(ev));
  197.     frame->GetContentView(ev)->FrameToViewContent(ev, mouse);
  198.  
  199.     FW_CLineShape lineShape(fStart.x, fStart.y, fEnd.x, fEnd.y);
  200.     return lineShape.HitTest(gc, mouse, FW_IntToFixed(2));
  201. }
  202.  
  203. //----------------------------------------------------------------------------------------
  204. // CLineShape::GetClipRegion
  205. //----------------------------------------------------------------------------------------
  206.  
  207. void CLineShape::GetClipRegion(Environment* ev, ODShape* clipRegion)
  208. {
  209.     ODShape* lineShape = ::FW_CreateLineODShape(ev, fStart, fEnd, GetPenSize());
  210.     clipRegion->CopyFrom(ev, lineShape);
  211.     lineShape->Release(ev);
  212. }
  213.  
  214. //----------------------------------------------------------------------------------------
  215. // CLineShape::CreateShapeOutline
  216. //----------------------------------------------------------------------------------------
  217.  
  218. ODShape* CLineShape::CreateShapeOutline(Environment *ev)
  219. {
  220.     ODShape* dragShape = ::FW_NewODShape(ev);        // We don't acquire it because we return it
  221.     GetClipRegion(ev, dragShape);
  222.     return dragShape;
  223. }
  224.  
  225. //----------------------------------------------------------------------------------------
  226. // CLineShape::GetHandleCenter
  227. //----------------------------------------------------------------------------------------
  228.  
  229. void CLineShape::GetHandleCenter(short whichHandle, FW_CPoint& center) const
  230. {
  231.     center = whichHandle == kInTopLeftCorner ? fStart : fEnd;
  232. }
  233.  
  234. //----------------------------------------------------------------------------------------
  235. // CLineShape::ResizeFeedback
  236. //----------------------------------------------------------------------------------------
  237.  
  238. void CLineShape::ResizeFeedback(FW_CGraphicContext& gc, const FW_CInk& ink, const FW_CStyle& style, 
  239.                                 short whichHandle, const FW_CPoint& mouseLoc)
  240. {
  241.     FW_CRect srcRect, dstRect;
  242.     GetMapRects(whichHandle, mouseLoc, srcRect, dstRect);
  243.     
  244.     FW_CPoint pt1(fStart);
  245.     FW_CPoint pt2(fEnd);
  246.  
  247.     pt1.Map(srcRect, dstRect);
  248.     pt2.Map(srcRect, dstRect);
  249.  
  250.     if (srcRect.left == srcRect.right)
  251.     {
  252.         pt1.x = dstRect.left;
  253.         pt2.x = dstRect.right;
  254.     }
  255.     
  256.     if (srcRect.top == srcRect.bottom)
  257.     {
  258.         pt1.y = dstRect.top;
  259.         pt2.y = dstRect.bottom;
  260.     }
  261.     
  262.     OutlineShape(gc, ink, style, pt1, pt2);
  263. }
  264.  
  265. //----------------------------------------------------------------------------------------
  266. // CLineShape::GetMapRects
  267. //----------------------------------------------------------------------------------------
  268.  
  269. void CLineShape::GetMapRects(short whichHandle, const FW_CPoint& mouseLoc,
  270.                                 FW_CRect& srcRect, FW_CRect& dstRect)
  271. {
  272.     srcRect.Set(fStart.x, fStart.y, fEnd.x, fEnd.y);
  273.     dstRect = srcRect;
  274.  
  275.     switch (whichHandle)
  276.     {
  277.         case kInTopLeftCorner:
  278.             dstRect.left = mouseLoc.x;
  279.             dstRect.top = mouseLoc.y;
  280.             break;
  281.         case kInBottomRightCorner:
  282.             dstRect.right = mouseLoc.x;
  283.             dstRect.bottom = mouseLoc.y;
  284.             break;
  285.     }
  286. }
  287.  
  288. //----------------------------------------------------------------------------------------
  289. // CLineShape::MapShape
  290. //----------------------------------------------------------------------------------------
  291.  
  292. void CLineShape::MapShape(Environment *ev, CDrawPart* part, const FW_CRect& srcRect, const FW_CRect& dstRect)
  293. {
  294. FW_UNUSED(srcRect);
  295.  
  296.     CheckPromise(ev, part);
  297.     
  298.     ClearCache();
  299.  
  300.     FW_CRect realSrcRect;
  301.     realSrcRect[FW_kTopLeft] = fStart;
  302.     realSrcRect[FW_kBotRight] = fEnd;
  303.  
  304.     fStart.Map(realSrcRect, dstRect);
  305.     fEnd.Map(realSrcRect, dstRect);
  306.  
  307.     if (realSrcRect.left == realSrcRect.right)
  308.     {
  309.         fStart.x = dstRect.left;
  310.         fEnd.x = dstRect.right;
  311.     }
  312.     
  313.     if (realSrcRect.top == realSrcRect.bottom)
  314.     {
  315.         fStart.y = dstRect.top;
  316.         fEnd.y = dstRect.bottom;
  317.     }
  318. }
  319.  
  320. //----------------------------------------------------------------------------------------
  321. // CLineShape::OffsetShape
  322. //----------------------------------------------------------------------------------------
  323.  
  324. void CLineShape::OffsetShape(Environment *ev, FW_Fixed xDelta, FW_Fixed yDelta)
  325. {
  326.     ClearCache();
  327.  
  328.     fStart.x += xDelta;
  329.     fStart.y += yDelta;
  330.  
  331.     fEnd.x += xDelta;
  332.     fEnd.y += yDelta;
  333. }
  334.  
  335. //----------------------------------------------------------------------------------------
  336. // CLineShape::SetShapeGeometry
  337. //----------------------------------------------------------------------------------------
  338.  
  339. void CLineShape::SetShapeGeometry(const FW_CPoint& anchorPoint,  const FW_CPoint& currentPoint)
  340. {
  341.     fStart = anchorPoint;
  342.     fEnd = currentPoint;
  343. }
  344.  
  345. //----------------------------------------------------------------------------------------
  346. // CLineShape::Flatten
  347. //----------------------------------------------------------------------------------------
  348.  
  349. void CLineShape::Flatten(FW_CWritableStream& archive)
  350. {    
  351.     CBaseShape::Flatten(archive);
  352.     
  353.     archive << fStart;
  354.     archive << fEnd;
  355. }
  356.  
  357. //----------------------------------------------------------------------------------------
  358. // CLineShape::Read
  359. //----------------------------------------------------------------------------------------
  360.  
  361. void* CLineShape::Read(FW_CReadableStream& stream, FW_ClassTypeConstant type)
  362. {
  363. FW_UNUSED(type);
  364.     return FW_NEW(CLineShape, (stream));
  365. }
  366.  
  367. //----------------------------------------------------------------------------------------
  368. // CLineShape::GetRectGeometry
  369. //----------------------------------------------------------------------------------------
  370.  
  371. FW_CRect CLineShape::GetRectGeometry() const
  372. {
  373.     FW_CRect rect(fStart, fEnd);
  374.     rect.Sort();
  375.     return rect;
  376. }
  377.  
  378.