OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <windowsx.h> | 5 #include <windowsx.h> |
6 | 6 |
7 #include "ui/base/events.h" | 7 #include "ui/base/events.h" |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "ui/base/keycodes/keyboard_code_conversion_win.h" | 10 #include "ui/base/keycodes/keyboard_code_conversion_win.h" |
11 #include "ui/gfx/point.h" | 11 #include "ui/gfx/point.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Get the native mouse key state from the native event message type. | 15 // Get the native mouse key state from the native event message type. |
16 int GetNativeMouseKey(const ui::NativeEvent& native_event) { | 16 int GetNativeMouseKey(const base::NativeEvent& native_event) { |
17 switch (native_event.message) { | 17 switch (native_event.message) { |
18 case WM_LBUTTONDBLCLK: | 18 case WM_LBUTTONDBLCLK: |
19 case WM_LBUTTONDOWN: | 19 case WM_LBUTTONDOWN: |
20 case WM_LBUTTONUP: | 20 case WM_LBUTTONUP: |
21 case WM_NCLBUTTONDBLCLK: | 21 case WM_NCLBUTTONDBLCLK: |
22 case WM_NCLBUTTONDOWN: | 22 case WM_NCLBUTTONDOWN: |
23 case WM_NCLBUTTONUP: | 23 case WM_NCLBUTTONUP: |
24 return MK_LBUTTON; | 24 return MK_LBUTTON; |
25 case WM_MBUTTONDBLCLK: | 25 case WM_MBUTTONDBLCLK: |
26 case WM_MBUTTONDOWN: | 26 case WM_MBUTTONDOWN: |
(...skipping 13 matching lines...) Expand all Loading... |
40 case WM_NCXBUTTONDOWN: | 40 case WM_NCXBUTTONDOWN: |
41 case WM_NCXBUTTONUP: | 41 case WM_NCXBUTTONUP: |
42 case WM_XBUTTONDBLCLK: | 42 case WM_XBUTTONDBLCLK: |
43 case WM_XBUTTONDOWN: | 43 case WM_XBUTTONDOWN: |
44 case WM_XBUTTONUP: | 44 case WM_XBUTTONUP: |
45 return MK_XBUTTON1; | 45 return MK_XBUTTON1; |
46 } | 46 } |
47 return 0; | 47 return 0; |
48 } | 48 } |
49 | 49 |
50 bool IsButtonDown(const ui::NativeEvent& native_event) { | 50 bool IsButtonDown(const base::NativeEvent& native_event) { |
51 return ((MK_LBUTTON | MK_MBUTTON | MK_RBUTTON | MK_XBUTTON1 | MK_XBUTTON2) & | 51 return ((MK_LBUTTON | MK_MBUTTON | MK_RBUTTON | MK_XBUTTON1 | MK_XBUTTON2) & |
52 native_event.wParam) != 0; | 52 native_event.wParam) != 0; |
53 } | 53 } |
54 | 54 |
55 bool IsClientMouseEvent(const ui::NativeEvent& native_event) { | 55 bool IsClientMouseEvent(const base::NativeEvent& native_event) { |
56 return native_event.message == WM_MOUSELEAVE || | 56 return native_event.message == WM_MOUSELEAVE || |
57 native_event.message == WM_MOUSEHOVER || | 57 native_event.message == WM_MOUSEHOVER || |
58 (native_event.message >= WM_MOUSEFIRST && | 58 (native_event.message >= WM_MOUSEFIRST && |
59 native_event.message <= WM_MOUSELAST); | 59 native_event.message <= WM_MOUSELAST); |
60 } | 60 } |
61 | 61 |
62 bool IsNonClientMouseEvent(const ui::NativeEvent& native_event) { | 62 bool IsNonClientMouseEvent(const base::NativeEvent& native_event) { |
63 return native_event.message == WM_NCMOUSELEAVE || | 63 return native_event.message == WM_NCMOUSELEAVE || |
64 native_event.message == WM_NCMOUSEHOVER || | 64 native_event.message == WM_NCMOUSEHOVER || |
65 (native_event.message >= WM_NCMOUSEMOVE && | 65 (native_event.message >= WM_NCMOUSEMOVE && |
66 native_event.message <= WM_NCXBUTTONDBLCLK); | 66 native_event.message <= WM_NCXBUTTONDBLCLK); |
67 } | 67 } |
68 | 68 |
69 bool IsDoubleClickMouseEvent(const ui::NativeEvent& native_event) { | 69 bool IsDoubleClickMouseEvent(const base::NativeEvent& native_event) { |
70 return native_event.message == WM_NCLBUTTONDBLCLK || | 70 return native_event.message == WM_NCLBUTTONDBLCLK || |
71 native_event.message == WM_NCMBUTTONDBLCLK || | 71 native_event.message == WM_NCMBUTTONDBLCLK || |
72 native_event.message == WM_NCRBUTTONDBLCLK || | 72 native_event.message == WM_NCRBUTTONDBLCLK || |
73 native_event.message == WM_NCXBUTTONDBLCLK || | 73 native_event.message == WM_NCXBUTTONDBLCLK || |
74 native_event.message == WM_LBUTTONDBLCLK || | 74 native_event.message == WM_LBUTTONDBLCLK || |
75 native_event.message == WM_MBUTTONDBLCLK || | 75 native_event.message == WM_MBUTTONDBLCLK || |
76 native_event.message == WM_RBUTTONDBLCLK || | 76 native_event.message == WM_RBUTTONDBLCLK || |
77 native_event.message == WM_XBUTTONDBLCLK; | 77 native_event.message == WM_XBUTTONDBLCLK; |
78 } | 78 } |
79 | 79 |
80 // Returns a mask corresponding to the set of pressed modifier keys. | 80 // Returns a mask corresponding to the set of pressed modifier keys. |
81 // Checks the current global state and the state sent by client mouse messages. | 81 // Checks the current global state and the state sent by client mouse messages. |
82 int KeyStateFlagsFromNative(const ui::NativeEvent& native_event) { | 82 int KeyStateFlagsFromNative(const base::NativeEvent& native_event) { |
83 int flags = 0; | 83 int flags = 0; |
84 flags |= (GetKeyState(VK_MENU) & 0x80) ? ui::EF_ALT_DOWN : 0; | 84 flags |= (GetKeyState(VK_MENU) & 0x80) ? ui::EF_ALT_DOWN : 0; |
85 flags |= (GetKeyState(VK_SHIFT) & 0x80) ? ui::EF_SHIFT_DOWN : 0; | 85 flags |= (GetKeyState(VK_SHIFT) & 0x80) ? ui::EF_SHIFT_DOWN : 0; |
86 flags |= (GetKeyState(VK_CONTROL) & 0x80) ? ui::EF_CONTROL_DOWN : 0; | 86 flags |= (GetKeyState(VK_CONTROL) & 0x80) ? ui::EF_CONTROL_DOWN : 0; |
87 flags |= (HIWORD(native_event.lParam) & KF_EXTENDED) ? ui::EF_EXTENDED : 0; | 87 flags |= (HIWORD(native_event.lParam) & KF_EXTENDED) ? ui::EF_EXTENDED : 0; |
88 | 88 |
89 // Most client mouse messages include key state information. | 89 // Most client mouse messages include key state information. |
90 if (IsClientMouseEvent(native_event)) { | 90 if (IsClientMouseEvent(native_event)) { |
91 int win_flags = GET_KEYSTATE_WPARAM(native_event.wParam); | 91 int win_flags = GET_KEYSTATE_WPARAM(native_event.wParam); |
92 flags |= (win_flags & MK_SHIFT) ? ui::EF_SHIFT_DOWN : 0; | 92 flags |= (win_flags & MK_SHIFT) ? ui::EF_SHIFT_DOWN : 0; |
93 flags |= (win_flags & MK_CONTROL) ? ui::EF_CONTROL_DOWN : 0; | 93 flags |= (win_flags & MK_CONTROL) ? ui::EF_CONTROL_DOWN : 0; |
94 } | 94 } |
95 | 95 |
96 return flags; | 96 return flags; |
97 } | 97 } |
98 | 98 |
99 // Returns a mask corresponding to the set of pressed mouse buttons. | 99 // Returns a mask corresponding to the set of pressed mouse buttons. |
100 // This includes the button of the given message, even if it is being released. | 100 // This includes the button of the given message, even if it is being released. |
101 int MouseStateFlagsFromNative(const ui::NativeEvent& native_event) { | 101 int MouseStateFlagsFromNative(const base::NativeEvent& native_event) { |
102 // TODO(msw): ORing the pressed/released button into the flags is _wrong_. | 102 // TODO(msw): ORing the pressed/released button into the flags is _wrong_. |
103 // It makes it impossible to tell which button was modified when multiple | 103 // It makes it impossible to tell which button was modified when multiple |
104 // buttons are/were held down. Instead, we need to track the modified button | 104 // buttons are/were held down. Instead, we need to track the modified button |
105 // independently and audit event consumers to do the right thing. | 105 // independently and audit event consumers to do the right thing. |
106 int win_flags = GetNativeMouseKey(native_event); | 106 int win_flags = GetNativeMouseKey(native_event); |
107 | 107 |
108 // Client mouse messages provide key states in their WPARAMs. | 108 // Client mouse messages provide key states in their WPARAMs. |
109 if (IsClientMouseEvent(native_event)) | 109 if (IsClientMouseEvent(native_event)) |
110 win_flags |= GET_KEYSTATE_WPARAM(native_event.wParam); | 110 win_flags |= GET_KEYSTATE_WPARAM(native_event.wParam); |
111 | 111 |
112 int flags = 0; | 112 int flags = 0; |
113 flags |= (win_flags & MK_LBUTTON) ? ui::EF_LEFT_BUTTON_DOWN : 0; | 113 flags |= (win_flags & MK_LBUTTON) ? ui::EF_LEFT_BUTTON_DOWN : 0; |
114 flags |= (win_flags & MK_MBUTTON) ? ui::EF_MIDDLE_BUTTON_DOWN : 0; | 114 flags |= (win_flags & MK_MBUTTON) ? ui::EF_MIDDLE_BUTTON_DOWN : 0; |
115 flags |= (win_flags & MK_RBUTTON) ? ui::EF_RIGHT_BUTTON_DOWN : 0; | 115 flags |= (win_flags & MK_RBUTTON) ? ui::EF_RIGHT_BUTTON_DOWN : 0; |
116 flags |= IsDoubleClickMouseEvent(native_event) ? ui::EF_IS_DOUBLE_CLICK: 0; | 116 flags |= IsDoubleClickMouseEvent(native_event) ? ui::EF_IS_DOUBLE_CLICK: 0; |
117 flags |= IsNonClientMouseEvent(native_event) ? ui::EF_IS_NON_CLIENT : 0; | 117 flags |= IsNonClientMouseEvent(native_event) ? ui::EF_IS_NON_CLIENT : 0; |
118 return flags; | 118 return flags; |
119 } | 119 } |
120 | 120 |
121 } // namespace | 121 } // namespace |
122 | 122 |
123 namespace ui { | 123 namespace ui { |
124 | 124 |
125 EventType EventTypeFromNative(const NativeEvent& native_event) { | 125 EventType EventTypeFromNative(const base::NativeEvent& native_event) { |
126 switch (native_event.message) { | 126 switch (native_event.message) { |
127 case WM_KEYDOWN: | 127 case WM_KEYDOWN: |
128 case WM_SYSKEYDOWN: | 128 case WM_SYSKEYDOWN: |
129 case WM_CHAR: | 129 case WM_CHAR: |
130 return ET_KEY_PRESSED; | 130 return ET_KEY_PRESSED; |
131 case WM_KEYUP: | 131 case WM_KEYUP: |
132 case WM_SYSKEYUP: | 132 case WM_SYSKEYUP: |
133 return ET_KEY_RELEASED; | 133 return ET_KEY_RELEASED; |
134 case WM_LBUTTONDBLCLK: | 134 case WM_LBUTTONDBLCLK: |
135 case WM_LBUTTONDOWN: | 135 case WM_LBUTTONDOWN: |
(...skipping 29 matching lines...) Expand all Loading... |
165 return ET_MOUSEWHEEL; | 165 return ET_MOUSEWHEEL; |
166 case WM_MOUSELEAVE: | 166 case WM_MOUSELEAVE: |
167 case WM_NCMOUSELEAVE: | 167 case WM_NCMOUSELEAVE: |
168 return ET_MOUSE_EXITED; | 168 return ET_MOUSE_EXITED; |
169 default: | 169 default: |
170 NOTREACHED(); | 170 NOTREACHED(); |
171 } | 171 } |
172 return ET_UNKNOWN; | 172 return ET_UNKNOWN; |
173 } | 173 } |
174 | 174 |
175 int EventFlagsFromNative(const NativeEvent& native_event) { | 175 int EventFlagsFromNative(const base::NativeEvent& native_event) { |
176 int flags = KeyStateFlagsFromNative(native_event); | 176 int flags = KeyStateFlagsFromNative(native_event); |
177 if (IsMouseEvent(native_event)) | 177 if (IsMouseEvent(native_event)) |
178 flags |= MouseStateFlagsFromNative(native_event); | 178 flags |= MouseStateFlagsFromNative(native_event); |
179 | 179 |
180 return flags; | 180 return flags; |
181 } | 181 } |
182 | 182 |
183 gfx::Point EventLocationFromNative(const NativeEvent& native_event) { | 183 gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) { |
184 // Client message. The position is contained in the LPARAM. | 184 // Client message. The position is contained in the LPARAM. |
185 if (IsClientMouseEvent(native_event)) | 185 if (IsClientMouseEvent(native_event)) |
186 return gfx::Point(native_event.lParam); | 186 return gfx::Point(native_event.lParam); |
187 DCHECK(IsNonClientMouseEvent(native_event)); | 187 DCHECK(IsNonClientMouseEvent(native_event)); |
188 // Non-client message. The position is contained in a POINTS structure in | 188 // Non-client message. The position is contained in a POINTS structure in |
189 // LPARAM, and is in screen coordinates so we have to convert to client. | 189 // LPARAM, and is in screen coordinates so we have to convert to client. |
190 POINT native_point = { GET_X_LPARAM(native_event.lParam), | 190 POINT native_point = { GET_X_LPARAM(native_event.lParam), |
191 GET_Y_LPARAM(native_event.lParam) }; | 191 GET_Y_LPARAM(native_event.lParam) }; |
192 ScreenToClient(native_event.hwnd, &native_point); | 192 ScreenToClient(native_event.hwnd, &native_point); |
193 return gfx::Point(native_point); | 193 return gfx::Point(native_point); |
194 } | 194 } |
195 | 195 |
196 KeyboardCode KeyboardCodeFromNative(const NativeEvent& native_event) { | 196 KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) { |
197 return KeyboardCodeForWindowsKeyCode(native_event.wParam); | 197 return KeyboardCodeForWindowsKeyCode(native_event.wParam); |
198 } | 198 } |
199 | 199 |
200 bool IsMouseEvent(const NativeEvent& native_event) { | 200 bool IsMouseEvent(const base::NativeEvent& native_event) { |
201 return IsClientMouseEvent(native_event) || | 201 return IsClientMouseEvent(native_event) || |
202 IsNonClientMouseEvent(native_event); | 202 IsNonClientMouseEvent(native_event); |
203 } | 203 } |
204 | 204 |
205 int GetMouseWheelOffset(const NativeEvent& native_event) { | 205 int GetMouseWheelOffset(const base::NativeEvent& native_event) { |
206 DCHECK(native_event.message == WM_MOUSEWHEEL); | 206 DCHECK(native_event.message == WM_MOUSEWHEEL); |
207 return GET_WHEEL_DELTA_WPARAM(native_event.wParam); | 207 return GET_WHEEL_DELTA_WPARAM(native_event.wParam); |
208 } | 208 } |
209 | 209 |
210 } // namespace ui | 210 } // namespace ui |
OLD | NEW |