UIListView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. item_->widget_ = this;
  193. for (int i = 0; i < item->depth_; i++)
  194. {
  195. LayoutParams lp;
  196. lp.SetWidth(6);
  197. lp.SetHeight(4);
  198. TBWidget* spacer = new TBWidget();
  199. spacer->SetLayoutParams(lp);
  200. GetContentRoot()->AddChild(spacer);
  201. }
  202. if (item_->children_.Size())
  203. {
  204. expandBox_ = new TBCheckBox();
  205. expandBox_->SetSkinBg(TBIDC("TBCheckBox.uilistview"));
  206. expandBox_->SetValue(item_->GetExpanded());
  207. expandBox_->SetID(item->id);
  208. GetContentRoot()->AddChild(expandBox_);
  209. }
  210. else
  211. {
  212. LayoutParams lp;
  213. lp.SetWidth(12);
  214. lp.SetHeight(4);
  215. TBWidget* spacer = new TBWidget();
  216. spacer->SetLayoutParams(lp);
  217. GetContentRoot()->AddChild(spacer);
  218. }
  219. if (item->icon_.Length())
  220. {
  221. icon_ = new TBSkinImage(TBIDC(item->icon_.CString()));
  222. icon_->SetIgnoreInput(true);
  223. GetContentRoot()->AddChild(icon_);
  224. }
  225. TBFontDescription fd;
  226. fd.SetID(TBIDC("Vera"));
  227. fd.SetSize(11);
  228. TBTextField* tfield = textField_ = new TBTextField();
  229. tfield->SetIgnoreInput(true);
  230. tfield->SetSkinBg(item->textSkin_.Length() ? TBIDC(item->textSkin_.CString()) : TBIDC("Folder"));
  231. tfield->SetText(item->str);
  232. tfield->SetFontDescription(fd);
  233. SetSkinBg(TBIDC("TBSelectItem"));
  234. GetContentRoot()->AddChild(tfield);
  235. SetID(item->id);
  236. }
  237. bool ListViewItemWidget::OnEvent(const TBWidgetEvent &ev)
  238. {
  239. if (ev.type == EVENT_TYPE_WHEEL)
  240. {
  241. return false;
  242. }
  243. if (ev.type == EVENT_TYPE_POINTER_DOWN || ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  244. {
  245. TBWidget* parent = GetParent();
  246. while (parent)
  247. {
  248. if (parent->IsOfType<TBSelectList>())
  249. {
  250. TBWidgetEvent nev = ev;
  251. nev.ref_id = item_->id;
  252. parent->InvokeEvent(nev);
  253. break;
  254. }
  255. parent = parent->GetParent();
  256. }
  257. return true;
  258. }
  259. // get clicks this way, not sure we want to
  260. if (ev.type == EVENT_TYPE_CLICK && ev.target == expandBox_ && ev.target->GetID() == item_->id)
  261. {
  262. item_->SetExpanded(!item_->GetExpanded());
  263. source_->list_->InvalidateList();
  264. // want to bubble
  265. return false;
  266. }
  267. return TBLayout::OnEvent(ev);
  268. }
  269. bool ListViewItemSource::Filter(int index, const char *filter)
  270. {
  271. ListViewItem* item = GetItem(index);
  272. if (!item->parent_)
  273. return true;
  274. if (item->parent_->GetExpanded())
  275. return true;
  276. return false;
  277. }
  278. TBWidget *ListViewItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  279. {
  280. ListViewItem* item = GetItem(index);
  281. if (TBLayout *layout = new ListViewItemWidget(item, this, viewer, index))
  282. return layout;
  283. return nullptr;
  284. }
  285. /*
  286. static int select_list_sort_cb(TBSelectItemSource *_source, const int *a, const int *b)
  287. {
  288. ListViewItemSource* source = (ListViewItemSource*) _source;
  289. ListViewItem* itemA = source->GetItem(*a);
  290. ListViewItem* itemB = source->GetItem(*b);
  291. int value;
  292. if (itemA->depth_ == itemB->depth_)
  293. {
  294. value = strcmp(source->GetItemString(*a), source->GetItemString(*b));
  295. }
  296. else
  297. {
  298. value = itemA->depth_ > itemB->depth_ ? 1 : -1;
  299. }
  300. return source->GetSort() == TB_SORT_DESCENDING ? -value : value;
  301. }
  302. */
  303. UIListView::UIListView(Context* context, bool createWidget) :
  304. UIWidget(context, createWidget),
  305. source_(0), itemLookupId_(0)
  306. {
  307. rootList_ = new UISelectList(context);
  308. // dummy filter so filter is called
  309. rootList_->SetFilter(" ");
  310. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  311. rootList_->SetGravity(UI_GRAVITY_ALL);
  312. source_ = new ListViewItemSource(rootList_->GetTBSelectList());
  313. rootList_->GetTBSelectList()->SetSource(source_);
  314. AddChild(rootList_);
  315. }
  316. UIListView::~UIListView()
  317. {
  318. }
  319. unsigned UIListView::AddRootItem(const String& text, const String& icon, const String& id)
  320. {
  321. ListViewItem* item = new ListViewItem(text.CString(), TBID(id.CString()), icon.CString(), source_);
  322. source_->AddItem(item);
  323. itemLookup_[itemLookupId_++] = item;
  324. return itemLookupId_ - 1;
  325. }
  326. void UIListView::SetItemText(const String& id, const String& text)
  327. {
  328. TBID tbid(id.CString());
  329. for (int i = 0; i < source_->GetNumItems(); i++)
  330. {
  331. if (source_->GetItemID(i) == tbid)
  332. {
  333. ListViewItem* item = source_->GetItem(i);
  334. item->UpdateText(text);
  335. return;
  336. }
  337. }
  338. }
  339. void UIListView::SetItemTextSkin(const String& id, const String& skin)
  340. {
  341. TBID tbid(id.CString());
  342. for (int i = 0; i < source_->GetNumItems(); i++)
  343. {
  344. if (source_->GetItemID(i) == tbid)
  345. {
  346. ListViewItem* item = source_->GetItem(i);
  347. item->UpdateTextSkin(skin);
  348. return;
  349. }
  350. }
  351. }
  352. void UIListView::SetItemIcon(const String& id, const String& icon)
  353. {
  354. TBID tbid(id.CString());
  355. for (int i = 0; i < source_->GetNumItems(); i++)
  356. {
  357. if (source_->GetItemID(i) == tbid)
  358. {
  359. ListViewItem* item = source_->GetItem(i);
  360. item->UpdateIcon(icon);
  361. return;
  362. }
  363. }
  364. }
  365. void UIListView::DeleteItemByID(const String& id)
  366. {
  367. TBID tbid(id.CString());
  368. for (int i = 0; i < source_->GetNumItems(); i++)
  369. {
  370. if (source_->GetItemID(i) == tbid)
  371. {
  372. ListViewItem* item = source_->GetItem(i);
  373. if (item->parent_)
  374. item->parent_->children_.Remove(item);
  375. PODVector<ListViewItem*> children;
  376. item->GetChildren(children, true);
  377. for (unsigned j = 0; j < children.Size(); j++)
  378. {
  379. for (int k = 0; k < source_->GetNumItems(); k++)
  380. {
  381. if (children[j] == source_->GetItem(k))
  382. {
  383. source_->DeleteItem(k);
  384. break;
  385. }
  386. }
  387. }
  388. source_->DeleteItem(i);
  389. return;
  390. }
  391. }
  392. }
  393. unsigned UIListView::AddChildItem(unsigned parentItemID, const String& text, const String& icon, const String& id)
  394. {
  395. if (!itemLookup_.Contains(parentItemID))
  396. return -1;
  397. ListViewItem* item = itemLookup_[parentItemID];
  398. ListViewItem* child = item->AddChild(text.CString(), icon.CString(), TBID(id.CString()));
  399. itemLookup_[itemLookupId_++] = child;
  400. return itemLookupId_ - 1;
  401. }
  402. void UIListView::SetExpanded(unsigned itemID, bool value)
  403. {
  404. if (!itemLookup_.Contains(itemID))
  405. return;
  406. itemLookup_[itemID]->SetExpanded(value);
  407. }
  408. void UIListView::DeleteAllItems()
  409. {
  410. itemLookup_.Clear();
  411. source_->DeleteAllItems();
  412. }
  413. void UIListView::SelectItemByID(const String& id)
  414. {
  415. TBID tid = TBIDC(id.CString());
  416. for (int i = 0; i < source_->GetNumItems(); i++)
  417. {
  418. ListViewItem* item = source_->GetItem(i);
  419. if (tid == item->id)
  420. {
  421. //item->SetExpanded(true);
  422. rootList_->SetValue(i);
  423. rootList_->InvalidateList();
  424. return;
  425. }
  426. }
  427. }
  428. void UIListView::ScrollToSelectedItem()
  429. {
  430. if (rootList_.Null())
  431. return;
  432. rootList_->ScrollToSelectedItem();
  433. }
  434. }