]> icculus.org git repositories - mikachu/openbox.git/blob - otk/rect.hh
get rid of unsigneds
[mikachu/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, int w, int h)
28     : _x1(x), _y1(y), _x2(w + x - 1), _y2(h + y - 1) { }
29   //! Constructs a Rect from 2 Point objects
30   /*!
31     @param location The point defining the top left corner of the rectangle
32     @param size The width and height of the rectangle
33   */
34   inline Rect(const Point &location, const Point &size)
35     : _x1(location.x()), _y1(location.y()),
36       _x2(size.x() + location.x() - 1), _y2(size.y() + location.y() - 1) { }
37   //! Constructs a Rect from an XRectangle
38   inline explicit Rect(const XRectangle& xrect)
39     : _x1(xrect.x), _y1(xrect.y), _x2(xrect.width + xrect.x - 1),
40       _y2(xrect.height + xrect.y - 1) { }
41
42   //! Returns the left coordinate of the Rect. Identical to Rect::x.
43   inline int left(void) const { return _x1; }
44   //! Returns the top coordinate of the Rect. Identical to Rect::y.
45   inline int top(void) const { return _y1; }
46   //! Returns the right coordinate of the Rect
47   inline int right(void) const { return _x2; }
48   //! Returns the bottom coordinate of the Rect
49   inline int bottom(void) const { return _y2; }
50
51   //! The x component of the point defining the top left corner of the Rect
52   inline int x(void) const { return _x1; }
53   //! The y component of the point defining the top left corner of the Rect
54   inline int y(void) const { return _y1; }
55   //! Returns the Point that defines the top left corner of the rectangle
56   inline Point location() const { return Point(_x1, _y1); }
57
58   //! Sets the x coordinate of the Rect.
59   /*!
60     @param x The new x component of the point defining the top left corner of
61              the rectangle
62   */
63   void setX(int x);
64   //! Sets the y coordinate of the Rect.
65   /*!
66     @param y The new y component of the point defining the top left corner of
67              the rectangle
68   */
69   void setY(int y);
70   //! Sets the x and y coordinates of the Rect.
71   /*!
72     @param x The new x component of the point defining the top left corner of
73              the rectangle
74     @param y The new y component of the point defining the top left corner of
75              the rectangle
76   */
77   void setPos(int x, int y);
78   //! Sets the x and y coordinates of the Rect.
79   /*!
80     @param location The point defining the top left corner of the rectangle.
81   */
82   void setPos(const Point &location);
83
84   //! The width of the Rect
85   inline int width(void) const { return _x2 - _x1 + 1; }
86   //! The height of the Rect
87   inline int height(void) const { return _y2 - _y1 + 1; }
88   //! Returns the size of the Rect
89   inline Point size() const { return Point(_x2 - _x1 + 1, _y2 - _y1 + 1); }
90
91   //! Sets the width of the Rect
92   /*!
93     @param w The new width of the rectangle
94   */
95   void setWidth(int w);
96   //! Sets the height of the Rect
97   /*!
98     @param h The new height of the rectangle
99   */
100   void setHeight(int h);
101   //! Sets the size of the Rect.
102   /*!
103     @param w The new width of the rectangle
104     @param h The new height of the rectangle
105   */
106   void setSize(int w, int h);
107   //! Sets the size of the Rect.
108   /*!
109     @param size The new size of the rectangle
110   */
111   void setSize(const Point &size);
112
113   //! Sets the position and size of the Rect
114   /*!
115     @param x The new x component of the point defining the top left corner of
116              the rectangle
117     @param y The new y component of the point defining the top left corner of
118              the rectangle
119     @param w The new width of the rectangle
120     @param h The new height of the rectangle
121    */
122   void setRect(int x, int y, int w, int h);
123   //! Sets the position and size of the Rect
124   /*!
125     @param location The new point defining the top left corner of the rectangle
126     @param size The new size of the rectangle
127    */
128   void setRect(const Point &location, const Point &size);
129
130   //! Sets the position of all 4 sides of the Rect
131   /*!
132     @param l The new left coordinate of the rectangle
133     @param t The new top coordinate of the rectangle
134     @param r The new right coordinate of the rectangle
135     @param b The new bottom coordinate of the rectangle
136    */
137   void setCoords(int l, int t, int r, int b);
138   //! Sets the position of all 4 sides of the Rect
139   /*!
140     @param tl The new point at the top left of the rectangle
141     @param br The new point at the bottom right of the rectangle
142    */
143   void setCoords(const Point &tl, const Point &br);
144
145   //! Determines if two Rect objects are equal
146   /*!
147     The rectangles are considered equal if they are in the same position and
148     are the same size.
149   */
150   inline bool operator==(const Rect &a)
151   { return _x1 == a._x1 && _y1 == a._y1 && _x2 == a._x2 && _y2 == a._y2; }
152   //! Determines if two Rect objects are inequal
153   /*!
154     @see operator==
155   */
156   inline bool operator!=(const Rect &a) { return ! operator==(a); }
157
158   //! Returns the union of two Rect objects
159   /*!
160     The union of the rectangles will consist of the maximimum area that the two
161     rectangles can make up.
162     @param a A second Rect object to form a union with.
163    */
164   Rect operator|(const Rect &a) const;
165   //! Returns the intersection of two Rect objects
166   /*!
167     The intersection of the rectangles will consist of just the area where the
168     two rectangles overlap.
169     @param a A second Rect object to form an intersection with.
170     @return The intersection between this Rect and the one passed to the
171             function
172   */
173   Rect operator&(const Rect &a) const;
174   //! Sets the Rect to the union of itself with another Rect object
175   /*!
176     The union of the rectangles will consist of the maximimum area that the two
177     rectangles can make up.
178     @param a A second Rect object to form a union with.
179     @return The union between this Rect and the one passed to the function
180    */
181   inline Rect &operator|=(const Rect &a) { *this = *this | a; return *this; }
182   //! Sets the Rect to the intersection of itself with another Rect object
183   /*!
184     The intersection of the rectangles will consist of just the area where the
185     two rectangles overlap.
186     @param a A second Rect object to form an intersection with.
187   */
188   inline Rect &operator&=(const Rect &a) { *this = *this & a; return *this; }
189
190   //! Returns if the Rect is valid
191   /*!
192     A rectangle is valid only if its right and bottom coordinates are larger
193     than its left and top coordinates (i.e. it does not have a negative width
194     or height).
195     @return true if the Rect is valid; otherwise, false
196   */
197   inline bool valid(void) const { return _x2 > _x1 && _y2 > _y1; }
198
199   //! Determines if this Rect intersects another Rect
200   /*!
201     The rectangles intersect if any part of them overlaps.
202     @param a Another Rect object to compare this Rect with
203     @return true if the Rect objects overlap; otherwise, false
204   */
205   bool intersects(const Rect &a) const;
206   //! Determines if this Rect contains a point
207   /*!
208     The rectangle contains the point if it falls within the rectangle's
209     boundaries.
210     @param x The x coordinate of the point to operate on
211     @param y The y coordinate of the point to operate on
212     @return true if the point is contained within this Rect; otherwise, false
213   */
214   bool contains(int x, int y) const;
215   //! Determines if this Rect contains another Rect entirely
216   /*!
217     This rectangle contains the second rectangle if it is entirely within this
218     rectangle's boundaries.
219     @param a The Rect to test for containment inside of this Rect
220     @return true if the second Rect is contained within this Rect; otherwise,
221             false
222   */
223   bool contains(const Rect &a) const;
224
225 private:
226   //! The left coordinate of the Rect
227   int _x1;
228   //! The top coordinate of the Rect
229   int _y1;
230   //! The right coordinate of the Rect
231   int _x2;
232   //! The bottom coordinate of the Rect
233   int _y2;
234 };
235
236 //! A list for Rect objects
237 typedef std::vector<Rect> RectList;
238
239 }
240
241 #endif // __rect_hh