ListView.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. HashSet<unsigned> prevSelections = selections_;
  248. selections_.Clear();
  249. for (HashSet<unsigned>::Iterator j = prevSelections.Begin(); j != prevSelections.End(); ++j)
  250. {
  251. if (*j >= index)
  252. selections_.Insert(*j + 1);
  253. else
  254. selections_.Insert(*j);
  255. }
  256. UpdateSelectionEffect();
  257. }
  258. }
  259. void ListView::RemoveItem(UIElement* item)
  260. {
  261. unsigned numItems = GetNumItems();
  262. for (unsigned i = 0; i < numItems; ++i)
  263. {
  264. if (GetItem(i) == item)
  265. {
  266. item->SetSelected(false);
  267. selections_.Erase(i);
  268. // Remove any child items in hierarchy mode
  269. unsigned removed = 1;
  270. if (hierarchyMode_)
  271. {
  272. int baseIndent = GetItemIndent(item);
  273. int j = i + 1;
  274. for (;;)
  275. {
  276. UIElement* childItem = GetItem(i + 1);
  277. if (!childItem)
  278. break;
  279. if (GetItemIndent(childItem) > baseIndent)
  280. {
  281. childItem->SetSelected(false);
  282. selections_.Erase(j);
  283. contentElement_->RemoveChild(childItem);
  284. ++removed;
  285. }
  286. else
  287. break;
  288. ++j;
  289. }
  290. }
  291. // If necessary, shift the following selections
  292. if (!selections_.Empty())
  293. {
  294. HashSet<unsigned> prevSelections = selections_;
  295. selections_.Clear();
  296. for (HashSet<unsigned>::Iterator j = prevSelections.Begin(); j != prevSelections.End(); ++j)
  297. {
  298. if (*j > i)
  299. selections_.Insert(*j - removed);
  300. else
  301. selections_.Insert(*j);
  302. }
  303. UpdateSelectionEffect();
  304. }
  305. break;
  306. }
  307. }
  308. contentElement_->RemoveChild(item);
  309. }
  310. void ListView::RemoveItem(unsigned index)
  311. {
  312. RemoveItem(GetItem(index));
  313. }
  314. void ListView::RemoveAllItems()
  315. {
  316. contentElement_->DisableLayoutUpdate();
  317. unsigned numItems = GetNumItems();
  318. for (unsigned i = 0; i < numItems; ++i)
  319. contentElement_->GetChild(i)->SetSelected(false);
  320. contentElement_->RemoveAllChildren();
  321. ClearSelection();
  322. contentElement_->EnableLayoutUpdate();
  323. contentElement_->UpdateLayout();
  324. }
  325. void ListView::SetSelection(unsigned index)
  326. {
  327. HashSet<unsigned> indices;
  328. indices.Insert(index);
  329. SetSelections(indices);
  330. EnsureItemVisibility(index);
  331. }
  332. void ListView::SetSelections(const PODVector<unsigned>& indices)
  333. {
  334. HashSet<unsigned> newSelection;
  335. for (PODVector<unsigned>::ConstIterator i = indices.Begin(); i != indices.End(); ++i)
  336. newSelection.Insert(*i);
  337. SetSelections(newSelection);
  338. }
  339. void ListView::AddSelection(unsigned index)
  340. {
  341. if (!multiselect_)
  342. SetSelection(index);
  343. else
  344. {
  345. if (index >= GetNumItems())
  346. return;
  347. HashSet<unsigned> newSelections = selections_;
  348. newSelections.Insert(index);
  349. SetSelections(newSelections);
  350. EnsureItemVisibility(index);
  351. }
  352. }
  353. void ListView::RemoveSelection(unsigned index)
  354. {
  355. if (index >= GetNumItems())
  356. return;
  357. HashSet<unsigned> newSelections = selections_;
  358. newSelections.Erase(index);
  359. SetSelections(newSelections);
  360. EnsureItemVisibility(index);
  361. }
  362. void ListView::ToggleSelection(unsigned index)
  363. {
  364. unsigned numItems = GetNumItems();
  365. if (index >= numItems)
  366. return;
  367. if (selections_.Contains(index))
  368. RemoveSelection(index);
  369. else
  370. AddSelection(index);
  371. }
  372. void ListView::ChangeSelection(int delta, bool additive)
  373. {
  374. if (selections_.Empty())
  375. return;
  376. if (!multiselect_)
  377. additive = false;
  378. // If going downwards, use the last selection as a base. Otherwise use first
  379. unsigned selection = delta > 0 ? selections_.Back() : selections_.Front();
  380. unsigned numItems = GetNumItems();
  381. unsigned newSelection = selection;
  382. unsigned okSelection = selection;
  383. while (delta != 0)
  384. {
  385. if (delta > 0)
  386. {
  387. ++newSelection;
  388. if (newSelection >= numItems)
  389. break;
  390. }
  391. if (delta < 0)
  392. {
  393. --newSelection;
  394. if (newSelection >= numItems)
  395. break;
  396. }
  397. UIElement* item = GetItem(newSelection);
  398. if (item->IsVisible())
  399. {
  400. okSelection = newSelection;
  401. if (delta > 0)
  402. --delta;
  403. if (delta < 0)
  404. ++delta;
  405. }
  406. }
  407. if (!additive)
  408. SetSelection(okSelection);
  409. else
  410. AddSelection(okSelection);
  411. }
  412. void ListView::ClearSelection()
  413. {
  414. SetSelections(HashSet<unsigned>());
  415. UpdateSelectionEffect();
  416. }
  417. void ListView::SetHighlightMode(HighlightMode mode)
  418. {
  419. highlightMode_ = mode;
  420. UpdateSelectionEffect();
  421. }
  422. void ListView::SetMultiselect(bool enable)
  423. {
  424. multiselect_ = enable;
  425. }
  426. void ListView::SetHierarchyMode(bool enable)
  427. {
  428. hierarchyMode_ = enable;
  429. }
  430. void ListView::SetClearSelectionOnDefocus(bool enable)
  431. {
  432. clearSelectionOnDefocus_ = enable;
  433. }
  434. void ListView::SetDoubleClickInterval(float interval)
  435. {
  436. doubleClickInterval_ = interval;
  437. }
  438. void ListView::SetChildItemsVisible(unsigned index, bool enable)
  439. {
  440. unsigned numItems = GetNumItems();
  441. if (!hierarchyMode_ || index >= numItems)
  442. return;
  443. contentElement_->DisableLayoutUpdate();
  444. int baseIndent = GetItemIndent(GetItem(index));
  445. for (unsigned i = index + 1; i < numItems; ++i)
  446. {
  447. UIElement* item = GetItem(i);
  448. if (GetItemIndent(item) > baseIndent)
  449. item->SetVisible(enable);
  450. else
  451. break;
  452. }
  453. contentElement_->EnableLayoutUpdate();
  454. contentElement_->UpdateLayout();
  455. }
  456. void ListView::SetChildItemsVisible(bool enable)
  457. {
  458. unsigned numItems = GetNumItems();
  459. for (unsigned i = 0; i < numItems; ++i)
  460. {
  461. if (!GetItemIndent(GetItem(i)))
  462. SetChildItemsVisible(i, enable);
  463. }
  464. if (GetSelections().Size() == 1)
  465. EnsureItemVisibility(GetSelection());
  466. }
  467. void ListView::ToggleChildItemsVisible(unsigned index)
  468. {
  469. unsigned numItems = GetNumItems();
  470. if (!hierarchyMode_ || index >= numItems)
  471. return;
  472. contentElement_->DisableLayoutUpdate();
  473. int baseIndent = GetItemIndent(GetItem(index));
  474. bool firstChild = true;
  475. UIElement* prevItem = 0;
  476. for (unsigned i = index + 1; i < numItems; ++i)
  477. {
  478. UIElement* item = GetItem(i);
  479. if (GetItemIndent(item) > baseIndent)
  480. {
  481. if (firstChild)
  482. {
  483. item->SetVisible(!item->IsVisible());
  484. firstChild = false;
  485. }
  486. else
  487. item->SetVisible(prevItem->IsVisible());
  488. }
  489. else
  490. break;
  491. prevItem = item;
  492. }
  493. contentElement_->EnableLayoutUpdate();
  494. contentElement_->UpdateLayout();
  495. }
  496. unsigned ListView::GetNumItems() const
  497. {
  498. return contentElement_->GetNumChildren();
  499. }
  500. UIElement* ListView::GetItem(unsigned index) const
  501. {
  502. return contentElement_->GetChild(index);
  503. }
  504. PODVector<UIElement*> ListView::GetItems() const
  505. {
  506. PODVector<UIElement*> items;
  507. contentElement_->GetChildren(items);
  508. return items;
  509. }
  510. unsigned ListView::GetSelection() const
  511. {
  512. if (selections_.Empty())
  513. return M_MAX_UNSIGNED;
  514. else
  515. return GetSelections().Front();
  516. }
  517. PODVector<unsigned> ListView::GetSelections() const
  518. {
  519. PODVector<unsigned> sortedIndices;
  520. for (HashSet<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
  521. sortedIndices.Push(*i);
  522. Sort(sortedIndices.Begin(), sortedIndices.End());
  523. return sortedIndices;
  524. }
  525. UIElement* ListView::GetSelectedItem() const
  526. {
  527. return contentElement_->GetChild(GetSelection());
  528. }
  529. PODVector<UIElement*> ListView::GetSelectedItems() const
  530. {
  531. PODVector<UIElement*> ret;
  532. PODVector<unsigned> sortedIndices;
  533. for (HashSet<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
  534. sortedIndices.Push(*i);
  535. Sort(sortedIndices.Begin(), sortedIndices.End());
  536. for (PODVector<unsigned>::ConstIterator i = sortedIndices.Begin(); i != sortedIndices.End(); ++i)
  537. {
  538. UIElement* item = GetItem(*i);
  539. if (item)
  540. ret.Push(item);
  541. }
  542. return ret;
  543. }
  544. bool ListView::IsSelected(unsigned index) const
  545. {
  546. return selections_.Contains(index);
  547. }
  548. void ListView::UpdateSelectionEffect()
  549. {
  550. unsigned numItems = GetNumItems();
  551. for (unsigned i = 0; i < numItems; ++i)
  552. {
  553. UIElement* item = GetItem(i);
  554. if (highlightMode_ != HM_NEVER && selections_.Contains(i))
  555. item->SetSelected(HasFocus() || highlightMode_ == HM_ALWAYS);
  556. else
  557. item->SetSelected(false);
  558. }
  559. }
  560. void ListView::EnsureItemVisibility(unsigned index)
  561. {
  562. EnsureItemVisibility(GetItem(index));
  563. }
  564. void ListView::EnsureItemVisibility(UIElement* item)
  565. {
  566. if (!item || !item->IsVisible())
  567. return;
  568. IntVector2 currentOffset = item->GetScreenPosition() - scrollPanel_->GetScreenPosition() - contentElement_->GetPosition();
  569. IntVector2 newView = GetViewPosition();
  570. const IntRect& clipBorder = scrollPanel_->GetClipBorder();
  571. IntVector2 windowSize(scrollPanel_->GetWidth() - clipBorder.left_ - clipBorder.right_, scrollPanel_->GetHeight() -
  572. clipBorder.top_ - clipBorder.bottom_);
  573. if (currentOffset.y_ < 0)
  574. newView.y_ += currentOffset.y_;
  575. if (currentOffset.y_ + item->GetHeight() > windowSize.y_)
  576. newView.y_ += currentOffset.y_ + item->GetHeight() - windowSize.y_;
  577. SetViewPosition(newView);
  578. }
  579. void ListView::SetSelections(const HashSet<unsigned>& indices)
  580. {
  581. unsigned numItems = GetNumItems();
  582. // Remove first items that should no longer be selected
  583. for (HashSet<unsigned>::Iterator i = selections_.Begin(); i != selections_.End();)
  584. {
  585. unsigned index = *i;
  586. if (!indices.Contains(index))
  587. {
  588. HashSet<unsigned>::Iterator current = i++;
  589. selections_.Erase(current);
  590. using namespace ItemSelected;
  591. VariantMap eventData;
  592. eventData[P_ELEMENT] = (void*)this;
  593. eventData[P_SELECTION] = index;
  594. SendEvent(E_ITEMDESELECTED, eventData);
  595. }
  596. else
  597. ++i;
  598. }
  599. // Then add missing items
  600. for (HashSet<unsigned>::ConstIterator i = indices.Begin(); i != indices.End(); ++i)
  601. {
  602. unsigned index = *i;
  603. if (index < numItems)
  604. {
  605. // In singleselect mode, resend the event even for the same selection
  606. if (selections_.Find(index) == selections_.End() || !multiselect_)
  607. {
  608. selections_.Insert(*i);
  609. using namespace ItemSelected;
  610. VariantMap eventData;
  611. eventData[P_ELEMENT] = (void*)this;
  612. eventData[P_SELECTION] = *i;
  613. SendEvent(E_ITEMSELECTED, eventData);
  614. }
  615. }
  616. // If no multiselect enabled, allow setting only one item
  617. if (!multiselect_)
  618. break;
  619. }
  620. SendEvent(E_SELECTIONCHANGED);
  621. UpdateSelectionEffect();
  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. HashSet<unsigned> newSelections = selections_;
  662. if (i == first || i == last)
  663. {
  664. for (unsigned j = first; j <= last; ++j)
  665. newSelections.Insert(j);
  666. }
  667. else if (i < first)
  668. {
  669. for (unsigned j = i; j <= first; ++j)
  670. newSelections.Insert(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.Insert(j);
  678. }
  679. else
  680. {
  681. for (unsigned j = i; j <= last; ++j)
  682. newSelections.Insert(j);
  683. }
  684. }
  685. else if (i > last)
  686. {
  687. for (unsigned j = last; j <= i; ++j)
  688. newSelections.Insert(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. }