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

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