diff options
-rw-r--r-- | chrome/browser/renderer_host/render_widget_host.cc | 22 | ||||
-rw-r--r-- | chrome/browser/renderer_host/render_widget_host.h | 39 | ||||
-rw-r--r-- | chrome/browser/renderer_host/render_widget_host_view_gtk.cc | 119 | ||||
-rw-r--r-- | chrome/browser/renderer_host/render_widget_host_view_gtk.h | 23 | ||||
-rw-r--r-- | chrome/common/native_web_keyboard_event.h | 1 | ||||
-rw-r--r-- | chrome/common/native_web_keyboard_event_linux.cc | 7 | ||||
-rw-r--r-- | webkit/api/public/gtk/WebInputEventFactory.h | 7 | ||||
-rw-r--r-- | webkit/api/src/gtk/WebInputEventFactory.cpp | 116 |
8 files changed, 327 insertions, 7 deletions
diff --git a/chrome/browser/renderer_host/render_widget_host.cc b/chrome/browser/renderer_host/render_widget_host.cc index 51d2b3b..fd734bb 100644 --- a/chrome/browser/renderer_host/render_widget_host.cc +++ b/chrome/browser/renderer_host/render_widget_host.cc @@ -424,6 +424,28 @@ void RenderWidgetHost::NotifyTextDirection() { } } +void RenderWidgetHost::ImeSetInputMode(bool activate) { + Send(new ViewMsg_ImeSetInputMode(routing_id(), activate)); +} + +void RenderWidgetHost::ImeSetComposition(const std::wstring& ime_string, + int cursor_position, + int target_start, + int target_end) { + Send(new ViewMsg_ImeSetComposition(routing_id(), 0, cursor_position, + target_start, target_end, ime_string)); +} + +void RenderWidgetHost::ImeConfirmComposition(const std::wstring& ime_string) { + Send(new ViewMsg_ImeSetComposition(routing_id(), 1, -1, -1, -1, ime_string)); +} + +void RenderWidgetHost::ImeCancelComposition() { + std::wstring empty_string; + Send(new ViewMsg_ImeSetComposition(routing_id(), -1, -1, -1, -1, + empty_string)); +} + gfx::Rect RenderWidgetHost::GetRootWindowResizerRect() const { return gfx::Rect(); } diff --git a/chrome/browser/renderer_host/render_widget_host.h b/chrome/browser/renderer_host/render_widget_host.h index c5913ef..aab4645 100644 --- a/chrome/browser/renderer_host/render_widget_host.h +++ b/chrome/browser/renderer_host/render_widget_host.h @@ -279,6 +279,45 @@ class RenderWidgetHost : public IPC::Channel::Listener { void CancelUpdateTextDirection(); void NotifyTextDirection(); + // Notifies the renderer whether or not the IME attached to this process is + // activated. + // When the IME is activated, a renderer process sends IPC messages to notify + // the status of its composition node. (This message is mainly used for + // notifying the position of the input cursor so that the browser can + // display IME windows under the cursor.) + void ImeSetInputMode(bool activate); + + // Update the composition node of the renderer (or WebKit). + // WebKit has a special node (a composition node) for IMEs to change its text + // without affecting any other DOM nodes. When the IME (attached to the + // browser) updates its text, the browser sends IPC messages to update the + // composition node of the renderer. + // (Read the comments of each function for its detail.) + + // Sets the text of the composition node. + // This function can also update the cursor position and mark the specified + // range in the composition node. + // A browser should call this function: + // * when it receives a WM_IME_COMPOSITION message with a GCS_COMPSTR flag + // (on Windows); + // * when it receives a "preedit_changed" signal of GtkIMContext (on Linux); + // * when markedText of NSTextInput is called (on Mac). + void ImeSetComposition(const std::wstring& ime_string, + int cursor_position, + int target_start, + int target_end); + + // Finishes an ongoing composition with the specified text. + // A browser should call this function: + // * when it receives a WM_IME_COMPOSITION message with a GCS_RESULTSTR flag + // (on Windows); + // * when it receives a "commit" signal of GtkIMContext (on Linux); + // * when insertText of NSTextInput is called (on Mac). + void ImeConfirmComposition(const std::wstring& ime_string); + + // Cancels an ongoing composition. + void ImeCancelComposition(); + // This is for derived classes to give us access to the resizer rect. // And to also expose it to the RenderWidgetHostView. virtual gfx::Rect GetRootWindowResizerRect() const; diff --git a/chrome/browser/renderer_host/render_widget_host_view_gtk.cc b/chrome/browser/renderer_host/render_widget_host_view_gtk.cc index 73b60c9..5d42ff8 100644 --- a/chrome/browser/renderer_host/render_widget_host_view_gtk.cc +++ b/chrome/browser/renderer_host/render_widget_host_view_gtk.cc @@ -71,6 +71,17 @@ class RenderWidgetHostViewGtkWidget { g_signal_connect(widget, "scroll-event", G_CALLBACK(MouseScrollEvent), host_view); + // Create a GtkIMContext instance and attach its signal handlers. + host_view->im_context_ = gtk_im_multicontext_new(); + g_signal_connect(host_view->im_context_, "preedit_start", + G_CALLBACK(InputMethodPreeditStart), host_view); + g_signal_connect(host_view->im_context_, "preedit_end", + G_CALLBACK(InputMethodPreeditEnd), host_view); + g_signal_connect(host_view->im_context_, "preedit_changed", + G_CALLBACK(InputMethodPreeditChanged), host_view); + g_signal_connect(host_view->im_context_, "commit", + G_CALLBACK(InputMethodCommit), host_view); + GtkTargetList* target_list = gtk_target_list_new(NULL, 0); gtk_target_list_add_text_targets(target_list, 0); gint num_targets = 0; @@ -109,6 +120,19 @@ class RenderWidgetHostViewGtkWidget { NativeWebKeyboardEvent wke(event); host_view->GetRenderWidgetHost()->ForwardKeyboardEvent(wke); } + + // Dispatch this event to the GtkIMContext object. + // It sends a "commit" signal when it has a character to be inserted + // even when we use a US keyboard so that we can send a Char event + // (or an IME event) to the renderer in our "commit"-signal handler. + // We should send a KeyDown (or a KeyUp) event before dispatching this + // event to the GtkIMContext object (and send a Char event) so that WebKit + // can dispatch the JavaScript events in the following order: onkeydown(), + // onkeypress(), and onkeyup(). (Many JavaScript pages assume this.) + // TODO(hbono): we should not dispatch a key event when the input focus + // is in a password input? + gtk_im_context_filter_keypress(host_view->im_context_, event); + // We return TRUE because we did handle the event. If it turns out webkit // can't handle the event, we'll deal with it in // RenderView::UnhandledKeyboardEvent(). @@ -215,6 +239,68 @@ class RenderWidgetHostViewGtkWidget { return FALSE; } + static void InputMethodCommit(GtkIMContext* im_context, + gchar* text, + RenderWidgetHostViewGtk* host_view) { + std::wstring im_text = UTF8ToWide(text); + if (!host_view->im_is_composing_cjk_text_ && im_text.length() == 1) { + // Send a Char event when we input a composed character without IMEs so + // that this event is to be dispatched to onkeypress() handlers, + // autofill, etc. + ForwardCharEvent(host_view, im_text[0]); + } else { + // Send an IME event. + // Unlike a Char event, an IME event is NOT dispatched to onkeypress() + // handlers or autofill. + host_view->GetRenderWidgetHost()->ImeConfirmComposition(im_text); + } + } + + static void InputMethodPreeditStart(GtkIMContext* im_context, + RenderWidgetHostViewGtk* host_view) { + // Start monitoring IME events of the renderer. + // TODO(hbono): a renderer sends these IME events not only for sending the + // caret position, but also for enabling/disabling IMEs. If we need to + // enable/disable IMEs, we should move this code to a better place. + // (This signal handler is called only when an IME is enabled. So, once + // we disable an IME, we cannot receive any IME events from the renderer, + // i.e. we cannot re-enable the IME any longer.) + host_view->GetRenderWidgetHost()->ImeSetInputMode(true); + host_view->im_is_composing_cjk_text_ = true; + } + + static void InputMethodPreeditEnd(GtkIMContext* im_context, + RenderWidgetHostViewGtk* host_view) { + // End monitoring IME events. + host_view->GetRenderWidgetHost()->ImeSetInputMode(false); + host_view->im_is_composing_cjk_text_ = false; + } + + static void InputMethodPreeditChanged(GtkIMContext* im_context, + RenderWidgetHostViewGtk* host_view) { + // Send an IME event to update the composition node of the renderer. + // TODO(hbono): an IME intercepts all key events while composing a text, + // i.e. we cannot receive any GDK_KEY_PRESS (or GDK_KEY_UP) events. + // Should we send pseudo KeyDown (and KeyUp) events to emulate Windows? + gchar* preedit_text = NULL; + gint cursor_position = 0; + gtk_im_context_get_preedit_string(im_context, &preedit_text, NULL, + &cursor_position); + host_view->GetRenderWidgetHost()->ImeSetComposition( + UTF8ToWide(preedit_text), cursor_position, -1, -1); + g_free(preedit_text); + } + + static void ForwardCharEvent(RenderWidgetHostViewGtk* host_view, + wchar_t im_character) { + if (!im_character) + return; + + NativeWebKeyboardEvent char_event(im_character, + base::Time::Now().ToDoubleT()); + host_view->GetRenderWidgetHost()->ForwardKeyboardEvent(char_event); + } + DISALLOW_IMPLICIT_CONSTRUCTORS(RenderWidgetHostViewGtkWidget); }; @@ -232,11 +318,15 @@ RenderWidgetHostViewGtk::RenderWidgetHostViewGtk(RenderWidgetHost* widget_host) is_showing_context_menu_(false), parent_host_view_(NULL), parent_(NULL), - is_popup_first_mouse_release_(true) { + is_popup_first_mouse_release_(true), + im_context_(NULL), + im_is_composing_cjk_text_(false) { host_->set_view(this); } RenderWidgetHostViewGtk::~RenderWidgetHostViewGtk() { + if (im_context_) + g_object_unref(im_context_); view_.Destroy(); } @@ -396,7 +486,32 @@ void RenderWidgetHostViewGtk::SetIsLoading(bool is_loading) { void RenderWidgetHostViewGtk::IMEUpdateStatus(int control, const gfx::Rect& caret_rect) { - NOTIMPLEMENTED(); + // The renderer has updated its IME status. + // Control the GtkIMContext object according to this status. + if (!im_context_) + return; + + if (control == IME_DISABLE) { + // TODO(hbono): this code just resets the GtkIMContext object and + // detaches it from this window. Should we prevent sending key events to + // the GtkIMContext object (or unref it) when we disable IMEs? + gtk_im_context_reset(im_context_); + gtk_im_context_set_client_window(im_context_, NULL); + gtk_im_context_set_cursor_location(im_context_, NULL); + } else { + // TODO(hbono): we should finish (not reset) an ongoing composition + // when |control| is IME_COMPLETE_COMPOSITION. + + // Attach the GtkIMContext object to this window. + gtk_im_context_set_client_window(im_context_, view_.get()->window); + + // Updates the position of the IME candidate window. + // The position sent from the renderer is a relative one, so we need to + // attach the GtkIMContext object to this window before changing the + // position. + GdkRectangle cursor_rect(caret_rect.ToGdkRectangle()); + gtk_im_context_set_cursor_location(im_context_, &cursor_rect); + } } void RenderWidgetHostViewGtk::DidPaintRect(const gfx::Rect& rect) { diff --git a/chrome/browser/renderer_host/render_widget_host_view_gtk.h b/chrome/browser/renderer_host/render_widget_host_view_gtk.h index af0ac47..e727e08 100644 --- a/chrome/browser/renderer_host/render_widget_host_view_gtk.h +++ b/chrome/browser/renderer_host/render_widget_host_view_gtk.h @@ -18,6 +18,7 @@ class RenderWidgetHost; typedef struct _GtkClipboard GtkClipboard; typedef struct _GtkSelectionData GtkSelectionData; +typedef struct _GtkIMContext GtkIMContext; // ----------------------------------------------------------------------------- // See comments in render_widget_host_view.h about this class and its members. @@ -109,6 +110,28 @@ class RenderWidgetHostViewGtk : public RenderWidgetHostView { // We ignore the first mouse release on popups. This allows the popup to // stay open. bool is_popup_first_mouse_release_; + + // The GtkIMContext object. + // In terms of the DOM event specification Appendix A + // <http://www.w3.org/TR/DOM-Level-3-Events/keyset.html>, + // GTK uses a GtkIMContext object for the following two purposes: + // 1. Composing Latin characters (A.1.2), and; + // 2. Composing CJK characters with an IME (A.1.3). + // Many JavaScript pages assume composed Latin characters are dispatched to + // their onkeypress() handlers but not dispatched CJK characters composed + // with an IME. To emulate this behavior, we should monitor the status of + // this GtkIMContext object and prevent sending Char events when a + // GtkIMContext object sends a "commit" signal with the CJK characters + // composed by an IME. + GtkIMContext* im_context_; + + // Whether or not the above GtkIMContext is composing a CJK text with an IME. + // The GtkIMContext object sends a "preedit_start" before it starts composing + // a CJK text and a "preedit_end" signal after it finishes composing it. + // On the other hand, the GtkIMContext object doesn't send them when + // composing Latin texts. So, we monitor the above signals to check whether + // or not the GtkIMContext object is composing a CJK text. + bool im_is_composing_cjk_text_; }; #endif // CHROME_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_GTK_H_ diff --git a/chrome/common/native_web_keyboard_event.h b/chrome/common/native_web_keyboard_event.h index 8dece32..4c25979 100644 --- a/chrome/common/native_web_keyboard_event.h +++ b/chrome/common/native_web_keyboard_event.h @@ -31,6 +31,7 @@ struct NativeWebKeyboardEvent : public WebKit::WebKeyboardEvent { explicit NativeWebKeyboardEvent(NSEvent *event); #elif defined(OS_LINUX) explicit NativeWebKeyboardEvent(const GdkEventKey* event); + NativeWebKeyboardEvent(wchar_t character, double time_stamp_seconds); #endif NativeWebKeyboardEvent(const NativeWebKeyboardEvent& event); diff --git a/chrome/common/native_web_keyboard_event_linux.cc b/chrome/common/native_web_keyboard_event_linux.cc index ae3b25a..4ad74a3 100644 --- a/chrome/common/native_web_keyboard_event_linux.cc +++ b/chrome/common/native_web_keyboard_event_linux.cc @@ -38,6 +38,13 @@ NativeWebKeyboardEvent::NativeWebKeyboardEvent(const GdkEventKey* native_event) CopyEventTo(native_event, &os_event); } +NativeWebKeyboardEvent::NativeWebKeyboardEvent(wchar_t character, + double time_stamp_seconds) + : WebKeyboardEvent(WebInputEventFactory::keyboardEvent(character, + time_stamp_seconds)), + os_event(NULL) { +} + NativeWebKeyboardEvent::NativeWebKeyboardEvent( const NativeWebKeyboardEvent& other) : WebKeyboardEvent(other) { CopyEventTo(other.os_event, &os_event); diff --git a/webkit/api/public/gtk/WebInputEventFactory.h b/webkit/api/public/gtk/WebInputEventFactory.h index 7caf629..cdc80ea 100644 --- a/webkit/api/public/gtk/WebInputEventFactory.h +++ b/webkit/api/public/gtk/WebInputEventFactory.h @@ -1,10 +1,10 @@ /* * Copyright (C) 2009 Google Inc. All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: - * + * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above @@ -14,7 +14,7 @@ * * Neither the name of Google Inc. nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR @@ -47,6 +47,7 @@ namespace WebKit { class WebInputEventFactory { public: WEBKIT_API static WebKeyboardEvent keyboardEvent(const GdkEventKey*); + WEBKIT_API static WebKeyboardEvent keyboardEvent(wchar_t character, double timeStampSeconds); WEBKIT_API static WebMouseEvent mouseEvent(const GdkEventButton*); WEBKIT_API static WebMouseEvent mouseEvent(const GdkEventMotion*); WEBKIT_API static WebMouseWheelEvent mouseWheelEvent(const GdkEventScroll*); diff --git a/webkit/api/src/gtk/WebInputEventFactory.cpp b/webkit/api/src/gtk/WebInputEventFactory.cpp index 24a4e4b..8086471 100644 --- a/webkit/api/src/gtk/WebInputEventFactory.cpp +++ b/webkit/api/src/gtk/WebInputEventFactory.cpp @@ -71,6 +71,101 @@ static int gdkStateToWebEventModifiers(guint state) return modifiers; } +static int gdkEventToWindowsKeyCode(const GdkEventKey* event) +{ + static const unsigned int hardwareCodeToGDKKeyval[] = { + 0, // 0x00: + 0, // 0x01: + 0, // 0x02: + 0, // 0x03: + 0, // 0x04: + 0, // 0x05: + 0, // 0x06: + 0, // 0x07: + 0, // 0x08: + 0, // 0x09: GDK_Escape + GDK_1, // 0x0A: GDK_1 + GDK_2, // 0x0B: GDK_2 + GDK_3, // 0x0C: GDK_3 + GDK_4, // 0x0D: GDK_4 + GDK_5, // 0x0E: GDK_5 + GDK_6, // 0x0F: GDK_6 + GDK_7, // 0x10: GDK_7 + GDK_8, // 0x11: GDK_8 + GDK_9, // 0x12: GDK_9 + GDK_0, // 0x13: GDK_0 + GDK_minus, // 0x14: GDK_minus + GDK_equal, // 0x15: GDK_equal + 0, // 0x16: GDK_BackSpace + 0, // 0x17: GDK_Tab + GDK_q, // 0x18: GDK_q + GDK_w, // 0x19: GDK_w + GDK_e, // 0x1A: GDK_e + GDK_r, // 0x1B: GDK_r + GDK_t, // 0x1C: GDK_t + GDK_y, // 0x1D: GDK_y + GDK_u, // 0x1E: GDK_u + GDK_i, // 0x1F: GDK_i + GDK_o, // 0x20: GDK_o + GDK_p, // 0x21: GDK_p + GDK_bracketleft, // 0x22: GDK_bracketleft + GDK_bracketright, // 0x23: GDK_bracketright + 0, // 0x24: GDK_Return + 0, // 0x25: GDK_Control_L + GDK_a, // 0x26: GDK_a + GDK_s, // 0x27: GDK_s + GDK_d, // 0x28: GDK_d + GDK_f, // 0x29: GDK_f + GDK_g, // 0x2A: GDK_g + GDK_h, // 0x2B: GDK_h + GDK_j, // 0x2C: GDK_j + GDK_k, // 0x2D: GDK_k + GDK_l, // 0x2E: GDK_l + GDK_semicolon, // 0x2F: GDK_semicolon + GDK_apostrophe, // 0x30: GDK_apostrophe + GDK_grave, // 0x31: GDK_grave + 0, // 0x32: GDK_Shift_L + GDK_backslash, // 0x33: GDK_backslash + GDK_z, // 0x34: GDK_z + GDK_x, // 0x35: GDK_x + GDK_c, // 0x36: GDK_c + GDK_v, // 0x37: GDK_v + GDK_b, // 0x38: GDK_b + GDK_n, // 0x39: GDK_n + GDK_m, // 0x3A: GDK_m + GDK_comma, // 0x3B: GDK_comma + GDK_period, // 0x3C: GDK_period + GDK_slash, // 0x3D: GDK_slash + 0, // 0x3E: GDK_Shift_R + }; + + // |windowKeyCode| shouldn't change even when we change the keyboard + // layout, e.g. when we type an 'A' key of a US keyboard on the French + // layout, |windowsKeyCode| should be VK_A. On the other hand, + // |event->keyval| may change when we change the keyboard layout (the + // GdkKeymap object attached to the GdkDisplay object), e.g. when we type + // an 'A' key of a US keyboard on the French (or Hebrew) layout, + // |event->keyval| becomes GDK_q (or GDK_hebrew_shin). + // To improve compatibilty with Windows, we use |event->hardware_keycode| + // for retrieving its Windows key-code for the keys that can be changed by + // GdkKeymap objects (keyboard-layout drivers). + // We shouldn't use |event->hardware_keycode| for keys that GdkKeymap + // objects cannot change because |event->hardware_keycode| doesn't change + // even when we change the layout options, e.g. when we swap a control + // key and a caps-lock key, GTK doesn't swap their + // |event->hardware_keycode| values but swap their |event->keyval| values. + const int tableSize = sizeof(hardwareCodeToGDKKeyval) / sizeof(hardwareCodeToGDKKeyval[0]); + if (event->hardware_keycode < tableSize) { + int keyval = hardwareCodeToGDKKeyval[event->hardware_keycode]; + if (keyval) + return WebCore::windowsKeyCodeForKeyEvent(keyval); + } + + // This key is one that keyboard-layout drivers cannot change. + // Use |event->keyval| to retrieve its |windowsKeyCode| value. + return WebCore::windowsKeyCodeForKeyEvent(event->keyval); +} + // WebKeyboardEvent ----------------------------------------------------------- WebKeyboardEvent WebInputEventFactory::keyboardEvent(const GdkEventKey* event) @@ -85,7 +180,7 @@ WebKeyboardEvent WebInputEventFactory::keyboardEvent(const GdkEventKey* event) result.type = WebInputEvent::KeyUp; break; case GDK_KEY_PRESS: - result.type = WebInputEvent::KeyDown; + result.type = WebInputEvent::RawKeyDown; break; default: ASSERT_NOT_REACHED(); @@ -94,7 +189,7 @@ WebKeyboardEvent WebInputEventFactory::keyboardEvent(const GdkEventKey* event) // The key code tells us which physical key was pressed (for example, the // A key went down or up). It does not determine whether A should be lower // or upper case. This is what text does, which should be the keyval. - result.windowsKeyCode = WebCore::windowsKeyCodeForKeyEvent(event->keyval); + result.windowsKeyCode = gdkEventToWindowsKeyCode(event); result.nativeKeyCode = event->hardware_keycode; switch (event->keyval) { @@ -119,6 +214,23 @@ WebKeyboardEvent WebInputEventFactory::keyboardEvent(const GdkEventKey* event) return result; } +WebKeyboardEvent WebInputEventFactory::keyboardEvent(wchar_t character, double timeStampSeconds) +{ + // keyboardEvent(const GdkEventKey*) depends on the GdkEventKey object and + // it is hard to use/ it from signal handlers which don't use GdkEventKey + // objects (e.g. GtkIMContext signal handlers.) For such handlers, this + // function creates a WebInputEvent::Char event without using a + // GdkEventKey object. + WebKeyboardEvent result; + result.type = WebKit::WebInputEvent::Char; + result.timeStampSeconds = timeStampSeconds; + result.windowsKeyCode = character; + result.nativeKeyCode = character; + result.text[0] = character; + result.unmodifiedText[0] = character; + return result; +} + // WebMouseEvent -------------------------------------------------------------- WebMouseEvent WebInputEventFactory::mouseEvent(const GdkEventButton* event) |