3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
30 * Declaration of GHOST_Rect rectangle class.
36 #include "GHOST_Types.h"
40 * Implements rectangle functionality.
41 * The four extreme coordinates are stored as left, top, right and bottom.
42 * To be valid, a rectangle should have a left coordinate smaller than or equal to right.
43 * To be valid, a rectangle should have a top coordinate smaller than or equal to bottom.
44 * @author Maarten Gribnau
52 * Constructs a rectangle with the given values.
53 * @param l requested left coordinate of the rectangle
54 * @param t requested top coordinate of the rectangle
55 * @param r requested right coordinate of the rectangle
56 * @param b requested bottom coordinate of the rectangle
58 GHOST_Rect(GHOST_TInt32 l=0, GHOST_TInt32 t=0, GHOST_TInt32 r=0, GHOST_TInt32 b=0)
59 : m_l(l), m_t(t), m_r(r), m_b(b) {}
63 * @param r rectangle to copy
65 GHOST_Rect(const GHOST_Rect& r)
66 : m_l(r.m_l), m_t(r.m_t), m_r(r.m_r), m_b(r.m_b) {}
71 virtual ~GHOST_Rect() {};
74 * Access to rectangle width.
75 * @return width of the rectangle
77 virtual inline GHOST_TInt32 getWidth() const;
80 * Access to rectangle height.
81 * @return height of the rectangle
83 virtual inline GHOST_TInt32 getHeight() const;
86 * Sets all members of the rectangle.
87 * @param l requested left coordinate of the rectangle
88 * @param t requested top coordinate of the rectangle
89 * @param r requested right coordinate of the rectangle
90 * @param b requested bottom coordinate of the rectangle
92 virtual inline void set(GHOST_TInt32 l, GHOST_TInt32 t, GHOST_TInt32 r, GHOST_TInt32 b);
95 * Returns whether this rectangle is empty.
96 * Empty rectangles are rectangles that have width==0 and/or height==0.
97 * @return boolean value (true==empty rectangle)
99 virtual inline bool isEmpty() const;
102 * Returns whether this rectangle is valid.
103 * Valid rectangles are rectangles that have m_l <= m_r and m_t <= m_b. Thus, emapty rectangles are valid.
104 * @return boolean value (true==valid rectangle)
106 virtual inline bool isValid() const;
109 * Grows (or shrinks the rectangle).
110 * The method avoids negative insets making the rectangle invalid
111 * @param i The amount of offset given to each extreme (negative values shrink the rectangle).
113 virtual void inset(GHOST_TInt32 i);
116 * Does a union of the rectangle given and this rectangle.
117 * The result is stored in this rectangle.
118 * @param r The rectangle that is input for the union operation.
120 virtual inline void unionRect(const GHOST_Rect& r);
123 * Grows the rectangle to included a point.
124 * @param x The x-coordinate of the point.
125 * @param y The y-coordinate of the point.
127 virtual inline void unionPoint(GHOST_TInt32 x, GHOST_TInt32 y);
130 * Grows the rectangle to included a point.
131 * @param x The x-coordinate of the point.
132 * @param y The y-coordinate of the point.
134 virtual inline void wrapPoint(GHOST_TInt32 &x, GHOST_TInt32 &y, GHOST_TInt32 ofs);
137 * Returns whether the point is inside this rectangle.
138 * Point on the boundary is considered inside.
139 * @param x x-coordinate of point to test.
140 * @param y y-coordinate of point to test.
141 * @return boolean value (true if point is inside).
143 virtual inline bool isInside(GHOST_TInt32 x, GHOST_TInt32 y) const;
146 * Returns whether the rectangle is inside this rectangle.
147 * @param r rectangle to test.
148 * @return visibility (not, partially or fully visible).
150 virtual GHOST_TVisibility getVisibility(GHOST_Rect& r) const;
153 * Sets rectangle members.
154 * Sets rectangle members such that it is centered at the given location.
155 * @param cx requested center x-coordinate of the rectangle
156 * @param cy requested center y-coordinate of the rectangle
158 virtual void setCenter(GHOST_TInt32 cx, GHOST_TInt32 cy);
161 * Sets rectangle members.
162 * Sets rectangle members such that it is centered at the given location,
163 * with the width requested.
164 * @param cx requested center x-coordinate of the rectangle
165 * @param cy requested center y-coordinate of the rectangle
166 * @param w requested width of the rectangle
167 * @param h requested height of the rectangle
169 virtual void setCenter(GHOST_TInt32 cx, GHOST_TInt32 cy, GHOST_TInt32 w, GHOST_TInt32 h);
173 * Updates the rectangle given such that it will fit within this one.
174 * This can result in an empty rectangle.
175 * @param r the rectangle to clip
176 * @return whether clipping has occurred
178 virtual bool clip(GHOST_Rect& r) const;
180 /** Left coordinate of the rectangle */
182 /** Top coordinate of the rectangle */
184 /** Right coordinate of the rectangle */
186 /** Bottom coordinate of the rectangle */
189 #ifdef WITH_CXX_GUARDEDALLOC
191 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GHOST:GHOST_Rect"); }
192 void operator delete( void *mem ) { MEM_freeN(mem); }
197 inline GHOST_TInt32 GHOST_Rect::getWidth() const
202 inline GHOST_TInt32 GHOST_Rect::getHeight() const
207 inline void GHOST_Rect::set(GHOST_TInt32 l, GHOST_TInt32 t, GHOST_TInt32 r, GHOST_TInt32 b)
209 m_l = l; m_t = t; m_r = r; m_b = b;
212 inline bool GHOST_Rect::isEmpty() const
214 return (getWidth() == 0) || (getHeight() == 0);
217 inline bool GHOST_Rect::isValid() const
219 return (m_l <= m_r) && (m_t <= m_b);
222 inline void GHOST_Rect::unionRect(const GHOST_Rect& r)
224 if (r.m_l < m_l) m_l = r.m_l;
225 if (r.m_r > m_r) m_r = r.m_r;
226 if (r.m_t < m_t) m_t = r.m_t;
227 if (r.m_b > m_b) m_b = r.m_b;
230 inline void GHOST_Rect::unionPoint(GHOST_TInt32 x, GHOST_TInt32 y)
232 if (x < m_l) m_l = x;
233 if (x > m_r) m_r = x;
234 if (y < m_t) m_t = y;
235 if (y > m_b) m_b = y;
238 inline void GHOST_Rect::wrapPoint(GHOST_TInt32 &x, GHOST_TInt32 &y, GHOST_TInt32 ofs)
240 GHOST_TInt32 w= getWidth();
241 GHOST_TInt32 h= getHeight();
243 /* highly unlikely but avoid eternal loop */
244 if(w-ofs*2 <= 0 || h-ofs*2 <= 0)
246 while(x-ofs < m_l) x+= w-(ofs*2);
247 while(y-ofs < m_t) y+= h-(ofs*2);
248 while(x+ofs > m_r) x-= w-(ofs*2);
249 while(y+ofs > m_b) y-= h-(ofs*2);
252 inline bool GHOST_Rect::isInside(GHOST_TInt32 x, GHOST_TInt32 y) const
254 return (x >= m_l) && (x <= m_r) && (y >= m_t) && (y <= m_b);
257 #endif // _H_GHOST_Rect