ListView.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. }
  177. void ListView::AddItem(UIElement* item)
  178. {
  179. InsertItem(contentElement_->GetNumChildren(), item);
  180. }
  181. void ListView::InsertItem(unsigned index, UIElement* item)
  182. {
  183. if (!item || item->GetParent() == contentElement_)
  184. return;
  185. // Enable input so that clicking the item can be detected
  186. item->SetActive(true);
  187. item->SetSelected(false);
  188. contentElement_->InsertChild(index, item);
  189. // If necessary, shift the following selections
  190. if (!selections_.Empty())
  191. {
  192. for (unsigned i = 0; i < selections_.Size(); ++i)
  193. {
  194. if (selections_[i] >= index)
  195. ++selections_[i];
  196. }
  197. UpdateSelectionEffect();
  198. }
  199. }
  200. void ListView::RemoveItem(UIElement* item, unsigned index)
  201. {
  202. unsigned numItems = GetNumItems();
  203. for (unsigned i = index; i < numItems; ++i)
  204. {
  205. if (GetItem(i) == item)
  206. {
  207. item->SetSelected(false);
  208. selections_.Remove(i);
  209. // Remove any child items in hierarchy mode
  210. unsigned removed = 1;
  211. if (hierarchyMode_)
  212. {
  213. int baseIndent = GetItemIndent(item);
  214. for (unsigned j = i + 1; ; ++j)
  215. {
  216. UIElement* childItem = GetItem(i + 1);
  217. if (!childItem)
  218. break;
  219. if (GetItemIndent(childItem) > baseIndent)
  220. {
  221. childItem->SetSelected(false);
  222. selections_.Erase(j);
  223. contentElement_->RemoveChild(childItem, i + 1);
  224. ++removed;
  225. }
  226. else
  227. break;
  228. }
  229. }
  230. // If necessary, shift the following selections
  231. if (!selections_.Empty())
  232. {
  233. for (unsigned j = 0; j < selections_.Size(); ++j)
  234. {
  235. if (selections_[j] > i)
  236. selections_[j] -= removed;
  237. }
  238. UpdateSelectionEffect();
  239. }
  240. contentElement_->RemoveChild(item, i);
  241. break;
  242. }
  243. }
  244. }
  245. void ListView::RemoveItem(unsigned index)
  246. {
  247. RemoveItem(GetItem(index), index);
  248. }
  249. void ListView::RemoveAllItems()
  250. {
  251. contentElement_->DisableLayoutUpdate();
  252. ClearSelection();
  253. contentElement_->RemoveAllChildren();
  254. contentElement_->EnableLayoutUpdate();
  255. contentElement_->UpdateLayout();
  256. }
  257. void ListView::SetSelection(unsigned index)
  258. {
  259. PODVector<unsigned> indices;
  260. indices.Push(index);
  261. SetSelections(indices);
  262. EnsureItemVisibility(index);
  263. }
  264. void ListView::SetSelections(const PODVector<unsigned>& indices)
  265. {
  266. unsigned numItems = GetNumItems();
  267. // Remove first items that should no longer be selected
  268. for (PODVector<unsigned>::Iterator i = selections_.Begin(); i != selections_.End();)
  269. {
  270. unsigned index = *i;
  271. if (!indices.Contains(index))
  272. {
  273. i = selections_.Erase(i);
  274. using namespace ItemSelected;
  275. VariantMap eventData;
  276. eventData[P_ELEMENT] = (void*)this;
  277. eventData[P_SELECTION] = index;
  278. SendEvent(E_ITEMDESELECTED, eventData);
  279. }
  280. else
  281. ++i;
  282. }
  283. bool added = false;
  284. // Then add missing items
  285. for (PODVector<unsigned>::ConstIterator i = indices.Begin(); i != indices.End(); ++i)
  286. {
  287. unsigned index = *i;
  288. if (index < numItems)
  289. {
  290. // In singleselect mode, resend the event even for the same selection
  291. bool duplicate = selections_.Contains(index);
  292. if (!duplicate || !multiselect_)
  293. {
  294. if (!duplicate)
  295. {
  296. selections_.Push(index);
  297. added = true;
  298. }
  299. using namespace ItemSelected;
  300. VariantMap eventData;
  301. eventData[P_ELEMENT] = (void*)this;
  302. eventData[P_SELECTION] = *i;
  303. SendEvent(E_ITEMSELECTED, eventData);
  304. }
  305. }
  306. // If no multiselect enabled, allow setting only one item
  307. if (!multiselect_)
  308. break;
  309. }
  310. // Re-sort selections if necessary
  311. if (added)
  312. Sort(selections_.Begin(), selections_.End());
  313. UpdateSelectionEffect();
  314. SendEvent(E_SELECTIONCHANGED);
  315. }
  316. void ListView::AddSelection(unsigned index)
  317. {
  318. if (!multiselect_)
  319. SetSelection(index);
  320. else
  321. {
  322. if (index >= GetNumItems())
  323. return;
  324. if (!selections_.Contains(index))
  325. {
  326. selections_.Push(index);
  327. using namespace ItemSelected;
  328. VariantMap eventData;
  329. eventData[P_ELEMENT] = (void*)this;
  330. eventData[P_SELECTION] = index;
  331. SendEvent(E_ITEMSELECTED, eventData);
  332. Sort(selections_.Begin(), selections_.End());
  333. }
  334. EnsureItemVisibility(index);
  335. UpdateSelectionEffect();
  336. SendEvent(E_SELECTIONCHANGED);
  337. }
  338. }
  339. void ListView::RemoveSelection(unsigned index)
  340. {
  341. if (index >= GetNumItems())
  342. return;
  343. if (selections_.Remove(index))
  344. {
  345. using namespace ItemSelected;
  346. VariantMap eventData;
  347. eventData[P_ELEMENT] = (void*)this;
  348. eventData[P_SELECTION] = index;
  349. SendEvent(E_ITEMDESELECTED, eventData);
  350. }
  351. EnsureItemVisibility(index);
  352. UpdateSelectionEffect();
  353. SendEvent(E_SELECTIONCHANGED);
  354. }
  355. void ListView::ToggleSelection(unsigned index)
  356. {
  357. unsigned numItems = GetNumItems();
  358. if (index >= numItems)
  359. return;
  360. if (selections_.Contains(index))
  361. RemoveSelection(index);
  362. else
  363. AddSelection(index);
  364. }
  365. void ListView::ChangeSelection(int delta, bool additive)
  366. {
  367. if (selections_.Empty())
  368. return;
  369. if (!multiselect_)
  370. additive = false;
  371. // If going downwards, use the last selection as a base. Otherwise use first
  372. unsigned selection = delta > 0 ? selections_.Back() : selections_.Front();
  373. int direction = delta > 0 ? 1 : -1;
  374. unsigned numItems = GetNumItems();
  375. unsigned newSelection = selection;
  376. unsigned okSelection = selection;
  377. PODVector<unsigned> indices = selections_;
  378. while (delta != 0)
  379. {
  380. newSelection += direction;
  381. if (newSelection >= numItems)
  382. break;
  383. UIElement* item = GetItem(newSelection);
  384. if (item->IsVisible())
  385. {
  386. indices.Push(okSelection = newSelection);
  387. delta -= direction;
  388. }
  389. }
  390. if (!additive)
  391. SetSelection(okSelection);
  392. else
  393. SetSelections(indices);
  394. }
  395. void ListView::ClearSelection()
  396. {
  397. SetSelections(PODVector<unsigned>());
  398. }
  399. void ListView::SetHighlightMode(HighlightMode mode)
  400. {
  401. highlightMode_ = mode;
  402. UpdateSelectionEffect();
  403. }
  404. void ListView::SetMultiselect(bool enable)
  405. {
  406. multiselect_ = enable;
  407. }
  408. void ListView::SetHierarchyMode(bool enable)
  409. {
  410. hierarchyMode_ = enable;
  411. }
  412. void ListView::SetClearSelectionOnDefocus(bool enable)
  413. {
  414. if (enable != clearSelectionOnDefocus_)
  415. {
  416. clearSelectionOnDefocus_ = enable;
  417. if (clearSelectionOnDefocus_)
  418. {
  419. SubscribeToEvent(this, E_DEFOCUSED, HANDLER(ListView, HandleDefocused));
  420. if (!HasFocus())
  421. ClearSelection();
  422. }
  423. else
  424. UnsubscribeFromEvent(this, E_DEFOCUSED);
  425. }
  426. }
  427. void ListView::SetDoubleClickInterval(float interval)
  428. {
  429. doubleClickInterval_ = Max((int)(interval * 1000.0f), 0);
  430. }
  431. void ListView::SetChildItemsVisible(unsigned index, bool enable)
  432. {
  433. unsigned numItems = GetNumItems();
  434. if (!hierarchyMode_ || index >= numItems)
  435. return;
  436. contentElement_->DisableLayoutUpdate();
  437. int baseIndent = GetItemIndent(GetItem(index));
  438. for (unsigned i = index + 1; i < numItems; ++i)
  439. {
  440. UIElement* item = GetItem(i);
  441. if (GetItemIndent(item) > baseIndent)
  442. item->SetVisible(enable);
  443. else
  444. break;
  445. }
  446. contentElement_->EnableLayoutUpdate();
  447. contentElement_->UpdateLayout();
  448. }
  449. void ListView::SetChildItemsVisible(bool enable)
  450. {
  451. unsigned numItems = GetNumItems();
  452. for (unsigned i = 0; i < numItems; ++i)
  453. {
  454. if (!GetItemIndent(GetItem(i)))
  455. SetChildItemsVisible(i, enable);
  456. }
  457. unsigned firstSelected = GetSelection();
  458. if (firstSelected != M_MAX_UNSIGNED)
  459. EnsureItemVisibility(firstSelected);
  460. }
  461. void ListView::ToggleChildItemsVisible(unsigned index)
  462. {
  463. unsigned numItems = GetNumItems();
  464. if (!hierarchyMode_ || index >= numItems)
  465. return;
  466. contentElement_->DisableLayoutUpdate();
  467. int baseIndent = GetItemIndent(GetItem(index));
  468. bool firstChild = true;
  469. bool firstChildVisible;
  470. for (unsigned i = index + 1; i < numItems; ++i)
  471. {
  472. UIElement* item = GetItem(i);
  473. if (GetItemIndent(item) > baseIndent)
  474. {
  475. if (firstChild)
  476. {
  477. item->SetVisible(firstChildVisible = !item->IsVisible());
  478. firstChild = false;
  479. }
  480. else
  481. item->SetVisible(firstChildVisible);
  482. }
  483. else
  484. break;
  485. }
  486. contentElement_->EnableLayoutUpdate();
  487. contentElement_->UpdateLayout();
  488. }
  489. unsigned ListView::GetNumItems() const
  490. {
  491. return contentElement_->GetNumChildren();
  492. }
  493. UIElement* ListView::GetItem(unsigned index) const
  494. {
  495. return contentElement_->GetChild(index);
  496. }
  497. PODVector<UIElement*> ListView::GetItems() const
  498. {
  499. PODVector<UIElement*> items;
  500. contentElement_->GetChildren(items);
  501. return items;
  502. }
  503. unsigned ListView::GetSelection() const
  504. {
  505. if (selections_.Empty())
  506. return M_MAX_UNSIGNED;
  507. else
  508. return GetSelections().Front();
  509. }
  510. UIElement* ListView::GetSelectedItem() const
  511. {
  512. return contentElement_->GetChild(GetSelection());
  513. }
  514. PODVector<UIElement*> ListView::GetSelectedItems() const
  515. {
  516. PODVector<UIElement*> ret;
  517. for (PODVector<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
  518. {
  519. UIElement* item = GetItem(*i);
  520. if (item)
  521. ret.Push(item);
  522. }
  523. return ret;
  524. }
  525. bool ListView::IsSelected(unsigned index) const
  526. {
  527. return selections_.Contains(index);
  528. }
  529. float ListView::GetDoubleClickInterval() const
  530. {
  531. return (float)doubleClickInterval_ / 1000.0f;
  532. }
  533. void ListView::UpdateSelectionEffect()
  534. {
  535. unsigned numItems = GetNumItems();
  536. bool highlighted = highlightMode_ == HM_ALWAYS || HasFocus();
  537. for (unsigned i = 0; i < numItems; ++i)
  538. {
  539. UIElement* item = GetItem(i);
  540. if (highlightMode_ != HM_NEVER && selections_.Contains(i))
  541. item->SetSelected(highlighted);
  542. else
  543. item->SetSelected(false);
  544. }
  545. }
  546. void ListView::EnsureItemVisibility(unsigned index)
  547. {
  548. EnsureItemVisibility(GetItem(index));
  549. }
  550. void ListView::EnsureItemVisibility(UIElement* item)
  551. {
  552. if (!item || !item->IsVisible())
  553. return;
  554. IntVector2 newView = GetViewPosition();
  555. IntVector2 currentOffset = item->GetPosition() - newView;
  556. const IntRect& clipBorder = scrollPanel_->GetClipBorder();
  557. IntVector2 windowSize(scrollPanel_->GetWidth() - clipBorder.left_ - clipBorder.right_, scrollPanel_->GetHeight() -
  558. clipBorder.top_ - clipBorder.bottom_);
  559. if (currentOffset.y_ < 0)
  560. newView.y_ += currentOffset.y_;
  561. if (currentOffset.y_ + item->GetHeight() > windowSize.y_)
  562. newView.y_ += currentOffset.y_ + item->GetHeight() - windowSize.y_;
  563. SetViewPosition(newView);
  564. }
  565. void ListView::HandleUIMouseClick(StringHash eventType, VariantMap& eventData)
  566. {
  567. if (eventData[UIMouseClick::P_BUTTON].GetInt() != MOUSEB_LEFT)
  568. return;
  569. int qualifiers = eventData[UIMouseClick::P_QUALIFIERS].GetInt();
  570. UIElement* element = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  571. unsigned numItems = GetNumItems();
  572. for (unsigned i = 0; i < numItems; ++i)
  573. {
  574. if (element == GetItem(i))
  575. {
  576. // Check doubleclick
  577. bool isDoubleClick = false;
  578. if (!multiselect_ || !qualifiers)
  579. {
  580. if (!(isDoubleClick = doubleClickTimer_.GetMSec(true) < doubleClickInterval_ && lastClickedItem_ == i))
  581. lastClickedItem_ = i;
  582. SetSelection(i);
  583. }
  584. // Check multiselect with shift & ctrl
  585. if (multiselect_)
  586. {
  587. if (qualifiers & QUAL_SHIFT)
  588. {
  589. if (selections_.Empty())
  590. SetSelection(i);
  591. else
  592. {
  593. unsigned first = selections_.Front();
  594. unsigned last = selections_.Back();
  595. PODVector<unsigned> newSelections = selections_;
  596. if (i == first || i == last)
  597. {
  598. for (unsigned j = first; j <= last; ++j)
  599. newSelections.Push(j);
  600. }
  601. else if (i < first)
  602. {
  603. for (unsigned j = i; j <= first; ++j)
  604. newSelections.Push(j);
  605. }
  606. else if (i < last)
  607. {
  608. if ((abs((int)i - (int)first)) <= (abs((int)i - (int)last)))
  609. {
  610. for (unsigned j = first; j <= i; ++j)
  611. newSelections.Push(j);
  612. }
  613. else
  614. {
  615. for (unsigned j = i; j <= last; ++j)
  616. newSelections.Push(j);
  617. }
  618. }
  619. else if (i > last)
  620. {
  621. for (unsigned j = last; j <= i; ++j)
  622. newSelections.Push(j);
  623. }
  624. SetSelections(newSelections);
  625. }
  626. }
  627. else if (qualifiers & QUAL_CTRL)
  628. ToggleSelection(i);
  629. }
  630. if (isDoubleClick)
  631. {
  632. VariantMap eventData;
  633. eventData[ItemDoubleClicked::P_ELEMENT] = (void*)this;
  634. eventData[ItemDoubleClicked::P_SELECTION] = i;
  635. SendEvent(E_ITEMDOUBLECLICKED, eventData);
  636. }
  637. return;
  638. }
  639. }
  640. }
  641. void ListView::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  642. {
  643. using namespace FocusChanged;
  644. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  645. while (element)
  646. {
  647. // If the focused element or its parent is in the list, scroll the list to make the item visible
  648. UIElement* parent = element->GetParent();
  649. if (parent == contentElement_)
  650. {
  651. EnsureItemVisibility(element);
  652. return;
  653. }
  654. element = parent;
  655. }
  656. }
  657. void ListView::HandleDefocused(StringHash eventType, VariantMap& eventData)
  658. {
  659. ClearSelection();
  660. }
  661. }