ListView.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. /// React to defocus.
  57. virtual void OnDefocus();
  58. /// React to focus.
  59. virtual void OnFocus();
  60. /// Add item to the end of the list.
  61. void AddItem(UIElement* item);
  62. /// Insert item at a specific position.
  63. void InsertItem(unsigned index, UIElement* item);
  64. /// Remove specific item. In hierarchy mode will also remove any children.
  65. void RemoveItem(UIElement* item);
  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 Set<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. /// Enable hierarchy mode. Allows hiding/showing items that have "indent" userdata.
  89. void SetHierarchyMode(bool enable);
  90. /// Enable clearing of selection on defocus.
  91. void SetClearSelectionOnDefocus(bool enable);
  92. /// %Set item doubleclick interval in seconds.
  93. void SetDoubleClickInterval(float interval);
  94. /// Show or hide child items starting from index. Only has effect in hierarchy mode.
  95. void SetChildItemsVisible(unsigned index, bool enable);
  96. /// Show or hide all child items. Only has effect in hierarchy mode.
  97. void SetChildItemsVisible(bool enable);
  98. /// Toggle child items' visibility starting from index. Only has effect in hierarchy mode.
  99. void ToggleChildItemsVisible(unsigned index);
  100. /// Return number of items.
  101. unsigned GetNumItems() const;
  102. /// Return item at index.
  103. UIElement* GetItem(unsigned index) const;
  104. /// Return all items.
  105. PODVector<UIElement*> GetItems() const;
  106. /// Return first selected index, or M_MAX_UNSIGNED if none selected.
  107. unsigned GetSelection() const;
  108. /// Return all selected indices.
  109. Set<unsigned> GetSelections() const { return selections_; }
  110. /// Return first selected item, or null if none selected.
  111. UIElement* GetSelectedItem() const;
  112. /// Return all selected items.
  113. PODVector<UIElement*> GetSelectedItems() const;
  114. /// Return whether an item at index is seleccted.
  115. bool IsSelected(unsigned index) const;
  116. /// Return highlight mode.
  117. HighlightMode GetHighlightMode() const { return highlightMode_; }
  118. /// Return whether multiselect enabled.
  119. bool GetMultiselect() const { return multiselect_; }
  120. /// Return whether selection is cleared on defocus.
  121. bool GetClearSelectionOnDefocus() const { return clearSelectionOnDefocus_; }
  122. /// Return whether hierarchy mode enabled.
  123. bool GetHierarchyMode() const { return hierarchyMode_; }
  124. /// Return item doubleclick interval in seconds.
  125. float GetDoubleClickInterval() const { return doubleClickInterval_; }
  126. protected:
  127. /// Update selection effect when selection or focus changes.
  128. void UpdateSelectionEffect();
  129. /// Ensure full visibility of the item.
  130. void EnsureItemVisibility(unsigned index);
  131. /// Ensure full visibility of the item.
  132. void EnsureItemVisibility(UIElement* item);
  133. /// Current selection.
  134. Set<unsigned> selections_;
  135. /// Highlight mode.
  136. HighlightMode highlightMode_;
  137. /// Multiselect flag.
  138. bool multiselect_;
  139. /// Hierarchy mode flag.
  140. bool hierarchyMode_;
  141. /// Clear selection on defocus flag.
  142. bool clearSelectionOnDefocus_;
  143. /// Doubleclick interval.
  144. float doubleClickInterval_;
  145. /// Doubleclick timer.
  146. float doubleClickTimer_;
  147. /// Last clicked item.
  148. unsigned lastClickedItem_;
  149. private:
  150. /// Handle global UI mouseclick to check for selection change.
  151. void HandleUIMouseClick(StringHash eventType, VariantMap& eventData);
  152. /// Handle focus change to check whether an invisible item was focused.
  153. void HandleFocusChanged(StringHash eventType, VariantMap& eventData);
  154. };