1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #ifndef QWINDOWSMOUSEHANDLER_H
41 #define QWINDOWSMOUSEHANDLER_H
42
43 #include "qtwindowsglobal.h"
44 #include <QtCore/qt_windows.h>
45
46 #include <QtCore/qpointer.h>
47 #include <QtCore/qhash.h>
48 #include <QtGui/qevent.h>
49
50 QT_BEGIN_NAMESPACE
51
52 class QWindow;
53 class QTouchDevice;
54
55 class QWindowsMouseHandler
56 {
57 Q_DISABLE_COPY_MOVE(QWindowsMouseHandler)
58 public:
59 QWindowsMouseHandler();
60
touchDevice()61 QTouchDevice *touchDevice() const { return m_touchDevice; }
62 QTouchDevice *ensureTouchDevice();
63
64 bool translateMouseEvent(QWindow *widget, HWND hwnd,
65 QtWindows::WindowsEventType t, MSG msg,
66 LRESULT *result);
67 bool translateTouchEvent(QWindow *widget, HWND hwnd,
68 QtWindows::WindowsEventType t, MSG msg,
69 LRESULT *result);
70 bool translateGestureEvent(QWindow *window, HWND hwnd,
71 QtWindows::WindowsEventType,
72 MSG msg, LRESULT *);
73 bool translateScrollEvent(QWindow *window, HWND hwnd,
74 MSG msg, LRESULT *result);
75
76 static inline Qt::MouseButtons keyStateToMouseButtons(WPARAM);
77 static inline Qt::KeyboardModifiers keyStateToModifiers(int);
78 static inline int mouseButtonsToKeyState(Qt::MouseButtons);
79
80 static Qt::MouseButtons queryMouseButtons();
windowUnderMouse()81 QWindow *windowUnderMouse() const { return m_windowUnderMouse.data(); }
clearWindowUnderMouse()82 void clearWindowUnderMouse() { m_windowUnderMouse = nullptr; }
83 void clearEvents();
84
85 private:
86 inline bool translateMouseWheelEvent(QWindow *window, HWND hwnd,
87 MSG msg, LRESULT *result);
88
89 QPointer<QWindow> m_windowUnderMouse;
90 QPointer<QWindow> m_trackedWindow;
91 QHash<DWORD, int> m_touchInputIDToTouchPointID;
92 QHash<int, QPointF> m_lastTouchPositions;
93 QTouchDevice *m_touchDevice = nullptr;
94 bool m_leftButtonDown = false;
95 QWindow *m_previousCaptureWindow = nullptr;
96 QEvent::Type m_lastEventType = QEvent::None;
97 Qt::MouseButton m_lastEventButton = Qt::NoButton;
98 };
99
keyStateToMouseButtons(WPARAM wParam)100 Qt::MouseButtons QWindowsMouseHandler::keyStateToMouseButtons(WPARAM wParam)
101 {
102 Qt::MouseButtons mb(Qt::NoButton);
103 if (wParam & MK_LBUTTON)
104 mb |= Qt::LeftButton;
105 if (wParam & MK_MBUTTON)
106 mb |= Qt::MiddleButton;
107 if (wParam & MK_RBUTTON)
108 mb |= Qt::RightButton;
109 if (wParam & MK_XBUTTON1)
110 mb |= Qt::XButton1;
111 if (wParam & MK_XBUTTON2)
112 mb |= Qt::XButton2;
113 return mb;
114 }
115
keyStateToModifiers(int wParam)116 Qt::KeyboardModifiers QWindowsMouseHandler::keyStateToModifiers(int wParam)
117 {
118 Qt::KeyboardModifiers mods(Qt::NoModifier);
119 if (wParam & MK_CONTROL)
120 mods |= Qt::ControlModifier;
121 if (wParam & MK_SHIFT)
122 mods |= Qt::ShiftModifier;
123 if (GetKeyState(VK_MENU) < 0)
124 mods |= Qt::AltModifier;
125 return mods;
126 }
127
mouseButtonsToKeyState(Qt::MouseButtons mb)128 int QWindowsMouseHandler::mouseButtonsToKeyState(Qt::MouseButtons mb)
129 {
130 int result = 0;
131 if (mb & Qt::LeftButton)
132 result |= MK_LBUTTON;
133 if (mb & Qt::MiddleButton)
134 result |= MK_MBUTTON;
135 if (mb & Qt::RightButton)
136 result |= MK_RBUTTON;
137 if (mb & Qt::XButton1)
138 result |= MK_XBUTTON1;
139 if (mb & Qt::XButton2)
140 result |= MK_XBUTTON2;
141 return result;
142 }
143
144 QT_END_NAMESPACE
145
146 #endif // QWINDOWSMOUSEHANDLER_H
147