ListView.cpp 30 KB

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