ListView.cpp 29 KB

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