ListView.cpp 31 KB

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