ListView.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. unsigned numItems = GetNumItems();
  777. for (unsigned i = 0; i < numItems; ++i)
  778. {
  779. if (element == GetItem(i))
  780. {
  781. // Single selection
  782. if (!multiselect_ || !qualifiers)
  783. SetSelection(i);
  784. // Check multiselect with shift & ctrl
  785. if (multiselect_)
  786. {
  787. if (qualifiers & QUAL_SHIFT)
  788. {
  789. if (selections_.Empty())
  790. SetSelection(i);
  791. else
  792. {
  793. unsigned first = selections_.Front();
  794. unsigned last = selections_.Back();
  795. PODVector<unsigned> newSelections = selections_;
  796. if (i == first || i == last)
  797. {
  798. for (unsigned j = first; j <= last; ++j)
  799. newSelections.Push(j);
  800. }
  801. else if (i < first)
  802. {
  803. for (unsigned j = i; j <= first; ++j)
  804. newSelections.Push(j);
  805. }
  806. else if (i < last)
  807. {
  808. if ((abs((int)i - (int)first)) <= (abs((int)i - (int)last)))
  809. {
  810. for (unsigned j = first; j <= i; ++j)
  811. newSelections.Push(j);
  812. }
  813. else
  814. {
  815. for (unsigned j = i; j <= last; ++j)
  816. newSelections.Push(j);
  817. }
  818. }
  819. else if (i > last)
  820. {
  821. for (unsigned j = last; j <= i; ++j)
  822. newSelections.Push(j);
  823. }
  824. SetSelections(newSelections);
  825. }
  826. }
  827. else if (qualifiers & QUAL_CTRL)
  828. ToggleSelection(i);
  829. }
  830. return;
  831. }
  832. }
  833. }
  834. void ListView::HandleUIMouseDoubleClick(StringHash eventType, VariantMap& eventData)
  835. {
  836. if (eventData[UIMouseClick::P_BUTTON].GetInt() != MOUSEB_LEFT)
  837. return;
  838. UIElement* element = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  839. unsigned numItems = GetNumItems();
  840. for (unsigned i = 0; i < numItems; ++i)
  841. {
  842. if (element == GetItem(i))
  843. {
  844. VariantMap eventData;
  845. eventData[ItemDoubleClicked::P_ELEMENT] = (void*)this;
  846. eventData[ItemDoubleClicked::P_SELECTION] = i;
  847. SendEvent(E_ITEMDOUBLECLICKED, eventData);
  848. return;
  849. }
  850. }
  851. }
  852. void ListView::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  853. {
  854. using namespace FocusChanged;
  855. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  856. while (element)
  857. {
  858. // If the focused element or its parent is in the list, scroll the list to make the item visible
  859. UIElement* parent = element->GetParent();
  860. if (parent == contentElement_)
  861. {
  862. EnsureItemVisibility(element);
  863. return;
  864. }
  865. element = parent;
  866. }
  867. }
  868. void ListView::HandleDefocused(StringHash eventType, VariantMap& eventData)
  869. {
  870. ClearSelection();
  871. }
  872. }