NXWidgets  1.19
cscrollinglistbox.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/cscrollinglistbox.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 <stdint.h>
77 #include <stdbool.h>
78 
79 #include "cscrollinglistbox.hxx"
80 #include "cscrollbarvertical.hxx"
81 #include "cgraphicsport.hxx"
82 
83 /****************************************************************************
84  * Pre-Processor Definitions
85  ****************************************************************************/
86 
87 /****************************************************************************
88  * Method Implementations
89  ****************************************************************************/
90 
91 using namespace NXWidgets;
92 
93 /**
94  * Constructor.
95  *
96  * @param pWidgetControl The widget control for the display.
97  * @param x The x coordinate of the widget.
98  * @param y The y coordinate of the widget.
99  * @param width The width of the widget.
100  * @param height The height of the widget.
101  * @param style The style that the widget should use. If this is not
102  * specified, the widget will use the values stored in the global
103  * g_defaultWidgetStyle object. The widget will copy the properties of
104  * the style into its own internal style object.
105  */
106 
108  nxgl_coord_t x, nxgl_coord_t y,
109  nxgl_coord_t width, nxgl_coord_t height,
110  CWidgetStyle *style)
111 : CNxWidget(pWidgetControl, x, y, width, height, 0, style)
112 {
113  m_scrollbarWidth = 10;
114 
115  setBorderless(true);
116 
117  m_listbox = new CListBox(pWidgetControl, 0, 0, width - m_scrollbarWidth,
118  height, &m_style);
120 
121  // Create scrollbar
122 
123  CRect rect;
124  m_listbox->getClientRect(rect);
125  m_scrollbar = new CScrollbarVertical(pWidgetControl, width - m_scrollbarWidth,
126  0, m_scrollbarWidth, height, &m_style);
131 
132  // Add children to child array
133 
136 }
137 
138 /**
139  * Add a new option to the widget using default colors.
140  *
141  * @param text Text to show in the option.
142  * @param value The value of the option.
143  */
144 
145 void CScrollingListBox::addOption(const CNxString &text, const uint32_t value)
146 {
147  m_listbox->addOption(text, value);
149 }
150 
151 /**
152  * Add an option to the widget.
153  *
154  * @param option The option to add.
155  */
156 
158 {
159  m_listbox->addOption(item);
161 }
162 
163 /**
164  * Add a new option to the widget.
165  *
166  * @param text Text to show in the option.
167  * @param value The value of the option.
168  * @param normalTextColor Color to draw the text with when not selected.
169  * @param normalBackColor Color to draw the background with when not selected.
170  * @param selectedTextColor Color to draw the text with when selected.
171  * @param selectedBackColor Color to draw the background with when selected.
172  */
173 
174 void CScrollingListBox::addOption(const CNxString &text, const uint32_t value,
175  const nxwidget_pixel_t normalTextColor,
176  const nxwidget_pixel_t normalBackColor,
177  const nxwidget_pixel_t selectedTextColor,
178  const nxwidget_pixel_t selectedBackColor)
179 {
180  m_listbox->addOption(text, value, normalTextColor, normalBackColor,
181  selectedTextColor, selectedBackColor);
183 }
184 
185 /**
186  * Remove an option from the widget by its index.
187  *
188  * @param index The index of the option to remove.
189  */
190 
191 void CScrollingListBox::removeOption(const int index)
192 {
193  m_listbox->removeOption(index);
195 
196  // Reposition grip if necessary
197 
199  {
200  m_scrollbar->setValue(0);
201  }
202 }
203 
204 /**
205  * Remove all options from the widget.
206  */
207 
209 {
212  m_scrollbar->setValue(0);
213 }
214 
215 /**
216  * Handles events raised by its sub-widgets.
217  *
218  * @param e Event arguments.
219  */
220 
222 {
223  if (e.getSource() != NULL)
224  {
225  if (e.getSource() == m_scrollbar)
226  {
227  if (m_listbox != NULL)
228  {
229  m_listbox->setRaisesEvents(false);
231  m_listbox->setRaisesEvents(true);
232  }
233  }
234  else if (e.getSource() == m_listbox)
235  {
237  }
238  }
239 }
240 
241 /**
242  * Handle a widget action event.
243  *
244  * @param e The event data.
245  */
246 
248 {
249  if (e.getSource() != NULL)
250  {
251  if (e.getSource() == m_listbox)
252  {
253  // Raise action events from list box to event handler
254 
256  }
257  }
258 }
259 
260 /**
261  * Handles events raised by its sub-widgets.
262  *
263  * @param e Event arguments.
264  */
265 
267 {
268  if (e.getSource() != NULL)
269  {
270  if (e.getSource() == m_listbox)
271  {
272  if (m_scrollbar != NULL)
273  {
275 
276  int value = ((0 - m_listbox->getCanvasY()) << 16) / m_listbox->getOptionHeight();
277 
280  }
281  }
282  }
283 }
284 
285 /**
286  * Handle a mouse button click event.
287  *
288  * @param e The event data.
289  */
290 
292 {
294 }
295 
296 /**
297  * Handles events raised by its sub-widgets.
298  *
299  * @param e Event arguments.
300  */
301 
303 {
305 }
306 
307 /**
308  * Handle a mouse button release event that occurred within the bounds of
309  * the source widget.
310  *
311  * @param e The event data.
312  */
313 
315 {
317 }
318 
319 /**
320  * Handle a mouse button release event that occurred outside the bounds of
321  * the source widget.
322  *
323  * @param e The event data.
324  */
325 
327 {
328  // Child raised a release outside event, but we need to raise a different
329  // event if the release occurred within the bounds of this parent widget
330 
331  if (checkCollision(e.getX(), e.getY()))
332  {
334  }
335  else
336  {
338  }
339 }
340 
341 /**
342  * Set the font used in the textbox.
343  *
344  * @param font Pointer to the new font.
345  */
346 
348 {
349  m_style.font = font;
350  m_listbox->setFont(font);
351  m_scrollbar->setFont(font);
352 }
353 
354 /**
355  * Insert the dimensions that this widget wants to have into the rect
356  * passed in as a parameter. All coordinates are relative to the widget's
357  * parent. Value is based on the length of the largest string in the
358  * set of options.
359  *
360  * @param rect Reference to a rect to populate with data.
361  */
362 
364 {
365  // Get the listbox's preferred dimensions
366 
368 
369  // Add on the scrollbar width
370 
371  nxgl_coord_t width = rect.getWidth();
372  rect.setWidth(width + m_scrollbarWidth);
373 }
374 
375 /**
376  * Draw the area of this widget that falls within the clipping region.
377  * Called by the redraw() function to draw all visible regions.
378  *
379  * @param port The CGraphicsPort to draw to.
380  * @see redraw()
381  */
382 
384 {
386 }
387 
388 /**
389  * Resize the listbox to the new dimensions.
390  *
391  * @param width The new width.
392  * @param height The new height.
393  */
394 
395 void CScrollingListBox::onResize(nxgl_coord_t width, nxgl_coord_t height)
396 {
397  // Resize the children
398 
399  m_listbox->resize(width - m_scrollbarWidth, height);
401 
402  // Adjust scrollbar page size
403 
404  CRect rect;
405  getClientRect(rect);
407 
408  // Move the scrollbar
409 
410  m_scrollbar->moveTo(width - m_scrollbarWidth, 0);
411 }
412 
413 
414 
415 
virtual void getPreferredDimensions(CRect &rect) const
Definition: clistbox.cxx:371
void setMinimumValue(const int value)
void setValueWithBitshift(const int32_t value)
const T & getSource(void) const
Definition: teventargs.hxx:127
virtual void removeOption(const int index)
nxgl_coord_t getHeight(void) const
Definition: crect.hxx:204
virtual void removeOption(const int index)
Definition: clistbox.cxx:167
virtual const int getOptionCount(void) const
Definition: clistbox.hxx:365
bool moveTo(nxgl_coord_t x, nxgl_coord_t y)
Definition: cnxwidget.cxx:990
void setRaisesEvents(bool raises)
Definition: cnxwidget.hxx:887
nxgl_coord_t getWidth(void) const
Definition: crect.hxx:181
CScrollingListBox(const CScrollingListBox &scrollingListBox)
nxgl_mxpixel_t getBackgroundColor(void) const
Definition: cnxwidget.hxx:716
virtual void drawContents(CGraphicsPort *port)
void raiseDoubleClickEvent(nxgl_coord_t x, nxgl_coord_t y)
uint8_t nxwidget_pixel_t
Definition: nxconfig.hxx:442
virtual void setFont(CNxFont *font)
bool resize(nxgl_coord_t width, nxgl_coord_t height)
Definition: cnxwidget.cxx:1079
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
const nxgl_coord_t getX(void) const
nxgl_coord_t getHeight(void) const
Definition: cnxwidget.hxx:593
virtual void handleActionEvent(const CWidgetEventArgs &e)
virtual void handleDoubleClickEvent(const CWidgetEventArgs &e)
void getClientRect(CRect &rect) const
Definition: cnxwidget.cxx:421
virtual const nxgl_coord_t getOptionHeight(void) const
Definition: clistbox.cxx:330
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
Definition: cnxwidget.hxx:854
virtual void handleReleaseOutsideEvent(const CWidgetEventArgs &e)
CScrollbarVertical * m_scrollbar
virtual void handleValueChangeEvent(const CWidgetEventArgs &e)
virtual void setFont(CNxFont *font)
Definition: cnxwidget.cxx:474
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
virtual void handleReleaseEvent(const CWidgetEventArgs &e)
virtual bool checkCollision(nxgl_coord_t x, nxgl_coord_t y) const
Definition: cnxwidget.cxx:1229
void raiseReleaseOutsideEvent(nxgl_coord_t x, nxgl_coord_t y)
const nxgl_coord_t getY(void) const
virtual void addOption(const CNxString &text, const uint32_t value)
void raiseClickEvent(nxgl_coord_t x, nxgl_coord_t y)
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
virtual const int32_t getCanvasY(void) const
void addWidget(CNxWidget *widget)
Definition: cnxwidget.cxx:1293
void setWidth(nxgl_coord_t width)
Definition: crect.hxx:250
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
void setMaximumValue(const int value)
CWidgetStyle m_style
Definition: cnxwidget.hxx:183
virtual void addOption(const CNxString &text, const uint32_t value)
Definition: clistbox.cxx:143
virtual void removeAllOptions(void)
Definition: clistbox.cxx:176
virtual void getPreferredDimensions(CRect &rect) const
void setBorderless(bool isBorderless)
Definition: cnxwidget.cxx:461
virtual void jump(int32_t x, int32_t y)
void setPageSize(const nxgl_coord_t pageSize)
virtual void handleScrollEvent(const CWidgetEventArgs &e)
void raiseReleaseEvent(nxgl_coord_t x, nxgl_coord_t y)
virtual void handleClickEvent(const CWidgetEventArgs &e)