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.

306 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_SELECTEDITEMSET_JUCEHEADER__
  19. #define __JUCE_SELECTEDITEMSET_JUCEHEADER__
  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. @see SelectableObject
  30. */
  31. template <class SelectableItemType>
  32. class JUCE_API SelectedItemSet : public ChangeBroadcaster
  33. {
  34. public:
  35. //==============================================================================
  36. typedef SelectableItemType ItemType;
  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 Array <SelectableItemType>& 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. const 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. const 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. const 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
  205. {
  206. return selectedItems.size();
  207. }
  208. /** Returns one of the currently selected items.
  209. Returns 0 if the index is out-of-range.
  210. @see getNumSelected
  211. */
  212. SelectableItemType getSelectedItem (const int index) const noexcept
  213. {
  214. return selectedItems [index];
  215. }
  216. /** True if this item is currently selected. */
  217. bool isSelected (ParameterType item) const noexcept
  218. {
  219. return selectedItems.contains (item);
  220. }
  221. const Array <SelectableItemType>& getItemArray() const noexcept { return selectedItems; }
  222. //==============================================================================
  223. /** Can be overridden to do special handling when an item is selected.
  224. For example, if the item is an object, you might want to call it and tell
  225. it that it's being selected.
  226. */
  227. virtual void itemSelected (SelectableItemType item) { (void) item; }
  228. /** Can be overridden to do special handling when an item is deselected.
  229. For example, if the item is an object, you might want to call it and tell
  230. it that it's being deselected.
  231. */
  232. virtual void itemDeselected (SelectableItemType item) { (void) item; }
  233. /** Used internally, but can be called to force a change message to be sent to the ChangeListeners. */
  234. void changed (const bool synchronous = false)
  235. {
  236. if (synchronous)
  237. sendSynchronousChangeMessage();
  238. else
  239. sendChangeMessage();
  240. }
  241. private:
  242. //==============================================================================
  243. Array <SelectableItemType> selectedItems;
  244. JUCE_LEAK_DETECTOR (SelectedItemSet <SelectableItemType>);
  245. };
  246. #endif // __JUCE_SELECTEDITEMSET_JUCEHEADER__