ListView.h 7.5 KB

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