ListView.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // Copyright (c) 2008-2013 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. #include "HashSet.h"
  25. namespace Urho3D
  26. {
  27. /// %ListView selection highlight mode.
  28. enum HighlightMode
  29. {
  30. /// Never highlight selections.
  31. HM_NEVER,
  32. /// Highlight when focused.
  33. HM_FOCUS,
  34. /// Highlight always.
  35. HM_ALWAYS
  36. };
  37. /// Scrollable list %UI element.
  38. class ListView : public ScrollView
  39. {
  40. OBJECT(ListView);
  41. public:
  42. /// Construct.
  43. ListView(Context* context);
  44. /// Destruct.
  45. virtual ~ListView();
  46. /// Register object factory.
  47. static void RegisterObject(Context* context);
  48. /// React to a key press.
  49. virtual void OnKey(int key, int buttons, int qualifiers);
  50. /// React to resize.
  51. virtual void OnResize();
  52. /// Add item to the end of the list.
  53. void AddItem(UIElement* item);
  54. /// Insert item at a specific position.
  55. void InsertItem(unsigned index, UIElement* item);
  56. /// Remove specific item, starting search at the specified index if provided. In hierarchy mode will also remove any children.
  57. void RemoveItem(UIElement* item, unsigned index = 0);
  58. /// Remove item at index. In hierarchy mode will also remove any children.
  59. void RemoveItem(unsigned index);
  60. /// Remove all items.
  61. void RemoveAllItems();
  62. /// Set selection.
  63. void SetSelection(unsigned index);
  64. /// Set multiple selected items. If multiselect disabled, sets only the first.
  65. void SetSelections(const PODVector<unsigned>& indices);
  66. /// Add item to the selection, multiselect mode only.
  67. void AddSelection(unsigned index);
  68. /// Remove item from the selection.
  69. void RemoveSelection(unsigned index);
  70. /// Toggle selection of an item.
  71. void ToggleSelection(unsigned index);
  72. /// Move selection by a delta and clamp at list ends. If additive (multiselect only), will add to the existing selection.
  73. void ChangeSelection(int delta, bool additive = false);
  74. /// Clear selection.
  75. void ClearSelection();
  76. /// Set selected items' highlight mode.
  77. void SetHighlightMode(HighlightMode mode);
  78. /// Enable multiselect.
  79. void SetMultiselect(bool enable);
  80. /// Enable hierarchy mode. Allows hiding/showing items that have "indent" userdata.
  81. void SetHierarchyMode(bool enable);
  82. /// Enable clearing of selection on defocus.
  83. void SetClearSelectionOnDefocus(bool enable);
  84. /// Set item doubleclick interval in seconds.
  85. void SetDoubleClickInterval(float interval);
  86. /// Show or hide child items starting from index. Only has effect in hierarchy mode.
  87. void SetChildItemsVisible(unsigned index, bool enable);
  88. /// Show or hide all child items. Only has effect in hierarchy mode.
  89. void SetChildItemsVisible(bool enable);
  90. /// Toggle child items' visibility starting from index. Only has effect in hierarchy mode.
  91. void ToggleChildItemsVisible(unsigned index);
  92. /// Return number of items.
  93. unsigned GetNumItems() const;
  94. /// Return item at index.
  95. UIElement* GetItem(unsigned index) const;
  96. /// Return all items.
  97. PODVector<UIElement*> GetItems() const;
  98. /// Return first selected index, or M_MAX_UNSIGNED if none selected.
  99. unsigned GetSelection() const;
  100. /// Return all selected indices.
  101. const PODVector<unsigned>& GetSelections() const { return selections_; }
  102. /// Return first selected item, or null if none selected.
  103. UIElement* GetSelectedItem() const;
  104. /// Return all selected items.
  105. PODVector<UIElement*> GetSelectedItems() const;
  106. /// Return whether an item at index is seleccted.
  107. bool IsSelected(unsigned index) const;
  108. /// Return highlight mode.
  109. HighlightMode GetHighlightMode() const { return highlightMode_; }
  110. /// Return whether multiselect enabled.
  111. bool GetMultiselect() const { return multiselect_; }
  112. /// Return whether selection is cleared on defocus.
  113. bool GetClearSelectionOnDefocus() const { return clearSelectionOnDefocus_; }
  114. /// Return whether hierarchy mode enabled.
  115. bool GetHierarchyMode() const { return hierarchyMode_; }
  116. /// Return item doubleclick interval in seconds.
  117. float GetDoubleClickInterval() const;
  118. protected:
  119. /// Update selection effect when selection or focus changes.
  120. void UpdateSelectionEffect();
  121. /// Ensure full visibility of the item.
  122. void EnsureItemVisibility(unsigned index);
  123. /// Ensure full visibility of the item.
  124. void EnsureItemVisibility(UIElement* item);
  125. /// Current selection.
  126. PODVector<unsigned> selections_;
  127. /// Highlight mode.
  128. HighlightMode highlightMode_;
  129. /// Multiselect flag.
  130. bool multiselect_;
  131. /// Hierarchy mode flag.
  132. bool hierarchyMode_;
  133. /// Clear selection on defocus flag.
  134. bool clearSelectionOnDefocus_;
  135. /// Doubleclick interval.
  136. unsigned doubleClickInterval_;
  137. /// Doubleclick timer.
  138. Timer doubleClickTimer_;
  139. /// Last clicked item.
  140. unsigned lastClickedItem_;
  141. private:
  142. /// Handle global UI mouseclick to check for selection change.
  143. void HandleUIMouseClick(StringHash eventType, VariantMap& eventData);
  144. /// Handle global focus change to check whether an invisible item was focused.
  145. void HandleFocusChanged(StringHash eventType, VariantMap& eventData);
  146. /// Handle being defocused.
  147. void HandleDefocused(StringHash eventType, VariantMap& eventData);
  148. };
  149. }