UIListView.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. void UpdateText(const String& text);
  39. ListViewItemSource* source_;
  40. ListViewItem* parent_;
  41. int depth_;
  42. PODVector<ListViewItem*> children_;
  43. ListViewItemWidget* widget_;
  44. String icon_;
  45. };
  46. class ListViewItemWidget : public TBLayout
  47. {
  48. public:
  49. ListViewItemWidget(ListViewItem *item, ListViewItemSource *source, TBSelectItemViewer *sourceviewer, int index);
  50. virtual bool OnEvent(const TBWidgetEvent &ev);
  51. void UpdateText(const String& text)
  52. {
  53. if (textField_)
  54. textField_->SetText(text.CString());
  55. }
  56. private:
  57. TBTextField* textField_;
  58. ListViewItemSource *source_;
  59. TBSelectItemViewer *sourceviewer_;
  60. int index_;
  61. ListViewItem* item_;
  62. };
  63. class ListViewItemSource : public TBSelectItemSourceList<ListViewItem>
  64. {
  65. public:
  66. TBSelectList* list_;
  67. ListViewItemSource(TBSelectList* list) : list_(list) {}
  68. virtual ~ListViewItemSource() {}
  69. virtual bool Filter(int index, const char *filter);
  70. virtual TBWidget *CreateItemWidget(int index, TBSelectItemViewer *viewer);
  71. };
  72. // implementation
  73. void ListViewItem::UpdateText(const String& text)
  74. {
  75. str = text.CString();
  76. if (widget_)
  77. widget_->UpdateText(text);
  78. }
  79. ListViewItem* ListViewItem::AddChild(const char *text, const char* icon, const TBID &id)
  80. {
  81. ListViewItem* child = new ListViewItem(text, id, icon, source_);
  82. child->parent_ = this;
  83. child->depth_ = depth_ + 1;
  84. // filter, alphabetically into source
  85. ListViewItem* insert = this;
  86. int parentIndex = -1;
  87. for (int i = 0; i < source_->GetNumItems(); i++)
  88. {
  89. if (source_->GetItem(i) == this)
  90. {
  91. parentIndex = i;
  92. break;
  93. }
  94. }
  95. for (int i = parentIndex + 1; i < source_->GetNumItems(); i++)
  96. {
  97. ListViewItem* item = source_->GetItem(i);
  98. if (item->depth_ <= depth_)
  99. break;
  100. if (item->depth_ != depth_ + 1)
  101. {
  102. insert = item;
  103. continue;
  104. }
  105. if (strcmp(item->str.CStr(), text) >= 0)
  106. break;
  107. insert = item;
  108. }
  109. for (int i = 0; i < source_->GetNumItems(); i++)
  110. {
  111. if (source_->GetItem(i) == insert)
  112. {
  113. source_->AddItem(child, i + 1);
  114. break;
  115. }
  116. }
  117. children_.Push(child);
  118. return child;
  119. }
  120. ListViewItemWidget::ListViewItemWidget(ListViewItem *item, ListViewItemSource *source,
  121. TBSelectItemViewer *sourceviewer, int index)
  122. : source_(source)
  123. , sourceviewer_(sourceviewer)
  124. , index_(index)
  125. , item_(item)
  126. , textField_(0)
  127. {
  128. SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  129. SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP);
  130. SetPaintOverflowFadeout(false);
  131. item_->widget_ = this;
  132. for (int i = 0; i < item->depth_; i++)
  133. {
  134. LayoutParams lp;
  135. lp.SetWidth(6);
  136. lp.SetHeight(4);
  137. TBWidget* spacer = new TBWidget();
  138. spacer->SetLayoutParams(lp);
  139. GetContentRoot()->AddChild(spacer);
  140. }
  141. if (item->icon_.Length())
  142. {
  143. TBSkinImage* skinImage = new TBSkinImage(TBIDC(item->icon_.CString()));
  144. skinImage->SetIgnoreInput(true);
  145. GetContentRoot()->AddChild(skinImage);
  146. }
  147. TBFontDescription fd;
  148. fd.SetID(TBIDC("Vera"));
  149. fd.SetSize(11);
  150. TBTextField* tfield = textField_ = new TBTextField();
  151. tfield->SetIgnoreInput(true);
  152. tfield->SetSkinBg(TBIDC("Folder"));
  153. tfield->SetText(item->str);
  154. tfield->SetFontDescription(fd);
  155. SetSkinBg(TBIDC("TBSelectItem"));
  156. GetContentRoot()->AddChild(tfield);
  157. SetID(item->id);
  158. }
  159. bool ListViewItemWidget::OnEvent(const TBWidgetEvent &ev)
  160. {
  161. if (ev.type == EVENT_TYPE_WHEEL)
  162. {
  163. return false;
  164. }
  165. // get clicks this way, not sure we want to
  166. if (ev.type == EVENT_TYPE_CLICK && ev.target->GetID() == item_->id)
  167. {
  168. item_->SetExpanded(!item_->GetExpanded());
  169. source_->list_->InvalidateList();
  170. // want to bubble
  171. return false;
  172. }
  173. return TBLayout::OnEvent(ev);
  174. }
  175. bool ListViewItemSource::Filter(int index, const char *filter)
  176. {
  177. ListViewItem* item = GetItem(index);
  178. if (!item->parent_)
  179. return true;
  180. if (item->parent_->GetExpanded())
  181. return true;
  182. return false;
  183. }
  184. TBWidget *ListViewItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  185. {
  186. ListViewItem* item = GetItem(index);
  187. if (TBLayout *layout = new ListViewItemWidget(item, this, viewer, index))
  188. return layout;
  189. return nullptr;
  190. }
  191. /*
  192. static int select_list_sort_cb(TBSelectItemSource *_source, const int *a, const int *b)
  193. {
  194. ListViewItemSource* source = (ListViewItemSource*) _source;
  195. ListViewItem* itemA = source->GetItem(*a);
  196. ListViewItem* itemB = source->GetItem(*b);
  197. int value;
  198. if (itemA->depth_ == itemB->depth_)
  199. {
  200. value = strcmp(source->GetItemString(*a), source->GetItemString(*b));
  201. }
  202. else
  203. {
  204. value = itemA->depth_ > itemB->depth_ ? 1 : -1;
  205. }
  206. return source->GetSort() == TB_SORT_DESCENDING ? -value : value;
  207. }
  208. */
  209. UIListView::UIListView(Context* context, bool createWidget) :
  210. UIWidget(context, createWidget),
  211. source_(0), itemLookupId_(0)
  212. {
  213. rootList_ = new UISelectList(context);
  214. // dummy filter so filter is called
  215. rootList_->SetFilter(" ");
  216. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  217. rootList_->SetGravity(UI_GRAVITY_ALL);
  218. source_ = new ListViewItemSource(rootList_->GetTBSelectList());
  219. rootList_->GetTBSelectList()->SetSource(source_);
  220. AddChild(rootList_);
  221. }
  222. UIListView::~UIListView()
  223. {
  224. }
  225. unsigned UIListView::AddRootItem(const String& text, const String& icon, const String& id)
  226. {
  227. ListViewItem* item = new ListViewItem(text.CString(), TBID(id.CString()), icon.CString(), source_);
  228. source_->AddItem(item);
  229. itemLookup_[itemLookupId_++] = item;
  230. return itemLookupId_ - 1;
  231. }
  232. void UIListView::SetItemText(const String& id, const String& text)
  233. {
  234. TBID tbid(id.CString());
  235. for (int i = 0; i < source_->GetNumItems(); i++)
  236. {
  237. if (source_->GetItemID(i) == tbid)
  238. {
  239. ListViewItem* item = source_->GetItem(i);
  240. item->UpdateText(text);
  241. return;
  242. }
  243. }
  244. }
  245. void UIListView::DeleteItemByID(const String& id)
  246. {
  247. TBID tbid(id.CString());
  248. for (int i = 0; i < source_->GetNumItems(); i++)
  249. {
  250. if (source_->GetItemID(i) == tbid)
  251. {
  252. ListViewItem* item = source_->GetItem(i);
  253. if (item->parent_)
  254. item->parent_->children_.Remove(item);
  255. source_->DeleteItem(i);
  256. return;
  257. }
  258. }
  259. }
  260. unsigned UIListView::AddChildItem(unsigned parentItemID, const String& text, const String& icon, const String& id)
  261. {
  262. if (!itemLookup_.Contains(parentItemID))
  263. return -1;
  264. ListViewItem* item = itemLookup_[parentItemID];
  265. ListViewItem* child = item->AddChild(text.CString(), icon.CString(), TBID(id.CString()));
  266. itemLookup_[itemLookupId_++] = child;
  267. return itemLookupId_ - 1;
  268. }
  269. void UIListView::SetExpanded(unsigned itemID, bool value)
  270. {
  271. if (!itemLookup_.Contains(itemID))
  272. return;
  273. itemLookup_[itemID]->SetExpanded(value);
  274. }
  275. void UIListView::DeleteAllItems()
  276. {
  277. itemLookup_.Clear();
  278. source_->DeleteAllItems();
  279. }
  280. void UIListView::SelectItemByID(const String& id)
  281. {
  282. TBID tid = TBIDC(id.CString());
  283. for (int i = 0; i < source_->GetNumItems(); i++)
  284. {
  285. ListViewItem* item = source_->GetItem(i);
  286. if (tid == item->id)
  287. {
  288. item->SetExpanded(true);
  289. rootList_->SetValue(i);
  290. rootList_->InvalidateList();
  291. return;
  292. }
  293. }
  294. }
  295. }