NXWidgets  1.19
crect.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/include/cbgwindow.hxx
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 #ifndef __INCLUDE_CRECT_HXX
71 #define __INCLUDE_CRECT_HXX
72 
73 /****************************************************************************
74  * Included Files
75  ****************************************************************************/
76 
77 #include <nuttx/config.h>
78 
79 #include <stdint.h>
80 #include <stdbool.h>
81 
82 #include <nuttx/nx/nxglib.h>
83 #include <nuttx/nx/nx.h>
84 
85 /****************************************************************************
86  * Pre-Processor Definitions
87  ****************************************************************************/
88 
89 /****************************************************************************
90  * Implementation Classes
91  ****************************************************************************/
92 
93 #if defined(__cplusplus)
94 
95 namespace NXWidgets
96 {
97  /**
98  * Class describing a rectangle.
99  */
100 
101  class CRect
102  {
103  public:
104  struct nxgl_point_s m_pos; /**< The position of the rectangle in the window */
105  struct nxgl_size_s m_size; /**< The size of the rectangle */
106 
107  /**
108  * Constructor.
109  */
110 
111  CRect(void);
112 
113  /**
114  * Constructor.
115  *
116  * @param x The x coordinate of the rect.
117  * @param y The y coordinate of the rect.
118  * @param width The width of the rect.
119  * @param height The height of the rect.
120  */
121 
122  CRect(nxgl_coord_t x, nxgl_coord_t y,
123  nxgl_coord_t width, nxgl_coord_t height);
124 
125  /**
126  * Constructor.
127  *
128  * @param rect Pointer to an NX rectangle
129  */
130 
131  CRect(FAR const nxgl_rect_s *rect);
132 
133  /**
134  * Copy constructor.
135  *
136  * @param rect CRect to copy.
137  */
138 
139  CRect(const CRect& rect);
140 
141  /**
142  * Create a rect object from the supplied coordinates.
143  *
144  * @param x1 The x coordinate of the rect's top-left corner.
145  * @param y1 The y coordinate of the rect's top-left corner.
146  * @param x2 The x coordinate of the rect's bottom-right corner.
147  * @param y2 The y coordinate of the rect's bottom-right corner.
148  * @return A new rect.
149  */
150 
151  static CRect fromCoordinates(nxgl_coord_t x1, nxgl_coord_t y1,
152  nxgl_coord_t x2, nxgl_coord_t y2);
153 
154  /**
155  * Get the rectangle's left x coordinate.
156  *
157  * @return The rectangle's x coordinate.
158  */
159 
160  inline nxgl_coord_t getX(void) const
161  {
162  return m_pos.x;
163  }
164 
165  /**
166  * Get the rectangle's top y coordinate.
167  * @return The rectangle's y coordinate.
168  */
169 
170  inline nxgl_coord_t getY(void) const
171  {
172  return m_pos.y;
173  }
174 
175  /**
176  * Get the rectangle's width.
177  *
178  * @return The rectangle's width.
179  */
180 
181  inline nxgl_coord_t getWidth(void) const
182  {
183  return m_size.w;
184  }
185 
186  /**
187  * Get the size of the rectangle
188  *
189  * @return The rectangle's size
190  */
191 
192  inline void getSize(struct nxgl_size_s &size) const
193  {
194  size.h = m_size.h;
195  size.w = m_size.w;
196  }
197 
198  /**
199  * Get the rectangle's height.
200  *
201  * @return The rectangle's height.
202  */
203 
204  inline nxgl_coord_t getHeight(void) const
205  {
206  return m_size.h;
207  }
208 
209  /**
210  * Get the NX rectangle representation
211  *
212  * @param rect Pointer to NX rectangle
213  */
214 
215  inline void getNxRect(FAR struct nxgl_rect_s *rect) const
216  {
217  rect->pt1.x = m_pos.x;
218  rect->pt1.y = m_pos.y;
219  rect->pt2.x = m_pos.x + m_size.w - 1;
220  rect->pt2.y = m_pos.y + m_size.h - 1;
221  }
222 
223  /**
224  * Set the rectangle's left x coordinate.
225  *
226  * @param x The new x coordinate.
227  */
228 
229  inline void setX(nxgl_coord_t x)
230  {
231  m_pos.x = x;
232  }
233 
234  /**
235  * Set the rectangle's top y coordinate.
236  *
237  * @param y The new y coordinate.
238  */
239 
240  inline void setY(nxgl_coord_t y)
241  {
242  m_pos.y = y;
243  }
244 
245  /**
246  * Set the rect's width.
247  *
248  * @param width The new width.
249  */
250  inline void setWidth(nxgl_coord_t width)
251  {
252  m_size.w = width;
253  }
254 
255  /**
256  * Set the rect's height.
257  *
258  * @param height The new height.
259  */
260 
261  inline void setHeight(nxgl_coord_t height)
262  {
263  m_size.h = height;
264  }
265 
266  /**
267  * Get the NX rectangle representation
268  *
269  * @param rect Pointer to NX rectangle
270  */
271 
272  inline void setNxRect(FAR const struct nxgl_rect_s *rect)
273  {
274  m_pos.x = rect->pt1.x;
275  m_pos.y = rect->pt1.y;
276  m_size.w = rect->pt2.x - rect->pt1.x + 1;
277  m_size.h = rect->pt2.y - rect->pt1.y + 1;
278  }
279 
280  /**
281  * Set the x coordinate of the rect's bottom-right corner. If x2 is less
282  * than the rect's current x coordinate the method automatically adjusts
283  * the coordinates so that the rect's width is never negative. Changing this
284  * property will change the width of the rect.
285  *
286  * @param x2 The x coordinate of the rect's bottom-right corner.
287  */
288 
289  void setX2(nxgl_coord_t x2);
290 
291  /**
292  * Set the y coordinate of the rect's bottom-right corner. If y2 is less
293  * than the rect's current y coordinate the method automatically adjusts
294  * the coordinates so that the rect's height is never negative. Changing this
295  * property will change the height of the rect.
296  *
297  * @param y2 The y coordinate of the rect's bottom-right corner.
298  */
299 
300  void setY2(nxgl_coord_t y2);
301 
302  /**
303  * Get the x coordinate of the rectangle's right side.
304  *
305  * @return The x coordinate of the rectangle's right side.
306  */
307 
308  inline nxgl_coord_t getX2(void) const
309  {
310  return m_pos.x + m_size.w - 1;
311  }
312 
313  /**
314  * Get the y coordinate of the rectangle's bottom side.
315  *
316  * @return The y coordinate of the rerectangle's bottom side.
317  */
318 
319  inline nxgl_coord_t getY2(void) const
320  {
321  return m_pos.y + m_size.h - 1;
322  }
323 
324  /**
325  * Offset the rectangle position by the specified dx, dy values.
326  *
327  * @param dx X offset value
328  * @param dy Y offset value
329  */
330 
331  inline void offset(nxgl_coord_t dx, nxgl_coord_t dy)
332  {
333  m_pos.x += dx;
334  m_pos.y += dy;
335  }
336 
337  /**
338  * Determines if the rectangle has two dimensions; in other words, does it
339  * have both height and width? Negative width or height is considered not to
340  * be valid.
341  *
342  * @return True if the rect has height and width; false if not.
343  */
344 
345  inline bool hasDimensions(void) const
346  {
347  return m_size.w > 0 && m_size.h > 0;
348  }
349 
350  /**
351  * Populates dest with a rectangle representating the intersection
352  * of this rectangle and rect.
353  *
354  * @param rect The rectangle to intersect with this.
355  * @param dest The destination rectangle.
356  */
357 
358  void getIntersect(const CRect& rect, CRect& dest) const;
359 
360  /**
361  * Populates dest with a rectangle representating the smallest
362  * rectangle that contains this rectangle and rect.
363  *
364  * @param rect The rectangle to add to this.
365  * @param dest The destination rectangle.
366  */
367 
368  void getAddition(const CRect &rect, CRect &dest) const;
369 
370  /**
371  * Clips this rect to the region that intersects the supplied rect.
372  *
373  * @param rect CRect to intersect.
374  */
375 
376  void clipToIntersect(const CRect &rect);
377 
378  /**
379  * Expands this rect so that it includes the area described by the supplied
380  * rect.
381  *
382  * @param rect CRect to include.
383  */
384 
385  void expandToInclude(const CRect &rect);
386 
387  /**
388  * Check if the supplied rect intersects this.
389  *
390  * @param rect CRect to check for intersection with this.
391  * @return True if the rect intersects this; false if not.
392  */
393 
394  bool intersects(const CRect &rect) const;
395 
396  /**
397  * Check if the rect contains the supplied point.
398  *
399  * @param x X coordinate of the point.
400  * @param y Y coordinate of the point.
401  * @return True if the rect contains the point; false if not.
402  */
403 
404  bool contains(nxgl_coord_t x, nxgl_coord_t y) const;
405 
406  /**
407  * Copy the properties of this rect to the destination rect.
408  *
409  * @param dest Destination rect to copy to.
410  */
411 
412  void copyTo(CRect &dest) const;
413 
414  /**
415  * Overloaded & operator. Returns the intersect of this rectangle and the
416  * rectangle passed as the "rect" argument".
417  *
418  * @param rect The rectangle to intersect with this.
419  */
420 
421  CRect operator&(const CRect &rect);
422 
423  /**
424  * Overloaded + operator. Returns the smallest rectangle that can contain
425  * this rectangle and the rectangle passed as the "rect" argument".
426  *
427  * @param rect The rectangle to add to this.
428  */
429 
430  CRect operator+(const CRect &rect);
431  };
432 }
433 
434 #endif // __cplusplus
435 
436 #endif // __INCLUDE_CRECT_HXX
nxgl_coord_t getY2(void) const
Definition: crect.hxx:319
nxgl_coord_t getHeight(void) const
Definition: crect.hxx:204
void expandToInclude(const CRect &rect)
Definition: crect.cxx:300
void getNxRect(FAR struct nxgl_rect_s *rect) const
Definition: crect.hxx:215
void copyTo(CRect &dest) const
Definition: crect.cxx:346
nxgl_coord_t getWidth(void) const
Definition: crect.hxx:181
nxgl_coord_t getX(void) const
Definition: crect.hxx:160
bool intersects(const CRect &rect) const
Definition: crect.cxx:318
nxgl_coord_t getX2(void) const
Definition: crect.hxx:308
struct nxgl_size_s m_size
Definition: crect.hxx:105
void getAddition(const CRect &rect, CRect &dest) const
Definition: crect.cxx:262
void setNxRect(FAR const struct nxgl_rect_s *rect)
Definition: crect.hxx:272
bool hasDimensions(void) const
Definition: crect.hxx:345
void setHeight(nxgl_coord_t height)
Definition: crect.hxx:261
void clipToIntersect(const CRect &rect)
Definition: crect.cxx:282
void getIntersect(const CRect &rect, CRect &dest) const
Definition: crect.cxx:241
void setX(nxgl_coord_t x)
Definition: crect.hxx:229
bool contains(nxgl_coord_t x, nxgl_coord_t y) const
Definition: crect.cxx:334
void getSize(struct nxgl_size_s &size) const
Definition: crect.hxx:192
static CRect fromCoordinates(nxgl_coord_t x1, nxgl_coord_t y1, nxgl_coord_t x2, nxgl_coord_t y2)
void setX2(nxgl_coord_t x2)
Definition: crect.cxx:192
void setY2(nxgl_coord_t y2)
Definition: crect.cxx:217
void offset(nxgl_coord_t dx, nxgl_coord_t dy)
Definition: crect.hxx:331
void setWidth(nxgl_coord_t width)
Definition: crect.hxx:250
nxgl_coord_t getY(void) const
Definition: crect.hxx:170
struct nxgl_point_s m_pos
Definition: crect.hxx:104
CRect operator &(const CRect &rect)
void setY(nxgl_coord_t y)
Definition: crect.hxx:240
CRect operator+(const CRect &rect)
Definition: crect.cxx:375