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 / OS / FWODMisc / Include / FWRect.h < prev    next >
Encoding:
Text File  |  1996-08-16  |  13.4 KB  |  373 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWRect.h
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef FWRECT_H
  11. #define FWRECT_H
  12.  
  13. #ifndef FWPOINT_H
  14. #include "FWPoint.h"
  15. #endif
  16.  
  17. #ifndef SLPTRECT_H
  18. #include "SLPtRect.h"
  19. #endif
  20.     
  21. //========================================================================================
  22. //    Typedefs
  23. //========================================================================================
  24.  
  25. typedef unsigned short FW_PointSelector;
  26.  
  27. const FW_PointSelector FW_kTopLeft = 0;
  28. const FW_PointSelector FW_kBotRight = 1;
  29.  
  30. //========================================================================================
  31. //    Forward Class Declarations
  32. //========================================================================================
  33.  
  34. class FW_CRect;
  35.  
  36. //========================================================================================
  37. //    Global
  38. //========================================================================================
  39.  
  40. extern const FW_CRect        FW_kZeroRect;
  41.  
  42. //========================================================================================
  43. //    Global operators << and >>
  44. //========================================================================================
  45.  
  46. FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_SRect& rect);
  47. FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_SRect& rect);
  48.  
  49. //========================================================================================
  50. //    class FW_CPlatformRect
  51. //========================================================================================
  52.  
  53. class FW_CPlatformRect : public FW_SPlatformRect
  54. {
  55. public:
  56.     FW_CPlatformRect();
  57.     
  58.     FW_CPlatformRect(FW_PlatformCoordinate l, FW_PlatformCoordinate t, 
  59.                     FW_PlatformCoordinate r, FW_PlatformCoordinate b);
  60.                     
  61.     FW_CPlatformRect(const FW_CPlatformPoint& topLeft, const FW_CPlatformPoint& botRight);
  62.     
  63.     FW_CPlatformRect(const FW_SPlatformRect& plfmRect);
  64.     FW_CPlatformRect(const FW_CPlatformRect& plfmRect);
  65.     FW_CPlatformRect(const FW_SRect& rect);
  66.         
  67.     FW_CPlatformRect& operator=(const FW_CPlatformRect& plfmRect);
  68.     FW_CPlatformRect& operator=(const FW_SPlatformRect& plfmRect);
  69.     FW_CPlatformRect& operator=(const FW_SRect& rect);
  70.     
  71.     const FW_CPlatformPoint&     TopLeft() const;
  72.     FW_CPlatformPoint&            TopLeft();
  73.     const FW_CPlatformPoint&     BotRight() const;
  74.     FW_CPlatformPoint&            BotRight();
  75.  
  76.     FW_CPlatformPoint             TopRight() const;        // Note: it is impossible to return non-const reference
  77.     FW_CPlatformPoint             BotLeft() const;        // Note: it is impossible to return non-const reference
  78.     
  79.     FW_Boolean            operator==(const FW_CPlatformRect& rect) const;
  80.     FW_Boolean            operator!=(const FW_CPlatformRect& rect) const;
  81.  
  82.     void Set(FW_PlatformCoordinate l, FW_PlatformCoordinate t, 
  83.             FW_PlatformCoordinate r, FW_PlatformCoordinate b);
  84. };
  85.  
  86. inline FW_CPlatformRect::FW_CPlatformRect() {left = right = top = bottom = 0;}
  87.  
  88. //========================================================================================
  89. // Transformation utilities
  90. //========================================================================================
  91.  
  92. void            FW_Transform(Environment* ev, FW_SRect& rect, ODTransform* trasnsform);
  93. void            FW_InverseTransform(Environment* ev, FW_SRect& rect, ODTransform* transform);
  94.  
  95. FW_CRect        FW_TransformCopy(Environment* ev, const FW_SRect& rect, ODTransform* trasnsform);
  96. FW_CRect        FW_InverseTransformCopy(Environment* ev, const FW_SRect& rect, ODTransform* transform);
  97.  
  98. //========================================================================================
  99. // class FW_CRect
  100. //========================================================================================
  101.  
  102. class FW_CRect : public FW_SRect
  103. {
  104. //----------------------------------------------------------------------------------------
  105. //    Constructors/Destructors
  106. //
  107. public:
  108.     FW_CRect();
  109.     FW_CRect(FW_Fixed l, FW_Fixed t, FW_Fixed r, FW_Fixed b);
  110.  
  111.     FW_CRect(const FW_SPlatformRect& plfmRect);
  112.     FW_CRect(const FW_SRect& rect);
  113.     FW_CRect(const FW_CRect& rect);
  114.     FW_CRect(const ODRect& rect);
  115.     
  116.     FW_CRect(FW_CReadableStream& stream);
  117.     
  118.     FW_CRect(const FW_SPoint& topLeft, const FW_SPoint& botRight);
  119.     FW_CRect(const FW_SPoint& startPoint, const FW_SPoint& endPoint, FW_Fixed penSize);
  120.     FW_CRect(const FW_SPoint& topLeft, FW_Fixed width, FW_Fixed height);
  121.  
  122. //----------------------------------------------------------------------------------------
  123. //    operators
  124. //
  125. public:
  126.     // ----- Assignement -----    
  127.     FW_CRect& operator=(const FW_SRect& rect);
  128.     FW_CRect& operator=(const FW_CRect& rect);
  129.     FW_CRect& operator=(const ODRect& odRect);
  130.     FW_CRect& operator=(const FW_SPlatformRect& plfmRect);
  131.  
  132.     // ----- Modification -----
  133.     void    operator&=(const FW_SRect& rect);                // Intersect with rectangle
  134.     void    operator|=(const FW_SRect& rect);                // Union with rectangle
  135.     void    operator|=(const FW_SPoint& point);                // Expand to fit point
  136.     
  137.     // ----- Conversion -----
  138. //     It is too dangerous to have automatic conversion. Use AsPlatformRect.
  139. //    operator FW_SPlatformRect() const;
  140.  
  141.     operator ODRect() const
  142.         { return * (ODRect*) this; }
  143.     
  144.     operator ODRect*()
  145.         { return (ODRect*) this; }
  146.     
  147.     operator FW_SRect*() const
  148.         { return (FW_SRect*) this; }
  149.  
  150.     // ----- Array -----
  151.     FW_CPoint& operator[](FW_PointSelector sel);        
  152.     const FW_CPoint& operator[](FW_PointSelector sel) const;
  153.     
  154.     // ----- Testing -----
  155.     FW_Boolean    operator==( const FW_SRect& ) const;
  156.     FW_Boolean    operator!=( const FW_SRect &r ) const
  157.                             {return !(*this==r);}
  158.     
  159. //----------------------------------------------------------------------------------------
  160. //    New API
  161. //
  162. public:
  163.     // ----- Conversion -----
  164.     FW_CPlatformRect    AsPlatformRect() const;
  165.  
  166.     // ----- Modification -----
  167.     void            Clear();
  168.     void            Set(FW_Fixed left, FW_Fixed top, FW_Fixed right, FW_Fixed bottom);
  169.     void            Set(const FW_SPoint& topLeft, FW_Fixed width, FW_Fixed height);
  170.     void            Set(const FW_SPoint& topLeft, const FW_SPoint& botRight);
  171.     void            SetInt(short left, short top, short right, short bottom);
  172.     void            Offset(FW_Fixed x, FW_Fixed y);
  173.     void            Offset(const FW_SPoint& offset);
  174.     void            Place(FW_Fixed x, FW_Fixed y);
  175.     void            Place(const FW_SPoint& offset);
  176.     void            Inset(FW_Fixed x, FW_Fixed y);
  177.     void            Inset(FW_Fixed inset)
  178.                         {Inset(inset, inset);}
  179.     void            Map(const FW_SRect& srcRect, const FW_SRect& dstRect);
  180.     void            PlaceInCenter(const FW_SRect& srcRect);
  181.     
  182.     void            Sort();
  183.     
  184.     FW_CRect&        operator+=(const FW_SPoint& pt);
  185.     FW_CRect        operator+(const FW_SPoint& pt) const;
  186.  
  187.     FW_CRect&        operator-=(const FW_SPoint& pt);
  188.     FW_CRect        operator-(const FW_SPoint& pt) const;
  189.  
  190.     // ----- OpenDoc Transform -----
  191.         // Modify the rectangle
  192.     void            Transform(Environment* ev, ODTransform* transform)
  193.                         { FW_Transform(ev, *this, transform); }
  194.     void            InverseTransform(Environment* ev, ODTransform* transform)
  195.                         { FW_InverseTransform(ev, *this, transform); }
  196.  
  197.         // Return a modified copy
  198.     FW_CRect        TransformCopy(Environment* ev, ODTransform* transform) const
  199.                         { return FW_TransformCopy(ev, *this, transform); }
  200.     FW_CRect        InverseTransformCopy(Environment* ev, ODTransform* transform) const
  201.                         { return FW_InverseTransformCopy(ev, *this, transform); }
  202.  
  203.     // ----- Accessors -----
  204.     
  205.     const FW_CPoint&     TopLeft() const
  206.                                 {return *(FW_CPoint*)&left;}
  207.     FW_CPoint&            TopLeft()
  208.                                 {return *(FW_CPoint*)&left;}
  209.     const FW_CPoint&     BotRight() const
  210.                                 {return *(FW_CPoint*)&right;}
  211.     FW_CPoint&            BotRight()
  212.                                 {return *(FW_CPoint*)&right;}
  213.     FW_Fixed            Width() const
  214.                                 {return FW_Fixed(right-left);}
  215.     FW_Fixed            Height() const
  216.                                 {return FW_Fixed(bottom-top);}
  217.     FW_CPoint            Size() const;
  218.     
  219.     FW_CPoint             TopRight() const;        // Note: it is impossible to return a non-const reference
  220.     FW_CPoint             BotLeft() const;        // Note: it is impossible to return a non-const reference
  221.  
  222.     // ----- Testing -----
  223.     FW_Boolean     IsEmpty() const;
  224.     FW_Boolean     Contains(const FW_SPoint& point) const;
  225.     FW_Boolean     Contains(const FW_SRect& rect) const;
  226.     FW_Boolean     IsIntersecting(const FW_SRect& rect) const;
  227.     
  228.     // ----- Operation on Rectangle -----
  229.     void         Intersection(const FW_SRect& rect);
  230.     void         Intersection(const FW_SRect& rect1, const FW_SRect& rect2);
  231.  
  232.     void         Union(const FW_SRect& rect);
  233.     void         Union(const FW_SRect& rect1, const FW_SRect& rect2);
  234. };
  235.  
  236. //========================================================================================
  237. //    Inlines
  238. //========================================================================================
  239.  
  240. //----------------------------------------------------------------------------------------
  241. //    FW_CPlatformRect::FW_CPlatformRect
  242. //----------------------------------------------------------------------------------------
  243. inline FW_CPlatformRect::FW_CPlatformRect(FW_PlatformCoordinate l, FW_PlatformCoordinate t, 
  244.                                             FW_PlatformCoordinate r, FW_PlatformCoordinate b)
  245. {
  246.     left = l; top = t; 
  247.     right = r; bottom = b;
  248.  
  249. //----------------------------------------------------------------------------------------
  250. //    FW_CPlatformRect::FW_CPlatformRect
  251. //----------------------------------------------------------------------------------------
  252. inline FW_CPlatformRect::FW_CPlatformRect(const FW_CPlatformPoint& topLeft, 
  253.                                           const FW_CPlatformPoint& botRight)
  254. {
  255. #ifdef FW_BUILD_MAC
  256.     left = topLeft.h; top = topLeft.v; 
  257.     right = botRight.h; bottom = botRight.v;
  258. #endif
  259. #ifdef FW_BUILD_WIN
  260.     left = topLeft.x; top = topLeft.y; 
  261.     right = botRight.x; bottom = botRight.y;
  262. #endif
  263.  
  264. //----------------------------------------------------------------------------------------
  265. //    FW_CPlatformRect::FW_CPlatformRect
  266. //----------------------------------------------------------------------------------------
  267. inline FW_CPlatformRect::FW_CPlatformRect(const FW_CPlatformRect& plfmRect)
  268. {
  269.     left = plfmRect.left; top = plfmRect.top; 
  270.     right = plfmRect.right; bottom = plfmRect.bottom;
  271.  
  272. //----------------------------------------------------------------------------------------
  273. //    FW_CPlatformRect::FW_CPlatformRect
  274. //----------------------------------------------------------------------------------------
  275. inline FW_CPlatformRect::FW_CPlatformRect(const FW_SPlatformRect& plfmRect)
  276. {
  277.     left = plfmRect.left; top = plfmRect.top; 
  278.     right = plfmRect.right; bottom = plfmRect.bottom;
  279.  
  280. //----------------------------------------------------------------------------------------
  281. //    FW_CPlatformRect::operator=
  282. //----------------------------------------------------------------------------------------
  283. inline FW_CPlatformRect& FW_CPlatformRect::operator=(const FW_CPlatformRect& plfmRect)
  284. {
  285.     left = plfmRect.left; top = plfmRect.top; 
  286.     right = plfmRect.right; bottom = plfmRect.bottom;
  287.      return *this;
  288.  
  289. //----------------------------------------------------------------------------------------
  290. //    FW_CPlatformRect::operator=
  291. //----------------------------------------------------------------------------------------
  292. inline FW_CPlatformRect& FW_CPlatformRect::operator=(const FW_SPlatformRect& plfmRect)
  293. {
  294.     left = plfmRect.left; top = plfmRect.top; 
  295.     right = plfmRect.right; bottom = plfmRect.bottom;
  296.      return *this;
  297.  
  298. //----------------------------------------------------------------------------------------
  299. //    FW_CPlatformRect::Set
  300. //----------------------------------------------------------------------------------------
  301. inline void FW_CPlatformRect::Set(FW_PlatformCoordinate l, FW_PlatformCoordinate t, 
  302.                                     FW_PlatformCoordinate r, FW_PlatformCoordinate b)
  303. {
  304.     left = l; top = t; 
  305.     right = r; bottom = b;
  306. }    
  307.  
  308. //----------------------------------------------------------------------------------------
  309. //    FW_CPlatformRect::TopLeft
  310. //----------------------------------------------------------------------------------------
  311. inline const FW_CPlatformPoint& FW_CPlatformRect::TopLeft() const
  312. {
  313.     return *(FW_CPlatformPoint*)this;
  314. }
  315.  
  316. //----------------------------------------------------------------------------------------
  317. //    FW_CPlatformRect::TopLeft
  318. //----------------------------------------------------------------------------------------
  319. inline FW_CPlatformPoint& FW_CPlatformRect::TopLeft()
  320. {
  321.     return *(FW_CPlatformPoint*)this;
  322. }
  323.  
  324. //----------------------------------------------------------------------------------------
  325. //    FW_CPlatformRect::BotRight
  326. //----------------------------------------------------------------------------------------
  327. inline const FW_CPlatformPoint& FW_CPlatformRect::BotRight() const
  328. {
  329. #ifdef FW_BUILD_MAC
  330.     return *(FW_CPlatformPoint*)⊥
  331. #endif
  332. #ifdef FW_BUILD_WIN
  333.     return *(FW_CPlatformPoint*)&right;
  334. #endif
  335. }
  336.  
  337. //----------------------------------------------------------------------------------------
  338. //    FW_CPlatformRect::BotRight
  339. //----------------------------------------------------------------------------------------
  340. inline FW_CPlatformPoint& FW_CPlatformRect::BotRight()
  341. {
  342. #ifdef FW_BUILD_MAC
  343.     return *(FW_CPlatformPoint*)⊥
  344. #endif
  345. #ifdef FW_BUILD_WIN
  346.     return *(FW_CPlatformPoint*)&right;
  347. #endif
  348. }
  349.  
  350. //----------------------------------------------------------------------------------------
  351. //    FW_CRect:: operator&=
  352. //----------------------------------------------------------------------------------------
  353. inline void FW_CRect:: operator&=(const FW_SRect &r)
  354. {
  355.     Intersection(r);
  356. }
  357.  
  358. //----------------------------------------------------------------------------------------
  359. //    FW_CRect:: operator|=
  360. //----------------------------------------------------------------------------------------
  361. inline void FW_CRect:: operator|= (const FW_SRect &r)
  362. {
  363.     Union(r);
  364. }
  365.  
  366. #endif
  367.