| 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 bool IsKeyEvent(const ui::NativeEvent& native_event) { | 80 bool IsKeyEvent(const base::NativeEvent& native_event) { |
| 81 return native_event.message == WM_KEYDOWN || | 81 return native_event.message == WM_KEYDOWN || |
| 82 native_event.message == WM_SYSKEYDOWN || | 82 native_event.message == WM_SYSKEYDOWN || |
| 83 native_event.message == WM_CHAR || | 83 native_event.message == WM_CHAR || |
| 84 native_event.message == WM_KEYUP || | 84 native_event.message == WM_KEYUP || |
| 85 native_event.message == WM_SYSKEYUP; | 85 native_event.message == WM_SYSKEYUP; |
| 86 } | 86 } |
| 87 | 87 |
| 88 // Returns a mask corresponding to the set of pressed modifier keys. | 88 // Returns a mask corresponding to the set of pressed modifier keys. |
| 89 // Checks the current global state and the state sent by client mouse messages. | 89 // Checks the current global state and the state sent by client mouse messages. |
| 90 int KeyStateFlagsFromNative(const ui::NativeEvent& native_event) { | 90 int KeyStateFlagsFromNative(const base::NativeEvent& native_event) { |
| 91 int flags = 0; | 91 int flags = 0; |
| 92 flags |= (GetKeyState(VK_MENU) & 0x80) ? ui::EF_ALT_DOWN : 0; | 92 flags |= (GetKeyState(VK_MENU) & 0x80) ? ui::EF_ALT_DOWN : 0; |
| 93 flags |= (GetKeyState(VK_SHIFT) & 0x80) ? ui::EF_SHIFT_DOWN : 0; | 93 flags |= (GetKeyState(VK_SHIFT) & 0x80) ? ui::EF_SHIFT_DOWN : 0; |
| 94 flags |= (GetKeyState(VK_CONTROL) & 0x80) ? ui::EF_CONTROL_DOWN : 0; | 94 flags |= (GetKeyState(VK_CONTROL) & 0x80) ? ui::EF_CONTROL_DOWN : 0; |
| 95 | 95 |
| 96 // Check key messages for the extended key flag. | 96 // Check key messages for the extended key flag. |
| 97 if (IsKeyEvent(native_event)) | 97 if (IsKeyEvent(native_event)) |
| 98 flags |= (HIWORD(native_event.lParam) & KF_EXTENDED) ? ui::EF_EXTENDED : 0; | 98 flags |= (HIWORD(native_event.lParam) & KF_EXTENDED) ? ui::EF_EXTENDED : 0; |
| 99 | 99 |
| 100 // Most client mouse messages include key state information. | 100 // Most client mouse messages include key state information. |
| 101 if (IsClientMouseEvent(native_event)) { | 101 if (IsClientMouseEvent(native_event)) { |
| 102 int win_flags = GET_KEYSTATE_WPARAM(native_event.wParam); | 102 int win_flags = GET_KEYSTATE_WPARAM(native_event.wParam); |
| 103 flags |= (win_flags & MK_SHIFT) ? ui::EF_SHIFT_DOWN : 0; | 103 flags |= (win_flags & MK_SHIFT) ? ui::EF_SHIFT_DOWN : 0; |
| 104 flags |= (win_flags & MK_CONTROL) ? ui::EF_CONTROL_DOWN : 0; | 104 flags |= (win_flags & MK_CONTROL) ? ui::EF_CONTROL_DOWN : 0; |
| 105 } | 105 } |
| 106 | 106 |
| 107 return flags; | 107 return flags; |
| 108 } | 108 } |
| 109 | 109 |
| 110 // Returns a mask corresponding to the set of pressed mouse buttons. | 110 // Returns a mask corresponding to the set of pressed mouse buttons. |
| 111 // This includes the button of the given message, even if it is being released. | 111 // This includes the button of the given message, even if it is being released. |
| 112 int MouseStateFlagsFromNative(const ui::NativeEvent& native_event) { | 112 int MouseStateFlagsFromNative(const base::NativeEvent& native_event) { |
| 113 // TODO(msw): ORing the pressed/released button into the flags is _wrong_. | 113 // TODO(msw): ORing the pressed/released button into the flags is _wrong_. |
| 114 // It makes it impossible to tell which button was modified when multiple | 114 // It makes it impossible to tell which button was modified when multiple |
| 115 // buttons are/were held down. Instead, we need to track the modified button | 115 // buttons are/were held down. Instead, we need to track the modified button |
| 116 // independently and audit event consumers to do the right thing. | 116 // independently and audit event consumers to do the right thing. |
| 117 int win_flags = GetNativeMouseKey(native_event); | 117 int win_flags = GetNativeMouseKey(native_event); |
| 118 | 118 |
| 119 // Client mouse messages provide key states in their WPARAMs. | 119 // Client mouse messages provide key states in their WPARAMs. |
| 120 if (IsClientMouseEvent(native_event)) | 120 if (IsClientMouseEvent(native_event)) |
| 121 win_flags |= GET_KEYSTATE_WPARAM(native_event.wParam); | 121 win_flags |= GET_KEYSTATE_WPARAM(native_event.wParam); |
| 122 | 122 |
| 123 int flags = 0; | 123 int flags = 0; |
| 124 flags |= (win_flags & MK_LBUTTON) ? ui::EF_LEFT_BUTTON_DOWN : 0; | 124 flags |= (win_flags & MK_LBUTTON) ? ui::EF_LEFT_BUTTON_DOWN : 0; |
| 125 flags |= (win_flags & MK_MBUTTON) ? ui::EF_MIDDLE_BUTTON_DOWN : 0; | 125 flags |= (win_flags & MK_MBUTTON) ? ui::EF_MIDDLE_BUTTON_DOWN : 0; |
| 126 flags |= (win_flags & MK_RBUTTON) ? ui::EF_RIGHT_BUTTON_DOWN : 0; | 126 flags |= (win_flags & MK_RBUTTON) ? ui::EF_RIGHT_BUTTON_DOWN : 0; |
| 127 flags |= IsDoubleClickMouseEvent(native_event) ? ui::EF_IS_DOUBLE_CLICK: 0; | 127 flags |= IsDoubleClickMouseEvent(native_event) ? ui::EF_IS_DOUBLE_CLICK: 0; |
| 128 flags |= IsNonClientMouseEvent(native_event) ? ui::EF_IS_NON_CLIENT : 0; | 128 flags |= IsNonClientMouseEvent(native_event) ? ui::EF_IS_NON_CLIENT : 0; |
| 129 return flags; | 129 return flags; |
| 130 } | 130 } |
| 131 | 131 |
| 132 } // namespace | 132 } // namespace |
| 133 | 133 |
| 134 namespace ui { | 134 namespace ui { |
| 135 | 135 |
| 136 EventType EventTypeFromNative(const NativeEvent& native_event) { | 136 EventType EventTypeFromNative(const base::NativeEvent& native_event) { |
| 137 switch (native_event.message) { | 137 switch (native_event.message) { |
| 138 case WM_KEYDOWN: | 138 case WM_KEYDOWN: |
| 139 case WM_SYSKEYDOWN: | 139 case WM_SYSKEYDOWN: |
| 140 case WM_CHAR: | 140 case WM_CHAR: |
| 141 return ET_KEY_PRESSED; | 141 return ET_KEY_PRESSED; |
| 142 case WM_KEYUP: | 142 case WM_KEYUP: |
| 143 case WM_SYSKEYUP: | 143 case WM_SYSKEYUP: |
| 144 return ET_KEY_RELEASED; | 144 return ET_KEY_RELEASED; |
| 145 case WM_LBUTTONDBLCLK: | 145 case WM_LBUTTONDBLCLK: |
| 146 case WM_LBUTTONDOWN: | 146 case WM_LBUTTONDOWN: |
| (...skipping 29 matching lines...) Expand all Loading... |
| 176 return ET_MOUSEWHEEL; | 176 return ET_MOUSEWHEEL; |
| 177 case WM_MOUSELEAVE: | 177 case WM_MOUSELEAVE: |
| 178 case WM_NCMOUSELEAVE: | 178 case WM_NCMOUSELEAVE: |
| 179 return ET_MOUSE_EXITED; | 179 return ET_MOUSE_EXITED; |
| 180 default: | 180 default: |
| 181 NOTREACHED(); | 181 NOTREACHED(); |
| 182 } | 182 } |
| 183 return ET_UNKNOWN; | 183 return ET_UNKNOWN; |
| 184 } | 184 } |
| 185 | 185 |
| 186 int EventFlagsFromNative(const NativeEvent& native_event) { | 186 int EventFlagsFromNative(const base::NativeEvent& native_event) { |
| 187 int flags = KeyStateFlagsFromNative(native_event); | 187 int flags = KeyStateFlagsFromNative(native_event); |
| 188 if (IsMouseEvent(native_event)) | 188 if (IsMouseEvent(native_event)) |
| 189 flags |= MouseStateFlagsFromNative(native_event); | 189 flags |= MouseStateFlagsFromNative(native_event); |
| 190 | 190 |
| 191 return flags; | 191 return flags; |
| 192 } | 192 } |
| 193 | 193 |
| 194 gfx::Point EventLocationFromNative(const NativeEvent& native_event) { | 194 gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) { |
| 195 // Client message. The position is contained in the LPARAM. | 195 // Client message. The position is contained in the LPARAM. |
| 196 if (IsClientMouseEvent(native_event)) | 196 if (IsClientMouseEvent(native_event)) |
| 197 return gfx::Point(native_event.lParam); | 197 return gfx::Point(native_event.lParam); |
| 198 DCHECK(IsNonClientMouseEvent(native_event)); | 198 DCHECK(IsNonClientMouseEvent(native_event)); |
| 199 // Non-client message. The position is contained in a POINTS structure in | 199 // Non-client message. The position is contained in a POINTS structure in |
| 200 // LPARAM, and is in screen coordinates so we have to convert to client. | 200 // LPARAM, and is in screen coordinates so we have to convert to client. |
| 201 POINT native_point = { GET_X_LPARAM(native_event.lParam), | 201 POINT native_point = { GET_X_LPARAM(native_event.lParam), |
| 202 GET_Y_LPARAM(native_event.lParam) }; | 202 GET_Y_LPARAM(native_event.lParam) }; |
| 203 ScreenToClient(native_event.hwnd, &native_point); | 203 ScreenToClient(native_event.hwnd, &native_point); |
| 204 return gfx::Point(native_point); | 204 return gfx::Point(native_point); |
| 205 } | 205 } |
| 206 | 206 |
| 207 KeyboardCode KeyboardCodeFromNative(const NativeEvent& native_event) { | 207 KeyboardCode KeyboardCodeFromNative(const base::NativeEvent& native_event) { |
| 208 return KeyboardCodeForWindowsKeyCode(native_event.wParam); | 208 return KeyboardCodeForWindowsKeyCode(native_event.wParam); |
| 209 } | 209 } |
| 210 | 210 |
| 211 bool IsMouseEvent(const NativeEvent& native_event) { | 211 bool IsMouseEvent(const base::NativeEvent& native_event) { |
| 212 return IsClientMouseEvent(native_event) || | 212 return IsClientMouseEvent(native_event) || |
| 213 IsNonClientMouseEvent(native_event); | 213 IsNonClientMouseEvent(native_event); |
| 214 } | 214 } |
| 215 | 215 |
| 216 int GetMouseWheelOffset(const NativeEvent& native_event) { | 216 int GetMouseWheelOffset(const base::NativeEvent& native_event) { |
| 217 DCHECK(native_event.message == WM_MOUSEWHEEL); | 217 DCHECK(native_event.message == WM_MOUSEWHEEL); |
| 218 return GET_WHEEL_DELTA_WPARAM(native_event.wParam); | 218 return GET_WHEEL_DELTA_WPARAM(native_event.wParam); |
| 219 } | 219 } |
| 220 | 220 |
| 221 } // namespace ui | 221 } // namespace ui |
| OLD | NEW |