UIListView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. void UpdateTextSkin(const String& skin);
  51. void UpdateIcon(const String& icon);
  52. ListViewItemSource* source_;
  53. ListViewItem* parent_;
  54. int depth_;
  55. PODVector<ListViewItem*> children_;
  56. ListViewItemWidget* widget_;
  57. String icon_;
  58. String textSkin_;
  59. };
  60. class ListViewItemWidget : public TBLayout
  61. {
  62. public:
  63. ListViewItemWidget(ListViewItem *item, ListViewItemSource *source, TBSelectItemViewer *sourceviewer, int index);
  64. virtual bool OnEvent(const TBWidgetEvent &ev);
  65. void UpdateText(const String& text)
  66. {
  67. if (textField_)
  68. textField_->SetText(text.CString());
  69. }
  70. void UpdateIcon(const String& icon)
  71. {
  72. if (icon_)
  73. icon_->SetSkinBg(TBIDC(icon.CString()));
  74. }
  75. void UpdateTextSkin(const String& skin)
  76. {
  77. if (textField_)
  78. textField_->SetSkinBg(TBIDC(skin.CString()));
  79. }
  80. private:
  81. TBCheckBox* expandBox_;
  82. TBTextField* textField_;
  83. TBSkinImage* icon_;
  84. ListViewItemSource *source_;
  85. TBSelectItemViewer *sourceviewer_;
  86. int index_;
  87. ListViewItem* item_;
  88. };
  89. class ListViewItemSource : public TBSelectItemSourceList<ListViewItem>
  90. {
  91. public:
  92. TBSelectList* list_;
  93. ListViewItemSource(TBSelectList* list) : list_(list) {}
  94. virtual ~ListViewItemSource() {}
  95. virtual bool Filter(int index, const char *filter);
  96. virtual TBWidget *CreateItemWidget(int index, TBSelectItemViewer *viewer);
  97. };
  98. // implementation
  99. void ListViewItem::UpdateText(const String& text)
  100. {
  101. str = text.CString();
  102. if (widget_)
  103. widget_->UpdateText(text);
  104. }
  105. void ListViewItem::UpdateIcon(const String& icon)
  106. {
  107. icon_ = icon;
  108. if (widget_)
  109. widget_->UpdateIcon(icon);
  110. }
  111. void ListViewItem::UpdateTextSkin(const String& skin)
  112. {
  113. textSkin_ = skin;
  114. if (widget_)
  115. widget_->UpdateTextSkin(skin);
  116. }
  117. ListViewItem* ListViewItem::AddChild(const char *text, const char* icon, const TBID &id)
  118. {
  119. ListViewItem* child = new ListViewItem(text, id, icon, source_);
  120. child->parent_ = this;
  121. child->depth_ = depth_ + 1;
  122. // filter, alphabetically into source
  123. ListViewItem* insert = this;
  124. int parentIndex = -1;
  125. for (int i = 0; i < source_->GetNumItems(); i++)
  126. {
  127. if (source_->GetItem(i) == this)
  128. {
  129. parentIndex = i;
  130. break;
  131. }
  132. }
  133. for (int i = parentIndex + 1; i < source_->GetNumItems(); i++)
  134. {
  135. ListViewItem* item = source_->GetItem(i);
  136. if (item->depth_ <= depth_)
  137. break;
  138. if (item->depth_ != depth_ + 1)
  139. {
  140. insert = item;
  141. continue;
  142. }
  143. if (strcmp(item->str.CStr(), text) >= 0)
  144. break;
  145. insert = item;
  146. }
  147. for (int i = 0; i < source_->GetNumItems(); i++)
  148. {
  149. if (source_->GetItem(i) == insert)
  150. {
  151. source_->AddItem(child, i + 1);
  152. break;
  153. }
  154. }
  155. children_.Push(child);
  156. return child;
  157. }
  158. ListViewItemWidget::ListViewItemWidget(ListViewItem *item, ListViewItemSource *source,
  159. TBSelectItemViewer *sourceviewer, int index)
  160. : source_(source)
  161. , sourceviewer_(sourceviewer)
  162. , index_(index)
  163. , item_(item)
  164. , expandBox_(0)
  165. , textField_(0)
  166. , icon_(0)
  167. {
  168. SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  169. SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP);
  170. SetPaintOverflowFadeout(false);
  171. item_->widget_ = this;
  172. for (int i = 0; i < item->depth_; i++)
  173. {
  174. LayoutParams lp;
  175. lp.SetWidth(6);
  176. lp.SetHeight(4);
  177. TBWidget* spacer = new TBWidget();
  178. spacer->SetLayoutParams(lp);
  179. GetContentRoot()->AddChild(spacer);
  180. }
  181. if (item_->children_.Size())
  182. {
  183. expandBox_ = new TBCheckBox();
  184. expandBox_->SetSkinBg(TBIDC("TBCheckBox.uilistview"));
  185. expandBox_->SetValue(item_->GetExpanded());
  186. expandBox_->SetID(item->id);
  187. GetContentRoot()->AddChild(expandBox_);
  188. }
  189. else
  190. {
  191. LayoutParams lp;
  192. lp.SetWidth(12);
  193. lp.SetHeight(4);
  194. TBWidget* spacer = new TBWidget();
  195. spacer->SetLayoutParams(lp);
  196. GetContentRoot()->AddChild(spacer);
  197. }
  198. if (item->icon_.Length())
  199. {
  200. icon_ = new TBSkinImage(TBIDC(item->icon_.CString()));
  201. icon_->SetIgnoreInput(true);
  202. GetContentRoot()->AddChild(icon_);
  203. }
  204. TBFontDescription fd;
  205. fd.SetID(TBIDC("Vera"));
  206. fd.SetSize(11);
  207. TBTextField* tfield = textField_ = new TBTextField();
  208. tfield->SetIgnoreInput(true);
  209. tfield->SetSkinBg(item->textSkin_.Length() ? TBIDC(item->textSkin_.CString()) : TBIDC("Folder"));
  210. tfield->SetText(item->str);
  211. tfield->SetFontDescription(fd);
  212. SetSkinBg(TBIDC("TBSelectItem"));
  213. GetContentRoot()->AddChild(tfield);
  214. SetID(item->id);
  215. }
  216. bool ListViewItemWidget::OnEvent(const TBWidgetEvent &ev)
  217. {
  218. if (ev.type == EVENT_TYPE_WHEEL)
  219. {
  220. return false;
  221. }
  222. if (ev.type == EVENT_TYPE_POINTER_DOWN || ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  223. {
  224. TBWidget* parent = GetParent();
  225. while (parent)
  226. {
  227. if (parent->IsOfType<TBSelectList>())
  228. {
  229. TBWidgetEvent nev = ev;
  230. nev.ref_id = item_->id;
  231. parent->InvokeEvent(nev);
  232. break;
  233. }
  234. parent = parent->GetParent();
  235. }
  236. return true;
  237. }
  238. // get clicks this way, not sure we want to
  239. if (ev.type == EVENT_TYPE_CLICK && ev.target == expandBox_ && ev.target->GetID() == item_->id)
  240. {
  241. item_->SetExpanded(!item_->GetExpanded());
  242. source_->list_->InvalidateList();
  243. // want to bubble
  244. return false;
  245. }
  246. return TBLayout::OnEvent(ev);
  247. }
  248. bool ListViewItemSource::Filter(int index, const char *filter)
  249. {
  250. ListViewItem* item = GetItem(index);
  251. if (!item->parent_)
  252. return true;
  253. if (item->parent_->GetExpanded())
  254. return true;
  255. return false;
  256. }
  257. TBWidget *ListViewItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  258. {
  259. ListViewItem* item = GetItem(index);
  260. if (TBLayout *layout = new ListViewItemWidget(item, this, viewer, index))
  261. return layout;
  262. return nullptr;
  263. }
  264. /*
  265. static int select_list_sort_cb(TBSelectItemSource *_source, const int *a, const int *b)
  266. {
  267. ListViewItemSource* source = (ListViewItemSource*) _source;
  268. ListViewItem* itemA = source->GetItem(*a);
  269. ListViewItem* itemB = source->GetItem(*b);
  270. int value;
  271. if (itemA->depth_ == itemB->depth_)
  272. {
  273. value = strcmp(source->GetItemString(*a), source->GetItemString(*b));
  274. }
  275. else
  276. {
  277. value = itemA->depth_ > itemB->depth_ ? 1 : -1;
  278. }
  279. return source->GetSort() == TB_SORT_DESCENDING ? -value : value;
  280. }
  281. */
  282. UIListView::UIListView(Context* context, bool createWidget) :
  283. UIWidget(context, createWidget),
  284. source_(0), itemLookupId_(0)
  285. {
  286. rootList_ = new UISelectList(context);
  287. // dummy filter so filter is called
  288. rootList_->SetFilter(" ");
  289. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  290. rootList_->SetGravity(UI_GRAVITY_ALL);
  291. source_ = new ListViewItemSource(rootList_->GetTBSelectList());
  292. rootList_->GetTBSelectList()->SetSource(source_);
  293. AddChild(rootList_);
  294. }
  295. UIListView::~UIListView()
  296. {
  297. }
  298. unsigned UIListView::AddRootItem(const String& text, const String& icon, const String& id)
  299. {
  300. ListViewItem* item = new ListViewItem(text.CString(), TBID(id.CString()), icon.CString(), source_);
  301. source_->AddItem(item);
  302. itemLookup_[itemLookupId_++] = item;
  303. return itemLookupId_ - 1;
  304. }
  305. void UIListView::SetItemText(const String& id, const String& text)
  306. {
  307. TBID tbid(id.CString());
  308. for (int i = 0; i < source_->GetNumItems(); i++)
  309. {
  310. if (source_->GetItemID(i) == tbid)
  311. {
  312. ListViewItem* item = source_->GetItem(i);
  313. item->UpdateText(text);
  314. return;
  315. }
  316. }
  317. }
  318. void UIListView::SetItemTextSkin(const String& id, const String& skin)
  319. {
  320. TBID tbid(id.CString());
  321. for (int i = 0; i < source_->GetNumItems(); i++)
  322. {
  323. if (source_->GetItemID(i) == tbid)
  324. {
  325. ListViewItem* item = source_->GetItem(i);
  326. item->UpdateTextSkin(skin);
  327. return;
  328. }
  329. }
  330. }
  331. void UIListView::SetItemIcon(const String& id, const String& icon)
  332. {
  333. TBID tbid(id.CString());
  334. for (int i = 0; i < source_->GetNumItems(); i++)
  335. {
  336. if (source_->GetItemID(i) == tbid)
  337. {
  338. ListViewItem* item = source_->GetItem(i);
  339. item->UpdateIcon(icon);
  340. return;
  341. }
  342. }
  343. }
  344. void UIListView::DeleteItemByID(const String& id)
  345. {
  346. TBID tbid(id.CString());
  347. for (int i = 0; i < source_->GetNumItems(); i++)
  348. {
  349. if (source_->GetItemID(i) == tbid)
  350. {
  351. ListViewItem* item = source_->GetItem(i);
  352. if (item->parent_)
  353. item->parent_->children_.Remove(item);
  354. PODVector<ListViewItem*> children;
  355. item->GetChildren(children, true);
  356. for (unsigned j = 0; j < children.Size(); j++)
  357. {
  358. for (int k = 0; k < source_->GetNumItems(); k++)
  359. {
  360. if (children[j] == source_->GetItem(k))
  361. {
  362. source_->DeleteItem(k);
  363. break;
  364. }
  365. }
  366. }
  367. source_->DeleteItem(i);
  368. return;
  369. }
  370. }
  371. }
  372. unsigned UIListView::AddChildItem(unsigned parentItemID, const String& text, const String& icon, const String& id)
  373. {
  374. if (!itemLookup_.Contains(parentItemID))
  375. return -1;
  376. ListViewItem* item = itemLookup_[parentItemID];
  377. ListViewItem* child = item->AddChild(text.CString(), icon.CString(), TBID(id.CString()));
  378. itemLookup_[itemLookupId_++] = child;
  379. return itemLookupId_ - 1;
  380. }
  381. void UIListView::SetExpanded(unsigned itemID, bool value)
  382. {
  383. if (!itemLookup_.Contains(itemID))
  384. return;
  385. itemLookup_[itemID]->SetExpanded(value);
  386. }
  387. void UIListView::DeleteAllItems()
  388. {
  389. itemLookup_.Clear();
  390. source_->DeleteAllItems();
  391. }
  392. void UIListView::SelectItemByID(const String& id)
  393. {
  394. TBID tid = TBIDC(id.CString());
  395. for (int i = 0; i < source_->GetNumItems(); i++)
  396. {
  397. ListViewItem* item = source_->GetItem(i);
  398. if (tid == item->id)
  399. {
  400. //item->SetExpanded(true);
  401. rootList_->SetValue(i);
  402. rootList_->InvalidateList();
  403. return;
  404. }
  405. }
  406. }
  407. }