UIListView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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. source_->uiListView_->UpdateItemVisibility();
  280. // want to bubble
  281. return false;
  282. }
  283. return TBLayout::OnEvent(ev);
  284. }
  285. bool ListViewItemSource::Filter(int index, const char *filter)
  286. {
  287. ListViewItem* item = GetItem(index);
  288. if (!item->parent_)
  289. return true;
  290. if (item->parent_->GetExpanded())
  291. return true;
  292. return true;
  293. }
  294. TBWidget *ListViewItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  295. {
  296. ListViewItem* item = GetItem(index);
  297. if (TBLayout *layout = new ListViewItemWidget(item, this, viewer, index))
  298. return layout;
  299. return nullptr;
  300. }
  301. /*
  302. static int select_list_sort_cb(TBSelectItemSource *_source, const int *a, const int *b)
  303. {
  304. ListViewItemSource* source = (ListViewItemSource*) _source;
  305. ListViewItem* itemA = source->GetItem(*a);
  306. ListViewItem* itemB = source->GetItem(*b);
  307. int value;
  308. if (itemA->depth_ == itemB->depth_)
  309. {
  310. value = strcmp(source->GetItemString(*a), source->GetItemString(*b));
  311. }
  312. else
  313. {
  314. value = itemA->depth_ > itemB->depth_ ? 1 : -1;
  315. }
  316. return source->GetSort() == TB_SORT_DESCENDING ? -value : value;
  317. }
  318. */
  319. UIListView::UIListView(Context* context, bool createWidget) :
  320. UIWidget(context, createWidget),
  321. source_(0), itemLookupId_(0), multiSelect_(false), moveDelta_(0.0f), pivot_(nullptr), pivotIndex_(0), startNewSelection_(true)
  322. {
  323. rootList_ = new UISelectList(context);
  324. rootList_->SetUIListView(true);
  325. // dummy filter so filter is called
  326. rootList_->SetFilter(" ");
  327. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  328. rootList_->SetGravity(UI_GRAVITY_ALL);
  329. source_ = new ListViewItemSource(this);
  330. rootList_->GetTBSelectList()->SetSource(source_);
  331. AddChild(rootList_);
  332. }
  333. UIListView::~UIListView()
  334. {
  335. }
  336. unsigned UIListView::AddRootItem(const String& text, const String& icon, const String& id)
  337. {
  338. ListViewItem* item = new ListViewItem(text.CString(), TBID(id.CString()), icon.CString(), source_);
  339. source_->AddItem(item);
  340. itemLookup_[itemLookupId_++] = item;
  341. return itemLookupId_ - 1;
  342. }
  343. void UIListView::SetItemText(const String& id, const String& text)
  344. {
  345. TBID tbid(id.CString());
  346. for (int i = 0; i < source_->GetNumItems(); i++)
  347. {
  348. if (source_->GetItemID(i) == tbid)
  349. {
  350. ListViewItem* item = source_->GetItem(i);
  351. item->UpdateText(text);
  352. return;
  353. }
  354. }
  355. }
  356. void UIListView::SetItemTextSkin(const String& id, const String& skin)
  357. {
  358. TBID tbid(id.CString());
  359. for (int i = 0; i < source_->GetNumItems(); i++)
  360. {
  361. if (source_->GetItemID(i) == tbid)
  362. {
  363. ListViewItem* item = source_->GetItem(i);
  364. item->UpdateTextSkin(skin);
  365. return;
  366. }
  367. }
  368. }
  369. void UIListView::SetItemIcon(const String& id, const String& icon)
  370. {
  371. TBID tbid(id.CString());
  372. for (int i = 0; i < source_->GetNumItems(); i++)
  373. {
  374. if (source_->GetItemID(i) == tbid)
  375. {
  376. ListViewItem* item = source_->GetItem(i);
  377. item->UpdateIcon(icon);
  378. return;
  379. }
  380. }
  381. }
  382. void UIListView::DeleteItemByID(const String& id)
  383. {
  384. TBID tbid(id.CString());
  385. for (int i = 0; i < source_->GetNumItems(); i++)
  386. {
  387. if (source_->GetItemID(i) == tbid)
  388. {
  389. ListViewItem* item = source_->GetItem(i);
  390. if (item->parent_)
  391. item->parent_->children_.Remove(item);
  392. PODVector<ListViewItem*> children;
  393. item->GetChildren(children, true);
  394. for (unsigned j = 0; j < children.Size(); j++)
  395. {
  396. for (int k = 0; k < source_->GetNumItems(); k++)
  397. {
  398. if (children[j] == source_->GetItem(k))
  399. {
  400. source_->DeleteItem(k);
  401. break;
  402. }
  403. }
  404. }
  405. source_->DeleteItem(i);
  406. rootList_->InvalidateList();
  407. return;
  408. }
  409. }
  410. }
  411. unsigned UIListView::AddChildItem(unsigned parentItemID, const String& text, const String& icon, const String& id)
  412. {
  413. if (!itemLookup_.Contains(parentItemID))
  414. return -1;
  415. ListViewItem* item = itemLookup_[parentItemID];
  416. ListViewItem* child = item->AddChild(text.CString(), icon.CString(), TBID(id.CString()));
  417. itemLookup_[itemLookupId_++] = child;
  418. return itemLookupId_ - 1;
  419. }
  420. void UIListView::SetExpanded(unsigned itemID, bool value)
  421. {
  422. if (!itemLookup_.Contains(itemID))
  423. return;
  424. itemLookup_[itemID]->SetExpanded(value);
  425. }
  426. bool UIListView::GetExpanded(unsigned itemID)
  427. {
  428. if (!itemLookup_.Contains(itemID))
  429. return false;
  430. return itemLookup_[itemID]->GetExpanded();
  431. }
  432. bool UIListView::GetExpandable(unsigned itemID)
  433. {
  434. if (!itemLookup_.Contains(itemID))
  435. return false;
  436. return itemLookup_[itemID]->children_.Size() > 0;
  437. }
  438. void UIListView::DeleteAllItems()
  439. {
  440. itemLookup_.Clear();
  441. source_->DeleteAllItems();
  442. }
  443. void UIListView::SelectItemByID(const String& id, bool selected)
  444. {
  445. TBID tid = TBIDC(id.CString());
  446. for (int i = 0; i < source_->GetNumItems(); i++)
  447. {
  448. ListViewItem* item = source_->GetItem(i);
  449. if (tid == item->id)
  450. {
  451. if (selected)
  452. {
  453. if (item->GetSelected())
  454. return;
  455. item->SetSelected(selected);
  456. if (item->parent_)
  457. item->parent_->SetExpanded(true);
  458. SetValueFirstSelected();
  459. UpdateItemVisibility();
  460. ScrollToSelectedItem();
  461. }
  462. else
  463. {
  464. if (!item->GetSelected())
  465. return;
  466. item->SetSelected(false);
  467. UpdateItemVisibility();
  468. }
  469. return;
  470. }
  471. }
  472. }
  473. void UIListView::UpdateItemVisibility()
  474. {
  475. for (int i = 0; i < source_->GetNumItems(); i++)
  476. {
  477. ListViewItem* item = source_->GetItem(i);
  478. if (!item->widget_)
  479. continue;
  480. item->widget_->SetVisibilility(WIDGET_VISIBILITY_VISIBLE);
  481. item->widget_->SetState(WIDGET_STATE_SELECTED, item->GetSelected());
  482. ListViewItem* parent = item->parent_;
  483. while (parent)
  484. {
  485. if (!parent->GetExpanded())
  486. break;
  487. parent = parent->parent_;
  488. }
  489. if (parent)
  490. item->widget_->SetVisibilility(WIDGET_VISIBILITY_GONE);
  491. }
  492. tb::TBScrollContainer* scroll = (tb::TBScrollContainer*) rootList_->GetInternalWidget()->GetFirstChild();
  493. scroll->OnProcess();
  494. }
  495. void UIListView::ScrollToSelectedItem()
  496. {
  497. if (rootList_.Null())
  498. return;
  499. rootList_->ScrollToSelectedItem();
  500. }
  501. void UIListView::SelectAllItems(bool select)
  502. {
  503. for (int i = 0; i < source_->GetNumItems(); i++)
  504. {
  505. ListViewItem* item = source_->GetItem(i);
  506. item->SetSelected(select);
  507. }
  508. }
  509. void UIListView::SetValueFirstSelected()
  510. {
  511. int index = -1;
  512. for (int i = 0; i < source_->GetNumItems(); i++)
  513. {
  514. ListViewItem* item = source_->GetItem(i);
  515. if (item->GetSelected())
  516. {
  517. index = i;
  518. break;
  519. }
  520. }
  521. rootList_->SetValue(index);
  522. }
  523. void UIListView::SelectSingleItem(ListViewItem* item, bool expand)
  524. {
  525. if (!item)
  526. return;
  527. bool dirty = !item->GetSelected();
  528. if (!dirty)
  529. {
  530. for (unsigned i = 0; i < source_->GetNumItems(); i++)
  531. {
  532. ListViewItem* sitem = source_->GetItem(i);
  533. if (sitem != item && sitem->GetSelected())
  534. {
  535. dirty = true;
  536. break;
  537. }
  538. }
  539. }
  540. if (!dirty)
  541. return;
  542. for (unsigned i = 0; i < source_->GetNumItems(); i++)
  543. {
  544. ListViewItem* sitem = source_->GetItem(i);
  545. if (sitem->GetSelected())
  546. {
  547. sitem->SetSelected(false);
  548. SendItemSelectedChanged(sitem);
  549. }
  550. }
  551. if (expand)
  552. item->SetExpanded(true);
  553. item->SetSelected(true);
  554. UpdateItemVisibility();
  555. SetValueFirstSelected();
  556. ScrollToSelectedItem();
  557. SendItemSelectedChanged(item);
  558. }
  559. void UIListView::Move(tb::SPECIAL_KEY key)
  560. {
  561. const float delta = 0.015f;
  562. if (moveDelta_)
  563. {
  564. Time* time = GetSubsystem<Time>();
  565. moveDelta_ -= time->GetTimeStep();
  566. if (moveDelta_ < 0.0f)
  567. moveDelta_ = 0.0f;
  568. }
  569. if (moveDelta_ > 0.0f)
  570. return;
  571. // selected index
  572. int index = -1;
  573. for (int i = 0; i < source_->GetNumItems(); i++)
  574. {
  575. ListViewItem* item = source_->GetItem(i);
  576. if (item->GetSelected())
  577. {
  578. index = i;
  579. break;
  580. }
  581. }
  582. // nothing selected
  583. if (index == -1)
  584. return;
  585. if (key == TB_KEY_LEFT)
  586. {
  587. ListViewItem* item = source_->GetItem(index);
  588. if (item->children_.Size() > 0 && item->GetExpanded())
  589. {
  590. item->SetExpanded(false);
  591. UpdateItemVisibility();
  592. moveDelta_ = delta;
  593. return;
  594. }
  595. else
  596. {
  597. if (!item->parent_)
  598. return;
  599. SelectSingleItem(item->parent_, false);
  600. moveDelta_ = delta;
  601. return;
  602. }
  603. }
  604. if (key == TB_KEY_RIGHT)
  605. {
  606. ListViewItem* item = source_->GetItem(index);
  607. if (item->children_.Size() > 0 && !item->GetExpanded())
  608. {
  609. item->SetExpanded(true);
  610. UpdateItemVisibility();
  611. moveDelta_ = delta;
  612. return;
  613. }
  614. else
  615. {
  616. if (!item->children_.Size())
  617. return;
  618. SelectSingleItem(source_->GetItem(index + 1), false);
  619. moveDelta_ = delta;
  620. return;
  621. }
  622. }
  623. if (key == TB_KEY_UP)
  624. {
  625. // can't go any further up list
  626. if (index == 0)
  627. return;
  628. for (int i = (int) (index - 1 ); i >= 0; i--)
  629. {
  630. ListViewItem* item = source_->GetItem(i);
  631. if (item->widget_ && item->widget_->GetVisibility() == WIDGET_VISIBILITY_VISIBLE)
  632. {
  633. SelectSingleItem(item, false);
  634. moveDelta_ = delta;
  635. return;
  636. }
  637. }
  638. }
  639. if (key == TB_KEY_DOWN)
  640. {
  641. // can't go any further down list
  642. if (index == source_->GetNumItems() - 1)
  643. return;
  644. for (int i = index + 1; i < source_->GetNumItems(); i++)
  645. {
  646. ListViewItem* item = source_->GetItem(i);
  647. if (item->widget_ && item->widget_->GetVisibility() == WIDGET_VISIBILITY_VISIBLE)
  648. {
  649. SelectSingleItem(item, false);
  650. moveDelta_ = delta;
  651. return;
  652. }
  653. }
  654. }
  655. }
  656. void UIListView::SendItemSelectedChanged(ListViewItem* item)
  657. {
  658. UI* ui = GetSubsystem<UI>();
  659. VariantMap eventData;
  660. String refid;
  661. ui->GetTBIDString(item->id, refid);
  662. eventData[UIListViewSelectionChanged::P_REFID] = refid;
  663. eventData[UIListViewSelectionChanged::P_SELECTED] = item->GetSelected();
  664. this->SendEvent(E_UILISTVIEWSELECTIONCHANGED, eventData);
  665. }
  666. void UIListView::SelectItem(ListViewItem* item, bool select)
  667. {
  668. item->SetSelected(select);
  669. SendItemSelectedChanged(item);
  670. }
  671. bool UIListView::OnEvent(const tb::TBWidgetEvent &ev)
  672. {
  673. if (ev.type == EVENT_TYPE_KEY_UP )
  674. {
  675. moveDelta_ = 0.0f;
  676. }
  677. if (ev.type == EVENT_TYPE_KEY_DOWN )
  678. {
  679. 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)
  680. {
  681. Move(ev.special_key);
  682. return true;
  683. }
  684. }
  685. if (ev.type == EVENT_TYPE_CUSTOM && ev.ref_id == TBIDC("select_list_validation_end"))
  686. {
  687. UpdateItemVisibility();
  688. return true;
  689. }
  690. if (ev.type == EVENT_TYPE_CUSTOM && ev.ref_id == TBIDC("select_list_selection_changed"))
  691. {
  692. for (int i = 0; i < source_->GetNumItems(); i++)
  693. {
  694. ListViewItem* item = source_->GetItem(i);
  695. if (item->id == ev.target->GetID())
  696. {
  697. bool multi = false;
  698. if (multiSelect_ && (ev.modifierkeys & TB_CTRL || ev.modifierkeys & TB_SUPER))
  699. multi = true;
  700. bool shiftMulti = false;
  701. if (multiSelect_ && (ev.modifierkeys & TB_SHIFT))
  702. shiftMulti = true;
  703. if (shiftMulti)
  704. {
  705. if (startNewSelection_)
  706. SelectAllItems(false);
  707. if (!pivot_)
  708. {
  709. pivotIndex_ = 0;
  710. pivot_ = source_->GetItem(pivotIndex_);
  711. }
  712. SetValueFirstSelected();
  713. if (i > pivotIndex_)
  714. {
  715. for (int j = pivotIndex_; j < i; j++)
  716. {
  717. ListViewItem* itemSelect = source_->GetItem(j);
  718. if (!itemSelect->parent_ || itemSelect->parent_->GetExpanded())
  719. SelectItem(itemSelect, true);
  720. }
  721. }
  722. else if (i < pivotIndex_)
  723. {
  724. for (int j = pivotIndex_; j > i; j--)
  725. {
  726. ListViewItem* itemSelect = source_->GetItem(j);
  727. if (itemSelect->parent_->GetExpanded())
  728. SelectItem(itemSelect, true);
  729. }
  730. }
  731. SelectItem(item, true);
  732. UpdateItemVisibility();
  733. }
  734. else if (multi)
  735. {
  736. if (item->GetSelected())
  737. {
  738. SelectItem(item, false);
  739. }
  740. else
  741. {
  742. SelectItem(item, true);
  743. }
  744. SetValueFirstSelected();
  745. UpdateItemVisibility();
  746. pivot_ = item;
  747. pivotIndex_ = i;
  748. startNewSelection_ = false;
  749. }
  750. else
  751. {
  752. SelectSingleItem(item, false);
  753. pivot_ = item;
  754. pivotIndex_ = i;
  755. startNewSelection_ = true;
  756. }
  757. return true;
  758. }
  759. }
  760. }
  761. if (ev.type == EVENT_TYPE_SHORTCUT)
  762. {
  763. return false;
  764. }
  765. return UIWidget::OnEvent(ev);
  766. }
  767. }