NXWidgets  1.19
capplicationwindow.cxx
Go to the documentation of this file.
1 /********************************************************************************************
2  * NxWidgets/nxwm/src/capplicationwindow.cxx
3  *
4  * Copyright (C) 2012-2013 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 <nuttx/nx/nxglib.h>
43 
44 #include "nxconfig.hxx"
45 #include "cwidgetcontrol.hxx"
46 #include "cgraphicsport.hxx"
47 
48 #include "nxwmconfig.hxx"
49 #include "nxwmglyphs.hxx"
50 #include "cwindowmessenger.hxx"
51 #include "capplicationwindow.hxx"
52 
53 /********************************************************************************************
54  * Pre-Processor Definitions
55  ********************************************************************************************/
56 
57 #ifdef CONFIG_NXWM_STOP_BITMAP
59 #else
60 # define CONFIG_NXWM_STOP_BITMAP g_stopBitmap
61 #endif
62 
63 #ifdef CONFIG_NXWM_MINIMIZE_BITMAP
65 #else
66 # define CONFIG_NXWM_MINIMIZE_BITMAP g_minimizeBitmap
67 #endif
68 
69 /********************************************************************************************
70  * CApplicationWindow Method Implementations
71  ********************************************************************************************/
72 
73  using namespace NxWM;
74 
75 /**
76  * CApplicationWindow Constructor
77  *
78  * @param taskbar. A pointer to the parent task bar instance.
79  * @param flags. Optional flags to control the window configuration (See EWindowFlags).
80  */
81 
83 {
84  // Save the window and window flags for later use
85 
86  m_window = window;
87  m_flags = flags;
88 
89  // These will be created with the open method is called
90 
92 
96 
99 
101 
102  // This will be initialized when the registerCallbacks() method is called
103 
105 }
106 
107 /**
108  * CApplicationWindow Destructor
109  */
110 
112 {
113  // Free the resources that we are responsible for
114 
115  if (m_minimizeImage)
116  {
117  delete m_minimizeImage;
118  }
119 
120  if (m_stopImage)
121  {
122  delete m_stopImage;
123  }
124 
125  if (m_windowLabel)
126  {
127  delete m_windowLabel;
128  }
129 
130  if (m_minimizeBitmap)
131  {
132  delete m_minimizeBitmap;
133  }
134 
135  if (m_stopBitmap)
136  {
137  delete m_stopBitmap;
138  }
139 
140  if (m_windowFont)
141  {
142  delete m_windowFont;
143  }
144 
145  if (m_toolbar)
146  {
147  delete m_toolbar;
148  }
149 
150  // We didn't create the window. That was done by the task bar,
151  // But we will handle destruction of with window as a courtesy.
152 
153  if (m_window)
154  {
155  delete m_window;
156  }
157 }
158 
159 /**
160  * Initialize window. Window initialization is separate from
161  * object instantiation so that failures can be reported.
162  *
163  * @return True if the window was successfully initialized.
164  */
165 
167 {
168  // Create the widget control (with the window messenger) using the default style
169 
170  CWindowMessenger *control = new CWindowMessenger();
171  if (!control)
172  {
173  return false;
174  }
175 
176  // Open the toolbar using the widget control
177 
178  m_toolbar = m_window->openToolbar(CONFIG_NXWM_TOOLBAR_HEIGHT, control);
179  if (!m_toolbar)
180  {
181  // We failed to open the toolbar
182 
183  return false;
184  }
185 
186  // Get the width of the display
187 
188  struct nxgl_size_s windowSize;
189  if (!m_toolbar->getSize(&windowSize))
190  {
191  return false;
192  }
193 
194  // Start positioning icons from the right side of the tool bar
195 
196  struct nxgl_point_s iconPos;
197  struct nxgl_size_s iconSize;
198 
199  iconPos.x = windowSize.w;
200 
201  // Create the STOP icon only if this is a non-persistent application
202 
203  if (!isPersistent())
204  {
205  // Create STOP bitmap container
206 
208  if (!m_stopBitmap)
209  {
210  return false;
211  }
212 
213  // Create the STOP application icon at the right of the toolbar
214  // Get the height and width of the stop bitmap
215 
216  iconSize.w = m_stopBitmap->getWidth();
217  iconSize.h = m_stopBitmap->getHeight();
218 
219  // The default CImage has borders enabled with thickness of the border
220  // width. Add twice the thickness of the border to the width and height.
221  // (We could let CImage do this for us by calling
222  // CImage::getPreferredDimensions())
223 
224  iconSize.w += 2 * 1;
225  iconSize.h += 2 * 1;
226 
227  // Pick an X/Y position such that the image will position at the right of
228  // the toolbar and centered vertically.
229 
230  iconPos.x -= iconSize.w;
231 
232  if (iconSize.h >= windowSize.h)
233  {
234  iconPos.y = 0;
235  }
236  else
237  {
238  iconPos.y = (windowSize.h - iconSize.h) >> 1;
239  }
240 
241  // Now we have enough information to create the image
242 
243  m_stopImage = new NXWidgets::CImage(control, iconPos.x, iconPos.y, iconSize.w,
244  iconSize.h, m_stopBitmap);
245  if (!m_stopImage)
246  {
247  return false;
248  }
249 
250  // Configure 'this' to receive mouse click inputs from the image
251 
252  m_stopImage->setBorderless(true);
254  }
255 
256 #ifndef CONFIG_NXWM_DISABLE_MINIMIZE
257  // Create MINIMIZE application bitmap container
258 
259  m_minimizeBitmap = new NXWidgets::CRlePaletteBitmap(&CONFIG_NXWM_MINIMIZE_BITMAP);
260  if (!m_minimizeBitmap)
261  {
262  return false;
263  }
264 
265  // Get the height and width of the stop bitmap
266 
267  iconSize.w = m_minimizeBitmap->getWidth();
268  iconSize.h = m_minimizeBitmap->getHeight();
269 
270  // The default CImage has borders enabled with thickness of the border
271  // width. Add twice the thickness of the border to the width and height.
272  // (We could let CImage do this for us by calling
273  // CImage::getPreferredDimensions())
274 
275  iconSize.w += 2 * 1;
276  iconSize.h += 2 * 1;
277 
278  // Pick an X/Y position such that the image will position at the right of
279  // the toolbar and centered vertically.
280 
281  iconPos.x -= iconSize.w;
282 
283  if (iconSize.h >= windowSize.h)
284  {
285  iconPos.y = 0;
286  }
287  else
288  {
289  iconPos.y = (windowSize.h - iconSize.h) >> 1;
290  }
291 
292  // Now we have enough information to create the image
293 
294  m_minimizeImage = new NXWidgets::CImage(control, iconPos.x, iconPos.y, iconSize.w,
295  iconSize.h, m_minimizeBitmap);
296  if (!m_minimizeImage)
297  {
298  return false;
299  }
300 
301  // Configure 'this' to receive mouse click inputs from the image
302 
305 #endif
306 
307  // The rest of the toolbar will hold the left-justified application label
308  // Create the default font instance
309 
310  m_windowFont = new NXWidgets::CNxFont((nx_fontid_e)CONFIG_NXWM_TOOLBAR_FONTID,
311  CONFIG_NXWM_DEFAULT_FONTCOLOR,
312  CONFIG_NXWM_TRANSPARENT_COLOR);
313  if (!m_windowFont)
314  {
315  return false;
316  }
317 
318  // Get the height and width of the text display area
319 
320  iconSize.w = iconPos.x;
321  iconSize.h = windowSize.h;
322 
323  iconPos.x = 0;
324  iconPos.y = 0;
325 
326  // Now we have enough information to create the label
327 
328  m_windowLabel = new NXWidgets::CLabel(control, iconPos.x, iconPos.y,
329  iconSize.w, iconSize.h, "");
330  if (!m_windowLabel)
331  {
332  return false;
333  }
334 
335  // Configure the label
336 
342  return true;
343 }
344 
345 /**
346  * Re-draw the application window
347  */
348 
350 {
351  // Get the widget control from the task bar
352 
354 
355  // Get the graphics port for drawing on the background window
356 
357  NXWidgets::CGraphicsPort *port = control->getGraphicsPort();
358 
359  // Get the size of the window
360 
361  struct nxgl_size_s windowSize;
362  if (!m_toolbar->getSize(&windowSize))
363  {
364  return;
365  }
366 
367  // Fill the entire tool bar with the non-shadowed border color
368 
369  port->drawFilledRect(0, 0, windowSize.w, windowSize.h,
370  CONFIG_NXTK_BORDERCOLOR1);
371 
372  // Then draw the stop image (which may not be present if this is a
373  // "persistent" application)
374 
375  if (m_stopImage)
376  {
378  m_stopImage->redraw();
380  }
381 
382  // Draw the minimize image (which may not be present if this is a
383  // mimimization is disabled)
384 
385  if (m_minimizeImage)
386  {
390  }
391 
392  // And finally draw the window label
393 
396 }
397 
398 /**
399  * The application window is hidden (either it is minimized or it is
400  * maximized, but not at the top of the hierarchy)
401  */
402 
404 {
405  // Disable the stop image (which may not be present if this is a
406  // "persistent" application)
407 
408  if (m_stopImage)
409  {
412  }
413 
414  // Disable the minimize image(which may not be present if this is a
415  // mimimization is disabled)
416 
417  if (m_minimizeImage)
418  {
421  }
422 
423  // Disable the window label
424 
426 }
427 
428 /**
429  * Recover the contained NXTK window instance
430  *
431  * @return. The window used by this application
432  */
433 
435 {
436  return static_cast<NXWidgets::INxWindow*>(m_window);
437 }
438 
439 /**
440  * Recover the contained widget control
441  *
442  * @return. The widget control used by this application
443  */
444 
446 {
447  return m_window->getWidgetControl();
448 }
449 
450 /**
451  * Block further activity on this window in preparation for window
452  * shutdown.
453  *
454  * @param app. The application to be blocked
455  */
456 
458 {
459  // Get the widget control from the NXWidgets::CNxWindow instance
460 
462 
463  // And then block further reporting activity on the underlying
464  // NX framed window
465 
466  nxtk_block(control->getWindowHandle(), (FAR void *)app);
467 }
468 
469 /**
470  * Set the window label
471  *
472  * @param appname. The name of the application to place on the window
473  */
474 
476 {
477  m_windowLabel->setText(appname);
478 }
479 
480 /**
481  * Report of this is a "normal" window or a full screen window. The
482  * primary purpose of this method is so that window manager will know
483  * whether or not it show draw the task bar.
484  *
485  * @return True if this is a full screen window.
486  */
487 
489 {
490  return false;
491 }
492 
493 /**
494  * Register to receive callbacks when toolbar icons are selected
495  */
496 
498 {
499  m_callback = callback;
500 }
501 
502 /**
503  * Simulate a mouse click or release on the minimize icon. This method
504  * is only available for automated testing of NxWM.
505  *
506  * @param click. True to click; false to release;
507  */
508 
509 #if defined(CONFIG_NXWM_UNITTEST) && !defined(CONFIG_NXWM_TOUCHSCREEN)
511 {
512  // Get the size and position of the widget
513 
514  struct nxgl_size_s imageSize;
515  m_minimizeImage->getSize(imageSize);
516 
517  struct nxgl_point_s imagePos;
518  m_minimizeImage->getPos(imagePos);
519 
520  // And click or release the image at its center
521 
522  if (click)
523  {
524  m_minimizeImage->click(imagePos.x + (imageSize.w >> 1),
525  imagePos.y + (imageSize.h >> 1));
526  }
527  else
528  {
529  m_minimizeImage->release(imagePos.x + (imageSize.w >> 1),
530  imagePos.y + (imageSize.h >> 1));
531  }
532 }
533 #endif
534 
535 /**
536  * Simulate a mouse click or release on the stop icon. This method
537  * is only available for automated testing of NxWM.
538  *
539  * @param click. True to click; false to release;
540  */
541 
542 #if defined(CONFIG_NXWM_UNITTEST) && !defined(CONFIG_NXWM_TOUCHSCREEN)
544 {
545  // The stop icon will not be available for "persistent" applications
546 
547  if (m_stopImage)
548  {
549  // Get the size and position of the widget
550 
551  struct nxgl_size_s imageSize;
552  m_stopImage->getSize(imageSize);
553 
554  struct nxgl_point_s imagePos;
555  m_stopImage->getPos(imagePos);
556 
557  // And click or release the image at its center
558 
559  if (click)
560  {
561  m_stopImage->click(imagePos.x + (imageSize.w >> 1),
562  imagePos.y + (imageSize.h >> 1));
563  }
564  else
565  {
566  m_stopImage->release(imagePos.x + (imageSize.w >> 1),
567  imagePos.y + (imageSize.h >> 1));
568  }
569  }
570 }
571 #endif
572 
573 /**
574  * Handle a widget action event. For CImage, this is a mouse button pre-release event.
575  *
576  * @param e The event data.
577  */
578 
580 {
581  // Ignore the event if no callback is registered
582 
583  if (m_callback)
584  {
585  // Check the stop application image
586 
588  {
589  // Notify the controlling logic that the application should be stopped
590 
591  m_callback->close();
592  }
593 
594  // Check the minimize image (only if the stop application image is not pressed)
595 
597  {
598  // Notify the controlling logic that the application should be miminsed
599 
600  m_callback->minimize();
601  }
602  }
603 }
604 
NXWidgets::CImage * m_stopImage
bool isClicked(void) const
Definition: cnxwidget.hxx:560
NXWidgets::CNxFont * m_windowFont
const nxgl_coord_t getHeight(void) const
CGraphicsPort * getGraphicsPort(void)
void disableDrawing(void)
Definition: cnxwidget.hxx:908
void setRaisesEvents(bool raises)
Definition: cnxwidget.hxx:887
CApplicationWindow(NXWidgets::CNxTkWindow *window, uint8_t flags=0)
const nxgl_coord_t getWidth(void) const
void setWindowLabel(NXWidgets::CNxString &appname)
CNxToolbar * openToolbar(nxgl_coord_t height, CWidgetControl *widgetControl=(CWidgetControl *) 0)
CWidgetControl * getWidgetControl(void) const
Definition: cnxtoolbar.cxx:133
virtual void setTextAlignmentHoriz(TextAlignmentHoriz alignment)
Definition: clabel.cxx:145
NXWidgets::CNxToolbar * m_toolbar
virtual bool click(nxgl_coord_t x, nxgl_coord_t y)
Definition: cnxwidget.cxx:630
virtual void minimize(void)=0
void getPos(struct nxgl_point_s &pos) const
Definition: cnxwidget.hxx:616
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
Definition: cnxwidget.hxx:854
const struct NXWidgets::SRlePaletteBitmap CONFIG_NXWM_MINIMIZE_BITMAP
void block(IApplication *app)
bool getSize(FAR struct nxgl_size_s *pSize)
Definition: cnxtoolbar.cxx:175
virtual void close(void)=0
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
void clickMinimizePosition(bool click)
void registerCallbacks(IApplicationCallback *callback)
const struct NXWidgets::SRlePaletteBitmap CONFIG_NXWM_STOP_BITMAP
void enableDrawing(void)
Definition: cnxwidget.hxx:917
void getSize(struct nxgl_size_s &size) const
Definition: cnxwidget.hxx:604
virtual void setTextAlignmentVert(TextAlignmentVert alignment)
Definition: clabel.cxx:158
NXWidgets::CLabel * m_windowLabel
NXWidgets::INxWindow * getWindow(void) const
NXWidgets::CWidgetControl * getWidgetControl(void) const
NXWidgets::CNxTkWindow * m_window
void handleActionEvent(const NXWidgets::CWidgetEventArgs &e)
NXWidgets::CImage * m_minimizeImage
bool release(nxgl_coord_t x, nxgl_coord_t y)
Definition: cnxwidget.cxx:771
NXWidgets::CRlePaletteBitmap * m_minimizeBitmap
CWidgetControl * getWidgetControl(void) const
IApplicationCallback * m_callback
virtual void setFont(CNxFont *font)
Definition: clabel.cxx:259
NXWidgets::CRlePaletteBitmap * m_stopBitmap
virtual void setText(const CNxString &text)
Definition: clabel.cxx:171
void setBorderless(bool isBorderless)
Definition: cnxwidget.cxx:461