ListView.h 8.5 KB

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