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.

61 lines
2.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. ComponentDragger::ComponentDragger() {}
  16. ComponentDragger::~ComponentDragger() {}
  17. //==============================================================================
  18. void ComponentDragger::startDraggingComponent (Component* const componentToDrag, const MouseEvent& e)
  19. {
  20. jassert (componentToDrag != nullptr);
  21. jassert (e.mods.isAnyMouseButtonDown()); // The event has to be a drag event!
  22. if (componentToDrag != nullptr)
  23. mouseDownWithinTarget = e.getEventRelativeTo (componentToDrag).getMouseDownPosition();
  24. }
  25. void ComponentDragger::dragComponent (Component* const componentToDrag, const MouseEvent& e,
  26. ComponentBoundsConstrainer* const constrainer)
  27. {
  28. jassert (componentToDrag != nullptr);
  29. jassert (e.mods.isAnyMouseButtonDown()); // The event has to be a drag event!
  30. if (componentToDrag != nullptr)
  31. {
  32. auto bounds = componentToDrag->getBounds();
  33. // If the component is a window, multiple mouse events can get queued while it's in the same position,
  34. // so their coordinates become wrong after the first one moves the window, so in that case, we'll use
  35. // the current mouse position instead of the one that the event contains...
  36. if (componentToDrag->isOnDesktop())
  37. bounds += componentToDrag->getLocalPoint (nullptr, e.source.getScreenPosition()).roundToInt() - mouseDownWithinTarget;
  38. else
  39. bounds += e.getEventRelativeTo (componentToDrag).getPosition() - mouseDownWithinTarget;
  40. if (constrainer != nullptr)
  41. constrainer->setBoundsForComponent (componentToDrag, bounds, false, false, false, false);
  42. else
  43. componentToDrag->setBounds (bounds);
  44. }
  45. }
  46. } // namespace juce