NXWidgets  1.19
cscrollinglistbox.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/include/cscrollinglistbox.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_CSCROLLINGLISTBOX_HXX
71 #define __INCLUDE_CSCROLLINGLISTBOX_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 
84 #include "cnxwidget.hxx"
85 #include "clistbox.hxx"
86 #include "cwidgeteventhandler.hxx"
87 #include "clistdata.hxx"
88 #include "clistboxdataitem.hxx"
89 #include "cwidgetstyle.hxx"
90 #include "ilistbox.hxx"
91 
92 /****************************************************************************
93  * Pre-Processor Definitions
94  ****************************************************************************/
95 
96 /****************************************************************************
97  * Implementation Classes
98  ****************************************************************************/
99 
100 #if defined(__cplusplus)
101 
102 namespace NXWidgets
103 {
104  class CScrollbarVertical;
105  class CWidgetControl;
106 
107  /**
108  * Widget containing a CListBox and a vertical scrollbar. Exposed
109  * methods are more or less identical to the methods exposed by the CListBox
110  * to ensure that the two are interchangeable.
111  */
112 
113  class CScrollingListBox : public IListBox, public CNxWidget,
114  public CWidgetEventHandler
115  {
116  protected:
117  CListBox *m_listbox; /**< Pointer to the list box. */
118  CScrollbarVertical *m_scrollbar; /**< Pointer to the scrollbar. */
119  uint8_t m_scrollbarWidth; /**< Width of the scrollbar. */
120 
121  /**
122  * Draw the area of this widget that falls within the clipping region.
123  * Called by the redraw() function to draw all visible regions.
124  *
125  * @param port The CGraphicsPort to draw to.
126  * @see redraw()
127  */
128 
129  virtual void drawContents(CGraphicsPort *port);
130 
131  /**
132  * Resize the listbox to the new dimensions.
133  *
134  * @param width The new width.
135  * @param height The new height.
136  */
137 
138  virtual void onResize(nxgl_coord_t width, nxgl_coord_t height);
139 
140  /**
141  * Destructor.
142  */
143 
144  virtual inline ~CScrollingListBox(void) { }
145 
146  /**
147  * Copy constructor is protected to prevent usage.
148  */
149 
150  inline CScrollingListBox(const CScrollingListBox &scrollingListBox)
151  : CNxWidget(scrollingListBox) { }
152 
153  public:
154 
155  /**
156  * Constructor.
157  *
158  * @param pWidgetControl The widget control for the display.
159  * @param x The x coordinate of the widget.
160  * @param y The y coordinate of the widget.
161  * @param width The width of the widget.
162  * @param height The height of the widget.
163  * @param style The style that the widget should use. If this is not
164  * specified, the widget will use the values stored in the global
165  * g_defaultWidgetStyle object. The widget will copy the properties of
166  * the style into its own internal style object.
167  */
168 
169  CScrollingListBox(CWidgetControl *pWidgetControl,
170  nxgl_coord_t x, nxgl_coord_t y,
171  nxgl_coord_t width, nxgl_coord_t height,
172  CWidgetStyle *style = (CWidgetStyle *)NULL);
173 
174  /**
175  * Add a new option to the widget using default colors.
176  *
177  * @param text Text to show in the option.
178  * @param value The value of the option.
179  */
180 
181  virtual void addOption(const CNxString &text, const uint32_t value);
182 
183  /**
184  * Add an option to the widget.
185  *
186  * @param option The option to add.
187  */
188 
189  virtual void addOption(CListBoxDataItem *option);
190 
191  /**
192  * Add a new option to the widget.
193  *
194  * @param text Text to show in the option.
195  * @param value The value of the option.
196  * @param normalTextColor Color to draw the text with when not selected.
197  * @param normalBackColor Color to draw the background with when not selected.
198  * @param selectedTextColor Color to draw the text with when selected.
199  * @param selectedBackColor Color to draw the background with when selected.
200  */
201 
202  virtual void addOption(const CNxString &text, const uint32_t value,
203  const nxwidget_pixel_t normalTextColor,
204  const nxwidget_pixel_t normalBackColor,
205  const nxwidget_pixel_t selectedTextColor,
206  const nxwidget_pixel_t selectedBackColor);
207 
208  /**
209  * Remove an option from the widget by its index.
210  *
211  * @param index The index of the option to remove.
212  */
213 
214  virtual void removeOption(const int index);
215 
216  /**
217  * Remove all options from the widget.
218  */
219 
220  virtual void removeAllOptions(void);
221 
222  /**
223  * Select an option by its index. Does not deselect any other selected options.
224  * Redraws the widget and raises a value changed event.
225  *
226  * @param index The index of the option to select.
227  */
228 
229  virtual inline void selectOption(const int index)
230  {
231  m_listbox->selectOption(index);
232  }
233 
234  /**
235  * Select an option by its index. Does not deselect any other selected options.
236  * Redraws the widget and raises a value changed event.
237  *
238  * @param index The index of the option to select.
239  */
240 
241  virtual inline void deselectOption(const int index)
242  {
243  m_listbox->deselectOption(index);
244  }
245 
246  /**
247  * Select all options. Does nothing if the listbox does not allow multiple selections.
248  * Redraws the widget and raises a value changed event.
249  */
250 
251  virtual inline void selectAllOptions(void)
252  {
253  m_listbox->selectAllOptions();
254  }
255 
256  /**
257  * Deselect all options.
258  * Redraws the widget and raises a value changed event.
259  */
260 
261  virtual inline void deselectAllOptions(void)
262  {
263  m_listbox->deselectAllOptions();
264  }
265 
266  /**
267  * Get the selected index. Returns -1 if nothing is selected. If more than one
268  * option is selected, the index of the first selected option is returned.
269  *
270  * @return The selected index.
271  */
272 
273  virtual inline const int getSelectedIndex(void) const
274  {
275  return m_listbox->getSelectedIndex();
276  }
277 
278  /**
279  * Sets the selected index. Specify -1 to select nothing. Resets any
280  * other selected items to deselected.
281  * Redraws the widget and raises a value changed event.
282  *
283  * @param index The selected index.
284  */
285 
286  virtual inline void setSelectedIndex(const int index)
287  {
288  m_listbox->setSelectedIndex(index);
289  }
290 
291  /**
292  * Get the selected option. Returns NULL if nothing is selected.
293  *
294  * @return The selected option.
295  */
296 
297  virtual inline const CListBoxDataItem *getSelectedOption(void) const
298  {
299  return m_listbox->getSelectedOption();
300  }
301 
302  /**
303  * Sets whether multiple selections are possible or not.
304  *
305  * @param allowMultipleSelections True to allow multiple selections.
306  */
307 
308  virtual inline
309  void setAllowMultipleSelections(const bool allowMultipleSelections)
310  {
311  m_listbox->setAllowMultipleSelections(allowMultipleSelections);
312  }
313 
314  /**
315  * Sets whether multiple selections are possible or not.
316  *
317  * @return True if multiple selections are allowed.
318  */
319 
320  virtual inline const bool allowsMultipleSelections(void) const
321  {
322  return m_listbox->allowsMultipleSelections();
323  }
324 
325  /**
326  * Resize the scrolling canvas to encompass all options.
327  */
328 
329  virtual inline void resizeCanvas(void)
330  {
331  m_listbox->resizeCanvas();
332  }
333 
334  /**
335  * Get the specified option.
336  *
337  * @return The specified option.
338  */
339 
340  virtual inline const CListBoxDataItem *getOption(const int index)
341  {
342  return m_listbox->getOption(index);
343  }
344 
345  /**
346  * Get the selected index. Returns -1 if nothing is selected.
347  *
348  * @return The selected index.
349  */
350 
351  virtual inline const CListBoxDataItem *getOption(const int index) const
352  {
353  return m_listbox->getOption(index);
354  }
355 
356  /**
357  * Sort the options alphabetically by the text of the options.
358  */
359 
360  virtual inline void sort(void)
361  {
362  m_listbox->sort();
363  }
364 
365  /**
366  * Get the total number of options.
367  *
368  * @return The number of options.
369  */
370 
371  virtual inline const int getOptionCount(void) const
372  {
373  return m_listbox->getOptionCount();
374  }
375 
376  /**
377  * Get the height of a single option.
378  *
379  * @return The height of an option.
380  */
381 
382  virtual inline const nxgl_coord_t getOptionHeight(void) const
383  {
384  return m_listbox->getOptionHeight();
385  }
386 
387  /**
388  * Handles events raised by its sub-widgets.
389  *
390  * @param e Event arguments.
391  */
392 
393  virtual void handleValueChangeEvent(const CWidgetEventArgs &e);
394 
395  /**
396  * Handle a widget action event.
397  *
398  * @param e The event data.
399  */
400 
401  virtual void handleActionEvent(const CWidgetEventArgs &e);
402 
403  /**
404  * Handles events raised by its sub-widgets.
405  *
406  * @param e Event arguments.
407  */
408 
409  virtual void handleScrollEvent(const CWidgetEventArgs &e);
410 
411  /**
412  * Handle a mouse button click event.
413  *
414  * @param e The event data.
415  */
416 
417  virtual void handleClickEvent(const CWidgetEventArgs &e);
418 
419  /**
420  * Handles events raised by its sub-widgets.
421  *
422  * @param e Event arguments.
423  */
424 
425  virtual void handleDoubleClickEvent(const CWidgetEventArgs &e);
426 
427  /**
428  * Handle a mouse button release event that occurred within the bounds of
429  * the source widget.
430  *
431  * @param e The event data.
432  */
433 
434  virtual void handleReleaseEvent(const CWidgetEventArgs &e);
435 
436  /**
437  * Handle a mouse button release event that occurred outside the bounds of
438  * the source widget.
439  *
440  * @param e The event data.
441  */
442 
443  virtual void handleReleaseOutsideEvent(const CWidgetEventArgs &e);
444 
445  /**
446  * Set the font used in the textbox.
447  *
448  * @param font Pointer to the new font.
449  */
450 
451  virtual void setFont(CNxFont *font);
452 
453  /**
454  * Sets whether or not items added to the list are automatically
455  * sorted on insert or not.
456  *
457  * @param sortInsertedItems True to enable sort on insertion.
458  */
459 
460  virtual inline void setSortInsertedItems(const bool sortInsertedItems)
461  {
462  m_listbox->setSortInsertedItems(sortInsertedItems);
463  }
464 
465  /**
466  * Insert the dimensions that this widget wants to have into the rect
467  * passed in as a parameter. All coordinates are relative to the widget's
468  * parent. Value is based on the length of the largest string in the
469  * set of options.
470  *
471  * @param rect Reference to a rect to populate with data.
472  */
473 
474  virtual void getPreferredDimensions(CRect &rect) const;
475  };
476 }
477 
478 #endif // __cplusplus
479 
480 #endif // __INCLUDE_CSCROLLINGLISTBOX_HXX
virtual void removeOption(const int index)
virtual void sort(void)
Definition: clistbox.cxx:319
virtual const int getOptionCount(void) const
Definition: clistbox.hxx:365
virtual void deselectAllOptions(void)
virtual const bool allowsMultipleSelections(void) const
virtual const bool allowsMultipleSelections(void) const
Definition: clistbox.hxx:331
virtual void resizeCanvas(void)
Definition: clistbox.cxx:289
virtual void setSelectedIndex(const int index)
Definition: clistbox.cxx:269
CScrollingListBox(const CScrollingListBox &scrollingListBox)
virtual const CListBoxDataItem * getSelectedOption(void) const
virtual void selectOption(const int index)
virtual void setSortInsertedItems(const bool sortInsertedItems)
virtual void deselectAllOptions(void)
Definition: clistbox.cxx:243
virtual const int getSelectedIndex(void) const
virtual void drawContents(CGraphicsPort *port)
virtual void selectAllOptions(void)
Definition: clistbox.cxx:233
uint8_t nxwidget_pixel_t
Definition: nxconfig.hxx:442
virtual void setFont(CNxFont *font)
virtual const CListBoxDataItem * getOption(const int index)
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
virtual void deselectOption(const int index)
virtual void handleActionEvent(const CWidgetEventArgs &e)
virtual void handleDoubleClickEvent(const CWidgetEventArgs &e)
virtual const int getOptionCount(void) const
virtual const nxgl_coord_t getOptionHeight(void) const
Definition: clistbox.cxx:330
virtual void setSortInsertedItems(const bool sortInsertedItems)
Definition: clistbox.hxx:384
virtual const int getSelectedIndex(void) const
Definition: clistbox.cxx:256
virtual void handleReleaseOutsideEvent(const CWidgetEventArgs &e)
CScrollbarVertical * m_scrollbar
virtual void handleValueChangeEvent(const CWidgetEventArgs &e)
virtual const CListBoxDataItem * getSelectedOption(void) const
Definition: clistbox.cxx:280
virtual void handleReleaseEvent(const CWidgetEventArgs &e)
virtual void addOption(const CNxString &text, const uint32_t value)
virtual const CListBoxDataItem * getOption(const int index) const
virtual void deselectOption(const int index)
Definition: clistbox.cxx:222
virtual void setAllowMultipleSelections(const bool allowMultipleSelections)
Definition: clistbox.hxx:320
virtual void getPreferredDimensions(CRect &rect) const
virtual const nxgl_coord_t getOptionHeight(void) const
virtual const CListBoxDataItem * getOption(const int index)
Definition: clistbox.hxx:348
virtual void setAllowMultipleSelections(const bool allowMultipleSelections)
virtual void setSelectedIndex(const int index)
virtual void selectOption(const int index)
Definition: clistbox.cxx:210
virtual void handleScrollEvent(const CWidgetEventArgs &e)
virtual void handleClickEvent(const CWidgetEventArgs &e)