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.

222 lines
8.4KB

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