UIListView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. SetFocus(WIDGET_FOCUS_REASON_POINTER);
  247. TBWidget* parent = GetParent();
  248. while (parent)
  249. {
  250. if (parent->IsOfType<TBSelectList>())
  251. {
  252. TBWidgetEvent nev = ev;
  253. nev.ref_id = item_->id;
  254. parent->InvokeEvent(nev);
  255. break;
  256. }
  257. parent = parent->GetParent();
  258. }
  259. return true;
  260. }
  261. // get clicks this way, not sure we want to
  262. if (ev.type == EVENT_TYPE_CLICK && ev.target == expandBox_ && ev.target->GetID() == item_->id)
  263. {
  264. item_->SetExpanded(!item_->GetExpanded());
  265. source_->list_->InvalidateList();
  266. // want to bubble
  267. return false;
  268. }
  269. return TBLayout::OnEvent(ev);
  270. }
  271. bool ListViewItemSource::Filter(int index, const char *filter)
  272. {
  273. ListViewItem* item = GetItem(index);
  274. if (!item->parent_)
  275. return true;
  276. if (item->parent_->GetExpanded())
  277. return true;
  278. return false;
  279. }
  280. TBWidget *ListViewItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  281. {
  282. ListViewItem* item = GetItem(index);
  283. if (TBLayout *layout = new ListViewItemWidget(item, this, viewer, index))
  284. return layout;
  285. return nullptr;
  286. }
  287. /*
  288. static int select_list_sort_cb(TBSelectItemSource *_source, const int *a, const int *b)
  289. {
  290. ListViewItemSource* source = (ListViewItemSource*) _source;
  291. ListViewItem* itemA = source->GetItem(*a);
  292. ListViewItem* itemB = source->GetItem(*b);
  293. int value;
  294. if (itemA->depth_ == itemB->depth_)
  295. {
  296. value = strcmp(source->GetItemString(*a), source->GetItemString(*b));
  297. }
  298. else
  299. {
  300. value = itemA->depth_ > itemB->depth_ ? 1 : -1;
  301. }
  302. return source->GetSort() == TB_SORT_DESCENDING ? -value : value;
  303. }
  304. */
  305. UIListView::UIListView(Context* context, bool createWidget) :
  306. UIWidget(context, createWidget),
  307. source_(0), itemLookupId_(0)
  308. {
  309. rootList_ = new UISelectList(context);
  310. // dummy filter so filter is called
  311. rootList_->SetFilter(" ");
  312. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  313. rootList_->SetGravity(UI_GRAVITY_ALL);
  314. source_ = new ListViewItemSource(rootList_->GetTBSelectList());
  315. rootList_->GetTBSelectList()->SetSource(source_);
  316. AddChild(rootList_);
  317. }
  318. UIListView::~UIListView()
  319. {
  320. }
  321. unsigned UIListView::AddRootItem(const String& text, const String& icon, const String& id)
  322. {
  323. ListViewItem* item = new ListViewItem(text.CString(), TBID(id.CString()), icon.CString(), source_);
  324. source_->AddItem(item);
  325. itemLookup_[itemLookupId_++] = item;
  326. return itemLookupId_ - 1;
  327. }
  328. void UIListView::SetItemText(const String& id, const String& text)
  329. {
  330. TBID tbid(id.CString());
  331. for (int i = 0; i < source_->GetNumItems(); i++)
  332. {
  333. if (source_->GetItemID(i) == tbid)
  334. {
  335. ListViewItem* item = source_->GetItem(i);
  336. item->UpdateText(text);
  337. return;
  338. }
  339. }
  340. }
  341. void UIListView::SetItemTextSkin(const String& id, const String& skin)
  342. {
  343. TBID tbid(id.CString());
  344. for (int i = 0; i < source_->GetNumItems(); i++)
  345. {
  346. if (source_->GetItemID(i) == tbid)
  347. {
  348. ListViewItem* item = source_->GetItem(i);
  349. item->UpdateTextSkin(skin);
  350. return;
  351. }
  352. }
  353. }
  354. void UIListView::SetItemIcon(const String& id, const String& icon)
  355. {
  356. TBID tbid(id.CString());
  357. for (int i = 0; i < source_->GetNumItems(); i++)
  358. {
  359. if (source_->GetItemID(i) == tbid)
  360. {
  361. ListViewItem* item = source_->GetItem(i);
  362. item->UpdateIcon(icon);
  363. return;
  364. }
  365. }
  366. }
  367. void UIListView::DeleteItemByID(const String& id)
  368. {
  369. TBID tbid(id.CString());
  370. for (int i = 0; i < source_->GetNumItems(); i++)
  371. {
  372. if (source_->GetItemID(i) == tbid)
  373. {
  374. ListViewItem* item = source_->GetItem(i);
  375. if (item->parent_)
  376. item->parent_->children_.Remove(item);
  377. PODVector<ListViewItem*> children;
  378. item->GetChildren(children, true);
  379. for (unsigned j = 0; j < children.Size(); j++)
  380. {
  381. for (int k = 0; k < source_->GetNumItems(); k++)
  382. {
  383. if (children[j] == source_->GetItem(k))
  384. {
  385. source_->DeleteItem(k);
  386. break;
  387. }
  388. }
  389. }
  390. source_->DeleteItem(i);
  391. return;
  392. }
  393. }
  394. }
  395. unsigned UIListView::AddChildItem(unsigned parentItemID, const String& text, const String& icon, const String& id)
  396. {
  397. if (!itemLookup_.Contains(parentItemID))
  398. return -1;
  399. ListViewItem* item = itemLookup_[parentItemID];
  400. ListViewItem* child = item->AddChild(text.CString(), icon.CString(), TBID(id.CString()));
  401. itemLookup_[itemLookupId_++] = child;
  402. return itemLookupId_ - 1;
  403. }
  404. void UIListView::SetExpanded(unsigned itemID, bool value)
  405. {
  406. if (!itemLookup_.Contains(itemID))
  407. return;
  408. itemLookup_[itemID]->SetExpanded(value);
  409. }
  410. bool UIListView::GetExpanded(unsigned itemID)
  411. {
  412. if (!itemLookup_.Contains(itemID))
  413. return false;
  414. return itemLookup_[itemID]->GetExpanded();
  415. }
  416. bool UIListView::GetExpandable(unsigned itemID)
  417. {
  418. if (!itemLookup_.Contains(itemID))
  419. return false;
  420. return itemLookup_[itemID]->children_.Size() > 0;
  421. }
  422. void UIListView::DeleteAllItems()
  423. {
  424. itemLookup_.Clear();
  425. source_->DeleteAllItems();
  426. }
  427. void UIListView::SelectItemByID(const String& id)
  428. {
  429. TBID tid = TBIDC(id.CString());
  430. for (int i = 0; i < source_->GetNumItems(); i++)
  431. {
  432. ListViewItem* item = source_->GetItem(i);
  433. if (tid == item->id)
  434. {
  435. //item->SetExpanded(true);
  436. rootList_->SetValue(i);
  437. rootList_->InvalidateList();
  438. return;
  439. }
  440. }
  441. }
  442. void UIListView::ScrollToSelectedItem()
  443. {
  444. if (rootList_.Null())
  445. return;
  446. rootList_->ScrollToSelectedItem();
  447. }
  448. }