ListView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  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 "Precompiled.h"
  23. #include "BorderImage.h"
  24. #include "Context.h"
  25. #include "InputEvents.h"
  26. #include "ListView.h"
  27. #include "Log.h"
  28. #include "Sort.h"
  29. #include "StringUtils.h"
  30. #include "UIEvents.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. static const ShortStringHash indentHash("Indent");
  35. static const char* highlightModes[] =
  36. {
  37. "Never",
  38. "Focus",
  39. "Always",
  40. ""
  41. };
  42. template<> HighlightMode Variant::Get<HighlightMode>() const
  43. {
  44. return (HighlightMode)GetInt();
  45. }
  46. int GetItemIndent(UIElement* item)
  47. {
  48. if (!item)
  49. return 0;
  50. return item->GetVar(indentHash).GetInt();
  51. }
  52. OBJECTTYPESTATIC(ListView);
  53. ListView::ListView(Context* context) :
  54. ScrollView(context),
  55. highlightMode_(HM_FOCUS),
  56. multiselect_(false),
  57. hierarchyMode_(false),
  58. clearSelectionOnDefocus_(false),
  59. doubleClickInterval_(500),
  60. lastClickedItem_(M_MAX_UNSIGNED)
  61. {
  62. UIElement* container = new UIElement(context_);
  63. container->SetInternal(true);
  64. container->SetActive(true);
  65. container->SetLayout(LM_VERTICAL);
  66. container->SetSortChildren(false);
  67. SetContentElement(container);
  68. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(ListView, HandleUIMouseClick));
  69. SubscribeToEvent(E_FOCUSCHANGED, HANDLER(ListView, HandleFocusChanged));
  70. }
  71. ListView::~ListView()
  72. {
  73. }
  74. void ListView::RegisterObject(Context* context)
  75. {
  76. context->RegisterFactory<ListView>();
  77. ENUM_ACCESSOR_ATTRIBUTE(ListView, "Highlight Mode", GetHighlightMode, SetHighlightMode, HighlightMode, highlightModes, HM_FOCUS, AM_FILE);
  78. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Multiselect", GetMultiselect, SetMultiselect, bool, false, AM_FILE);
  79. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Hierarchy Mode", GetHierarchyMode, SetHierarchyMode, bool, false, AM_FILE);
  80. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Clear Sel. On Defocus", GetClearSelectionOnDefocus, SetClearSelectionOnDefocus, bool, false, AM_FILE);
  81. ACCESSOR_ATTRIBUTE(ListView, VAR_FLOAT, "Double Click Interval", GetDoubleClickInterval, SetDoubleClickInterval, float, 0.5f, AM_FILE);
  82. COPY_BASE_ATTRIBUTES(ListView, ScrollView);
  83. }
  84. void ListView::OnKey(int key, int buttons, int qualifiers)
  85. {
  86. // If no selection, can not move with keys
  87. unsigned numItems = GetNumItems();
  88. unsigned selection = GetSelection();
  89. // If either shift or ctrl held down, add to selection if multiselect enabled
  90. bool additive = multiselect_ && qualifiers & (QUAL_SHIFT | QUAL_CTRL);
  91. int delta = 0;
  92. int pageDirection = 1;
  93. if (selection != M_MAX_UNSIGNED && numItems)
  94. {
  95. switch (key)
  96. {
  97. case KEY_LEFT:
  98. case KEY_RIGHT:
  99. if (hierarchyMode_)
  100. {
  101. SetChildItemsVisible(selection, key == KEY_RIGHT);
  102. return;
  103. }
  104. break;
  105. case KEY_RETURN:
  106. case KEY_RETURN2:
  107. case KEY_KP_ENTER:
  108. if (hierarchyMode_)
  109. {
  110. ToggleChildItemsVisible(selection);
  111. return;
  112. }
  113. break;
  114. case KEY_UP:
  115. delta = -1;
  116. break;
  117. case KEY_DOWN:
  118. delta = 1;
  119. break;
  120. case KEY_PAGEUP:
  121. pageDirection = -1;
  122. // Fallthru
  123. case KEY_PAGEDOWN:
  124. {
  125. // Convert page step to pixels and see how many items have to be skipped to reach that many pixels
  126. int stepPixels = ((int)(pageStep_ * scrollPanel_->GetHeight())) - GetSelectedItem()->GetHeight();
  127. unsigned newSelection = selection;
  128. unsigned okSelection = selection;
  129. unsigned invisible = 0;
  130. while (newSelection < numItems)
  131. {
  132. UIElement* item = GetItem(newSelection);
  133. int height = 0;
  134. if (item->IsVisible())
  135. {
  136. height = item->GetHeight();
  137. okSelection = newSelection;
  138. }
  139. else
  140. ++invisible;
  141. if (stepPixels < height)
  142. break;
  143. stepPixels -= height;
  144. newSelection += pageDirection;
  145. }
  146. delta = okSelection - selection - pageDirection * invisible;
  147. }
  148. break;
  149. case KEY_HOME:
  150. delta = -(int)GetNumItems();
  151. break;
  152. case KEY_END:
  153. delta = GetNumItems();
  154. break;
  155. }
  156. }
  157. if (delta)
  158. {
  159. ChangeSelection(delta, additive);
  160. return;
  161. }
  162. using namespace UnhandledKey;
  163. VariantMap eventData;
  164. eventData[P_ELEMENT] = (void*)this;
  165. eventData[P_KEY] = key;
  166. eventData[P_BUTTONS] = buttons;
  167. eventData[P_QUALIFIERS] = qualifiers;
  168. SendEvent(E_UNHANDLEDKEY, eventData);
  169. }
  170. void ListView::OnResize()
  171. {
  172. ScrollView::OnResize();
  173. // Set the content element width to match the scrollpanel minus clipping
  174. IntRect panelBorder = scrollPanel_->GetClipBorder();
  175. contentElement_->SetWidth(scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_);
  176. // If scrollbar autovisibility is enabled, need one final pass to correct the view
  177. /// \todo Rework, this is inefficient
  178. if (scrollBarsAutoVisible_)
  179. ScrollView::OnResize();
  180. }
  181. void ListView::AddItem(UIElement* item)
  182. {
  183. InsertItem(contentElement_->GetNumChildren(), item);
  184. }
  185. void ListView::InsertItem(unsigned index, UIElement* item)
  186. {
  187. if (!item || item->GetParent() == contentElement_)
  188. return;
  189. // Enable input so that clicking the item can be detected
  190. item->SetActive(true);
  191. item->SetSelected(false);
  192. contentElement_->InsertChild(index, item);
  193. // If necessary, shift the following selections
  194. if (!selections_.Empty())
  195. {
  196. for (unsigned i = 0; i < selections_.Size(); ++i)
  197. {
  198. if (selections_[i] >= index)
  199. ++selections_[i];
  200. }
  201. UpdateSelectionEffect();
  202. }
  203. }
  204. void ListView::RemoveItem(UIElement* item, unsigned index)
  205. {
  206. unsigned numItems = GetNumItems();
  207. for (unsigned i = index; i < numItems; ++i)
  208. {
  209. if (GetItem(i) == item)
  210. {
  211. item->SetSelected(false);
  212. selections_.Remove(i);
  213. // Remove any child items in hierarchy mode
  214. unsigned removed = 1;
  215. if (hierarchyMode_)
  216. {
  217. int baseIndent = GetItemIndent(item);
  218. for (unsigned j = i + 1; ; ++j)
  219. {
  220. UIElement* childItem = GetItem(i + 1);
  221. if (!childItem)
  222. break;
  223. if (GetItemIndent(childItem) > baseIndent)
  224. {
  225. childItem->SetSelected(false);
  226. selections_.Erase(j);
  227. contentElement_->RemoveChild(childItem, i + 1);
  228. ++removed;
  229. }
  230. else
  231. break;
  232. }
  233. }
  234. // If necessary, shift the following selections
  235. if (!selections_.Empty())
  236. {
  237. for (unsigned j = 0; j < selections_.Size(); ++j)
  238. {
  239. if (selections_[j] > i)
  240. selections_[j] -= removed;
  241. }
  242. UpdateSelectionEffect();
  243. }
  244. contentElement_->RemoveChild(item, i);
  245. break;
  246. }
  247. }
  248. }
  249. void ListView::RemoveItem(unsigned index)
  250. {
  251. RemoveItem(GetItem(index), index);
  252. }
  253. void ListView::RemoveAllItems()
  254. {
  255. contentElement_->DisableLayoutUpdate();
  256. ClearSelection();
  257. contentElement_->RemoveAllChildren();
  258. contentElement_->EnableLayoutUpdate();
  259. contentElement_->UpdateLayout();
  260. }
  261. void ListView::SetSelection(unsigned index)
  262. {
  263. PODVector<unsigned> indices;
  264. indices.Push(index);
  265. SetSelections(indices);
  266. EnsureItemVisibility(index);
  267. }
  268. void ListView::SetSelections(const PODVector<unsigned>& indices)
  269. {
  270. unsigned numItems = GetNumItems();
  271. // Remove first items that should no longer be selected
  272. for (PODVector<unsigned>::Iterator i = selections_.Begin(); i != selections_.End();)
  273. {
  274. unsigned index = *i;
  275. if (!indices.Contains(index))
  276. {
  277. i = selections_.Erase(i);
  278. using namespace ItemSelected;
  279. VariantMap eventData;
  280. eventData[P_ELEMENT] = (void*)this;
  281. eventData[P_SELECTION] = index;
  282. SendEvent(E_ITEMDESELECTED, eventData);
  283. }
  284. else
  285. ++i;
  286. }
  287. bool added = false;
  288. // Then add missing items
  289. for (PODVector<unsigned>::ConstIterator i = indices.Begin(); i != indices.End(); ++i)
  290. {
  291. unsigned index = *i;
  292. if (index < numItems)
  293. {
  294. // In singleselect mode, resend the event even for the same selection
  295. bool duplicate = selections_.Contains(index);
  296. if (!duplicate || !multiselect_)
  297. {
  298. if (!duplicate)
  299. {
  300. selections_.Push(index);
  301. added = true;
  302. }
  303. using namespace ItemSelected;
  304. VariantMap eventData;
  305. eventData[P_ELEMENT] = (void*)this;
  306. eventData[P_SELECTION] = *i;
  307. SendEvent(E_ITEMSELECTED, eventData);
  308. }
  309. }
  310. // If no multiselect enabled, allow setting only one item
  311. if (!multiselect_)
  312. break;
  313. }
  314. // Re-sort selections if necessary
  315. if (added)
  316. Sort(selections_.Begin(), selections_.End());
  317. UpdateSelectionEffect();
  318. SendEvent(E_SELECTIONCHANGED);
  319. }
  320. void ListView::AddSelection(unsigned index)
  321. {
  322. if (!multiselect_)
  323. SetSelection(index);
  324. else
  325. {
  326. if (index >= GetNumItems())
  327. return;
  328. if (!selections_.Contains(index))
  329. {
  330. selections_.Push(index);
  331. using namespace ItemSelected;
  332. VariantMap eventData;
  333. eventData[P_ELEMENT] = (void*)this;
  334. eventData[P_SELECTION] = index;
  335. SendEvent(E_ITEMSELECTED, eventData);
  336. Sort(selections_.Begin(), selections_.End());
  337. }
  338. EnsureItemVisibility(index);
  339. UpdateSelectionEffect();
  340. SendEvent(E_SELECTIONCHANGED);
  341. }
  342. }
  343. void ListView::RemoveSelection(unsigned index)
  344. {
  345. if (index >= GetNumItems())
  346. return;
  347. if (selections_.Remove(index))
  348. {
  349. using namespace ItemSelected;
  350. VariantMap eventData;
  351. eventData[P_ELEMENT] = (void*)this;
  352. eventData[P_SELECTION] = index;
  353. SendEvent(E_ITEMDESELECTED, eventData);
  354. }
  355. EnsureItemVisibility(index);
  356. UpdateSelectionEffect();
  357. SendEvent(E_SELECTIONCHANGED);
  358. }
  359. void ListView::ToggleSelection(unsigned index)
  360. {
  361. unsigned numItems = GetNumItems();
  362. if (index >= numItems)
  363. return;
  364. if (selections_.Contains(index))
  365. RemoveSelection(index);
  366. else
  367. AddSelection(index);
  368. }
  369. void ListView::ChangeSelection(int delta, bool additive)
  370. {
  371. if (selections_.Empty())
  372. return;
  373. if (!multiselect_)
  374. additive = false;
  375. // If going downwards, use the last selection as a base. Otherwise use first
  376. unsigned selection = delta > 0 ? selections_.Back() : selections_.Front();
  377. int direction = delta > 0 ? 1 : -1;
  378. unsigned numItems = GetNumItems();
  379. unsigned newSelection = selection;
  380. unsigned okSelection = selection;
  381. PODVector<unsigned> indices = selections_;
  382. while (delta != 0)
  383. {
  384. newSelection += direction;
  385. if (newSelection >= numItems)
  386. break;
  387. UIElement* item = GetItem(newSelection);
  388. if (item->IsVisible())
  389. {
  390. indices.Push(okSelection = newSelection);
  391. delta -= direction;
  392. }
  393. }
  394. if (!additive)
  395. SetSelection(okSelection);
  396. else
  397. SetSelections(indices);
  398. }
  399. void ListView::ClearSelection()
  400. {
  401. SetSelections(PODVector<unsigned>());
  402. }
  403. void ListView::SetHighlightMode(HighlightMode mode)
  404. {
  405. highlightMode_ = mode;
  406. UpdateSelectionEffect();
  407. }
  408. void ListView::SetMultiselect(bool enable)
  409. {
  410. multiselect_ = enable;
  411. }
  412. void ListView::SetHierarchyMode(bool enable)
  413. {
  414. hierarchyMode_ = enable;
  415. }
  416. void ListView::SetClearSelectionOnDefocus(bool enable)
  417. {
  418. if (enable != clearSelectionOnDefocus_)
  419. {
  420. clearSelectionOnDefocus_ = enable;
  421. if (clearSelectionOnDefocus_)
  422. {
  423. SubscribeToEvent(this, E_DEFOCUSED, HANDLER(ListView, HandleDefocused));
  424. if (!HasFocus())
  425. ClearSelection();
  426. }
  427. else
  428. UnsubscribeFromEvent(this, E_DEFOCUSED);
  429. }
  430. }
  431. void ListView::SetDoubleClickInterval(float interval)
  432. {
  433. doubleClickInterval_ = Max((int)(interval * 1000.0f), 0);
  434. }
  435. void ListView::SetChildItemsVisible(unsigned index, bool enable)
  436. {
  437. unsigned numItems = GetNumItems();
  438. if (!hierarchyMode_ || index >= numItems)
  439. return;
  440. contentElement_->DisableLayoutUpdate();
  441. int baseIndent = GetItemIndent(GetItem(index));
  442. for (unsigned i = index + 1; i < numItems; ++i)
  443. {
  444. UIElement* item = GetItem(i);
  445. if (GetItemIndent(item) > baseIndent)
  446. item->SetVisible(enable);
  447. else
  448. break;
  449. }
  450. contentElement_->EnableLayoutUpdate();
  451. contentElement_->UpdateLayout();
  452. }
  453. void ListView::SetChildItemsVisible(bool enable)
  454. {
  455. unsigned numItems = GetNumItems();
  456. for (unsigned i = 0; i < numItems; ++i)
  457. {
  458. if (!GetItemIndent(GetItem(i)))
  459. SetChildItemsVisible(i, enable);
  460. }
  461. unsigned firstSelected = GetSelection();
  462. if (firstSelected != M_MAX_UNSIGNED)
  463. EnsureItemVisibility(firstSelected);
  464. }
  465. void ListView::ToggleChildItemsVisible(unsigned index)
  466. {
  467. unsigned numItems = GetNumItems();
  468. if (!hierarchyMode_ || index >= numItems)
  469. return;
  470. contentElement_->DisableLayoutUpdate();
  471. int baseIndent = GetItemIndent(GetItem(index));
  472. bool firstChild = true;
  473. bool firstChildVisible;
  474. for (unsigned i = index + 1; i < numItems; ++i)
  475. {
  476. UIElement* item = GetItem(i);
  477. if (GetItemIndent(item) > baseIndent)
  478. {
  479. if (firstChild)
  480. {
  481. item->SetVisible(firstChildVisible = !item->IsVisible());
  482. firstChild = false;
  483. }
  484. else
  485. item->SetVisible(firstChildVisible);
  486. }
  487. else
  488. break;
  489. }
  490. contentElement_->EnableLayoutUpdate();
  491. contentElement_->UpdateLayout();
  492. }
  493. unsigned ListView::GetNumItems() const
  494. {
  495. return contentElement_->GetNumChildren();
  496. }
  497. UIElement* ListView::GetItem(unsigned index) const
  498. {
  499. return contentElement_->GetChild(index);
  500. }
  501. PODVector<UIElement*> ListView::GetItems() const
  502. {
  503. PODVector<UIElement*> items;
  504. contentElement_->GetChildren(items);
  505. return items;
  506. }
  507. unsigned ListView::GetSelection() const
  508. {
  509. if (selections_.Empty())
  510. return M_MAX_UNSIGNED;
  511. else
  512. return GetSelections().Front();
  513. }
  514. UIElement* ListView::GetSelectedItem() const
  515. {
  516. return contentElement_->GetChild(GetSelection());
  517. }
  518. PODVector<UIElement*> ListView::GetSelectedItems() const
  519. {
  520. PODVector<UIElement*> ret;
  521. for (PODVector<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
  522. {
  523. UIElement* item = GetItem(*i);
  524. if (item)
  525. ret.Push(item);
  526. }
  527. return ret;
  528. }
  529. bool ListView::IsSelected(unsigned index) const
  530. {
  531. return selections_.Contains(index);
  532. }
  533. float ListView::GetDoubleClickInterval() const
  534. {
  535. return (float)doubleClickInterval_ / 1000.0f;
  536. }
  537. void ListView::UpdateSelectionEffect()
  538. {
  539. unsigned numItems = GetNumItems();
  540. bool highlighted = highlightMode_ == HM_ALWAYS || HasFocus();
  541. for (unsigned i = 0; i < numItems; ++i)
  542. {
  543. UIElement* item = GetItem(i);
  544. if (highlightMode_ != HM_NEVER && selections_.Contains(i))
  545. item->SetSelected(highlighted);
  546. else
  547. item->SetSelected(false);
  548. }
  549. }
  550. void ListView::EnsureItemVisibility(unsigned index)
  551. {
  552. EnsureItemVisibility(GetItem(index));
  553. }
  554. void ListView::EnsureItemVisibility(UIElement* item)
  555. {
  556. if (!item || !item->IsVisible())
  557. return;
  558. IntVector2 newView = GetViewPosition();
  559. IntVector2 currentOffset = item->GetPosition() - newView;
  560. const IntRect& clipBorder = scrollPanel_->GetClipBorder();
  561. IntVector2 windowSize(scrollPanel_->GetWidth() - clipBorder.left_ - clipBorder.right_, scrollPanel_->GetHeight() -
  562. clipBorder.top_ - clipBorder.bottom_);
  563. if (currentOffset.y_ < 0)
  564. newView.y_ += currentOffset.y_;
  565. if (currentOffset.y_ + item->GetHeight() > windowSize.y_)
  566. newView.y_ += currentOffset.y_ + item->GetHeight() - windowSize.y_;
  567. SetViewPosition(newView);
  568. }
  569. void ListView::HandleUIMouseClick(StringHash eventType, VariantMap& eventData)
  570. {
  571. if (eventData[UIMouseClick::P_BUTTON].GetInt() != MOUSEB_LEFT)
  572. return;
  573. int qualifiers = eventData[UIMouseClick::P_QUALIFIERS].GetInt();
  574. UIElement* element = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  575. unsigned numItems = GetNumItems();
  576. for (unsigned i = 0; i < numItems; ++i)
  577. {
  578. if (element == GetItem(i))
  579. {
  580. // Check doubleclick
  581. bool isDoubleClick = false;
  582. if (!multiselect_ || !qualifiers)
  583. {
  584. if (!(isDoubleClick = doubleClickTimer_.GetMSec(true) < doubleClickInterval_ && lastClickedItem_ == i))
  585. lastClickedItem_ = i;
  586. SetSelection(i);
  587. }
  588. // Check multiselect with shift & ctrl
  589. if (multiselect_)
  590. {
  591. if (qualifiers & QUAL_SHIFT)
  592. {
  593. if (selections_.Empty())
  594. SetSelection(i);
  595. else
  596. {
  597. unsigned first = selections_.Front();
  598. unsigned last = selections_.Back();
  599. PODVector<unsigned> newSelections = selections_;
  600. if (i == first || i == last)
  601. {
  602. for (unsigned j = first; j <= last; ++j)
  603. newSelections.Push(j);
  604. }
  605. else if (i < first)
  606. {
  607. for (unsigned j = i; j <= first; ++j)
  608. newSelections.Push(j);
  609. }
  610. else if (i < last)
  611. {
  612. if ((abs((int)i - (int)first)) <= (abs((int)i - (int)last)))
  613. {
  614. for (unsigned j = first; j <= i; ++j)
  615. newSelections.Push(j);
  616. }
  617. else
  618. {
  619. for (unsigned j = i; j <= last; ++j)
  620. newSelections.Push(j);
  621. }
  622. }
  623. else if (i > last)
  624. {
  625. for (unsigned j = last; j <= i; ++j)
  626. newSelections.Push(j);
  627. }
  628. SetSelections(newSelections);
  629. }
  630. }
  631. else if (qualifiers & QUAL_CTRL)
  632. ToggleSelection(i);
  633. }
  634. if (isDoubleClick)
  635. {
  636. VariantMap eventData;
  637. eventData[ItemDoubleClicked::P_ELEMENT] = (void*)this;
  638. eventData[ItemDoubleClicked::P_SELECTION] = i;
  639. SendEvent(E_ITEMDOUBLECLICKED, eventData);
  640. }
  641. return;
  642. }
  643. }
  644. }
  645. void ListView::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  646. {
  647. using namespace FocusChanged;
  648. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  649. while (element)
  650. {
  651. // If the focused element or its parent is in the list, scroll the list to make the item visible
  652. UIElement* parent = element->GetParent();
  653. if (parent == contentElement_)
  654. {
  655. EnsureItemVisibility(element);
  656. return;
  657. }
  658. element = parent;
  659. }
  660. }
  661. void ListView::HandleDefocused(StringHash eventType, VariantMap& eventData)
  662. {
  663. ClearSelection();
  664. }
  665. }