ListView.cpp 25 KB

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