The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

322 lines
11KB

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