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.

321 lines
11KB

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