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.

juce_SelectedItemSet.h 11KB

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