UIListView.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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 <Atomic/IO/Log.h>
  25. #include "UI.h"
  26. #include "UIEvents.h"
  27. #include "UIListView.h"
  28. using namespace tb;
  29. namespace Atomic
  30. {
  31. class ListViewItemSource;
  32. class ListViewItemWidget;
  33. class ListViewItemWidget : public TBLayout
  34. {
  35. public:
  36. ListViewItemWidget(ListViewItem *item, ListViewItemSource *source, TBSelectItemViewer *sourceviewer, int index);
  37. virtual bool OnEvent(const TBWidgetEvent &ev);
  38. void UpdateText(const String& text)
  39. {
  40. if (textField_)
  41. textField_->SetText(text.CString());
  42. }
  43. void UpdateIcon(const String& icon)
  44. {
  45. if (icon_)
  46. icon_->SetSkinBg(TBIDC(icon.CString()));
  47. }
  48. void UpdateTextSkin(const String& skin)
  49. {
  50. if (textField_)
  51. textField_->SetSkinBg(TBIDC(skin.CString()));
  52. }
  53. void SetExpanded(bool expanded)
  54. {
  55. if (expandBox_)
  56. expandBox_->SetValue(expanded ? 1 : 0);
  57. }
  58. private:
  59. TBCheckBox* expandBox_;
  60. TBTextField* textField_;
  61. TBSkinImage* icon_;
  62. ListViewItemSource *source_;
  63. TBSelectItemViewer *sourceviewer_;
  64. int index_;
  65. ListViewItem* item_;
  66. };
  67. class ListViewItem : public TBGenericStringItem
  68. {
  69. bool expanded_;
  70. bool selected_;
  71. public:
  72. ListViewItem(const char *str, const TBID &id, const char* icon, ListViewItemSource* source)
  73. : TBGenericStringItem(str, id), source_(source), parent_(0),
  74. depth_(0), widget_(0), expanded_(false), icon_(icon), selected_(false)
  75. {
  76. }
  77. ListViewItem* AddChild(const char* text, const char* icon, const TBID &id);
  78. bool GetSelected() { return selected_; }
  79. void SetSelected(bool value)
  80. {
  81. selected_ = value;
  82. }
  83. bool GetExpanded() { return expanded_; }
  84. void GetChildren(PODVector<ListViewItem*>& children, bool recursive = false)
  85. {
  86. children += children_;
  87. if (recursive)
  88. {
  89. for (unsigned i = 0; i < children_.Size(); i++)
  90. {
  91. children_[i]->GetChildren(children, recursive);
  92. }
  93. }
  94. }
  95. void SetExpanded(bool expanded)
  96. {
  97. if (widget_)
  98. widget_->SetExpanded(expanded);
  99. expanded_ = expanded;
  100. if (!expanded_)
  101. {
  102. //for (unsigned i = 0; i < children_.Size(); i ++)
  103. // children_[i]->SetExpanded(expanded);
  104. }
  105. else
  106. {
  107. if (parent_)
  108. parent_->SetExpanded(expanded_);
  109. }
  110. }
  111. void UpdateText(const String& text);
  112. void UpdateTextSkin(const String& skin);
  113. void UpdateIcon(const String& icon);
  114. ListViewItemSource* source_;
  115. ListViewItem* parent_;
  116. int depth_;
  117. PODVector<ListViewItem*> children_;
  118. ListViewItemWidget* widget_;
  119. String icon_;
  120. String textSkin_;
  121. };
  122. class ListViewItemSource : public TBSelectItemSourceList<ListViewItem>
  123. {
  124. public:
  125. UIListView* uiListView_;
  126. ListViewItemSource(UIListView* list) : uiListView_(list) {}
  127. virtual ~ListViewItemSource() {}
  128. virtual bool Filter(int index, const char *filter);
  129. virtual TBWidget *CreateItemWidget(int index, TBSelectItemViewer *viewer);
  130. };
  131. // implementation
  132. void ListViewItem::UpdateText(const String& text)
  133. {
  134. str = text.CString();
  135. if (widget_)
  136. widget_->UpdateText(text);
  137. }
  138. void ListViewItem::UpdateIcon(const String& icon)
  139. {
  140. icon_ = icon;
  141. if (widget_)
  142. widget_->UpdateIcon(icon);
  143. }
  144. void ListViewItem::UpdateTextSkin(const String& skin)
  145. {
  146. textSkin_ = skin;
  147. if (widget_)
  148. widget_->UpdateTextSkin(skin);
  149. }
  150. ListViewItem* ListViewItem::AddChild(const char *text, const char* icon, const TBID &id)
  151. {
  152. ListViewItem* child = new ListViewItem(text, id, icon, source_);
  153. child->parent_ = this;
  154. child->depth_ = depth_ + 1;
  155. // filter, alphabetically into source
  156. ListViewItem* insert = this;
  157. int parentIndex = -1;
  158. for (int i = 0; i < source_->GetNumItems(); i++)
  159. {
  160. if (source_->GetItem(i) == this)
  161. {
  162. parentIndex = i;
  163. break;
  164. }
  165. }
  166. for (int i = parentIndex + 1; i < source_->GetNumItems(); i++)
  167. {
  168. ListViewItem* item = source_->GetItem(i);
  169. if (item->depth_ <= depth_)
  170. break;
  171. if (item->depth_ != depth_ + 1)
  172. {
  173. insert = item;
  174. continue;
  175. }
  176. if (strcmp(item->str.CStr(), text) >= 0)
  177. break;
  178. insert = item;
  179. }
  180. for (int i = 0; i < source_->GetNumItems(); i++)
  181. {
  182. if (source_->GetItem(i) == insert)
  183. {
  184. source_->AddItem(child, i + 1);
  185. break;
  186. }
  187. }
  188. children_.Push(child);
  189. return child;
  190. }
  191. ListViewItemWidget::ListViewItemWidget(ListViewItem *item, ListViewItemSource *source,
  192. TBSelectItemViewer *sourceviewer, int index)
  193. : source_(source)
  194. , sourceviewer_(sourceviewer)
  195. , index_(index)
  196. , item_(item)
  197. , expandBox_(0)
  198. , textField_(0)
  199. , icon_(0)
  200. {
  201. SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  202. SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP);
  203. SetPaintOverflowFadeout(false);
  204. SetCapturing(false);
  205. item_->widget_ = this;
  206. for (int i = 0; i < item->depth_; i++)
  207. {
  208. LayoutParams lp;
  209. lp.SetWidth(6);
  210. lp.SetHeight(4);
  211. TBWidget* spacer = new TBWidget();
  212. spacer->SetLayoutParams(lp);
  213. GetContentRoot()->AddChild(spacer);
  214. }
  215. if (item_->children_.Size())
  216. {
  217. expandBox_ = new TBCheckBox();
  218. expandBox_->SetSkinBg(TBIDC("TBCheckBox.uilistview"));
  219. expandBox_->SetValue(item_->GetExpanded());
  220. expandBox_->SetID(item->id);
  221. GetContentRoot()->AddChild(expandBox_);
  222. }
  223. else
  224. {
  225. LayoutParams lp;
  226. lp.SetWidth(12);
  227. lp.SetHeight(4);
  228. TBWidget* spacer = new TBWidget();
  229. spacer->SetLayoutParams(lp);
  230. GetContentRoot()->AddChild(spacer);
  231. }
  232. if (item->icon_.Length())
  233. {
  234. icon_ = new TBSkinImage(TBIDC(item->icon_.CString()));
  235. icon_->SetIgnoreInput(true);
  236. GetContentRoot()->AddChild(icon_);
  237. }
  238. TBFontDescription fd;
  239. fd.SetID(TBIDC("Vera"));
  240. fd.SetSize(11);
  241. TBTextField* tfield = textField_ = new TBTextField();
  242. tfield->SetIgnoreInput(true);
  243. tfield->SetSkinBg(item->textSkin_.Length() ? TBIDC(item->textSkin_.CString()) : TBIDC("Folder"));
  244. tfield->SetText(item->str);
  245. tfield->SetFontDescription(fd);
  246. SetSkinBg(TBIDC("TBSelectItem"));
  247. GetContentRoot()->AddChild(tfield);
  248. SetID(item->id);
  249. }
  250. bool ListViewItemWidget::OnEvent(const TBWidgetEvent &ev)
  251. {
  252. if (ev.type == EVENT_TYPE_WHEEL)
  253. {
  254. return false;
  255. }
  256. if (ev.type == EVENT_TYPE_POINTER_DOWN || ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  257. {
  258. SetFocus(WIDGET_FOCUS_REASON_POINTER);
  259. TBWidget* parent = GetParent();
  260. while (parent)
  261. {
  262. if (parent->IsOfType<TBSelectList>())
  263. {
  264. TBWidgetEvent nev = ev;
  265. nev.ref_id = item_->id;
  266. parent->InvokeEvent(nev);
  267. break;
  268. }
  269. parent = parent->GetParent();
  270. }
  271. return true;
  272. }
  273. // get clicks this way, not sure we want to
  274. if (ev.type == EVENT_TYPE_CLICK && ev.target == expandBox_ && ev.target->GetID() == item_->id)
  275. {
  276. item_->SetExpanded(!item_->GetExpanded());
  277. source_->uiListView_->UpdateItemVisibility();
  278. // want to bubble
  279. return false;
  280. }
  281. return TBLayout::OnEvent(ev);
  282. }
  283. bool ListViewItemSource::Filter(int index, const char *filter)
  284. {
  285. ListViewItem* item = GetItem(index);
  286. if (!item->parent_)
  287. return true;
  288. if (item->parent_->GetExpanded())
  289. return true;
  290. return true;
  291. }
  292. TBWidget *ListViewItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  293. {
  294. ListViewItem* item = GetItem(index);
  295. if (TBLayout *layout = new ListViewItemWidget(item, this, viewer, index))
  296. return layout;
  297. return nullptr;
  298. }
  299. /*
  300. static int select_list_sort_cb(TBSelectItemSource *_source, const int *a, const int *b)
  301. {
  302. ListViewItemSource* source = (ListViewItemSource*) _source;
  303. ListViewItem* itemA = source->GetItem(*a);
  304. ListViewItem* itemB = source->GetItem(*b);
  305. int value;
  306. if (itemA->depth_ == itemB->depth_)
  307. {
  308. value = strcmp(source->GetItemString(*a), source->GetItemString(*b));
  309. }
  310. else
  311. {
  312. value = itemA->depth_ > itemB->depth_ ? 1 : -1;
  313. }
  314. return source->GetSort() == TB_SORT_DESCENDING ? -value : value;
  315. }
  316. */
  317. UIListView::UIListView(Context* context, bool createWidget) :
  318. UIWidget(context, createWidget),
  319. source_(0), itemLookupId_(0), multiSelect_(false)
  320. {
  321. rootList_ = new UISelectList(context);
  322. rootList_->SetUIListView(true);
  323. // dummy filter so filter is called
  324. rootList_->SetFilter(" ");
  325. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  326. rootList_->SetGravity(UI_GRAVITY_ALL);
  327. source_ = new ListViewItemSource(this);
  328. rootList_->GetTBSelectList()->SetSource(source_);
  329. AddChild(rootList_);
  330. }
  331. UIListView::~UIListView()
  332. {
  333. }
  334. unsigned UIListView::AddRootItem(const String& text, const String& icon, const String& id)
  335. {
  336. ListViewItem* item = new ListViewItem(text.CString(), TBID(id.CString()), icon.CString(), source_);
  337. source_->AddItem(item);
  338. itemLookup_[itemLookupId_++] = item;
  339. return itemLookupId_ - 1;
  340. }
  341. void UIListView::SetItemText(const String& id, const String& text)
  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->UpdateText(text);
  350. return;
  351. }
  352. }
  353. }
  354. void UIListView::SetItemTextSkin(const String& id, const String& skin)
  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->UpdateTextSkin(skin);
  363. return;
  364. }
  365. }
  366. }
  367. void UIListView::SetItemIcon(const String& id, const String& icon)
  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. item->UpdateIcon(icon);
  376. return;
  377. }
  378. }
  379. }
  380. void UIListView::DeleteItemByID(const String& id)
  381. {
  382. TBID tbid(id.CString());
  383. for (int i = 0; i < source_->GetNumItems(); i++)
  384. {
  385. if (source_->GetItemID(i) == tbid)
  386. {
  387. ListViewItem* item = source_->GetItem(i);
  388. if (item->parent_)
  389. item->parent_->children_.Remove(item);
  390. PODVector<ListViewItem*> children;
  391. item->GetChildren(children, true);
  392. for (unsigned j = 0; j < children.Size(); j++)
  393. {
  394. for (int k = 0; k < source_->GetNumItems(); k++)
  395. {
  396. if (children[j] == source_->GetItem(k))
  397. {
  398. source_->DeleteItem(k);
  399. break;
  400. }
  401. }
  402. }
  403. source_->DeleteItem(i);
  404. rootList_->InvalidateList();
  405. return;
  406. }
  407. }
  408. }
  409. unsigned UIListView::AddChildItem(unsigned parentItemID, const String& text, const String& icon, const String& id)
  410. {
  411. if (!itemLookup_.Contains(parentItemID))
  412. return -1;
  413. ListViewItem* item = itemLookup_[parentItemID];
  414. ListViewItem* child = item->AddChild(text.CString(), icon.CString(), TBID(id.CString()));
  415. itemLookup_[itemLookupId_++] = child;
  416. return itemLookupId_ - 1;
  417. }
  418. void UIListView::SetExpanded(unsigned itemID, bool value)
  419. {
  420. if (!itemLookup_.Contains(itemID))
  421. return;
  422. itemLookup_[itemID]->SetExpanded(value);
  423. }
  424. bool UIListView::GetExpanded(unsigned itemID)
  425. {
  426. if (!itemLookup_.Contains(itemID))
  427. return false;
  428. return itemLookup_[itemID]->GetExpanded();
  429. }
  430. bool UIListView::GetExpandable(unsigned itemID)
  431. {
  432. if (!itemLookup_.Contains(itemID))
  433. return false;
  434. return itemLookup_[itemID]->children_.Size() > 0;
  435. }
  436. void UIListView::DeleteAllItems()
  437. {
  438. itemLookup_.Clear();
  439. source_->DeleteAllItems();
  440. }
  441. void UIListView::SelectItemByID(const String& id, bool selected)
  442. {
  443. TBID tid = TBIDC(id.CString());
  444. for (int i = 0; i < source_->GetNumItems(); i++)
  445. {
  446. ListViewItem* item = source_->GetItem(i);
  447. if (tid == item->id)
  448. {
  449. if (selected)
  450. {
  451. if (item->GetSelected())
  452. return;
  453. item->SetSelected(selected);
  454. if (item->parent_)
  455. item->parent_->SetExpanded(true);
  456. SetValueFirstSelected();
  457. UpdateItemVisibility();
  458. ScrollToSelectedItem();
  459. }
  460. else
  461. {
  462. if (!item->GetSelected())
  463. return;
  464. item->SetSelected(false);
  465. UpdateItemVisibility();
  466. }
  467. return;
  468. }
  469. }
  470. }
  471. void UIListView::UpdateItemVisibility()
  472. {
  473. for (int i = 0; i < source_->GetNumItems(); i++)
  474. {
  475. ListViewItem* item = source_->GetItem(i);
  476. if (!item->widget_)
  477. continue;
  478. item->widget_->SetVisibilility(WIDGET_VISIBILITY_VISIBLE);
  479. item->widget_->SetState(WIDGET_STATE_SELECTED, item->GetSelected());
  480. ListViewItem* parent = item->parent_;
  481. while (parent)
  482. {
  483. if (!parent->GetExpanded())
  484. break;
  485. parent = parent->parent_;
  486. }
  487. if (parent)
  488. item->widget_->SetVisibilility(WIDGET_VISIBILITY_GONE);
  489. }
  490. tb::TBScrollContainer* scroll = (tb::TBScrollContainer*) rootList_->GetInternalWidget()->GetFirstChild();
  491. scroll->OnProcess();
  492. }
  493. void UIListView::ScrollToSelectedItem()
  494. {
  495. if (rootList_.Null())
  496. return;
  497. rootList_->ScrollToSelectedItem();
  498. }
  499. void UIListView::SelectAllItems(bool select)
  500. {
  501. for (int i = 0; i < source_->GetNumItems(); i++)
  502. {
  503. ListViewItem* item = source_->GetItem(i);
  504. item->SetSelected(select);
  505. }
  506. }
  507. void UIListView::SetValueFirstSelected()
  508. {
  509. int index = -1;
  510. for (int i = 0; i < source_->GetNumItems(); i++)
  511. {
  512. ListViewItem* item = source_->GetItem(i);
  513. if (item->GetSelected())
  514. {
  515. index = i;
  516. break;
  517. }
  518. }
  519. rootList_->SetValue(index);
  520. }
  521. void UIListView::SelectSingleItem(ListViewItem* item, bool expand)
  522. {
  523. bool dirty = !item->GetSelected();
  524. if (!dirty)
  525. {
  526. for (unsigned i = 0; i < source_->GetNumItems(); i++)
  527. {
  528. ListViewItem* sitem = source_->GetItem(i);
  529. if (sitem != item && sitem->GetSelected())
  530. {
  531. dirty = true;
  532. break;
  533. }
  534. }
  535. }
  536. if (!dirty)
  537. return;
  538. for (unsigned i = 0; i < source_->GetNumItems(); i++)
  539. {
  540. ListViewItem* sitem = source_->GetItem(i);
  541. if (sitem->GetSelected())
  542. {
  543. sitem->SetSelected(false);
  544. SendItemSelectedChanged(sitem);
  545. }
  546. }
  547. if (expand)
  548. item->SetExpanded(true);
  549. item->SetSelected(true);
  550. UpdateItemVisibility();
  551. SetValueFirstSelected();
  552. ScrollToSelectedItem();
  553. SendItemSelectedChanged(item);
  554. }
  555. void UIListView::Move(tb::SPECIAL_KEY key)
  556. {
  557. // selected index
  558. int index = -1;
  559. for (int i = 0; i < source_->GetNumItems(); i++)
  560. {
  561. ListViewItem* item = source_->GetItem(i);
  562. if (item->GetSelected())
  563. {
  564. index = i;
  565. break;
  566. }
  567. }
  568. // nothing selected
  569. if (index == -1)
  570. return;
  571. if (key == TB_KEY_LEFT)
  572. {
  573. ListViewItem* item = source_->GetItem(index);
  574. if (item->children_.Size() > 0 && item->GetExpanded())
  575. {
  576. item->SetExpanded(false);
  577. UpdateItemVisibility();
  578. }
  579. else
  580. {
  581. key = TB_KEY_UP;
  582. }
  583. }
  584. if (key == TB_KEY_RIGHT)
  585. {
  586. ListViewItem* item = source_->GetItem(index);
  587. if (item->children_.Size() > 0 && !item->GetExpanded())
  588. {
  589. item->SetExpanded(true);
  590. UpdateItemVisibility();
  591. }
  592. else
  593. {
  594. key = TB_KEY_DOWN;
  595. }
  596. }
  597. if (key == TB_KEY_UP)
  598. {
  599. // can't go any further up list
  600. if (index == 0)
  601. return;
  602. for (int i = (int) (index - 1 ); i >= 0; i--)
  603. {
  604. ListViewItem* item = source_->GetItem(i);
  605. if (item->widget_ && item->widget_->GetVisibility() == WIDGET_VISIBILITY_VISIBLE)
  606. {
  607. SelectSingleItem(item, false);
  608. return;
  609. }
  610. }
  611. }
  612. if (key == TB_KEY_DOWN)
  613. {
  614. // can't go any further down list
  615. if (index == source_->GetNumItems() - 1)
  616. return;
  617. for (int i = index + 1; i < source_->GetNumItems(); i++)
  618. {
  619. ListViewItem* item = source_->GetItem(i);
  620. if (item->widget_ && item->widget_->GetVisibility() == WIDGET_VISIBILITY_VISIBLE)
  621. {
  622. SelectSingleItem(item, false);
  623. return;
  624. }
  625. }
  626. }
  627. }
  628. void UIListView::SendItemSelectedChanged(ListViewItem* item)
  629. {
  630. UI* ui = GetSubsystem<UI>();
  631. VariantMap eventData;
  632. String refid;
  633. ui->GetTBIDString(item->id, refid);
  634. eventData[UIListViewSelectionChanged::P_REFID] = refid;
  635. eventData[UIListViewSelectionChanged::P_SELECTED] = item->GetSelected();
  636. this->SendEvent(E_UILISTVIEWSELECTIONCHANGED, eventData);
  637. }
  638. bool UIListView::OnEvent(const tb::TBWidgetEvent &ev)
  639. {
  640. if (ev.type == EVENT_TYPE_KEY_UP )
  641. {
  642. if (ev.special_key == TB_KEY_DOWN || ev.special_key == TB_KEY_UP || ev.special_key == TB_KEY_LEFT || ev.special_key == TB_KEY_RIGHT)
  643. {
  644. Move(ev.special_key);
  645. return true;
  646. }
  647. }
  648. if (ev.type == EVENT_TYPE_CUSTOM && ev.ref_id == TBIDC("select_list_validation_end"))
  649. {
  650. UpdateItemVisibility();
  651. return true;
  652. }
  653. if (ev.type == EVENT_TYPE_CUSTOM && ev.ref_id == TBIDC("select_list_selection_changed"))
  654. {
  655. for (int i = 0; i < source_->GetNumItems(); i++)
  656. {
  657. ListViewItem* item = source_->GetItem(i);
  658. if (item->id == ev.target->GetID())
  659. {
  660. bool multi = false;
  661. if (multiSelect_ && (ev.modifierkeys & TB_SHIFT || ev.modifierkeys & TB_CTRL || ev.modifierkeys & TB_SUPER))
  662. multi = true;
  663. if (multi)
  664. {
  665. if (item->GetSelected())
  666. {
  667. item->SetSelected(false);
  668. UpdateItemVisibility();
  669. SendItemSelectedChanged(item);
  670. }
  671. else
  672. {
  673. item->SetSelected(true);
  674. UpdateItemVisibility();
  675. SendItemSelectedChanged(item);
  676. }
  677. SetValueFirstSelected();
  678. }
  679. else
  680. {
  681. SelectSingleItem(item, false);
  682. }
  683. return true;
  684. }
  685. }
  686. }
  687. if (ev.type == EVENT_TYPE_SHORTCUT)
  688. {
  689. return false;
  690. }
  691. return UIWidget::OnEvent(ev);
  692. }
  693. }