ListView.cpp 24 KB

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