UIListView.cpp 24 KB

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