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.
dexed/JuceLibraryCode/modules/juce_gui_basics/mouse/juce_ComponentDragger.cpp

62 lines
2.6 KiB

11 years ago
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
ComponentDragger::ComponentDragger() {}
ComponentDragger::~ComponentDragger() {}
//==============================================================================
void ComponentDragger::startDraggingComponent (Component* const componentToDrag, const MouseEvent& e)
{
jassert (componentToDrag != nullptr);
jassert (e.mods.isAnyMouseButtonDown()); // The event has to be a drag event!
if (componentToDrag != nullptr)
mouseDownWithinTarget = e.getEventRelativeTo (componentToDrag).getMouseDownPosition();
}
void ComponentDragger::dragComponent (Component* const componentToDrag, const MouseEvent& e,
ComponentBoundsConstrainer* const constrainer)
{
jassert (componentToDrag != nullptr);
jassert (e.mods.isAnyMouseButtonDown()); // The event has to be a drag event!
if (componentToDrag != nullptr)
{
Rectangle<int> bounds (componentToDrag->getBounds());
// If the component is a window, multiple mouse events can get queued while it's in the same position,
// so their coordinates become wrong after the first one moves the window, so in that case, we'll use
// the current mouse position instead of the one that the event contains...
if (componentToDrag->isOnDesktop())
bounds += componentToDrag->getLocalPoint (nullptr, e.source.getScreenPosition()) - mouseDownWithinTarget;
else
bounds += e.getEventRelativeTo (componentToDrag).getPosition() - mouseDownWithinTarget;
if (constrainer != nullptr)
constrainer->setBoundsForComponent (componentToDrag, bounds, false, false, false, false);
else
componentToDrag->setBounds (bounds);
}
}