1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#include "FreezePointer.h"
#include <wx/window.h>
#include "debugging/debugging.h"
#include "MouseButton.h"
namespace wxutil
{
void FreezePointer::startCapture(wxWindow* window,
const MotionFunction& motionDelta,
const CaptureLostFunction& endMove,
bool freezePointer,
bool hidePointer,
bool motionReceivesDeltas)
{
assert(window);
ASSERT_MESSAGE(motionDelta, "can't capture pointer");
ASSERT_MESSAGE(endMove, "can't capture pointer");
// Store the flags before going ahead
_freezePointer = freezePointer;
_hidePointer = hidePointer;
_motionReceivesDeltas = motionReceivesDeltas;
// Find the toplevel window
wxWindow* topLevel = wxGetTopLevelParent(window);
if (_hidePointer)
{
window->SetCursor(wxCursor(wxCURSOR_BLANK));
}
// We capture the mouse on the toplevel app, coordinates
// are measured relative to the child window
if (!topLevel->HasCapture())
{
topLevel->CaptureMouse();
}
_capturedWindow = window;
wxPoint windowMousePos = _capturedWindow->ScreenToClient(wxGetMousePosition());
_freezePosX = windowMousePos.x;
_freezePosY = windowMousePos.y;
if (_freezePointer)
{
_capturedWindow->WarpPointer(_freezePosX, _freezePosY);
}
_motionFunction = motionDelta;
_captureLostFunction = endMove;
topLevel->Connect(wxEVT_MOTION, wxMouseEventHandler(FreezePointer::onMouseMotion), NULL, this);
topLevel->Connect(wxEVT_LEFT_UP, wxMouseEventHandler(FreezePointer::onMouseUp), NULL, this);
topLevel->Connect(wxEVT_RIGHT_UP, wxMouseEventHandler(FreezePointer::onMouseUp), NULL, this);
topLevel->Connect(wxEVT_MIDDLE_UP, wxMouseEventHandler(FreezePointer::onMouseUp), NULL, this);
topLevel->Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(FreezePointer::onMouseDown), NULL, this);
topLevel->Connect(wxEVT_RIGHT_DOWN, wxMouseEventHandler(FreezePointer::onMouseDown), NULL, this);
topLevel->Connect(wxEVT_MIDDLE_DOWN, wxMouseEventHandler(FreezePointer::onMouseDown), NULL, this);
topLevel->Connect(wxEVT_MOUSE_CAPTURE_LOST, wxMouseCaptureLostEventHandler(FreezePointer::onMouseCaptureLost), NULL, this);
}
bool FreezePointer::isCapturing(wxWindow* window) const
{
return _capturedWindow != nullptr;
}
void FreezePointer::endCapture()
{
if (!_capturedWindow)
{
return; // safeguard against duplicate unfreeze() calls
}
wxWindow* window = _capturedWindow;
wxWindow* topLevel = wxGetTopLevelParent(window);
_capturedWindow = nullptr;
_motionFunction = MotionFunction();
_captureLostFunction = CaptureLostFunction();
if (_freezePointer)
{
window->WarpPointer(_freezePosX, _freezePosY);
}
if (_hidePointer)
{
window->SetCursor(wxCursor(wxCURSOR_DEFAULT));
}
if (topLevel->HasCapture())
{
topLevel->ReleaseMouse();
}
topLevel->Disconnect(wxEVT_MOUSE_CAPTURE_LOST, wxMouseCaptureLostEventHandler(FreezePointer::onMouseCaptureLost), NULL, this);
topLevel->Disconnect(wxEVT_MOTION, wxMouseEventHandler(FreezePointer::onMouseMotion), NULL, this);
topLevel->Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(FreezePointer::onMouseUp), NULL, this);
topLevel->Disconnect(wxEVT_RIGHT_UP, wxMouseEventHandler(FreezePointer::onMouseUp), NULL, this);
topLevel->Disconnect(wxEVT_MIDDLE_UP, wxMouseEventHandler(FreezePointer::onMouseUp), NULL, this);
topLevel->Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(FreezePointer::onMouseDown), NULL, this);
topLevel->Disconnect(wxEVT_RIGHT_DOWN, wxMouseEventHandler(FreezePointer::onMouseDown), NULL, this);
topLevel->Disconnect(wxEVT_MIDDLE_DOWN, wxMouseEventHandler(FreezePointer::onMouseDown), NULL, this);
}
void FreezePointer::connectMouseEvents(const MouseEventFunction& onMouseDown,
const MouseEventFunction& onMouseUp)
{
_onMouseUp = onMouseUp;
_onMouseDown = onMouseDown;
}
void FreezePointer::disconnectMouseEvents()
{
_onMouseUp = MouseEventFunction();
_onMouseDown = MouseEventFunction();
}
void FreezePointer::onMouseDown(wxMouseEvent& ev)
{
if (_onMouseDown && _capturedWindow)
{
// The connected mouse up event expects window coordinates,
// not coordinates relative to the captured window
wxMouseEvent copy(ev);
wxPoint windowMousePos = _capturedWindow->ScreenToClient(wxGetMousePosition());
copy.SetX(windowMousePos.x);
copy.SetY(windowMousePos.y);
_onMouseDown(copy);
}
}
void FreezePointer::onMouseUp(wxMouseEvent& ev)
{
if (_onMouseUp && _capturedWindow)
{
// The connected mouse up event expects window coordinates,
// not coordinates relative to the captured window
wxMouseEvent copy(ev);
wxPoint windowMousePos = _capturedWindow->ScreenToClient(wxGetMousePosition());
copy.SetX(windowMousePos.x);
copy.SetY(windowMousePos.y);
_onMouseUp(copy);
}
}
void FreezePointer::onMouseMotion(wxMouseEvent& ev)
{
if (!_capturedWindow) return;
wxPoint windowMousePos = _capturedWindow->ScreenToClient(wxGetMousePosition());
int dx = windowMousePos.x - _freezePosX;
int dy = windowMousePos.y - _freezePosY;
if (dx != 0 || dy != 0)
{
if (_freezePointer)
{
// Force the mouse cursor to stay where it is
_capturedWindow->WarpPointer(_freezePosX, _freezePosY);
}
else
{
// Non-freezing, update the reference point for the next delta
_freezePosX = windowMousePos.x;
_freezePosY = windowMousePos.y;
}
if (_motionFunction)
{
if (_motionReceivesDeltas)
{
_motionFunction(dx, dy, MouseButton::GetStateForMouseEvent(ev));
}
else
{
_motionFunction(windowMousePos.x, windowMousePos.y, MouseButton::GetStateForMouseEvent(ev));
}
}
}
ev.Skip();
}
void FreezePointer::onMouseCaptureLost(wxMouseCaptureLostEvent& ev)
{
if (_captureLostFunction)
{
_captureLostFunction();
}
// Regardless of what the client does, we need to end capture now
endCapture();
}
} // namespace
|