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.

106 lines
4.0KB

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