NXWidgets  1.19
cwidgeteventhandlerlist.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/cwidgeteventhandlerlist.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 <cstdint>
77 #include <cstdbool>
78 
79 #include "cnxwidget.hxx"
80 #include "cwidgeteventhandler.hxx"
81 #include "cwidgeteventargs.hxx"
82 
83 /****************************************************************************
84  * Pre-Processor Definitions
85  ****************************************************************************/
86 
87 #define DOUBLE_CLICK_BOUNDS 10
88 
89 /****************************************************************************
90  * Method Implementations
91  ****************************************************************************/
92 
93 using namespace NXWidgets;
94 
95 /**
96  * Constructor.
97  *
98  * @param widget The owning widget.
99  */
100 
102 {
103  m_widget = widget;
104  m_isEnabled = true;
105 }
106 
107 /**
108  * Check if the object raises events or not.
109  *
110  * @return True if events are enabled.
111  */
112 
114 {
115  return m_isEnabled && !m_widget->isDeleted();
116 }
117 
118 /**
119  * Adds a widget event handler. The event handler will receive
120  * all events raised by this object.
121  * @param eventHandler A pointer to the event handler.
122  */
123 
125 {
126  // Prevent insertion if the handler already exists
127 
128  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
129  {
130  if (m_widgetEventHandlers.at(i) == eventHandler)
131  {
132  return;
133  }
134  }
135 
136  // Add the new handler
137 
138  m_widgetEventHandlers.push_back(eventHandler);
139 }
140 
141 /**
142  * Remove a widget event handler.
143  *
144  * @param eventHandler A pointer to the event handler to remove.
145  */
146 
148 {
149  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
150  {
151  if (m_widgetEventHandlers.at(i) == eventHandler)
152  {
153  m_widgetEventHandlers.erase(i);
154  return;
155  }
156  }
157 }
158 
159 /**
160  * Raise a click event to the event handler.
161  *
162  * @param x The x coordinate of the click.
163  * @param y The y coordinate of the click.
164  */
165 
166 void CWidgetEventHandlerList::raiseClickEvent(nxgl_coord_t x, nxgl_coord_t y)
167 {
168  if (isEnabled())
169  {
170  CWidgetEventArgs e(m_widget, x, y, 0, 0, KEY_CODE_NONE);
171 
172  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
173  {
174  m_widgetEventHandlers.at(i)->handleClickEvent(e);
175  }
176  }
177 }
178 
179 /**
180  * Raise a double-click event to the event handler.
181  *
182  * @param x The x coordinate of the click.
183  * @param y The y coordinate of the click.
184  */
185 
186 void CWidgetEventHandlerList::raiseDoubleClickEvent(nxgl_coord_t x, nxgl_coord_t y)
187 {
188  if (isEnabled())
189  {
190  CWidgetEventArgs e(m_widget, x, y, 0, 0, KEY_CODE_NONE);
191 
192  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
193  {
194  m_widgetEventHandlers.at(i)->handleDoubleClickEvent(e);
195  }
196  }
197 }
198 
199 /**
200  * Raise a mouse release event to the event handler.
201  *
202  * @param x The x coordinate of the release.
203  * @param y The y coordinate of the release.
204  */
205 
206 void CWidgetEventHandlerList::raiseReleaseEvent(nxgl_coord_t x, nxgl_coord_t y)
207 {
208  if (isEnabled())
209  {
210  CWidgetEventArgs e(m_widget, x, y, 0, 0, KEY_CODE_NONE);
211 
212  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
213  {
214  m_widgetEventHandlers.at(i)->handleReleaseEvent(e);
215  }
216  }
217 }
218 
219 /**
220  * Raise a mouse release-outside event to the event handler.
221  *
222  * @param x The x coordinate of the release.
223  * @param y The y coordinate of the release.
224  */
225 
226 void CWidgetEventHandlerList::raiseReleaseOutsideEvent(nxgl_coord_t x, nxgl_coord_t y)
227 {
228  if (isEnabled())
229  {
230  CWidgetEventArgs e(m_widget, x, y, 0, 0, KEY_CODE_NONE);
231 
232  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
233  {
234  m_widgetEventHandlers.at(i)->handleReleaseOutsideEvent(e);
235  }
236  }
237 }
238 
239 /**
240  * Raise a mouse drag event to the event handler.
241  *
242  * @param x The x coordinate of the mouse when the drag started.
243  * @param y The y coordinate of the mouse when the drag started.
244  * @param vX The horizontal distance dragged.
245  * @param vY The vertical distance dragged.
246  */
247 
248 void CWidgetEventHandlerList::raiseDragEvent(nxgl_coord_t x, nxgl_coord_t y,
249  nxgl_coord_t vX, nxgl_coord_t vY)
250 {
251  if (isEnabled())
252  {
253  CWidgetEventArgs e(m_widget, x, y, vX, vY, KEY_CODE_NONE);
254 
255  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
256  {
257  m_widgetEventHandlers.at(i)->handleDragEvent(e);
258  }
259  }
260 }
261 
262 /**
263  * Raise a widget drop event to the event handler.
264  *
265  * @param x The x coordinate of the mouse when the drop occurred.
266  * @param y The y coordinate of the mouse when the drop occurred.
267  */
268 
269 void CWidgetEventHandlerList::raiseDropEvent(nxgl_coord_t x, nxgl_coord_t y)
270 {
271  if (isEnabled())
272  {
273  CWidgetEventArgs e(m_widget, x, y, 0, 0, KEY_CODE_NONE);
274 
275  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
276  {
277  m_widgetEventHandlers.at(i)->handleDropEvent(e);
278  }
279  }
280 }
281 
282 /**
283  * Raise a key press event to the event handler.
284  *
285  * @param key The code of the key that caused the event.
286  */
287 
289 {
290  if (isEnabled())
291  {
292  CWidgetEventArgs e(m_widget, 0, 0, 0, 0, key);
293 
294  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
295  {
296  m_widgetEventHandlers.at(i)->handleKeyPressEvent(e);
297  }
298  }
299 }
300 
301 /**
302  * Raise a cursor control event to the event handler.
303  *
304  * @param cursorControl The cursor control code that caused the event.
305  */
306 
308 {
309  if (isEnabled())
310  {
311  CWidgetEventArgs e(m_widget, 0, 0, 0, 0, (nxwidget_char_t)cursorControl);
312 
313  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
314  {
315  m_widgetEventHandlers.at(i)->handleCursorControlEvent(e);
316  }
317  }
318 }
319 
320 /**
321  * Raise a focus event to the event handler.
322  */
323 
325 {
326  if (isEnabled())
327  {
328  CWidgetEventArgs e(m_widget, 0, 0, 0, 0, KEY_CODE_NONE);
329 
330  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
331  {
332  m_widgetEventHandlers.at(i)->handleFocusEvent(e);
333  }
334  }
335 }
336 
337 /**
338  * Raise a blur event to the event handler.
339  */
340 
342 {
343  if (isEnabled())
344  {
345  CWidgetEventArgs e(m_widget, 0, 0, 0, 0, KEY_CODE_NONE);
346 
347  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
348  {
349  m_widgetEventHandlers.at(i)->handleBlurEvent(e);
350  }
351  }
352 }
353 
354 /**
355  * Raise a close event to the event handler.
356  */
357 
359 {
360  if (isEnabled())
361  {
362  CWidgetEventArgs e(m_widget, 0, 0, 0, 0, KEY_CODE_NONE);
363 
364  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
365  {
366  m_widgetEventHandlers.at(i)->handleCloseEvent(e);
367  }
368  }
369 }
370 
371 /**
372  * Raise a hide event to the event handler.
373  */
374 
376 {
377  if (isEnabled())
378  {
379  CWidgetEventArgs e(m_widget, 0, 0, 0, 0, KEY_CODE_NONE);
380 
381  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
382  {
383  m_widgetEventHandlers.at(i)->handleHideEvent(e);
384  }
385  }
386 }
387 
388 /**
389  * Raise a show event to the event handler.
390  */
391 
393 {
394  if (isEnabled())
395  {
396  CWidgetEventArgs e(m_widget, 0, 0, 0, 0, KEY_CODE_NONE);
397 
398  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
399  {
400  m_widgetEventHandlers.at(i)->handleShowEvent(e);
401  }
402  }
403 }
404 
405 /**
406  * Raise an enable event to the event handler.
407  */
408 
410 {
411  if (isEnabled())
412  {
413  CWidgetEventArgs e(m_widget, 0, 0, 0, 0, KEY_CODE_NONE);
414 
415  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
416  {
417  m_widgetEventHandlers.at(i)->handleEnableEvent(e);
418  }
419  }
420 }
421 
422 /**
423  * Raise a disable event to the event handler.
424  */
425 
427 {
428  if (isEnabled())
429  {
430  CWidgetEventArgs e(m_widget, 0, 0, 0, 0, KEY_CODE_NONE);
431 
432  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
433  {
434  m_widgetEventHandlers.at(i)->handleDisableEvent(e);
435  }
436  }
437 }
438 
439 /**
440  * Raise a value change event to the event handler.
441  */
442 
444 {
445  if (isEnabled())
446  {
447  CWidgetEventArgs e(m_widget, 0, 0, 0, 0, KEY_CODE_NONE);
448 
449  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
450  {
451  m_widgetEventHandlers.at(i)->handleValueChangeEvent(e);
452  }
453  }
454 }
455 
456 /**
457  * Raise a resize event to the event handler.
458  *
459  * @param width The new width of the widget.
460  * @param height The new height of the widget.
461  */
462 
463 void CWidgetEventHandlerList::raiseResizeEvent(nxgl_coord_t width, nxgl_coord_t height)
464 {
465  if (isEnabled())
466  {
467  CWidgetEventArgs e(m_widget, 0, 0, 0, 0, KEY_CODE_NONE);
468 
469  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
470  {
471  m_widgetEventHandlers.at(i)->handleResizeEvent(e);
472  }
473  }
474 }
475 
476 /**
477  * Raise a move event to the event handler.
478  *
479  * @param x The new x coordinate of the widget.
480  * @param y The new y coordinate of the widget.
481  * @param vX The horizontal distance moved.
482  * @param vY The vertical distance moved.
483  */
484 
485 void CWidgetEventHandlerList::raiseMoveEvent(nxgl_coord_t x, nxgl_coord_t y,
486  nxgl_coord_t vX, nxgl_coord_t vY)
487 {
488  if (isEnabled())
489  {
490  CWidgetEventArgs e(m_widget, x, y, vX, vY, KEY_CODE_NONE);
491 
492  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
493  {
494  m_widgetEventHandlers.at(i)->handleMoveEvent(e);
495  }
496  }
497 }
498 
499 /**
500  * Raise an action event to the event handler. This should be called
501  * when a widget's purpose has been fulfilled. For example, in the case
502  * of a button, this event is raised when the button is released within
503  * its boundaries. The button has produced a valid click, and thus
504  * fulfilled its purpose, so it needs to raise an "action" event.
505  */
506 
508 {
509  if (isEnabled())
510  {
511  CWidgetEventArgs e(m_widget, 0, 0, 0, 0, KEY_CODE_NONE);
512 
513  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
514  {
515  m_widgetEventHandlers.at(i)->handleActionEvent(e);
516  }
517  }
518 }
519 
520 /**
521  * Raises a scroll event. Fired when the panel scrolls.
522  *
523  * @param vX Horizontal distance scrolled.
524  * @param vY Vertical distance scrolled.
525  */
526 
527 void CWidgetEventHandlerList::raiseScrollEvent(nxgl_coord_t vX, nxgl_coord_t vY)
528 {
529  if (isEnabled())
530  {
531  CWidgetEventArgs e(m_widget, 0, 0, vX, vY, KEY_CODE_NONE);
532 
533  for (int i = 0; i < m_widgetEventHandlers.size(); i++)
534  {
535  m_widgetEventHandlers.at(i)->handleScrollEvent(e);
536  }
537  }
538 }
void raiseScrollEvent(nxgl_coord_t vX, nxgl_coord_t vY)
void raiseDragEvent(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t vX, nxgl_coord_t vY)
void raiseResizeEvent(nxgl_coord_t width, nxgl_coord_t height)
void raiseCursorControlEvent(ECursorControl cursorControl)
void raiseDoubleClickEvent(nxgl_coord_t x, nxgl_coord_t y)
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
void raiseMoveEvent(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t vX, nxgl_coord_t vY)
bool isDeleted(void) const
Definition: cnxwidget.cxx:304
uint16_t nxwidget_char_t
Definition: nxconfig.hxx:454
TNxArray< CWidgetEventHandler * > m_widgetEventHandlers
void raiseReleaseOutsideEvent(nxgl_coord_t x, nxgl_coord_t y)
void raiseClickEvent(nxgl_coord_t x, nxgl_coord_t y)
void removeWidgetEventHandler(CWidgetEventHandler *eventHandler)
void raiseReleaseEvent(nxgl_coord_t x, nxgl_coord_t y)
void raiseDropEvent(nxgl_coord_t x, nxgl_coord_t y)