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