ListView.cpp 24 KB

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