tb_select_item.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_SELECT_ITEM_H
  6. #define TB_SELECT_ITEM_H
  7. #include "tb_linklist.h"
  8. #include "tb_list.h"
  9. #include "tb_value.h"
  10. namespace tb {
  11. class TBSelectItemSource;
  12. enum TB_SORT {
  13. TB_SORT_NONE, ///< No sorting. Items appear in list order.
  14. TB_SORT_ASCENDING, ///< Ascending sort.
  15. TB_SORT_DESCENDING ///< Descending sort.
  16. };
  17. /** TBSelectItemViewer is the viewer for items provided by TBSelectItemSource.
  18. There can be multiple viewers for each source. The viewer will recieve
  19. callbacks when the source is changed, so it can update itself.
  20. */
  21. class TBSelectItemViewer : public TBLinkOf<TBSelectItemViewer>
  22. {
  23. public:
  24. TBSelectItemViewer() : m_source(nullptr) {}
  25. virtual ~TBSelectItemViewer() {}
  26. /** Set the source which should provide the items for this viewer.
  27. This source needs to live longer than this viewer.
  28. Set nullptr to unset currently set source. */
  29. void SetSource(TBSelectItemSource *source);
  30. TBSelectItemSource *GetSource() const { return m_source; }
  31. /** Called when the source has changed or been unset by calling SetSource. */
  32. virtual void OnSourceChanged() = 0;
  33. /** Called when the item at the given index has changed in a way that should
  34. update the viewer. */
  35. virtual void OnItemChanged(int index) = 0;
  36. /** Called when the item at the given index has been added. */
  37. virtual void OnItemAdded(int index) = 0;
  38. /** Called when the item at the given index has been removed. */
  39. virtual void OnItemRemoved(int index) = 0;
  40. /** Called when all items have been removed. */
  41. virtual void OnAllItemsRemoved() = 0;
  42. protected:
  43. TBSelectItemSource *m_source;
  44. };
  45. /** TBSelectItemSource is a item provider interface for list widgets (TBSelectList and
  46. TBSelectDropdown).
  47. Instead of feeding all list widgets with all items all the time, the list widgets
  48. will ask TBSelectItemSource when it needs it. The list widgets may also apply
  49. filtering so only a subset of all the items are shown.
  50. CreateItemWidget can be overridden to create any set of widget content for each item.
  51. This class has no storage of items. If you want an array storage of items,
  52. use the subclass TBSelectItemSourceList. If you implement your own storage,
  53. remember to call InvokeItem[Added/...] to notify viewers that they need to update.
  54. */
  55. class TBSelectItemSource
  56. {
  57. public:
  58. TBSelectItemSource() : m_sort(TB_SORT_NONE) {}
  59. virtual ~TBSelectItemSource();
  60. /** Return true if a item matches the given filter text.
  61. By default, it returns true if GetItemString contains filter. */
  62. virtual bool Filter(int index, const char *filter);
  63. /** Get the string of a item. If a item has more than one string,
  64. return the one that should be used for inline-find (pressing keys
  65. in the list will scroll to the item starting with the same letters),
  66. and for sorting the list. */
  67. virtual const char *GetItemString(int index) = 0;
  68. /** Get the source to be used if this item should open a sub menu. */
  69. virtual TBSelectItemSource *GetItemSubSource(int index) { return nullptr; }
  70. /** Get the skin image to be painted before the text for this item. */
  71. virtual TBID GetItemImage(int index) { return TBID(); }
  72. /** Get the if of the item. */
  73. virtual TBID GetItemID(int index) { return TBID(); }
  74. /** Create the item representation widget(s). By default, it will create
  75. a TBTextField for string-only items, and other types for items that
  76. also has image or submenu. */
  77. virtual TBWidget *CreateItemWidget(int index, TBSelectItemViewer *viewer);
  78. /** Get the number of items */
  79. virtual int GetNumItems() = 0;
  80. /** Set sort type. Default is TB_SORT_NONE. */
  81. void SetSort(TB_SORT sort) { m_sort = sort; }
  82. TB_SORT GetSort() const { return m_sort; }
  83. /** Invoke OnItemChanged on all open viewers for this source. */
  84. void InvokeItemChanged(int index, TBSelectItemViewer *exclude_viewer = nullptr);
  85. void InvokeItemAdded(int index);
  86. void InvokeItemRemoved(int index);
  87. void InvokeAllItemsRemoved();
  88. private:
  89. friend class TBSelectItemViewer;
  90. TBLinkListOf<TBSelectItemViewer> m_viewers;
  91. TB_SORT m_sort;
  92. };
  93. /** TBSelectItemSourceList is a item provider for list widgets (TBSelectList and
  94. TBSelectDropdown). It stores items of the type specified by the template in an array. */
  95. template<class T>
  96. class TBSelectItemSourceList : public TBSelectItemSource
  97. {
  98. public:
  99. TBSelectItemSourceList() {}
  100. virtual ~TBSelectItemSourceList() { DeleteAllItems(); }
  101. virtual const char *GetItemString(int index) { return GetItem(index)->str; }
  102. virtual TBSelectItemSource *GetItemSubSource(int index){ return GetItem(index)->sub_source; }
  103. virtual TBID GetItemImage(int index) { return GetItem(index)->skin_image; }
  104. virtual TBID GetItemID(int index) { return GetItem(index)->id; }
  105. virtual int GetNumItems() { return m_items.GetNumItems(); }
  106. virtual TBWidget *CreateItemWidget(int index, TBSelectItemViewer *viewer)
  107. {
  108. if (TBWidget *widget = TBSelectItemSource::CreateItemWidget(index, viewer))
  109. {
  110. T *item = m_items[index];
  111. widget->SetID(item->id);
  112. return widget;
  113. }
  114. return nullptr;
  115. }
  116. /** Add a new item at the given index. */
  117. bool AddItem(T *item, int index)
  118. {
  119. if (m_items.Add(item, index))
  120. {
  121. InvokeItemAdded(index);
  122. return true;
  123. }
  124. return false;
  125. }
  126. /** Add a new item last. */
  127. bool AddItem(T *item) { return AddItem(item, m_items.GetNumItems()); }
  128. /** Get the item at the given index. */
  129. T *GetItem(int index) { return m_items[index]; }
  130. /** Delete the item at the given index. */
  131. void DeleteItem(int index)
  132. {
  133. if (!m_items.GetNumItems())
  134. return;
  135. m_items.Delete(index);
  136. InvokeItemRemoved(index);
  137. }
  138. /** Delete all items. */
  139. void DeleteAllItems()
  140. {
  141. if (!m_items.GetNumItems())
  142. return;
  143. m_items.DeleteAll();
  144. InvokeAllItemsRemoved();
  145. }
  146. private:
  147. TBListOf<T> m_items;
  148. };
  149. /** TBGenericStringItem item for TBGenericStringItemSource.
  150. It has a string and may have a skin image and sub item source. */
  151. class TBGenericStringItem
  152. {
  153. public:
  154. TBGenericStringItem(const TBGenericStringItem& other) : str(other.str), id(other.id), sub_source(other.sub_source), tag(other.tag) {}
  155. TBGenericStringItem(const char *str) : str(str), sub_source(nullptr) {}
  156. TBGenericStringItem(const char *str, TBID id) : str(str), id(id), sub_source(nullptr) {}
  157. TBGenericStringItem(const char *str, TBSelectItemSource *sub_source) : str(str), sub_source(sub_source) {}
  158. const TBGenericStringItem& operator = (const TBGenericStringItem &other) { str.Set(other.str); id = other.id; sub_source = other.sub_source; tag = other.tag; return *this; }
  159. void SetSkinImage(const TBID &image) { skin_image = image; }
  160. public:
  161. TBStr str;
  162. TBID id;
  163. TBID skin_image;
  164. TBSelectItemSource *sub_source;
  165. /** This value is free to use for anything. It's not used internally. */
  166. TBValue tag;
  167. };
  168. /** TBGenericStringItemSource is a item source list providing items of type TBGenericStringItem. */
  169. class TBGenericStringItemSource : public TBSelectItemSourceList<TBGenericStringItem> { };
  170. }; // namespace tb
  171. #endif // TB_SELECT_ITEM_H