ListView.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Input/InputConstants.h"
  6. #include "../UI/ScrollView.h"
  7. namespace Urho3D
  8. {
  9. /// %ListView selection highlight mode.
  10. enum HighlightMode
  11. {
  12. /// Never highlight selections.
  13. HM_NEVER,
  14. /// Highlight when focused.
  15. HM_FOCUS,
  16. /// Highlight always.
  17. HM_ALWAYS
  18. };
  19. /// Scrollable list %UI element.
  20. class URHO3D_API ListView : public ScrollView
  21. {
  22. URHO3D_OBJECT(ListView, ScrollView);
  23. public:
  24. /// Construct.
  25. explicit ListView(Context* context);
  26. /// Destruct.
  27. ~ListView() override;
  28. /// Register object factory.
  29. /// @nobind
  30. static void RegisterObject(Context* context);
  31. /// React to a key press.
  32. void OnKey(Key key, MouseButtonFlags buttons, QualifierFlags qualifiers) override;
  33. /// React to resize.
  34. void OnResize(const IntVector2& newSize, const IntVector2& delta) override;
  35. /// Manually update layout on internal elements.
  36. void UpdateInternalLayout();
  37. /// Disable automatic layout update for internal elements.
  38. void DisableInternalLayoutUpdate();
  39. /// Enable automatic layout update for internal elements.
  40. void EnableInternalLayoutUpdate();
  41. /// Add item to the end of the list.
  42. void AddItem(UIElement* item);
  43. /// \brief Insert item at a specific index. In hierarchy mode, the optional parameter will be used to determine the child's indent level in respect to its parent.
  44. /// If index is greater than the total items then the new item is inserted at the end of the list.
  45. /// In hierarchy mode, if index is greater than the index of last children of the specified parent item then the new item is inserted next to the last children.
  46. /// And if the index is lesser than the index of the parent item itself then the new item is inserted before the first child item.
  47. /// index can be ENDPOS.
  48. void InsertItem(i32 index, UIElement* item, UIElement* parentItem = nullptr);
  49. /// Remove specific item, starting search at the specified index if provided. In hierarchy mode will also remove any children.
  50. void RemoveItem(UIElement* item, i32 index = 0);
  51. /// Remove item at index. In hierarchy mode will also remove any children.
  52. void RemoveItem(i32 index);
  53. /// Remove all items.
  54. void RemoveAllItems();
  55. /// Set selection.
  56. /// @property
  57. void SetSelection(i32 index);
  58. /// Set multiple selected items. If multiselect disabled, sets only the first.
  59. void SetSelections(const Vector<i32>& indices);
  60. /// Add item to the selection, multiselect mode only.
  61. void AddSelection(i32 index);
  62. /// Remove item from the selection.
  63. void RemoveSelection(i32 index);
  64. /// Toggle selection of an item.
  65. void ToggleSelection(i32 index);
  66. /// Move selection by a delta and clamp at list ends. If additive (multiselect only), will add to the existing selection.
  67. void ChangeSelection(int delta, bool additive = false);
  68. /// Clear selection.
  69. void ClearSelection();
  70. /// Set selected items' highlight mode.
  71. /// @property
  72. void SetHighlightMode(HighlightMode mode);
  73. /// Enable multiselect.
  74. /// @property
  75. void SetMultiselect(bool enable);
  76. /// \brief Enable hierarchy mode. Allows items to have parent-child relationship at different indent level and the ability to expand/collapse child items.
  77. /// All items in the list will be lost during mode change.
  78. /// @property
  79. void SetHierarchyMode(bool enable);
  80. /// Set base indent, i.e. the indent level of the ultimate parent item.
  81. /// @property
  82. void SetBaseIndent(int baseIndent);
  83. /// Enable clearing of selection on defocus.
  84. /// @property
  85. void SetClearSelectionOnDefocus(bool enable);
  86. /// Enable reacting to click end instead of click start for item selection. Default false.
  87. /// @property
  88. void SetSelectOnClickEnd(bool enable);
  89. /// Expand item at index. Only has effect in hierarchy mode.
  90. void Expand(i32 index, bool enable, bool recursive = false);
  91. /// Toggle item's expanded flag at index. Only has effect in hierarchy mode.
  92. void ToggleExpand(i32 index, bool recursive = false);
  93. /// Return number of items.
  94. /// @property
  95. i32 GetNumItems() const;
  96. /// Return item at index.
  97. /// @property{get_items}
  98. UIElement* GetItem(i32 index) const;
  99. /// Return all items.
  100. Vector<UIElement*> GetItems() const;
  101. /// Return index of item, or NINDEX If not found.
  102. i32 FindItem(UIElement* item) const;
  103. /// Return first selected index, or NINDEX if none selected.
  104. /// @property
  105. i32 GetSelection() const;
  106. /// Return all selected indices.
  107. /// @property
  108. const Vector<i32>& GetSelections() const { return selections_; }
  109. /// Copy selected items to system clipboard. Currently only applicable to Text items.
  110. void CopySelectedItemsToClipboard() const;
  111. /// Return first selected item, or null if none selected.
  112. /// @property
  113. UIElement* GetSelectedItem() const;
  114. /// Return all selected items.
  115. /// @property
  116. Vector<UIElement*> GetSelectedItems() const;
  117. /// Return whether an item at index is selected.
  118. bool IsSelected(i32 index) const;
  119. /// Return whether an item at index has its children expanded (in hierarchy mode only).
  120. bool IsExpanded(i32 index) const;
  121. /// Return highlight mode.
  122. /// @property
  123. HighlightMode GetHighlightMode() const { return highlightMode_; }
  124. /// Return whether multiselect enabled.
  125. /// @property
  126. bool GetMultiselect() const { return multiselect_; }
  127. /// Return whether selection is cleared on defocus.
  128. /// @property
  129. bool GetClearSelectionOnDefocus() const { return clearSelectionOnDefocus_; }
  130. /// Return whether reacts to click end instead of click start for item selection.
  131. /// @property
  132. bool GetSelectOnClickEnd() const { return selectOnClickEnd_; }
  133. /// Return whether hierarchy mode enabled.
  134. /// @property
  135. bool GetHierarchyMode() const { return hierarchyMode_; }
  136. /// Return base indent.
  137. /// @property
  138. int GetBaseIndent() const { return baseIndent_; }
  139. /// Ensure full visibility of the item.
  140. void EnsureItemVisibility(i32 index);
  141. /// Ensure full visibility of the item.
  142. void EnsureItemVisibility(UIElement* item);
  143. protected:
  144. /// Filter implicit attributes in serialization process.
  145. bool FilterImplicitAttributes(XMLElement& dest) const override;
  146. /// Update selection effect when selection or focus changes.
  147. void UpdateSelectionEffect();
  148. /// Current selection.
  149. Vector<i32> selections_;
  150. /// Highlight mode.
  151. HighlightMode highlightMode_;
  152. /// Multiselect flag.
  153. bool multiselect_;
  154. /// Hierarchy mode flag.
  155. bool hierarchyMode_;
  156. /// Base indent, used in hierarchy mode only.
  157. int baseIndent_;
  158. /// Overlay container, used in hierarchy mode only.
  159. SharedPtr<UIElement> overlayContainer_;
  160. /// Clear selection on defocus flag.
  161. bool clearSelectionOnDefocus_;
  162. /// React to click end instead of click start flag.
  163. bool selectOnClickEnd_;
  164. private:
  165. /// Handle global UI mouseclick to check for selection change.
  166. void HandleUIMouseClick(StringHash eventType, VariantMap& eventData);
  167. /// Handle global UI mouse doubleclick.
  168. void HandleUIMouseDoubleClick(StringHash eventType, VariantMap& eventData);
  169. /// Handle global focus change to check whether an invisible item was focused.
  170. void HandleItemFocusChanged(StringHash eventType, VariantMap& eventData);
  171. /// Handle focus changed.
  172. void HandleFocusChanged(StringHash eventType, VariantMap& eventData);
  173. /// Update subscription to UI click events.
  174. void UpdateUIClickSubscription();
  175. };
  176. }