2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file ghost/intern/GHOST_Debug.h
30 * Macro's used in GHOST debug target.
33 #ifndef __GHOST_RECT_H__
34 #define __GHOST_RECT_H__
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)
64 * \param r rectangle to copy
66 GHOST_Rect(const GHOST_Rect& r)
67 : m_l(r.m_l), m_t(r.m_t), m_r(r.m_r), m_b(r.m_b)
73 virtual ~GHOST_Rect() {}
76 * Access to rectangle width.
77 * \return width of the rectangle
79 virtual inline GHOST_TInt32 getWidth() const;
82 * Access to rectangle height.
83 * \return height of the rectangle
85 virtual inline GHOST_TInt32 getHeight() const;
88 * Sets all members of the rectangle.
89 * \param l requested left coordinate of the rectangle
90 * \param t requested top coordinate of the rectangle
91 * \param r requested right coordinate of the rectangle
92 * \param b requested bottom coordinate of the rectangle
94 virtual inline void set(GHOST_TInt32 l, GHOST_TInt32 t, GHOST_TInt32 r, GHOST_TInt32 b);
97 * Returns whether this rectangle is empty.
98 * Empty rectangles are rectangles that have width==0 and/or height==0.
99 * \return boolean value (true==empty rectangle)
101 virtual inline bool isEmpty() const;
104 * Returns whether this rectangle is valid.
105 * Valid rectangles are rectangles that have m_l <= m_r and m_t <= m_b. Thus, emapty rectangles are valid.
106 * \return boolean value (true==valid rectangle)
108 virtual inline bool isValid() const;
111 * Grows (or shrinks the rectangle).
112 * The method avoids negative insets making the rectangle invalid
113 * \param i The amount of offset given to each extreme (negative values shrink the rectangle).
115 virtual void inset(GHOST_TInt32 i);
118 * Does a union of the rectangle given and this rectangle.
119 * The result is stored in this rectangle.
120 * \param r The rectangle that is input for the union operation.
122 virtual inline void unionRect(const GHOST_Rect& r);
125 * Grows the rectangle to included a point.
126 * \param x The x-coordinate of the point.
127 * \param y The y-coordinate of the point.
129 virtual inline void unionPoint(GHOST_TInt32 x, GHOST_TInt32 y);
132 * Grows the rectangle to included a point.
133 * \param x The x-coordinate of the point.
134 * \param y The y-coordinate of the point.
136 virtual inline void wrapPoint(GHOST_TInt32 &x, GHOST_TInt32 &y, GHOST_TInt32 ofs);
139 * Returns whether the point is inside this rectangle.
140 * Point on the boundary is considered inside.
141 * \param x x-coordinate of point to test.
142 * \param y y-coordinate of point to test.
143 * \return boolean value (true if point is inside).
145 virtual inline bool isInside(GHOST_TInt32 x, GHOST_TInt32 y) const;
148 * Returns whether the rectangle is inside this rectangle.
149 * \param r rectangle to test.
150 * \return visibility (not, partially or fully visible).
152 virtual GHOST_TVisibility getVisibility(GHOST_Rect& r) const;
155 * Sets rectangle members.
156 * Sets rectangle members such that it is centered at the given location.
157 * \param cx requested center x-coordinate of the rectangle
158 * \param cy requested center y-coordinate of the rectangle
160 virtual void setCenter(GHOST_TInt32 cx, GHOST_TInt32 cy);
163 * Sets rectangle members.
164 * Sets rectangle members such that it is centered at the given location,
165 * with the width requested.
166 * \param cx requested center x-coordinate of the rectangle
167 * \param cy requested center y-coordinate of the rectangle
168 * \param w requested width of the rectangle
169 * \param h requested height of the rectangle
171 virtual void setCenter(GHOST_TInt32 cx, GHOST_TInt32 cy, GHOST_TInt32 w, GHOST_TInt32 h);
175 * Updates the rectangle given such that it will fit within this one.
176 * This can result in an empty rectangle.
177 * \param r the rectangle to clip
178 * \return whether clipping has occurred
180 virtual bool clip(GHOST_Rect& r) const;
182 /** Left coordinate of the rectangle */
184 /** Top coordinate of the rectangle */
186 /** Right coordinate of the rectangle */
188 /** Bottom coordinate of the rectangle */
191 #ifdef WITH_CXX_GUARDEDALLOC
192 MEM_CXX_CLASS_ALLOC_FUNCS("GHOST:GHOST_Rect")
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) {
248 while (x - ofs < m_l) x += w - (ofs * 2);
249 while (y - ofs < m_t) y += h - (ofs * 2);
250 while (x + ofs > m_r) x -= w - (ofs * 2);
251 while (y + ofs > m_b) y -= h - (ofs * 2);
254 inline bool GHOST_Rect::isInside(GHOST_TInt32 x, GHOST_TInt32 y) const
256 return (x >= m_l) && (x <= m_r) && (y >= m_t) && (y <= m_b);
259 #endif // __GHOST_RECT_H__