ListView.h 8.9 KB

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