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