UIListView.cpp 10.0 KB

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