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.

97 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #pragma once
  18. //==============================================================================
  19. /**
  20. An object to take care of the logic for dragging components around with the mouse.
  21. Very easy to use - in your mouseDown() callback, call startDraggingComponent(),
  22. then in your mouseDrag() callback, call dragComponent().
  23. When starting a drag, you can give it a ComponentBoundsConstrainer to use
  24. to limit the component's position and keep it on-screen.
  25. e.g. @code
  26. class MyDraggableComp
  27. {
  28. ComponentDragger myDragger;
  29. void mouseDown (const MouseEvent& e)
  30. {
  31. myDragger.startDraggingComponent (this, e);
  32. }
  33. void mouseDrag (const MouseEvent& e)
  34. {
  35. myDragger.dragComponent (this, e, nullptr);
  36. }
  37. };
  38. @endcode
  39. */
  40. class JUCE_API ComponentDragger
  41. {
  42. public:
  43. //==============================================================================
  44. /** Creates a ComponentDragger. */
  45. ComponentDragger();
  46. /** Destructor. */
  47. virtual ~ComponentDragger();
  48. //==============================================================================
  49. /** Call this from your component's mouseDown() method, to prepare for dragging.
  50. @param componentToDrag the component that you want to drag
  51. @param e the mouse event that is triggering the drag
  52. @see dragComponent
  53. */
  54. void startDraggingComponent (Component* componentToDrag,
  55. const MouseEvent& e);
  56. /** Call this from your mouseDrag() callback to move the component.
  57. This will move the component, using the given constrainer object to check
  58. the new position.
  59. @param componentToDrag the component that you want to drag
  60. @param e the current mouse-drag event
  61. @param constrainer an optional constrainer object that should be used
  62. to apply limits to the component's position. Pass
  63. null if you don't want to contrain the movement.
  64. @see startDraggingComponent
  65. */
  66. void dragComponent (Component* componentToDrag,
  67. const MouseEvent& e,
  68. ComponentBoundsConstrainer* constrainer);
  69. private:
  70. //==============================================================================
  71. Point<int> mouseDownWithinTarget;
  72. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentDragger)
  73. };