| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/renderer_host/render_widget_host_view_gtk.h" | 5 #include "chrome/browser/renderer_host/render_widget_host_view_gtk.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 #include <gdk/gdk.h> | 8 #include <gdk/gdk.h> |
| 9 #include <gdk/gdkkeysyms.h> | 9 #include <gdk/gdkkeysyms.h> |
| 10 #include <gdk/gdkx.h> | 10 #include <gdk/gdkx.h> |
| 11 #include <cairo/cairo.h> | 11 #include <cairo/cairo.h> |
| 12 | 12 |
| 13 #include <algorithm> |
| 14 #include <string> |
| 15 |
| 13 #include "base/gfx/gtk_util.h" | 16 #include "base/gfx/gtk_util.h" |
| 14 #include "base/logging.h" | 17 #include "base/logging.h" |
| 15 #include "base/message_loop.h" | 18 #include "base/message_loop.h" |
| 16 #include "base/string_util.h" | 19 #include "base/string_util.h" |
| 17 #include "base/task.h" | 20 #include "base/task.h" |
| 18 #include "base/time.h" | 21 #include "base/time.h" |
| 19 #include "chrome/browser/plugin_process_host.h" | 22 #include "chrome/browser/plugin_process_host.h" |
| 20 #include "chrome/common/native_web_keyboard_event.h" | 23 #include "chrome/common/native_web_keyboard_event.h" |
| 21 #include "chrome/common/render_messages.h" | 24 #include "chrome/common/render_messages.h" |
| 22 #include "chrome/common/x11_util.h" | 25 #include "chrome/common/x11_util.h" |
| 23 #include "chrome/browser/renderer_host/backing_store.h" | 26 #include "chrome/browser/renderer_host/backing_store.h" |
| 27 #include "chrome/browser/renderer_host/gtk_im_context_wrapper.h" |
| 24 #include "chrome/browser/renderer_host/render_widget_host.h" | 28 #include "chrome/browser/renderer_host/render_widget_host.h" |
| 25 #include "webkit/api/public/gtk/WebInputEventFactory.h" | 29 #include "webkit/api/public/gtk/WebInputEventFactory.h" |
| 26 #include "webkit/glue/webcursor_gtk_data.h" | 30 #include "webkit/glue/webcursor_gtk_data.h" |
| 27 | 31 |
| 28 static const int kMaxWindowWidth = 4000; | 32 static const int kMaxWindowWidth = 4000; |
| 29 static const int kMaxWindowHeight = 4000; | 33 static const int kMaxWindowHeight = 4000; |
| 30 | 34 |
| 31 using WebKit::WebInputEventFactory; | 35 using WebKit::WebInputEventFactory; |
| 32 | 36 |
| 33 // This class is a conveience wrapper for GtkIMContext. | |
| 34 class RenderWidgetHostViewGtkIMContext { | |
| 35 public: | |
| 36 explicit RenderWidgetHostViewGtkIMContext(RenderWidgetHostViewGtk* host_view) | |
| 37 : host_view_(host_view), | |
| 38 context_(gtk_im_multicontext_new()), | |
| 39 context_simple_(gtk_im_context_simple_new()), | |
| 40 is_focused_(false), | |
| 41 is_composing_text_(false), | |
| 42 is_enabled_(false), | |
| 43 is_in_key_event_handler_(false), | |
| 44 preedit_cursor_position_(0), | |
| 45 is_preedit_changed_(false) { | |
| 46 DCHECK(context_); | |
| 47 DCHECK(context_simple_); | |
| 48 | |
| 49 // context_ and context_simple_ share the same callback handlers. | |
| 50 // All data come from them are treated equally. | |
| 51 // context_ is for full input method support. | |
| 52 // context_simple_ is for supporting dead/compose keys when input method is | |
| 53 // disabled by webkit, eg. in password input box. | |
| 54 g_signal_connect(context_, "preedit_start", | |
| 55 G_CALLBACK(HandlePreeditStartThunk), this); | |
| 56 g_signal_connect(context_, "preedit_end", | |
| 57 G_CALLBACK(HandlePreeditEndThunk), this); | |
| 58 g_signal_connect(context_, "preedit_changed", | |
| 59 G_CALLBACK(HandlePreeditChangedThunk), this); | |
| 60 g_signal_connect(context_, "commit", | |
| 61 G_CALLBACK(HandleCommitThunk), this); | |
| 62 | |
| 63 g_signal_connect(context_simple_, "preedit_start", | |
| 64 G_CALLBACK(HandlePreeditStartThunk), this); | |
| 65 g_signal_connect(context_simple_, "preedit_end", | |
| 66 G_CALLBACK(HandlePreeditEndThunk), this); | |
| 67 g_signal_connect(context_simple_, "preedit_changed", | |
| 68 G_CALLBACK(HandlePreeditChangedThunk), this); | |
| 69 g_signal_connect(context_simple_, "commit", | |
| 70 G_CALLBACK(HandleCommitThunk), this); | |
| 71 } | |
| 72 | |
| 73 ~RenderWidgetHostViewGtkIMContext() { | |
| 74 if (context_) | |
| 75 g_object_unref(context_); | |
| 76 if (context_simple_) | |
| 77 g_object_unref(context_simple_); | |
| 78 } | |
| 79 | |
| 80 void ProcessKeyEvent(GdkEventKey* event) { | |
| 81 // Indicates preedit-changed and commit signal handlers that we are | |
| 82 // processing a key event. | |
| 83 is_in_key_event_handler_ = true; | |
| 84 // Reset this flag so that we can know if preedit is changed after | |
| 85 // processing this key event. | |
| 86 is_preedit_changed_ = false; | |
| 87 // Clear it so that we can know if something needs committing after | |
| 88 // processing this key event. | |
| 89 commit_text_.clear(); | |
| 90 | |
| 91 // According to Document Object Model (DOM) Level 3 Events Specification | |
| 92 // Appendix A: Keyboard events and key identifiers | |
| 93 // http://www.w3.org/TR/DOM-Level-3-Events/keyset.html: | |
| 94 // The event sequence would be: | |
| 95 // 1. keydown | |
| 96 // 2. textInput | |
| 97 // 3. keyup | |
| 98 // | |
| 99 // So keydown must be sent to webkit before sending input method result, | |
| 100 // while keyup must be sent afterwards. | |
| 101 // However on Windows, if a keydown event has been processed by IME, its | |
| 102 // virtual keycode will be changed to VK_PROCESSKEY(0xE5) before being sent | |
| 103 // to application. | |
| 104 // To emulate the windows behavior as much as possible, we need to send the | |
| 105 // key event to the GtkIMContext object first, and decide whether or not to | |
| 106 // send the original key event to webkit according to the result from IME. | |
| 107 // | |
| 108 // If IME is enabled by WebKit, this event will be dispatched to context_ | |
| 109 // to get full IME support. Otherwise it'll be dispatched to | |
| 110 // context_simple_, so that dead/compose keys can still work. | |
| 111 // | |
| 112 // It sends a "commit" signal when it has a character to be inserted | |
| 113 // even when we use a US keyboard so that we can send a Char event | |
| 114 // (or an IME event) to the renderer in our "commit"-signal handler. | |
| 115 // We should send a KeyDown (or a KeyUp) event before dispatching this | |
| 116 // event to the GtkIMContext object (and send a Char event) so that WebKit | |
| 117 // can dispatch the JavaScript events in the following order: onkeydown(), | |
| 118 // onkeypress(), and onkeyup(). (Many JavaScript pages assume this.) | |
| 119 gboolean filtered = false; | |
| 120 if (is_enabled_) { | |
| 121 filtered = gtk_im_context_filter_keypress(context_, event); | |
| 122 } else { | |
| 123 filtered = gtk_im_context_filter_keypress(context_simple_, event); | |
| 124 } | |
| 125 | |
| 126 NativeWebKeyboardEvent wke(event); | |
| 127 | |
| 128 // Send filtered keydown event before sending IME result. | |
| 129 if (event->type == GDK_KEY_PRESS && filtered) | |
| 130 ProcessFilteredKeyPressEvent(&wke); | |
| 131 | |
| 132 // Send IME results. In most cases, it's only available if the key event | |
| 133 // is filtered by IME. But in rare cases, an unfiltered key event may also | |
| 134 // generate IME results. | |
| 135 // Any IME results generated by a unfiltered key down event must be sent | |
| 136 // before the key down event, to avoid some tricky issues. For example, | |
| 137 // when using latin-post input method, pressing 'a' then Backspace, may | |
| 138 // generate following events in sequence: | |
| 139 // 1. keydown 'a' (filtered) | |
| 140 // 2. preedit changed to "a" | |
| 141 // 3. keyup 'a' (unfiltered) | |
| 142 // 4. keydown Backspace (unfiltered) | |
| 143 // 5. commit "a" | |
| 144 // 6. preedit end | |
| 145 // 7. keyup Backspace (unfiltered) | |
| 146 // | |
| 147 // In this case, the input box will be in a strange state if keydown | |
| 148 // Backspace is sent to webkit before commit "a" and preedit end. | |
| 149 ProcessInputMethodResult(event, filtered); | |
| 150 | |
| 151 // Send unfiltered keydown and keyup events after sending IME result. | |
| 152 if (event->type == GDK_KEY_PRESS && !filtered) | |
| 153 ProcessUnfilteredKeyPressEvent(&wke); | |
| 154 else if (event->type == GDK_KEY_RELEASE) | |
| 155 host_view_->GetRenderWidgetHost()->ForwardKeyboardEvent(wke); | |
| 156 | |
| 157 // End of key event processing. | |
| 158 is_in_key_event_handler_ = false; | |
| 159 } | |
| 160 | |
| 161 void UpdateStatus(int control, const gfx::Rect& caret_rect) { | |
| 162 // The renderer has updated its IME status. | |
| 163 // Control the GtkIMContext object according to this status. | |
| 164 if (!context_ || !is_focused_) | |
| 165 return; | |
| 166 | |
| 167 DCHECK(!is_in_key_event_handler_); | |
| 168 | |
| 169 // TODO(james.su@gmail.com): Following code causes a side effect: | |
| 170 // When trying to move cursor from one text input box to another while | |
| 171 // composition text is still not confirmed, following CompleteComposition() | |
| 172 // calls will prevent the cursor from moving outside the first input box. | |
| 173 if (control == IME_DISABLE) { | |
| 174 if (is_enabled_) { | |
| 175 CompleteComposition(); | |
| 176 gtk_im_context_reset(context_simple_); | |
| 177 gtk_im_context_focus_out(context_); | |
| 178 is_enabled_ = false; | |
| 179 } | |
| 180 } else { | |
| 181 // Enable the GtkIMContext object if it's not enabled yet. | |
| 182 if (!is_enabled_) { | |
| 183 // Reset context_simple_ to its initial state, in case it's currently | |
| 184 // in middle of a composition session inside a password box. | |
| 185 gtk_im_context_reset(context_simple_); | |
| 186 gtk_im_context_focus_in(context_); | |
| 187 // It might be true when switching from a password box in middle of a | |
| 188 // composition session. | |
| 189 is_composing_text_ = false; | |
| 190 is_enabled_ = true; | |
| 191 } else if (control == IME_COMPLETE_COMPOSITION) { | |
| 192 CompleteComposition(); | |
| 193 } | |
| 194 | |
| 195 // Updates the position of the IME candidate window. | |
| 196 // The position sent from the renderer is a relative one, so we need to | |
| 197 // attach the GtkIMContext object to this window before changing the | |
| 198 // position. | |
| 199 GdkRectangle cursor_rect(caret_rect.ToGdkRectangle()); | |
| 200 gtk_im_context_set_cursor_location(context_, &cursor_rect); | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 void OnFocusIn() { | |
| 205 if (is_focused_) | |
| 206 return; | |
| 207 | |
| 208 // Tracks the focused state so that we can give focus to the | |
| 209 // GtkIMContext object correctly later when IME is enabled by WebKit. | |
| 210 is_focused_ = true; | |
| 211 | |
| 212 // We should call gtk_im_context_set_client_window() only when this window | |
| 213 // gain (or release) the window focus because an immodule may reset its | |
| 214 // internal status when processing this function. | |
| 215 gtk_im_context_set_client_window(context_, | |
| 216 host_view_->native_view()->window); | |
| 217 | |
| 218 // Notify the GtkIMContext object of this focus-in event only if IME is | |
| 219 // enabled by WebKit. | |
| 220 if (is_enabled_) | |
| 221 gtk_im_context_focus_in(context_); | |
| 222 | |
| 223 // Actually current GtkIMContextSimple implementation doesn't care about | |
| 224 // client window. This line is just for safe. | |
| 225 gtk_im_context_set_client_window(context_simple_, | |
| 226 host_view_->native_view()->window); | |
| 227 | |
| 228 // context_simple_ is always enabled. | |
| 229 // Actually it doesn't care focus state at all. | |
| 230 gtk_im_context_focus_in(context_simple_); | |
| 231 | |
| 232 // Enables RenderWidget's IME related events, so that we can be notified | |
| 233 // when WebKit wants to enable or disable IME. | |
| 234 host_view_->GetRenderWidgetHost()->ImeSetInputMode(true); | |
| 235 } | |
| 236 | |
| 237 void OnFocusOut() { | |
| 238 if (!is_focused_) | |
| 239 return; | |
| 240 | |
| 241 // Tracks the focused state so that we won't give focus to the | |
| 242 // GtkIMContext object unexpectly. | |
| 243 is_focused_ = false; | |
| 244 | |
| 245 // Notify the GtkIMContext object of this focus-out event only if IME is | |
| 246 // enabled by WebKit. | |
| 247 if (is_enabled_) { | |
| 248 // To reset the GtkIMContext object and prevent data loss. | |
| 249 CompleteComposition(); | |
| 250 gtk_im_context_focus_out(context_); | |
| 251 } | |
| 252 | |
| 253 // Detach this GtkIMContext object from this window. | |
| 254 gtk_im_context_set_client_window(context_, NULL); | |
| 255 | |
| 256 // To make sure it'll be in correct state when focused in again. | |
| 257 gtk_im_context_reset(context_simple_); | |
| 258 gtk_im_context_focus_out(context_simple_); | |
| 259 gtk_im_context_set_client_window(context_simple_, NULL); | |
| 260 | |
| 261 // Reset stored IME status. | |
| 262 is_composing_text_ = false; | |
| 263 preedit_text_.clear(); | |
| 264 preedit_cursor_position_ = 0; | |
| 265 | |
| 266 // Disable RenderWidget's IME related events to save bandwidth. | |
| 267 host_view_->GetRenderWidgetHost()->ImeSetInputMode(false); | |
| 268 } | |
| 269 | |
| 270 private: | |
| 271 // Check if a text needs commit by forwarding a char event instead of | |
| 272 // by confirming as a composition text. | |
| 273 bool NeedCommitByForwardingCharEvent() { | |
| 274 // If there is no composition text and has only one character to be | |
| 275 // committed, then the character will be send to webkit as a Char event | |
| 276 // instead of a confirmed composition text. | |
| 277 // It should be fine to handle BMP character only, as non-BMP characters | |
| 278 // can always be committed as confirmed composition text. | |
| 279 return !is_composing_text_ && commit_text_.length() == 1; | |
| 280 } | |
| 281 | |
| 282 void ProcessFilteredKeyPressEvent(NativeWebKeyboardEvent* wke) { | |
| 283 // Copied from third_party/WebKit/WebCore/page/EventHandler.cpp | |
| 284 // | |
| 285 // Match key code of composition keydown event on windows. | |
| 286 // IE sends VK_PROCESSKEY which has value 229; | |
| 287 // | |
| 288 // Please refer to following documents for detals: | |
| 289 // - Virtual-Key Codes | |
| 290 // http://msdn.microsoft.com/en-us/library/ms645540(VS.85).aspx | |
| 291 // - How the IME System Works | |
| 292 // http://msdn.microsoft.com/en-us/library/cc194848.aspx | |
| 293 // - ImmGetVirtualKey Function | |
| 294 // http://msdn.microsoft.com/en-us/library/dd318570(VS.85).aspx | |
| 295 const int kCompositionEventKeyCode = 229; | |
| 296 | |
| 297 // If IME has filtered this event, then replace virtual key code with | |
| 298 // VK_PROCESSKEY. See comment in ProcessKeyEvent() for details. | |
| 299 // It's only required for keydown events. | |
| 300 // To emulate windows behavior, when input method is enabled, if the commit | |
| 301 // text can be emulated by a Char event, then don't do this replacement. | |
| 302 if (!NeedCommitByForwardingCharEvent()) { | |
| 303 wke->windowsKeyCode = kCompositionEventKeyCode; | |
| 304 // Prevent RenderView::UnhandledKeyboardEvent() from processing it. | |
| 305 // Otherwise unexpected result may occur. For example if it's a | |
| 306 // Backspace key event, the browser may go back to previous page. | |
| 307 if (wke->os_event) { | |
| 308 wke->os_event->keyval = GDK_VoidSymbol; | |
| 309 wke->os_event->state = 0; | |
| 310 } | |
| 311 } | |
| 312 host_view_->GetRenderWidgetHost()->ForwardKeyboardEvent(*wke); | |
| 313 } | |
| 314 | |
| 315 void ProcessUnfilteredKeyPressEvent(NativeWebKeyboardEvent* wke) { | |
| 316 RenderWidgetHost* host = host_view_->GetRenderWidgetHost(); | |
| 317 | |
| 318 // Send keydown event as it, because it's not filtered by IME. | |
| 319 host->ForwardKeyboardEvent(*wke); | |
| 320 | |
| 321 // IME is disabled by WebKit or the GtkIMContext object cannot handle | |
| 322 // this key event. | |
| 323 // This case is caused by two reasons: | |
| 324 // 1. The given key event is a control-key event, (e.g. return, page up, | |
| 325 // page down, tab, arrows, etc.) or; | |
| 326 // 2. The given key event is not a control-key event but printable | |
| 327 // characters aren't assigned to the event, (e.g. alt+d, etc.) | |
| 328 // Create a Char event manually from this key event and send it to the | |
| 329 // renderer when this Char event contains a printable character which | |
| 330 // should be processed by WebKit. | |
| 331 // isSystemKey will be set to true if this key event has Alt modifier, | |
| 332 // see WebInputEventFactory::keyboardEvent() for details. | |
| 333 if (wke->text[0]) { | |
| 334 wke->type = WebKit::WebInputEvent::Char; | |
| 335 host->ForwardKeyboardEvent(*wke); | |
| 336 } | |
| 337 } | |
| 338 | |
| 339 // Processes result returned from input method after filtering a key event. | |
| 340 // |filtered| indicates if the key event was filtered by the input method. | |
| 341 void ProcessInputMethodResult(const GdkEventKey* event, bool filtered) { | |
| 342 RenderWidgetHost* host = host_view_->GetRenderWidgetHost(); | |
| 343 bool committed = false; | |
| 344 // We do commit before preedit change, so that we can optimize some | |
| 345 // unnecessary preedit changes. | |
| 346 if (commit_text_.length()) { | |
| 347 if (filtered && NeedCommitByForwardingCharEvent()) { | |
| 348 // Send a Char event when we input a composed character without IMEs | |
| 349 // so that this event is to be dispatched to onkeypress() handlers, | |
| 350 // autofill, etc. | |
| 351 // Only commit text generated by a filtered key down event can be sent | |
| 352 // as a Char event, because a unfiltered key down event will probably | |
| 353 // generate another Char event. | |
| 354 // TODO(james.su@gmail.com): Is it necessary to support non BMP chars | |
| 355 // here? | |
| 356 NativeWebKeyboardEvent char_event(commit_text_[0], | |
| 357 event->state, | |
| 358 base::Time::Now().ToDoubleT()); | |
| 359 host->ForwardKeyboardEvent(char_event); | |
| 360 } else { | |
| 361 committed = true; | |
| 362 // Send an IME event. | |
| 363 // Unlike a Char event, an IME event is NOT dispatched to onkeypress() | |
| 364 // handlers or autofill. | |
| 365 host->ImeConfirmComposition(commit_text_); | |
| 366 // Set this flag to false, as this composition session has been | |
| 367 // finished. | |
| 368 is_composing_text_ = false; | |
| 369 } | |
| 370 } | |
| 371 | |
| 372 // Send preedit text only if it's changed. | |
| 373 // If a text has been committed, then we don't need to send the empty | |
| 374 // preedit text again. | |
| 375 if (is_preedit_changed_) { | |
| 376 if (preedit_text_.length()) { | |
| 377 host->ImeSetComposition(preedit_text_, preedit_cursor_position_, | |
| 378 -1, -1); | |
| 379 } else if (!committed) { | |
| 380 host->ImeCancelComposition(); | |
| 381 } | |
| 382 } | |
| 383 } | |
| 384 | |
| 385 void CompleteComposition() { | |
| 386 if (!is_enabled_) | |
| 387 return; | |
| 388 | |
| 389 // If WebKit requires to complete current composition, then we need commit | |
| 390 // existing preedit text and reset the GtkIMContext object. | |
| 391 | |
| 392 // Backup existing preedit text to avoid it's being cleared when resetting | |
| 393 // the GtkIMContext object. | |
| 394 string16 old_preedit_text = preedit_text_; | |
| 395 | |
| 396 // Clear it so that we can know if anything is committed by following | |
| 397 // line. | |
| 398 commit_text_.clear(); | |
| 399 | |
| 400 // Resetting the GtkIMContext. Input method may commit something at this | |
| 401 // point. In this case, we shall not commit the preedit text again. | |
| 402 gtk_im_context_reset(context_); | |
| 403 | |
| 404 // If nothing was committed by above line, then commit stored preedit text | |
| 405 // to prevent data loss. | |
| 406 if (old_preedit_text.length() && commit_text_.length() == 0) { | |
| 407 host_view_->GetRenderWidgetHost()->ImeConfirmComposition( | |
| 408 old_preedit_text); | |
| 409 } | |
| 410 | |
| 411 is_composing_text_ = false; | |
| 412 preedit_text_.clear(); | |
| 413 preedit_cursor_position_ = 0; | |
| 414 } | |
| 415 | |
| 416 // Real code of "commit" signal handler. | |
| 417 void HandleCommit(const string16& text) { | |
| 418 // Append the text to the buffer, because commit signal might be fired | |
| 419 // multiple times when processing a key event. | |
| 420 commit_text_.append(text); | |
| 421 // Nothing needs to do, if it's currently in ProcessKeyEvent() | |
| 422 // handler, which will send commit text to webkit later. Otherwise, | |
| 423 // we need send it here. | |
| 424 // It's possible that commit signal is fired without a key event, for | |
| 425 // example when user input via a voice or handwriting recognition software. | |
| 426 // In this case, the text must be committed directly. | |
| 427 if (!is_in_key_event_handler_) { | |
| 428 host_view_->GetRenderWidgetHost()->ImeConfirmComposition(text); | |
| 429 } | |
| 430 } | |
| 431 | |
| 432 // Real code of "preedit-start" signal handler. | |
| 433 void HandlePreeditStart() { | |
| 434 is_composing_text_ = true; | |
| 435 } | |
| 436 | |
| 437 // Real code of "preedit-changed" signal handler. | |
| 438 void HandlePreeditChanged(const string16& text, int cursor_position) { | |
| 439 bool changed = false; | |
| 440 // If preedit text or cursor position is not changed since last time, | |
| 441 // then it's not necessary to update it again. | |
| 442 // Preedit text is always stored, so that we can commit it when webkit | |
| 443 // requires. | |
| 444 // Don't set is_preedit_changed_ to false if there is no change, because | |
| 445 // this handler might be called multiple times with the same data. | |
| 446 if (cursor_position != preedit_cursor_position_ || text != preedit_text_) { | |
| 447 preedit_text_ = text; | |
| 448 preedit_cursor_position_ = cursor_position; | |
| 449 is_preedit_changed_ = true; | |
| 450 changed = true; | |
| 451 } | |
| 452 | |
| 453 // In case we are using a buggy input method which doesn't fire | |
| 454 // "preedit_start" signal. | |
| 455 if (text.length()) | |
| 456 is_composing_text_ = true; | |
| 457 | |
| 458 // Nothing needs to do, if it's currently in ProcessKeyEvent() | |
| 459 // handler, which will send preedit text to webkit later. | |
| 460 // Otherwise, we need send it here if it's been changed. | |
| 461 if (!is_in_key_event_handler_ && changed) { | |
| 462 if (text.length()) { | |
| 463 host_view_->GetRenderWidgetHost()->ImeSetComposition( | |
| 464 text, cursor_position, -1, -1); | |
| 465 } else { | |
| 466 host_view_->GetRenderWidgetHost()->ImeCancelComposition(); | |
| 467 } | |
| 468 } | |
| 469 } | |
| 470 | |
| 471 // Real code of "preedit-end" signal handler. | |
| 472 void HandlePreeditEnd() { | |
| 473 bool changed = false; | |
| 474 if (preedit_text_.length()) { | |
| 475 // The composition session has been finished. | |
| 476 preedit_text_.clear(); | |
| 477 preedit_cursor_position_ = 0; | |
| 478 is_preedit_changed_ = true; | |
| 479 changed = true; | |
| 480 } | |
| 481 | |
| 482 // If there is still a preedit text when firing "preedit-end" signal, | |
| 483 // we need inform webkit to clear it. | |
| 484 // It's only necessary when it's not in ProcessKeyEvent (). | |
| 485 if (!is_in_key_event_handler_ && changed) { | |
| 486 host_view_->GetRenderWidgetHost()->ImeCancelComposition(); | |
| 487 } | |
| 488 | |
| 489 // Don't set is_composing_text_ to false here, because "preedit_end" | |
| 490 // signal may be fired before "commit" signal. | |
| 491 } | |
| 492 | |
| 493 private: | |
| 494 // Signal handlers of GtkIMContext object. | |
| 495 static void HandleCommitThunk(GtkIMContext* context, gchar* text, | |
| 496 RenderWidgetHostViewGtkIMContext* self) { | |
| 497 self->HandleCommit(UTF8ToUTF16(text)); | |
| 498 } | |
| 499 | |
| 500 static void HandlePreeditStartThunk(GtkIMContext* context, | |
| 501 RenderWidgetHostViewGtkIMContext* self) { | |
| 502 self->HandlePreeditStart(); | |
| 503 } | |
| 504 | |
| 505 static void HandlePreeditChangedThunk( | |
| 506 GtkIMContext* context, RenderWidgetHostViewGtkIMContext* self) { | |
| 507 gchar* text = NULL; | |
| 508 gint cursor_position = 0; | |
| 509 gtk_im_context_get_preedit_string(context, &text, NULL, &cursor_position); | |
| 510 self->HandlePreeditChanged(UTF8ToUTF16(text), cursor_position); | |
| 511 g_free(text); | |
| 512 } | |
| 513 | |
| 514 static void HandlePreeditEndThunk(GtkIMContext* context, | |
| 515 RenderWidgetHostViewGtkIMContext* self) { | |
| 516 self->HandlePreeditEnd(); | |
| 517 } | |
| 518 | |
| 519 private: | |
| 520 // The parent object. | |
| 521 RenderWidgetHostViewGtk* host_view_; | |
| 522 | |
| 523 // The GtkIMContext object. | |
| 524 // In terms of the DOM event specification Appendix A | |
| 525 // <http://www.w3.org/TR/DOM-Level-3-Events/keyset.html>, | |
| 526 // GTK uses a GtkIMContext object for the following two purposes: | |
| 527 // 1. Composing Latin characters (A.1.2), and; | |
| 528 // 2. Composing CJK characters with an IME (A.1.3). | |
| 529 // Many JavaScript pages assume composed Latin characters are dispatched to | |
| 530 // their onkeypress() handlers but not dispatched CJK characters composed | |
| 531 // with an IME. To emulate this behavior, we should monitor the status of | |
| 532 // this GtkIMContext object and prevent sending Char events when a | |
| 533 // GtkIMContext object sends a "commit" signal with the CJK characters | |
| 534 // composed by an IME. | |
| 535 GtkIMContext* context_; | |
| 536 | |
| 537 // A GtkIMContextSimple object, for supporting dead/compose keys when input | |
| 538 // method is disabled, eg. in password input box. | |
| 539 GtkIMContext* context_simple_; | |
| 540 | |
| 541 // Whether or not this widget is focused. | |
| 542 bool is_focused_; | |
| 543 | |
| 544 // Whether or not the above GtkIMContext is composing a text with an IME. | |
| 545 // This flag is used in "commit" signal handler of the GtkIMContext object, | |
| 546 // which determines how to submit the result text to WebKit according to this | |
| 547 // flag. | |
| 548 // If this flag is true or there are more than one characters in the result, | |
| 549 // then the result text will be committed to WebKit as a confirmed | |
| 550 // composition. Otherwise, it'll be forwarded as a key event. | |
| 551 // | |
| 552 // The GtkIMContext object sends a "preedit_start" before it starts composing | |
| 553 // a text and a "preedit_end" signal after it finishes composing it. | |
| 554 // "preedit_start" signal is monitored to turn it on. | |
| 555 // We don't monitor "preedit_end" signal to turn it off, because an input | |
| 556 // method may fire "preedit_end" signal before "commit" signal. | |
| 557 // A buggy input method may not fire "preedit_start" and/or "preedit_end" | |
| 558 // at all, so this flag will also be set to true when "preedit_changed" signal | |
| 559 // is fired with non-empty preedit text. | |
| 560 bool is_composing_text_; | |
| 561 | |
| 562 // Whether or not the IME is enabled. | |
| 563 // This flag is actually controlled by RenderWidget. | |
| 564 // It shall be set to false when an ImeUpdateStatus message with control == | |
| 565 // IME_DISABLE is received, and shall be set to true if control == | |
| 566 // IME_COMPLETE_COMPOSITION or IME_MOVE_WINDOWS. | |
| 567 // When this flag is false, keyboard events shall be dispatched directly | |
| 568 // instead of sending to context_. | |
| 569 bool is_enabled_; | |
| 570 | |
| 571 // Whether or not it's currently running inside key event handler. | |
| 572 // If it's true, then preedit-changed and commit handler will backup the | |
| 573 // preedit or commit text instead of sending them down to webkit. | |
| 574 // key event handler will send them later. | |
| 575 bool is_in_key_event_handler_; | |
| 576 | |
| 577 // Stores a copy of the most recent preedit text retrieved from context_. | |
| 578 // When an ImeUpdateStatus message with control == IME_COMPLETE_COMPOSITION | |
| 579 // is received, this stored preedit text (if not empty) shall be committed, | |
| 580 // and context_ shall be reset. | |
| 581 string16 preedit_text_; | |
| 582 | |
| 583 // Stores the cursor position in the stored preedit text. | |
| 584 int preedit_cursor_position_; | |
| 585 | |
| 586 // Whether or not the preedit has been changed since last key event. | |
| 587 bool is_preedit_changed_; | |
| 588 | |
| 589 // Stores a copy of the most recent commit text received by commit signal | |
| 590 // handler. | |
| 591 string16 commit_text_; | |
| 592 | |
| 593 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewGtkIMContext); | |
| 594 }; | |
| 595 | |
| 596 // This class is a simple convenience wrapper for Gtk functions. It has only | 37 // This class is a simple convenience wrapper for Gtk functions. It has only |
| 597 // static methods. | 38 // static methods. |
| 598 class RenderWidgetHostViewGtkWidget { | 39 class RenderWidgetHostViewGtkWidget { |
| 599 public: | 40 public: |
| 600 static GtkWidget* CreateNewWidget(RenderWidgetHostViewGtk* host_view) { | 41 static GtkWidget* CreateNewWidget(RenderWidgetHostViewGtk* host_view) { |
| 601 GtkWidget* widget = gtk_fixed_new(); | 42 GtkWidget* widget = gtk_fixed_new(); |
| 602 gtk_fixed_set_has_window(GTK_FIXED(widget), TRUE); | 43 gtk_fixed_set_has_window(GTK_FIXED(widget), TRUE); |
| 603 gtk_widget_set_double_buffered(widget, FALSE); | 44 gtk_widget_set_double_buffered(widget, FALSE); |
| 604 gtk_widget_set_redraw_on_allocate(widget, FALSE); | 45 gtk_widget_set_redraw_on_allocate(widget, FALSE); |
| 605 #if defined(NDEBUG) | 46 #if defined(NDEBUG) |
| (...skipping 29 matching lines...) Expand all Loading... |
| 635 g_signal_connect(widget, "button-release-event", | 76 g_signal_connect(widget, "button-release-event", |
| 636 G_CALLBACK(ButtonPressReleaseEvent), host_view); | 77 G_CALLBACK(ButtonPressReleaseEvent), host_view); |
| 637 g_signal_connect(widget, "motion-notify-event", | 78 g_signal_connect(widget, "motion-notify-event", |
| 638 G_CALLBACK(MouseMoveEvent), host_view); | 79 G_CALLBACK(MouseMoveEvent), host_view); |
| 639 // Connect after so that we are called after the handler installed by the | 80 // Connect after so that we are called after the handler installed by the |
| 640 // TabContentsView which handles zoom events. | 81 // TabContentsView which handles zoom events. |
| 641 g_signal_connect_after(widget, "scroll-event", | 82 g_signal_connect_after(widget, "scroll-event", |
| 642 G_CALLBACK(MouseScrollEvent), host_view); | 83 G_CALLBACK(MouseScrollEvent), host_view); |
| 643 | 84 |
| 644 // Create GtkIMContext wrapper object. | 85 // Create GtkIMContext wrapper object. |
| 645 host_view->im_context_.reset( | 86 host_view->im_context_.reset(new GtkIMContextWrapper(host_view)); |
| 646 new RenderWidgetHostViewGtkIMContext(host_view)); | |
| 647 | 87 |
| 648 return widget; | 88 return widget; |
| 649 } | 89 } |
| 650 | 90 |
| 651 private: | 91 private: |
| 652 static gboolean SizeAllocate(GtkWidget* widget, GtkAllocation* allocation, | 92 static gboolean SizeAllocate(GtkWidget* widget, GtkAllocation* allocation, |
| 653 RenderWidgetHostViewGtk* host_view) { | 93 RenderWidgetHostViewGtk* host_view) { |
| 654 host_view->GetRenderWidgetHost()->WasResized(); | 94 host_view->GetRenderWidgetHost()->WasResized(); |
| 655 return FALSE; | 95 return FALSE; |
| 656 } | 96 } |
| (...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1197 } | 637 } |
| 1198 } | 638 } |
| 1199 | 639 |
| 1200 void RenderWidgetHostViewGtk::PluginProcessCrashed(base::ProcessId pid) { | 640 void RenderWidgetHostViewGtk::PluginProcessCrashed(base::ProcessId pid) { |
| 1201 for (PluginPidMap::iterator i = plugin_pid_map_.find(pid); | 641 for (PluginPidMap::iterator i = plugin_pid_map_.find(pid); |
| 1202 i != plugin_pid_map_.end() && i->first == pid; ++i) { | 642 i != plugin_pid_map_.end() && i->first == pid; ++i) { |
| 1203 plugin_container_manager_.DestroyPluginContainer(i->second); | 643 plugin_container_manager_.DestroyPluginContainer(i->second); |
| 1204 } | 644 } |
| 1205 plugin_pid_map_.erase(pid); | 645 plugin_pid_map_.erase(pid); |
| 1206 } | 646 } |
| OLD | NEW |