ListView.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "BorderImage.h"
  24. #include "Context.h"
  25. #include "InputEvents.h"
  26. #include "ListView.h"
  27. #include "Log.h"
  28. #include "Sort.h"
  29. #include "StringUtils.h"
  30. #include "UIEvents.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. static const ShortStringHash indentHash("Indent");
  35. static const char* highlightModes[] =
  36. {
  37. "Never",
  38. "Focus",
  39. "Always",
  40. ""
  41. };
  42. template<> HighlightMode Variant::Get<HighlightMode>() const
  43. {
  44. return (HighlightMode)GetInt();
  45. }
  46. int GetItemIndent(UIElement* item)
  47. {
  48. if (!item)
  49. return 0;
  50. return item->GetVar(indentHash).GetInt();
  51. }
  52. OBJECTTYPESTATIC(ListView);
  53. ListView::ListView(Context* context) :
  54. ScrollView(context),
  55. highlightMode_(HM_FOCUS),
  56. multiselect_(false),
  57. hierarchyMode_(false),
  58. clearSelectionOnDefocus_(false),
  59. doubleClickInterval_(500),
  60. lastClickedItem_(M_MAX_UNSIGNED)
  61. {
  62. resizeContentWidth_ = true;
  63. UIElement* container = new UIElement(context_);
  64. container->SetInternal(true);
  65. container->SetActive(true);
  66. container->SetLayout(LM_VERTICAL);
  67. container->SetSortChildren(false);
  68. SetContentElement(container);
  69. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(ListView, HandleUIMouseClick));
  70. SubscribeToEvent(E_FOCUSCHANGED, HANDLER(ListView, HandleFocusChanged));
  71. }
  72. ListView::~ListView()
  73. {
  74. }
  75. void ListView::RegisterObject(Context* context)
  76. {
  77. context->RegisterFactory<ListView>();
  78. ENUM_ACCESSOR_ATTRIBUTE(ListView, "Highlight Mode", GetHighlightMode, SetHighlightMode, HighlightMode, highlightModes, HM_FOCUS, AM_FILE);
  79. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Multiselect", GetMultiselect, SetMultiselect, bool, false, AM_FILE);
  80. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Hierarchy Mode", GetHierarchyMode, SetHierarchyMode, bool, false, AM_FILE);
  81. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Clear Sel. On Defocus", GetClearSelectionOnDefocus, SetClearSelectionOnDefocus, bool, false, AM_FILE);
  82. ACCESSOR_ATTRIBUTE(ListView, VAR_FLOAT, "Double Click Interval", GetDoubleClickInterval, SetDoubleClickInterval, float, 0.5f, AM_FILE);
  83. COPY_BASE_ATTRIBUTES(ListView, ScrollView);
  84. }
  85. void ListView::OnKey(int key, int buttons, int qualifiers)
  86. {
  87. // If no selection, can not move with keys
  88. unsigned numItems = GetNumItems();
  89. unsigned selection = GetSelection();
  90. // If either shift or ctrl held down, add to selection if multiselect enabled
  91. bool additive = multiselect_ && qualifiers & (QUAL_SHIFT | QUAL_CTRL);
  92. int delta = 0;
  93. int pageDirection = 1;
  94. if (selection != M_MAX_UNSIGNED && numItems)
  95. {
  96. switch (key)
  97. {
  98. case KEY_LEFT:
  99. case KEY_RIGHT:
  100. if (hierarchyMode_)
  101. {
  102. SetChildItemsVisible(selection, key == KEY_RIGHT);
  103. return;
  104. }
  105. break;
  106. case KEY_RETURN:
  107. case KEY_RETURN2:
  108. case KEY_KP_ENTER:
  109. if (hierarchyMode_)
  110. {
  111. ToggleChildItemsVisible(selection);
  112. return;
  113. }
  114. break;
  115. case KEY_UP:
  116. delta = -1;
  117. break;
  118. case KEY_DOWN:
  119. delta = 1;
  120. break;
  121. case KEY_PAGEUP:
  122. pageDirection = -1;
  123. // Fallthru
  124. case KEY_PAGEDOWN:
  125. {
  126. // Convert page step to pixels and see how many items have to be skipped to reach that many pixels
  127. int stepPixels = ((int)(pageStep_ * scrollPanel_->GetHeight())) - GetSelectedItem()->GetHeight();
  128. unsigned newSelection = selection;
  129. unsigned okSelection = selection;
  130. unsigned invisible = 0;
  131. while (newSelection < numItems)
  132. {
  133. UIElement* item = GetItem(newSelection);
  134. int height = 0;
  135. if (item->IsVisible())
  136. {
  137. height = item->GetHeight();
  138. okSelection = newSelection;
  139. }
  140. else
  141. ++invisible;
  142. if (stepPixels < height)
  143. break;
  144. stepPixels -= height;
  145. newSelection += pageDirection;
  146. }
  147. delta = okSelection - selection - pageDirection * invisible;
  148. }
  149. break;
  150. case KEY_HOME:
  151. delta = -(int)GetNumItems();
  152. break;
  153. case KEY_END:
  154. delta = GetNumItems();
  155. break;
  156. }
  157. }
  158. if (delta)
  159. {
  160. ChangeSelection(delta, additive);
  161. return;
  162. }
  163. using namespace UnhandledKey;
  164. VariantMap eventData;
  165. eventData[P_ELEMENT] = (void*)this;
  166. eventData[P_KEY] = key;
  167. eventData[P_BUTTONS] = buttons;
  168. eventData[P_QUALIFIERS] = qualifiers;
  169. SendEvent(E_UNHANDLEDKEY, eventData);
  170. }
  171. void ListView::AddItem(UIElement* item)
  172. {
  173. InsertItem(contentElement_->GetNumChildren(), item);
  174. }
  175. void ListView::InsertItem(unsigned index, UIElement* item)
  176. {
  177. if (!item || item->GetParent() == contentElement_)
  178. return;
  179. // Enable input so that clicking the item can be detected
  180. item->SetActive(true);
  181. item->SetSelected(false);
  182. contentElement_->InsertChild(index, item);
  183. // If necessary, shift the following selections
  184. if (!selections_.Empty())
  185. {
  186. for (unsigned i = 0; i < selections_.Size(); ++i)
  187. {
  188. if (selections_[i] >= index)
  189. ++selections_[i];
  190. }
  191. UpdateSelectionEffect();
  192. }
  193. }
  194. void ListView::RemoveItem(UIElement* item, unsigned index)
  195. {
  196. unsigned numItems = GetNumItems();
  197. for (unsigned i = index; i < numItems; ++i)
  198. {
  199. if (GetItem(i) == item)
  200. {
  201. item->SetSelected(false);
  202. selections_.Remove(i);
  203. // Remove any child items in hierarchy mode
  204. unsigned removed = 1;
  205. if (hierarchyMode_)
  206. {
  207. int baseIndent = GetItemIndent(item);
  208. for (unsigned j = i + 1; ; ++j)
  209. {
  210. UIElement* childItem = GetItem(i + 1);
  211. if (!childItem)
  212. break;
  213. if (GetItemIndent(childItem) > baseIndent)
  214. {
  215. childItem->SetSelected(false);
  216. selections_.Erase(j);
  217. contentElement_->RemoveChild(childItem, i + 1);
  218. ++removed;
  219. }
  220. else
  221. break;
  222. }
  223. }
  224. // If necessary, shift the following selections
  225. if (!selections_.Empty())
  226. {
  227. for (unsigned j = 0; j < selections_.Size(); ++j)
  228. {
  229. if (selections_[j] > i)
  230. selections_[j] -= removed;
  231. }
  232. UpdateSelectionEffect();
  233. }
  234. contentElement_->RemoveChild(item, i);
  235. break;
  236. }
  237. }
  238. }
  239. void ListView::RemoveItem(unsigned index)
  240. {
  241. RemoveItem(GetItem(index), index);
  242. }
  243. void ListView::RemoveAllItems()
  244. {
  245. contentElement_->DisableLayoutUpdate();
  246. ClearSelection();
  247. contentElement_->RemoveAllChildren();
  248. contentElement_->EnableLayoutUpdate();
  249. contentElement_->UpdateLayout();
  250. }
  251. void ListView::SetSelection(unsigned index)
  252. {
  253. PODVector<unsigned> indices;
  254. indices.Push(index);
  255. SetSelections(indices);
  256. EnsureItemVisibility(index);
  257. }
  258. void ListView::SetSelections(const PODVector<unsigned>& indices)
  259. {
  260. unsigned numItems = GetNumItems();
  261. // Remove first items that should no longer be selected
  262. for (PODVector<unsigned>::Iterator i = selections_.Begin(); i != selections_.End();)
  263. {
  264. unsigned index = *i;
  265. if (!indices.Contains(index))
  266. {
  267. i = selections_.Erase(i);
  268. using namespace ItemSelected;
  269. VariantMap eventData;
  270. eventData[P_ELEMENT] = (void*)this;
  271. eventData[P_SELECTION] = index;
  272. SendEvent(E_ITEMDESELECTED, eventData);
  273. }
  274. else
  275. ++i;
  276. }
  277. bool added = false;
  278. // Then add missing items
  279. for (PODVector<unsigned>::ConstIterator i = indices.Begin(); i != indices.End(); ++i)
  280. {
  281. unsigned index = *i;
  282. if (index < numItems)
  283. {
  284. // In singleselect mode, resend the event even for the same selection
  285. bool duplicate = selections_.Contains(index);
  286. if (!duplicate || !multiselect_)
  287. {
  288. if (!duplicate)
  289. {
  290. selections_.Push(index);
  291. added = true;
  292. }
  293. using namespace ItemSelected;
  294. VariantMap eventData;
  295. eventData[P_ELEMENT] = (void*)this;
  296. eventData[P_SELECTION] = *i;
  297. SendEvent(E_ITEMSELECTED, eventData);
  298. }
  299. }
  300. // If no multiselect enabled, allow setting only one item
  301. if (!multiselect_)
  302. break;
  303. }
  304. // Re-sort selections if necessary
  305. if (added)
  306. Sort(selections_.Begin(), selections_.End());
  307. UpdateSelectionEffect();
  308. SendEvent(E_SELECTIONCHANGED);
  309. }
  310. void ListView::AddSelection(unsigned index)
  311. {
  312. if (!multiselect_)
  313. SetSelection(index);
  314. else
  315. {
  316. if (index >= GetNumItems())
  317. return;
  318. if (!selections_.Contains(index))
  319. {
  320. selections_.Push(index);
  321. using namespace ItemSelected;
  322. VariantMap eventData;
  323. eventData[P_ELEMENT] = (void*)this;
  324. eventData[P_SELECTION] = index;
  325. SendEvent(E_ITEMSELECTED, eventData);
  326. Sort(selections_.Begin(), selections_.End());
  327. }
  328. EnsureItemVisibility(index);
  329. UpdateSelectionEffect();
  330. SendEvent(E_SELECTIONCHANGED);
  331. }
  332. }
  333. void ListView::RemoveSelection(unsigned index)
  334. {
  335. if (index >= GetNumItems())
  336. return;
  337. if (selections_.Remove(index))
  338. {
  339. using namespace ItemSelected;
  340. VariantMap eventData;
  341. eventData[P_ELEMENT] = (void*)this;
  342. eventData[P_SELECTION] = index;
  343. SendEvent(E_ITEMDESELECTED, eventData);
  344. }
  345. EnsureItemVisibility(index);
  346. UpdateSelectionEffect();
  347. SendEvent(E_SELECTIONCHANGED);
  348. }
  349. void ListView::ToggleSelection(unsigned index)
  350. {
  351. unsigned numItems = GetNumItems();
  352. if (index >= numItems)
  353. return;
  354. if (selections_.Contains(index))
  355. RemoveSelection(index);
  356. else
  357. AddSelection(index);
  358. }
  359. void ListView::ChangeSelection(int delta, bool additive)
  360. {
  361. if (selections_.Empty())
  362. return;
  363. if (!multiselect_)
  364. additive = false;
  365. // If going downwards, use the last selection as a base. Otherwise use first
  366. unsigned selection = delta > 0 ? selections_.Back() : selections_.Front();
  367. int direction = delta > 0 ? 1 : -1;
  368. unsigned numItems = GetNumItems();
  369. unsigned newSelection = selection;
  370. unsigned okSelection = selection;
  371. PODVector<unsigned> indices = selections_;
  372. while (delta != 0)
  373. {
  374. newSelection += direction;
  375. if (newSelection >= numItems)
  376. break;
  377. UIElement* item = GetItem(newSelection);
  378. if (item->IsVisible())
  379. {
  380. indices.Push(okSelection = newSelection);
  381. delta -= direction;
  382. }
  383. }
  384. if (!additive)
  385. SetSelection(okSelection);
  386. else
  387. SetSelections(indices);
  388. }
  389. void ListView::ClearSelection()
  390. {
  391. SetSelections(PODVector<unsigned>());
  392. }
  393. void ListView::SetHighlightMode(HighlightMode mode)
  394. {
  395. highlightMode_ = mode;
  396. UpdateSelectionEffect();
  397. }
  398. void ListView::SetMultiselect(bool enable)
  399. {
  400. multiselect_ = enable;
  401. }
  402. void ListView::SetHierarchyMode(bool enable)
  403. {
  404. hierarchyMode_ = enable;
  405. }
  406. void ListView::SetClearSelectionOnDefocus(bool enable)
  407. {
  408. if (enable != clearSelectionOnDefocus_)
  409. {
  410. clearSelectionOnDefocus_ = enable;
  411. if (clearSelectionOnDefocus_)
  412. {
  413. SubscribeToEvent(this, E_DEFOCUSED, HANDLER(ListView, HandleDefocused));
  414. if (!HasFocus())
  415. ClearSelection();
  416. }
  417. else
  418. UnsubscribeFromEvent(this, E_DEFOCUSED);
  419. }
  420. }
  421. void ListView::SetDoubleClickInterval(float interval)
  422. {
  423. doubleClickInterval_ = Max((int)(interval * 1000.0f), 0);
  424. }
  425. void ListView::SetChildItemsVisible(unsigned index, bool enable)
  426. {
  427. unsigned numItems = GetNumItems();
  428. if (!hierarchyMode_ || index >= numItems)
  429. return;
  430. contentElement_->DisableLayoutUpdate();
  431. int baseIndent = GetItemIndent(GetItem(index));
  432. for (unsigned i = index + 1; i < numItems; ++i)
  433. {
  434. UIElement* item = GetItem(i);
  435. if (GetItemIndent(item) > baseIndent)
  436. item->SetVisible(enable);
  437. else
  438. break;
  439. }
  440. contentElement_->EnableLayoutUpdate();
  441. contentElement_->UpdateLayout();
  442. }
  443. void ListView::SetChildItemsVisible(bool enable)
  444. {
  445. unsigned numItems = GetNumItems();
  446. for (unsigned i = 0; i < numItems; ++i)
  447. {
  448. if (!GetItemIndent(GetItem(i)))
  449. SetChildItemsVisible(i, enable);
  450. }
  451. unsigned firstSelected = GetSelection();
  452. if (firstSelected != M_MAX_UNSIGNED)
  453. EnsureItemVisibility(firstSelected);
  454. }
  455. void ListView::ToggleChildItemsVisible(unsigned index)
  456. {
  457. unsigned numItems = GetNumItems();
  458. if (!hierarchyMode_ || index >= numItems)
  459. return;
  460. contentElement_->DisableLayoutUpdate();
  461. int baseIndent = GetItemIndent(GetItem(index));
  462. bool firstChild = true;
  463. bool firstChildVisible;
  464. for (unsigned i = index + 1; i < numItems; ++i)
  465. {
  466. UIElement* item = GetItem(i);
  467. if (GetItemIndent(item) > baseIndent)
  468. {
  469. if (firstChild)
  470. {
  471. item->SetVisible(firstChildVisible = !item->IsVisible());
  472. firstChild = false;
  473. }
  474. else
  475. item->SetVisible(firstChildVisible);
  476. }
  477. else
  478. break;
  479. }
  480. contentElement_->EnableLayoutUpdate();
  481. contentElement_->UpdateLayout();
  482. }
  483. unsigned ListView::GetNumItems() const
  484. {
  485. return contentElement_->GetNumChildren();
  486. }
  487. UIElement* ListView::GetItem(unsigned index) const
  488. {
  489. return contentElement_->GetChild(index);
  490. }
  491. PODVector<UIElement*> ListView::GetItems() const
  492. {
  493. PODVector<UIElement*> items;
  494. contentElement_->GetChildren(items);
  495. return items;
  496. }
  497. unsigned ListView::GetSelection() const
  498. {
  499. if (selections_.Empty())
  500. return M_MAX_UNSIGNED;
  501. else
  502. return GetSelections().Front();
  503. }
  504. UIElement* ListView::GetSelectedItem() const
  505. {
  506. return contentElement_->GetChild(GetSelection());
  507. }
  508. PODVector<UIElement*> ListView::GetSelectedItems() const
  509. {
  510. PODVector<UIElement*> ret;
  511. for (PODVector<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
  512. {
  513. UIElement* item = GetItem(*i);
  514. if (item)
  515. ret.Push(item);
  516. }
  517. return ret;
  518. }
  519. bool ListView::IsSelected(unsigned index) const
  520. {
  521. return selections_.Contains(index);
  522. }
  523. float ListView::GetDoubleClickInterval() const
  524. {
  525. return (float)doubleClickInterval_ / 1000.0f;
  526. }
  527. void ListView::UpdateSelectionEffect()
  528. {
  529. unsigned numItems = GetNumItems();
  530. bool highlighted = highlightMode_ == HM_ALWAYS || HasFocus();
  531. for (unsigned i = 0; i < numItems; ++i)
  532. {
  533. UIElement* item = GetItem(i);
  534. if (highlightMode_ != HM_NEVER && selections_.Contains(i))
  535. item->SetSelected(highlighted);
  536. else
  537. item->SetSelected(false);
  538. }
  539. }
  540. void ListView::EnsureItemVisibility(unsigned index)
  541. {
  542. EnsureItemVisibility(GetItem(index));
  543. }
  544. void ListView::EnsureItemVisibility(UIElement* item)
  545. {
  546. if (!item || !item->IsVisible())
  547. return;
  548. IntVector2 newView = GetViewPosition();
  549. IntVector2 currentOffset = item->GetPosition() - newView;
  550. const IntRect& clipBorder = scrollPanel_->GetClipBorder();
  551. IntVector2 windowSize(scrollPanel_->GetWidth() - clipBorder.left_ - clipBorder.right_, scrollPanel_->GetHeight() -
  552. clipBorder.top_ - clipBorder.bottom_);
  553. if (currentOffset.y_ < 0)
  554. newView.y_ += currentOffset.y_;
  555. if (currentOffset.y_ + item->GetHeight() > windowSize.y_)
  556. newView.y_ += currentOffset.y_ + item->GetHeight() - windowSize.y_;
  557. SetViewPosition(newView);
  558. }
  559. void ListView::HandleUIMouseClick(StringHash eventType, VariantMap& eventData)
  560. {
  561. if (eventData[UIMouseClick::P_BUTTON].GetInt() != MOUSEB_LEFT)
  562. return;
  563. int qualifiers = eventData[UIMouseClick::P_QUALIFIERS].GetInt();
  564. UIElement* element = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  565. unsigned numItems = GetNumItems();
  566. for (unsigned i = 0; i < numItems; ++i)
  567. {
  568. if (element == GetItem(i))
  569. {
  570. // Check doubleclick
  571. bool isDoubleClick = false;
  572. if (!multiselect_ || !qualifiers)
  573. {
  574. if (!(isDoubleClick = doubleClickTimer_.GetMSec(true) < doubleClickInterval_ && lastClickedItem_ == i))
  575. lastClickedItem_ = i;
  576. SetSelection(i);
  577. }
  578. // Check multiselect with shift & ctrl
  579. if (multiselect_)
  580. {
  581. if (qualifiers & QUAL_SHIFT)
  582. {
  583. if (selections_.Empty())
  584. SetSelection(i);
  585. else
  586. {
  587. unsigned first = selections_.Front();
  588. unsigned last = selections_.Back();
  589. PODVector<unsigned> newSelections = selections_;
  590. if (i == first || i == last)
  591. {
  592. for (unsigned j = first; j <= last; ++j)
  593. newSelections.Push(j);
  594. }
  595. else if (i < first)
  596. {
  597. for (unsigned j = i; j <= first; ++j)
  598. newSelections.Push(j);
  599. }
  600. else if (i < last)
  601. {
  602. if ((abs((int)i - (int)first)) <= (abs((int)i - (int)last)))
  603. {
  604. for (unsigned j = first; j <= i; ++j)
  605. newSelections.Push(j);
  606. }
  607. else
  608. {
  609. for (unsigned j = i; j <= last; ++j)
  610. newSelections.Push(j);
  611. }
  612. }
  613. else if (i > last)
  614. {
  615. for (unsigned j = last; j <= i; ++j)
  616. newSelections.Push(j);
  617. }
  618. SetSelections(newSelections);
  619. }
  620. }
  621. else if (qualifiers & QUAL_CTRL)
  622. ToggleSelection(i);
  623. }
  624. if (isDoubleClick)
  625. {
  626. VariantMap eventData;
  627. eventData[ItemDoubleClicked::P_ELEMENT] = (void*)this;
  628. eventData[ItemDoubleClicked::P_SELECTION] = i;
  629. SendEvent(E_ITEMDOUBLECLICKED, eventData);
  630. }
  631. return;
  632. }
  633. }
  634. }
  635. void ListView::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  636. {
  637. using namespace FocusChanged;
  638. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  639. while (element)
  640. {
  641. // If the focused element or its parent is in the list, scroll the list to make the item visible
  642. UIElement* parent = element->GetParent();
  643. if (parent == contentElement_)
  644. {
  645. EnsureItemVisibility(element);
  646. return;
  647. }
  648. element = parent;
  649. }
  650. }
  651. void ListView::HandleDefocused(StringHash eventType, VariantMap& eventData)
  652. {
  653. ClearSelection();
  654. }
  655. }