1ffefcba2edcdd54160e280cdfea698931b9f1b9
[dana/openbox.git] / otk / rect.hh
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 #ifndef   __rect_hh
3 #define   __rect_hh
4
5 extern "C" {
6 #include <X11/Xlib.h>
7 }
8
9 #include <vector>
10
11 namespace otk {
12
13 //! The Rect class defines a rectangle in the plane.
14 class Rect {
15 public:
16   //! Constructs an invalid Rect
17   inline Rect(void) : _x1(0), _y1(0), _x2(0), _y2(0) { }
18   //! Constructs a Rect
19   /*!
20     @param x The x component of the point defining the top left corner of the 
21              rectangle
22     @param y The y component of the point defining the top left corner of the
23              rectangle
24     @param w The width of the rectangle
25     @param h The height of the rectangle
26   */
27   inline Rect(int x, int y, unsigned int w, unsigned int h)
28     : _x1(x), _y1(y), _x2(w + x - 1), _y2(h + y - 1) { }
29   //! Constructs a Rect from an XRectangle
30   inline explicit Rect(const XRectangle& xrect)
31     : _x1(xrect.x), _y1(xrect.y), _x2(xrect.width + xrect.x - 1),
32       _y2(xrect.height + xrect.y - 1) { }
33
34   //! Returns the left coordinate of the Rect. Identical to Rect::x.
35   inline int left(void) const { return _x1; }
36   //! Returns the top coordinate of the Rect. Identical to Rect::y.
37   inline int top(void) const { return _y1; }
38   //! Returns the right coordinate of the Rect
39   inline int right(void) const { return _x2; }
40   //! Returns the bottom coordinate of the Rect
41   inline int bottom(void) const { return _y2; }
42
43   //! The x component of the point defining the top left corner of the Rect
44   inline int x(void) const { return _x1; }
45   //! The y component of the point defining the top left corner of the Rect
46   inline int y(void) const { return _y1; }
47   //! Sets the x coordinate of the Rect.
48   /*!
49     @param x The new x component of the point defining the top left corner of
50              the rectangle
51   */
52   void setX(int x);
53   //! Sets the y coordinate of the Rect.
54   /*!
55     @param y The new y component of the point defining the top left corner of
56              the rectangle
57   */
58   void setY(int y);
59   //! Sets the x and y coordinates of the Rect.
60   /*!
61     @param x The new x component of the point defining the top left corner of
62              the rectangle
63     @param y The new y component of the point defining the top left corner of
64              the rectangle
65   */
66   void setPos(int x, int y);
67
68   //! The width of the Rect
69   inline unsigned int width(void) const { return _x2 - _x1 + 1; }
70   //! The height of the Rect
71   inline unsigned int height(void) const { return _y2 - _y1 + 1; }
72   //! Sets the width of the Rect
73   /*!
74     @param w The new width of the rectangle
75   */
76   void setWidth(unsigned int w);
77   //! Sets the height of the Rect
78   /*!
79     @param h The new height of the rectangle
80   */
81   void setHeight(unsigned int h);
82   //! Sets the width of the Rect.
83   /*!
84     @param w The new width of the rectangle
85     @param h The new height of the rectangle
86   */
87   void setSize(unsigned int w, unsigned int h);
88
89   //! Sets the position and size of the Rect
90   /*!
91     @param x The new x component of the point defining the top left corner of
92              the rectangle
93     @param y The new y component of the point defining the top left corner of
94              the rectangle
95     @param w The new width of the rectangle
96     @param h The new height of the rectangle
97    */
98   void setRect(int x, int y, unsigned int w, unsigned int h);
99
100   //! Sets the position of all 4 sides of the Rect
101   /*!
102     @param l The new left coordinate of the rectangle
103     @param t The new top coordinate of the rectangle
104     @param r The new right coordinate of the rectangle
105     @param b The new bottom coordinate of the rectangle
106    */
107   void setCoords(int l, int t, int r, int b);
108
109   //! Determines if two Rect objects are equal
110   /*!
111     The rectangles are considered equal if they are in the same position and
112     are the same size.
113   */
114   inline bool operator==(const Rect &a)
115   { return _x1 == a._x1 && _y1 == a._y1 && _x2 == a._x2 && _y2 == a._y2; }
116   //! Determines if two Rect objects are inequal
117   /*!
118     @see operator==
119   */
120   inline bool operator!=(const Rect &a) { return ! operator==(a); }
121
122   //! Returns the union of two Rect objects
123   /*!
124     The union of the rectangles will consist of the maximimum area that the two
125     rectangles can make up.
126     @param a A second Rect object to form a union with.
127    */
128   Rect operator|(const Rect &a) const;
129   //! Returns the intersection of two Rect objects
130   /*!
131     The intersection of the rectangles will consist of just the area where the
132     two rectangles overlap.
133     @param A second Rect object to form an intersection with.
134     @return The intersection between this Rect and the one passed to the
135             function
136   */
137   Rect operator&(const Rect &a) const;
138   //! Sets the Rect to the union of itself with another Rect object
139   /*!
140     The union of the rectangles will consist of the maximimum area that the two
141     rectangles can make up.
142     @param a A second Rect object to form a union with.
143     @return The union between this Rect and the one passed to the function
144    */
145   inline Rect &operator|=(const Rect &a) { *this = *this | a; return *this; }
146   //! Sets the Rect to the intersection of itself with another Rect object
147   /*!
148     The intersection of the rectangles will consist of just the area where the
149     two rectangles overlap.
150     @param A second Rect object to form an intersection with.
151   */
152   inline Rect &operator&=(const Rect &a) { *this = *this & a; return *this; }
153
154   //! Returns if the Rect is valid
155   /*!
156     A rectangle is valid only if its right and bottom coordinates are larger
157     than its left and top coordinates (i.e. it does not have a negative width
158     or height).
159     @return true if the Rect is valid; otherwise, false
160   */
161   inline bool valid(void) const { return _x2 > _x1 && _y2 > _y1; }
162
163   //! Determines if this Rect intersects another Rect
164   /*!
165     The rectangles intersect if any part of them overlaps.
166     @param a Another Rect object to compare this Rect with
167     @return true if the Rect objects overlap; otherwise, false
168   */
169   bool intersects(const Rect &a) const;
170   //! Determines if this Rect contains a point
171   /*!
172     The rectangle contains the point if it falls within the rectangle's
173     boundaries.
174     @param x The x coordinate of the point to operate on
175     @param y The y coordinate of the point to operate on
176     @return true if the point is contained within this Rect; otherwise, false
177   */
178   bool contains(int x, int y) const;
179   //! Determines if this Rect contains another Rect entirely
180   /*!
181     This rectangle contains the second rectangle if it is entirely within this
182     rectangle's boundaries.
183     @param a The Rect to test for containment inside of this Rect
184     @return true if the second Rect is contained within this Rect; otherwise,
185             false
186   */
187   bool contains(const Rect &a) const;
188
189 private:
190   //! The left coordinate of the Rect
191   int _x1;
192   //! The top coordinate of the Rect
193   int _y1;
194   //! The right coordinate of the Rect
195   int _x2;
196   //! The bottom coordinate of the Rect
197   int _y2;
198 };
199
200 //! A list for Rect objects
201 typedef std::vector<Rect> RectList;
202
203 }
204
205 #endif // __rect_hh