ListView.cpp 25 KB

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