home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / rectangl.h < prev    next >
C/C++ Source or Header  |  1995-04-08  |  6KB  |  206 lines

  1.  
  2.  
  3. #ifndef _rectangle_h_
  4. #define _rectangle_h_
  5.  
  6.  
  7.  
  8.  
  9.  
  10. /*
  11.  *
  12.  *          Copyright (C) 1994, M. A. Sridhar
  13.  *  
  14.  *
  15.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  16.  *     to copy, modify or distribute this software  as you see fit,
  17.  *     and to use  it  for  any  purpose, provided   this copyright
  18.  *     notice and the following   disclaimer are included  with all
  19.  *     copies.
  20.  *
  21.  *                        DISCLAIMER
  22.  *
  23.  *     The author makes no warranties, either expressed or implied,
  24.  *     with respect  to  this  software, its  quality, performance,
  25.  *     merchantability, or fitness for any particular purpose. This
  26.  *     software is distributed  AS IS.  The  user of this  software
  27.  *     assumes all risks  as to its quality  and performance. In no
  28.  *     event shall the author be liable for any direct, indirect or
  29.  *     consequential damages, even if the  author has been  advised
  30.  *     as to the possibility of such damages.
  31.  *
  32.  */
  33.  
  34.  
  35.  
  36.  
  37. #if defined(__GNUC__)
  38. #pragma interface
  39. #endif
  40.  
  41.  
  42. #include "ui/point.h"
  43. #include "ui/graphic.h"
  44. #include "base/sequence.h"
  45.  
  46. // This is an abstraction of a rectangle at a given position in
  47. // two-dimensional space.
  48.  
  49. // A convenient definition of a rectangle
  50. struct UI_RectangleStruct {
  51.     long x, y, w, h;  // h = height, w = width
  52. };
  53.  
  54.  
  55. class CL_EXPORT UI_DrawingSurface;
  56.  
  57.  
  58. class CL_EXPORT UI_Rectangle: public UI_GraphicObject {
  59.  
  60. public:
  61.  
  62.     //
  63.     //-------------------Construction------------------
  64.     //
  65.     UI_Rectangle (const UI_Point& topLeft, long width, long height);
  66.  
  67.     UI_Rectangle (long x, long y, long width, long height);
  68.  
  69.     UI_Rectangle (const UI_RectangleStruct&);
  70.     
  71.     UI_Rectangle ();
  72.  
  73.     UI_Rectangle (const UI_Rectangle& );
  74.  
  75.     ~UI_Rectangle ();
  76.  
  77.     //
  78.     // --------------------Query-------------------------
  79.     //
  80.  
  81.     UI_Rectangle BoundingRectangle() const {return *this; };
  82.  
  83.     UI_Point Center () const;
  84.     
  85.     long TopLeftX () const {return _origin.XCoord();};
  86.  
  87.     long TopLeftY () const {return _origin.YCoord();};
  88.  
  89.     long BottomRightX () const {return _origin.XCoord() + _width - 1;};
  90.  
  91.     long BottomRightY () const {return _origin.YCoord() + _height - 1;};
  92.  
  93.     long Top () const {return _origin.YCoord();}
  94.  
  95.     long Left () const {return _origin.XCoord();}
  96.  
  97.     long Bottom () const {return _origin.YCoord() + _height - 1;}
  98.  
  99.     long Right () const {return _origin.XCoord() + _width - 1;}
  100.     
  101.     const UI_Point& Origin () const {return _origin;};
  102.  
  103.     UI_Point TopLeft () const {return _origin;}
  104.  
  105.     UI_Point TopRight () const   {return _origin + UI_Point (_width-1, 0);};
  106.     
  107.     UI_Point BottomLeft () const {return _origin + UI_Point (0, _height-1);};
  108.     
  109.     UI_Point BottomRight () const
  110.         {return _origin + UI_Point (_width-1, _height-1);};
  111.  
  112.     long Height () const;
  113.  
  114.     long Width () const;
  115.  
  116.     long Area () const;
  117.  
  118.     UI_HitTest HitTest (const UI_Point& p) const;
  119.     // Override inherited virtual method.
  120.  
  121.     bool OnBoundary (const UI_Point& p) const;
  122.     // Return TRUE if {\tt p} is on the boundary of this rectangle, and
  123.     // FALSE otherwise.
  124.  
  125.     bool Includes (const UI_Point& p) const;
  126.     // Return TRUE if {\tt p} is inside or on the boundary of this
  127.     // rectangle, and  FALSE otherwise.
  128.     
  129.     bool IntersectsBoundary (const UI_Rectangle& r) const;
  130.  
  131.     bool IsContainedIn (const UI_Rectangle& r) const;
  132.  
  133.     CL_Sequence<UI_Point> RowMajor (long m, long n) const;
  134.     // Imagine that the surface of this rectangle is ``tiled'' by a matrix
  135.     // of sub-rectangles, each of which is $m$ units wide and $n$ units
  136.     // high, and that the top left corner of the top left rectangle in the
  137.     // matrix coincides with the top left corner of this rectangle. Return
  138.     // the top left corners of the sub-rectangles, in row-major order, as a
  139.     // sequence of points. The width of this rectangle must be divisible by
  140.     // $m$, and the height must be divisible by $n$.
  141.     
  142.  
  143.     // ------------------------ Geometric operations -----------------
  144.  
  145.  
  146.     void Origin  (const UI_Point& o);
  147.     // Set the origin to the given point, thus moving the rectangle.
  148.  
  149.     void AddToHeight (long increment);
  150.     // Add this value to the height. The parameter may be negative. This
  151.     // method will refuse any change that causes the height to fall below zero.
  152.     
  153.     void AddToWidth (long increment);
  154.     // Add this value to the width. The parameter may be negative. This
  155.     // method will refuse a change that causes the width to fall below zero.
  156.     
  157.     UI_Rectangle operator+  (const UI_Point&) const;
  158.  
  159.     void operator+= (const UI_Point& p) {*this = *this + p;};
  160.  
  161.     bool ReshapeTo (const UI_Point& p1, const UI_Point& p2);
  162.  
  163.     // ------------------------- Drawing -------------------------------
  164.     
  165.     bool DrawOn (UI_DrawingSurface& sfc,
  166.                  const UI_Point& p = UI_Point (0, 0)) const;
  167.     // Override the method inherited from GraphicObject.
  168.     
  169.     bool Fill   (UI_DrawingSurface& s) const;
  170.         
  171.     // ----------------------- Basic operations ------------------
  172.     
  173.     void operator= (const CL_Object& );
  174.  
  175.     void operator= (const UI_Rectangle& rect);
  176.     
  177.     bool operator== (const CL_Object& r) const;
  178.  
  179.     bool operator== (const UI_Rectangle& r) const;
  180.  
  181.     CL_String AsString () const;
  182.     // Return a string that looks like \verb|"(23, 15, 2, 3)"| (for x, y,
  183.     // w, h).
  184.  
  185.     const char* ClassName () const {return "UI_Rectangle";};
  186.  
  187. protected:
  188.  
  189.     UI_Point _origin; 
  190.     long     _height;
  191.     long     _width;
  192.  
  193.  
  194. public:
  195. #if defined(__MS_WINDOWS__)
  196.     struct tagRECT AsMSRect () const;
  197.     // [MS-Windows-specific: internal use only]
  198.  
  199. #endif
  200. };
  201.  
  202.  
  203.  
  204.  
  205. #endif
  206.