home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / OS / FWGraphx / Sources / FWRect.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  12.0 KB  |  427 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWRect.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/28/94
  7. //
  8. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef FWRECT_H
  13. #include "FWRect.h"
  14. #endif
  15.  
  16. #ifndef FWSTREAM_H
  17. #include "FWStream.h"
  18. #endif
  19.  
  20. // ----- OpenDoc Includes -----
  21.  
  22. #ifndef _XMPTYPES_
  23. #include <XMPTypes.h>
  24. #endif
  25.  
  26. // ----- Macintosh Includes -----
  27.  
  28. #if defined(FW_BUILD_MAC) && !defined(__TOOLUTILS__)
  29. #include <ToolUtils.h>
  30. #endif
  31.  
  32. #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
  33. #include <math routines.h>
  34. #endif
  35.  
  36. //==============================================================================
  37. //    •• RunTime Info
  38. //==============================================================================
  39.  
  40. #ifdef FW_BUILD_MAC
  41. #pragma segment fwgraphx
  42. #endif
  43.  
  44. //==============================================================================
  45. //    •• struct FW_CRect
  46. //==============================================================================
  47.  
  48. inline XMPCoordinate Min( XMPCoordinate a, XMPCoordinate b )    {return a<b ?a :b;}
  49. inline XMPCoordinate Max( XMPCoordinate a, XMPCoordinate b )    {return a>b ?a :b;}
  50.  
  51. //------------------------------------------------------------------------------
  52. //    • FW_CRect::FW_CRect
  53. //------------------------------------------------------------------------------
  54.  
  55. FW_CRect::FW_CRect(const FW_CPoint &topLeft, const FW_CPoint &botRight)
  56. {
  57.     left = topLeft.x;        right = botRight.x;
  58.     top = topLeft.y;        bottom = botRight.y;
  59. }
  60.  
  61. //------------------------------------------------------------------------------
  62. //    • FW_CRect::FW_CRect
  63. //------------------------------------------------------------------------------
  64.  
  65. FW_CRect::FW_CRect(const FW_CPoint &topLeft, XMPCoordinate width, XMPCoordinate height )
  66. {
  67.     left = topLeft.x;        right = left + width;
  68.     top  = topLeft.y;        bottom= top + height;
  69. }
  70.  
  71. //------------------------------------------------------------------------------
  72. //    • FW_CRect:: operator=
  73. //------------------------------------------------------------------------------
  74.  
  75. FW_CRect& FW_CRect:: operator=(const XMPRect& xmpRect)
  76. {
  77.     left = xmpRect.left;    right = xmpRect.right;
  78.     top  = xmpRect.top;        bottom= xmpRect.bottom;
  79.  
  80.     return *this;
  81. }
  82.  
  83. //------------------------------------------------------------------------------
  84. //    • FW_CRect::FW_CRect
  85. //------------------------------------------------------------------------------
  86.  
  87. FW_CRect::FW_CRect(const FW_SPlatformRect &plfmRect)
  88. {
  89. #ifdef FW_BUILD_MAC
  90.     left = ff(plfmRect.left);        right = ff(plfmRect.right);
  91.     top  = ff(plfmRect.top);        bottom= ff(plfmRect.bottom);
  92. #endif
  93.  
  94. #ifdef FW_BUILD_WIN
  95. #endif
  96. }
  97.  
  98. //------------------------------------------------------------------------------
  99. //    • FW_CRect:: operator=
  100. //------------------------------------------------------------------------------
  101.  
  102. FW_CRect& FW_CRect:: operator=(const FW_SPlatformRect &plfmRect)
  103. {
  104. #ifdef FW_BUILD_MAC
  105.     left = ff(plfmRect.left);        right = ff(plfmRect.right);
  106.     top  = ff(plfmRect.top);        bottom= ff(plfmRect.bottom);
  107. #endif
  108.  
  109. #ifdef FW_BUILD_WIN
  110. #endif
  111.  
  112.     return *this;
  113. }
  114.  
  115. //------------------------------------------------------------------------------
  116. //    • FW_CRect::Set
  117. //------------------------------------------------------------------------------
  118.  
  119. void FW_CRect::Set(XMPCoordinate l, XMPCoordinate t, XMPCoordinate r, XMPCoordinate b)
  120. {
  121.     left = l;                right = r;
  122.     top  = t;                bottom= b;
  123. }
  124.  
  125. //------------------------------------------------------------------------------
  126. //    • FW_CRect::Set
  127. //------------------------------------------------------------------------------
  128.  
  129. void FW_CRect::Set(const FW_CPoint &topLeft, XMPCoordinate width, XMPCoordinate height)
  130. {
  131.     left = topLeft.x;                right = left+width;
  132.     top  = topLeft.y;                bottom= top +height;
  133. }
  134.  
  135. //------------------------------------------------------------------------------
  136. //     • FW_CRect::SetInt
  137. //------------------------------------------------------------------------------
  138.  
  139. void FW_CRect::SetInt(short l, short t, short r, short b)
  140. {
  141.     left = ff(l);            right = ff(r);
  142.     top  = ff(t);            bottom= ff(b);
  143. }
  144.  
  145. //------------------------------------------------------------------------------
  146. //    • FW_CRect::Offset
  147. //------------------------------------------------------------------------------
  148.  
  149. void FW_CRect::Offset(XMPCoordinate x, XMPCoordinate y)
  150. {
  151.     left += x;                right += x;
  152.     top  += y;                bottom+= y;
  153. }
  154.  
  155. //------------------------------------------------------------------------------
  156. //    • FW_CRect::Offset
  157. //------------------------------------------------------------------------------
  158.  
  159. void FW_CRect::Offset(const FW_CPoint &pt)
  160. {
  161.     left += pt.x;            right += pt.x;
  162.     top  += pt.y;            bottom+= pt.y;
  163. }
  164.  
  165. //------------------------------------------------------------------------------
  166. //    • FW_CRect::Place
  167. //------------------------------------------------------------------------------
  168.  
  169. void FW_CRect::Place(XMPCoordinate x, XMPCoordinate y)
  170. {
  171.     right += x - left;
  172.     left = x;
  173.     bottom += y - top;
  174.     top = x;
  175. }
  176.  
  177. //------------------------------------------------------------------------------
  178. //    • FW_CRect::Place
  179. //------------------------------------------------------------------------------
  180.  
  181. void FW_CRect::Place(const FW_CPoint &pt)
  182. {
  183.     right += pt.x - left;
  184.     left = pt.x;
  185.     bottom += pt.y - top;
  186.     top = pt.y;
  187. }
  188.  
  189. //------------------------------------------------------------------------------
  190. //    • FW_CRect::Inset
  191. //------------------------------------------------------------------------------
  192.  
  193. void FW_CRect::Inset(XMPCoordinate x, XMPCoordinate y )
  194. {
  195.     left += x;
  196.     right -= x;
  197.     top += y;
  198.     bottom -= y;
  199. }
  200.  
  201. //------------------------------------------------------------------------------
  202. //    • FW_CRect::Clear
  203. //------------------------------------------------------------------------------
  204.  
  205. void FW_CRect::Clear( )
  206. {
  207.     left = right = top = bottom = 0;
  208. }
  209.  
  210. //------------------------------------------------------------------------------
  211. //    • FW_CRect:: operator&=
  212. //------------------------------------------------------------------------------
  213.  
  214. void FW_CRect:: operator&=(const FW_CRect &r)
  215. {
  216.     left = Max(left,r.left);    right = Min(right, r.right);
  217.     top  = Max(top, r.top);        bottom= Min(bottom,r.bottom);
  218. }
  219.  
  220.  
  221. //------------------------------------------------------------------------------
  222. //    • FW_CRect:: operator|=
  223. //------------------------------------------------------------------------------
  224.  
  225. void FW_CRect:: operator|= (const FW_CRect &r)
  226. {
  227.     left = Min(left,r.left);
  228.     right= Max(right,r.right);
  229.     top  = Min(top,r.top);
  230.     bottom=Max(bottom,r.bottom);
  231. }
  232.  
  233. //------------------------------------------------------------------------------
  234. //    • operator|= FW_CPoint
  235. //------------------------------------------------------------------------------
  236.  
  237. void FW_CRect:: operator|= (const FW_CPoint &pt)
  238. {
  239.     left = Min(left,pt.x);
  240.     right= Max(right,pt.x);
  241.     top  = Min(top,pt.y);
  242.     bottom=Max(bottom,pt.y);
  243. }
  244.  
  245. //------------------------------------------------------------------------------
  246. //    • FW_CRect::AsPlatformRect
  247. //------------------------------------------------------------------------------
  248.  
  249. void FW_CRect::AsPlatformRect(FW_SPlatformRect &plfmRect) const
  250. {
  251. #ifdef FW_BUILD_MAC
  252.     SetRect(&plfmRect, FixedToInt(left), FixedToInt(top), FixedToInt(right), FixedToInt(bottom));
  253. #endif
  254.  
  255. #ifdef FW_BUILD_WIN
  256. #endif
  257. }
  258.  
  259. //------------------------------------------------------------------------------
  260. //    • FW_CRect::operator FW_SPlatformRect() const
  261. //------------------------------------------------------------------------------
  262.  
  263. FW_CRect::operator FW_SPlatformRect() const
  264. {
  265.     FW_SPlatformRect plfmRect;    
  266.  
  267. #ifdef FW_BUILD_MAC
  268.     SetRect(&plfmRect, FixedToInt(left), FixedToInt(top), FixedToInt(right), FixedToInt(bottom));
  269. #endif
  270.  
  271. #ifdef FW_BUILD_WIN
  272. #endif
  273.  
  274.     return plfmRect;
  275. }
  276.  
  277. //------------------------------------------------------------------------------
  278. // FW_CRect::IsEmpty
  279. //------------------------------------------------------------------------------
  280.  
  281. FW_Boolean FW_CRect::IsEmpty() const
  282. {
  283.     return right<=left || bottom<=top;
  284. }
  285.  
  286. //------------------------------------------------------------------------------
  287. //    • FW_CRect::Contains
  288. //------------------------------------------------------------------------------
  289.  
  290. FW_Boolean FW_CRect::Contains(const FW_CPoint &pt) const
  291. {
  292.     return left<=pt.x && pt.x<right
  293.         && top <=pt.y && pt.y<bottom;
  294. }
  295.  
  296. //------------------------------------------------------------------------------
  297. //    • FW_CRect::Contains
  298. //------------------------------------------------------------------------------
  299.  
  300. FW_Boolean FW_CRect::Contains( const FW_CRect &r) const
  301. {
  302.     return left<=r.left && r.right<=right
  303.         && top <=r.top  && r.bottom<=bottom;
  304. }
  305.  
  306. //------------------------------------------------------------------------------
  307. //    • FW_CRect::operator==
  308. //------------------------------------------------------------------------------
  309.  
  310. FW_Boolean FW_CRect::operator==(const FW_CRect &r) const
  311. {
  312.     return (left == r.left && top == r.top  && right == r.right  && bottom == r.bottom);
  313. }
  314.  
  315. //------------------------------------------------------------------------------
  316. //    • FW_CRect::IsIntersecting
  317. //------------------------------------------------------------------------------
  318.  
  319. FW_Boolean FW_CRect::IsIntersecting(const FW_CRect &r) const
  320. {
  321.     return Max(left,r.left) < Min(right,r.right)
  322.         && Max(top,r.top) < Min(bottom,r.bottom);
  323. }
  324.  
  325. //------------------------------------------------------------------------------
  326. //    • FW_CRect::Intersects
  327. //------------------------------------------------------------------------------
  328.  
  329. FW_CRect FW_CRect::Intersects(const FW_CRect &r) const
  330. {
  331.     FW_CRect result;
  332.     
  333.     result.left = Max(left,r.left);
  334.     result.top = Max(top,r.top);
  335.     result.right = Min(right,r.right);
  336.     result.bottom = Min(bottom,r.bottom);
  337.     
  338.     if (result.IsEmpty())
  339.         result.Set(0,0,0,0);
  340.     
  341.     return result;
  342. }
  343.  
  344. //------------------------------------------------------------------------------
  345. //    • FW_CRect::Sort
  346. //------------------------------------------------------------------------------
  347.  
  348. void FW_CRect::Sort()
  349. {
  350.     if (left > right)
  351.     {
  352.         XMPCoordinate temp = left;
  353.         left = right;
  354.         right = temp;
  355.     }
  356.     if (top > bottom)
  357.     {
  358.         XMPCoordinate temp = top;
  359.         top = bottom;
  360.         bottom = temp;
  361.     }
  362. }
  363.  
  364. //-------------------------------------------------------------------------
  365. //    • FW_CRect::Map
  366. //-------------------------------------------------------------------------
  367.  
  368. void FW_CRect::Map(const FW_CRect& srcRect, const FW_CRect& dstRect)
  369. {
  370.     XMPCoordinate ratio = ::FixDiv(right - left, srcRect.right - srcRect.left);
  371.     
  372.     left += ::FixMul(dstRect.left - srcRect.left, ratio);
  373.     right += ::FixMul(dstRect.right - srcRect.right, ratio);
  374.     
  375.     ratio = ::FixDiv(bottom - top, srcRect.bottom - srcRect.top);
  376.     
  377.     top += ::FixMul(dstRect.top - srcRect.top, ratio);
  378.     bottom += ::FixMul(dstRect.bottom - srcRect.bottom, ratio);
  379. }
  380.  
  381. //------------------------------------------------------------------------------
  382. //    • FW_CRect::operator[]
  383. //------------------------------------------------------------------------------
  384.  
  385. FW_CPoint& FW_CRect::operator[](FW_PointSelector sel)
  386. {
  387.     if (sel == FW_kTopLeft)
  388.         return *((FW_CPoint *) &left);
  389.     else
  390.         return *((FW_CPoint *) &right);
  391. }
  392.  
  393. //------------------------------------------------------------------------------
  394. //    • FW_CRect::operator[]
  395. //------------------------------------------------------------------------------
  396.  
  397. const FW_CPoint& FW_CRect::operator[](FW_PointSelector sel) const
  398. {
  399.     if (sel == FW_kTopLeft)
  400.         return *((FW_CPoint *) &left);
  401.     else
  402.         return *((FW_CPoint *) &right);
  403. }
  404.  
  405. //----------------------------------------------------------------------------------------
  406. //    • FW_CRect::Flatten
  407. //----------------------------------------------------------------------------------------
  408.  
  409. void FW_CRect::Flatten(FW_CWritableStream& stream)
  410. {
  411.     stream.Write((char*)this, sizeof(XMPRect));
  412. }
  413.  
  414. //----------------------------------------------------------------------------------------
  415. //    • FW_CRect::Unflatten
  416. //----------------------------------------------------------------------------------------
  417.  
  418. void FW_CRect::Unflatten(FW_CReadableStream& stream)
  419. {
  420.     stream.Read((char*)this, sizeof(XMPRect));
  421. }
  422.  
  423.  
  424.  
  425.  
  426.  
  427.