NXWidgets: cnxtkwindow.cxx Source File

NXWidgets 1.19

cnxtkwindow.cxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/libnxwidgets/src/cnxtkwindow.cxx
3 *
4 * Copyright (C) 2012, 2015 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 #include <sched.h>
45 #include <cassert>
46
47 #include "cwidgetcontrol.hxx"
48 #include "cnxtkwindow.hxx"
49 #include "cnxtoolbar.hxx"
50 #include "cbitmap.hxx"
51
52 /****************************************************************************
53 * Pre-Processor Definitions
54 ****************************************************************************/
55
56 /****************************************************************************
57 * Method Implementations
58 ****************************************************************************/
59
60 using namespace NXWidgets;
61
62 /**
63 * Constructor.
64 *
65 * @param hNxServer Handle to the NX server.
66 * @param widgetControl Controlling widget for this window.
67 */
68
69 CNxTkWindow::CNxTkWindow(NXHANDLE hNxServer, CWidgetControl *widgetControl)
70 : CCallback(widgetControl)
71 {
72 // Save construction values
73
74 m_hNxServer = hNxServer;
75 m_widgetControl = widgetControl;
76
77 // Nullify uninitilized pointers and values
78
79 m_hNxTkWindow = (NXTKWINDOW )0;
80 m_toolbar = (CNxToolbar *)0;
81 m_toolbarHeight = 0;
82
83 // Create the CGraphicsPort instance for this window
84
85 m_widgetControl->createGraphicsPort(static_cast<INxWindow*>(this));
86 }
87
88 /**
89 * Destructor.
90 */
91
92 CNxTkWindow::~CNxTkWindow(void)
93 {
94 // It would be a disaster if toolbar instance persists beyond
95 // the window!
96
97 DEBUGASSERT(!m_toolbar);
98
99 // Release the window. We do not release the widget control
100 // instance. The lifetime of that instance is owned by he-who-
101 // constructed-us.
102
103 (void)nxtk_closewindow(m_hNxTkWindow);
104
105 delete m_widgetControl;
106 }
107
108 /**
109 * Creates a new framed window. Window creation is separate from
110 * object instantiation so that failures can be reported.
111 *
112 * @return True if the framed window was successfully created.
113 */
114
115 bool CNxTkWindow::open(void)
116 {
117 // Get the C-callable callback vtable
118
119 FAR struct nx_callback_s *vtable = getCallbackVTable();
120
121 // Create the window
122
123 m_hNxTkWindow = nxtk_openwindow(m_hNxServer, vtable,
124 (FAR void *)static_cast<CCallback*>(this));
125 return m_hNxTkWindow != NULL;
126 }
127
128 /**
129 * Each implementation of INxWindow must provide a method to recover
130 * the contained CWidgetControl instance.
131 *
132 * @return The contained CWidgetControl instance
133 */
134
135 CWidgetControl *CNxTkWindow::getWidgetControl(void) const
136 {
137 return m_widgetControl;
138 }
139
140 /**
141 * Open a toolbar on the framed window. This method both instantiates
142 * the toolbar object AND calls the INxWindow::open() method to
143 * create the toolbar. The toolbar is ready for use upon return.
144 *
145 * @param height. The height in rows of the tool bar
146 * @param widgetControl. The controlling widget for this window. If
147 * none is provided, then a new, vanilla CWidgetControl will be created
148 * for the tool bar.
149 * @param height Height of the toolbar
150 */
151
152 CNxToolbar *CNxTkWindow::openToolbar(nxgl_coord_t height, CWidgetControl *widgetControl)
153 {
154 if (m_hNxTkWindow && !m_toolbar)
155 {
156 // Create a new widget control if none was provided
157
158 CWidgetControl *allocControl = (CWidgetControl *)0;
159 if (!widgetControl)
160 {
161 // NOTE: This constructor would accept the toolbar "style" as a argument.
162 // However, we will explicitly set the style below to handle the case
163 // where the user has provided a custom widget control
164
165 allocControl = new CWidgetControl();
166 if (!allocControl)
167 {
168 return (CNxToolbar *)0;
169 }
170
171 // Use the allocated widget control
172
173 widgetControl = allocControl;
174 }
175
176 // Get current window style from the NXTK window's widget control
177
178 CWidgetStyle style;
179 m_widgetControl->getWidgetStyle(&style);
180
181 // Set the background color(s) to the color of the toolbar
182
183 style.colors.background = CONFIG_NXTK_BORDERCOLOR1;
184 style.colors.selectedBackground = CONFIG_NXTK_BORDERCOLOR1;
185
186 widgetControl->setWidgetStyle(&style);
187
188 // And create the toolbar
189
190 m_toolbar = new CNxToolbar(this, m_hNxTkWindow,
191 widgetControl, height);
192 if (!m_toolbar)
193 {
194 if (allocControl)
195 {
196 delete allocControl;
197 }
198 return (CNxToolbar *)0;
199 }
200
201 // Create the new toolbar. Toolbar creation is separate from
202 // object instantiation so that failures can be reported.
203
204 if (!m_toolbar->open())
205 {
206 // Failed to create the toolbar. Clean-up and return NULL
207
208 delete m_toolbar;
209 if (allocControl)
210 {
211 delete allocControl;
212 }
213 return (CNxToolbar *)0;
214 }
215
216 // Save the height of the toolbar. We will need this because it will change
217 // how we report the size of drawable part of the window.
218
219 m_toolbarHeight = height;
220
221 // Provide parent widget control information to new widget control instance.
222 // This information is reported by an NX callback for "normal" windows. But
223 // the toolbar widget control does not get NX callbacks and has to get the
224 // window size through the setWindowBounds method.
225
226 // Disable preemption so that we can be assured that all of the following
227 // values are synchronized.
228
229 sched_lock();
230
231 // Get the physical bounding box of the window in display coordinates
232
233 struct nxgl_rect_s windowBounds;
234 m_widgetControl->getWindowBoundingBox(&windowBounds);
235
236 // Get the position of the parent window in display coordinates
237
238 struct nxgl_point_s windowPos;
239 m_widgetControl->getWindowPosition(&windowPos);
240
241 // Get the bounding box of the toolbar in parent window coordinates
242
243 struct nxgl_rect_s toolbarBounds;
244 nxtk_toolbarbounds(m_hNxTkWindow, &toolbarBounds);
245
246 // Get the toolbar size
247
248 struct nxgl_size_s toolbarSize;
249 nxgl_rectsize(&toolbarSize, &toolbarBounds);
250
251 // Get the toolbar position in display coordinates by adding the window position
252
253 struct nxgl_point_s toolbarPos;
254 nxgl_vectoradd(&toolbarPos, &toolbarBounds.pt1, &windowPos);
255
256 // Perform the fake NX callback
257
258 widgetControl->geometryEvent(m_hNxTkWindow, &toolbarSize,
259 &toolbarPos, &windowBounds);
260 sched_unlock();
261 }
262
263 return m_toolbar;
264 }
265
266 /**
267 * Request the position and size information of the window. The values
268 * will be returned asynchronously through the client callback method.
269 * The GetPosition() method may than be called to obtain the positional
270 * data as provided by the callback.
271 *
272 * @return True on success, false on any failure.
273 */
274
275 bool CNxTkWindow::requestPosition(void)
276 {
277 // Request the window position
278
279 return nxtk_getposition(m_hNxTkWindow) == OK;
280 }
281
282 /**
283 * Get the position of the window in the physical display coordinates
284 * (as reported by the NX callback).
285 *
286 * @return The position.
287 */
288
289 bool CNxTkWindow::getPosition(FAR struct nxgl_point_s *pos)
290 {
291 return m_widgetControl->getWindowPosition(pos);
292 }
293
294 /**
295 * Get the size of the window drawable region.
296 *
297 * @return The size.
298 */
299
300 bool CNxTkWindow::getSize(FAR struct nxgl_size_s *size)
301 {
302 // Get the size of the NXTK window (this will exclude the thickness of
303 // the frame and the height of the toolbar, if any).
304
305 return m_widgetControl->getWindowSize(size);
306 }
307
308 /**
309 * Set the position and size of the window.
310 *
311 * @param pos The new position of the window.
312 * @return True on success, false on any failure.
313 */
314
315 bool CNxTkWindow::setPosition(FAR const struct nxgl_point_s *pos)
316 {
317 // Set the window size and position
318
319 return nxtk_setposition(m_hNxTkWindow, pos) == OK;
320 }
321
322 /**
323 * Set the size of the selected window.
324 *
325 * @param size The new size of the window.
326 * @return True on success, false on any failure.
327 */
328
329 bool CNxTkWindow::setSize(FAR const struct nxgl_size_s *size)
330 {
331 // Set the window size
332
333 return nxtk_setsize(m_hNxTkWindow, size) == OK;
334 }
335
336 /**
337 * Bring the window to the top of the display.
338 *
339 * @return True on success, false on any failure.
340 */
341
342 bool CNxTkWindow::raise(void)
343 {
344 // Raise the window to the top of the display
345
346 return nxtk_raise(m_hNxTkWindow) == OK;
347 }
348
349 /**
350 * Lower the window to the bottom of the display.
351 *
352 * @return True on success, false on any failure.
353 */
354
355 bool CNxTkWindow::lower(void)
356 {
357 // Lower the window to the bottom of the display
358
359 return nxtk_lower(m_hNxTkWindow) == OK;
360 }
361
362 /**
363 * Set an individual pixel in the window with the specified color.
364 *
365 * @param pos The location of the pixel to be filled.
366 * @param color The color to use in the fill.
367 *
368 * @return True on success; false on failure.
369 */
370
371 bool CNxTkWindow::setPixel(FAR const struct nxgl_point_s *pos,
372 nxgl_mxpixel_t color)
373 {
374 #if 0
375 // Set an individual pixel to the specified color
376
377 return nxtk_setpixel(m_hNxTkWindow, pos, &color) == OK;
378 #else
379 // REVISIT
380 return false;
381 #endif
382 }
383
384 /**
385 * Fill the specified rectangle in the window with the specified color.
386 *
387 * @param pRect The location to be filled.
388 * @param color The color to use in the fill.
389 *
390 * @return True on success; false on failure.
391 */
392
393 bool CNxTkWindow::fill(FAR const struct nxgl_rect_s *pRect,
394 nxgl_mxpixel_t color)
395 {
396 // Fill a rectangular region with a solid color
397
398 return nxtk_fillwindow(m_hNxTkWindow, pRect, &color) == OK;
399 }
400
401 /**
402 * Get the raw contents of graphic memory within a rectangular region. NOTE:
403 * Since raw graphic memory is returned, the returned memory content may be
404 * the memory of windows above this one and may not necessarily belong to
405 * this window unless you assure that this is the top window.
406 *
407 * @param rect The location to be copied
408 * @param dest - The describes the destination bitmap to receive the
409 * graphics data.
410 */
411
412 void CNxTkWindow::getRectangle(FAR const struct nxgl_rect_s *rect, struct SBitmap *dest)
413 {
414 // Get a rectangule region from the window
415
416 (void)nxtk_getwindow(m_hNxTkWindow, rect, 0, (FAR uint8_t*)dest->data, dest->stride);
417 }
418
419 /**
420 * Fill the specified trapezoidal region in the window with the specified
421 * color.
422 *
423 * @param pClip Clipping rectangle relative to window (may be null).
424 * @param pTrap The trapezoidal region to be filled.
425 * @param color The color to use in the fill.
426 *
427 * @return True on success; false on failure.
428 */
429
430 bool CNxTkWindow::fillTrapezoid(FAR const struct nxgl_rect_s *pClip,
431 FAR const struct nxgl_trapezoid_s *pTrap,
432 nxgl_mxpixel_t color)
433 {
434 // Fill a trapezoidal region with a solid color
435
436 return nxtk_filltrapwindow(m_hNxTkWindow, pTrap, &color) == OK;
437 }
438
439 /**
440 * Fill the specified line in the window with the specified color.
441 *
442 * @param vector - Describes the line to be drawn
443 * @param width - The width of the line
444 * @param color - The color to use to fill the line
445 * @param caps - Draw a circular cap on both ends of the line to support
446 * better line joins
447 *
448 * @return True on success; false on failure.
449 */
450
451 bool CNxTkWindow::drawLine(FAR struct nxgl_vector_s *vector,
452 nxgl_coord_t width, nxgl_mxpixel_t color,
453 enum ELineCaps caps)
454 {
455 // Draw a line with the specified color
456
457 return nxtk_drawlinewindow(m_hNxTkWindow, vector, width, &color, (uint8_t)caps) == OK;
458 }
459
460 /**
461 * Draw a filled circle at the specified position, size, and color.
462 *
463 * @param center The window-relative coordinates of the circle center.
464 * @param radius The radius of the rectangle in pixels.
465 * @param color The color of the rectangle.
466 */
467
468 bool CNxTkWindow::drawFilledCircle(struct nxgl_point_s *center, nxgl_coord_t radius,
469 nxgl_mxpixel_t color)
470 {
471 return nxtk_fillcirclewindow(m_hNxTkWindow, center, radius, &color) == OK;
472 }
473
474 /**
475 * Move a rectangular region within the window.
476 *
477 * @param pRect Describes the rectangular region to move.
478 * @param pOffset The offset to move the region.
479 *
480 * @return True on success; false on failure.
481 */
482
483 bool CNxTkWindow::move(FAR const struct nxgl_rect_s *pRect,
484 FAR const struct nxgl_point_s *pOffset)
485 {
486 // Move a rectangular region of the display
487
488 return nxtk_movewindow(m_hNxTkWindow, pRect, pOffset) == OK;
489 }
490
491 /**
492 * Copy a rectangular region of a larger image into the rectangle in the
493 * specified window. The source image is treated as an opaque image.
494 *
495 * @param pDest Describes the rectangular on the display that will receive
496 * the bitmap.
497 * @param pSrc The start of the source image.
498 * @param pOrigin the pOrigin of the upper, left-most corner of the full
499 * bitmap. Both pDest and pOrigin are in window coordinates, however,
500 * pOrigin may lie outside of the display.
501 * @param stride The width of the full source image in bytes.
502 *
503 * @return True on success; false on failure.
504 */
505
506 bool CNxTkWindow::bitmap(FAR const struct nxgl_rect_s *pDest,
507 FAR const void *pSrc,
508 FAR const struct nxgl_point_s *pOrigin,
509 unsigned int stride)
510 {
511 // Copy a rectangular bitmap image in a region on the display
512
513 return nxtk_bitmapwindow(m_hNxTkWindow, pDest, &pSrc, pOrigin, stride) == OK;
514 }
NXWidgets::CNxTkWindow::getRectangle
void getRectangle(FAR const struct nxgl_rect_s *rect, struct SBitmap *dest)
Definition: cnxtkwindow.cxx:412
NXWidgets::CNxToolbar::open
bool open(void)
Definition: cnxtoolbar.cxx:113
NXWidgets::CWidgetColors::selectedBackground
nxgl_mxpixel_t selectedBackground
Definition: cwidgetstyle.hxx:106
NXWidgets::CCallback
Definition: ccallback.hxx:91
NXWidgets::CWidgetControl::createGraphicsPort
bool createGraphicsPort(INxWindow *window)
Definition: cwidgetcontrol.cxx:656
NXWidgets::CNxTkWindow::~CNxTkWindow
~CNxTkWindow(void)
Definition: cnxtkwindow.cxx:92
NXWidgets::CNxTkWindow::setPixel
bool setPixel(FAR const struct nxgl_point_s *pos, nxgl_mxpixel_t color)
Definition: cnxtkwindow.cxx:371
NXWidgets::CWidgetStyle
Definition: cwidgetstyle.hxx:125
NXWidgets::CNxTkWindow::CNxTkWindow
CNxTkWindow(NXHANDLE hNxServer, CWidgetControl *widgetControl)
Definition: cnxtkwindow.cxx:69
NXWidgets::CWidgetControl
Definition: cwidgetcontrol.hxx:95
NXWidgets::CNxTkWindow::raise
bool raise(void)
Definition: cnxtkwindow.cxx:342
NXWidgets::CNxTkWindow::m_toolbarHeight
nxgl_coord_t m_toolbarHeight
Definition: cnxtkwindow.hxx:91
NXWidgets::CNxTkWindow::m_hNxServer
NXHANDLE m_hNxServer
Definition: cnxtkwindow.hxx:87
cwidgetcontrol.hxx
NXWidgets::CNxTkWindow::openToolbar
CNxToolbar * openToolbar(nxgl_coord_t height, CWidgetControl *widgetControl=(CWidgetControl *) 0)
Definition: cnxtkwindow.cxx:152
NXWidgets::CWidgetControl::getWindowPosition
bool getWindowPosition(FAR struct nxgl_point_s *pos)
Definition: cwidgetcontrol.hxx:699
NXWidgets::CNxTkWindow::fill
bool fill(FAR const struct nxgl_rect_s *pRect, nxgl_mxpixel_t color)
Definition: cnxtkwindow.cxx:393
NXWidgets::CNxTkWindow::getPosition
bool getPosition(FAR struct nxgl_point_s *pos)
Definition: cnxtkwindow.cxx:289
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CNxTkWindow::lower
bool lower(void)
Definition: cnxtkwindow.cxx:355
NXWidgets::CNxTkWindow::move
bool move(FAR const struct nxgl_rect_s *pRect, FAR const struct nxgl_point_s *pOffset)
Definition: cnxtkwindow.cxx:483
NXWidgets::CWidgetControl::setWidgetStyle
void setWidgetStyle(const CWidgetStyle *style)
Definition: cwidgetcontrol.hxx:577
NXWidgets::CNxTkWindow::setPosition
bool setPosition(FAR const struct nxgl_point_s *pos)
Definition: cnxtkwindow.cxx:315
NXWidgets::INxWindow::ELineCaps
ELineCaps
Definition: inxwindow.hxx:86
NXWidgets::CNxTkWindow::open
bool open(void)
Definition: cnxtkwindow.cxx:115
NXWidgets::CNxTkWindow::drawLine
bool drawLine(FAR struct nxgl_vector_s *vector, nxgl_coord_t width, nxgl_mxpixel_t color, enum ELineCaps caps)
Definition: cnxtkwindow.cxx:451
NXWidgets::CNxTkWindow::fillTrapezoid
bool fillTrapezoid(FAR const struct nxgl_rect_s *pClip, FAR const struct nxgl_trapezoid_s *pTrap, nxgl_mxpixel_t color)
Definition: cnxtkwindow.cxx:430
NXWidgets::CNxToolbar
Definition: cnxtoolbar.hxx:85
NXWidgets::CWidgetControl::getWindowBoundingBox
CRect getWindowBoundingBox(void)
Definition: cwidgetcontrol.hxx:680
NXWidgets::SBitmap::data
FAR const void * data
Definition: cbitmap.hxx:110
NXWidgets::CNxTkWindow::drawFilledCircle
bool drawFilledCircle(struct nxgl_point_s *center, nxgl_coord_t radius, nxgl_mxpixel_t color)
Definition: cnxtkwindow.cxx:468
NXWidgets::CNxTkWindow::m_widgetControl
CWidgetControl * m_widgetControl
Definition: cnxtkwindow.hxx:89
NXWidgets::CWidgetColors::background
nxgl_mxpixel_t background
Definition: cwidgetstyle.hxx:105
NXWidgets::CNxTkWindow::requestPosition
bool requestPosition(void)
Definition: cnxtkwindow.cxx:275
NXWidgets::CWidgetControl::geometryEvent
void geometryEvent(NXHANDLE hWindow, const struct nxgl_size_s *size, const struct nxgl_point_s *pos, const struct nxgl_rect_s *bounds)
Definition: cwidgetcontrol.cxx:372
cnxtkwindow.hxx
NXWidgets::CNxTkWindow::m_toolbar
CNxToolbar * m_toolbar
Definition: cnxtkwindow.hxx:90
NXWidgets::CWidgetControl::getWidgetStyle
void getWidgetStyle(CWidgetStyle *style)
Definition: cwidgetcontrol.hxx:566
cbitmap.hxx
NXWidgets::SBitmap
Definition: cbitmap.hxx:103
cnxtoolbar.hxx
NXWidgets::CNxTkWindow::bitmap
bool bitmap(FAR const struct nxgl_rect_s *pDest, FAR const void *pSrc, FAR const struct nxgl_point_s *pOrigin, unsigned int stride)
Definition: cnxtkwindow.cxx:506
NXWidgets::CWidgetStyle::colors
CWidgetColors colors
Definition: cwidgetstyle.hxx:128
NXWidgets::CWidgetControl::getWindowSize
bool getWindowSize(FAR struct nxgl_size_s *size)
Definition: cwidgetcontrol.hxx:714
NXWidgets::CNxTkWindow::getWidgetControl
CWidgetControl * getWidgetControl(void) const
Definition: cnxtkwindow.cxx:135
NXWidgets::CNxTkWindow::getSize
bool getSize(FAR struct nxgl_size_s *size)
Definition: cnxtkwindow.cxx:300
NXWidgets::CCallback::getCallbackVTable
FAR struct nx_callback_s * getCallbackVTable(void)
Definition: ccallback.hxx:239
NXWidgets::CNxTkWindow::setSize
bool setSize(FAR const struct nxgl_size_s *size)
Definition: cnxtkwindow.cxx:329
NXWidgets::SBitmap::stride
uint16_t stride
Definition: cbitmap.hxx:109
NXWidgets::CNxTkWindow::m_hNxTkWindow
NXTKWINDOW m_hNxTkWindow
Definition: cnxtkwindow.hxx:88