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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. @see SelectableObject
  29. */
  30. template <class SelectableItemType>
  31. class SelectedItemSet : public ChangeBroadcaster
  32. {
  33. public:
  34. //==============================================================================
  35. typedef SelectableItemType ItemType;
  36. typedef Array<SelectableItemType> ItemArray;
  37. typedef PARAMETER_TYPE (SelectableItemType) ParameterType;
  38. //==============================================================================
  39. /** Creates an empty set. */
  40. SelectedItemSet()
  41. {
  42. }
  43. /** Creates a set based on an array of items. */
  44. explicit SelectedItemSet (const ItemArray& items)
  45. : selectedItems (items)
  46. {
  47. }
  48. /** Creates a copy of another set. */
  49. SelectedItemSet (const SelectedItemSet& other)
  50. : selectedItems (other.selectedItems)
  51. {
  52. }
  53. /** Creates a copy of another set. */
  54. SelectedItemSet& operator= (const SelectedItemSet& other)
  55. {
  56. if (selectedItems != other.selectedItems)
  57. {
  58. selectedItems = other.selectedItems;
  59. changed();
  60. }
  61. return *this;
  62. }
  63. //==============================================================================
  64. /** Clears any other currently selected items, and selects this item.
  65. If this item is already the only thing selected, no change notification
  66. will be sent out.
  67. @see addToSelection, addToSelectionBasedOnModifiers
  68. */
  69. void selectOnly (ParameterType item)
  70. {
  71. if (isSelected (item))
  72. {
  73. for (int i = selectedItems.size(); --i >= 0;)
  74. {
  75. if (selectedItems.getUnchecked(i) != item)
  76. {
  77. deselect (selectedItems.getUnchecked(i));
  78. i = jmin (i, selectedItems.size());
  79. }
  80. }
  81. }
  82. else
  83. {
  84. deselectAll();
  85. changed();
  86. selectedItems.add (item);
  87. itemSelected (item);
  88. }
  89. }
  90. /** Selects an item.
  91. If the item is already selected, no change notification will be sent out.
  92. @see selectOnly, addToSelectionBasedOnModifiers
  93. */
  94. void addToSelection (ParameterType item)
  95. {
  96. if (! isSelected (item))
  97. {
  98. changed();
  99. selectedItems.add (item);
  100. itemSelected (item);
  101. }
  102. }
  103. /** Selects or deselects an item.
  104. This will use the modifier keys to decide whether to deselect other items
  105. first.
  106. So if the shift key is held down, the item will be added without deselecting
  107. anything (same as calling addToSelection() )
  108. If no modifiers are down, the current selection will be cleared first (same
  109. as calling selectOnly() )
  110. If the ctrl (or command on the Mac) key is held down, the item will be toggled -
  111. so it'll be added to the set unless it's already there, in which case it'll be
  112. deselected.
  113. If the items that you're selecting can also be dragged, you may need to use the
  114. addToSelectionOnMouseDown() and addToSelectionOnMouseUp() calls to handle the
  115. subtleties of this kind of usage.
  116. @see selectOnly, addToSelection, addToSelectionOnMouseDown, addToSelectionOnMouseUp
  117. */
  118. void addToSelectionBasedOnModifiers (ParameterType item,
  119. ModifierKeys modifiers)
  120. {
  121. if (modifiers.isShiftDown())
  122. {
  123. addToSelection (item);
  124. }
  125. else if (modifiers.isCommandDown())
  126. {
  127. if (isSelected (item))
  128. deselect (item);
  129. else
  130. addToSelection (item);
  131. }
  132. else
  133. {
  134. selectOnly (item);
  135. }
  136. }
  137. /** Selects or deselects items that can also be dragged, based on a mouse-down event.
  138. If you call addToSelectionOnMouseDown() at the start of your mouseDown event,
  139. and then call addToSelectionOnMouseUp() at the end of your mouseUp event, this
  140. makes it easy to handle multiple-selection of sets of objects that can also
  141. be dragged.
  142. For example, if you have several items already selected, and you click on
  143. one of them (without dragging), then you'd expect this to deselect the other, and
  144. just select the item you clicked on. But if you had clicked on this item and
  145. dragged it, you'd have expected them all to stay selected.
  146. When you call this method, you'll need to store the boolean result, because the
  147. addToSelectionOnMouseUp() method will need to be know this value.
  148. @see addToSelectionOnMouseUp, addToSelectionBasedOnModifiers
  149. */
  150. bool addToSelectionOnMouseDown (ParameterType item,
  151. ModifierKeys modifiers)
  152. {
  153. if (isSelected (item))
  154. return ! modifiers.isPopupMenu();
  155. addToSelectionBasedOnModifiers (item, modifiers);
  156. return false;
  157. }
  158. /** Selects or deselects items that can also be dragged, based on a mouse-up event.
  159. Call this during a mouseUp callback, when you have previously called the
  160. addToSelectionOnMouseDown() method during your mouseDown event.
  161. See addToSelectionOnMouseDown() for more info
  162. @param item the item to select (or deselect)
  163. @param modifiers the modifiers from the mouse-up event
  164. @param wasItemDragged true if your item was dragged during the mouse click
  165. @param resultOfMouseDownSelectMethod this is the boolean return value that came
  166. back from the addToSelectionOnMouseDown() call that you
  167. should have made during the matching mouseDown event
  168. */
  169. void addToSelectionOnMouseUp (ParameterType item,
  170. ModifierKeys modifiers,
  171. const bool wasItemDragged,
  172. const bool resultOfMouseDownSelectMethod)
  173. {
  174. if (resultOfMouseDownSelectMethod && ! wasItemDragged)
  175. addToSelectionBasedOnModifiers (item, modifiers);
  176. }
  177. /** Deselects an item. */
  178. void deselect (ParameterType item)
  179. {
  180. const int i = selectedItems.indexOf (item);
  181. if (i >= 0)
  182. {
  183. changed();
  184. itemDeselected (selectedItems.remove (i));
  185. }
  186. }
  187. /** Deselects all items. */
  188. void deselectAll()
  189. {
  190. if (selectedItems.size() > 0)
  191. {
  192. changed();
  193. for (int i = selectedItems.size(); --i >= 0;)
  194. {
  195. itemDeselected (selectedItems.remove (i));
  196. i = jmin (i, selectedItems.size());
  197. }
  198. }
  199. }
  200. //==============================================================================
  201. /** Returns the number of currently selected items.
  202. @see getSelectedItem
  203. */
  204. int getNumSelected() const noexcept { return selectedItems.size(); }
  205. /** Returns one of the currently selected items.
  206. If the index is out-of-range, this returns a default-constructed SelectableItemType.
  207. @see getNumSelected
  208. */
  209. SelectableItemType getSelectedItem (const int index) const { return selectedItems [index]; }
  210. /** True if this item is currently selected. */
  211. bool isSelected (ParameterType item) const noexcept { return selectedItems.contains (item); }
  212. /** Provides access to the array of items. */
  213. const ItemArray& getItemArray() const noexcept { return selectedItems; }
  214. /** Provides iterator access to the array of items. */
  215. SelectableItemType* begin() const noexcept { return selectedItems.begin(); }
  216. /** Provides iterator access to the array of items. */
  217. SelectableItemType* end() const noexcept { return selectedItems.end(); }
  218. //==============================================================================
  219. /** Can be overridden to do special handling when an item is selected.
  220. For example, if the item is an object, you might want to call it and tell
  221. it that it's being selected.
  222. */
  223. virtual void itemSelected (SelectableItemType) {}
  224. /** Can be overridden to do special handling when an item is deselected.
  225. For example, if the item is an object, you might want to call it and tell
  226. it that it's being deselected.
  227. */
  228. virtual void itemDeselected (SelectableItemType) {}
  229. /** Used internally, but can be called to force a change message to be sent
  230. to the ChangeListeners.
  231. */
  232. void changed()
  233. {
  234. sendChangeMessage();
  235. }
  236. /** Used internally, but can be called to force a change message to be sent
  237. to the ChangeListeners.
  238. */
  239. void changed (const bool synchronous)
  240. {
  241. if (synchronous)
  242. sendSynchronousChangeMessage();
  243. else
  244. sendChangeMessage();
  245. }
  246. private:
  247. //==============================================================================
  248. ItemArray selectedItems;
  249. JUCE_LEAK_DETECTOR (SelectedItemSet<SelectableItemType>)
  250. };
  251. #endif // JUCE_SELECTEDITEMSET_H_INCLUDED