ListView.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "BorderImage.h"
  25. #include "Context.h"
  26. #include "InputEvents.h"
  27. #include "ListView.h"
  28. #include "Log.h"
  29. #include "StringUtils.h"
  30. #include "UIEvents.h"
  31. #include "DebugNew.h"
  32. static const ShortStringHash indentHash("Indent");
  33. static const String highlightModes[] =
  34. {
  35. "never",
  36. "focus",
  37. "always",
  38. ""
  39. };
  40. int GetItemIndent(UIElement* item)
  41. {
  42. if (!item)
  43. return 0;
  44. return item->vars_[indentHash].GetInt();
  45. }
  46. OBJECTTYPESTATIC(ListView);
  47. ListView::ListView(Context* context) :
  48. ScrollView(context),
  49. highlightMode_(HM_FOCUS),
  50. multiselect_(false),
  51. hierarchyMode_(false),
  52. clearSelectionOnDefocus_(false),
  53. doubleClickInterval_(0.5f),
  54. doubleClickTimer_(0.0f),
  55. lastClickedItem_(M_MAX_UNSIGNED)
  56. {
  57. UIElement* container = new UIElement(context_);
  58. container->SetActive(true);
  59. container->SetLayout(LM_VERTICAL);
  60. container->SetSortChildren(false);
  61. SetContentElement(container);
  62. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(ListView, HandleUIMouseClick));
  63. SubscribeToEvent(E_FOCUSCHANGED, HANDLER(ListView, HandleFocusChanged));
  64. SubscribeToEvent(this, E_FOCUSED, HANDLER(ListView, HandleFocused));
  65. SubscribeToEvent(this, E_DEFOCUSED, HANDLER(ListView, HandleDefocused));
  66. }
  67. ListView::~ListView()
  68. {
  69. }
  70. void ListView::RegisterObject(Context* context)
  71. {
  72. context->RegisterFactory<ListView>();
  73. }
  74. void ListView::SetStyle(const XMLElement& element)
  75. {
  76. ScrollView::SetStyle(element);
  77. UIElement* root = GetRoot();
  78. XMLElement itemElem = element.GetChild("listitem");
  79. if (root)
  80. {
  81. while (itemElem)
  82. {
  83. if (itemElem.HasAttribute("name"))
  84. {
  85. UIElement* item = root->GetChild(itemElem.GetAttribute("name"), true);
  86. AddItem(item);
  87. if (itemElem.HasAttribute("indent"))
  88. item->vars_[indentHash] = itemElem.GetInt("indent");
  89. itemElem = itemElem.GetNext("listitem");
  90. }
  91. }
  92. }
  93. if (element.HasChild("highlight"))
  94. {
  95. String highlight = element.GetChild("highlight").GetAttributeLower("value");
  96. SetHighlightMode((HighlightMode)GetStringListIndex(highlight, highlightModes, HM_FOCUS));
  97. }
  98. if (element.HasChild("multiselect"))
  99. SetMultiselect(element.GetChild("multiselect").GetBool("enable"));
  100. if (element.HasChild("hierarchy"))
  101. SetHierarchyMode(element.GetChild("hierarchy").GetBool("enable"));
  102. if (element.HasChild("clearselection"))
  103. SetClearSelectionOnDefocus(element.GetChild("clearselection").GetBool("enable"));
  104. if (element.HasChild("doubleclickinterval"))
  105. SetDoubleClickInterval(element.GetChild("doubleclickinterval").GetFloat("value"));
  106. XMLElement selectionElem = element.GetChild("selection");
  107. while (selectionElem)
  108. {
  109. AddSelection(selectionElem.GetInt("value"));
  110. selectionElem = selectionElem.GetNext("selection");
  111. }
  112. // Set the container's layout border to match scroll panel clipping, so that elements are not left partially hidden
  113. contentElement_->SetLayoutBorder(scrollPanel_->GetClipBorder());
  114. }
  115. void ListView::Update(float timeStep)
  116. {
  117. if (doubleClickTimer_ > 0.0f)
  118. doubleClickTimer_ = Max(doubleClickTimer_ - timeStep, 0.0f);
  119. }
  120. void ListView::OnKey(int key, int buttons, int qualifiers)
  121. {
  122. // If no selection, can not move with keys
  123. unsigned numItems = GetNumItems();
  124. unsigned selection = GetSelection();
  125. if (selection != M_MAX_UNSIGNED && numItems)
  126. {
  127. // If either shift or ctrl held down, add to selection if multiselect enabled
  128. bool additive = multiselect_ && qualifiers != 0;
  129. switch (key)
  130. {
  131. case KEY_LEFT:
  132. if (hierarchyMode_)
  133. {
  134. SetChildItemsVisible(selection, false);
  135. return;
  136. }
  137. break;
  138. case KEY_RIGHT:
  139. if (hierarchyMode_)
  140. {
  141. SetChildItemsVisible(selection, true);
  142. return;
  143. }
  144. break;
  145. case KEY_RETURN:
  146. if (hierarchyMode_)
  147. {
  148. ToggleChildItemsVisible(selection);
  149. return;
  150. }
  151. break;
  152. case KEY_UP:
  153. ChangeSelection(-1, additive);
  154. return;
  155. case KEY_DOWN:
  156. ChangeSelection(1, additive);
  157. return;
  158. case KEY_PAGEUP:
  159. {
  160. // Convert page step to pixels and see how many items have to be skipped to reach that many pixels
  161. int stepPixels = ((int)(pageStep_ * scrollPanel_->GetHeight())) - GetSelectedItem()->GetHeight();
  162. unsigned newSelection = selection;
  163. unsigned okSelection = selection;
  164. while (newSelection < numItems)
  165. {
  166. UIElement* item = GetItem(newSelection);
  167. int height = 0;
  168. if (item->IsVisible())
  169. {
  170. height = item->GetHeight();
  171. okSelection = newSelection;
  172. }
  173. if (stepPixels < height)
  174. break;
  175. stepPixels -= height;
  176. --newSelection;
  177. }
  178. if (!additive)
  179. SetSelection(okSelection);
  180. else
  181. AddSelection(okSelection);
  182. }
  183. return;
  184. case KEY_PAGEDOWN:
  185. {
  186. int stepPixels = ((int)(pageStep_ * scrollPanel_->GetHeight())) - GetSelectedItem()->GetHeight();
  187. unsigned newSelection = selection;
  188. unsigned okSelection = selection;
  189. while (newSelection < numItems)
  190. {
  191. UIElement* item = GetItem(newSelection);
  192. int height = 0;
  193. if (item->IsVisible())
  194. {
  195. height = item->GetHeight();
  196. okSelection = newSelection;
  197. }
  198. if (stepPixels < height)
  199. break;
  200. stepPixels -= height;
  201. ++newSelection;
  202. }
  203. if (!additive)
  204. SetSelection(okSelection);
  205. else
  206. AddSelection(okSelection);
  207. }
  208. return;
  209. case KEY_HOME:
  210. ChangeSelection(-(int)GetNumItems(), additive);
  211. return;
  212. case KEY_END:
  213. ChangeSelection(GetNumItems(), additive);
  214. return;
  215. }
  216. }
  217. using namespace UnhandledKey;
  218. VariantMap eventData;
  219. eventData[P_ELEMENT] = (void*)this;
  220. eventData[P_KEY] = key;
  221. eventData[P_BUTTONS] = buttons;
  222. eventData[P_QUALIFIERS] = qualifiers;
  223. SendEvent(E_UNHANDLEDKEY, eventData);
  224. }
  225. void ListView::OnResize()
  226. {
  227. ScrollView::OnResize();
  228. // Set the content element width to match the scrollpanel
  229. contentElement_->SetWidth(scrollPanel_->GetWidth());
  230. }
  231. void ListView::AddItem(UIElement* item)
  232. {
  233. InsertItem(contentElement_->GetNumChildren(), item);
  234. }
  235. void ListView::InsertItem(unsigned index, UIElement* item)
  236. {
  237. if (!item || item->GetParent() == contentElement_)
  238. return;
  239. // Enable input so that clicking the item can be detected
  240. item->SetActive(true);
  241. item->SetSelected(false);
  242. contentElement_->InsertChild(index, item);
  243. // If necessary, shift the following selections
  244. if (!selections_.Empty())
  245. {
  246. Set<unsigned> prevSelections;
  247. selections_.Clear();
  248. for (Set<unsigned>::Iterator j = prevSelections.Begin(); j != prevSelections.End(); ++j)
  249. {
  250. if (*j >= index)
  251. selections_.Insert(*j + 1);
  252. else
  253. selections_.Insert(*j);
  254. }
  255. UpdateSelectionEffect();
  256. }
  257. }
  258. void ListView::RemoveItem(UIElement* item)
  259. {
  260. unsigned numItems = GetNumItems();
  261. for (unsigned i = 0; i < numItems; ++i)
  262. {
  263. if (GetItem(i) == item)
  264. {
  265. item->SetSelected(false);
  266. selections_.Erase(i);
  267. // Remove any child items in hierarchy mode
  268. unsigned removed = 1;
  269. if (hierarchyMode_)
  270. {
  271. int baseIndent = GetItemIndent(item);
  272. int j = i + 1;
  273. for (;;)
  274. {
  275. UIElement* childItem = GetItem(i + 1);
  276. if (!childItem)
  277. break;
  278. if (GetItemIndent(childItem) > baseIndent)
  279. {
  280. childItem->SetSelected(false);
  281. selections_.Erase(j);
  282. contentElement_->RemoveChild(childItem);
  283. ++removed;
  284. }
  285. else
  286. break;
  287. ++j;
  288. }
  289. }
  290. // If necessary, shift the following selections
  291. if (!selections_.Empty())
  292. {
  293. Set<unsigned> prevSelections;
  294. selections_.Clear();
  295. for (Set<unsigned>::Iterator j = prevSelections.Begin(); j != prevSelections.End(); ++j)
  296. {
  297. if (*j > i)
  298. selections_.Insert(*j - removed);
  299. else
  300. selections_.Insert(*j);
  301. }
  302. UpdateSelectionEffect();
  303. }
  304. break;
  305. }
  306. }
  307. contentElement_->RemoveChild(item);
  308. }
  309. void ListView::RemoveItem(unsigned index)
  310. {
  311. RemoveItem(GetItem(index));
  312. }
  313. void ListView::RemoveAllItems()
  314. {
  315. contentElement_->DisableLayoutUpdate();
  316. unsigned numItems = GetNumItems();
  317. for (unsigned i = 0; i < numItems; ++i)
  318. contentElement_->GetChild(i)->SetSelected(false);
  319. contentElement_->RemoveAllChildren();
  320. ClearSelection();
  321. contentElement_->EnableLayoutUpdate();
  322. contentElement_->UpdateLayout();
  323. }
  324. void ListView::SetSelection(unsigned index)
  325. {
  326. Set<unsigned> indices;
  327. indices.Insert(index);
  328. SetSelections(indices);
  329. EnsureItemVisibility(index);
  330. }
  331. void ListView::SetSelections(const Set<unsigned>& indices)
  332. {
  333. unsigned numItems = GetNumItems();
  334. // Remove first items that should no longer be selected
  335. for (Set<unsigned>::Iterator i = selections_.Begin(); i != selections_.End();)
  336. {
  337. unsigned index = *i;
  338. if (!indices.Contains(index))
  339. {
  340. Set<unsigned>::Iterator current = i++;
  341. selections_.Erase(current);
  342. using namespace ItemSelected;
  343. VariantMap eventData;
  344. eventData[P_ELEMENT] = (void*)this;
  345. eventData[P_SELECTION] = index;
  346. SendEvent(E_ITEMDESELECTED, eventData);
  347. }
  348. else
  349. ++i;
  350. }
  351. // Then add missing items
  352. for (Set<unsigned>::ConstIterator i = indices.Begin(); i != indices.End(); ++i)
  353. {
  354. unsigned index = *i;
  355. if (index < numItems)
  356. {
  357. // In singleselect mode, resend the event even for the same selection
  358. if (selections_.Find(index) == selections_.End() || !multiselect_)
  359. {
  360. selections_.Insert(*i);
  361. using namespace ItemSelected;
  362. VariantMap eventData;
  363. eventData[P_ELEMENT] = (void*)this;
  364. eventData[P_SELECTION] = *i;
  365. SendEvent(E_ITEMSELECTED, eventData);
  366. }
  367. }
  368. // If no multiselect enabled, allow setting only one item
  369. if (!multiselect_)
  370. break;
  371. }
  372. SendEvent(E_SELECTIONCHANGED);
  373. UpdateSelectionEffect();
  374. }
  375. void ListView::AddSelection(unsigned index)
  376. {
  377. if (!multiselect_)
  378. SetSelection(index);
  379. else
  380. {
  381. if (index >= GetNumItems())
  382. return;
  383. Set<unsigned> newSelections = selections_;
  384. newSelections.Insert(index);
  385. SetSelections(newSelections);
  386. EnsureItemVisibility(index);
  387. }
  388. }
  389. void ListView::RemoveSelection(unsigned index)
  390. {
  391. if (index >= GetNumItems())
  392. return;
  393. Set<unsigned> newSelections = selections_;
  394. newSelections.Erase(index);
  395. SetSelections(newSelections);
  396. EnsureItemVisibility(index);
  397. }
  398. void ListView::ToggleSelection(unsigned index)
  399. {
  400. unsigned numItems = GetNumItems();
  401. if (index >= numItems)
  402. return;
  403. if (selections_.Contains(index))
  404. RemoveSelection(index);
  405. else
  406. AddSelection(index);
  407. }
  408. void ListView::ChangeSelection(int delta, bool additive)
  409. {
  410. if (selections_.Empty())
  411. return;
  412. if (!multiselect_)
  413. additive = false;
  414. // If going downwards, use the last selection as a base. Otherwise use first
  415. unsigned selection = delta > 0 ? selections_.Back() : selections_.Front();
  416. unsigned numItems = GetNumItems();
  417. unsigned newSelection = selection;
  418. unsigned okSelection = selection;
  419. while (delta != 0)
  420. {
  421. if (delta > 0)
  422. {
  423. ++newSelection;
  424. if (newSelection >= numItems)
  425. break;
  426. }
  427. if (delta < 0)
  428. {
  429. --newSelection;
  430. if (newSelection >= numItems)
  431. break;
  432. }
  433. UIElement* item = GetItem(newSelection);
  434. if (item->IsVisible())
  435. {
  436. okSelection = newSelection;
  437. if (delta > 0)
  438. --delta;
  439. if (delta < 0)
  440. ++delta;
  441. }
  442. }
  443. if (!additive)
  444. SetSelection(okSelection);
  445. else
  446. AddSelection(okSelection);
  447. }
  448. void ListView::ClearSelection()
  449. {
  450. SetSelections(Set<unsigned>());
  451. UpdateSelectionEffect();
  452. }
  453. void ListView::SetHighlightMode(HighlightMode mode)
  454. {
  455. highlightMode_ = mode;
  456. UpdateSelectionEffect();
  457. }
  458. void ListView::SetMultiselect(bool enable)
  459. {
  460. multiselect_ = enable;
  461. }
  462. void ListView::SetHierarchyMode(bool enable)
  463. {
  464. hierarchyMode_ = enable;
  465. }
  466. void ListView::SetClearSelectionOnDefocus(bool enable)
  467. {
  468. clearSelectionOnDefocus_ = enable;
  469. }
  470. void ListView::SetDoubleClickInterval(float interval)
  471. {
  472. doubleClickInterval_ = interval;
  473. }
  474. void ListView::SetChildItemsVisible(unsigned index, bool enable)
  475. {
  476. unsigned numItems = GetNumItems();
  477. if (!hierarchyMode_ || index >= numItems)
  478. return;
  479. contentElement_->DisableLayoutUpdate();
  480. int baseIndent = GetItemIndent(GetItem(index));
  481. for (unsigned i = index + 1; i < numItems; ++i)
  482. {
  483. UIElement* item = GetItem(i);
  484. if (GetItemIndent(item) > baseIndent)
  485. item->SetVisible(enable);
  486. else
  487. break;
  488. }
  489. contentElement_->EnableLayoutUpdate();
  490. contentElement_->UpdateLayout();
  491. }
  492. void ListView::SetChildItemsVisible(bool enable)
  493. {
  494. unsigned numItems = GetNumItems();
  495. for (unsigned i = 0; i < numItems; ++i)
  496. {
  497. if (!GetItemIndent(GetItem(i)))
  498. SetChildItemsVisible(i, enable);
  499. }
  500. if (GetSelections().Size() == 1)
  501. EnsureItemVisibility(GetSelection());
  502. }
  503. void ListView::ToggleChildItemsVisible(unsigned index)
  504. {
  505. unsigned numItems = GetNumItems();
  506. if (!hierarchyMode_ || index >= numItems)
  507. return;
  508. contentElement_->DisableLayoutUpdate();
  509. int baseIndent = GetItemIndent(GetItem(index));
  510. bool firstChild = true;
  511. UIElement* prevItem = 0;
  512. for (unsigned i = index + 1; i < numItems; ++i)
  513. {
  514. UIElement* item = GetItem(i);
  515. if (GetItemIndent(item) > baseIndent)
  516. {
  517. if (firstChild)
  518. {
  519. item->SetVisible(!item->IsVisible());
  520. firstChild = false;
  521. }
  522. else
  523. item->SetVisible(prevItem->IsVisible());
  524. }
  525. else
  526. break;
  527. prevItem = item;
  528. }
  529. contentElement_->EnableLayoutUpdate();
  530. contentElement_->UpdateLayout();
  531. }
  532. unsigned ListView::GetNumItems() const
  533. {
  534. return contentElement_->GetNumChildren();
  535. }
  536. UIElement* ListView::GetItem(unsigned index) const
  537. {
  538. return contentElement_->GetChild(index);
  539. }
  540. PODVector<UIElement*> ListView::GetItems() const
  541. {
  542. PODVector<UIElement*> items;
  543. contentElement_->GetChildren(items);
  544. return items;
  545. }
  546. unsigned ListView::GetSelection() const
  547. {
  548. if (selections_.Empty())
  549. return M_MAX_UNSIGNED;
  550. else
  551. return *selections_.Begin();
  552. }
  553. UIElement* ListView::GetSelectedItem() const
  554. {
  555. return contentElement_->GetChild(GetSelection());
  556. }
  557. PODVector<UIElement*> ListView::GetSelectedItems() const
  558. {
  559. PODVector<UIElement*> ret;
  560. for (Set<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
  561. {
  562. UIElement* item = GetItem(*i);
  563. if (item)
  564. ret.Push(item);
  565. }
  566. return ret;
  567. }
  568. bool ListView::IsSelected(unsigned index) const
  569. {
  570. return selections_.Contains(index);
  571. }
  572. void ListView::UpdateSelectionEffect()
  573. {
  574. unsigned numItems = GetNumItems();
  575. for (unsigned i = 0; i < numItems; ++i)
  576. {
  577. UIElement* item = GetItem(i);
  578. if (highlightMode_ != HM_NEVER && selections_.Contains(i))
  579. item->SetSelected(HasFocus() || highlightMode_ == HM_ALWAYS);
  580. else
  581. item->SetSelected(false);
  582. }
  583. }
  584. void ListView::EnsureItemVisibility(unsigned index)
  585. {
  586. EnsureItemVisibility(GetItem(index));
  587. }
  588. void ListView::EnsureItemVisibility(UIElement* item)
  589. {
  590. if (!item || !item->IsVisible())
  591. return;
  592. IntVector2 currentOffset = item->GetScreenPosition() - scrollPanel_->GetScreenPosition() - contentElement_->GetPosition();
  593. IntVector2 newView = GetViewPosition();
  594. const IntRect& clipBorder = scrollPanel_->GetClipBorder();
  595. IntVector2 windowSize(scrollPanel_->GetWidth() - clipBorder.left_ - clipBorder.right_, scrollPanel_->GetHeight() -
  596. clipBorder.top_ - clipBorder.bottom_);
  597. if (currentOffset.y_ < 0)
  598. newView.y_ += currentOffset.y_;
  599. if (currentOffset.y_ + item->GetHeight() > windowSize.y_)
  600. newView.y_ += currentOffset.y_ + item->GetHeight() - windowSize.y_;
  601. SetViewPosition(newView);
  602. }
  603. void ListView::HandleUIMouseClick(StringHash eventType, VariantMap& eventData)
  604. {
  605. if (eventData[UIMouseClick::P_BUTTON].GetInt() != MOUSEB_LEFT)
  606. return;
  607. int qualifiers = eventData[UIMouseClick::P_QUALIFIERS].GetInt();
  608. UIElement* element = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  609. unsigned numItems = GetNumItems();
  610. for (unsigned i = 0; i < numItems; ++i)
  611. {
  612. if (element == GetItem(i))
  613. {
  614. // Check doubleclick
  615. bool isDoubleClick = false;
  616. if (!multiselect_ || !qualifiers)
  617. {
  618. if (doubleClickTimer_ > 0.0f && lastClickedItem_ == i)
  619. {
  620. isDoubleClick = true;
  621. doubleClickTimer_ = 0.0f;
  622. }
  623. else
  624. {
  625. doubleClickTimer_ = doubleClickInterval_;
  626. lastClickedItem_ = i;
  627. }
  628. SetSelection(i);
  629. }
  630. // Check multiselect with shift & ctrl
  631. if (multiselect_)
  632. {
  633. if (qualifiers & QUAL_SHIFT)
  634. {
  635. if (selections_.Empty())
  636. SetSelection(i);
  637. else
  638. {
  639. unsigned first = selections_.Front();
  640. unsigned last = selections_.Back();
  641. Set<unsigned> newSelections = selections_;
  642. if (i == first || i == last)
  643. {
  644. for (unsigned j = first; j <= last; ++j)
  645. newSelections.Insert(j);
  646. }
  647. else if (i < first)
  648. {
  649. for (unsigned j = i; j <= first; ++j)
  650. newSelections.Insert(j);
  651. }
  652. else if (i < last)
  653. {
  654. if ((abs((int)i - (int)first)) <= (abs((int)i - (int)last)))
  655. {
  656. for (unsigned j = first; j <= i; ++j)
  657. newSelections.Insert(j);
  658. }
  659. else
  660. {
  661. for (unsigned j = i; j <= last; ++j)
  662. newSelections.Insert(j);
  663. }
  664. }
  665. else if (i > last)
  666. {
  667. for (unsigned j = last; j <= i; ++j)
  668. newSelections.Insert(j);
  669. }
  670. SetSelections(newSelections);
  671. }
  672. }
  673. else if (qualifiers & QUAL_CTRL)
  674. ToggleSelection(i);
  675. }
  676. if (isDoubleClick)
  677. {
  678. VariantMap eventData;
  679. eventData[ItemDoubleClicked::P_ELEMENT] = (void*)this;
  680. eventData[ItemDoubleClicked::P_SELECTION] = i;
  681. SendEvent(E_ITEMDOUBLECLICKED, eventData);
  682. }
  683. return;
  684. }
  685. }
  686. }
  687. void ListView::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  688. {
  689. using namespace FocusChanged;
  690. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  691. while (element)
  692. {
  693. // If the focused element or its parent is in the list, scroll the list to make the item visible
  694. UIElement* parent = element->GetParent();
  695. if (parent == contentElement_)
  696. {
  697. EnsureItemVisibility(element);
  698. return;
  699. }
  700. element = parent;
  701. }
  702. }
  703. void ListView::HandleFocused(StringHash eventType, VariantMap& eventData)
  704. {
  705. UpdateSelectionEffect();
  706. }
  707. void ListView::HandleDefocused(StringHash eventType, VariantMap& eventData)
  708. {
  709. if (clearSelectionOnDefocus_)
  710. ClearSelection();
  711. UpdateSelectionEffect();
  712. }