Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_SELECTEDITEMSET_H_INCLUDED
  18. #define JUCE_SELECTEDITEMSET_H_INCLUDED
  19. //==============================================================================
  20. /** Manages a list of selectable items.
  21. Use one of these to keep a track of things that the user has highlighted, like
  22. icons or things in a list.
  23. The class is templated so that you can use it to hold either a set of pointers
  24. to objects, or a set of ID numbers or handles, for cases where each item may
  25. not always have a corresponding object.
  26. To be informed when items are selected/deselected, register a ChangeListener with
  27. this object.
  28. */
  29. template <class SelectableItemType>
  30. class SelectedItemSet : public ChangeBroadcaster
  31. {
  32. public:
  33. //==============================================================================
  34. typedef SelectableItemType ItemType;
  35. typedef Array<SelectableItemType> ItemArray;
  36. typedef PARAMETER_TYPE (SelectableItemType) ParameterType;
  37. //==============================================================================
  38. /** Creates an empty set. */
  39. SelectedItemSet()
  40. {
  41. }
  42. /** Creates a set based on an array of items. */
  43. explicit SelectedItemSet (const ItemArray& items)
  44. : selectedItems (items)
  45. {
  46. }
  47. /** Creates a copy of another set. */
  48. SelectedItemSet (const SelectedItemSet& other)
  49. : selectedItems (other.selectedItems)
  50. {
  51. }
  52. /** Creates a copy of another set. */
  53. SelectedItemSet& operator= (const SelectedItemSet& other)
  54. {
  55. if (selectedItems != other.selectedItems)
  56. {
  57. selectedItems = other.selectedItems;
  58. changed();
  59. }
  60. return *this;
  61. }
  62. //==============================================================================
  63. /** Clears any other currently selected items, and selects this item.
  64. If this item is already the only thing selected, no change notification
  65. will be sent out.
  66. @see addToSelection, addToSelectionBasedOnModifiers
  67. */
  68. void selectOnly (ParameterType item)
  69. {
  70. if (isSelected (item))
  71. {
  72. for (int i = selectedItems.size(); --i >= 0;)
  73. {
  74. if (selectedItems.getUnchecked(i) != item)
  75. {
  76. deselect (selectedItems.getUnchecked(i));
  77. i = jmin (i, selectedItems.size());
  78. }
  79. }
  80. }
  81. else
  82. {
  83. deselectAll();
  84. changed();
  85. selectedItems.add (item);
  86. itemSelected (item);
  87. }
  88. }
  89. /** Selects an item.
  90. If the item is already selected, no change notification will be sent out.
  91. @see selectOnly, addToSelectionBasedOnModifiers
  92. */
  93. void addToSelection (ParameterType item)
  94. {
  95. if (! isSelected (item))
  96. {
  97. changed();
  98. selectedItems.add (item);
  99. itemSelected (item);
  100. }
  101. }
  102. /** Selects or deselects an item.
  103. This will use the modifier keys to decide whether to deselect other items
  104. first.
  105. So if the shift key is held down, the item will be added without deselecting
  106. anything (same as calling addToSelection() )
  107. If no modifiers are down, the current selection will be cleared first (same
  108. as calling selectOnly() )
  109. If the ctrl (or command on the Mac) key is held down, the item will be toggled -
  110. so it'll be added to the set unless it's already there, in which case it'll be
  111. deselected.
  112. If the items that you're selecting can also be dragged, you may need to use the
  113. addToSelectionOnMouseDown() and addToSelectionOnMouseUp() calls to handle the
  114. subtleties of this kind of usage.
  115. @see selectOnly, addToSelection, addToSelectionOnMouseDown, addToSelectionOnMouseUp
  116. */
  117. void addToSelectionBasedOnModifiers (ParameterType item,
  118. ModifierKeys modifiers)
  119. {
  120. if (modifiers.isShiftDown())
  121. {
  122. addToSelection (item);
  123. }
  124. else if (modifiers.isCommandDown())
  125. {
  126. if (isSelected (item))
  127. deselect (item);
  128. else
  129. addToSelection (item);
  130. }
  131. else
  132. {
  133. selectOnly (item);
  134. }
  135. }
  136. /** Selects or deselects items that can also be dragged, based on a mouse-down event.
  137. If you call addToSelectionOnMouseDown() at the start of your mouseDown event,
  138. and then call addToSelectionOnMouseUp() at the end of your mouseUp event, this
  139. makes it easy to handle multiple-selection of sets of objects that can also
  140. be dragged.
  141. For example, if you have several items already selected, and you click on
  142. one of them (without dragging), then you'd expect this to deselect the other, and
  143. just select the item you clicked on. But if you had clicked on this item and
  144. dragged it, you'd have expected them all to stay selected.
  145. When you call this method, you'll need to store the boolean result, because the
  146. addToSelectionOnMouseUp() method will need to be know this value.
  147. @see addToSelectionOnMouseUp, addToSelectionBasedOnModifiers
  148. */
  149. bool addToSelectionOnMouseDown (ParameterType item,
  150. ModifierKeys modifiers)
  151. {
  152. if (isSelected (item))
  153. return ! modifiers.isPopupMenu();
  154. addToSelectionBasedOnModifiers (item, modifiers);
  155. return false;
  156. }
  157. /** Selects or deselects items that can also be dragged, based on a mouse-up event.
  158. Call this during a mouseUp callback, when you have previously called the
  159. addToSelectionOnMouseDown() method during your mouseDown event.
  160. See addToSelectionOnMouseDown() for more info
  161. @param item the item to select (or deselect)
  162. @param modifiers the modifiers from the mouse-up event
  163. @param wasItemDragged true if your item was dragged during the mouse click
  164. @param resultOfMouseDownSelectMethod this is the boolean return value that came
  165. back from the addToSelectionOnMouseDown() call that you
  166. should have made during the matching mouseDown event
  167. */
  168. void addToSelectionOnMouseUp (ParameterType item,
  169. ModifierKeys modifiers,
  170. const bool wasItemDragged,
  171. const bool resultOfMouseDownSelectMethod)
  172. {
  173. if (resultOfMouseDownSelectMethod && ! wasItemDragged)
  174. addToSelectionBasedOnModifiers (item, modifiers);
  175. }
  176. /** Deselects an item. */
  177. void deselect (ParameterType item)
  178. {
  179. const int i = selectedItems.indexOf (item);
  180. if (i >= 0)
  181. {
  182. changed();
  183. itemDeselected (selectedItems.remove (i));
  184. }
  185. }
  186. /** Deselects all items. */
  187. void deselectAll()
  188. {
  189. if (selectedItems.size() > 0)
  190. {
  191. changed();
  192. for (int i = selectedItems.size(); --i >= 0;)
  193. {
  194. itemDeselected (selectedItems.remove (i));
  195. i = jmin (i, selectedItems.size());
  196. }
  197. }
  198. }
  199. //==============================================================================
  200. /** Returns the number of currently selected items.
  201. @see getSelectedItem
  202. */
  203. int getNumSelected() const noexcept { return selectedItems.size(); }
  204. /** Returns one of the currently selected items.
  205. If the index is out-of-range, this returns a default-constructed SelectableItemType.
  206. @see getNumSelected
  207. */
  208. SelectableItemType getSelectedItem (const int index) const { return selectedItems [index]; }
  209. /** True if this item is currently selected. */
  210. bool isSelected (ParameterType item) const noexcept { return selectedItems.contains (item); }
  211. /** Provides access to the array of items. */
  212. const ItemArray& getItemArray() const noexcept { return selectedItems; }
  213. /** Provides iterator access to the array of items. */
  214. SelectableItemType* begin() const noexcept { return selectedItems.begin(); }
  215. /** Provides iterator access to the array of items. */
  216. SelectableItemType* end() const noexcept { return selectedItems.end(); }
  217. //==============================================================================
  218. /** Can be overridden to do special handling when an item is selected.
  219. For example, if the item is an object, you might want to call it and tell
  220. it that it's being selected.
  221. */
  222. virtual void itemSelected (SelectableItemType) {}
  223. /** Can be overridden to do special handling when an item is deselected.
  224. For example, if the item is an object, you might want to call it and tell
  225. it that it's being deselected.
  226. */
  227. virtual void itemDeselected (SelectableItemType) {}
  228. /** Used internally, but can be called to force a change message to be sent
  229. to the ChangeListeners.
  230. */
  231. void changed()
  232. {
  233. sendChangeMessage();
  234. }
  235. /** Used internally, but can be called to force a change message to be sent
  236. to the ChangeListeners.
  237. */
  238. void changed (const bool synchronous)
  239. {
  240. if (synchronous)
  241. sendSynchronousChangeMessage();
  242. else
  243. sendChangeMessage();
  244. }
  245. private:
  246. //==============================================================================
  247. ItemArray selectedItems;
  248. JUCE_LEAK_DETECTOR (SelectedItemSet<SelectableItemType>)
  249. };
  250. #endif // JUCE_SELECTEDITEMSET_H_INCLUDED