ListView.h 8.1 KB

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