UIListView.cpp 9.4 KB

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