ListView.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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. COPY_BASE_ATTRIBUTES(ListView, ScrollView);
  159. ENUM_ACCESSOR_ATTRIBUTE(ListView, "Highlight Mode", GetHighlightMode, SetHighlightMode, HighlightMode, highlightModes, HM_FOCUS, AM_FILE);
  160. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Multiselect", GetMultiselect, SetMultiselect, bool, false, AM_FILE);
  161. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Hierarchy Mode", GetHierarchyMode, SetHierarchyMode, bool, false, AM_FILE);
  162. ACCESSOR_ATTRIBUTE(ListView, VAR_INT, "Base Indent", GetBaseIndent, SetBaseIndent, int, 0, AM_FILE);
  163. ACCESSOR_ATTRIBUTE(ListView, VAR_BOOL, "Clear Sel. On Defocus", GetClearSelectionOnDefocus, SetClearSelectionOnDefocus, bool, false, AM_FILE);
  164. ACCESSOR_ATTRIBUTE(ListView, VAR_FLOAT, "Double Click Interval", GetDoubleClickInterval, SetDoubleClickInterval, float, 0.5f, AM_FILE);
  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. if (!item)
  318. return;
  319. unsigned numItems = GetNumItems();
  320. for (unsigned i = index; i < numItems; ++i)
  321. {
  322. if (GetItem(i) == item)
  323. {
  324. item->SetSelected(false);
  325. selections_.Remove(i);
  326. unsigned removed = 1;
  327. if (hierarchyMode_)
  328. {
  329. // Remove any child items in hierarchy mode
  330. if (GetItemHierarchyParent(item))
  331. {
  332. int baseIndent = item->GetIndent();
  333. for (unsigned j = i + 1; ; ++j)
  334. {
  335. UIElement* childItem = GetItem(i + 1);
  336. if (!childItem)
  337. break;
  338. if (childItem->GetIndent() > baseIndent)
  339. {
  340. childItem->SetSelected(false);
  341. selections_.Erase(j);
  342. contentElement_->RemoveChildAtIndex(i + 1);
  343. overlayContainer_->RemoveChildAtIndex(i + 1);
  344. ++removed;
  345. }
  346. else
  347. break;
  348. }
  349. }
  350. // Check if the parent of removed item still has other children
  351. if (i > 0)
  352. {
  353. int baseIndent = item->GetIndent();
  354. UIElement* prevKin = GetItem(i - 1); // Could be parent or sibling
  355. if (prevKin->GetIndent() < baseIndent)
  356. {
  357. UIElement* nextKin = GetItem(i + 1); // Could be sibling or parent-sibling or 0 if index out of bound
  358. if (!nextKin || nextKin->GetIndent() < baseIndent)
  359. {
  360. // If we reach here then the parent has no other children
  361. SetItemHierarchyParent(prevKin, false);
  362. }
  363. }
  364. }
  365. // Remove the overlay at the same index
  366. overlayContainer_->RemoveChildAtIndex(i);
  367. }
  368. // If necessary, shift the following selections
  369. if (!selections_.Empty())
  370. {
  371. for (unsigned j = 0; j < selections_.Size(); ++j)
  372. {
  373. if (selections_[j] > i)
  374. selections_[j] -= removed;
  375. }
  376. UpdateSelectionEffect();
  377. }
  378. contentElement_->RemoveChildAtIndex(i);
  379. break;
  380. }
  381. }
  382. }
  383. void ListView::RemoveItem(unsigned index)
  384. {
  385. RemoveItem(GetItem(index), index);
  386. }
  387. void ListView::RemoveAllItems()
  388. {
  389. contentElement_->DisableLayoutUpdate();
  390. ClearSelection();
  391. contentElement_->RemoveAllChildren();
  392. if (hierarchyMode_)
  393. overlayContainer_->RemoveAllChildren();
  394. contentElement_->EnableLayoutUpdate();
  395. contentElement_->UpdateLayout();
  396. }
  397. void ListView::SetSelection(unsigned index)
  398. {
  399. PODVector<unsigned> indices;
  400. indices.Push(index);
  401. SetSelections(indices);
  402. EnsureItemVisibility(index);
  403. }
  404. void ListView::SetSelections(const PODVector<unsigned>& indices)
  405. {
  406. // Make a weak pointer to self to check for destruction as a response to events
  407. WeakPtr<ListView> self(this);
  408. unsigned numItems = GetNumItems();
  409. // Remove first items that should no longer be selected
  410. for (PODVector<unsigned>::Iterator i = selections_.Begin(); i != selections_.End();)
  411. {
  412. unsigned index = *i;
  413. if (!indices.Contains(index))
  414. {
  415. i = selections_.Erase(i);
  416. using namespace ItemSelected;
  417. VariantMap eventData;
  418. eventData[P_ELEMENT] = (void*)this;
  419. eventData[P_SELECTION] = index;
  420. SendEvent(E_ITEMDESELECTED, eventData);
  421. if (self.Expired())
  422. return;
  423. }
  424. else
  425. ++i;
  426. }
  427. bool added = false;
  428. // Then add missing items
  429. for (PODVector<unsigned>::ConstIterator i = indices.Begin(); i != indices.End(); ++i)
  430. {
  431. unsigned index = *i;
  432. if (index < numItems)
  433. {
  434. // In singleselect mode, resend the event even for the same selection
  435. bool duplicate = selections_.Contains(index);
  436. if (!duplicate || !multiselect_)
  437. {
  438. if (!duplicate)
  439. {
  440. selections_.Push(index);
  441. added = true;
  442. }
  443. using namespace ItemSelected;
  444. VariantMap eventData;
  445. eventData[P_ELEMENT] = (void*)this;
  446. eventData[P_SELECTION] = *i;
  447. SendEvent(E_ITEMSELECTED, eventData);
  448. if (self.Expired())
  449. return;
  450. }
  451. }
  452. // If no multiselect enabled, allow setting only one item
  453. if (!multiselect_)
  454. break;
  455. }
  456. // Re-sort selections if necessary
  457. if (added)
  458. Sort(selections_.Begin(), selections_.End());
  459. UpdateSelectionEffect();
  460. SendEvent(E_SELECTIONCHANGED);
  461. }
  462. void ListView::AddSelection(unsigned index)
  463. {
  464. // Make a weak pointer to self to check for destruction as a response to events
  465. WeakPtr<ListView> self(this);
  466. if (!multiselect_)
  467. SetSelection(index);
  468. else
  469. {
  470. if (index >= GetNumItems())
  471. return;
  472. if (!selections_.Contains(index))
  473. {
  474. selections_.Push(index);
  475. using namespace ItemSelected;
  476. VariantMap eventData;
  477. eventData[P_ELEMENT] = (void*)this;
  478. eventData[P_SELECTION] = index;
  479. SendEvent(E_ITEMSELECTED, eventData);
  480. if (self.Expired())
  481. return;
  482. Sort(selections_.Begin(), selections_.End());
  483. }
  484. EnsureItemVisibility(index);
  485. UpdateSelectionEffect();
  486. SendEvent(E_SELECTIONCHANGED);
  487. }
  488. }
  489. void ListView::RemoveSelection(unsigned index)
  490. {
  491. if (index >= GetNumItems())
  492. return;
  493. if (selections_.Remove(index))
  494. {
  495. using namespace ItemSelected;
  496. VariantMap eventData;
  497. eventData[P_ELEMENT] = (void*)this;
  498. eventData[P_SELECTION] = index;
  499. SendEvent(E_ITEMDESELECTED, eventData);
  500. }
  501. EnsureItemVisibility(index);
  502. UpdateSelectionEffect();
  503. SendEvent(E_SELECTIONCHANGED);
  504. }
  505. void ListView::ToggleSelection(unsigned index)
  506. {
  507. unsigned numItems = GetNumItems();
  508. if (index >= numItems)
  509. return;
  510. if (selections_.Contains(index))
  511. RemoveSelection(index);
  512. else
  513. AddSelection(index);
  514. }
  515. void ListView::ChangeSelection(int delta, bool additive)
  516. {
  517. if (selections_.Empty())
  518. return;
  519. if (!multiselect_)
  520. additive = false;
  521. // If going downwards, use the last selection as a base. Otherwise use first
  522. unsigned selection = delta > 0 ? selections_.Back() : selections_.Front();
  523. int direction = delta > 0 ? 1 : -1;
  524. unsigned numItems = GetNumItems();
  525. unsigned newSelection = selection;
  526. unsigned okSelection = selection;
  527. PODVector<unsigned> indices = selections_;
  528. while (delta != 0)
  529. {
  530. newSelection += direction;
  531. if (newSelection >= numItems)
  532. break;
  533. UIElement* item = GetItem(newSelection);
  534. if (item->IsVisible())
  535. {
  536. indices.Push(okSelection = newSelection);
  537. delta -= direction;
  538. }
  539. }
  540. if (!additive)
  541. SetSelection(okSelection);
  542. else
  543. SetSelections(indices);
  544. }
  545. void ListView::ClearSelection()
  546. {
  547. SetSelections(PODVector<unsigned>());
  548. }
  549. void ListView::SetHighlightMode(HighlightMode mode)
  550. {
  551. highlightMode_ = mode;
  552. UpdateSelectionEffect();
  553. }
  554. void ListView::SetMultiselect(bool enable)
  555. {
  556. multiselect_ = enable;
  557. }
  558. void ListView::SetHierarchyMode(bool enable)
  559. {
  560. if (enable == hierarchyMode_)
  561. return;
  562. hierarchyMode_ = enable;
  563. UIElement* container;
  564. if (enable)
  565. {
  566. overlayContainer_ = CreateChild<UIElement>();
  567. overlayContainer_->SetInternal(true);
  568. overlayContainer_->SetLayoutMode(LM_FREE);
  569. overlayContainer_->SetSortChildren(false);
  570. overlayContainer_->SetClipChildren(true);
  571. container = new HierarchyContainer(context_, overlayContainer_);
  572. }
  573. else
  574. {
  575. if (overlayContainer_)
  576. {
  577. RemoveChild(overlayContainer_);
  578. overlayContainer_.Reset();
  579. }
  580. container = new UIElement(context_);
  581. }
  582. SetContentElement(container);
  583. container->SetInternal(true);
  584. container->SetEnabled(true);
  585. container->SetLayout(LM_VERTICAL);
  586. container->SetSortChildren(false);
  587. }
  588. void ListView::SetBaseIndent(int baseIndent)
  589. {
  590. baseIndent_ = baseIndent;
  591. UpdateLayout();
  592. }
  593. void ListView::SetClearSelectionOnDefocus(bool enable)
  594. {
  595. if (enable != clearSelectionOnDefocus_)
  596. {
  597. clearSelectionOnDefocus_ = enable;
  598. if (clearSelectionOnDefocus_)
  599. {
  600. SubscribeToEvent(this, E_DEFOCUSED, HANDLER(ListView, HandleDefocused));
  601. if (!HasFocus())
  602. ClearSelection();
  603. }
  604. else
  605. UnsubscribeFromEvent(this, E_DEFOCUSED);
  606. }
  607. }
  608. void ListView::SetDoubleClickInterval(float interval)
  609. {
  610. doubleClickInterval_ = Max((int)(interval * 1000.0f), 0);
  611. }
  612. void ListView::Expand(unsigned index, bool enable, bool recursive)
  613. {
  614. if (!hierarchyMode_)
  615. return;
  616. unsigned numItems = GetNumItems();
  617. if (index >= numItems)
  618. return;
  619. UIElement* item = GetItem(index++);
  620. SetItemExpanded(item, enable);
  621. int baseIndent = item->GetIndent();
  622. PODVector<bool> expanded(baseIndent + 1);
  623. expanded[baseIndent] = enable;
  624. contentElement_->DisableLayoutUpdate();
  625. while (index < numItems)
  626. {
  627. item = GetItem(index++);
  628. int indent = item->GetIndent();
  629. if (indent <= baseIndent)
  630. break;
  631. // Propagate the state to children when it is recursive
  632. if (recursive)
  633. SetItemExpanded(item, enable);
  634. // Use the parent expanded flag to influence the visibility of its children
  635. bool visible = enable && expanded[indent - 1];
  636. item->SetVisible(visible);
  637. if (indent >= (int)expanded.Size())
  638. expanded.Resize(indent + 1);
  639. expanded[indent] = visible && GetItemExpanded(item);
  640. }
  641. contentElement_->EnableLayoutUpdate();
  642. contentElement_->UpdateLayout();
  643. }
  644. void ListView::ToggleExpand(unsigned index, bool recursive)
  645. {
  646. if (!hierarchyMode_)
  647. return;
  648. unsigned numItems = GetNumItems();
  649. if (index >= numItems)
  650. return;
  651. UIElement* item = GetItem(index);
  652. Expand(index, !GetItemExpanded(item), recursive);
  653. }
  654. unsigned ListView::GetNumItems() const
  655. {
  656. return contentElement_->GetNumChildren();
  657. }
  658. UIElement* ListView::GetItem(unsigned index) const
  659. {
  660. return contentElement_->GetChild(index);
  661. }
  662. PODVector<UIElement*> ListView::GetItems() const
  663. {
  664. PODVector<UIElement*> items;
  665. contentElement_->GetChildren(items);
  666. return items;
  667. }
  668. unsigned ListView::FindItem(UIElement* item) const
  669. {
  670. const Vector<SharedPtr<UIElement> >& children = contentElement_->GetChildren();
  671. Vector<SharedPtr<UIElement> >::ConstIterator i = children.Find(SharedPtr<UIElement>(item));
  672. if (i != children.End())
  673. return i - children.Begin();
  674. else
  675. return M_MAX_UNSIGNED;
  676. }
  677. unsigned ListView::GetSelection() const
  678. {
  679. if (selections_.Empty())
  680. return M_MAX_UNSIGNED;
  681. else
  682. return GetSelections().Front();
  683. }
  684. UIElement* ListView::GetSelectedItem() const
  685. {
  686. return contentElement_->GetChild(GetSelection());
  687. }
  688. PODVector<UIElement*> ListView::GetSelectedItems() const
  689. {
  690. PODVector<UIElement*> ret;
  691. for (PODVector<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
  692. {
  693. UIElement* item = GetItem(*i);
  694. if (item)
  695. ret.Push(item);
  696. }
  697. return ret;
  698. }
  699. bool ListView::IsSelected(unsigned index) const
  700. {
  701. return selections_.Contains(index);
  702. }
  703. bool ListView::IsExpanded(unsigned index) const
  704. {
  705. return GetItemExpanded(contentElement_->GetChild(index));
  706. }
  707. float ListView::GetDoubleClickInterval() const
  708. {
  709. return (float)doubleClickInterval_ / 1000.0f;
  710. }
  711. void ListView::UpdateSelectionEffect()
  712. {
  713. unsigned numItems = GetNumItems();
  714. bool highlighted = highlightMode_ == HM_ALWAYS || HasFocus();
  715. for (unsigned i = 0; i < numItems; ++i)
  716. {
  717. UIElement* item = GetItem(i);
  718. if (highlightMode_ != HM_NEVER && selections_.Contains(i))
  719. item->SetSelected(highlighted);
  720. else
  721. item->SetSelected(false);
  722. }
  723. }
  724. void ListView::EnsureItemVisibility(unsigned index)
  725. {
  726. EnsureItemVisibility(GetItem(index));
  727. }
  728. void ListView::EnsureItemVisibility(UIElement* item)
  729. {
  730. if (!item || !item->IsVisible())
  731. return;
  732. IntVector2 newView = GetViewPosition();
  733. IntVector2 currentOffset = item->GetPosition() - newView;
  734. const IntRect& clipBorder = scrollPanel_->GetClipBorder();
  735. IntVector2 windowSize(scrollPanel_->GetWidth() - clipBorder.left_ - clipBorder.right_, scrollPanel_->GetHeight() -
  736. clipBorder.top_ - clipBorder.bottom_);
  737. if (currentOffset.y_ < 0)
  738. newView.y_ += currentOffset.y_;
  739. if (currentOffset.y_ + item->GetHeight() > windowSize.y_)
  740. newView.y_ += currentOffset.y_ + item->GetHeight() - windowSize.y_;
  741. SetViewPosition(newView);
  742. }
  743. void ListView::HandleUIMouseClick(StringHash eventType, VariantMap& eventData)
  744. {
  745. if (eventData[UIMouseClick::P_BUTTON].GetInt() != MOUSEB_LEFT)
  746. return;
  747. int qualifiers = eventData[UIMouseClick::P_QUALIFIERS].GetInt();
  748. UIElement* element = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  749. unsigned numItems = GetNumItems();
  750. for (unsigned i = 0; i < numItems; ++i)
  751. {
  752. if (element == GetItem(i))
  753. {
  754. // Check doubleclick
  755. bool isDoubleClick = false;
  756. if (!multiselect_ || !qualifiers)
  757. {
  758. if (!(isDoubleClick = doubleClickTimer_.GetMSec(true) < doubleClickInterval_ && lastClickedItem_ == i))
  759. lastClickedItem_ = i;
  760. SetSelection(i);
  761. }
  762. // Check multiselect with shift & ctrl
  763. if (multiselect_)
  764. {
  765. if (qualifiers & QUAL_SHIFT)
  766. {
  767. if (selections_.Empty())
  768. SetSelection(i);
  769. else
  770. {
  771. unsigned first = selections_.Front();
  772. unsigned last = selections_.Back();
  773. PODVector<unsigned> newSelections = selections_;
  774. if (i == first || i == last)
  775. {
  776. for (unsigned j = first; j <= last; ++j)
  777. newSelections.Push(j);
  778. }
  779. else if (i < first)
  780. {
  781. for (unsigned j = i; j <= first; ++j)
  782. newSelections.Push(j);
  783. }
  784. else if (i < last)
  785. {
  786. if ((abs((int)i - (int)first)) <= (abs((int)i - (int)last)))
  787. {
  788. for (unsigned j = first; j <= i; ++j)
  789. newSelections.Push(j);
  790. }
  791. else
  792. {
  793. for (unsigned j = i; j <= last; ++j)
  794. newSelections.Push(j);
  795. }
  796. }
  797. else if (i > last)
  798. {
  799. for (unsigned j = last; j <= i; ++j)
  800. newSelections.Push(j);
  801. }
  802. SetSelections(newSelections);
  803. }
  804. }
  805. else if (qualifiers & QUAL_CTRL)
  806. ToggleSelection(i);
  807. }
  808. if (isDoubleClick)
  809. {
  810. VariantMap eventData;
  811. eventData[ItemDoubleClicked::P_ELEMENT] = (void*)this;
  812. eventData[ItemDoubleClicked::P_SELECTION] = i;
  813. SendEvent(E_ITEMDOUBLECLICKED, eventData);
  814. }
  815. return;
  816. }
  817. }
  818. }
  819. void ListView::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  820. {
  821. using namespace FocusChanged;
  822. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  823. while (element)
  824. {
  825. // If the focused element or its parent is in the list, scroll the list to make the item visible
  826. UIElement* parent = element->GetParent();
  827. if (parent == contentElement_)
  828. {
  829. EnsureItemVisibility(element);
  830. return;
  831. }
  832. element = parent;
  833. }
  834. }
  835. void ListView::HandleDefocused(StringHash eventType, VariantMap& eventData)
  836. {
  837. ClearSelection();
  838. }
  839. }