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 *****
29 /** \file ghost/intern/GHOST_Debug.h
31 * Macro's used in GHOST debug target.
37 #include "GHOST_Types.h"
41 * Implements rectangle functionality.
42 * The four extreme coordinates are stored as left, top, right and bottom.
43 * To be valid, a rectangle should have a left coordinate smaller than or equal to right.
44 * To be valid, a rectangle should have a top coordinate smaller than or equal to bottom.
45 * @author Maarten Gribnau
53 * Constructs a rectangle with the given values.
54 * @param l requested left coordinate of the rectangle
55 * @param t requested top coordinate of the rectangle
56 * @param r requested right coordinate of the rectangle
57 * @param b requested bottom coordinate of the rectangle
59 GHOST_Rect(GHOST_TInt32 l=0, GHOST_TInt32 t=0, GHOST_TInt32 r=0, GHOST_TInt32 b=0)
60 : 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) {}
72 virtual ~GHOST_Rect() {};
75 * Access to rectangle width.
76 * @return width of the rectangle
78 virtual inline GHOST_TInt32 getWidth() const;
81 * Access to rectangle height.
82 * @return height of the rectangle
84 virtual inline GHOST_TInt32 getHeight() const;
87 * Sets all members of the rectangle.
88 * @param l requested left coordinate of the rectangle
89 * @param t requested top coordinate of the rectangle
90 * @param r requested right coordinate of the rectangle
91 * @param b requested bottom coordinate of the rectangle
93 virtual inline void set(GHOST_TInt32 l, GHOST_TInt32 t, GHOST_TInt32 r, GHOST_TInt32 b);
96 * Returns whether this rectangle is empty.
97 * Empty rectangles are rectangles that have width==0 and/or height==0.
98 * @return boolean value (true==empty rectangle)
100 virtual inline bool isEmpty() const;
103 * Returns whether this rectangle is valid.
104 * Valid rectangles are rectangles that have m_l <= m_r and m_t <= m_b. Thus, emapty rectangles are valid.
105 * @return boolean value (true==valid rectangle)
107 virtual inline bool isValid() const;
110 * Grows (or shrinks the rectangle).
111 * The method avoids negative insets making the rectangle invalid
112 * @param i The amount of offset given to each extreme (negative values shrink the rectangle).
114 virtual void inset(GHOST_TInt32 i);
117 * Does a union of the rectangle given and this rectangle.
118 * The result is stored in this rectangle.
119 * @param r The rectangle that is input for the union operation.
121 virtual inline void unionRect(const GHOST_Rect& r);
124 * Grows the rectangle to included a point.
125 * @param x The x-coordinate of the point.
126 * @param y The y-coordinate of the point.
128 virtual inline void unionPoint(GHOST_TInt32 x, GHOST_TInt32 y);
131 * Grows the rectangle to included a point.
132 * @param x The x-coordinate of the point.
133 * @param y The y-coordinate of the point.
135 virtual inline void wrapPoint(GHOST_TInt32 &x, GHOST_TInt32 &y, GHOST_TInt32 ofs);
138 * Returns whether the point is inside this rectangle.
139 * Point on the boundary is considered inside.
140 * @param x x-coordinate of point to test.
141 * @param y y-coordinate of point to test.
142 * @return boolean value (true if point is inside).
144 virtual inline bool isInside(GHOST_TInt32 x, GHOST_TInt32 y) const;
147 * Returns whether the rectangle is inside this rectangle.
148 * @param r rectangle to test.
149 * @return visibility (not, partially or fully visible).
151 virtual GHOST_TVisibility getVisibility(GHOST_Rect& r) const;
154 * Sets rectangle members.
155 * Sets rectangle members such that it is centered at the given location.
156 * @param cx requested center x-coordinate of the rectangle
157 * @param cy requested center y-coordinate of the rectangle
159 virtual void setCenter(GHOST_TInt32 cx, GHOST_TInt32 cy);
162 * Sets rectangle members.
163 * Sets rectangle members such that it is centered at the given location,
164 * with the width requested.
165 * @param cx requested center x-coordinate of the rectangle
166 * @param cy requested center y-coordinate of the rectangle
167 * @param w requested width of the rectangle
168 * @param h requested height of the rectangle
170 virtual void setCenter(GHOST_TInt32 cx, GHOST_TInt32 cy, GHOST_TInt32 w, GHOST_TInt32 h);
174 * Updates the rectangle given such that it will fit within this one.
175 * This can result in an empty rectangle.
176 * @param r the rectangle to clip
177 * @return whether clipping has occurred
179 virtual bool clip(GHOST_Rect& r) const;
181 /** Left coordinate of the rectangle */
183 /** Top coordinate of the rectangle */
185 /** Right coordinate of the rectangle */
187 /** Bottom coordinate of the rectangle */
190 #ifdef WITH_CXX_GUARDEDALLOC
192 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GHOST:GHOST_Rect"); }
193 void operator delete( void *mem ) { MEM_freeN(mem); }
198 inline GHOST_TInt32 GHOST_Rect::getWidth() const
203 inline GHOST_TInt32 GHOST_Rect::getHeight() const
208 inline void GHOST_Rect::set(GHOST_TInt32 l, GHOST_TInt32 t, GHOST_TInt32 r, GHOST_TInt32 b)
210 m_l = l; m_t = t; m_r = r; m_b = b;
213 inline bool GHOST_Rect::isEmpty() const
215 return (getWidth() == 0) || (getHeight() == 0);
218 inline bool GHOST_Rect::isValid() const
220 return (m_l <= m_r) && (m_t <= m_b);
223 inline void GHOST_Rect::unionRect(const GHOST_Rect& r)
225 if (r.m_l < m_l) m_l = r.m_l;
226 if (r.m_r > m_r) m_r = r.m_r;
227 if (r.m_t < m_t) m_t = r.m_t;
228 if (r.m_b > m_b) m_b = r.m_b;
231 inline void GHOST_Rect::unionPoint(GHOST_TInt32 x, GHOST_TInt32 y)
233 if (x < m_l) m_l = x;
234 if (x > m_r) m_r = x;
235 if (y < m_t) m_t = y;
236 if (y > m_b) m_b = y;
239 inline void GHOST_Rect::wrapPoint(GHOST_TInt32 &x, GHOST_TInt32 &y, GHOST_TInt32 ofs)
241 GHOST_TInt32 w= getWidth();
242 GHOST_TInt32 h= getHeight();
244 /* highly unlikely but avoid eternal loop */
245 if(w-ofs*2 <= 0 || h-ofs*2 <= 0)
247 while(x-ofs < m_l) x+= w-(ofs*2);
248 while(y-ofs < m_t) y+= h-(ofs*2);
249 while(x+ofs > m_r) x-= w-(ofs*2);
250 while(y+ofs > m_b) y-= h-(ofs*2);
253 inline bool GHOST_Rect::isInside(GHOST_TInt32 x, GHOST_TInt32 y) const
255 return (x >= m_l) && (x <= m_r) && (y >= m_t) && (y <= m_b);
258 #endif // _H_GHOST_Rect