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