OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <windowsx.h> |
| 6 |
| 7 #include "ui/base/events.h" |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "ui/base/keycodes/keyboard_code_conversion_win.h" |
| 11 #include "ui/gfx/point.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // Get the native mouse key state from the native event message type. |
| 16 int GetNativeMouseKey(const ui::NativeEvent& native_event) { |
| 17 switch (native_event.message) { |
| 18 case WM_LBUTTONDBLCLK: |
| 19 case WM_LBUTTONDOWN: |
| 20 case WM_LBUTTONUP: |
| 21 case WM_NCLBUTTONDBLCLK: |
| 22 case WM_NCLBUTTONDOWN: |
| 23 case WM_NCLBUTTONUP: |
| 24 return MK_LBUTTON; |
| 25 case WM_MBUTTONDBLCLK: |
| 26 case WM_MBUTTONDOWN: |
| 27 case WM_MBUTTONUP: |
| 28 case WM_NCMBUTTONDBLCLK: |
| 29 case WM_NCMBUTTONDOWN: |
| 30 case WM_NCMBUTTONUP: |
| 31 return MK_MBUTTON; |
| 32 case WM_RBUTTONDBLCLK: |
| 33 case WM_RBUTTONDOWN: |
| 34 case WM_RBUTTONUP: |
| 35 case WM_NCRBUTTONDBLCLK: |
| 36 case WM_NCRBUTTONDOWN: |
| 37 case WM_NCRBUTTONUP: |
| 38 return MK_RBUTTON; |
| 39 case WM_NCXBUTTONDBLCLK: |
| 40 case WM_NCXBUTTONDOWN: |
| 41 case WM_NCXBUTTONUP: |
| 42 case WM_XBUTTONDBLCLK: |
| 43 case WM_XBUTTONDOWN: |
| 44 case WM_XBUTTONUP: |
| 45 return MK_XBUTTON1; |
| 46 } |
| 47 return 0; |
| 48 } |
| 49 |
| 50 bool IsButtonDown(const ui::NativeEvent& native_event) { |
| 51 return ((MK_LBUTTON | MK_MBUTTON | MK_RBUTTON | MK_XBUTTON1 | MK_XBUTTON2) & |
| 52 native_event.wParam) != 0; |
| 53 } |
| 54 |
| 55 bool IsClientMouseEvent(const ui::NativeEvent& native_event) { |
| 56 return native_event.message == WM_MOUSELEAVE || |
| 57 native_event.message == WM_MOUSEHOVER || |
| 58 (native_event.message >= WM_MOUSEFIRST && |
| 59 native_event.message <= WM_MOUSELAST); |
| 60 } |
| 61 |
| 62 bool IsNonClientMouseEvent(const ui::NativeEvent& native_event) { |
| 63 return native_event.message == WM_NCMOUSELEAVE || |
| 64 native_event.message == WM_NCMOUSEHOVER || |
| 65 (native_event.message >= WM_NCMOUSEMOVE && |
| 66 native_event.message <= WM_NCXBUTTONDBLCLK); |
| 67 } |
| 68 |
| 69 bool IsDoubleClickMouseEvent(const ui::NativeEvent& native_event) { |
| 70 return native_event.message == WM_NCLBUTTONDBLCLK || |
| 71 native_event.message == WM_NCMBUTTONDBLCLK || |
| 72 native_event.message == WM_NCRBUTTONDBLCLK || |
| 73 native_event.message == WM_NCXBUTTONDBLCLK || |
| 74 native_event.message == WM_LBUTTONDBLCLK || |
| 75 native_event.message == WM_MBUTTONDBLCLK || |
| 76 native_event.message == WM_RBUTTONDBLCLK || |
| 77 native_event.message == WM_XBUTTONDBLCLK; |
| 78 } |
| 79 |
| 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. |
| 82 int KeyStateFlagsFromNative(const ui::NativeEvent& native_event) { |
| 83 int flags = 0; |
| 84 flags |= (GetKeyState(VK_MENU) & 0x80) ? ui::EF_ALT_DOWN : 0; |
| 85 flags |= (GetKeyState(VK_SHIFT) & 0x80) ? ui::EF_SHIFT_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; |
| 88 |
| 89 // Most client mouse messages include key state information. |
| 90 if (IsClientMouseEvent(native_event)) { |
| 91 int win_flags = GET_KEYSTATE_WPARAM(native_event.wParam); |
| 92 flags |= (win_flags & MK_SHIFT) ? ui::EF_SHIFT_DOWN : 0; |
| 93 flags |= (win_flags & MK_CONTROL) ? ui::EF_CONTROL_DOWN : 0; |
| 94 } |
| 95 |
| 96 return flags; |
| 97 } |
| 98 |
| 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. |
| 101 int MouseStateFlagsFromNative(const ui::NativeEvent& native_event) { |
| 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 |
| 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. |
| 106 int win_flags = GetNativeMouseKey(native_event); |
| 107 |
| 108 // Client mouse messages provide key states in their WPARAMs. |
| 109 if (IsClientMouseEvent(native_event)) |
| 110 win_flags |= GET_KEYSTATE_WPARAM(native_event.wParam); |
| 111 |
| 112 int flags = 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; |
| 115 flags |= (win_flags & MK_RBUTTON) ? ui::EF_RIGHT_BUTTON_DOWN : 0; |
| 116 flags |= IsDoubleClickMouseEvent(native_event) ? ui::EF_IS_DOUBLE_CLICK: 0; |
| 117 flags |= IsNonClientMouseEvent(native_event) ? ui::EF_IS_NON_CLIENT : 0; |
| 118 return flags; |
| 119 } |
| 120 |
| 121 } // namespace |
| 122 |
| 123 namespace ui { |
| 124 |
| 125 EventType EventTypeFromNative(const NativeEvent& native_event) { |
| 126 switch (native_event.message) { |
| 127 case WM_KEYDOWN: |
| 128 case WM_SYSKEYDOWN: |
| 129 case WM_CHAR: |
| 130 return ET_KEY_PRESSED; |
| 131 case WM_KEYUP: |
| 132 case WM_SYSKEYUP: |
| 133 return ET_KEY_RELEASED; |
| 134 case WM_LBUTTONDBLCLK: |
| 135 case WM_LBUTTONDOWN: |
| 136 case WM_MBUTTONDBLCLK: |
| 137 case WM_MBUTTONDOWN: |
| 138 case WM_NCLBUTTONDBLCLK: |
| 139 case WM_NCLBUTTONDOWN: |
| 140 case WM_NCMBUTTONDBLCLK: |
| 141 case WM_NCMBUTTONDOWN: |
| 142 case WM_NCRBUTTONDBLCLK: |
| 143 case WM_NCRBUTTONDOWN: |
| 144 case WM_NCXBUTTONDBLCLK: |
| 145 case WM_NCXBUTTONDOWN: |
| 146 case WM_RBUTTONDBLCLK: |
| 147 case WM_RBUTTONDOWN: |
| 148 case WM_XBUTTONDBLCLK: |
| 149 case WM_XBUTTONDOWN: |
| 150 return ET_MOUSE_PRESSED; |
| 151 case WM_LBUTTONUP: |
| 152 case WM_MBUTTONUP: |
| 153 case WM_NCLBUTTONUP: |
| 154 case WM_NCMBUTTONUP: |
| 155 case WM_NCRBUTTONUP: |
| 156 case WM_NCXBUTTONUP: |
| 157 case WM_RBUTTONUP: |
| 158 case WM_XBUTTONUP: |
| 159 return ET_MOUSE_RELEASED; |
| 160 case WM_MOUSEMOVE: |
| 161 return IsButtonDown(native_event) ? ET_MOUSE_DRAGGED : ET_MOUSE_MOVED; |
| 162 case WM_NCMOUSEMOVE: |
| 163 return ET_MOUSE_MOVED; |
| 164 case WM_MOUSEWHEEL: |
| 165 return ET_MOUSEWHEEL; |
| 166 case WM_MOUSELEAVE: |
| 167 case WM_NCMOUSELEAVE: |
| 168 return ET_MOUSE_EXITED; |
| 169 default: |
| 170 NOTREACHED(); |
| 171 } |
| 172 return ET_UNKNOWN; |
| 173 } |
| 174 |
| 175 int EventFlagsFromNative(const NativeEvent& native_event) { |
| 176 int flags = KeyStateFlagsFromNative(native_event); |
| 177 if (IsMouseEvent(native_event)) |
| 178 flags |= MouseStateFlagsFromNative(native_event); |
| 179 |
| 180 return flags; |
| 181 } |
| 182 |
| 183 gfx::Point EventLocationFromNative(const NativeEvent& native_event) { |
| 184 // Client message. The position is contained in the LPARAM. |
| 185 if (IsClientMouseEvent(native_event)) |
| 186 return gfx::Point(native_event.lParam); |
| 187 DCHECK(IsNonClientMouseEvent(native_event)); |
| 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. |
| 190 POINT native_point = { GET_X_LPARAM(native_event.lParam), |
| 191 GET_Y_LPARAM(native_event.lParam) }; |
| 192 ScreenToClient(native_event.hwnd, &native_point); |
| 193 return gfx::Point(native_point); |
| 194 } |
| 195 |
| 196 KeyboardCode KeyboardCodeFromNative(const NativeEvent& native_event) { |
| 197 return KeyboardCodeForWindowsKeyCode(native_event.wParam); |
| 198 } |
| 199 |
| 200 bool IsMouseEvent(const NativeEvent& native_event) { |
| 201 return IsClientMouseEvent(native_event) || |
| 202 IsNonClientMouseEvent(native_event); |
| 203 } |
| 204 |
| 205 int GetMouseWheelOffset(const NativeEvent& native_event) { |
| 206 DCHECK(native_event.message == WM_MOUSEWHEEL); |
| 207 return GET_WHEEL_DELTA_WPARAM(native_event.wParam); |
| 208 } |
| 209 |
| 210 } // namespace ui |
OLD | NEW |