UIListView.cpp 11 KB

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