NXWidgets  1.19
cnxwindow.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/cnxwindow.cxx
3  *
4  * Copyright (C) 2012, 2015-2016 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 /****************************************************************************
37  * Included Files
38  ****************************************************************************/
39 
40 #include <nuttx/config.h>
41 
42 #include <stdint.h>
43 #include <stdbool.h>
44 
45 #include "cwidgetcontrol.hxx"
46 #include "cnxwindow.hxx"
47 #include "cbitmap.hxx"
48 
49 /****************************************************************************
50  * Pre-Processor Definitions
51  ****************************************************************************/
52 
53 /****************************************************************************
54  * Method Implementations
55  ****************************************************************************/
56 
57 using namespace NXWidgets;
58 
59 /**
60  * Constructor.
61  *
62  * @param hNxServer Handle to the NX server.
63  */
64 
65 CNxWindow::CNxWindow(NXHANDLE hNxServer, CWidgetControl *pWidgetControl)
66  : CCallback(pWidgetControl), m_hNxServer(hNxServer), m_hNxWindow(0),
67  m_widgetControl(pWidgetControl)
68 {
69  // Create the CGraphicsPort instance for this window
70 
71  m_widgetControl->createGraphicsPort(static_cast<INxWindow*>(this));
72 }
73 
74 /**
75  * Destructor.
76  */
77 
79 {
80  // Release the window.
81 
82  (void)nx_closewindow(m_hNxWindow);
83 
84  // Release the widget control instance. Whether its lifetime
85  // should be handled by the window, or the owner-of-the-window
86  // is debatable. However in general the widget control is stored
87  // only in the window, so it makes sense to release it here also.
88 
89  delete m_widgetControl;
90  m_widgetControl = NULL;
91 }
92 
93 /**
94  * Creates a new window. Window creation is separate from
95  * object instantiation so that failures can be reported.
96  *
97  * @return True if the window was successfully opened.
98  */
99 
100 bool CNxWindow::open(void)
101 {
102  // Get the C-callable callback vtable
103 
104  FAR struct nx_callback_s *vtable = getCallbackVTable();
105 
106  // Create the window
107 
108  m_hNxWindow = nx_openwindow(m_hNxServer, vtable,
109  (FAR void *)static_cast<CCallback*>(this));
110  return m_hNxWindow != NULL;
111 }
112 
113 /**
114  * Each implementation of INxWindow must provide a method to recover
115  * the contained CWidgetControl instance.
116  *
117  * @return The contained CWidgetControl instance
118  */
119 
121 {
122  return m_widgetControl;
123 }
124 
125 /**
126  * Request the position and size information of the window. The values
127  * will be returned asynchronously through the client callback method.
128  * The GetPosition() method may than be called to obtain the positional
129  * data as provided by the callback.
130  *
131  * @return True on success, false on any failure.
132  */
133 
135 {
136  // Request the window position
137 
138  return nx_getposition(m_hNxWindow) == OK;
139 }
140 
141 /**
142  * Get the position of the window in the physical display coordinates
143  * (as reported by the NX callback).
144  *
145  * @return The position.
146  */
147 
148 bool CNxWindow::getPosition(FAR struct nxgl_point_s *pPos)
149 {
150  return m_widgetControl->getWindowPosition(pPos);
151 }
152 
153 /**
154  * Get the size of the window (as reported by the NX callback).
155  *
156  * @return The size.
157  */
158 
159 bool CNxWindow::getSize(FAR struct nxgl_size_s *pSize)
160 {
161  return m_widgetControl->getWindowSize(pSize);
162 }
163 
164 /**
165  * Set the position and size of the window.
166  *
167  * @param pPos The new position of the window.
168  * @return True on success, false on any failure.
169  */
170 
171 bool CNxWindow::setPosition(FAR const struct nxgl_point_s *pPos)
172 {
173  // Set the window size and position
174 
175  return nx_setposition(m_hNxWindow, pPos) == OK;
176 }
177 
178 /**
179  * Set the size of the selected window.
180  *
181  * @param pSize The new size of the window.
182  * @return True on success, false on any failure.
183  */
184 
185 bool CNxWindow::setSize(FAR const struct nxgl_size_s *pSize)
186 {
187  // Set the window size
188 
189  return nx_setsize(m_hNxWindow, pSize) == OK;
190 }
191 
192 /**
193  * Bring the window to the top of the display.
194  *
195  * @return True on success, false on any failure.
196  */
197 
199 {
200  // Raise the window to the top of the display
201 
202  return nx_raise(m_hNxWindow) == OK;
203 }
204 
205 /**
206  * Lower the window to the bottom of the display.
207  *
208  * @return True on success, false on any failure.
209  */
210 
212 {
213  // Lower the window to the bottom of the display
214 
215  return nx_lower(m_hNxWindow) == OK;
216 }
217 
218 /**
219  * Set an individual pixel in the window with the specified color.
220  *
221  * @param pPos The location of the pixel to be filled.
222  * @param color The color to use in the fill.
223  *
224  * @return True on success; false on failure.
225  */
226 
227 bool CNxWindow::setPixel(FAR const struct nxgl_point_s *pPos,
228  nxgl_mxpixel_t color)
229 {
230  // Set an individual pixel to the specified color
231 
232  return nx_setpixel(m_hNxWindow, pPos, &color) == OK;
233 }
234 
235 /**
236  * Fill the specified rectangle in the window with the specified color.
237  *
238  * @param pRect The location to be filled.
239  * @param color The color to use in the fill.
240  *
241  * @return True on success; false on failure.
242  */
243 
244 bool CNxWindow::fill(FAR const struct nxgl_rect_s *pRect,
245  nxgl_mxpixel_t color)
246 {
247  // Fill a rectangular region with a solid color
248 
249  return nx_fill(m_hNxWindow, pRect, &color) == OK;
250 }
251 
252 /**
253  * Get the raw contents of graphic memory within a rectangular region. NOTE:
254  * Since raw graphic memory is returned, the returned memory content may be
255  * the memory of windows above this one and may not necessarily belong to
256  * this window unless you assure that this is the top window.
257  *
258  * @param rect The location to be copied
259  * @param dest - The describes the destination bitmap to receive the
260  * graphics data.
261  */
262 
263 void CNxWindow::getRectangle(FAR const struct nxgl_rect_s *rect, struct SBitmap *dest)
264 {
265  // Get a rectangule region from the window
266 
267  (void)nx_getrectangle(m_hNxWindow, rect, 0, (FAR uint8_t*)dest->data, dest->stride);
268 }
269 /**
270  * Fill the specified trapezoidal region in the window with the specified
271  * color.
272  *
273  * @param pClip Clipping rectangle relative to window (may be null).
274  * @param pTrap The trapezoidal region to be filled.
275  * @param color The color to use in the fill.
276  *
277  * @return True on success; false on failure.
278  */
279 
280 bool CNxWindow::fillTrapezoid(FAR const struct nxgl_rect_s *pClip,
281  FAR const struct nxgl_trapezoid_s *pTrap,
282  nxgl_mxpixel_t color)
283 {
284  // Fill a trapezoidal region with a solid color
285 
286  return nx_filltrapezoid(m_hNxWindow, pClip, pTrap, &color) == OK;
287 }
288 
289 /**
290  * Fill the specified line in the window with the specified color.
291  *
292  * @param vector - Describes the line to be drawn
293  * @param width - The width of the line
294  * @param color - The color to use to fill the line
295  * @param caps - Draw a circular cap on the ends of the line to support
296  * better line joins
297  *
298  * @return True on success; false on failure.
299  */
300 
301 bool CNxWindow::drawLine(FAR struct nxgl_vector_s *vector,
302  nxgl_coord_t width, nxgl_mxpixel_t color,
303  enum ELineCaps caps)
304 {
305  // Draw a line with the specified color
306 
307  return nx_drawline(m_hNxWindow, vector, width, &color, (uint8_t)caps) == OK;
308 }
309 
310 /**
311  * Draw a filled circle at the specified position, size, and color.
312  *
313  * @param center The window-relative coordinates of the circle center.
314  * @param radius The radius of the rectangle in pixels.
315  * @param color The color of the rectangle.
316  */
317 
318 bool CNxWindow::drawFilledCircle(struct nxgl_point_s *center, nxgl_coord_t radius,
319  nxgl_mxpixel_t color)
320 {
321  return nx_fillcircle(m_hNxWindow, center, radius, &color) == OK;
322 }
323 
324 /**
325  * Move a rectangular region within the window.
326  *
327  * @param pRect Describes the rectangular region to move.
328  * @param pOffset The offset to move the region.
329  *
330  * @return True on success; false on failure.
331  */
332 
333 bool CNxWindow::move(FAR const struct nxgl_rect_s *pRect,
334  FAR const struct nxgl_point_s *pOffset)
335 {
336  // Move a rectangular region of the display
337 
338  return nx_move(m_hNxWindow, pRect, pOffset) == OK;
339 }
340 
341 /**
342  * Copy a rectangular region of a larger image into the rectangle in the
343  * specified window. The source image is treated as an opaque image.
344  *
345  * @param pDest Describes the rectangular on the display that will receive
346  * the bitmap.
347  * @param pSrc The start of the source image.
348  * @param pOrigin the pOrigin of the upper, left-most corner of the full
349  * bitmap. Both pDest and pOrigin are in window coordinates, however,
350  * pOrigin may lie outside of the display.
351  * @param stride The width of the full source image in bytes.
352  *
353  * @return True on success; false on failure.
354  */
355 
356 bool CNxWindow::bitmap(FAR const struct nxgl_rect_s *pDest,
357  FAR const void *pSrc,
358  FAR const struct nxgl_point_s *pOrigin,
359  unsigned int stride)
360 {
361  // Copy a rectangular bitmap image in a region on the display
362 
363  return nx_bitmap(m_hNxWindow, pDest, &pSrc, pOrigin, stride) == OK;
364 }
bool getSize(FAR struct nxgl_size_s *pSize)
Definition: cnxwindow.cxx:159
bool createGraphicsPort(INxWindow *window)
bool drawLine(FAR struct nxgl_vector_s *vector, nxgl_coord_t width, nxgl_mxpixel_t color, enum ELineCaps caps)
Definition: cnxwindow.cxx:301
bool fill(FAR const struct nxgl_rect_s *pRect, nxgl_mxpixel_t color)
Definition: cnxwindow.cxx:244
bool setPosition(FAR const struct nxgl_point_s *pPos)
Definition: cnxwindow.cxx:171
bool move(FAR const struct nxgl_rect_s *pRect, FAR const struct nxgl_point_s *pOffset)
Definition: cnxwindow.cxx:333
bool getWindowPosition(FAR struct nxgl_point_s *pos)
bool requestPosition(void)
Definition: cnxwindow.cxx:134
CNxWindow(NXHANDLE hNxServer, CWidgetControl *pWidgetControl)
Definition: cnxwindow.cxx:65
bool setSize(FAR const struct nxgl_size_s *pSize)
Definition: cnxwindow.cxx:185
bool fillTrapezoid(FAR const struct nxgl_rect_s *pClip, FAR const struct nxgl_trapezoid_s *pTrap, nxgl_mxpixel_t color)
Definition: cnxwindow.cxx:280
bool setPixel(FAR const struct nxgl_point_s *pPos, nxgl_mxpixel_t color)
Definition: cnxwindow.cxx:227
FAR const void * data
Definition: cbitmap.hxx:110
CWidgetControl * m_widgetControl
Definition: cnxwindow.hxx:91
void getRectangle(FAR const struct nxgl_rect_s *rect, struct SBitmap *dest)
Definition: cnxwindow.cxx:263
bool drawFilledCircle(struct nxgl_point_s *center, nxgl_coord_t radius, nxgl_mxpixel_t color)
Definition: cnxwindow.cxx:318
CWidgetControl * getWidgetControl(void) const
Definition: cnxwindow.cxx:120
bool getWindowSize(FAR struct nxgl_size_s *size)
FAR struct nx_callback_s * getCallbackVTable(void)
Definition: ccallback.hxx:239
bool getPosition(FAR struct nxgl_point_s *pPos)
Definition: cnxwindow.cxx:148
bool bitmap(FAR const struct nxgl_rect_s *pDest, FAR const void *pSrc, FAR const struct nxgl_point_s *pOrigin, unsigned int stride)
Definition: cnxwindow.cxx:356