clistdata.hxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /****************************************************************************
  2. * apps/include/graphics/nxwidgets/clistdata.hxx
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. *
  22. * Portions of this package derive from Woopsi (http://woopsi.org/) and
  23. * portions are original efforts. It is difficult to determine at this
  24. * point what parts are original efforts and which parts derive from Woopsi.
  25. * However, in any event, the work of Antony Dzeryn will be acknowledged
  26. * in most NxWidget files. Thanks Antony!
  27. *
  28. * Copyright (c) 2007-2011, Antony Dzeryn
  29. * All rights reserved.
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions are met:
  33. *
  34. * * Redistributions of source code must retain the above copyright
  35. * notice, this list of conditions and the following disclaimer.
  36. * * Redistributions in binary form must reproduce the above copyright
  37. * notice, this list of conditions and the following disclaimer in the
  38. * documentation and/or other materials provided with the distribution.
  39. * * Neither the names "Woopsi", "Simian Zombie" nor the
  40. * names of its contributors may be used to endorse or promote products
  41. * derived from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
  44. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  45. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  46. * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
  47. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  48. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  49. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  50. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  51. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  52. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53. *
  54. ****************************************************************************/
  55. #ifndef __APPS_INCLUDE_GRAPHICS_NXWIDGETS_CLISTDATA_HXX
  56. #define __APPS_INCLUDE_GRAPHICS_NXWIDGETS_CLISTDATA_HXX
  57. /****************************************************************************
  58. * Included Files
  59. ****************************************************************************/
  60. #include <nuttx/config.h>
  61. #include <cstdint>
  62. #include <cstdbool>
  63. #include <nuttx/nx/nxglib.h>
  64. #include "graphics/nxwidgets/tnxarray.hxx"
  65. #include "graphics/nxwidgets/ilistdataeventhandler.hxx"
  66. #include "graphics/nxwidgets/clistdataitem.hxx"
  67. #include "graphics/nxwidgets/cnxstring.hxx"
  68. /****************************************************************************
  69. * Pre-Processor Definitions
  70. ****************************************************************************/
  71. /****************************************************************************
  72. * Implementation Classes
  73. ****************************************************************************/
  74. #if defined(__cplusplus)
  75. namespace NXWidgets
  76. {
  77. /**
  78. * Class representing a list of items. Designed to be used by the
  79. * CListBox class, etc, to store its data. Fires events to notify
  80. * listeners when the list changes or a new selection is made.
  81. */
  82. class CListData
  83. {
  84. protected:
  85. TNxArray<CListDataItem*> m_items; /**< Collection of list data items. */
  86. TNxArray<IListDataEventHandler*> m_listDataEventhandlers; /**< Collection of event handlers. */
  87. bool m_allowMultipleSelections; /**< If true, multiple options can
  88. be selected. */
  89. bool m_sortInsertedItems; /**< Automatically sorts items on
  90. insertion if true. */
  91. /**
  92. * Quick sort the items using their compareTo() methods.
  93. *
  94. * @param start The index to start sorting at.
  95. * @param end The index to stop sorting at.
  96. */
  97. virtual void quickSort(const int start, const int end);
  98. /**
  99. * Swap the locations of two items in the array.
  100. *
  101. * @param index1 The index of the first item to swap.
  102. * @param index2 The index of the second item to swap.
  103. */
  104. virtual void swapItems(const int index1, const int index2);
  105. /**
  106. * Return the index that an item should be inserted at to maintain a sorted list of data.
  107. *
  108. * @param item The item to insert.
  109. * @return The index that the item should be imserted into at.
  110. */
  111. const int getSortedInsertionIndex(const CListDataItem *item) const;
  112. /**
  113. * Raise a data changed event.
  114. */
  115. void raiseDataChangedEvent(void);
  116. /**
  117. * Raise a selection changed event.
  118. */
  119. void raiseSelectionChangedEvent(void);
  120. public:
  121. /**
  122. * Constructor.
  123. */
  124. CListData(void);
  125. /**
  126. * Destructor.
  127. */
  128. virtual ~CListData(void);
  129. /**
  130. * Add a new item.
  131. *
  132. * @param text Text to show in the option.
  133. * @param value The value of the option.
  134. */
  135. virtual void addItem(const CNxString &text, const uint32_t value);
  136. /**
  137. * Add an existing item. CListData becomes the owner of the option and will delete it
  138. * when the list is deleted.
  139. *
  140. * @param item The item to add.
  141. */
  142. virtual void addItem(CListDataItem *item);
  143. /**
  144. * Remove an item by its index.
  145. *
  146. * @param index The index of the option to remove.
  147. */
  148. virtual void removeItem(const int index);
  149. /**
  150. * Select an item by its index.
  151. *
  152. * @param index The index of the item to select.
  153. */
  154. virtual void selectItem(const int index);
  155. /**
  156. * Deselect an item by its index.
  157. *
  158. * @param index The index of the item to select.
  159. */
  160. virtual void deselectItem(const int index);
  161. /**
  162. * Remove all items.
  163. */
  164. virtual void removeAllItems(void);
  165. /**
  166. * Get the selected index. Returns -1 if nothing is selected. If more than one
  167. * item is selected, the index of the first selected item is returned.
  168. *
  169. * @return The selected index.
  170. */
  171. virtual const int getSelectedIndex(void) const;
  172. /**
  173. * Sets the selected index. Specify -1 to select nothing. Resets any
  174. * other selected items to deselected.
  175. *
  176. * @param index The selected index.
  177. */
  178. virtual void setSelectedIndex(const int index);
  179. /**
  180. * Get the selected item. Returns NULL if nothing is selected.
  181. *
  182. * @return The selected option.
  183. */
  184. virtual const CListDataItem *getSelectedItem(void) const;
  185. /**
  186. * Sets whether multiple selections are possible or not.
  187. *
  188. * @param allowMultipleSelections True to allow multiple selections.
  189. */
  190. virtual inline void
  191. setAllowMultipleSelections(const bool allowMultipleSelections)
  192. {
  193. m_allowMultipleSelections = allowMultipleSelections;
  194. }
  195. /**
  196. * Get the specified item.
  197. *
  198. * @return The specified item.
  199. */
  200. virtual inline const CListDataItem *getItem(const int index) const
  201. {
  202. if (index < 0 || index >= m_items.size())
  203. {
  204. return (const CListDataItem *)0;
  205. }
  206. else
  207. {
  208. return m_items[index];
  209. }
  210. }
  211. /**
  212. * Sort the items using their compareTo() methods.
  213. */
  214. virtual void sort(void);
  215. /**
  216. * Get the total number of items.
  217. *
  218. * @return The number of items.
  219. */
  220. virtual inline const int getItemCount(void) const
  221. {
  222. return m_items.size();
  223. }
  224. /**
  225. * Select all items. Does nothing if the list does not allow
  226. * multiple selections.
  227. */
  228. virtual void selectAllItems(void);
  229. /**
  230. * Deselect all items.
  231. */
  232. virtual void deselectAllItems(void);
  233. /**
  234. * Select or deselect an item by its index. Does not deselect any
  235. * other selected items. Set index to -1 to select nothing.
  236. *
  237. * @param index The index of the item to select.
  238. * @param selected True to select the item, false to deselect it.
  239. */
  240. virtual void setItemSelected(const int index, const bool selected);
  241. /**
  242. * Returns whether multiple selections are possible or not.
  243. *
  244. * @return True if multiple selections are allowed.
  245. */
  246. virtual inline const bool allowsMultipleSelections(void) const
  247. {
  248. return m_allowMultipleSelections;
  249. }
  250. /**
  251. * Sets whether or not items added to the list are automatically
  252. * sorted on insert or not.
  253. *
  254. * @param sortInsertedItems True to enable sort on insertion.
  255. */
  256. virtual inline void setSortInsertedItems(const bool sortInsertedItems)
  257. {
  258. m_sortInsertedItems = sortInsertedItems;
  259. }
  260. /**
  261. * Add an event handler.
  262. *
  263. * @param eventHandler The event handler to add.
  264. */
  265. inline void addListDataEventHandler(IListDataEventHandler *eventHandler)
  266. {
  267. m_listDataEventhandlers.push_back(eventHandler);
  268. }
  269. /**
  270. * Remove an event handler.
  271. *
  272. * @param eventHandler The event handler to remove.
  273. */
  274. void removeListDataEventHandler(IListDataEventHandler *eventHandler);
  275. };
  276. }
  277. #endif // __cplusplus
  278. #endif // __APPS_INCLUDE_GRAPHICS_NXWIDGETS_CLISTDATA_HXX