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.

105 lines
3.8KB

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