OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "content/browser/renderer_host/input/web_input_event_builders_gtk.h" |
| 6 |
| 7 #include <gdk/gdk.h> |
| 8 #include <gdk/gdkkeysyms.h> |
| 9 #include <gtk/gtk.h> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "content/browser/renderer_host/input/web_input_event_posix.h" |
| 13 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 14 #include "ui/base/keycodes/keyboard_code_conversion_gtk.h" |
| 15 |
| 16 using WebKit::WebInputEvent; |
| 17 using WebKit::WebMouseEvent; |
| 18 using WebKit::WebMouseWheelEvent; |
| 19 using WebKit::WebKeyboardEvent; |
| 20 |
| 21 namespace { |
| 22 |
| 23 // For click count tracking. |
| 24 static int num_clicks = 0; |
| 25 static GdkWindow* lack_click_event_window = 0; |
| 26 static gint last_click_time = 0; |
| 27 static gint last_click_x = 0; |
| 28 static gint last_click_y = 0; |
| 29 static WebMouseEvent::Button last_click_button = WebMouseEvent::ButtonNone; |
| 30 |
| 31 bool ShouldForgetPreviousClick(GdkWindow* window, gint time, gint x, gint y) { |
| 32 static GtkSettings* settings = gtk_settings_get_default(); |
| 33 |
| 34 if (window != lack_click_event_window) |
| 35 return true; |
| 36 |
| 37 gint double_click_time = 250; |
| 38 gint double_click_distance = 5; |
| 39 g_object_get(G_OBJECT(settings), |
| 40 "gtk-double-click-time", |
| 41 &double_click_time, |
| 42 "gtk-double-click-distance", |
| 43 &double_click_distance, |
| 44 NULL); |
| 45 return (time - last_click_time) > double_click_time || |
| 46 abs(x - last_click_x) > double_click_distance || |
| 47 abs(y - last_click_y) > double_click_distance; |
| 48 } |
| 49 |
| 50 void ResetClickCountState() { |
| 51 num_clicks = 0; |
| 52 lack_click_event_window = 0; |
| 53 last_click_time = 0; |
| 54 last_click_x = 0; |
| 55 last_click_y = 0; |
| 56 last_click_button = WebKit::WebMouseEvent::ButtonNone; |
| 57 } |
| 58 |
| 59 bool IsKeyPadKeyval(guint keyval) { |
| 60 // Keypad keyvals all fall into one range. |
| 61 return keyval >= GDK_KP_Space && keyval <= GDK_KP_9; |
| 62 } |
| 63 |
| 64 double GdkEventTimeToWebEventTime(guint32 time) { |
| 65 // Convert from time in ms to time in sec. |
| 66 return time / 1000.0; |
| 67 } |
| 68 |
| 69 int GdkStateToWebEventModifiers(guint state) { |
| 70 int modifiers = 0; |
| 71 if (state & GDK_SHIFT_MASK) |
| 72 modifiers |= WebInputEvent::ShiftKey; |
| 73 if (state & GDK_CONTROL_MASK) |
| 74 modifiers |= WebInputEvent::ControlKey; |
| 75 if (state & GDK_MOD1_MASK) |
| 76 modifiers |= WebInputEvent::AltKey; |
| 77 if (state & GDK_META_MASK) |
| 78 modifiers |= WebInputEvent::MetaKey; |
| 79 if (state & GDK_BUTTON1_MASK) |
| 80 modifiers |= WebInputEvent::LeftButtonDown; |
| 81 if (state & GDK_BUTTON2_MASK) |
| 82 modifiers |= WebInputEvent::MiddleButtonDown; |
| 83 if (state & GDK_BUTTON3_MASK) |
| 84 modifiers |= WebInputEvent::RightButtonDown; |
| 85 if (state & GDK_LOCK_MASK) |
| 86 modifiers |= WebInputEvent::CapsLockOn; |
| 87 if (state & GDK_MOD2_MASK) |
| 88 modifiers |= WebInputEvent::NumLockOn; |
| 89 return modifiers; |
| 90 } |
| 91 |
| 92 ui::KeyboardCode GdkEventToWindowsKeyCode(const GdkEventKey* event) { |
| 93 return ui::WindowsKeyCodeForGdkKeyCode(event->keyval); |
| 94 /* |
| 95 static const unsigned int hardwareCodeToGDKKeyval[] = { |
| 96 0, // 0x00: |
| 97 0, // 0x01: |
| 98 0, // 0x02: |
| 99 0, // 0x03: |
| 100 0, // 0x04: |
| 101 0, // 0x05: |
| 102 0, // 0x06: |
| 103 0, // 0x07: |
| 104 0, // 0x08: |
| 105 0, // 0x09: GDK_Escape |
| 106 GDK_1, // 0x0A: GDK_1 |
| 107 GDK_2, // 0x0B: GDK_2 |
| 108 GDK_3, // 0x0C: GDK_3 |
| 109 GDK_4, // 0x0D: GDK_4 |
| 110 GDK_5, // 0x0E: GDK_5 |
| 111 GDK_6, // 0x0F: GDK_6 |
| 112 GDK_7, // 0x10: GDK_7 |
| 113 GDK_8, // 0x11: GDK_8 |
| 114 GDK_9, // 0x12: GDK_9 |
| 115 GDK_0, // 0x13: GDK_0 |
| 116 GDK_minus, // 0x14: GDK_minus |
| 117 GDK_equal, // 0x15: GDK_equal |
| 118 0, // 0x16: GDK_BackSpace |
| 119 0, // 0x17: GDK_Tab |
| 120 GDK_q, // 0x18: GDK_q |
| 121 GDK_w, // 0x19: GDK_w |
| 122 GDK_e, // 0x1A: GDK_e |
| 123 GDK_r, // 0x1B: GDK_r |
| 124 GDK_t, // 0x1C: GDK_t |
| 125 GDK_y, // 0x1D: GDK_y |
| 126 GDK_u, // 0x1E: GDK_u |
| 127 GDK_i, // 0x1F: GDK_i |
| 128 GDK_o, // 0x20: GDK_o |
| 129 GDK_p, // 0x21: GDK_p |
| 130 GDK_bracketleft, // 0x22: GDK_bracketleft |
| 131 GDK_bracketright, // 0x23: GDK_bracketright |
| 132 0, // 0x24: GDK_Return |
| 133 0, // 0x25: GDK_Control_L |
| 134 GDK_a, // 0x26: GDK_a |
| 135 GDK_s, // 0x27: GDK_s |
| 136 GDK_d, // 0x28: GDK_d |
| 137 GDK_f, // 0x29: GDK_f |
| 138 GDK_g, // 0x2A: GDK_g |
| 139 GDK_h, // 0x2B: GDK_h |
| 140 GDK_j, // 0x2C: GDK_j |
| 141 GDK_k, // 0x2D: GDK_k |
| 142 GDK_l, // 0x2E: GDK_l |
| 143 GDK_semicolon, // 0x2F: GDK_semicolon |
| 144 GDK_apostrophe, // 0x30: GDK_apostrophe |
| 145 GDK_grave, // 0x31: GDK_grave |
| 146 0, // 0x32: GDK_Shift_L |
| 147 GDK_backslash, // 0x33: GDK_backslash |
| 148 GDK_z, // 0x34: GDK_z |
| 149 GDK_x, // 0x35: GDK_x |
| 150 GDK_c, // 0x36: GDK_c |
| 151 GDK_v, // 0x37: GDK_v |
| 152 GDK_b, // 0x38: GDK_b |
| 153 GDK_n, // 0x39: GDK_n |
| 154 GDK_m, // 0x3A: GDK_m |
| 155 GDK_comma, // 0x3B: GDK_comma |
| 156 GDK_period, // 0x3C: GDK_period |
| 157 GDK_slash, // 0x3D: GDK_slash |
| 158 0, // 0x3E: GDK_Shift_R |
| 159 0, // 0x3F: |
| 160 0, // 0x40: |
| 161 0, // 0x41: |
| 162 0, // 0x42: |
| 163 0, // 0x43: |
| 164 0, // 0x44: |
| 165 0, // 0x45: |
| 166 0, // 0x46: |
| 167 0, // 0x47: |
| 168 0, // 0x48: |
| 169 0, // 0x49: |
| 170 0, // 0x4A: |
| 171 0, // 0x4B: |
| 172 0, // 0x4C: |
| 173 0, // 0x4D: |
| 174 0, // 0x4E: |
| 175 0, // 0x4F: |
| 176 0, // 0x50: |
| 177 0, // 0x51: |
| 178 0, // 0x52: |
| 179 0, // 0x53: |
| 180 0, // 0x54: |
| 181 0, // 0x55: |
| 182 0, // 0x56: |
| 183 0, // 0x57: |
| 184 0, // 0x58: |
| 185 0, // 0x59: |
| 186 0, // 0x5A: |
| 187 0, // 0x5B: |
| 188 0, // 0x5C: |
| 189 0, // 0x5D: |
| 190 0, // 0x5E: |
| 191 0, // 0x5F: |
| 192 0, // 0x60: |
| 193 0, // 0x61: |
| 194 0, // 0x62: |
| 195 0, // 0x63: |
| 196 0, // 0x64: |
| 197 0, // 0x65: |
| 198 0, // 0x66: |
| 199 0, // 0x67: |
| 200 0, // 0x68: |
| 201 0, // 0x69: |
| 202 0, // 0x6A: |
| 203 0, // 0x6B: |
| 204 0, // 0x6C: |
| 205 0, // 0x6D: |
| 206 0, // 0x6E: |
| 207 0, // 0x6F: |
| 208 0, // 0x70: |
| 209 0, // 0x71: |
| 210 0, // 0x72: |
| 211 GDK_Super_L, // 0x73: GDK_Super_L |
| 212 GDK_Super_R, // 0x74: GDK_Super_R |
| 213 }; |
| 214 |
| 215 // |windowsKeyCode| has to include a valid virtual-key code even when we |
| 216 // use non-US layouts, e.g. even when we type an 'A' key of a US keyboard |
| 217 // on the Hebrew layout, |windowsKeyCode| should be VK_A. |
| 218 // On the other hand, |event->keyval| value depends on the current |
| 219 // GdkKeymap object, i.e. when we type an 'A' key of a US keyboard on |
| 220 // the Hebrew layout, |event->keyval| becomes GDK_hebrew_shin and this |
| 221 // WebCore::windowsKeyCodeForKeyEvent() call returns 0. |
| 222 // To improve compatibilty with Windows, we use |event->hardware_keycode| |
| 223 // for retrieving its Windows key-code for the keys when the |
| 224 // WebCore::windowsKeyCodeForEvent() call returns 0. |
| 225 // We shouldn't use |event->hardware_keycode| for keys that GdkKeymap |
| 226 // objects cannot change because |event->hardware_keycode| doesn't change |
| 227 // even when we change the layout options, e.g. when we swap a control |
| 228 // key and a caps-lock key, GTK doesn't swap their |
| 229 // |event->hardware_keycode| values but swap their |event->keyval| values. |
| 230 int windowsKeyCode = WebCore::windowsKeyCodeForKeyEvent(event->keyval); |
| 231 if (windowsKeyCode) |
| 232 return windowsKeyCode; |
| 233 |
| 234 const int tableSize = |
| 235 sizeof(hardwareCodeToGDKKeyval) / sizeof(hardwareCodeToGDKKeyval[0]); |
| 236 if (event->hardware_keycode < tableSize) { |
| 237 int keyval = hardwareCodeToGDKKeyval[event->hardware_keycode]; |
| 238 if (keyval) |
| 239 return WebCore::windowsKeyCodeForKeyEvent(keyval); |
| 240 } |
| 241 |
| 242 // This key is one that keyboard-layout drivers cannot change. |
| 243 // Use |event->keyval| to retrieve its |windowsKeyCode| value. |
| 244 return WebCore::windowsKeyCodeForKeyEvent(event->keyval); |
| 245 */ |
| 246 } |
| 247 |
| 248 // Normalizes event->state to make it Windows/Mac compatible. Since the way |
| 249 // of setting modifier mask on X is very different than Windows/Mac as shown |
| 250 // in http://crbug.com/127142#c8, the normalization is necessary. |
| 251 guint NormalizeEventState(const GdkEventKey* event) { |
| 252 guint mask = 0; |
| 253 switch (GdkEventToWindowsKeyCode(event)) { |
| 254 case ui::VKEY_CONTROL: |
| 255 case ui::VKEY_LCONTROL: |
| 256 case ui::VKEY_RCONTROL: |
| 257 mask = GDK_CONTROL_MASK; |
| 258 break; |
| 259 case ui::VKEY_SHIFT: |
| 260 case ui::VKEY_LSHIFT: |
| 261 case ui::VKEY_RSHIFT: |
| 262 mask = GDK_SHIFT_MASK; |
| 263 break; |
| 264 case ui::VKEY_MENU: |
| 265 case ui::VKEY_LMENU: |
| 266 case ui::VKEY_RMENU: |
| 267 mask = GDK_MOD1_MASK; |
| 268 break; |
| 269 case ui::VKEY_CAPITAL: |
| 270 mask = GDK_LOCK_MASK; |
| 271 break; |
| 272 default: |
| 273 return event->state; |
| 274 } |
| 275 if (event->type == GDK_KEY_PRESS) |
| 276 return event->state | mask; |
| 277 return event->state & ~mask; |
| 278 } |
| 279 |
| 280 // Gets the corresponding control character of a specified key code. See: |
| 281 // http://en.wikipedia.org/wiki/Control_characters |
| 282 // We emulate Windows behavior here. |
| 283 int GetControlCharacter(ui::KeyboardCode windows_key_code, bool shift) { |
| 284 if (windows_key_code >= ui::VKEY_A && windows_key_code <= ui::VKEY_Z) { |
| 285 // ctrl-A ~ ctrl-Z map to \x01 ~ \x1A |
| 286 return windows_key_code - ui::VKEY_A + 1; |
| 287 } |
| 288 if (shift) { |
| 289 // following graphics chars require shift key to input. |
| 290 switch (windows_key_code) { |
| 291 // ctrl-@ maps to \x00 (Null byte) |
| 292 case ui::VKEY_2: |
| 293 return 0; |
| 294 // ctrl-^ maps to \x1E (Record separator, Information separator two) |
| 295 case ui::VKEY_6: |
| 296 return 0x1E; |
| 297 // ctrl-_ maps to \x1F (Unit separator, Information separator one) |
| 298 case ui::VKEY_OEM_MINUS: |
| 299 return 0x1F; |
| 300 // Returns 0 for all other keys to avoid inputting unexpected chars. |
| 301 default: |
| 302 return 0; |
| 303 } |
| 304 } else { |
| 305 switch (windows_key_code) { |
| 306 // ctrl-[ maps to \x1B (Escape) |
| 307 case ui::VKEY_OEM_4: |
| 308 return 0x1B; |
| 309 // ctrl-\ maps to \x1C (File separator, Information separator four) |
| 310 case ui::VKEY_OEM_5: |
| 311 return 0x1C; |
| 312 // ctrl-] maps to \x1D (Group separator, Information separator three) |
| 313 case ui::VKEY_OEM_6: |
| 314 return 0x1D; |
| 315 // ctrl-Enter maps to \x0A (Line feed) |
| 316 case ui::VKEY_RETURN: |
| 317 return 0x0A; |
| 318 // Returns 0 for all other keys to avoid inputting unexpected chars. |
| 319 default: |
| 320 return 0; |
| 321 } |
| 322 } |
| 323 } |
| 324 |
| 325 } // namespace |
| 326 |
| 327 namespace content { |
| 328 |
| 329 // WebKeyboardEvent ----------------------------------------------------------- |
| 330 |
| 331 WebKeyboardEvent WebKeyboardEventBuilder::Build(const GdkEventKey* event) { |
| 332 WebKeyboardEvent result; |
| 333 |
| 334 result.timeStampSeconds = GdkEventTimeToWebEventTime(event->time); |
| 335 result.modifiers = GdkStateToWebEventModifiers(NormalizeEventState(event)); |
| 336 |
| 337 switch (event->type) { |
| 338 case GDK_KEY_RELEASE: |
| 339 result.type = WebInputEvent::KeyUp; |
| 340 break; |
| 341 case GDK_KEY_PRESS: |
| 342 result.type = WebInputEvent::RawKeyDown; |
| 343 break; |
| 344 default: |
| 345 NOTREACHED(); |
| 346 } |
| 347 |
| 348 // According to MSDN: |
| 349 // http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx |
| 350 // Key events with Alt modifier and F10 are system key events. |
| 351 // We just emulate this behavior. It's necessary to prevent webkit from |
| 352 // processing keypress event generated by alt-d, etc. |
| 353 // F10 is not special on Linux, so don't treat it as system key. |
| 354 if (result.modifiers & WebInputEvent::AltKey) |
| 355 result.isSystemKey = true; |
| 356 |
| 357 // The key code tells us which physical key was pressed (for example, the |
| 358 // A key went down or up). It does not determine whether A should be lower |
| 359 // or upper case. This is what text does, which should be the keyval. |
| 360 ui::KeyboardCode windows_key_code = GdkEventToWindowsKeyCode(event); |
| 361 result.windowsKeyCode = GetWindowsKeyCodeWithoutLocation(windows_key_code); |
| 362 result.modifiers |= GetLocationModifiersFromWindowsKeyCode(windows_key_code); |
| 363 result.nativeKeyCode = event->hardware_keycode; |
| 364 |
| 365 if (result.windowsKeyCode == ui::VKEY_RETURN) { |
| 366 // We need to treat the enter key as a key press of character \r. This |
| 367 // is apparently just how webkit handles it and what it expects. |
| 368 result.unmodifiedText[0] = '\r'; |
| 369 } else { |
| 370 // FIXME: fix for non BMP chars |
| 371 result.unmodifiedText[0] = |
| 372 static_cast<int>(gdk_keyval_to_unicode(event->keyval)); |
| 373 } |
| 374 |
| 375 // If ctrl key is pressed down, then control character shall be input. |
| 376 if (result.modifiers & WebInputEvent::ControlKey) { |
| 377 result.text[0] = |
| 378 GetControlCharacter(ui::KeyboardCode(result.windowsKeyCode), |
| 379 result.modifiers & WebInputEvent::ShiftKey); |
| 380 } else { |
| 381 result.text[0] = result.unmodifiedText[0]; |
| 382 } |
| 383 |
| 384 result.setKeyIdentifierFromWindowsKeyCode(); |
| 385 |
| 386 // FIXME: Do we need to set IsAutoRepeat? |
| 387 if (IsKeyPadKeyval(event->keyval)) |
| 388 result.modifiers |= WebInputEvent::IsKeyPad; |
| 389 |
| 390 return result; |
| 391 } |
| 392 |
| 393 WebKeyboardEvent WebKeyboardEventBuilder::Build(wchar_t character, |
| 394 int state, |
| 395 double timeStampSeconds) { |
| 396 // keyboardEvent(const GdkEventKey*) depends on the GdkEventKey object and |
| 397 // it is hard to use/ it from signal handlers which don't use GdkEventKey |
| 398 // objects (e.g. GtkIMContext signal handlers.) For such handlers, this |
| 399 // function creates a WebInputEvent::Char event without using a |
| 400 // GdkEventKey object. |
| 401 WebKeyboardEvent result; |
| 402 result.type = WebKit::WebInputEvent::Char; |
| 403 result.timeStampSeconds = timeStampSeconds; |
| 404 result.modifiers = GdkStateToWebEventModifiers(state); |
| 405 result.windowsKeyCode = character; |
| 406 result.nativeKeyCode = character; |
| 407 result.text[0] = character; |
| 408 result.unmodifiedText[0] = character; |
| 409 |
| 410 // According to MSDN: |
| 411 // http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx |
| 412 // Key events with Alt modifier and F10 are system key events. |
| 413 // We just emulate this behavior. It's necessary to prevent webkit from |
| 414 // processing keypress event generated by alt-d, etc. |
| 415 // F10 is not special on Linux, so don't treat it as system key. |
| 416 if (result.modifiers & WebInputEvent::AltKey) |
| 417 result.isSystemKey = true; |
| 418 |
| 419 return result; |
| 420 } |
| 421 |
| 422 // WebMouseEvent -------------------------------------------------------------- |
| 423 |
| 424 WebMouseEvent WebMouseEventBuilder::Build(const GdkEventButton* event) { |
| 425 WebMouseEvent result; |
| 426 |
| 427 result.timeStampSeconds = GdkEventTimeToWebEventTime(event->time); |
| 428 |
| 429 result.modifiers = GdkStateToWebEventModifiers(event->state); |
| 430 result.x = static_cast<int>(event->x); |
| 431 result.y = static_cast<int>(event->y); |
| 432 result.windowX = result.x; |
| 433 result.windowY = result.y; |
| 434 result.globalX = static_cast<int>(event->x_root); |
| 435 result.globalY = static_cast<int>(event->y_root); |
| 436 result.clickCount = 0; |
| 437 |
| 438 switch (event->type) { |
| 439 case GDK_BUTTON_PRESS: |
| 440 result.type = WebInputEvent::MouseDown; |
| 441 break; |
| 442 case GDK_BUTTON_RELEASE: |
| 443 result.type = WebInputEvent::MouseUp; |
| 444 break; |
| 445 case GDK_3BUTTON_PRESS: |
| 446 case GDK_2BUTTON_PRESS: |
| 447 default: |
| 448 NOTREACHED(); |
| 449 } |
| 450 |
| 451 result.button = WebMouseEvent::ButtonNone; |
| 452 if (event->button == 1) |
| 453 result.button = WebMouseEvent::ButtonLeft; |
| 454 else if (event->button == 2) |
| 455 result.button = WebMouseEvent::ButtonMiddle; |
| 456 else if (event->button == 3) |
| 457 result.button = WebMouseEvent::ButtonRight; |
| 458 |
| 459 if (result.type == WebInputEvent::MouseDown) { |
| 460 bool forgetPreviousClick = ShouldForgetPreviousClick( |
| 461 event->window, event->time, event->x, event->y); |
| 462 |
| 463 if (!forgetPreviousClick && result.button == last_click_button) |
| 464 ++num_clicks; |
| 465 else { |
| 466 num_clicks = 1; |
| 467 |
| 468 lack_click_event_window = event->window; |
| 469 last_click_x = event->x; |
| 470 last_click_y = event->y; |
| 471 last_click_button = result.button; |
| 472 } |
| 473 last_click_time = event->time; |
| 474 } |
| 475 result.clickCount = num_clicks; |
| 476 |
| 477 return result; |
| 478 } |
| 479 |
| 480 WebMouseEvent WebMouseEventBuilder::Build(const GdkEventMotion* event) { |
| 481 WebMouseEvent result; |
| 482 |
| 483 result.timeStampSeconds = GdkEventTimeToWebEventTime(event->time); |
| 484 result.modifiers = GdkStateToWebEventModifiers(event->state); |
| 485 result.x = static_cast<int>(event->x); |
| 486 result.y = static_cast<int>(event->y); |
| 487 result.windowX = result.x; |
| 488 result.windowY = result.y; |
| 489 result.globalX = static_cast<int>(event->x_root); |
| 490 result.globalY = static_cast<int>(event->y_root); |
| 491 |
| 492 switch (event->type) { |
| 493 case GDK_MOTION_NOTIFY: |
| 494 result.type = WebInputEvent::MouseMove; |
| 495 break; |
| 496 default: |
| 497 NOTREACHED(); |
| 498 } |
| 499 |
| 500 result.button = WebMouseEvent::ButtonNone; |
| 501 if (event->state & GDK_BUTTON1_MASK) |
| 502 result.button = WebMouseEvent::ButtonLeft; |
| 503 else if (event->state & GDK_BUTTON2_MASK) |
| 504 result.button = WebMouseEvent::ButtonMiddle; |
| 505 else if (event->state & GDK_BUTTON3_MASK) |
| 506 result.button = WebMouseEvent::ButtonRight; |
| 507 |
| 508 if (ShouldForgetPreviousClick(event->window, event->time, event->x, event->y)) |
| 509 ResetClickCountState(); |
| 510 |
| 511 return result; |
| 512 } |
| 513 |
| 514 WebMouseEvent WebMouseEventBuilder::Build(const GdkEventCrossing* event) { |
| 515 WebMouseEvent result; |
| 516 |
| 517 result.timeStampSeconds = GdkEventTimeToWebEventTime(event->time); |
| 518 result.modifiers = GdkStateToWebEventModifiers(event->state); |
| 519 result.x = static_cast<int>(event->x); |
| 520 result.y = static_cast<int>(event->y); |
| 521 result.windowX = result.x; |
| 522 result.windowY = result.y; |
| 523 result.globalX = static_cast<int>(event->x_root); |
| 524 result.globalY = static_cast<int>(event->y_root); |
| 525 |
| 526 switch (event->type) { |
| 527 case GDK_ENTER_NOTIFY: |
| 528 case GDK_LEAVE_NOTIFY: |
| 529 // Note that if we sent MouseEnter or MouseLeave to WebKit, it |
| 530 // wouldn't work - they don't result in the proper JavaScript events. |
| 531 // MouseMove does the right thing. |
| 532 result.type = WebInputEvent::MouseMove; |
| 533 break; |
| 534 default: |
| 535 NOTREACHED(); |
| 536 } |
| 537 |
| 538 result.button = WebMouseEvent::ButtonNone; |
| 539 if (event->state & GDK_BUTTON1_MASK) |
| 540 result.button = WebMouseEvent::ButtonLeft; |
| 541 else if (event->state & GDK_BUTTON2_MASK) |
| 542 result.button = WebMouseEvent::ButtonMiddle; |
| 543 else if (event->state & GDK_BUTTON3_MASK) |
| 544 result.button = WebMouseEvent::ButtonRight; |
| 545 |
| 546 if (ShouldForgetPreviousClick(event->window, event->time, event->x, event->y)) |
| 547 ResetClickCountState(); |
| 548 |
| 549 return result; |
| 550 } |
| 551 |
| 552 // WebMouseWheelEvent --------------------------------------------------------- |
| 553 |
| 554 WebMouseWheelEvent WebMouseWheelEventBuilder::Build( |
| 555 const GdkEventScroll* event) { |
| 556 WebMouseWheelEvent result; |
| 557 |
| 558 result.type = WebInputEvent::MouseWheel; |
| 559 result.button = WebMouseEvent::ButtonNone; |
| 560 |
| 561 result.timeStampSeconds = GdkEventTimeToWebEventTime(event->time); |
| 562 result.modifiers = GdkStateToWebEventModifiers(event->state); |
| 563 result.x = static_cast<int>(event->x); |
| 564 result.y = static_cast<int>(event->y); |
| 565 result.windowX = result.x; |
| 566 result.windowY = result.y; |
| 567 result.globalX = static_cast<int>(event->x_root); |
| 568 result.globalY = static_cast<int>(event->y_root); |
| 569 |
| 570 // How much should we scroll per mouse wheel event? |
| 571 // - Windows uses 3 lines by default and obeys a system setting. |
| 572 // - Mozilla has a pref that lets you either use the "system" number of lines |
| 573 // to scroll, or lets the user override it. |
| 574 // For the "system" number of lines, it appears they've hardcoded 3. |
| 575 // See case NS_MOUSE_SCROLL in content/events/src/nsEventStateManager.cpp |
| 576 // and InitMouseScrollEvent in widget/src/gtk2/nsCommonWidget.cpp . |
| 577 // - Gtk makes the scroll amount a function of the size of the scroll bar, |
| 578 // which is not available to us here. |
| 579 // Instead, we pick a number that empirically matches Firefox's behavior. |
| 580 static const float scrollbarPixelsPerTick = 160.0f / 3.0f; |
| 581 |
| 582 switch (event->direction) { |
| 583 case GDK_SCROLL_UP: |
| 584 result.deltaY = scrollbarPixelsPerTick; |
| 585 result.wheelTicksY = 1; |
| 586 break; |
| 587 case GDK_SCROLL_DOWN: |
| 588 result.deltaY = -scrollbarPixelsPerTick; |
| 589 result.wheelTicksY = -1; |
| 590 break; |
| 591 case GDK_SCROLL_LEFT: |
| 592 result.deltaX = scrollbarPixelsPerTick; |
| 593 result.wheelTicksX = 1; |
| 594 break; |
| 595 case GDK_SCROLL_RIGHT: |
| 596 result.deltaX = -scrollbarPixelsPerTick; |
| 597 result.wheelTicksX = -1; |
| 598 break; |
| 599 } |
| 600 |
| 601 return result; |
| 602 } |
| 603 |
| 604 } // namespace content |
OLD | NEW |