UIListView.cpp 21 KB

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