ListView.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /// Add item to the end of the list.
  51. void AddItem(UIElement* item);
  52. /// Insert item at a specific position.
  53. void InsertItem(unsigned index, UIElement* item);
  54. /// Remove specific item, starting search at the specified index if provided. In hierarchy mode will also remove any children.
  55. void RemoveItem(UIElement* item, unsigned index = 0);
  56. /// Remove item at index. In hierarchy mode will also remove any children.
  57. void RemoveItem(unsigned index);
  58. /// Remove all items.
  59. void RemoveAllItems();
  60. /// Set selection.
  61. void SetSelection(unsigned index);
  62. /// Set multiple selected items. If multiselect disabled, sets only the first.
  63. void SetSelections(const PODVector<unsigned>& indices);
  64. /// Add item to the selection, multiselect mode only.
  65. void AddSelection(unsigned index);
  66. /// Remove item from the selection.
  67. void RemoveSelection(unsigned index);
  68. /// Toggle selection of an item.
  69. void ToggleSelection(unsigned index);
  70. /// Move selection by a delta and clamp at list ends. If additive (multiselect only), will add to the existing selection.
  71. void ChangeSelection(int delta, bool additive = false);
  72. /// Clear selection.
  73. void ClearSelection();
  74. /// Set selected items' highlight mode.
  75. void SetHighlightMode(HighlightMode mode);
  76. /// Enable multiselect.
  77. void SetMultiselect(bool enable);
  78. /// Enable hierarchy mode. Allows hiding/showing items that have "indent" userdata.
  79. void SetHierarchyMode(bool enable);
  80. /// Enable clearing of selection on defocus.
  81. void SetClearSelectionOnDefocus(bool enable);
  82. /// Set item doubleclick interval in seconds.
  83. void SetDoubleClickInterval(float interval);
  84. /// Expand item at index. Only has effect in hierarchy mode.
  85. void Expand(unsigned index, bool enable, bool recursive = false);
  86. /// Toggle item's expanded flag at index. Only has effect in hierarchy mode.
  87. void ToggleExpand(unsigned index, bool recursive = false);
  88. /// Return number of items.
  89. unsigned GetNumItems() const;
  90. /// Return item at index.
  91. UIElement* GetItem(unsigned index) const;
  92. /// Return all items.
  93. PODVector<UIElement*> GetItems() const;
  94. /// Return first selected index, or M_MAX_UNSIGNED if none selected.
  95. unsigned GetSelection() const;
  96. /// Return all selected indices.
  97. const PODVector<unsigned>& GetSelections() const { return selections_; }
  98. /// Return first selected item, or null if none selected.
  99. UIElement* GetSelectedItem() const;
  100. /// Return all selected items.
  101. PODVector<UIElement*> GetSelectedItems() const;
  102. /// Return whether an item at index is seleccted.
  103. bool IsSelected(unsigned index) const;
  104. /// Return highlight mode.
  105. HighlightMode GetHighlightMode() const { return highlightMode_; }
  106. /// Return whether multiselect enabled.
  107. bool GetMultiselect() const { return multiselect_; }
  108. /// Return whether selection is cleared on defocus.
  109. bool GetClearSelectionOnDefocus() const { return clearSelectionOnDefocus_; }
  110. /// Return whether hierarchy mode enabled.
  111. bool GetHierarchyMode() const { return hierarchyMode_; }
  112. /// Return item doubleclick interval in seconds.
  113. float GetDoubleClickInterval() const;
  114. protected:
  115. /// Update selection effect when selection or focus changes.
  116. void UpdateSelectionEffect();
  117. /// Ensure full visibility of the item.
  118. void EnsureItemVisibility(unsigned index);
  119. /// Ensure full visibility of the item.
  120. void EnsureItemVisibility(UIElement* item);
  121. /// Current selection.
  122. PODVector<unsigned> selections_;
  123. /// Highlight mode.
  124. HighlightMode highlightMode_;
  125. /// Multiselect flag.
  126. bool multiselect_;
  127. /// Hierarchy mode flag.
  128. bool hierarchyMode_;
  129. /// Clear selection on defocus flag.
  130. bool clearSelectionOnDefocus_;
  131. /// Doubleclick interval.
  132. unsigned doubleClickInterval_;
  133. /// Doubleclick timer.
  134. Timer doubleClickTimer_;
  135. /// Last clicked item.
  136. unsigned lastClickedItem_;
  137. private:
  138. /// Handle global UI mouseclick to check for selection change.
  139. void HandleUIMouseClick(StringHash eventType, VariantMap& eventData);
  140. /// Handle global focus change to check whether an invisible item was focused.
  141. void HandleFocusChanged(StringHash eventType, VariantMap& eventData);
  142. /// Handle being defocused.
  143. void HandleDefocused(StringHash eventType, VariantMap& eventData);
  144. };
  145. }