NXWidgets: crect.cxx Source File

NXWidgets 1.19

crect.cxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/libnxwidgets/src/cbgwindow.cxx
3 *
4 * Copyright (C) 2012 Gregory Nutt. All rights reserved.
5 * Author: Gregory Nutt <gnutt@nuttx.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
18 * me be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************
35 *
36 * Portions of this package derive from Woopsi (http://woopsi.org/) and
37 * portions are original efforts. It is difficult to determine at this
38 * point what parts are original efforts and which parts derive from Woopsi.
39 * However, in any event, the work of Antony Dzeryn will be acknowledged
40 * in most NxWidget files. Thanks Antony!
41 *
42 * Copyright (c) 2007-2011, Antony Dzeryn
43 * All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions are met:
47 *
48 * * Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * * Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * * Neither the names "Woopsi", "Simian Zombie" nor the
54 * names of its contributors may be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
58 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
59 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
60 * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
61 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
62 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
64 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
66 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 *
68 ****************************************************************************/
69
70 /****************************************************************************
71 * Included Files
72 ****************************************************************************/
73
74 #include <nuttx/config.h>
75
76 #include <stdint.h>
77 #include <stdbool.h>
78
79 #include "crect.hxx"
80
81 /****************************************************************************
82 * Pre-Processor Definitions
83 ****************************************************************************/
84
85 /****************************************************************************
86 * Method Implementations
87 ****************************************************************************/
88
89 using namespace NXWidgets;
90
91 /**
92 * Constructor.
93 */
94
95 CRect::CRect(void)
96 {
97 m_pos.x = 0;
98 m_pos.y = 0;
99 m_size.w = 0;
100 m_size.h = 0;
101 }
102
103 /**
104 * Constructor.
105 *
106 * @param x The x coordinate of the rect.
107 * @param y The y coordinate of the rect.
108 * @param width The width of the rect.
109 * @param height The height of the rect.
110 */
111
112 CRect::CRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height)
113 {
114 m_pos.x = x;
115 m_pos.y = y;
116 m_size.w = width;
117 m_size.h = height;
118 }
119
120 /**
121 * Constructor.
122 *
123 * @param rect Pointer to an NX rectangle
124 */
125
126 CRect::CRect(FAR const nxgl_rect_s *rect)
127 {
128 setNxRect(rect);
129 }
130
131 /**
132 * Copy constructor.
133 *
134 * @param rect CRect to copy.
135 */
136
137 CRect::CRect(const CRect &rect)
138 {
139 m_pos.x = rect.getX();
140 m_pos.y = rect.getY();
141 m_size.w = rect.getWidth();
142 m_size.h = rect.getHeight();
143 }
144
145 /**
146 * Create a rect object from the supplied coordinates.
147 *
148 * @param x1 The x coordinate of the rect's top-left corner.
149 * @param y1 The y coordinate of the rect's top-left corner.
150 * @param x2 The x coordinate of the rect's bottom-right corner.
151 * @param y2 The y coordinate of the rect's bottom-right corner.
152 * @return A new rect.
153 */
154
155 CRect fromCoordinates(nxgl_coord_t x1, nxgl_coord_t y1,
156 nxgl_coord_t x2, nxgl_coord_t y2)
157 {
158 // Ensure x2 is the larger value
159
160 if (x2 < x1)
161 {
162 nxgl_coord_t swap = x1;
163 x1 = x2;
164 x2 = swap;
165 }
166 nxgl_coord_t width = x2 - x1 + 1;
167
168 // Ensure y2 is the larger value
169
170 if (y2 < y1)
171 {
172 nxgl_coord_t swap = y1;
173 y1 = y2;
174 y2 = swap;
175 }
176 nxgl_coord_t height = y2 - y1 + 1;
177
178 // Create the CRect instance
179
180 return CRect(x1, y1, width, height);
181 }
182
183 /**
184 * Set the x coordinate of the rect's bottom-right corner. If x2 is less
185 * than the rect's current x coordinate the method automatically adjusts
186 * the coordinates so that the rect's width is never negative. Changing this
187 * property will change the width of the rect.
188 *
189 * @param x2 The x coordinate of the rect's bottom-right corner.
190 */
191
192 void CRect::setX2(nxgl_coord_t x2)
193 {
194 // Ensure that x contains the smaller value
195
196 if (x2 < m_pos.x)
197 {
198 nxgl_coord_t swap = m_pos.x;
199 m_pos.x = x2;
200 x2 = swap;
201 }
202
203 // Then set the width
204
205 m_size.w = x2 - m_pos.x + 1;
206 }
207
208 /**
209 * Set the y coordinate of the rect's bottom-right corner. If y2 is less
210 * than the rect's current y coordinate the method automatically adjusts
211 * the coordinates so that the rect's height is never negative. Changing this
212 * property will change the height of the rect.
213 *
214 * @param y2 The y coordinate of the rect's bottom-right corner.
215 */
216
217 void CRect::setY2(nxgl_coord_t y2)
218 {
219 // Ensure that y contains the smaller value
220
221 if (y2 < m_pos.y)
222 {
223 nxgl_coord_t swap = m_pos.y;
224 m_pos.y = y2;
225 y2 = swap;
226 }
227
228 // Then set the height
229
230 m_size.h = y2 - m_pos.y + 1;
231 }
232
233 /**
234 * Populates dest with a rectangle representating the intersection
235 * of this rectangle and rect.
236 *
237 * @param rect The rectangle to intersect with this.
238 * @param dest The destination rectangle.
239 */
240
241 void CRect::getIntersect(const CRect &rect, CRect &dest) const
242 {
243 nxgl_coord_t x1 = ngl_max(m_pos.x, rect.getX());
244 nxgl_coord_t y1 = ngl_max(m_pos.y, rect.getY());
245 nxgl_coord_t x2 = ngl_min(getX2(), rect.getX2());
246 nxgl_coord_t y2 = ngl_min(getY2(), rect.getY2());
247
248 dest.setX(x1);
249 dest.setY(y1);
250 dest.setWidth(x2 - x1 + 1);
251 dest.setHeight(y2 - y1 + 1);
252 }
253
254 /**
255 * Populates dest with a rectangle representating the smallest
256 * rectangle that contains this rectangle and rect.
257 *
258 * @param rect The rectangle to add to this.
259 * @param dest The destination rectangle.
260 */
261
262 void CRect::getAddition(const CRect &rect, CRect &dest) const
263 {
264
265 nxgl_coord_t x1 = ngl_min(m_pos.x, rect.getX());
266 nxgl_coord_t y1 = ngl_min(m_pos.y, rect.getY());
267 nxgl_coord_t x2 = ngl_max(getX2(), rect.getX2());
268 nxgl_coord_t y2 = ngl_max(getY2(), rect.getY2());
269
270 dest.setX(x1);
271 dest.setY(y1);
272 dest.setWidth(x2 - x1 + 1);
273 dest.setHeight(y2 - y1 + 1);
274 }
275
276 /**
277 * Clips this rect to the region that intersects the supplied rect.
278 *
279 * @param rect CRect to intersect.
280 */
281
282 void CRect::clipToIntersect(const CRect &rect)
283 {
284 CRect clipped;
285 getIntersect(rect, clipped);
286
287 setX(clipped.getX());
288 setY(clipped.getY());
289 setWidth(clipped.getWidth());
290 setHeight(clipped.getHeight());
291 }
292
293 /**
294 * Expands this rect so that it includes the area described by the supplied
295 * rect.
296 *
297 * @param rect CRect to include.
298 */
299
300 void CRect::expandToInclude(const CRect& rect)
301 {
302 CRect addition;
303 getAddition(rect, addition);
304
305 setX(addition.getX());
306 setY(addition.getY());
307 setWidth(addition.getWidth());
308 setHeight(addition.getHeight());
309 }
310
311 /**
312 * Check if the supplied rect intersects this.
313 *
314 * @param rect CRect to check for intersection with this.
315 * @return True if the rect intersects this; false if not.
316 */
317
318 bool CRect::intersects(const CRect &rect) const
319 {
320 return ((m_pos.x + m_size.w > rect.getX()) &&
321 (m_pos.y + m_size.h > rect.getY()) &&
322 (m_pos.x < rect.getX() + rect.getWidth()) &&
323 (m_pos.y < rect.getY() + rect.getHeight()));
324 }
325
326 /**
327 * Check if the rect contains the supplied point.
328 *
329 * @param x X coordinate of the point.
330 * @param y Y coordinate of the point.
331 * @return True if the rect contains the point; false if not.
332 */
333
334 bool CRect::contains(nxgl_coord_t x, nxgl_coord_t y) const
335 {
336 return ((x >= m_pos.x) && (y >= m_pos.y) &&
337 (x < m_pos.x + m_size.w) && (y < m_pos.y + m_size.h));
338 }
339
340 /**
341 * Copy the properties of this rect to the destination rect.
342 *
343 * @param dest Destination rect to copy to.
344 */
345
346 void CRect::copyTo(CRect &dest) const
347 {
348 dest.setX(m_pos.x);
349 dest.setY(m_pos.y);
350 dest.setWidth(m_size.w);
351 dest.setHeight(m_size.h);
352 }
353
354 /**
355 * Overloaded & operator. Returns the intersect of this rectangle and the
356 * rectangle passed as the "rect" argument".
357 *
358 * @param rect The rectangle to intersect with this.
359 */
360
361 CRect CRect::operator&(const CRect &rect)
362 {
363 CRect dest;
364 getIntersect(rect, dest);
365 return dest;
366 }
367
368 /**
369 * Overloaded + operator. Returns the smallest rectangle that can contain
370 * this rectangle and the rectangle passed as the "rect" argument".
371 *
372 * @param rect The rectangle to add to this.
373 */
374
375 CRect CRect::operator+(const CRect &rect)
376 {
377 CRect dest;
378 getAddition(rect, dest);
379 return dest;
380 }
NXWidgets::CRect::getY2
nxgl_coord_t getY2(void) const
Definition: crect.hxx:319
NXWidgets::CRect::getHeight
nxgl_coord_t getHeight(void) const
Definition: crect.hxx:204
NXWidgets::CRect::expandToInclude
void expandToInclude(const CRect &rect)
Definition: crect.cxx:300
NXWidgets::CRect::copyTo
void copyTo(CRect &dest) const
Definition: crect.cxx:346
NXWidgets::CRect::getWidth
nxgl_coord_t getWidth(void) const
Definition: crect.hxx:181
NXWidgets::CRect::getX
nxgl_coord_t getX(void) const
Definition: crect.hxx:160
NXWidgets::CRect::intersects
bool intersects(const CRect &rect) const
Definition: crect.cxx:318
NXWidgets::CRect::getX2
nxgl_coord_t getX2(void) const
Definition: crect.hxx:308
NXWidgets::CRect::m_size
struct nxgl_size_s m_size
Definition: crect.hxx:105
NXWidgets::CRect::getAddition
void getAddition(const CRect &rect, CRect &dest) const
Definition: crect.cxx:262
NXWidgets::CRect::setNxRect
void setNxRect(FAR const struct nxgl_rect_s *rect)
Definition: crect.hxx:272
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CRect::setHeight
void setHeight(nxgl_coord_t height)
Definition: crect.hxx:261
NXWidgets::CRect::clipToIntersect
void clipToIntersect(const CRect &rect)
Definition: crect.cxx:282
NXWidgets::CRect::getIntersect
void getIntersect(const CRect &rect, CRect &dest) const
Definition: crect.cxx:241
NXWidgets::CRect::setX
void setX(nxgl_coord_t x)
Definition: crect.hxx:229
NXWidgets::CRect::contains
bool contains(nxgl_coord_t x, nxgl_coord_t y) const
Definition: crect.cxx:334
NXWidgets::CRect::fromCoordinates
static CRect fromCoordinates(nxgl_coord_t x1, nxgl_coord_t y1, nxgl_coord_t x2, nxgl_coord_t y2)
NXWidgets::CRect::setX2
void setX2(nxgl_coord_t x2)
Definition: crect.cxx:192
NXWidgets::CRect::setY2
void setY2(nxgl_coord_t y2)
Definition: crect.cxx:217
NXWidgets::CRect
Definition: crect.hxx:101
NXWidgets::CRect::setWidth
void setWidth(nxgl_coord_t width)
Definition: crect.hxx:250
NXWidgets::CRect::getY
nxgl_coord_t getY(void) const
Definition: crect.hxx:170
NXWidgets::CRect::m_pos
struct nxgl_point_s m_pos
Definition: crect.hxx:104
NXWidgets::CRect::operator &
CRect operator &(const CRect &rect)
NXWidgets::CRect::setY
void setY(nxgl_coord_t y)
Definition: crect.hxx:240
NXWidgets::CRect::operator+
CRect operator+(const CRect &rect)
Definition: crect.cxx:375
NXWidgets::CRect::CRect
CRect(void)
Definition: crect.cxx:95
crect.hxx