UIListView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_menu_window.h>
  23. #include <TurboBadger/tb_select.h>
  24. #include "UIListView.h"
  25. using namespace tb;
  26. namespace Atomic
  27. {
  28. class ListViewItemSource;
  29. class ListViewItemWidget;
  30. class ListViewItem : public TBGenericStringItem
  31. {
  32. bool expanded_;
  33. public:
  34. ListViewItem(const char *str, const TBID &id, const char* icon, ListViewItemSource* source)
  35. : TBGenericStringItem(str, id), source_(source), parent_(0),
  36. depth_(0), widget_(0), expanded_(false), icon_(icon)
  37. {
  38. }
  39. ListViewItem* AddChild(const char* text, const char* icon, const TBID &id);
  40. bool GetExpanded() { return expanded_; }
  41. void GetChildren(PODVector<ListViewItem*>& children, bool recursive = false)
  42. {
  43. children += children_;
  44. if (recursive)
  45. {
  46. for (unsigned i = 0; i < children_.Size(); i++)
  47. {
  48. children_[i]->GetChildren(children, recursive);
  49. }
  50. }
  51. }
  52. void SetExpanded(bool expanded)
  53. {
  54. expanded_ = expanded;
  55. if (!expanded_)
  56. {
  57. for (unsigned i = 0; i < children_.Size(); i ++)
  58. children_[i]->SetExpanded(expanded);
  59. }
  60. else
  61. {
  62. ListViewItem* p = parent_;
  63. while (p)
  64. {
  65. p->expanded_ = true;
  66. p = p->parent_;
  67. }
  68. }
  69. }
  70. void UpdateText(const String& text);
  71. void UpdateTextSkin(const String& skin);
  72. void UpdateIcon(const String& icon);
  73. ListViewItemSource* source_;
  74. ListViewItem* parent_;
  75. int depth_;
  76. PODVector<ListViewItem*> children_;
  77. ListViewItemWidget* widget_;
  78. String icon_;
  79. String textSkin_;
  80. };
  81. class ListViewItemWidget : public TBLayout
  82. {
  83. public:
  84. ListViewItemWidget(ListViewItem *item, ListViewItemSource *source, TBSelectItemViewer *sourceviewer, int index);
  85. virtual bool OnEvent(const TBWidgetEvent &ev);
  86. void UpdateText(const String& text)
  87. {
  88. if (textField_)
  89. textField_->SetText(text.CString());
  90. }
  91. void UpdateIcon(const String& icon)
  92. {
  93. if (icon_)
  94. icon_->SetSkinBg(TBIDC(icon.CString()));
  95. }
  96. void UpdateTextSkin(const String& skin)
  97. {
  98. if (textField_)
  99. textField_->SetSkinBg(TBIDC(skin.CString()));
  100. }
  101. private:
  102. TBCheckBox* expandBox_;
  103. TBTextField* textField_;
  104. TBSkinImage* icon_;
  105. ListViewItemSource *source_;
  106. TBSelectItemViewer *sourceviewer_;
  107. int index_;
  108. ListViewItem* item_;
  109. };
  110. class ListViewItemSource : public TBSelectItemSourceList<ListViewItem>
  111. {
  112. public:
  113. TBSelectList* list_;
  114. ListViewItemSource(TBSelectList* list) : list_(list) {}
  115. virtual ~ListViewItemSource() {}
  116. virtual bool Filter(int index, const char *filter);
  117. virtual TBWidget *CreateItemWidget(int index, TBSelectItemViewer *viewer);
  118. };
  119. // implementation
  120. void ListViewItem::UpdateText(const String& text)
  121. {
  122. str = text.CString();
  123. if (widget_)
  124. widget_->UpdateText(text);
  125. }
  126. void ListViewItem::UpdateIcon(const String& icon)
  127. {
  128. icon_ = icon;
  129. if (widget_)
  130. widget_->UpdateIcon(icon);
  131. }
  132. void ListViewItem::UpdateTextSkin(const String& skin)
  133. {
  134. textSkin_ = skin;
  135. if (widget_)
  136. widget_->UpdateTextSkin(skin);
  137. }
  138. ListViewItem* ListViewItem::AddChild(const char *text, const char* icon, const TBID &id)
  139. {
  140. ListViewItem* child = new ListViewItem(text, id, icon, source_);
  141. child->parent_ = this;
  142. child->depth_ = depth_ + 1;
  143. // filter, alphabetically into source
  144. ListViewItem* insert = this;
  145. int parentIndex = -1;
  146. for (int i = 0; i < source_->GetNumItems(); i++)
  147. {
  148. if (source_->GetItem(i) == this)
  149. {
  150. parentIndex = i;
  151. break;
  152. }
  153. }
  154. for (int i = parentIndex + 1; i < source_->GetNumItems(); i++)
  155. {
  156. ListViewItem* item = source_->GetItem(i);
  157. if (item->depth_ <= depth_)
  158. break;
  159. if (item->depth_ != depth_ + 1)
  160. {
  161. insert = item;
  162. continue;
  163. }
  164. if (strcmp(item->str.CStr(), text) >= 0)
  165. break;
  166. insert = item;
  167. }
  168. for (int i = 0; i < source_->GetNumItems(); i++)
  169. {
  170. if (source_->GetItem(i) == insert)
  171. {
  172. source_->AddItem(child, i + 1);
  173. break;
  174. }
  175. }
  176. children_.Push(child);
  177. return child;
  178. }
  179. ListViewItemWidget::ListViewItemWidget(ListViewItem *item, ListViewItemSource *source,
  180. TBSelectItemViewer *sourceviewer, int index)
  181. : source_(source)
  182. , sourceviewer_(sourceviewer)
  183. , index_(index)
  184. , item_(item)
  185. , expandBox_(0)
  186. , textField_(0)
  187. , icon_(0)
  188. {
  189. SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  190. SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP);
  191. SetPaintOverflowFadeout(false);
  192. SetCapturing(false);
  193. item_->widget_ = this;
  194. for (int i = 0; i < item->depth_; i++)
  195. {
  196. LayoutParams lp;
  197. lp.SetWidth(6);
  198. lp.SetHeight(4);
  199. TBWidget* spacer = new TBWidget();
  200. spacer->SetLayoutParams(lp);
  201. GetContentRoot()->AddChild(spacer);
  202. }
  203. if (item_->children_.Size())
  204. {
  205. expandBox_ = new TBCheckBox();
  206. expandBox_->SetSkinBg(TBIDC("TBCheckBox.uilistview"));
  207. expandBox_->SetValue(item_->GetExpanded());
  208. expandBox_->SetID(item->id);
  209. GetContentRoot()->AddChild(expandBox_);
  210. }
  211. else
  212. {
  213. LayoutParams lp;
  214. lp.SetWidth(12);
  215. lp.SetHeight(4);
  216. TBWidget* spacer = new TBWidget();
  217. spacer->SetLayoutParams(lp);
  218. GetContentRoot()->AddChild(spacer);
  219. }
  220. if (item->icon_.Length())
  221. {
  222. icon_ = new TBSkinImage(TBIDC(item->icon_.CString()));
  223. icon_->SetIgnoreInput(true);
  224. GetContentRoot()->AddChild(icon_);
  225. }
  226. TBFontDescription fd;
  227. fd.SetID(TBIDC("Vera"));
  228. fd.SetSize(11);
  229. TBTextField* tfield = textField_ = new TBTextField();
  230. tfield->SetIgnoreInput(true);
  231. tfield->SetSkinBg(item->textSkin_.Length() ? TBIDC(item->textSkin_.CString()) : TBIDC("Folder"));
  232. tfield->SetText(item->str);
  233. tfield->SetFontDescription(fd);
  234. SetSkinBg(TBIDC("TBSelectItem"));
  235. GetContentRoot()->AddChild(tfield);
  236. SetID(item->id);
  237. }
  238. bool ListViewItemWidget::OnEvent(const TBWidgetEvent &ev)
  239. {
  240. if (ev.type == EVENT_TYPE_WHEEL)
  241. {
  242. return false;
  243. }
  244. if (ev.type == EVENT_TYPE_POINTER_DOWN || ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  245. {
  246. TBWidget* parent = GetParent();
  247. while (parent)
  248. {
  249. if (parent->IsOfType<TBSelectList>())
  250. {
  251. TBWidgetEvent nev = ev;
  252. nev.ref_id = item_->id;
  253. parent->InvokeEvent(nev);
  254. break;
  255. }
  256. parent = parent->GetParent();
  257. }
  258. return true;
  259. }
  260. // get clicks this way, not sure we want to
  261. if (ev.type == EVENT_TYPE_CLICK && ev.target == expandBox_ && ev.target->GetID() == item_->id)
  262. {
  263. item_->SetExpanded(!item_->GetExpanded());
  264. source_->list_->InvalidateList();
  265. // want to bubble
  266. return false;
  267. }
  268. return TBLayout::OnEvent(ev);
  269. }
  270. bool ListViewItemSource::Filter(int index, const char *filter)
  271. {
  272. ListViewItem* item = GetItem(index);
  273. if (!item->parent_)
  274. return true;
  275. if (item->parent_->GetExpanded())
  276. return true;
  277. return false;
  278. }
  279. TBWidget *ListViewItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  280. {
  281. ListViewItem* item = GetItem(index);
  282. if (TBLayout *layout = new ListViewItemWidget(item, this, viewer, index))
  283. return layout;
  284. return nullptr;
  285. }
  286. /*
  287. static int select_list_sort_cb(TBSelectItemSource *_source, const int *a, const int *b)
  288. {
  289. ListViewItemSource* source = (ListViewItemSource*) _source;
  290. ListViewItem* itemA = source->GetItem(*a);
  291. ListViewItem* itemB = source->GetItem(*b);
  292. int value;
  293. if (itemA->depth_ == itemB->depth_)
  294. {
  295. value = strcmp(source->GetItemString(*a), source->GetItemString(*b));
  296. }
  297. else
  298. {
  299. value = itemA->depth_ > itemB->depth_ ? 1 : -1;
  300. }
  301. return source->GetSort() == TB_SORT_DESCENDING ? -value : value;
  302. }
  303. */
  304. UIListView::UIListView(Context* context, bool createWidget) :
  305. UIWidget(context, createWidget),
  306. source_(0), itemLookupId_(0)
  307. {
  308. rootList_ = new UISelectList(context);
  309. // dummy filter so filter is called
  310. rootList_->SetFilter(" ");
  311. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  312. rootList_->SetGravity(UI_GRAVITY_ALL);
  313. source_ = new ListViewItemSource(rootList_->GetTBSelectList());
  314. rootList_->GetTBSelectList()->SetSource(source_);
  315. AddChild(rootList_);
  316. }
  317. UIListView::~UIListView()
  318. {
  319. }
  320. unsigned UIListView::AddRootItem(const String& text, const String& icon, const String& id)
  321. {
  322. ListViewItem* item = new ListViewItem(text.CString(), TBID(id.CString()), icon.CString(), source_);
  323. source_->AddItem(item);
  324. itemLookup_[itemLookupId_++] = item;
  325. return itemLookupId_ - 1;
  326. }
  327. void UIListView::SetItemText(const String& id, const String& text)
  328. {
  329. TBID tbid(id.CString());
  330. for (int i = 0; i < source_->GetNumItems(); i++)
  331. {
  332. if (source_->GetItemID(i) == tbid)
  333. {
  334. ListViewItem* item = source_->GetItem(i);
  335. item->UpdateText(text);
  336. return;
  337. }
  338. }
  339. }
  340. void UIListView::SetItemTextSkin(const String& id, const String& skin)
  341. {
  342. TBID tbid(id.CString());
  343. for (int i = 0; i < source_->GetNumItems(); i++)
  344. {
  345. if (source_->GetItemID(i) == tbid)
  346. {
  347. ListViewItem* item = source_->GetItem(i);
  348. item->UpdateTextSkin(skin);
  349. return;
  350. }
  351. }
  352. }
  353. void UIListView::SetItemIcon(const String& id, const String& icon)
  354. {
  355. TBID tbid(id.CString());
  356. for (int i = 0; i < source_->GetNumItems(); i++)
  357. {
  358. if (source_->GetItemID(i) == tbid)
  359. {
  360. ListViewItem* item = source_->GetItem(i);
  361. item->UpdateIcon(icon);
  362. return;
  363. }
  364. }
  365. }
  366. void UIListView::DeleteItemByID(const String& id)
  367. {
  368. TBID tbid(id.CString());
  369. for (int i = 0; i < source_->GetNumItems(); i++)
  370. {
  371. if (source_->GetItemID(i) == tbid)
  372. {
  373. ListViewItem* item = source_->GetItem(i);
  374. if (item->parent_)
  375. item->parent_->children_.Remove(item);
  376. PODVector<ListViewItem*> children;
  377. item->GetChildren(children, true);
  378. for (unsigned j = 0; j < children.Size(); j++)
  379. {
  380. for (int k = 0; k < source_->GetNumItems(); k++)
  381. {
  382. if (children[j] == source_->GetItem(k))
  383. {
  384. source_->DeleteItem(k);
  385. break;
  386. }
  387. }
  388. }
  389. source_->DeleteItem(i);
  390. return;
  391. }
  392. }
  393. }
  394. unsigned UIListView::AddChildItem(unsigned parentItemID, const String& text, const String& icon, const String& id)
  395. {
  396. if (!itemLookup_.Contains(parentItemID))
  397. return -1;
  398. ListViewItem* item = itemLookup_[parentItemID];
  399. ListViewItem* child = item->AddChild(text.CString(), icon.CString(), TBID(id.CString()));
  400. itemLookup_[itemLookupId_++] = child;
  401. return itemLookupId_ - 1;
  402. }
  403. void UIListView::SetExpanded(unsigned itemID, bool value)
  404. {
  405. if (!itemLookup_.Contains(itemID))
  406. return;
  407. itemLookup_[itemID]->SetExpanded(value);
  408. }
  409. void UIListView::DeleteAllItems()
  410. {
  411. itemLookup_.Clear();
  412. source_->DeleteAllItems();
  413. }
  414. void UIListView::SelectItemByID(const String& id)
  415. {
  416. TBID tid = TBIDC(id.CString());
  417. for (int i = 0; i < source_->GetNumItems(); i++)
  418. {
  419. ListViewItem* item = source_->GetItem(i);
  420. if (tid == item->id)
  421. {
  422. //item->SetExpanded(true);
  423. rootList_->SetValue(i);
  424. rootList_->InvalidateList();
  425. return;
  426. }
  427. }
  428. }
  429. void UIListView::ScrollToSelectedItem()
  430. {
  431. if (rootList_.Null())
  432. return;
  433. rootList_->ScrollToSelectedItem();
  434. }
  435. }