ListView.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 "CheckBox.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 char* highlightModes[] =
  35. {
  36. "Never",
  37. "Focus",
  38. "Always",
  39. ""
  40. };
  41. template<> HighlightMode Variant::Get<HighlightMode>() const
  42. {
  43. return (HighlightMode)GetInt();
  44. }
  45. static const ShortStringHash expandedHash("Expanded");
  46. bool GetItemExpanded(UIElement* item)
  47. {
  48. return item ? item->GetVar(expandedHash).GetBool() : false;
  49. }
  50. void SetItemExpanded(UIElement* item, bool enable)
  51. {
  52. item->SetVar(expandedHash, enable);
  53. }
  54. static const ShortStringHash hierarchyParentHash("HierarchyParent");
  55. bool GetItemHierarchyParent(UIElement* item)
  56. {
  57. return item ? item->GetVar(hierarchyParentHash).GetBool() : false;
  58. }
  59. void SetItemHierarchyParent(UIElement* item, bool enable)
  60. {
  61. item->SetVar(hierarchyParentHash, enable);
  62. }
  63. /// Hierarchy container (used by ListView internally when in hierarchy mode).
  64. class HierarchyContainer : public UIElement
  65. {
  66. OBJECT(HierarchyContainer);
  67. public:
  68. /// Construct.
  69. HierarchyContainer(Context* context, UIElement* overlayContainer) :
  70. UIElement(context),
  71. overlayContainer_(overlayContainer)
  72. {
  73. SubscribeToEvent(this, E_LAYOUTUPDATED, HANDLER(HierarchyContainer, HandleLayoutUpdated));
  74. SubscribeToEvent(overlayContainer->GetParent(), E_VIEWCHANGED, HANDLER(HierarchyContainer, HandleViewChanged));
  75. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(HierarchyContainer, HandleUIMouseClick));
  76. }
  77. /// Handle layout updated by adjusting the position of the overlays.
  78. void HandleLayoutUpdated(StringHash eventType, VariantMap& eventData)
  79. {
  80. // Adjust the container size for child clipping effect
  81. overlayContainer_->SetSize(GetParent()->GetSize());
  82. for (unsigned i = 0; i < children_.Size(); ++i)
  83. {
  84. const IntVector2& position = children_[i]->GetPosition();
  85. CheckBox* overlay = static_cast<CheckBox*>(overlayContainer_->GetChild(i));
  86. bool visible = children_[i]->IsVisible() && GetItemHierarchyParent(children_[i]);
  87. overlay->SetVisible(visible);
  88. if (visible)
  89. {
  90. overlay->SetPosition(position.x_, position.y_);
  91. overlay->SetChecked(GetItemExpanded(children_[i]));
  92. }
  93. }
  94. }
  95. /// Handle view changed by scrolling the overlays in tandem.
  96. void HandleViewChanged(StringHash eventType, VariantMap& eventData)
  97. {
  98. using namespace ViewChanged;
  99. int x = eventData[P_X].GetInt();
  100. int y = eventData[P_Y].GetInt();
  101. IntRect panelBorder = GetParent()->GetClipBorder();
  102. overlayContainer_->SetChildOffset(IntVector2(-x + panelBorder.left_, -y + panelBorder.top_));
  103. }
  104. /// Handle mouse click on overlays by toggling the expansion state of the corresponding item
  105. void HandleUIMouseClick(StringHash eventType, VariantMap& eventData)
  106. {
  107. using namespace UIMouseClick;
  108. UIElement* overlay = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  109. if (overlay)
  110. {
  111. const Vector<SharedPtr<UIElement> >& children = overlayContainer_->GetChildren();
  112. Vector<SharedPtr<UIElement> >::ConstIterator i = children.Find(SharedPtr<UIElement>(overlay));
  113. if (i != children.End())
  114. static_cast<ListView*>(overlayContainer_->GetParent())->ToggleExpand(i - children.Begin());
  115. }
  116. }
  117. /// Insert a child element into a specific position in the child list.
  118. void InsertChild(unsigned index, UIElement* element)
  119. {
  120. // Insert the overlay at the same index position to the overlay container
  121. CheckBox* overlay = new CheckBox(context_);
  122. overlay->SetStyle(overlayContainer_->GetDefaultStyle(), "ListViewHierarchyOverlay");
  123. overlay->SetIndent(element->GetIndent() - 1);
  124. overlay->SetFixedWidth(element->GetIndentWidth());
  125. overlayContainer_->InsertChild(index, overlay);
  126. // Then insert the element as child as per normal
  127. UIElement::InsertChild(index, element);
  128. }
  129. private:
  130. UIElement* overlayContainer_;
  131. };
  132. OBJECTTYPESTATIC(HierarchyContainer);
  133. OBJECTTYPESTATIC(ListView);
  134. ListView::ListView(Context* context) :
  135. ScrollView(context),
  136. highlightMode_(HM_FOCUS),
  137. multiselect_(false),
  138. hierarchyMode_(true), // Init to true here so that the setter below takes effect
  139. clearSelectionOnDefocus_(false),
  140. doubleClickInterval_(500),
  141. lastClickedItem_(M_MAX_UNSIGNED)
  142. {
  143. resizeContentWidth_ = true;
  144. // By default list view is set to non-hierarchy mode
  145. SetHierarchyMode(false);
  146. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(ListView, HandleUIMouseClick));
  147. SubscribeToEvent(E_FOCUSCHANGED, HANDLER(ListView, HandleFocusChanged));
  148. }
  149. ListView::~ListView()
  150. {
  151. }
  152. void ListView::RegisterObject(Context* context)
  153. {
  154. context->RegisterFactory<ListView>();
  155. ENUM_ACCESSOR_ATTRIBUTE(ListView, "Highlight Mode", GetHighlightMode, SetHighlightMode, HighlightMode, highlightModes, HM_FOCUS, AM_FILE);
  156. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Multiselect", GetMultiselect, SetMultiselect, bool, false, AM_FILE);
  157. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Hierarchy Mode", GetHierarchyMode, SetHierarchyMode, bool, false, AM_FILE);
  158. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Clear Sel. On Defocus", GetClearSelectionOnDefocus, SetClearSelectionOnDefocus, bool, false, AM_FILE);
  159. ACCESSOR_ATTRIBUTE(ListView, VAR_FLOAT, "Double Click Interval", GetDoubleClickInterval, SetDoubleClickInterval, float, 0.5f, AM_FILE);
  160. COPY_BASE_ATTRIBUTES(ListView, ScrollView);
  161. }
  162. void ListView::OnKey(int key, int buttons, int qualifiers)
  163. {
  164. // If no selection, can not move with keys
  165. unsigned numItems = GetNumItems();
  166. unsigned selection = GetSelection();
  167. // If either shift or ctrl held down, add to selection if multiselect enabled
  168. bool additive = multiselect_ && qualifiers & (QUAL_SHIFT | QUAL_CTRL);
  169. int delta = 0;
  170. int pageDirection = 1;
  171. if (selection != M_MAX_UNSIGNED && numItems)
  172. {
  173. switch (key)
  174. {
  175. case KEY_LEFT:
  176. case KEY_RIGHT:
  177. if (hierarchyMode_)
  178. {
  179. Expand(selection, key == KEY_RIGHT);
  180. return;
  181. }
  182. break;
  183. case KEY_RETURN:
  184. case KEY_RETURN2:
  185. case KEY_KP_ENTER:
  186. if (hierarchyMode_)
  187. {
  188. ToggleExpand(selection);
  189. return;
  190. }
  191. break;
  192. case KEY_UP:
  193. delta = -1;
  194. break;
  195. case KEY_DOWN:
  196. delta = 1;
  197. break;
  198. case KEY_PAGEUP:
  199. pageDirection = -1;
  200. // Fallthru
  201. case KEY_PAGEDOWN:
  202. {
  203. // Convert page step to pixels and see how many items have to be skipped to reach that many pixels
  204. int stepPixels = ((int)(pageStep_ * scrollPanel_->GetHeight())) - GetSelectedItem()->GetHeight();
  205. unsigned newSelection = selection;
  206. unsigned okSelection = selection;
  207. unsigned invisible = 0;
  208. while (newSelection < numItems)
  209. {
  210. UIElement* item = GetItem(newSelection);
  211. int height = 0;
  212. if (item->IsVisible())
  213. {
  214. height = item->GetHeight();
  215. okSelection = newSelection;
  216. }
  217. else
  218. ++invisible;
  219. if (stepPixels < height)
  220. break;
  221. stepPixels -= height;
  222. newSelection += pageDirection;
  223. }
  224. delta = okSelection - selection - pageDirection * invisible;
  225. }
  226. break;
  227. case KEY_HOME:
  228. delta = -(int)GetNumItems();
  229. break;
  230. case KEY_END:
  231. delta = GetNumItems();
  232. break;
  233. }
  234. }
  235. if (delta)
  236. {
  237. ChangeSelection(delta, additive);
  238. return;
  239. }
  240. using namespace UnhandledKey;
  241. VariantMap eventData;
  242. eventData[P_ELEMENT] = (void*)this;
  243. eventData[P_KEY] = key;
  244. eventData[P_BUTTONS] = buttons;
  245. eventData[P_QUALIFIERS] = qualifiers;
  246. SendEvent(E_UNHANDLEDKEY, eventData);
  247. }
  248. void ListView::OnResize()
  249. {
  250. ScrollView::OnResize();
  251. // When in hierarchy mode also need to resize the overlay container
  252. if (hierarchyMode_)
  253. overlayContainer_->SetSize(scrollPanel_->GetSize());
  254. }
  255. void ListView::AddItem(UIElement* item)
  256. {
  257. InsertItem(M_MAX_UNSIGNED, item);
  258. }
  259. void ListView::InsertItem(unsigned index, UIElement* item, UIElement* parentItem)
  260. {
  261. if (!item || item->GetParent() == contentElement_)
  262. return;
  263. // Enable input so that clicking the item can be detected
  264. item->SetActive(true);
  265. item->SetSelected(false);
  266. unsigned numItems = contentElement_->GetNumChildren();
  267. if (hierarchyMode_)
  268. {
  269. int baseIndent = 0;
  270. if (parentItem)
  271. {
  272. baseIndent = parentItem->GetIndent();
  273. SetItemHierarchyParent(parentItem, true);
  274. }
  275. item->SetIndent(baseIndent + 1);
  276. SetItemExpanded(item, item->IsVisible());
  277. // Adjust the index to ensure it is within the children index limit of the parent item
  278. unsigned indexLimit = FindItem(parentItem);
  279. if (index <= indexLimit)
  280. index = indexLimit + 1;
  281. else
  282. {
  283. while (++indexLimit < numItems)
  284. {
  285. if (contentElement_->GetChild(indexLimit)->GetIndent() <= baseIndent)
  286. break;
  287. }
  288. if (index > indexLimit)
  289. index = indexLimit;
  290. }
  291. // Use the 'overrided' version to insert the child item
  292. static_cast<HierarchyContainer*>(contentElement_.Get())->InsertChild(index, item);
  293. }
  294. else
  295. {
  296. if (index > numItems)
  297. index = numItems;
  298. contentElement_->InsertChild(index, item);
  299. }
  300. // If necessary, shift the following selections
  301. if (!selections_.Empty())
  302. {
  303. for (unsigned i = 0; i < selections_.Size(); ++i)
  304. {
  305. if (selections_[i] >= index)
  306. ++selections_[i];
  307. }
  308. UpdateSelectionEffect();
  309. }
  310. }
  311. void ListView::RemoveItem(UIElement* item, unsigned index)
  312. {
  313. unsigned numItems = GetNumItems();
  314. for (unsigned i = index; i < numItems; ++i)
  315. {
  316. if (GetItem(i) == item)
  317. {
  318. item->SetSelected(false);
  319. selections_.Remove(i);
  320. unsigned removed = 1;
  321. if (hierarchyMode_)
  322. {
  323. // Remove any child items in hierarchy mode
  324. if (GetItemHierarchyParent(item))
  325. {
  326. int baseIndent = item->GetIndent();
  327. for (unsigned j = i + 1; ; ++j)
  328. {
  329. UIElement* childItem = GetItem(i + 1);
  330. if (!childItem)
  331. break;
  332. if (childItem->GetIndent() > baseIndent)
  333. {
  334. childItem->SetSelected(false);
  335. selections_.Erase(j);
  336. contentElement_->RemoveChildAtIndex(i + 1);
  337. overlayContainer_->RemoveChildAtIndex(i + 1);
  338. ++removed;
  339. }
  340. else
  341. break;
  342. }
  343. }
  344. // Check if the parent of removed item still has other children
  345. if (i > 0)
  346. {
  347. int baseIndent = item->GetIndent();
  348. UIElement* prevKin = GetItem(i - 1); // Could be parent or sibling
  349. if (prevKin->GetIndent() < baseIndent)
  350. {
  351. UIElement* nextKin = GetItem(i + 1); // Could be sibling or parent-sibling or 0 if index out of bound
  352. if (!nextKin || nextKin->GetIndent() < baseIndent)
  353. {
  354. // If we reach here then the parent has no other children
  355. SetItemHierarchyParent(prevKin, false);
  356. }
  357. }
  358. }
  359. // Remove the overlay at the same index
  360. overlayContainer_->RemoveChildAtIndex(i);
  361. }
  362. // If necessary, shift the following selections
  363. if (!selections_.Empty())
  364. {
  365. for (unsigned j = 0; j < selections_.Size(); ++j)
  366. {
  367. if (selections_[j] > i)
  368. selections_[j] -= removed;
  369. }
  370. UpdateSelectionEffect();
  371. }
  372. contentElement_->RemoveChildAtIndex(i);
  373. break;
  374. }
  375. }
  376. }
  377. void ListView::RemoveItem(unsigned index)
  378. {
  379. RemoveItem(GetItem(index), index);
  380. }
  381. void ListView::RemoveAllItems()
  382. {
  383. contentElement_->DisableLayoutUpdate();
  384. ClearSelection();
  385. contentElement_->RemoveAllChildren();
  386. if (hierarchyMode_)
  387. overlayContainer_->RemoveAllChildren();
  388. contentElement_->EnableLayoutUpdate();
  389. contentElement_->UpdateLayout();
  390. }
  391. void ListView::SetSelection(unsigned index)
  392. {
  393. PODVector<unsigned> indices;
  394. indices.Push(index);
  395. SetSelections(indices);
  396. EnsureItemVisibility(index);
  397. }
  398. void ListView::SetSelections(const PODVector<unsigned>& indices)
  399. {
  400. unsigned numItems = GetNumItems();
  401. // Remove first items that should no longer be selected
  402. for (PODVector<unsigned>::Iterator i = selections_.Begin(); i != selections_.End();)
  403. {
  404. unsigned index = *i;
  405. if (!indices.Contains(index))
  406. {
  407. i = selections_.Erase(i);
  408. using namespace ItemSelected;
  409. VariantMap eventData;
  410. eventData[P_ELEMENT] = (void*)this;
  411. eventData[P_SELECTION] = index;
  412. SendEvent(E_ITEMDESELECTED, eventData);
  413. }
  414. else
  415. ++i;
  416. }
  417. bool added = false;
  418. // Then add missing items
  419. for (PODVector<unsigned>::ConstIterator i = indices.Begin(); i != indices.End(); ++i)
  420. {
  421. unsigned index = *i;
  422. if (index < numItems)
  423. {
  424. // In singleselect mode, resend the event even for the same selection
  425. bool duplicate = selections_.Contains(index);
  426. if (!duplicate || !multiselect_)
  427. {
  428. if (!duplicate)
  429. {
  430. selections_.Push(index);
  431. added = true;
  432. }
  433. using namespace ItemSelected;
  434. VariantMap eventData;
  435. eventData[P_ELEMENT] = (void*)this;
  436. eventData[P_SELECTION] = *i;
  437. SendEvent(E_ITEMSELECTED, eventData);
  438. }
  439. }
  440. // If no multiselect enabled, allow setting only one item
  441. if (!multiselect_)
  442. break;
  443. }
  444. // Re-sort selections if necessary
  445. if (added)
  446. Sort(selections_.Begin(), selections_.End());
  447. UpdateSelectionEffect();
  448. SendEvent(E_SELECTIONCHANGED);
  449. }
  450. void ListView::AddSelection(unsigned index)
  451. {
  452. if (!multiselect_)
  453. SetSelection(index);
  454. else
  455. {
  456. if (index >= GetNumItems())
  457. return;
  458. if (!selections_.Contains(index))
  459. {
  460. selections_.Push(index);
  461. using namespace ItemSelected;
  462. VariantMap eventData;
  463. eventData[P_ELEMENT] = (void*)this;
  464. eventData[P_SELECTION] = index;
  465. SendEvent(E_ITEMSELECTED, eventData);
  466. Sort(selections_.Begin(), selections_.End());
  467. }
  468. EnsureItemVisibility(index);
  469. UpdateSelectionEffect();
  470. SendEvent(E_SELECTIONCHANGED);
  471. }
  472. }
  473. void ListView::RemoveSelection(unsigned index)
  474. {
  475. if (index >= GetNumItems())
  476. return;
  477. if (selections_.Remove(index))
  478. {
  479. using namespace ItemSelected;
  480. VariantMap eventData;
  481. eventData[P_ELEMENT] = (void*)this;
  482. eventData[P_SELECTION] = index;
  483. SendEvent(E_ITEMDESELECTED, eventData);
  484. }
  485. EnsureItemVisibility(index);
  486. UpdateSelectionEffect();
  487. SendEvent(E_SELECTIONCHANGED);
  488. }
  489. void ListView::ToggleSelection(unsigned index)
  490. {
  491. unsigned numItems = GetNumItems();
  492. if (index >= numItems)
  493. return;
  494. if (selections_.Contains(index))
  495. RemoveSelection(index);
  496. else
  497. AddSelection(index);
  498. }
  499. void ListView::ChangeSelection(int delta, bool additive)
  500. {
  501. if (selections_.Empty())
  502. return;
  503. if (!multiselect_)
  504. additive = false;
  505. // If going downwards, use the last selection as a base. Otherwise use first
  506. unsigned selection = delta > 0 ? selections_.Back() : selections_.Front();
  507. int direction = delta > 0 ? 1 : -1;
  508. unsigned numItems = GetNumItems();
  509. unsigned newSelection = selection;
  510. unsigned okSelection = selection;
  511. PODVector<unsigned> indices = selections_;
  512. while (delta != 0)
  513. {
  514. newSelection += direction;
  515. if (newSelection >= numItems)
  516. break;
  517. UIElement* item = GetItem(newSelection);
  518. if (item->IsVisible())
  519. {
  520. indices.Push(okSelection = newSelection);
  521. delta -= direction;
  522. }
  523. }
  524. if (!additive)
  525. SetSelection(okSelection);
  526. else
  527. SetSelections(indices);
  528. }
  529. void ListView::ClearSelection()
  530. {
  531. SetSelections(PODVector<unsigned>());
  532. }
  533. void ListView::SetHighlightMode(HighlightMode mode)
  534. {
  535. highlightMode_ = mode;
  536. UpdateSelectionEffect();
  537. }
  538. void ListView::SetMultiselect(bool enable)
  539. {
  540. multiselect_ = enable;
  541. }
  542. void ListView::SetHierarchyMode(bool enable)
  543. {
  544. if (enable == hierarchyMode_)
  545. return;
  546. hierarchyMode_ = enable;
  547. UIElement* container;
  548. if (enable)
  549. {
  550. overlayContainer_ = CreateChild<UIElement>();
  551. overlayContainer_->SetInternal(true);
  552. overlayContainer_->SetLayoutMode(LM_FREE);
  553. overlayContainer_->SetSortChildren(false);
  554. overlayContainer_->SetClipChildren(true);
  555. container = new HierarchyContainer(context_, overlayContainer_);
  556. }
  557. else
  558. {
  559. if (overlayContainer_)
  560. {
  561. RemoveChild(overlayContainer_);
  562. overlayContainer_.Reset();
  563. }
  564. container = new UIElement(context_);
  565. }
  566. SetContentElement(container);
  567. container->SetInternal(true);
  568. container->SetActive(true);
  569. container->SetLayout(LM_VERTICAL);
  570. container->SetSortChildren(false);
  571. }
  572. void ListView::SetClearSelectionOnDefocus(bool enable)
  573. {
  574. if (enable != clearSelectionOnDefocus_)
  575. {
  576. clearSelectionOnDefocus_ = enable;
  577. if (clearSelectionOnDefocus_)
  578. {
  579. SubscribeToEvent(this, E_DEFOCUSED, HANDLER(ListView, HandleDefocused));
  580. if (!HasFocus())
  581. ClearSelection();
  582. }
  583. else
  584. UnsubscribeFromEvent(this, E_DEFOCUSED);
  585. }
  586. }
  587. void ListView::SetDoubleClickInterval(float interval)
  588. {
  589. doubleClickInterval_ = Max((int)(interval * 1000.0f), 0);
  590. }
  591. void ListView::Expand(unsigned index, bool enable, bool recursive)
  592. {
  593. if (!hierarchyMode_)
  594. return;
  595. unsigned numItems = GetNumItems();
  596. if (index >= numItems)
  597. return;
  598. UIElement* item = GetItem(index++);
  599. SetItemExpanded(item, enable);
  600. int baseIndent = item->GetIndent();
  601. PODVector<bool> expanded(baseIndent + 1);
  602. expanded[baseIndent] = enable;
  603. contentElement_->DisableLayoutUpdate();
  604. while (index < numItems)
  605. {
  606. item = GetItem(index++);
  607. int indent = item->GetIndent();
  608. if (indent <= baseIndent)
  609. break;
  610. // Propagate the state to children when it is recursive
  611. if (recursive)
  612. SetItemExpanded(item, enable);
  613. // Use the parent expanded flag to influence the visibility of its children
  614. bool visible = enable && expanded[indent - 1];
  615. item->SetVisible(visible);
  616. if (indent >= expanded.Size())
  617. expanded.Resize(indent + 1);
  618. expanded[indent] = visible && GetItemExpanded(item);
  619. }
  620. contentElement_->EnableLayoutUpdate();
  621. contentElement_->UpdateLayout();
  622. }
  623. void ListView::ToggleExpand(unsigned index, bool recursive)
  624. {
  625. if (!hierarchyMode_)
  626. return;
  627. unsigned numItems = GetNumItems();
  628. if (index >= numItems)
  629. return;
  630. UIElement* item = GetItem(index);
  631. Expand(index, !GetItemExpanded(item), recursive);
  632. }
  633. unsigned ListView::GetNumItems() const
  634. {
  635. return contentElement_->GetNumChildren();
  636. }
  637. UIElement* ListView::GetItem(unsigned index) const
  638. {
  639. return contentElement_->GetChild(index);
  640. }
  641. PODVector<UIElement*> ListView::GetItems() const
  642. {
  643. PODVector<UIElement*> items;
  644. contentElement_->GetChildren(items);
  645. return items;
  646. }
  647. unsigned ListView::FindItem(UIElement* item) const
  648. {
  649. const Vector<SharedPtr<UIElement> >& children = contentElement_->GetChildren();
  650. Vector<SharedPtr<UIElement> >::ConstIterator i = children.Find(SharedPtr<UIElement>(item));
  651. if (i != children.End())
  652. return i - children.Begin();
  653. else
  654. return M_MAX_UNSIGNED;
  655. }
  656. unsigned ListView::GetSelection() const
  657. {
  658. if (selections_.Empty())
  659. return M_MAX_UNSIGNED;
  660. else
  661. return GetSelections().Front();
  662. }
  663. UIElement* ListView::GetSelectedItem() const
  664. {
  665. return contentElement_->GetChild(GetSelection());
  666. }
  667. PODVector<UIElement*> ListView::GetSelectedItems() const
  668. {
  669. PODVector<UIElement*> ret;
  670. for (PODVector<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
  671. {
  672. UIElement* item = GetItem(*i);
  673. if (item)
  674. ret.Push(item);
  675. }
  676. return ret;
  677. }
  678. bool ListView::IsSelected(unsigned index) const
  679. {
  680. return selections_.Contains(index);
  681. }
  682. bool ListView::IsExpanded(unsigned index) const
  683. {
  684. return GetItemExpanded(contentElement_->GetChild(index));
  685. }
  686. float ListView::GetDoubleClickInterval() const
  687. {
  688. return (float)doubleClickInterval_ / 1000.0f;
  689. }
  690. void ListView::UpdateSelectionEffect()
  691. {
  692. unsigned numItems = GetNumItems();
  693. bool highlighted = highlightMode_ == HM_ALWAYS || HasFocus();
  694. for (unsigned i = 0; i < numItems; ++i)
  695. {
  696. UIElement* item = GetItem(i);
  697. if (highlightMode_ != HM_NEVER && selections_.Contains(i))
  698. item->SetSelected(highlighted);
  699. else
  700. item->SetSelected(false);
  701. }
  702. }
  703. void ListView::EnsureItemVisibility(unsigned index)
  704. {
  705. EnsureItemVisibility(GetItem(index));
  706. }
  707. void ListView::EnsureItemVisibility(UIElement* item)
  708. {
  709. if (!item || !item->IsVisible())
  710. return;
  711. IntVector2 newView = GetViewPosition();
  712. IntVector2 currentOffset = item->GetPosition() - newView;
  713. const IntRect& clipBorder = scrollPanel_->GetClipBorder();
  714. IntVector2 windowSize(scrollPanel_->GetWidth() - clipBorder.left_ - clipBorder.right_, scrollPanel_->GetHeight() -
  715. clipBorder.top_ - clipBorder.bottom_);
  716. if (currentOffset.y_ < 0)
  717. newView.y_ += currentOffset.y_;
  718. if (currentOffset.y_ + item->GetHeight() > windowSize.y_)
  719. newView.y_ += currentOffset.y_ + item->GetHeight() - windowSize.y_;
  720. SetViewPosition(newView);
  721. }
  722. void ListView::HandleUIMouseClick(StringHash eventType, VariantMap& eventData)
  723. {
  724. if (eventData[UIMouseClick::P_BUTTON].GetInt() != MOUSEB_LEFT)
  725. return;
  726. int qualifiers = eventData[UIMouseClick::P_QUALIFIERS].GetInt();
  727. UIElement* element = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  728. unsigned numItems = GetNumItems();
  729. for (unsigned i = 0; i < numItems; ++i)
  730. {
  731. if (element == GetItem(i))
  732. {
  733. // Check doubleclick
  734. bool isDoubleClick = false;
  735. if (!multiselect_ || !qualifiers)
  736. {
  737. if (!(isDoubleClick = doubleClickTimer_.GetMSec(true) < doubleClickInterval_ && lastClickedItem_ == i))
  738. lastClickedItem_ = i;
  739. SetSelection(i);
  740. }
  741. // Check multiselect with shift & ctrl
  742. if (multiselect_)
  743. {
  744. if (qualifiers & QUAL_SHIFT)
  745. {
  746. if (selections_.Empty())
  747. SetSelection(i);
  748. else
  749. {
  750. unsigned first = selections_.Front();
  751. unsigned last = selections_.Back();
  752. PODVector<unsigned> newSelections = selections_;
  753. if (i == first || i == last)
  754. {
  755. for (unsigned j = first; j <= last; ++j)
  756. newSelections.Push(j);
  757. }
  758. else if (i < first)
  759. {
  760. for (unsigned j = i; j <= first; ++j)
  761. newSelections.Push(j);
  762. }
  763. else if (i < last)
  764. {
  765. if ((abs((int)i - (int)first)) <= (abs((int)i - (int)last)))
  766. {
  767. for (unsigned j = first; j <= i; ++j)
  768. newSelections.Push(j);
  769. }
  770. else
  771. {
  772. for (unsigned j = i; j <= last; ++j)
  773. newSelections.Push(j);
  774. }
  775. }
  776. else if (i > last)
  777. {
  778. for (unsigned j = last; j <= i; ++j)
  779. newSelections.Push(j);
  780. }
  781. SetSelections(newSelections);
  782. }
  783. }
  784. else if (qualifiers & QUAL_CTRL)
  785. ToggleSelection(i);
  786. }
  787. if (isDoubleClick)
  788. {
  789. VariantMap eventData;
  790. eventData[ItemDoubleClicked::P_ELEMENT] = (void*)this;
  791. eventData[ItemDoubleClicked::P_SELECTION] = i;
  792. SendEvent(E_ITEMDOUBLECLICKED, eventData);
  793. }
  794. return;
  795. }
  796. }
  797. }
  798. void ListView::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  799. {
  800. using namespace FocusChanged;
  801. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  802. while (element)
  803. {
  804. // If the focused element or its parent is in the list, scroll the list to make the item visible
  805. UIElement* parent = element->GetParent();
  806. if (parent == contentElement_)
  807. {
  808. EnsureItemVisibility(element);
  809. return;
  810. }
  811. element = parent;
  812. }
  813. }
  814. void ListView::HandleDefocused(StringHash eventType, VariantMap& eventData)
  815. {
  816. ClearSelection();
  817. }
  818. }