| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/browser/renderer_host/input/web_input_event_builders_win.h" | 5 #include "ui/events/blink/web_input_event_builders_win.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | |
| 8 #include "content/browser/renderer_host/input/web_input_event_util.h" | |
| 9 #include "ui/display/win/screen_win.h" | 7 #include "ui/display/win/screen_win.h" |
| 10 #include "ui/events/blink/blink_event_util.h" | 8 #include "ui/events/blink/blink_event_util.h" |
| 11 #include "ui/events/event_utils.h" | 9 #include "ui/events/event_utils.h" |
| 12 | 10 |
| 13 using blink::WebInputEvent; | 11 using blink::WebInputEvent; |
| 14 using blink::WebKeyboardEvent; | 12 using blink::WebKeyboardEvent; |
| 15 using blink::WebMouseEvent; | 13 using blink::WebMouseEvent; |
| 16 using blink::WebMouseWheelEvent; | 14 using blink::WebMouseWheelEvent; |
| 17 | 15 |
| 18 namespace content { | 16 namespace ui { |
| 19 | 17 |
| 20 static const unsigned long kDefaultScrollLinesPerWheelDelta = 3; | 18 static const unsigned long kDefaultScrollLinesPerWheelDelta = 3; |
| 21 static const unsigned long kDefaultScrollCharsPerWheelDelta = 1; | 19 static const unsigned long kDefaultScrollCharsPerWheelDelta = 1; |
| 22 | 20 |
| 23 WebKeyboardEvent WebKeyboardEventBuilder::Build(HWND hwnd, | 21 WebKeyboardEvent WebKeyboardEventBuilder::Build(HWND hwnd, |
| 24 UINT message, | 22 UINT message, |
| 25 WPARAM wparam, | 23 WPARAM wparam, |
| 26 LPARAM lparam, | 24 LPARAM lparam, |
| 27 double time_stamp) { | 25 double time_stamp) { |
| 28 WebKeyboardEvent result; | 26 WebKeyboardEvent result; |
| 29 | 27 |
| 30 result.timeStampSeconds = time_stamp; | 28 result.timeStampSeconds = time_stamp; |
| 31 | 29 |
| 32 result.windowsKeyCode = static_cast<int>(wparam); | 30 result.windowsKeyCode = static_cast<int>(wparam); |
| 33 // Record the scan code (along with other context bits) for this key event. | 31 // Record the scan code (along with other context bits) for this key event. |
| 34 result.nativeKeyCode = static_cast<int>(lparam); | 32 result.nativeKeyCode = static_cast<int>(lparam); |
| 35 | 33 |
| 36 switch (message) { | 34 switch (message) { |
| 37 case WM_SYSKEYDOWN: | 35 case WM_SYSKEYDOWN: |
| 38 result.isSystemKey = true; | 36 result.isSystemKey = true; |
| 39 case WM_KEYDOWN: | 37 // fallthrough |
| 40 result.type = WebInputEvent::RawKeyDown; | 38 case WM_KEYDOWN: |
| 41 break; | 39 result.type = WebInputEvent::RawKeyDown; |
| 42 case WM_SYSKEYUP: | 40 break; |
| 43 result.isSystemKey = true; | 41 case WM_SYSKEYUP: |
| 44 case WM_KEYUP: | 42 result.isSystemKey = true; |
| 45 result.type = WebInputEvent::KeyUp; | 43 // fallthrough |
| 46 break; | 44 case WM_KEYUP: |
| 47 case WM_IME_CHAR: | 45 result.type = WebInputEvent::KeyUp; |
| 48 result.type = WebInputEvent::Char; | 46 break; |
| 49 break; | 47 case WM_IME_CHAR: |
| 50 case WM_SYSCHAR: | 48 result.type = WebInputEvent::Char; |
| 51 result.isSystemKey = true; | 49 break; |
| 52 result.type = WebInputEvent::Char; | 50 case WM_SYSCHAR: |
| 53 case WM_CHAR: | 51 result.isSystemKey = true; |
| 54 result.type = WebInputEvent::Char; | 52 // fallthrough |
| 55 break; | 53 case WM_CHAR: |
| 56 default: | 54 result.type = WebInputEvent::Char; |
| 57 NOTREACHED(); | 55 break; |
| 56 default: |
| 57 NOTREACHED(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 if (result.type == WebInputEvent::Char | 60 if (result.type == WebInputEvent::Char || |
| 61 || result.type == WebInputEvent::RawKeyDown) { | 61 result.type == WebInputEvent::RawKeyDown) { |
| 62 result.text[0] = result.windowsKeyCode; | 62 result.text[0] = result.windowsKeyCode; |
| 63 result.unmodifiedText[0] = result.windowsKeyCode; | 63 result.unmodifiedText[0] = result.windowsKeyCode; |
| 64 } | 64 } |
| 65 result.modifiers = | 65 result.modifiers = |
| 66 ui::EventFlagsToWebEventModifiers(ui::GetModifiersFromKeyState()); | 66 ui::EventFlagsToWebEventModifiers(ui::GetModifiersFromKeyState()); |
| 67 // NOTE: There doesn't seem to be a way to query the mouse button state in | 67 // NOTE: There doesn't seem to be a way to query the mouse button state in |
| 68 // this case. | 68 // this case. |
| 69 | 69 |
| 70 // Bit 30 of lParam represents the "previous key state". If set, the key was | 70 // Bit 30 of lParam represents the "previous key state". If set, the key was |
| 71 // already down, therefore this is an auto-repeat. Only apply this to key | 71 // already down, therefore this is an auto-repeat. Only apply this to key |
| (...skipping 19 matching lines...) Expand all Loading... |
| 91 WebMouseEvent WebMouseEventBuilder::Build( | 91 WebMouseEvent WebMouseEventBuilder::Build( |
| 92 HWND hwnd, | 92 HWND hwnd, |
| 93 UINT message, | 93 UINT message, |
| 94 WPARAM wparam, | 94 WPARAM wparam, |
| 95 LPARAM lparam, | 95 LPARAM lparam, |
| 96 double time_stamp, | 96 double time_stamp, |
| 97 blink::WebPointerProperties::PointerType pointer_type) { | 97 blink::WebPointerProperties::PointerType pointer_type) { |
| 98 WebMouseEvent result; | 98 WebMouseEvent result; |
| 99 | 99 |
| 100 switch (message) { | 100 switch (message) { |
| 101 case WM_MOUSEMOVE: | 101 case WM_MOUSEMOVE: |
| 102 result.type = WebInputEvent::MouseMove; | 102 result.type = WebInputEvent::MouseMove; |
| 103 if (wparam & MK_LBUTTON) | 103 if (wparam & MK_LBUTTON) |
| 104 result.button = WebMouseEvent::ButtonLeft; |
| 105 else if (wparam & MK_MBUTTON) |
| 106 result.button = WebMouseEvent::ButtonMiddle; |
| 107 else if (wparam & MK_RBUTTON) |
| 108 result.button = WebMouseEvent::ButtonRight; |
| 109 else |
| 110 result.button = WebMouseEvent::ButtonNone; |
| 111 break; |
| 112 case WM_MOUSELEAVE: |
| 113 case WM_NCMOUSELEAVE: |
| 114 // TODO(rbyers): This should be MouseLeave but is disabled temporarily. |
| 115 // See http://crbug.com/450631 |
| 116 result.type = WebInputEvent::MouseMove; |
| 117 result.button = WebMouseEvent::ButtonNone; |
| 118 // set the current mouse position (relative to the client area of the |
| 119 // current window) since none is specified for this event |
| 120 lparam = GetRelativeCursorPos(hwnd); |
| 121 break; |
| 122 case WM_LBUTTONDOWN: |
| 123 case WM_LBUTTONDBLCLK: |
| 124 result.type = WebInputEvent::MouseDown; |
| 104 result.button = WebMouseEvent::ButtonLeft; | 125 result.button = WebMouseEvent::ButtonLeft; |
| 105 else if (wparam & MK_MBUTTON) | 126 break; |
| 127 case WM_MBUTTONDOWN: |
| 128 case WM_MBUTTONDBLCLK: |
| 129 result.type = WebInputEvent::MouseDown; |
| 106 result.button = WebMouseEvent::ButtonMiddle; | 130 result.button = WebMouseEvent::ButtonMiddle; |
| 107 else if (wparam & MK_RBUTTON) | 131 break; |
| 132 case WM_RBUTTONDOWN: |
| 133 case WM_RBUTTONDBLCLK: |
| 134 result.type = WebInputEvent::MouseDown; |
| 108 result.button = WebMouseEvent::ButtonRight; | 135 result.button = WebMouseEvent::ButtonRight; |
| 109 else | 136 break; |
| 110 result.button = WebMouseEvent::ButtonNone; | 137 case WM_LBUTTONUP: |
| 111 break; | 138 result.type = WebInputEvent::MouseUp; |
| 112 case WM_MOUSELEAVE: | 139 result.button = WebMouseEvent::ButtonLeft; |
| 113 case WM_NCMOUSELEAVE: | 140 break; |
| 114 // TODO(rbyers): This should be MouseLeave but is disabled temporarily. | 141 case WM_MBUTTONUP: |
| 115 // See http://crbug.com/450631 | 142 result.type = WebInputEvent::MouseUp; |
| 116 result.type = WebInputEvent::MouseMove; | 143 result.button = WebMouseEvent::ButtonMiddle; |
| 117 result.button = WebMouseEvent::ButtonNone; | 144 break; |
| 118 // set the current mouse position (relative to the client area of the | 145 case WM_RBUTTONUP: |
| 119 // current window) since none is specified for this event | 146 result.type = WebInputEvent::MouseUp; |
| 120 lparam = GetRelativeCursorPos(hwnd); | 147 result.button = WebMouseEvent::ButtonRight; |
| 121 break; | 148 break; |
| 122 case WM_LBUTTONDOWN: | 149 default: |
| 123 case WM_LBUTTONDBLCLK: | 150 NOTREACHED(); |
| 124 result.type = WebInputEvent::MouseDown; | |
| 125 result.button = WebMouseEvent::ButtonLeft; | |
| 126 break; | |
| 127 case WM_MBUTTONDOWN: | |
| 128 case WM_MBUTTONDBLCLK: | |
| 129 result.type = WebInputEvent::MouseDown; | |
| 130 result.button = WebMouseEvent::ButtonMiddle; | |
| 131 break; | |
| 132 case WM_RBUTTONDOWN: | |
| 133 case WM_RBUTTONDBLCLK: | |
| 134 result.type = WebInputEvent::MouseDown; | |
| 135 result.button = WebMouseEvent::ButtonRight; | |
| 136 break; | |
| 137 case WM_LBUTTONUP: | |
| 138 result.type = WebInputEvent::MouseUp; | |
| 139 result.button = WebMouseEvent::ButtonLeft; | |
| 140 break; | |
| 141 case WM_MBUTTONUP: | |
| 142 result.type = WebInputEvent::MouseUp; | |
| 143 result.button = WebMouseEvent::ButtonMiddle; | |
| 144 break; | |
| 145 case WM_RBUTTONUP: | |
| 146 result.type = WebInputEvent::MouseUp; | |
| 147 result.button = WebMouseEvent::ButtonRight; | |
| 148 break; | |
| 149 default: | |
| 150 NOTREACHED(); | |
| 151 } | 151 } |
| 152 | 152 |
| 153 result.timeStampSeconds = time_stamp; | 153 result.timeStampSeconds = time_stamp; |
| 154 result.pointerType = pointer_type; | 154 result.pointerType = pointer_type; |
| 155 | 155 |
| 156 // set position fields: | 156 // set position fields: |
| 157 | 157 |
| 158 result.x = static_cast<short>(LOWORD(lparam)); | 158 result.x = static_cast<short>(LOWORD(lparam)); |
| 159 result.y = static_cast<short>(HIWORD(lparam)); | 159 result.y = static_cast<short>(HIWORD(lparam)); |
| 160 result.windowX = result.x; | 160 result.windowX = result.x; |
| 161 result.windowY = result.y; | 161 result.windowY = result.y; |
| 162 | 162 |
| 163 POINT global_point = { result.x, result.y }; | 163 POINT global_point = {result.x, result.y}; |
| 164 ClientToScreen(hwnd, &global_point); | 164 ClientToScreen(hwnd, &global_point); |
| 165 | 165 |
| 166 // We need to convert the global point back to DIP before using it. | 166 // We need to convert the global point back to DIP before using it. |
| 167 gfx::Point dip_global_point = display::win::ScreenWin::ScreenToDIPPoint( | 167 gfx::Point dip_global_point = display::win::ScreenWin::ScreenToDIPPoint( |
| 168 gfx::Point(global_point.x, global_point.y)); | 168 gfx::Point(global_point.x, global_point.y)); |
| 169 | 169 |
| 170 result.globalX = dip_global_point.x(); | 170 result.globalX = dip_global_point.x(); |
| 171 result.globalY = dip_global_point.y(); | 171 result.globalY = dip_global_point.y(); |
| 172 | 172 |
| 173 // calculate number of clicks: | 173 // calculate number of clicks: |
| 174 | 174 |
| 175 // This differs slightly from the WebKit code in WebKit/win/WebView.cpp | 175 // This differs slightly from the WebKit code in WebKit/win/WebView.cpp |
| 176 // where their original code looks buggy. | 176 // where their original code looks buggy. |
| 177 static int last_click_position_x; | 177 static int last_click_position_x; |
| 178 static int last_click_position_y; | 178 static int last_click_position_y; |
| 179 static WebMouseEvent::Button last_click_button = WebMouseEvent::ButtonLeft; | 179 static WebMouseEvent::Button last_click_button = WebMouseEvent::ButtonLeft; |
| 180 | 180 |
| 181 double current_time = result.timeStampSeconds; | 181 double current_time = result.timeStampSeconds; |
| 182 bool cancel_previous_click = | 182 bool cancel_previous_click = |
| 183 (abs(last_click_position_x - result.x) > | 183 (abs(last_click_position_x - result.x) > |
| 184 (::GetSystemMetrics(SM_CXDOUBLECLK) / 2)) | 184 (::GetSystemMetrics(SM_CXDOUBLECLK) / 2)) || |
| 185 || (abs(last_click_position_y - result.y) > | 185 (abs(last_click_position_y - result.y) > |
| 186 (::GetSystemMetrics(SM_CYDOUBLECLK) / 2)) | 186 (::GetSystemMetrics(SM_CYDOUBLECLK) / 2)) || |
| 187 || ((current_time - g_last_click_time) * 1000.0 > ::GetDoubleClickTime()); | 187 ((current_time - g_last_click_time) * 1000.0 > ::GetDoubleClickTime()); |
| 188 | 188 |
| 189 if (result.type == WebInputEvent::MouseDown) { | 189 if (result.type == WebInputEvent::MouseDown) { |
| 190 if (!cancel_previous_click && (result.button == last_click_button)) { | 190 if (!cancel_previous_click && (result.button == last_click_button)) { |
| 191 ++g_last_click_count; | 191 ++g_last_click_count; |
| 192 } else { | 192 } else { |
| 193 g_last_click_count = 1; | 193 g_last_click_count = 1; |
| 194 last_click_position_x = result.x; | 194 last_click_position_x = result.x; |
| 195 last_click_position_y = result.y; | 195 last_click_position_y = result.y; |
| 196 } | 196 } |
| 197 g_last_click_time = current_time; | 197 g_last_click_time = current_time; |
| 198 last_click_button = result.button; | 198 last_click_button = result.button; |
| 199 } else if (result.type == WebInputEvent::MouseMove | 199 } else if (result.type == WebInputEvent::MouseMove || |
| 200 || result.type == WebInputEvent::MouseLeave) { | 200 result.type == WebInputEvent::MouseLeave) { |
| 201 if (cancel_previous_click) { | 201 if (cancel_previous_click) { |
| 202 g_last_click_count = 0; | 202 g_last_click_count = 0; |
| 203 last_click_position_x = 0; | 203 last_click_position_x = 0; |
| 204 last_click_position_y = 0; | 204 last_click_position_y = 0; |
| 205 g_last_click_time = 0; | 205 g_last_click_time = 0; |
| 206 } | 206 } |
| 207 } | 207 } |
| 208 result.clickCount = g_last_click_count; | 208 result.clickCount = g_last_click_count; |
| 209 | 209 |
| 210 // set modifiers: | 210 // set modifiers: |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 key_state |= MK_CONTROL; | 258 key_state |= MK_CONTROL; |
| 259 // NOTE: There doesn't seem to be a way to query the mouse button state | 259 // NOTE: There doesn't seem to be a way to query the mouse button state |
| 260 // in this case. | 260 // in this case. |
| 261 | 261 |
| 262 POINT cursor_position = {0}; | 262 POINT cursor_position = {0}; |
| 263 GetCursorPos(&cursor_position); | 263 GetCursorPos(&cursor_position); |
| 264 result.globalX = cursor_position.x; | 264 result.globalX = cursor_position.x; |
| 265 result.globalY = cursor_position.y; | 265 result.globalY = cursor_position.y; |
| 266 | 266 |
| 267 switch (LOWORD(wparam)) { | 267 switch (LOWORD(wparam)) { |
| 268 case SB_LINEUP: // == SB_LINELEFT | 268 case SB_LINEUP: // == SB_LINELEFT |
| 269 wheel_delta = WHEEL_DELTA; | 269 wheel_delta = WHEEL_DELTA; |
| 270 break; | 270 break; |
| 271 case SB_LINEDOWN: // == SB_LINERIGHT | 271 case SB_LINEDOWN: // == SB_LINERIGHT |
| 272 wheel_delta = -WHEEL_DELTA; | 272 wheel_delta = -WHEEL_DELTA; |
| 273 break; | 273 break; |
| 274 case SB_PAGEUP: | 274 case SB_PAGEUP: |
| 275 wheel_delta = 1; | 275 wheel_delta = 1; |
| 276 result.scrollByPage = true; | 276 result.scrollByPage = true; |
| 277 break; | 277 break; |
| 278 case SB_PAGEDOWN: | 278 case SB_PAGEDOWN: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 if (key_state & MK_CONTROL) | 314 if (key_state & MK_CONTROL) |
| 315 result.modifiers |= WebInputEvent::ControlKey; | 315 result.modifiers |= WebInputEvent::ControlKey; |
| 316 if (key_state & MK_LBUTTON) | 316 if (key_state & MK_LBUTTON) |
| 317 result.modifiers |= WebInputEvent::LeftButtonDown; | 317 result.modifiers |= WebInputEvent::LeftButtonDown; |
| 318 if (key_state & MK_MBUTTON) | 318 if (key_state & MK_MBUTTON) |
| 319 result.modifiers |= WebInputEvent::MiddleButtonDown; | 319 result.modifiers |= WebInputEvent::MiddleButtonDown; |
| 320 if (key_state & MK_RBUTTON) | 320 if (key_state & MK_RBUTTON) |
| 321 result.modifiers |= WebInputEvent::RightButtonDown; | 321 result.modifiers |= WebInputEvent::RightButtonDown; |
| 322 | 322 |
| 323 // Set coordinates by translating event coordinates from screen to client. | 323 // Set coordinates by translating event coordinates from screen to client. |
| 324 POINT client_point = { result.globalX, result.globalY }; | 324 POINT client_point = {result.globalX, result.globalY}; |
| 325 MapWindowPoints(0, hwnd, &client_point, 1); | 325 MapWindowPoints(0, hwnd, &client_point, 1); |
| 326 result.x = client_point.x; | 326 result.x = client_point.x; |
| 327 result.y = client_point.y; | 327 result.y = client_point.y; |
| 328 result.windowX = result.x; | 328 result.windowX = result.x; |
| 329 result.windowY = result.y; | 329 result.windowY = result.y; |
| 330 | 330 |
| 331 // Convert wheel delta amount to a number of pixels to scroll. | 331 // Convert wheel delta amount to a number of pixels to scroll. |
| 332 // | 332 // |
| 333 // How many pixels should we scroll per line? Gecko uses the height of the | 333 // How many pixels should we scroll per line? Gecko uses the height of the |
| 334 // current line, which means scroll distance changes as you go through the | 334 // current line, which means scroll distance changes as you go through the |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 result.deltaX = scroll_delta; | 366 result.deltaX = scroll_delta; |
| 367 result.wheelTicksX = wheel_delta; | 367 result.wheelTicksX = wheel_delta; |
| 368 } else { | 368 } else { |
| 369 result.deltaY = scroll_delta; | 369 result.deltaY = scroll_delta; |
| 370 result.wheelTicksY = wheel_delta; | 370 result.wheelTicksY = wheel_delta; |
| 371 } | 371 } |
| 372 | 372 |
| 373 return result; | 373 return result; |
| 374 } | 374 } |
| 375 | 375 |
| 376 } // namespace content | 376 } // namespace ui |
| OLD | NEW |