ListView.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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->SetSortingEnabled(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. UpdateSelectionEffect();
  381. }
  382. void ListView::AddSelection(unsigned index)
  383. {
  384. if (!multiselect_)
  385. SetSelection(index);
  386. else
  387. {
  388. if (index >= GetNumItems())
  389. return;
  390. Set<unsigned> newSelections = selections_;
  391. newSelections.Insert(index);
  392. SetSelections(newSelections);
  393. EnsureItemVisibility(index);
  394. }
  395. }
  396. void ListView::RemoveSelection(unsigned index)
  397. {
  398. if (index >= GetNumItems())
  399. return;
  400. Set<unsigned> newSelections = selections_;
  401. newSelections.Erase(index);
  402. SetSelections(newSelections);
  403. EnsureItemVisibility(index);
  404. }
  405. void ListView::ToggleSelection(unsigned index)
  406. {
  407. unsigned numItems = GetNumItems();
  408. if (index >= numItems)
  409. return;
  410. if (selections_.Contains(index))
  411. RemoveSelection(index);
  412. else
  413. AddSelection(index);
  414. }
  415. void ListView::ChangeSelection(int delta, bool additive)
  416. {
  417. if (selections_.Empty())
  418. return;
  419. if (!multiselect_)
  420. additive = false;
  421. // If going downwards, use the last selection as a base. Otherwise use first
  422. unsigned selection = delta > 0 ? selections_.Back() : selections_.Front();
  423. unsigned numItems = GetNumItems();
  424. unsigned newSelection = selection;
  425. unsigned okSelection = selection;
  426. while (delta != 0)
  427. {
  428. if (delta > 0)
  429. {
  430. ++newSelection;
  431. if (newSelection >= numItems)
  432. break;
  433. }
  434. if (delta < 0)
  435. {
  436. --newSelection;
  437. if (newSelection >= numItems)
  438. break;
  439. }
  440. UIElement* item = GetItem(newSelection);
  441. if (item->IsVisible())
  442. {
  443. okSelection = newSelection;
  444. if (delta > 0)
  445. --delta;
  446. if (delta < 0)
  447. ++delta;
  448. }
  449. }
  450. if (!additive)
  451. SetSelection(okSelection);
  452. else
  453. AddSelection(okSelection);
  454. }
  455. void ListView::ClearSelection()
  456. {
  457. SetSelections(Set<unsigned>());
  458. UpdateSelectionEffect();
  459. }
  460. void ListView::SetHighlightMode(HighlightMode mode)
  461. {
  462. highlightMode_ = mode;
  463. UpdateSelectionEffect();
  464. }
  465. void ListView::SetMultiselect(bool enable)
  466. {
  467. multiselect_ = enable;
  468. }
  469. void ListView::SetHierarchyMode(bool enable)
  470. {
  471. hierarchyMode_ = enable;
  472. }
  473. void ListView::SetClearSelectionOnDefocus(bool enable)
  474. {
  475. clearSelectionOnDefocus_ = enable;
  476. }
  477. void ListView::SetDoubleClickInterval(float interval)
  478. {
  479. doubleClickInterval_ = interval;
  480. }
  481. void ListView::SetChildItemsVisible(unsigned index, bool enable)
  482. {
  483. unsigned numItems = GetNumItems();
  484. if (!hierarchyMode_ || index >= numItems)
  485. return;
  486. contentElement_->DisableLayoutUpdate();
  487. int baseIndent = GetItemIndent(GetItem(index));
  488. for (unsigned i = index + 1; i < numItems; ++i)
  489. {
  490. UIElement* item = GetItem(i);
  491. if (GetItemIndent(item) > baseIndent)
  492. item->SetVisible(enable);
  493. else
  494. break;
  495. }
  496. contentElement_->EnableLayoutUpdate();
  497. contentElement_->UpdateLayout();
  498. }
  499. void ListView::SetChildItemsVisible(bool enable)
  500. {
  501. unsigned numItems = GetNumItems();
  502. for (unsigned i = 0; i < numItems; ++i)
  503. {
  504. if (!GetItemIndent(GetItem(i)))
  505. SetChildItemsVisible(i, enable);
  506. }
  507. if (GetSelections().Size() == 1)
  508. EnsureItemVisibility(GetSelection());
  509. }
  510. void ListView::ToggleChildItemsVisible(unsigned index)
  511. {
  512. unsigned numItems = GetNumItems();
  513. if (!hierarchyMode_ || index >= numItems)
  514. return;
  515. contentElement_->DisableLayoutUpdate();
  516. int baseIndent = GetItemIndent(GetItem(index));
  517. bool firstChild = true;
  518. UIElement* prevItem = 0;
  519. for (unsigned i = index + 1; i < numItems; ++i)
  520. {
  521. UIElement* item = GetItem(i);
  522. if (GetItemIndent(item) > baseIndent)
  523. {
  524. if (firstChild)
  525. {
  526. item->SetVisible(!item->IsVisible());
  527. firstChild = false;
  528. }
  529. else
  530. item->SetVisible(prevItem->IsVisible());
  531. }
  532. else
  533. break;
  534. prevItem = item;
  535. }
  536. contentElement_->EnableLayoutUpdate();
  537. contentElement_->UpdateLayout();
  538. }
  539. unsigned ListView::GetNumItems() const
  540. {
  541. return contentElement_->GetNumChildren();
  542. }
  543. UIElement* ListView::GetItem(unsigned index) const
  544. {
  545. return contentElement_->GetChild(index);
  546. }
  547. PODVector<UIElement*> ListView::GetItems() const
  548. {
  549. PODVector<UIElement*> items;
  550. contentElement_->GetChildren(items);
  551. return items;
  552. }
  553. unsigned ListView::GetSelection() const
  554. {
  555. if (selections_.Empty())
  556. return M_MAX_UNSIGNED;
  557. else
  558. return *selections_.Begin();
  559. }
  560. UIElement* ListView::GetSelectedItem() const
  561. {
  562. return contentElement_->GetChild(GetSelection());
  563. }
  564. PODVector<UIElement*> ListView::GetSelectedItems() const
  565. {
  566. PODVector<UIElement*> ret;
  567. for (Set<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
  568. {
  569. UIElement* item = GetItem(*i);
  570. if (item)
  571. ret.Push(item);
  572. }
  573. return ret;
  574. }
  575. void ListView::UpdateSelectionEffect()
  576. {
  577. unsigned numItems = GetNumItems();
  578. for (unsigned i = 0; i < numItems; ++i)
  579. {
  580. UIElement* item = GetItem(i);
  581. if (highlightMode_ != HM_NEVER && selections_.Contains(i))
  582. item->SetSelected(HasFocus() || highlightMode_ == HM_ALWAYS);
  583. else
  584. item->SetSelected(false);
  585. }
  586. }
  587. void ListView::EnsureItemVisibility(unsigned index)
  588. {
  589. EnsureItemVisibility(GetItem(index));
  590. }
  591. void ListView::EnsureItemVisibility(UIElement* item)
  592. {
  593. if (!item || !item->IsVisible())
  594. return;
  595. IntVector2 currentOffset = item->GetScreenPosition() - scrollPanel_->GetScreenPosition() - contentElement_->GetPosition();
  596. IntVector2 newView = GetViewPosition();
  597. const IntRect& clipBorder = scrollPanel_->GetClipBorder();
  598. IntVector2 windowSize(scrollPanel_->GetWidth() - clipBorder.left_ - clipBorder.right_, scrollPanel_->GetHeight() -
  599. clipBorder.top_ - clipBorder.bottom_);
  600. if (currentOffset.y_ < 0)
  601. newView.y_ += currentOffset.y_;
  602. if (currentOffset.y_ + item->GetHeight() > windowSize.y_)
  603. newView.y_ += currentOffset.y_ + item->GetHeight() - windowSize.y_;
  604. SetViewPosition(newView);
  605. }
  606. void ListView::HandleUIMouseClick(StringHash eventType, VariantMap& eventData)
  607. {
  608. if (eventData[UIMouseClick::P_BUTTON].GetInt() != MOUSEB_LEFT)
  609. return;
  610. int qualifiers = eventData[UIMouseClick::P_QUALIFIERS].GetInt();
  611. UIElement* element = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  612. unsigned numItems = GetNumItems();
  613. for (unsigned i = 0; i < numItems; ++i)
  614. {
  615. if (element == GetItem(i))
  616. {
  617. // Check doubleclick
  618. bool isDoubleClick = false;
  619. if (!multiselect_ || !qualifiers)
  620. {
  621. if (doubleClickTimer_ > 0.0f && lastClickedItem_ == i)
  622. {
  623. isDoubleClick = true;
  624. doubleClickTimer_ = 0.0f;
  625. }
  626. else
  627. {
  628. doubleClickTimer_ = doubleClickInterval_;
  629. lastClickedItem_ = i;
  630. }
  631. SetSelection(i);
  632. }
  633. // Check multiselect with shift & ctrl
  634. if (multiselect_)
  635. {
  636. if (qualifiers & QUAL_SHIFT)
  637. {
  638. if (selections_.Empty())
  639. SetSelection(i);
  640. else
  641. {
  642. unsigned first = selections_.Front();
  643. unsigned last = selections_.Back();
  644. Set<unsigned> newSelections = selections_;
  645. if (i == first || i == last)
  646. {
  647. for (unsigned j = first; j <= last; ++j)
  648. newSelections.Insert(j);
  649. }
  650. else if (i < first)
  651. {
  652. for (unsigned j = i; j <= first; ++j)
  653. newSelections.Insert(j);
  654. }
  655. else if (i < last)
  656. {
  657. if ((abs((int)i - (int)first)) <= (abs((int)i - (int)last)))
  658. {
  659. for (unsigned j = first; j <= i; ++j)
  660. newSelections.Insert(j);
  661. }
  662. else
  663. {
  664. for (unsigned j = i; j <= last; ++j)
  665. newSelections.Insert(j);
  666. }
  667. }
  668. else if (i > last)
  669. {
  670. for (unsigned j = last; j <= i; ++j)
  671. newSelections.Insert(j);
  672. }
  673. SetSelections(newSelections);
  674. }
  675. }
  676. else if (qualifiers & QUAL_CTRL)
  677. ToggleSelection(i);
  678. }
  679. if (isDoubleClick)
  680. {
  681. VariantMap eventData;
  682. eventData[ItemDoubleClicked::P_ELEMENT] = (void*)this;
  683. eventData[ItemDoubleClicked::P_SELECTION] = i;
  684. SendEvent(E_ITEMDOUBLECLICKED, eventData);
  685. }
  686. return;
  687. }
  688. }
  689. }
  690. void ListView::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  691. {
  692. using namespace FocusChanged;
  693. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  694. while (element)
  695. {
  696. // If the focused element or its parent is in the list, scroll the list to make the item visible
  697. UIElement* parent = element->GetParent();
  698. if (parent == contentElement_)
  699. {
  700. EnsureItemVisibility(element);
  701. return;
  702. }
  703. element = parent;
  704. }
  705. }