UIListView.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. SetID(item->id);
  188. }
  189. bool ListViewItemWidget::OnEvent(const TBWidgetEvent &ev)
  190. {
  191. if (ev.type == EVENT_TYPE_WHEEL)
  192. {
  193. return false;
  194. }
  195. if (ev.type == EVENT_TYPE_POINTER_DOWN || ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  196. {
  197. TBWidget* parent = GetParent();
  198. while (parent)
  199. {
  200. if (parent->IsOfType<TBSelectList>())
  201. {
  202. TBWidgetEvent nev = ev;
  203. nev.ref_id = item_->id;
  204. parent->InvokeEvent(nev);
  205. break;
  206. }
  207. parent = parent->GetParent();
  208. }
  209. return true;
  210. }
  211. // get clicks this way, not sure we want to
  212. if (ev.type == EVENT_TYPE_CLICK && ev.target == expandBox_ && ev.target->GetID() == item_->id)
  213. {
  214. item_->SetExpanded(!item_->GetExpanded());
  215. source_->list_->InvalidateList();
  216. // want to bubble
  217. return false;
  218. }
  219. return TBLayout::OnEvent(ev);
  220. }
  221. bool ListViewItemSource::Filter(int index, const char *filter)
  222. {
  223. ListViewItem* item = GetItem(index);
  224. if (!item->parent_)
  225. return true;
  226. if (item->parent_->GetExpanded())
  227. return true;
  228. return false;
  229. }
  230. TBWidget *ListViewItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  231. {
  232. ListViewItem* item = GetItem(index);
  233. if (TBLayout *layout = new ListViewItemWidget(item, this, viewer, index))
  234. return layout;
  235. return nullptr;
  236. }
  237. /*
  238. static int select_list_sort_cb(TBSelectItemSource *_source, const int *a, const int *b)
  239. {
  240. ListViewItemSource* source = (ListViewItemSource*) _source;
  241. ListViewItem* itemA = source->GetItem(*a);
  242. ListViewItem* itemB = source->GetItem(*b);
  243. int value;
  244. if (itemA->depth_ == itemB->depth_)
  245. {
  246. value = strcmp(source->GetItemString(*a), source->GetItemString(*b));
  247. }
  248. else
  249. {
  250. value = itemA->depth_ > itemB->depth_ ? 1 : -1;
  251. }
  252. return source->GetSort() == TB_SORT_DESCENDING ? -value : value;
  253. }
  254. */
  255. UIListView::UIListView(Context* context, bool createWidget) :
  256. UIWidget(context, createWidget),
  257. source_(0), itemLookupId_(0)
  258. {
  259. rootList_ = new UISelectList(context);
  260. // dummy filter so filter is called
  261. rootList_->SetFilter(" ");
  262. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  263. rootList_->SetGravity(UI_GRAVITY_ALL);
  264. source_ = new ListViewItemSource(rootList_->GetTBSelectList());
  265. rootList_->GetTBSelectList()->SetSource(source_);
  266. AddChild(rootList_);
  267. }
  268. UIListView::~UIListView()
  269. {
  270. }
  271. unsigned UIListView::AddRootItem(const String& text, const String& icon, const String& id)
  272. {
  273. ListViewItem* item = new ListViewItem(text.CString(), TBID(id.CString()), icon.CString(), source_);
  274. source_->AddItem(item);
  275. itemLookup_[itemLookupId_++] = item;
  276. return itemLookupId_ - 1;
  277. }
  278. void UIListView::SetItemText(const String& id, const String& text)
  279. {
  280. TBID tbid(id.CString());
  281. for (int i = 0; i < source_->GetNumItems(); i++)
  282. {
  283. if (source_->GetItemID(i) == tbid)
  284. {
  285. ListViewItem* item = source_->GetItem(i);
  286. item->UpdateText(text);
  287. return;
  288. }
  289. }
  290. }
  291. void UIListView::DeleteItemByID(const String& id)
  292. {
  293. TBID tbid(id.CString());
  294. for (int i = 0; i < source_->GetNumItems(); i++)
  295. {
  296. if (source_->GetItemID(i) == tbid)
  297. {
  298. ListViewItem* item = source_->GetItem(i);
  299. if (item->parent_)
  300. item->parent_->children_.Remove(item);
  301. PODVector<ListViewItem*> children;
  302. item->GetChildren(children, true);
  303. for (unsigned j = 0; j < children.Size(); j++)
  304. {
  305. for (int k = 0; k < source_->GetNumItems(); k++)
  306. {
  307. if (children[j] == source_->GetItem(k))
  308. {
  309. source_->DeleteItem(k);
  310. break;
  311. }
  312. }
  313. }
  314. source_->DeleteItem(i);
  315. return;
  316. }
  317. }
  318. }
  319. unsigned UIListView::AddChildItem(unsigned parentItemID, const String& text, const String& icon, const String& id)
  320. {
  321. if (!itemLookup_.Contains(parentItemID))
  322. return -1;
  323. ListViewItem* item = itemLookup_[parentItemID];
  324. ListViewItem* child = item->AddChild(text.CString(), icon.CString(), TBID(id.CString()));
  325. itemLookup_[itemLookupId_++] = child;
  326. return itemLookupId_ - 1;
  327. }
  328. void UIListView::SetExpanded(unsigned itemID, bool value)
  329. {
  330. if (!itemLookup_.Contains(itemID))
  331. return;
  332. itemLookup_[itemID]->SetExpanded(value);
  333. }
  334. void UIListView::DeleteAllItems()
  335. {
  336. itemLookup_.Clear();
  337. source_->DeleteAllItems();
  338. }
  339. void UIListView::SelectItemByID(const String& id)
  340. {
  341. TBID tid = TBIDC(id.CString());
  342. for (int i = 0; i < source_->GetNumItems(); i++)
  343. {
  344. ListViewItem* item = source_->GetItem(i);
  345. if (tid == item->id)
  346. {
  347. //item->SetExpanded(true);
  348. rootList_->SetValue(i);
  349. rootList_->InvalidateList();
  350. return;
  351. }
  352. }
  353. }
  354. }