ListView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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_(0.5f),
  60. doubleClickTimer_(0.0f),
  61. lastClickedItem_(M_MAX_UNSIGNED)
  62. {
  63. UIElement* container = new UIElement(context_);
  64. container->SetInternal(true);
  65. container->SetActive(true);
  66. container->SetLayout(LM_VERTICAL);
  67. container->SetSortChildren(false);
  68. SetContentElement(container);
  69. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(ListView, HandleUIMouseClick));
  70. SubscribeToEvent(E_FOCUSCHANGED, HANDLER(ListView, HandleFocusChanged));
  71. }
  72. ListView::~ListView()
  73. {
  74. }
  75. void ListView::RegisterObject(Context* context)
  76. {
  77. context->RegisterFactory<ListView>();
  78. ENUM_ACCESSOR_ATTRIBUTE(ListView, "Highlight Mode", GetHighlightMode, SetHighlightMode, HighlightMode, highlightModes, HM_FOCUS, AM_FILE);
  79. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Multiselect", GetMultiselect, SetMultiselect, bool, false, AM_FILE);
  80. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Hierarchy Mode", GetHierarchyMode, SetHierarchyMode, bool, false, AM_FILE);
  81. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Clear Sel. On Defocus", GetClearSelectionOnDefocus, SetClearSelectionOnDefocus, bool, false, AM_FILE);
  82. ACCESSOR_ATTRIBUTE(ListView, VAR_FLOAT, "Double Click Interval", GetDoubleClickInterval, SetDoubleClickInterval, float, 0.5f, AM_FILE);
  83. COPY_BASE_ATTRIBUTES(ListView, ScrollView);
  84. }
  85. void ListView::Update(float timeStep)
  86. {
  87. if (doubleClickTimer_ > 0.0f)
  88. doubleClickTimer_ = Max(doubleClickTimer_ - timeStep, 0.0f);
  89. }
  90. void ListView::OnKey(int key, int buttons, int qualifiers)
  91. {
  92. // If no selection, can not move with keys
  93. unsigned numItems = GetNumItems();
  94. unsigned selection = GetSelection();
  95. // If either shift or ctrl held down, add to selection if multiselect enabled
  96. bool additive = multiselect_ && qualifiers & (QUAL_SHIFT | QUAL_CTRL);
  97. int delta = 0;
  98. int pageDirection = 1;
  99. if (selection != M_MAX_UNSIGNED && numItems)
  100. {
  101. switch (key)
  102. {
  103. case KEY_LEFT:
  104. case KEY_RIGHT:
  105. if (hierarchyMode_)
  106. {
  107. SetChildItemsVisible(selection, key == KEY_RIGHT);
  108. return;
  109. }
  110. break;
  111. case KEY_RETURN:
  112. if (hierarchyMode_)
  113. {
  114. ToggleChildItemsVisible(selection);
  115. return;
  116. }
  117. break;
  118. case KEY_UP:
  119. delta = -1;
  120. break;
  121. case KEY_DOWN:
  122. delta = 1;
  123. break;
  124. case KEY_PAGEUP:
  125. pageDirection = -1;
  126. // Fallthru
  127. case KEY_PAGEDOWN:
  128. {
  129. // Convert page step to pixels and see how many items have to be skipped to reach that many pixels
  130. int stepPixels = ((int)(pageStep_ * scrollPanel_->GetHeight())) - GetSelectedItem()->GetHeight();
  131. unsigned newSelection = selection;
  132. unsigned okSelection = selection;
  133. unsigned invisible = 0;
  134. while (newSelection < numItems)
  135. {
  136. UIElement* item = GetItem(newSelection);
  137. int height = 0;
  138. if (item->IsVisible())
  139. {
  140. height = item->GetHeight();
  141. okSelection = newSelection;
  142. }
  143. else
  144. ++invisible;
  145. if (stepPixels < height)
  146. break;
  147. stepPixels -= height;
  148. newSelection += pageDirection;
  149. }
  150. delta = okSelection - selection - pageDirection * invisible;
  151. }
  152. break;
  153. case KEY_HOME:
  154. delta = -(int)GetNumItems();
  155. break;
  156. case KEY_END:
  157. delta = GetNumItems();
  158. break;
  159. }
  160. }
  161. if (delta)
  162. {
  163. ChangeSelection(delta, additive);
  164. return;
  165. }
  166. using namespace UnhandledKey;
  167. VariantMap eventData;
  168. eventData[P_ELEMENT] = (void*)this;
  169. eventData[P_KEY] = key;
  170. eventData[P_BUTTONS] = buttons;
  171. eventData[P_QUALIFIERS] = qualifiers;
  172. SendEvent(E_UNHANDLEDKEY, eventData);
  173. }
  174. void ListView::OnResize()
  175. {
  176. ScrollView::OnResize();
  177. // Set the content element width to match the scrollpanel minus clipping
  178. IntRect panelBorder = scrollPanel_->GetClipBorder();
  179. contentElement_->SetWidth(scrollPanel_->GetWidth() - panelBorder.left_ - panelBorder.right_);
  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_ = interval;
  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. 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 (doubleClickTimer_ > 0.0f && lastClickedItem_ == i)
  581. {
  582. isDoubleClick = true;
  583. doubleClickTimer_ = 0.0f;
  584. }
  585. else
  586. {
  587. doubleClickTimer_ = doubleClickInterval_;
  588. lastClickedItem_ = i;
  589. }
  590. SetSelection(i);
  591. }
  592. // Check multiselect with shift & ctrl
  593. if (multiselect_)
  594. {
  595. if (qualifiers & QUAL_SHIFT)
  596. {
  597. if (selections_.Empty())
  598. SetSelection(i);
  599. else
  600. {
  601. unsigned first = selections_.Front();
  602. unsigned last = selections_.Back();
  603. PODVector<unsigned> newSelections = selections_;
  604. if (i == first || i == last)
  605. {
  606. for (unsigned j = first; j <= last; ++j)
  607. newSelections.Push(j);
  608. }
  609. else if (i < first)
  610. {
  611. for (unsigned j = i; j <= first; ++j)
  612. newSelections.Push(j);
  613. }
  614. else if (i < last)
  615. {
  616. if ((abs((int)i - (int)first)) <= (abs((int)i - (int)last)))
  617. {
  618. for (unsigned j = first; j <= i; ++j)
  619. newSelections.Push(j);
  620. }
  621. else
  622. {
  623. for (unsigned j = i; j <= last; ++j)
  624. newSelections.Push(j);
  625. }
  626. }
  627. else if (i > last)
  628. {
  629. for (unsigned j = last; j <= i; ++j)
  630. newSelections.Push(j);
  631. }
  632. SetSelections(newSelections);
  633. }
  634. }
  635. else if (qualifiers & QUAL_CTRL)
  636. ToggleSelection(i);
  637. }
  638. if (isDoubleClick)
  639. {
  640. VariantMap eventData;
  641. eventData[ItemDoubleClicked::P_ELEMENT] = (void*)this;
  642. eventData[ItemDoubleClicked::P_SELECTION] = i;
  643. SendEvent(E_ITEMDOUBLECLICKED, eventData);
  644. }
  645. return;
  646. }
  647. }
  648. }
  649. void ListView::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  650. {
  651. using namespace FocusChanged;
  652. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  653. while (element)
  654. {
  655. // If the focused element or its parent is in the list, scroll the list to make the item visible
  656. UIElement* parent = element->GetParent();
  657. if (parent == contentElement_)
  658. {
  659. EnsureItemVisibility(element);
  660. return;
  661. }
  662. element = parent;
  663. }
  664. }
  665. void ListView::HandleDefocused(StringHash eventType, VariantMap& eventData)
  666. {
  667. ClearSelection();
  668. }
  669. }