NXWidgets: ccyclebutton.hxx Source File

NXWidgets 1.19

ccyclebutton.hxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/libnxwidgets/include/ccyclebutton.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 all 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_CCYLEBUTTON_HXX
71 #define __INCLUDE_CCYLEBUTTON_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 "cbutton.hxx"
85 #include "ilistdataeventhandler.hxx"
86 #include "clistdata.hxx"
87 #include "clistdataitem.hxx"
88 #include "cwidgetstyle.hxx"
89 #include "cnxstring.hxx"
90
91 /****************************************************************************
92 * Pre-Processor Definitions
93 ****************************************************************************/
94
95 /****************************************************************************
96 * Implementation Classes
97 ****************************************************************************/
98
99 #if defined(__cplusplus)
100
101 namespace NXWidgets
102 {
103 /**
104 * Forward references
105 */
106
107 class CWidgetControl;
108
109 /**
110 * Cycle button widget. Displays text within the button. Clicking it cycles
111 * through its available options.
112 */
113
114 class CCycleButton : public CButton, public IListDataEventHandler
115 {
116 protected:
117
118 CListData m_options; /**< Option storage. */
119
120 /**
121 * Draw the area of this widget that falls within the clipping region.
122 * Called by the redraw() function to draw all visible regions.
123 *
124 * @param port The CGraphicsPort to draw to.
125 * @see redraw()
126 */
127
128 virtual void drawContents(CGraphicsPort *port);
129
130 /**
131 * Draw the area of this widget that falls within the clipping region.
132 * Called by the redraw() function to draw all visible regions.
133 *
134 * @param port The CGraphicsPort to draw to.
135 * @see redraw()
136 */
137
138 virtual void drawBorder(CGraphicsPort *port);
139
140 /**
141 * Draws the outline of the button.
142 *
143 * @param port Graphics port to draw to.
144 */
145
146 virtual void drawOutline(CGraphicsPort *port);
147
148 /**
149 * Selects the next option in the list and redraws the button.
150 *
151 * @param x The x coordinate of the mouse.
152 * @param y The y coordinate of the mouse.
153 */
154
155 virtual void onPreRelease(nxgl_coord_t x, nxgl_coord_t y);
156
157 /**
158 * Redraws the button.
159 *
160 * @param x The x coordinate of the mouse.
161 * @param y The y coordinate of the mouse.
162 */
163
164 virtual void onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y);
165
166 /**
167 * Prevents the CButton onResize() method from recalculating the text
168 * positions by overriding it.
169 *
170 * @param width The new width.
171 * @param height The new height.
172 */
173
174 virtual inline void onResize(nxgl_coord_t width, nxgl_coord_t height) { }
175
176 /**
177 * Override method in Label class to prevent recalculation of text positions.
178 */
179
180 virtual inline void calculateTextPosition(void) { }
181
182 /**
183 * Copy constructor is protected to prevent usage.
184 */
185
186 inline CCycleButton(const CCycleButton &cycleButton) : CButton(cycleButton) { }
187
188 public:
189
190 /**
191 * Constructor for cycle buttons.
192 *
193 * @param pWidgetControl The widget control for the display.
194 * @param x The x coordinate of the button, relative to its parent.
195 * @param y The y coordinate of the button, relative to its parent.
196 * @param width The width of the button.
197 * @param height The height of the button.
198 * @param style The style that the button should use. If this is not
199 * specified, the button will use the values stored in the global
200 * g_defaultWidgetStyle object. The button will copy the properties of
201 * the style into its own internal style object.
202 */
203
204 CCycleButton(CWidgetControl *pWidgetControl,
205 nxgl_coord_t x, nxgl_coord_t y,
206 nxgl_coord_t width, nxgl_coord_t height,
207 CWidgetStyle *style = (CWidgetStyle *)NULL);
208
209 /**
210 * Destructor.
211 */
212
213 virtual ~CCycleButton(void) { }
214
215 /**
216 * Add a new option to the widget.
217 *
218 * @param text The text of the option.
219 * @param value The value of the option.
220 */
221
222 void addOption(const CNxString &text, const uint32_t value);
223
224 /**
225 * Remove an option from the widget by its index.
226 *
227 * @param index The index of the option to remove.
228 */
229
230 virtual void removeOption(const int index);
231
232 /**
233 * Remove all options from the widget.
234 */
235
236 virtual void removeAllOptions(void);
237
238 /**
239 * Select an option by its index.
240 * Redraws the widget and raises a value changed event.
241 *
242 * @param index The index of the option to select.
243 */
244
245 virtual void selectOption(const int index);
246
247 /**
248 * Get the selected index. Returns -1 if nothing is selected. If more than one
249 * option is selected, the index of the first selected option is returned.
250 *
251 * @return The selected index.
252 */
253
254 virtual const int getSelectedIndex(void) const;
255
256 /**
257 * Sets the selected index. Specify -1 to select nothing. Resets any
258 * other selected options to deselected.
259 * Redraws the widget and raises a value changed event.
260 *
261 * @param index The selected index.
262 */
263
264 virtual void setSelectedIndex(const int index);
265
266 /**
267 * Get the selected option. Returns NULL if nothing is selected.
268 *
269 * @return The selected option.
270 */
271
272 virtual const CListDataItem *getSelectedOption(void) const;
273
274 /**
275 * Get the value of the current option.
276 *
277 * @return Value of the current option.
278 */
279
280 inline const uint32_t getValue(void) const
281 {
282 return getSelectedOption()->getValue();
283 }
284
285 /**
286 * Get the specified option.
287 *
288 * @return The specified option.
289 */
290
291 virtual inline const CListDataItem *getOption(const int index)
292 {
293 return m_options.getItem(index);
294 }
295
296 /**
297 * Sort the options alphabetically by the text of the options.
298 */
299
300 virtual void sort(void);
301
302 /**
303 * Get the total number of options.
304 *
305 * @return The number of options.
306 */
307
308 virtual inline const int getOptionCount(void) const
309 {
310 return m_options.getItemCount();
311 }
312
313 /**
314 * Sets whether or not items added to the list are automatically sorted
315 * on insert or not.
316 *
317 * @param sortInsertedItems True to enable sort on insertion.
318 */
319
320 virtual inline void setSortInsertedItems(const bool sortInsertedItems)
321 {
322 m_options.setSortInsertedItems(sortInsertedItems);
323 }
324
325 /**
326 * Handles list data changed events.
327 *
328 * @param e Event arguments.
329 */
330
331 virtual void handleListDataChangedEvent(const CListDataEventArgs &e);
332
333 /**
334 * Handles list selection changed events.
335 *
336 * @param e Event arguments.
337 */
338
339 virtual void handleListDataSelectionChangedEvent(const CListDataEventArgs &e);
340
341 /**
342 * Insert the dimensions that this widget wants to have into the rect
343 * passed in as a parameter. All coordinates are relative to the widget's
344 * parent. Value is based on the length of the largest string in the
345 * set of options.
346 *
347 * @param rect Reference to a rect to populate with data.
348 */
349
350 virtual void getPreferredDimensions(CRect &rect) const;
351 };
352 }
353
354 #endif // __cplusplus
355
356 #endif // __INCLUDE_CCYLEBUTTON_HXX
NXWidgets::CCycleButton::~CCycleButton
virtual ~CCycleButton(void)
Definition: ccyclebutton.hxx:213
NXWidgets::CCycleButton::getOption
virtual const CListDataItem * getOption(const int index)
Definition: ccyclebutton.hxx:291
NXWidgets::CWidgetStyle
Definition: cwidgetstyle.hxx:125
NXWidgets::CCycleButton::CCycleButton
CCycleButton(const CCycleButton &cycleButton)
Definition: ccyclebutton.hxx:186
NXWidgets::CCycleButton::getPreferredDimensions
virtual void getPreferredDimensions(CRect &rect) const
Definition: ccyclebutton.cxx:258
NXWidgets::CListData::getItem
virtual const CListDataItem * getItem(const int index) const
Definition: clistdata.hxx:260
NXWidgets::CCycleButton::removeAllOptions
virtual void removeAllOptions(void)
Definition: ccyclebutton.cxx:164
NXWidgets::CWidgetControl
Definition: cwidgetcontrol.hxx:95
clistdata.hxx
NXWidgets::IListDataEventHandler
Definition: ilistdataeventhandler.hxx:103
NXWidgets::CListDataItem
Definition: clistdataitem.hxx:103
NXWidgets::CCycleButton::onResize
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
Definition: ccyclebutton.hxx:174
NXWidgets::CCycleButton::getSelectedOption
virtual const CListDataItem * getSelectedOption(void) const
Definition: ccyclebutton.cxx:212
NXWidgets::CCycleButton::addOption
void addOption(const CNxString &text, const uint32_t value)
Definition: ccyclebutton.cxx:137
NXWidgets::CCycleButton::onPreRelease
virtual void onPreRelease(nxgl_coord_t x, nxgl_coord_t y)
Definition: ccyclebutton.cxx:427
cwidgetstyle.hxx
NXWidgets::CCycleButton::getOptionCount
virtual const int getOptionCount(void) const
Definition: ccyclebutton.hxx:308
NXWidgets::CListDataEventArgs
Definition: clistdataeventargs.hxx:103
NXWidgets::CCycleButton::handleListDataSelectionChangedEvent
virtual void handleListDataSelectionChangedEvent(const CListDataEventArgs &e)
Definition: ccyclebutton.cxx:243
NXWidgets::CListDataItem::getValue
const uint32_t getValue(void) const
Definition: clistdataitem.hxx:144
NXWidgets::CCycleButton::onReleaseOutside
virtual void onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y)
Definition: ccyclebutton.cxx:461
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CCycleButton::getSelectedIndex
virtual const int getSelectedIndex(void) const
Definition: ccyclebutton.cxx:188
NXWidgets::CCycleButton::removeOption
virtual void removeOption(const int index)
Definition: ccyclebutton.cxx:155
NXWidgets::CCycleButton::sort
virtual void sort(void)
Definition: ccyclebutton.cxx:221
NXWidgets::CListData::getItemCount
virtual const int getItemCount(void) const
Definition: clistdata.hxx:284
NXWidgets::CCycleButton
Definition: ccyclebutton.hxx:114
NXWidgets::CCycleButton::handleListDataChangedEvent
virtual void handleListDataChangedEvent(const CListDataEventArgs &e)
Definition: ccyclebutton.cxx:232
NXWidgets::CRect
Definition: crect.hxx:101
cbutton.hxx
NXWidgets::CListData::setSortInsertedItems
virtual void setSortInsertedItems(const bool sortInsertedItems)
Definition: clistdata.hxx:330
NXWidgets::CCycleButton::drawBorder
virtual void drawBorder(CGraphicsPort *port)
Definition: ccyclebutton.cxx:358
NXWidgets::CCycleButton::getValue
const uint32_t getValue(void) const
Definition: ccyclebutton.hxx:280
NXWidgets::CCycleButton::selectOption
virtual void selectOption(const int index)
Definition: ccyclebutton.cxx:176
NXWidgets::CNxString
Definition: cnxstring.hxx:121
clistdataitem.hxx
NXWidgets::CCycleButton::m_options
CListData m_options
Definition: ccyclebutton.hxx:118
cnxstring.hxx
NXWidgets::CListData
Definition: clistdata.hxx:107
NXWidgets::CButton
Definition: cbutton.hxx:109
NXWidgets::CCycleButton::setSortInsertedItems
virtual void setSortInsertedItems(const bool sortInsertedItems)
Definition: ccyclebutton.hxx:320
NXWidgets::CCycleButton::drawOutline
virtual void drawOutline(CGraphicsPort *port)
Definition: ccyclebutton.cxx:388
ilistdataeventhandler.hxx
NXWidgets::CCycleButton::setSelectedIndex
virtual void setSelectedIndex(const int index)
Definition: ccyclebutton.cxx:201
NXWidgets::CGraphicsPort
Definition: cgraphicsport.hxx:106
NXWidgets::CCycleButton::drawContents
virtual void drawContents(CGraphicsPort *port)
Definition: ccyclebutton.cxx:305
NXWidgets::CCycleButton::calculateTextPosition
virtual void calculateTextPosition(void)
Definition: ccyclebutton.hxx:180