NXWidgets: clistdata.hxx Source File

NXWidgets 1.19

clistdata.hxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/libnxwidgets/include/clistdata.hxx
3 *
4 * Copyright (C) 2012, 2016 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_CLISTDATA_HXX
71 #define __INCLUDE_CLISTDATA_HXX
72
73 /****************************************************************************
74 * Included Files
75 ****************************************************************************/
76
77 #include <nuttx/config.h>
78
79 #include <cstdint>
80 #include <cstdbool>
81
82 #include <nuttx/nx/nxglib.h>
83
84 #include "tnxarray.hxx"
85 #include "ilistdataeventhandler.hxx"
86 #include "clistdataitem.hxx"
87 #include "cnxstring.hxx"
88
89 /****************************************************************************
90 * Pre-Processor Definitions
91 ****************************************************************************/
92
93 /****************************************************************************
94 * Implementation Classes
95 ****************************************************************************/
96
97 #if defined(__cplusplus)
98
99 namespace NXWidgets
100 {
101 /**
102 * Class representing a list of items. Designed to be used by the
103 * CListBox class, etc, to store its data. Fires events to notify
104 * listeners when the list changes or a new selection is made.
105 */
106
107 class CListData
108 {
109 protected:
110 TNxArray<CListDataItem*> m_items; /**< Collection of list data items. */
111 TNxArray<IListDataEventHandler*> m_listDataEventhandlers; /**< Collection of event handlers. */
112 bool m_allowMultipleSelections; /**< If true, multiple options can
113 be selected. */
114 bool m_sortInsertedItems; /**< Automatically sorts items on
115 insertion if true. */
116
117 /**
118 * Quick sort the items using their compareTo() methods.
119 *
120 * @param start The index to start sorting at.
121 * @param end The index to stop sorting at.
122 */
123
124 virtual void quickSort(const int start, const int end);
125
126 /**
127 * Swap the locations of two items in the array.
128 *
129 * @param index1 The index of the first item to swap.
130 * @param index2 The index of the second item to swap.
131 */
132
133 virtual void swapItems(const int index1, const int index2);
134
135 /**
136 * Return the index that an item should be inserted at to maintain a sorted list of data.
137 *
138 * @param item The item to insert.
139 * @return The index that the item should be imserted into at.
140 */
141
142 const int getSortedInsertionIndex(const CListDataItem *item) const;
143
144 /**
145 * Raise a data changed event.
146 */
147
148 void raiseDataChangedEvent(void);
149
150 /**
151 * Raise a selection changed event.
152 */
153
154 void raiseSelectionChangedEvent(void);
155
156 public:
157
158 /**
159 * Constructor.
160 */
161 CListData(void);
162
163 /**
164 * Destructor.
165 */
166 virtual ~CListData(void);
167
168 /**
169 * Add a new item.
170 *
171 * @param text Text to show in the option.
172 * @param value The value of the option.
173 */
174
175 virtual void addItem(const CNxString &text, const uint32_t value);
176
177 /**
178 * Add an existing item. CListData becomes the owner of the option and will delete it
179 * when the list is deleted.
180 *
181 * @param item The item to add.
182 */
183
184 virtual void addItem(CListDataItem *item);
185
186 /**
187 * Remove an item by its index.
188 *
189 * @param index The index of the option to remove.
190 */
191
192 virtual void removeItem(const int index);
193
194 /**
195 * Select an item by its index.
196 *
197 * @param index The index of the item to select.
198 */
199
200 virtual void selectItem(const int index);
201
202 /**
203 * Deselect an item by its index.
204 *
205 * @param index The index of the item to select.
206 */
207
208 virtual void deselectItem(const int index);
209
210 /**
211 * Remove all items.
212 */
213
214 virtual void removeAllItems(void);
215
216 /**
217 * Get the selected index. Returns -1 if nothing is selected. If more than one
218 * item is selected, the index of the first selected item is returned.
219 *
220 * @return The selected index.
221 */
222
223 virtual const int getSelectedIndex(void) const;
224
225 /**
226 * Sets the selected index. Specify -1 to select nothing. Resets any
227 * other selected items to deselected.
228 *
229 * @param index The selected index.
230 */
231
232 virtual void setSelectedIndex(const int index);
233
234 /**
235 * Get the selected item. Returns NULL if nothing is selected.
236 *
237 * @return The selected option.
238 */
239
240 virtual const CListDataItem *getSelectedItem(void) const;
241
242 /**
243 * Sets whether multiple selections are possible or not.
244 *
245 * @param allowMultipleSelections True to allow multiple selections.
246 */
247
248 virtual inline void
249 setAllowMultipleSelections(const bool allowMultipleSelections)
250 {
251 m_allowMultipleSelections = allowMultipleSelections;
252 }
253
254 /**
255 * Get the specified item.
256 *
257 * @return The specified item.
258 */
259
260 virtual inline const CListDataItem *getItem(const int index) const
261 {
262 if (index < 0 || index >= m_items.size())
263 {
264 return (const CListDataItem *)0;
265 }
266 else
267 {
268 return m_items[index];
269 }
270 }
271
272 /**
273 * Sort the items using their compareTo() methods.
274 */
275
276 virtual void sort(void);
277
278 /**
279 * Get the total number of items.
280 *
281 * @return The number of items.
282 */
283
284 virtual inline const int getItemCount(void) const
285 {
286 return m_items.size();
287 }
288
289 /**
290 * Select all items. Does nothing if the list does not allow
291 * multiple selections.
292 */
293
294 virtual void selectAllItems(void);
295
296 /**
297 * Deselect all items.
298 */
299
300 virtual void deselectAllItems(void);
301
302 /**
303 * Select or deselect an item by its index. Does not deselect any
304 * other selected items. Set index to -1 to select nothing.
305 *
306 * @param index The index of the item to select.
307 * @param selected True to select the item, false to deselect it.
308 */
309
310 virtual void setItemSelected(const int index, const bool selected);
311
312 /**
313 * Returns whether multiple selections are possible or not.
314 *
315 * @return True if multiple selections are allowed.
316 */
317
318 virtual inline const bool allowsMultipleSelections(void) const
319 {
320 return m_allowMultipleSelections;
321 }
322
323 /**
324 * Sets whether or not items added to the list are automatically
325 * sorted on insert or not.
326 *
327 * @param sortInsertedItems True to enable sort on insertion.
328 */
329
330 virtual inline void setSortInsertedItems(const bool sortInsertedItems)
331 {
332 m_sortInsertedItems = sortInsertedItems;
333 }
334
335 /**
336 * Add an event handler.
337 *
338 * @param eventHandler The event handler to add.
339 */
340
341 inline void addListDataEventHandler(IListDataEventHandler *eventHandler)
342 {
343 m_listDataEventhandlers.push_back(eventHandler);
344 }
345
346 /**
347 * Remove an event handler.
348 *
349 * @param eventHandler The event handler to remove.
350 */
351
352 void removeListDataEventHandler(IListDataEventHandler *eventHandler);
353 };
354 }
355
356 #endif // __cplusplus
357
358 #endif // __INCLUDE_CLISTDATA_HXX
NXWidgets::CListData::CListData
CListData(void)
Definition: clistdata.cxx:97
NXWidgets::CListData::getItem
virtual const CListDataItem * getItem(const int index) const
Definition: clistdata.hxx:260
NXWidgets::CListData::raiseSelectionChangedEvent
void raiseSelectionChangedEvent(void)
Definition: clistdata.cxx:454
NXWidgets::CListData::quickSort
virtual void quickSort(const int start, const int end)
Definition: clistdata.cxx:365
NXWidgets::IListDataEventHandler
Definition: ilistdataeventhandler.hxx:103
NXWidgets::CListDataItem
Definition: clistdataitem.hxx:103
NXWidgets::CListData::removeItem
virtual void removeItem(const int index)
Definition: clistdata.cxx:159
NXWidgets::CListData::swapItems
virtual void swapItems(const int index1, const int index2)
Definition: clistdata.cxx:409
NXWidgets::CListData::raiseDataChangedEvent
void raiseDataChangedEvent(void)
Definition: clistdata.cxx:440
NXWidgets::CListData::removeListDataEventHandler
void removeListDataEventHandler(IListDataEventHandler *eventHandler)
Definition: clistdata.cxx:346
tnxarray.hxx
NXWidgets::CListData::getSelectedItem
virtual const CListDataItem * getSelectedItem(void) const
Definition: clistdata.cxx:255
NXWidgets::CListData::sort
virtual void sort(void)
Definition: clistdata.cxx:271
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CListData::setSelectedIndex
virtual void setSelectedIndex(const int index)
Definition: clistdata.cxx:244
NXWidgets::CListData::m_items
TNxArray< CListDataItem * > m_items
Definition: clistdata.hxx:110
NXWidgets::CListData::m_allowMultipleSelections
bool m_allowMultipleSelections
Definition: clistdata.hxx:112
NXWidgets::CListData::getSelectedIndex
virtual const int getSelectedIndex(void) const
Definition: clistdata.cxx:222
TNxArray
Definition: tnxarray.hxx:109
NXWidgets::CListData::getItemCount
virtual const int getItemCount(void) const
Definition: clistdata.hxx:284
NXWidgets::CListData::removeAllItems
virtual void removeAllItems(void)
Definition: clistdata.cxx:202
NXWidgets::CListData::deselectItem
virtual void deselectItem(const int index)
Definition: clistdata.cxx:193
NXWidgets::CListData::~CListData
virtual ~CListData(void)
Definition: clistdata.cxx:107
NXWidgets::CListData::addListDataEventHandler
void addListDataEventHandler(IListDataEventHandler *eventHandler)
Definition: clistdata.hxx:341
TNxArray::push_back
void push_back(const T &value)
Definition: tnxarray.hxx:267
NXWidgets::CListData::m_sortInsertedItems
bool m_sortInsertedItems
Definition: clistdata.hxx:114
NXWidgets::CListData::m_listDataEventhandlers
TNxArray< IListDataEventHandler * > m_listDataEventhandlers
Definition: clistdata.hxx:111
NXWidgets::CListData::selectItem
virtual void selectItem(const int index)
Definition: clistdata.cxx:182
NXWidgets::CListData::setSortInsertedItems
virtual void setSortInsertedItems(const bool sortInsertedItems)
Definition: clistdata.hxx:330
NXWidgets::CListData::setAllowMultipleSelections
virtual void setAllowMultipleSelections(const bool allowMultipleSelections)
Definition: clistdata.hxx:249
NXWidgets::CListData::deselectAllItems
virtual void deselectAllItems(void)
Definition: clistdata.cxx:299
NXWidgets::CNxString
Definition: cnxstring.hxx:121
NXWidgets::CListData::allowsMultipleSelections
virtual const bool allowsMultipleSelections(void) const
Definition: clistdata.hxx:318
NXWidgets::CListData::selectAllItems
virtual void selectAllItems(void)
Definition: clistdata.cxx:282
clistdataitem.hxx
NXWidgets::CListData::setItemSelected
virtual void setItemSelected(const int index, const bool selected)
Definition: clistdata.cxx:317
NXWidgets::CListData::getSortedInsertionIndex
const int getSortedInsertionIndex(const CListDataItem *item) const
Definition: clistdata.cxx:423
TNxArray::size
const int size(void) const
Definition: tnxarray.hxx:261
cnxstring.hxx
NXWidgets::CListData
Definition: clistdata.hxx:107
NXWidgets::CListData::addItem
virtual void addItem(const CNxString &text, const uint32_t value)
Definition: clistdata.cxx:119
ilistdataeventhandler.hxx