ListView.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "ScrollView.h"
  25. #include "Set.h"
  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 ListView : public ScrollView
  38. {
  39. OBJECT(ListView);
  40. using UIElement::SetStyle;
  41. public:
  42. /// Construct.
  43. ListView(Context* context);
  44. /// Destruct.
  45. virtual ~ListView();
  46. /// Register object factory.
  47. static void RegisterObject(Context* context);
  48. /// %Set UI element style from XML data.
  49. virtual void SetStyle(const XMLElement& element);
  50. /// Perform UI element update.
  51. virtual void Update(float timeStep);
  52. /// React to a key press.
  53. virtual void OnKey(int key, int buttons, int qualifiers);
  54. /// React to resize.
  55. virtual void OnResize();
  56. /// Add item to the end of the list.
  57. void AddItem(UIElement* item);
  58. /// Insert item at a specific position.
  59. void InsertItem(unsigned index, UIElement* item);
  60. /// Remove specific item. In hierarchy mode will also remove any children.
  61. void RemoveItem(UIElement* item);
  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 Set<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. /// Enable hierarchy mode. Allows hiding/showing items that have "indent" userdata.
  85. void SetHierarchyMode(bool enable);
  86. /// Enable clearing of selection on defocus.
  87. void SetClearSelectionOnDefocus(bool enable);
  88. /// %Set item doubleclick interval in seconds.
  89. void SetDoubleClickInterval(float interval);
  90. /// Show or hide child items starting from index. Only has effect in hierarchy mode.
  91. void SetChildItemsVisible(unsigned index, bool enable);
  92. /// Show or hide all child items. Only has effect in hierarchy mode.
  93. void SetChildItemsVisible(bool enable);
  94. /// Toggle child items' visibility starting from index. Only has effect in hierarchy mode.
  95. void ToggleChildItemsVisible(unsigned index);
  96. /// Return number of items.
  97. unsigned GetNumItems() const;
  98. /// Return item at index.
  99. UIElement* GetItem(unsigned index) const;
  100. /// Return all items.
  101. PODVector<UIElement*> GetItems() const;
  102. /// Return first selected index, or M_MAX_UNSIGNED if none selected.
  103. unsigned GetSelection() const;
  104. /// Return all selected indices.
  105. Set<unsigned> GetSelections() const { return selections_; }
  106. /// Return first selected item, or null if none selected.
  107. UIElement* GetSelectedItem() const;
  108. /// Return all selected items.
  109. PODVector<UIElement*> GetSelectedItems() const;
  110. /// Return whether an item at index is seleccted.
  111. bool IsSelected(unsigned index) const;
  112. /// Return highlight mode.
  113. HighlightMode GetHighlightMode() const { return highlightMode_; }
  114. /// Return whether multiselect enabled.
  115. bool GetMultiselect() const { return multiselect_; }
  116. /// Return whether selection is cleared on defocus.
  117. bool GetClearSelectionOnDefocus() const { return clearSelectionOnDefocus_; }
  118. /// Return whether hierarchy mode enabled.
  119. bool GetHierarchyMode() const { return hierarchyMode_; }
  120. /// Return item doubleclick interval in seconds.
  121. float GetDoubleClickInterval() const { return doubleClickInterval_; }
  122. protected:
  123. /// Update selection effect when selection or focus changes.
  124. void UpdateSelectionEffect();
  125. /// Ensure full visibility of the item.
  126. void EnsureItemVisibility(unsigned index);
  127. /// Ensure full visibility of the item.
  128. void EnsureItemVisibility(UIElement* item);
  129. /// Current selection.
  130. Set<unsigned> selections_;
  131. /// Highlight mode.
  132. HighlightMode highlightMode_;
  133. /// Multiselect flag.
  134. bool multiselect_;
  135. /// Hierarchy mode flag.
  136. bool hierarchyMode_;
  137. /// Clear selection on defocus flag.
  138. bool clearSelectionOnDefocus_;
  139. /// Doubleclick interval.
  140. float doubleClickInterval_;
  141. /// Doubleclick timer.
  142. float doubleClickTimer_;
  143. /// Last clicked item.
  144. unsigned lastClickedItem_;
  145. private:
  146. /// Handle global UI mouseclick to check for selection change.
  147. void HandleUIMouseClick(StringHash eventType, VariantMap& eventData);
  148. /// Handle global focus change to check whether an invisible item was focused.
  149. void HandleFocusChanged(StringHash eventType, VariantMap& eventData);
  150. /// Handle being focused.
  151. void HandleFocused(StringHash eventType, VariantMap& eventData);
  152. /// Handle being defocused.
  153. void HandleDefocused(StringHash eventType, VariantMap& eventData);
  154. };