Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_select_item.h
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 
6 #ifndef TB_SELECT_ITEM_H
7 #define TB_SELECT_ITEM_H
8 
9 #include "tb_linklist.h"
10 #include "tb_list.h"
11 #include "tb_value.h"
12 
13 namespace tb {
14 
15 class TBSelectItemSource;
16 
17 enum TB_SORT {
21 };
22 
27 class TBSelectItemViewer : public TBLinkOf<TBSelectItemViewer>
28 {
29 public:
30  TBSelectItemViewer() : m_source(nullptr) {}
31  virtual ~TBSelectItemViewer() {}
32 
36  void SetSource(TBSelectItemSource *source);
37  TBSelectItemSource *GetSource() const { return m_source; }
38 
40  virtual void OnSourceChanged() = 0;
41 
44  virtual void OnItemChanged(int index) = 0;
45 
47  virtual void OnItemAdded(int index) = 0;
48 
50  virtual void OnItemRemoved(int index) = 0;
51 
53  virtual void OnAllItemsRemoved() = 0;
54 protected:
55  TBSelectItemSource *m_source;
56 };
57 
73 {
74 public:
75  TBSelectItemSource() : m_sort(TB_SORT_NONE) {}
76  virtual ~TBSelectItemSource();
77  TBOBJECT_SUBCLASS(TBSelectItemSource, TBTypedObject);
78 
81  virtual bool Filter(int index, const TBStr & filter);
82 
87  virtual const TBStr & GetItemString(int index) const = 0;
88 
90  virtual TBSelectItemSource *GetItemSubSource(int /*index*/) { return nullptr; }
91 
93  virtual TBID GetItemImage(int /*index*/) const { return TBID(); }
94 
96  virtual TBID GetItemID(int /*index*/) const { return TBID(); }
97 
101  virtual TBWidget *CreateItemWidget(int index, TBSelectItemViewer *viewer);
102 
104  virtual int GetNumItems() const = 0;
105 
107  virtual int FindIDIndex(TBID id) const;
108 
110  void SetSort(TB_SORT sort) { m_sort = sort; }
111  TB_SORT GetSort() const { return m_sort; }
112 
114  void InvokeItemChanged(int index, TBSelectItemViewer *exclude_viewer = nullptr);
115  void InvokeItemAdded(int index);
116  void InvokeItemRemoved(int index);
117  void InvokeAllItemsRemoved();
118 private:
119  friend class TBSelectItemViewer;
120  TBLinkListOf<TBSelectItemViewer> m_viewers;
121  TB_SORT m_sort;
122 };
123 
127 template<class T>
129 {
130 public:
131  TBOBJECT_SUBCLASS(TBSelectItemSourceList, TBSelectItemSource);
133  virtual ~TBSelectItemSourceList() { DeleteAllItems(); }
134  virtual const TBStr & GetItemString(int index) const { return GetItem(index)->str; }
135  virtual TBSelectItemSource *GetItemSubSource(int index){ return GetItem(index)->sub_source; }
136  virtual TBID GetItemImage(int index) const { return GetItem(index)->skin_image; }
137  virtual TBID GetItemID(int index) const { return GetItem(index)->id; }
138  virtual int GetNumItems() const { return m_items.GetNumItems(); }
139  virtual TBWidget *CreateItemWidget(int index, TBSelectItemViewer *viewer)
140  {
141  if (TBWidget *widget = TBSelectItemSource::CreateItemWidget(index, viewer))
142  {
143  T *item = m_items[index];
144  widget->SetID(item->id);
145  widget->SetStateRaw(item->state);
146  return widget;
147  }
148  return nullptr;
149  }
150 
152  bool AddItem(T *item, int index)
153  {
154  if (m_items.Add(item, index))
155  {
156  InvokeItemAdded(index);
157  return true;
158  }
159  return false;
160  }
161 
163  bool AddItem(T *item) { return AddItem(item, m_items.GetNumItems()); }
164 
166  T *GetItem(int index) { return m_items[index]; }
167 
169  const T *GetItem(int index) const { return m_items[index]; }
170 
172  T *FindItemByID(TBID id) { int index = FindIDIndex(id); return index >= 0 ? m_items[index] : nullptr; }
173 
175  const T *FindItemByID(TBID id) const { int index = FindIDIndex(id); return index >= 0 ? m_items[index] : nullptr; }
176 
178  void DeleteItem(int index)
179  {
180  if (!m_items.GetNumItems())
181  return;
182  m_items.Delete(index);
183  InvokeItemRemoved(index);
184  }
185 
188  {
189  if (!m_items.GetNumItems())
190  return;
191  m_items.DeleteAll();
192  InvokeAllItemsRemoved();
193  }
194 private:
195  TBListOf<T> m_items;
196 };
197 
201 {
202 public:
203 #define def_skin TBIDC("NormalItem")
204  TBGenericStringItem(TBStr str) : str(str), id(str) {}
205  TBGenericStringItem(TBStr str, TBID id, TBValue val = TBValue(), TBID img_id = def_skin)
206  : str(str), id(id), skin_image(img_id), tag(val) {}
207  TBGenericStringItem(TBStr str, TBSelectItemSource *sub_source, TBID img_id = def_skin)
208  : str(str), id(str), skin_image(img_id), sub_source(sub_source) {}
209 
210  // protect the sub_source pointer from copy, since we own it.
211  const TBGenericStringItem& operator = (const TBGenericStringItem &other) = delete;
212  TBGenericStringItem(const TBGenericStringItem& other) = delete;
213 
214  ~TBGenericStringItem() { if (sub_source) delete sub_source; }
215  void SetSkinImage(const TBID &image) { skin_image = image; }
216 
217  public:
218  TBStr str;
219  TBID id;
220  TBID skin_image = def_skin;
221  TBSelectItemSource *sub_source = nullptr;
222  WIDGET_STATE state = WIDGET_STATE_NONE;
223 
226 };
227 
230 public:
231  TBGenericSeparatorItem() : TBGenericStringItem("-", nullptr, TBID()) {}
232 };
233 
236 class TBGenericStringItemSource : public TBSelectItemSourceList<TBGenericStringItem> {
237 public:
239 };
240 
241 } // namespace tb
242 
243 #endif // TB_SELECT_ITEM_H
virtual const TBStr & GetItemString(int index) const
Get the string of a item.
Definition: tb_select_item.h:134
virtual TBSelectItemSource * GetItemSubSource(int)
Get the source to be used if this item should open a sub menu.
Definition: tb_select_item.h:90
virtual int GetNumItems() const =0
Get the number of items.
bool AddItem(T *item)
Add a new item last.
Definition: tb_select_item.h:163
The base TBWidget class.
Definition: tb_widgets.h:418
TB_SORT
Definition: tb_select_item.h:17
bool AddItem(T *item, int index)
Add a new item at the given index.
Definition: tb_select_item.h:152
T * FindItemByID(TBID id)
Get the item at the given index.
Definition: tb_select_item.h:172
virtual TBID GetItemID(int index) const
Get the id of the item.
Definition: tb_select_item.h:137
const T * GetItem(int index) const
Get the item at the given index.
Definition: tb_select_item.h:169
TBGenericStringItem item for TBGenericStringItemSource.
Definition: tb_select_item.h:200
Descending sort.
Definition: tb_select_item.h:20
void DeleteItem(int index)
Delete the item at the given index.
Definition: tb_select_item.h:178
TBGenericStringItemSource is a item source list providing items of type TBGenericStringItem.
Definition: tb_select_item.h:236
virtual TBSelectItemSource * GetItemSubSource(int index)
Get the source to be used if this item should open a sub menu.
Definition: tb_select_item.h:135
void SetSource(TBSelectItemSource *source)
Set the source which should provide the items for this viewer.
Definition: tb_select_item.cpp:133
virtual TBWidget * CreateItemWidget(int index, TBSelectItemViewer *viewer)
Create the item representation widget(s).
Definition: tb_select_item.h:139
T * GetItem(int index)
Get the item at the given index.
Definition: tb_select_item.h:166
TBValue tag
This value is free to use for anything.
Definition: tb_select_item.h:225
Item Separator.
Definition: tb_select_item.h:229
virtual void OnItemAdded(int index)=0
Called when the item at the given index has been added.
TBID is a wrapper for a uint32_t to be used as ID.
Definition: tb_id.h:18
const T * FindItemByID(TBID id) const
Get the item at the given index.
Definition: tb_select_item.h:175
virtual int GetNumItems() const
Get the number of items.
Definition: tb_select_item.h:138
virtual TBWidget * CreateItemWidget(int index, TBSelectItemViewer *viewer)
Create the item representation widget(s).
Definition: tb_select_item.cpp:164
void InvokeItemChanged(int index, TBSelectItemViewer *exclude_viewer=nullptr)
Invoke OnItemChanged on all open viewers for this source.
Definition: tb_select_item.cpp:201
virtual void OnAllItemsRemoved()=0
Called when all items have been removed.
virtual TBID GetItemImage(int) const
Get the skin image to be painted before the text for this item.
Definition: tb_select_item.h:93
TBSelectItemViewer is the viewer for items provided by TBSelectItemSource.
Definition: tb_select_item.h:27
TBSelectItemSource is a item provider interface for list widgets (TBSelectList and TBSelectDropdown)...
Definition: tb_select_item.h:72
virtual void OnItemRemoved(int index)=0
Called when the item at the given index has been removed.
Definition: tb_object.h:21
TBListOf is a list (array) of pointers to the specified object type.
Definition: tb_list.h:47
void DeleteAllItems()
Delete all items.
Definition: tb_select_item.h:187
Ascending sort.
Definition: tb_select_item.h:19
TBStr is a simple string class.
Definition: tb_str.h:62
TBValue holds value of a specific type.
Definition: tb_value.h:59
virtual void OnSourceChanged()=0
Called when the source has changed or been unset by calling SetSource.
void SetSort(TB_SORT sort)
Set sort type.
Definition: tb_select_item.h:110
virtual int FindIDIndex(TBID id) const
Get the index of the first item having this id or -1 if not found.
Definition: tb_select_item.cpp:193
No sorting. Items appear in list order.
Definition: tb_select_item.h:18
virtual const TBStr & GetItemString(int index) const =0
Get the string of a item.
WIDGET_STATE
TBWidget state types (may be combined).
Definition: tb_widgets.h:219
virtual void OnItemChanged(int index)=0
Called when the item at the given index has changed in a way that should update the viewer...
TBSelectItemSourceList is a item provider for list widgets (TBSelectList and TBSelectDropdown).
Definition: tb_select_item.h:128
virtual TBID GetItemID(int) const
Get the id of the item.
Definition: tb_select_item.h:96
virtual bool Filter(int index, const TBStr &filter)
Return true if an item matches the given filter text.
Definition: tb_select_item.cpp:156
virtual TBID GetItemImage(int index) const
Get the skin image to be painted before the text for this item.
Definition: tb_select_item.h:136