UIListView.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include <TurboBadger/tb_menu_window.h>
  2. #include <TurboBadger/tb_select.h>
  3. #include "UIListView.h"
  4. using namespace tb;
  5. namespace Atomic
  6. {
  7. class ListViewItemSource;
  8. class ListViewItemWidget;
  9. class ListViewItem : public TBGenericStringItem
  10. {
  11. bool expanded_;
  12. public:
  13. ListViewItem(const char *str, const TBID &id, const char* icon, ListViewItemSource* source)
  14. : TBGenericStringItem(str, id), source_(source), parent_(0),
  15. depth_(0), widget_(0), expanded_(false), icon_(icon)
  16. {
  17. }
  18. ListViewItem* AddChild(const char* text, const char* icon, const TBID &id);
  19. bool GetExpanded() { return expanded_; }
  20. void SetExpanded(bool expanded)
  21. {
  22. expanded_ = expanded;
  23. if (!expanded_)
  24. {
  25. for (unsigned i = 0; i < children_.Size(); i ++)
  26. children_[i]->SetExpanded(expanded);
  27. }
  28. else
  29. {
  30. ListViewItem* p = parent_;
  31. while (p)
  32. {
  33. p->expanded_ = true;
  34. p = p->parent_;
  35. }
  36. }
  37. }
  38. ListViewItemSource* source_;
  39. ListViewItem* parent_;
  40. int depth_;
  41. PODVector<ListViewItem*> children_;
  42. ListViewItemWidget* widget_;
  43. String icon_;
  44. };
  45. class ListViewItemWidget : public TBLayout
  46. {
  47. public:
  48. ListViewItemWidget(ListViewItem *item, ListViewItemSource *source, TBSelectItemViewer *sourceviewer, int index);
  49. virtual bool OnEvent(const TBWidgetEvent &ev);
  50. private:
  51. ListViewItemSource *source_;
  52. TBSelectItemViewer *sourceviewer_;
  53. int index_;
  54. ListViewItem* item_;
  55. };
  56. class ListViewItemSource : public TBSelectItemSourceList<ListViewItem>
  57. {
  58. public:
  59. TBSelectList* list_;
  60. ListViewItemSource(TBSelectList* list) : list_(list) {}
  61. virtual ~ListViewItemSource() {}
  62. virtual bool Filter(int index, const char *filter);
  63. virtual TBWidget *CreateItemWidget(int index, TBSelectItemViewer *viewer);
  64. };
  65. // implementation
  66. ListViewItem* ListViewItem::AddChild(const char *text, const char* icon, const TBID &id)
  67. {
  68. ListViewItem* child = new ListViewItem(text, id, icon, source_);
  69. child->parent_ = this;
  70. child->depth_ = depth_ + 1;
  71. source_->AddItem(child);
  72. children_.Push(child);
  73. return child;
  74. }
  75. ListViewItemWidget::ListViewItemWidget(ListViewItem *item, ListViewItemSource *source,
  76. TBSelectItemViewer *sourceviewer, int index)
  77. : source_(source)
  78. , sourceviewer_(sourceviewer)
  79. , index_(index)
  80. , item_(item)
  81. {
  82. SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  83. SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP);
  84. SetPaintOverflowFadeout(false);
  85. item_->widget_ = this;
  86. for (int i = 0; i < item->depth_; i++)
  87. {
  88. LayoutParams lp;
  89. lp.SetWidth(6);
  90. lp.SetHeight(4);
  91. TBWidget* spacer = new TBWidget();
  92. spacer->SetLayoutParams(lp);
  93. GetContentRoot()->AddChild(spacer);
  94. }
  95. if (item->icon_.Length())
  96. {
  97. TBSkinImage* skinImage = new TBSkinImage(TBIDC(item->icon_.CString()));
  98. skinImage->SetIgnoreInput(true);
  99. GetContentRoot()->AddChild(skinImage);
  100. }
  101. TBFontDescription fd;
  102. fd.SetID(TBIDC("Vera"));
  103. fd.SetSize(11);
  104. TBTextField* tfield = new TBTextField();
  105. tfield->SetIgnoreInput(true);
  106. tfield->SetSkinBg(TBIDC("Folder"));
  107. tfield->SetText(item->str);
  108. tfield->SetFontDescription(fd);
  109. SetSkinBg(TBIDC("TBSelectItem"));
  110. GetContentRoot()->AddChild(tfield);
  111. SetID(item->id);
  112. }
  113. bool ListViewItemWidget::OnEvent(const TBWidgetEvent &ev)
  114. {
  115. if (ev.type == EVENT_TYPE_WHEEL)
  116. {
  117. return false;
  118. }
  119. // get clicks this way, not sure we want to
  120. if (ev.type == EVENT_TYPE_CLICK && ev.target->GetID() == item_->id)
  121. {
  122. item_->SetExpanded(!item_->GetExpanded());
  123. source_->list_->InvalidateList();
  124. // want to bubble
  125. return false;
  126. }
  127. return TBLayout::OnEvent(ev);
  128. }
  129. bool ListViewItemSource::Filter(int index, const char *filter)
  130. {
  131. ListViewItem* item = GetItem(index);
  132. if (!item->parent_)
  133. return true;
  134. if (item->parent_->GetExpanded())
  135. return true;
  136. return false;
  137. }
  138. TBWidget *ListViewItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  139. {
  140. ListViewItem* item = GetItem(index);
  141. if (TBLayout *layout = new ListViewItemWidget(item, this, viewer, index))
  142. return layout;
  143. return nullptr;
  144. }
  145. UIListView::UIListView(Context* context, bool createWidget) :
  146. UIWidget(context, createWidget),
  147. source_(0), itemLookupId_(0)
  148. {
  149. rootList_ = new UISelectList(context);
  150. // dummy filter so filter is called
  151. rootList_->SetFilter(" ");
  152. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  153. rootList_->SetGravity(UI_GRAVITY_ALL);
  154. source_ = new ListViewItemSource(rootList_->GetTBSelectList());
  155. rootList_->GetTBSelectList()->SetSource(source_);
  156. AddChild(rootList_);
  157. }
  158. UIListView::~UIListView()
  159. {
  160. }
  161. unsigned UIListView::AddRootItem(const String& text, const String& icon, const String& id)
  162. {
  163. ListViewItem* item = new ListViewItem(text.CString(), TBID(id.CString()), icon.CString(), source_);
  164. source_->AddItem(item);
  165. itemLookup_[itemLookupId_++] = item;
  166. return itemLookupId_ - 1;
  167. }
  168. void UIListView::DeleteItemByID(const String& id)
  169. {
  170. TBID tbid(id.CString());
  171. for (int i = 0; i < source_->GetNumItems(); i++)
  172. {
  173. if (source_->GetItemID(i) == tbid)
  174. {
  175. source_->DeleteItem(i);
  176. return;
  177. }
  178. }
  179. }
  180. unsigned UIListView::AddChildItem(unsigned parentItemID, const String& text, const String& icon, const String& id)
  181. {
  182. if (!itemLookup_.Contains(parentItemID))
  183. return -1;
  184. ListViewItem* item = itemLookup_[parentItemID];
  185. ListViewItem* child = item->AddChild(text.CString(), icon.CString(), TBID(id.CString()));
  186. itemLookup_[itemLookupId_++] = child;
  187. return itemLookupId_ - 1;
  188. }
  189. void UIListView::SetExpanded(unsigned itemID, bool value)
  190. {
  191. if (!itemLookup_.Contains(itemID))
  192. return;
  193. itemLookup_[itemID]->SetExpanded(value);
  194. }
  195. void UIListView::DeleteAllItems()
  196. {
  197. itemLookup_.Clear();
  198. source_->DeleteAllItems();
  199. }
  200. void UIListView::SelectItemByID(const String& id)
  201. {
  202. TBID tid = TBIDC(id.CString());
  203. for (int i = 0; i < source_->GetNumItems(); i++)
  204. {
  205. ListViewItem* item = source_->GetItem(i);
  206. if (tid == item->id)
  207. {
  208. item->SetExpanded(true);
  209. rootList_->SetValue(i);
  210. rootList_->InvalidateList();
  211. return;
  212. }
  213. }
  214. }
  215. }