ListView.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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 "HashSet.h"
  26. namespace Urho3D
  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 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. /// Perform UI element update.
  50. virtual void Update(float timeStep);
  51. /// React to a key press.
  52. virtual void OnKey(int key, int buttons, int qualifiers);
  53. /// React to resize.
  54. virtual void OnResize();
  55. /// Add item to the end of the list.
  56. void AddItem(UIElement* item);
  57. /// Insert item at a specific position.
  58. void InsertItem(unsigned index, UIElement* item);
  59. /// Remove specific item. In hierarchy mode will also remove any children.
  60. void RemoveItem(UIElement* item);
  61. /// Remove item at index. In hierarchy mode will also remove any children.
  62. void RemoveItem(unsigned index);
  63. /// Remove all items.
  64. void RemoveAllItems();
  65. /// Set selection.
  66. void SetSelection(unsigned index);
  67. /// Set multiple selected items. If multiselect disabled, sets only the first.
  68. void SetSelections(const PODVector<unsigned>& indices);
  69. /// Add item to the selection, multiselect mode only.
  70. void AddSelection(unsigned index);
  71. /// Remove item from the selection.
  72. void RemoveSelection(unsigned index);
  73. /// Toggle selection of an item.
  74. void ToggleSelection(unsigned index);
  75. /// Move selection by a delta and clamp at list ends. If additive (multiselect only), will add to the existing selection.
  76. void ChangeSelection(int delta, bool additive = false);
  77. /// Clear selection.
  78. void ClearSelection();
  79. /// Set selected items' highlight mode.
  80. void SetHighlightMode(HighlightMode mode);
  81. /// Enable multiselect.
  82. void SetMultiselect(bool enable);
  83. /// Enable hierarchy mode. Allows hiding/showing items that have "indent" userdata.
  84. void SetHierarchyMode(bool enable);
  85. /// Enable clearing of selection on defocus.
  86. void SetClearSelectionOnDefocus(bool enable);
  87. /// Set item doubleclick interval in seconds.
  88. void SetDoubleClickInterval(float interval);
  89. /// Show or hide child items starting from index. Only has effect in hierarchy mode.
  90. void SetChildItemsVisible(unsigned index, bool enable);
  91. /// Show or hide all child items. Only has effect in hierarchy mode.
  92. void SetChildItemsVisible(bool enable);
  93. /// Toggle child items' visibility starting from index. Only has effect in hierarchy mode.
  94. void ToggleChildItemsVisible(unsigned index);
  95. /// Return number of items.
  96. unsigned GetNumItems() const;
  97. /// Return item at index.
  98. UIElement* GetItem(unsigned index) const;
  99. /// Return all items.
  100. PODVector<UIElement*> GetItems() 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 highlight mode.
  112. HighlightMode GetHighlightMode() const { return highlightMode_; }
  113. /// Return whether multiselect enabled.
  114. bool GetMultiselect() const { return multiselect_; }
  115. /// Return whether selection is cleared on defocus.
  116. bool GetClearSelectionOnDefocus() const { return clearSelectionOnDefocus_; }
  117. /// Return whether hierarchy mode enabled.
  118. bool GetHierarchyMode() const { return hierarchyMode_; }
  119. /// Return item doubleclick interval in seconds.
  120. float GetDoubleClickInterval() const { return doubleClickInterval_; }
  121. protected:
  122. /// Update selection effect when selection or focus changes.
  123. void UpdateSelectionEffect();
  124. /// Ensure full visibility of the item.
  125. void EnsureItemVisibility(unsigned index);
  126. /// Ensure full visibility of the item.
  127. void EnsureItemVisibility(UIElement* item);
  128. /// Current selection.
  129. PODVector<unsigned> selections_;
  130. /// Highlight mode.
  131. HighlightMode highlightMode_;
  132. /// Multiselect flag.
  133. bool multiselect_;
  134. /// Hierarchy mode flag.
  135. bool hierarchyMode_;
  136. /// Clear selection on defocus flag.
  137. bool clearSelectionOnDefocus_;
  138. /// Doubleclick interval.
  139. float doubleClickInterval_;
  140. /// Doubleclick timer.
  141. float doubleClickTimer_;
  142. /// Last clicked item.
  143. unsigned lastClickedItem_;
  144. private:
  145. /// Handle global UI mouseclick to check for selection change.
  146. void HandleUIMouseClick(StringHash eventType, VariantMap& eventData);
  147. /// Handle global focus change to check whether an invisible item was focused.
  148. void HandleFocusChanged(StringHash eventType, VariantMap& eventData);
  149. /// Handle being focused.
  150. void HandleFocused(StringHash eventType, VariantMap& eventData);
  151. /// Handle being defocused.
  152. void HandleDefocused(StringHash eventType, VariantMap& eventData);
  153. };
  154. }