UIListView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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. 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), moveDelta_(0.0f), pivot_(nullptr), pivotIndex_(0), startNewSelection_(true)
  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. if (!item)
  524. return;
  525. bool dirty = !item->GetSelected();
  526. if (!dirty)
  527. {
  528. for (unsigned i = 0; i < source_->GetNumItems(); i++)
  529. {
  530. ListViewItem* sitem = source_->GetItem(i);
  531. if (sitem != item && sitem->GetSelected())
  532. {
  533. dirty = true;
  534. break;
  535. }
  536. }
  537. }
  538. if (!dirty)
  539. return;
  540. for (unsigned i = 0; i < source_->GetNumItems(); i++)
  541. {
  542. ListViewItem* sitem = source_->GetItem(i);
  543. if (sitem->GetSelected())
  544. {
  545. sitem->SetSelected(false);
  546. SendItemSelectedChanged(sitem);
  547. }
  548. }
  549. if (expand)
  550. item->SetExpanded(true);
  551. item->SetSelected(true);
  552. UpdateItemVisibility();
  553. SetValueFirstSelected();
  554. ScrollToSelectedItem();
  555. SendItemSelectedChanged(item);
  556. }
  557. void UIListView::Move(tb::SPECIAL_KEY key)
  558. {
  559. const float delta = 0.015f;
  560. if (moveDelta_)
  561. {
  562. Time* time = GetSubsystem<Time>();
  563. moveDelta_ -= time->GetTimeStep();
  564. if (moveDelta_ < 0.0f)
  565. moveDelta_ = 0.0f;
  566. }
  567. if (moveDelta_ > 0.0f)
  568. return;
  569. // selected index
  570. int index = -1;
  571. for (int i = 0; i < source_->GetNumItems(); i++)
  572. {
  573. ListViewItem* item = source_->GetItem(i);
  574. if (item->GetSelected())
  575. {
  576. index = i;
  577. break;
  578. }
  579. }
  580. // nothing selected
  581. if (index == -1)
  582. return;
  583. if (key == TB_KEY_LEFT)
  584. {
  585. ListViewItem* item = source_->GetItem(index);
  586. if (item->children_.Size() > 0 && item->GetExpanded())
  587. {
  588. item->SetExpanded(false);
  589. UpdateItemVisibility();
  590. moveDelta_ = delta;
  591. return;
  592. }
  593. else
  594. {
  595. if (!item->parent_)
  596. return;
  597. SelectSingleItem(item->parent_, false);
  598. moveDelta_ = delta;
  599. return;
  600. }
  601. }
  602. if (key == TB_KEY_RIGHT)
  603. {
  604. ListViewItem* item = source_->GetItem(index);
  605. if (item->children_.Size() > 0 && !item->GetExpanded())
  606. {
  607. item->SetExpanded(true);
  608. UpdateItemVisibility();
  609. moveDelta_ = delta;
  610. return;
  611. }
  612. else
  613. {
  614. if (!item->children_.Size())
  615. return;
  616. SelectSingleItem(source_->GetItem(index + 1), false);
  617. moveDelta_ = delta;
  618. return;
  619. }
  620. }
  621. if (key == TB_KEY_UP)
  622. {
  623. // can't go any further up list
  624. if (index == 0)
  625. return;
  626. for (int i = (int) (index - 1 ); i >= 0; i--)
  627. {
  628. ListViewItem* item = source_->GetItem(i);
  629. if (item->widget_ && item->widget_->GetVisibility() == WIDGET_VISIBILITY_VISIBLE)
  630. {
  631. SelectSingleItem(item, false);
  632. moveDelta_ = delta;
  633. return;
  634. }
  635. }
  636. }
  637. if (key == TB_KEY_DOWN)
  638. {
  639. // can't go any further down list
  640. if (index == source_->GetNumItems() - 1)
  641. return;
  642. for (int i = index + 1; i < source_->GetNumItems(); i++)
  643. {
  644. ListViewItem* item = source_->GetItem(i);
  645. if (item->widget_ && item->widget_->GetVisibility() == WIDGET_VISIBILITY_VISIBLE)
  646. {
  647. SelectSingleItem(item, false);
  648. moveDelta_ = delta;
  649. return;
  650. }
  651. }
  652. }
  653. }
  654. void UIListView::SendItemSelectedChanged(ListViewItem* item)
  655. {
  656. UI* ui = GetSubsystem<UI>();
  657. VariantMap eventData;
  658. String refid;
  659. ui->GetTBIDString(item->id, refid);
  660. eventData[UIListViewSelectionChanged::P_REFID] = refid;
  661. eventData[UIListViewSelectionChanged::P_SELECTED] = item->GetSelected();
  662. this->SendEvent(E_UILISTVIEWSELECTIONCHANGED, eventData);
  663. }
  664. void UIListView::SelectItem(ListViewItem* item, bool select)
  665. {
  666. item->SetSelected(select);
  667. SendItemSelectedChanged(item);
  668. }
  669. bool UIListView::OnEvent(const tb::TBWidgetEvent &ev)
  670. {
  671. if (ev.type == EVENT_TYPE_KEY_UP )
  672. {
  673. moveDelta_ = 0.0f;
  674. }
  675. if (ev.type == EVENT_TYPE_KEY_DOWN )
  676. {
  677. 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)
  678. {
  679. Move(ev.special_key);
  680. return true;
  681. }
  682. }
  683. if (ev.type == EVENT_TYPE_CUSTOM && ev.ref_id == TBIDC("select_list_validation_end"))
  684. {
  685. UpdateItemVisibility();
  686. return true;
  687. }
  688. if (ev.type == EVENT_TYPE_CUSTOM && ev.ref_id == TBIDC("select_list_selection_changed"))
  689. {
  690. for (int i = 0; i < source_->GetNumItems(); i++)
  691. {
  692. ListViewItem* item = source_->GetItem(i);
  693. if (item->id == ev.target->GetID())
  694. {
  695. bool multi = false;
  696. if (multiSelect_ && (ev.modifierkeys & TB_CTRL || ev.modifierkeys & TB_SUPER))
  697. multi = true;
  698. bool shiftMulti = false;
  699. if (multiSelect_ && (ev.modifierkeys & TB_SHIFT))
  700. shiftMulti = true;
  701. if (shiftMulti)
  702. {
  703. if (startNewSelection_)
  704. SelectAllItems(false);
  705. if (!pivot_)
  706. {
  707. pivotIndex_ = 0;
  708. pivot_ = source_->GetItem(pivotIndex_);
  709. }
  710. SetValueFirstSelected();
  711. if (i > pivotIndex_)
  712. {
  713. for (int j = pivotIndex_; j < i; j++)
  714. {
  715. ListViewItem* itemSelect = source_->GetItem(j);
  716. if (!itemSelect->parent_ || itemSelect->parent_->GetExpanded())
  717. SelectItem(itemSelect, true);
  718. }
  719. }
  720. else if (i < pivotIndex_)
  721. {
  722. for (int j = pivotIndex_; j > i; j--)
  723. {
  724. ListViewItem* itemSelect = source_->GetItem(j);
  725. if (itemSelect->parent_->GetExpanded())
  726. SelectItem(itemSelect, true);
  727. }
  728. }
  729. SelectItem(item, true);
  730. UpdateItemVisibility();
  731. }
  732. else if (multi)
  733. {
  734. if (item->GetSelected())
  735. {
  736. SelectItem(item, false);
  737. }
  738. else
  739. {
  740. SelectItem(item, true);
  741. }
  742. SetValueFirstSelected();
  743. UpdateItemVisibility();
  744. pivot_ = item;
  745. pivotIndex_ = i;
  746. startNewSelection_ = false;
  747. }
  748. else
  749. {
  750. SelectSingleItem(item, false);
  751. pivot_ = item;
  752. pivotIndex_ = i;
  753. startNewSelection_ = true;
  754. }
  755. return true;
  756. }
  757. }
  758. }
  759. if (ev.type == EVENT_TYPE_SHORTCUT)
  760. {
  761. return false;
  762. }
  763. return UIWidget::OnEvent(ev);
  764. }
  765. }