ListView.cpp 22 KB

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