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.

245 lines
9.4KB

  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_LASSOCOMPONENT_JUCEHEADER__
  19. #define __JUCE_LASSOCOMPONENT_JUCEHEADER__
  20. #include "../components/juce_Component.h"
  21. #include "juce_SelectedItemSet.h"
  22. //==============================================================================
  23. /**
  24. A class used by the LassoComponent to manage the things that it selects.
  25. This allows the LassoComponent to find out which items are within the lasso,
  26. and to change the list of selected items.
  27. @see LassoComponent, SelectedItemSet
  28. */
  29. template <class SelectableItemType>
  30. class LassoSource
  31. {
  32. public:
  33. /** Destructor. */
  34. virtual ~LassoSource() {}
  35. /** Returns the set of items that lie within a given lassoable region.
  36. Your implementation of this method must find all the relevent items that lie
  37. within the given rectangle. and add them to the itemsFound array.
  38. The co-ordinates are relative to the top-left of the lasso component's parent
  39. component. (i.e. they are the same as the size and position of the lasso
  40. component itself).
  41. */
  42. virtual void findLassoItemsInArea (Array <SelectableItemType>& itemsFound,
  43. const Rectangle<int>& area) = 0;
  44. /** Returns the SelectedItemSet that the lasso should update.
  45. This set will be continuously updated by the LassoComponent as it gets
  46. dragged around, so make sure that you've got a ChangeListener attached to
  47. the set so that your UI objects will know when the selection changes and
  48. be able to update themselves appropriately.
  49. */
  50. virtual SelectedItemSet <SelectableItemType>& getLassoSelection() = 0;
  51. };
  52. //==============================================================================
  53. /**
  54. A component that acts as a rectangular selection region, which you drag with
  55. the mouse to select groups of objects (in conjunction with a SelectedItemSet).
  56. To use one of these:
  57. - In your mouseDown or mouseDrag event, add the LassoComponent to your parent
  58. component, and call its beginLasso() method, giving it a
  59. suitable LassoSource object that it can use to find out which items are in
  60. the active area.
  61. - Each time your parent component gets a mouseDrag event, call dragLasso()
  62. to update the lasso's position - it will use its LassoSource to calculate and
  63. update the current selection.
  64. - After the drag has finished and you get a mouseUp callback, you should call
  65. endLasso() to clean up. This will make the lasso component invisible, and you
  66. can remove it from the parent component, or delete it.
  67. The class takes into account the modifier keys that are being held down while
  68. the lasso is being dragged, so if shift is pressed, then any lassoed items will
  69. be added to the original selection; if ctrl or command is pressed, they will be
  70. xor'ed with any previously selected items.
  71. @see LassoSource, SelectedItemSet
  72. */
  73. template <class SelectableItemType>
  74. class LassoComponent : public Component
  75. {
  76. public:
  77. //==============================================================================
  78. /** Creates a Lasso component.
  79. The fill colour is used to fill the lasso'ed rectangle, and the outline
  80. colour is used to draw a line around its edge.
  81. */
  82. explicit LassoComponent (const int outlineThickness_ = 1)
  83. : source (nullptr),
  84. outlineThickness (outlineThickness_)
  85. {
  86. }
  87. /** Destructor. */
  88. ~LassoComponent()
  89. {
  90. }
  91. //==============================================================================
  92. /** Call this in your mouseDown event, to initialise a drag.
  93. Pass in a suitable LassoSource object which the lasso will use to find
  94. the items and change the selection.
  95. After using this method to initialise the lasso, repeatedly call dragLasso()
  96. in your component's mouseDrag callback.
  97. @see dragLasso, endLasso, LassoSource
  98. */
  99. void beginLasso (const MouseEvent& e,
  100. LassoSource <SelectableItemType>* const lassoSource)
  101. {
  102. jassert (source == nullptr); // this suggests that you didn't call endLasso() after the last drag...
  103. jassert (lassoSource != nullptr); // the source can't be null!
  104. jassert (getParentComponent() != nullptr); // you need to add this to a parent component for it to work!
  105. source = lassoSource;
  106. if (lassoSource != nullptr)
  107. originalSelection = lassoSource->getLassoSelection().getItemArray();
  108. setSize (0, 0);
  109. dragStartPos = e.getMouseDownPosition();
  110. }
  111. /** Call this in your mouseDrag event, to update the lasso's position.
  112. This must be repeatedly calling when the mouse is dragged, after you've
  113. first initialised the lasso with beginLasso().
  114. This method takes into account the modifier keys that are being held down, so
  115. if shift is pressed, then the lassoed items will be added to any that were
  116. previously selected; if ctrl or command is pressed, then they will be xor'ed
  117. with previously selected items.
  118. @see beginLasso, endLasso
  119. */
  120. void dragLasso (const MouseEvent& e)
  121. {
  122. if (source != nullptr)
  123. {
  124. setBounds (Rectangle<int> (dragStartPos, e.getPosition()));
  125. setVisible (true);
  126. Array <SelectableItemType> itemsInLasso;
  127. source->findLassoItemsInArea (itemsInLasso, getBounds());
  128. if (e.mods.isShiftDown())
  129. {
  130. itemsInLasso.removeValuesIn (originalSelection); // to avoid duplicates
  131. itemsInLasso.addArray (originalSelection);
  132. }
  133. else if (e.mods.isCommandDown() || e.mods.isAltDown())
  134. {
  135. Array <SelectableItemType> originalMinusNew (originalSelection);
  136. originalMinusNew.removeValuesIn (itemsInLasso);
  137. itemsInLasso.removeValuesIn (originalSelection);
  138. itemsInLasso.addArray (originalMinusNew);
  139. }
  140. source->getLassoSelection() = SelectedItemSet <SelectableItemType> (itemsInLasso);
  141. }
  142. }
  143. /** Call this in your mouseUp event, after the lasso has been dragged.
  144. @see beginLasso, dragLasso
  145. */
  146. void endLasso()
  147. {
  148. source = nullptr;
  149. originalSelection.clear();
  150. setVisible (false);
  151. }
  152. //==============================================================================
  153. /** A set of colour IDs to use to change the colour of various aspects of the label.
  154. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  155. methods.
  156. Note that you can also use the constants from TextEditor::ColourIds to change the
  157. colour of the text editor that is opened when a label is editable.
  158. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  159. */
  160. enum ColourIds
  161. {
  162. lassoFillColourId = 0x1000440, /**< The colour to fill the lasso rectangle with. */
  163. lassoOutlineColourId = 0x1000441, /**< The colour to draw the outline with. */
  164. };
  165. //==============================================================================
  166. /** @internal */
  167. void paint (Graphics& g)
  168. {
  169. g.fillAll (findColour (lassoFillColourId));
  170. g.setColour (findColour (lassoOutlineColourId));
  171. g.drawRect (0, 0, getWidth(), getHeight(), outlineThickness);
  172. // this suggests that you've left a lasso comp lying around after the
  173. // mouse drag has finished.. Be careful to call endLasso() when you get a
  174. // mouse-up event.
  175. jassert (isMouseButtonDownAnywhere());
  176. }
  177. /** @internal */
  178. bool hitTest (int, int) { return false; }
  179. private:
  180. //==============================================================================
  181. Array <SelectableItemType> originalSelection;
  182. LassoSource <SelectableItemType>* source;
  183. int outlineThickness;
  184. Point<int> dragStartPos;
  185. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LassoComponent);
  186. };
  187. #endif // __JUCE_LASSOCOMPONENT_JUCEHEADER__