Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Side by Side Diff: chrome/browser/renderer_host/render_widget_host_view_gtk.cc

Issue 149755: This change list improves IME support on Linux. Many corner cases that were n... Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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>
(...skipping 12 matching lines...) Expand all
23 #include "chrome/browser/renderer_host/backing_store.h" 23 #include "chrome/browser/renderer_host/backing_store.h"
24 #include "chrome/browser/renderer_host/render_widget_host.h" 24 #include "chrome/browser/renderer_host/render_widget_host.h"
25 #include "webkit/api/public/gtk/WebInputEventFactory.h" 25 #include "webkit/api/public/gtk/WebInputEventFactory.h"
26 #include "webkit/glue/webcursor_gtk_data.h" 26 #include "webkit/glue/webcursor_gtk_data.h"
27 27
28 static const int kMaxWindowWidth = 4000; 28 static const int kMaxWindowWidth = 4000;
29 static const int kMaxWindowHeight = 4000; 29 static const int kMaxWindowHeight = 4000;
30 30
31 using WebKit::WebInputEventFactory; 31 using WebKit::WebInputEventFactory;
32 32
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
33 // This class is a simple convenience wrapper for Gtk functions. It has only 596 // This class is a simple convenience wrapper for Gtk functions. It has only
34 // static methods. 597 // static methods.
35 class RenderWidgetHostViewGtkWidget { 598 class RenderWidgetHostViewGtkWidget {
36 public: 599 public:
37 static GtkWidget* CreateNewWidget(RenderWidgetHostViewGtk* host_view) { 600 static GtkWidget* CreateNewWidget(RenderWidgetHostViewGtk* host_view) {
38 GtkWidget* widget = gtk_fixed_new(); 601 GtkWidget* widget = gtk_fixed_new();
39 gtk_fixed_set_has_window(GTK_FIXED(widget), TRUE); 602 gtk_fixed_set_has_window(GTK_FIXED(widget), TRUE);
40 gtk_widget_set_double_buffered(widget, FALSE); 603 gtk_widget_set_double_buffered(widget, FALSE);
41 gtk_widget_set_redraw_on_allocate(widget, FALSE); 604 gtk_widget_set_redraw_on_allocate(widget, FALSE);
42 #if defined(NDEBUG) 605 #if defined(NDEBUG)
(...skipping 28 matching lines...) Expand all
71 G_CALLBACK(ButtonPressReleaseEvent), host_view); 634 G_CALLBACK(ButtonPressReleaseEvent), host_view);
72 g_signal_connect(widget, "button-release-event", 635 g_signal_connect(widget, "button-release-event",
73 G_CALLBACK(ButtonPressReleaseEvent), host_view); 636 G_CALLBACK(ButtonPressReleaseEvent), host_view);
74 g_signal_connect(widget, "motion-notify-event", 637 g_signal_connect(widget, "motion-notify-event",
75 G_CALLBACK(MouseMoveEvent), host_view); 638 G_CALLBACK(MouseMoveEvent), host_view);
76 // Connect after so that we are called after the handler installed by the 639 // Connect after so that we are called after the handler installed by the
77 // TabContentsView which handles zoom events. 640 // TabContentsView which handles zoom events.
78 g_signal_connect_after(widget, "scroll-event", 641 g_signal_connect_after(widget, "scroll-event",
79 G_CALLBACK(MouseScrollEvent), host_view); 642 G_CALLBACK(MouseScrollEvent), host_view);
80 643
81 // Create a GtkIMContext instance and attach its signal handlers. 644 // Create GtkIMContext wrapper object.
82 host_view->im_context_ = gtk_im_multicontext_new(); 645 host_view->im_context_.reset(
83 g_signal_connect(host_view->im_context_, "preedit_start", 646 new RenderWidgetHostViewGtkIMContext(host_view));
84 G_CALLBACK(InputMethodPreeditStart), host_view);
85 g_signal_connect(host_view->im_context_, "preedit_end",
86 G_CALLBACK(InputMethodPreeditEnd), host_view);
87 g_signal_connect(host_view->im_context_, "preedit_changed",
88 G_CALLBACK(InputMethodPreeditChanged), host_view);
89 g_signal_connect(host_view->im_context_, "commit",
90 G_CALLBACK(InputMethodCommit), host_view);
91 647
92 return widget; 648 return widget;
93 } 649 }
94 650
95 private: 651 private:
96 static gboolean SizeAllocate(GtkWidget* widget, GtkAllocation* allocation, 652 static gboolean SizeAllocate(GtkWidget* widget, GtkAllocation* allocation,
97 RenderWidgetHostViewGtk* host_view) { 653 RenderWidgetHostViewGtk* host_view) {
98 host_view->GetRenderWidgetHost()->WasResized(); 654 host_view->GetRenderWidgetHost()->WasResized();
99 return FALSE; 655 return FALSE;
100 } 656 }
101 657
102 static gboolean ExposeEvent(GtkWidget* widget, GdkEventExpose* expose, 658 static gboolean ExposeEvent(GtkWidget* widget, GdkEventExpose* expose,
103 RenderWidgetHostViewGtk* host_view) { 659 RenderWidgetHostViewGtk* host_view) {
104 const gfx::Rect damage_rect(expose->area); 660 const gfx::Rect damage_rect(expose->area);
105 host_view->Paint(damage_rect); 661 host_view->Paint(damage_rect);
106 return FALSE; 662 return FALSE;
107 } 663 }
108 664
109 static gboolean KeyPressReleaseEvent(GtkWidget* widget, GdkEventKey* event, 665 static gboolean KeyPressReleaseEvent(GtkWidget* widget, GdkEventKey* event,
110 RenderWidgetHostViewGtk* host_view) { 666 RenderWidgetHostViewGtk* host_view) {
111 if (host_view->parent_ && host_view->activatable() && 667 if (host_view->parent_ && host_view->activatable() &&
112 GDK_Escape == event->keyval) { 668 GDK_Escape == event->keyval) {
113 // Force popups to close on Esc just in case the renderer is hung. This 669 // Force popups to close on Esc just in case the renderer is hung. This
114 // allows us to release our keyboard grab. 670 // allows us to release our keyboard grab.
115 host_view->host_->Shutdown(); 671 host_view->host_->Shutdown();
116 } else { 672 } else {
117 NativeWebKeyboardEvent wke(event); 673 // Send key event to input method.
118 host_view->GetRenderWidgetHost()->ForwardKeyboardEvent(wke); 674 host_view->im_context_->ProcessKeyEvent(event);
119 }
120
121 // Save the current modifier-key state before dispatching this event to the
122 // GtkIMContext object so its event handlers can use this state to create
123 // Char events.
124 host_view->im_modifier_state_ = event->state;
125
126 // Dispatch this event to the GtkIMContext object.
127 // It sends a "commit" signal when it has a character to be inserted
128 // even when we use a US keyboard so that we can send a Char event
129 // (or an IME event) to the renderer in our "commit"-signal handler.
130 // We should send a KeyDown (or a KeyUp) event before dispatching this
131 // event to the GtkIMContext object (and send a Char event) so that WebKit
132 // can dispatch the JavaScript events in the following order: onkeydown(),
133 // onkeypress(), and onkeyup(). (Many JavaScript pages assume this.)
134 // TODO(hbono): we should not dispatch a key event when the input focus
135 // is in a password input?
136 if (!gtk_im_context_filter_keypress(host_view->im_context_, event)) {
137 // The GtkIMContext object cannot handle this key event.
138 // This case is caused by two reasons:
139 // 1. The given key event is a control-key event, (e.g. return, page up,
140 // page down, tab, arrows, etc.) or;
141 // 2. The given key event is not a control-key event but printable
142 // characters aren't assigned to the event, (e.g. alt+d, etc.)
143 // Create a Char event manually from this key event and send it to the
144 // renderer when this Char event contains a printable character which
145 // should be processed by WebKit.
146 // TODO(hbono): Windows Chrome sends a Char event with its isSystemKey
147 // value true for the above case 2. We should emulate this behavior?
148 if (event->type == GDK_KEY_PRESS &&
149 !gdk_keyval_to_unicode(event->keyval)) {
150 NativeWebKeyboardEvent wke(event);
151 wke.type = WebKit::WebInputEvent::Char;
152 if (wke.text[0])
153 host_view->GetRenderWidgetHost()->ForwardKeyboardEvent(wke);
154 }
155 } 675 }
156 676
157 // We return TRUE because we did handle the event. If it turns out webkit 677 // We return TRUE because we did handle the event. If it turns out webkit
158 // can't handle the event, we'll deal with it in 678 // can't handle the event, we'll deal with it in
159 // RenderView::UnhandledKeyboardEvent(). 679 // RenderView::UnhandledKeyboardEvent().
160 return TRUE; 680 return TRUE;
161 } 681 }
162 682
163 // WARNING: OnGrabNotify relies on the fact this function doesn't try to 683 // WARNING: OnGrabNotify relies on the fact this function doesn't try to
164 // dereference |focus|. 684 // dereference |focus|.
(...skipping 16 matching lines...) Expand all
181 fake_event.globalX = fake_event.x + x; 701 fake_event.globalX = fake_event.x + x;
182 fake_event.globalY = fake_event.y + y; 702 fake_event.globalY = fake_event.y + y;
183 fake_event.type = WebKit::WebInputEvent::MouseMove; 703 fake_event.type = WebKit::WebInputEvent::MouseMove;
184 fake_event.button = WebKit::WebMouseEvent::ButtonNone; 704 fake_event.button = WebKit::WebMouseEvent::ButtonNone;
185 host_view->GetRenderWidgetHost()->ForwardMouseEvent(fake_event); 705 host_view->GetRenderWidgetHost()->ForwardMouseEvent(fake_event);
186 } 706 }
187 707
188 host_view->ShowCurrentCursor(); 708 host_view->ShowCurrentCursor();
189 host_view->GetRenderWidgetHost()->Focus(); 709 host_view->GetRenderWidgetHost()->Focus();
190 710
191 // Notify the GtkIMContext object of this focus-in event and 711 // The only way to enable a GtkIMContext object is to call its focus in
192 // attach this GtkIMContext object to this window. 712 // handler.
193 // We should call gtk_im_context_set_client_window() only when this window 713 host_view->im_context_->OnFocusIn();
194 // gain (or release) the window focus because an immodule may reset its 714
195 // internal status when processing this function.
196 gtk_im_context_focus_in(host_view->im_context_);
197 gtk_im_context_set_client_window(host_view->im_context_,
198 host_view->native_view()->window);
199 return FALSE; 715 return FALSE;
200 } 716 }
201 717
202 // WARNING: OnGrabNotify relies on the fact this function doesn't try to 718 // WARNING: OnGrabNotify relies on the fact this function doesn't try to
203 // dereference |focus|. 719 // dereference |focus|.
204 static gboolean OnFocusOut(GtkWidget* widget, GdkEventFocus* focus, 720 static gboolean OnFocusOut(GtkWidget* widget, GdkEventFocus* focus,
205 RenderWidgetHostViewGtk* host_view) { 721 RenderWidgetHostViewGtk* host_view) {
206 // Whenever we lose focus, set the cursor back to that of our parent window, 722 // Whenever we lose focus, set the cursor back to that of our parent window,
207 // which should be the default arrow. 723 // which should be the default arrow.
208 gdk_window_set_cursor(widget->window, NULL); 724 gdk_window_set_cursor(widget->window, NULL);
209 // If we are showing a context menu, maintain the illusion that webkit has 725 // If we are showing a context menu, maintain the illusion that webkit has
210 // focus. 726 // focus.
211 if (!host_view->is_showing_context_menu_) 727 if (!host_view->is_showing_context_menu_)
212 host_view->GetRenderWidgetHost()->Blur(); 728 host_view->GetRenderWidgetHost()->Blur();
213 729
214 // Notify the GtkIMContext object of this focus-in event and 730 // Disable the GtkIMContext object.
215 // detach this GtkIMContext object from this window. 731 host_view->im_context_->OnFocusOut();
216 gtk_im_context_focus_out(host_view->im_context_); 732
217 gtk_im_context_set_client_window(host_view->im_context_, NULL);
218 return FALSE; 733 return FALSE;
219 } 734 }
220 735
221 // Called when we are shadowed or unshadowed by a keyboard grab (which will 736 // Called when we are shadowed or unshadowed by a keyboard grab (which will
222 // occur for activatable popups, such as dropdown menus). Popup windows do not 737 // occur for activatable popups, such as dropdown menus). Popup windows do not
223 // take focus, so we never get a focus out or focus in event when they are 738 // take focus, so we never get a focus out or focus in event when they are
224 // shown, and must rely on this signal instead. 739 // shown, and must rely on this signal instead.
225 static void OnGrabNotify(GtkWidget* widget, gboolean was_grabbed, 740 static void OnGrabNotify(GtkWidget* widget, gboolean was_grabbed,
226 RenderWidgetHostViewGtk* host_view) { 741 RenderWidgetHostViewGtk* host_view) {
227 if (was_grabbed) 742 if (was_grabbed) {
228 OnFocusIn(widget, NULL, host_view); 743 if (host_view->was_focused_before_grab_)
229 else 744 OnFocusIn(widget, NULL, host_view);
230 OnFocusOut(widget, NULL, host_view); 745 } else {
746 host_view->was_focused_before_grab_ = host_view->HasFocus();
747 if (host_view->was_focused_before_grab_)
748 OnFocusOut(widget, NULL, host_view);
749 }
231 } 750 }
232 751
233 static gboolean ButtonPressReleaseEvent( 752 static gboolean ButtonPressReleaseEvent(
234 GtkWidget* widget, GdkEventButton* event, 753 GtkWidget* widget, GdkEventButton* event,
235 RenderWidgetHostViewGtk* host_view) { 754 RenderWidgetHostViewGtk* host_view) {
236 if (!(event->button == 1 || event->button == 2 || event->button == 3)) 755 if (!(event->button == 1 || event->button == 2 || event->button == 3))
237 return FALSE; // We do not forward any other buttons to the renderer. 756 return FALSE; // We do not forward any other buttons to the renderer.
238 757
239 // We want to translate the coordinates of events that do not originate 758 // We want to translate the coordinates of events that do not originate
240 // from this widget to be relative to the top left of the widget. 759 // from this widget to be relative to the top left of the widget.
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 event->direction = GDK_SCROLL_LEFT; 821 event->direction = GDK_SCROLL_LEFT;
303 else if (event->direction == GDK_SCROLL_DOWN) 822 else if (event->direction == GDK_SCROLL_DOWN)
304 event->direction = GDK_SCROLL_RIGHT; 823 event->direction = GDK_SCROLL_RIGHT;
305 } 824 }
306 825
307 host_view->GetRenderWidgetHost()->ForwardWheelEvent( 826 host_view->GetRenderWidgetHost()->ForwardWheelEvent(
308 WebInputEventFactory::mouseWheelEvent(event)); 827 WebInputEventFactory::mouseWheelEvent(event));
309 return FALSE; 828 return FALSE;
310 } 829 }
311 830
312 static void InputMethodCommit(GtkIMContext* im_context,
313 gchar* text,
314 RenderWidgetHostViewGtk* host_view) {
315 const string16& im_text = UTF8ToUTF16(text);
316 if (!host_view->im_is_composing_cjk_text_ && im_text.length() == 1) {
317 // Send a Char event when we input a composed character without IMEs so
318 // that this event is to be dispatched to onkeypress() handlers,
319 // autofill, etc.
320 ForwardCharEvent(host_view, im_text[0]);
321 } else {
322 // Send an IME event.
323 // Unlike a Char event, an IME event is NOT dispatched to onkeypress()
324 // handlers or autofill.
325 host_view->GetRenderWidgetHost()->ImeConfirmComposition(im_text);
326 }
327 }
328
329 static void InputMethodPreeditStart(GtkIMContext* im_context,
330 RenderWidgetHostViewGtk* host_view) {
331 // Start monitoring IME events of the renderer.
332 // TODO(hbono): a renderer sends these IME events not only for sending the
333 // caret position, but also for enabling/disabling IMEs. If we need to
334 // enable/disable IMEs, we should move this code to a better place.
335 // (This signal handler is called only when an IME is enabled. So, once
336 // we disable an IME, we cannot receive any IME events from the renderer,
337 // i.e. we cannot re-enable the IME any longer.)
338 host_view->GetRenderWidgetHost()->ImeSetInputMode(true);
339 host_view->im_is_composing_cjk_text_ = true;
340 }
341
342 static void InputMethodPreeditEnd(GtkIMContext* im_context,
343 RenderWidgetHostViewGtk* host_view) {
344 // End monitoring IME events.
345 host_view->GetRenderWidgetHost()->ImeSetInputMode(false);
346 host_view->im_is_composing_cjk_text_ = false;
347 }
348
349 static void InputMethodPreeditChanged(GtkIMContext* im_context,
350 RenderWidgetHostViewGtk* host_view) {
351 // Send an IME event to update the composition node of the renderer.
352 // TODO(hbono): an IME intercepts all key events while composing a text,
353 // i.e. we cannot receive any GDK_KEY_PRESS (or GDK_KEY_UP) events.
354 // Should we send pseudo KeyDown (and KeyUp) events to emulate Windows?
355 gchar* preedit_text = NULL;
356 gint cursor_position = 0;
357 gtk_im_context_get_preedit_string(im_context, &preedit_text, NULL,
358 &cursor_position);
359 host_view->GetRenderWidgetHost()->ImeSetComposition(
360 UTF8ToUTF16(preedit_text), cursor_position, -1, -1);
361 g_free(preedit_text);
362 }
363
364 static void ForwardCharEvent(RenderWidgetHostViewGtk* host_view,
365 wchar_t im_character) {
366 if (!im_character)
367 return;
368
369 NativeWebKeyboardEvent char_event(im_character,
370 host_view->im_modifier_state_,
371 base::Time::Now().ToDoubleT());
372 host_view->GetRenderWidgetHost()->ForwardKeyboardEvent(char_event);
373 }
374
375 DISALLOW_IMPLICIT_CONSTRUCTORS(RenderWidgetHostViewGtkWidget); 831 DISALLOW_IMPLICIT_CONSTRUCTORS(RenderWidgetHostViewGtkWidget);
376 }; 832 };
377 833
378 // static 834 // static
379 RenderWidgetHostView* RenderWidgetHostView::CreateViewForWidget( 835 RenderWidgetHostView* RenderWidgetHostView::CreateViewForWidget(
380 RenderWidgetHost* widget) { 836 RenderWidgetHost* widget) {
381 return new RenderWidgetHostViewGtk(widget); 837 return new RenderWidgetHostViewGtk(widget);
382 } 838 }
383 839
384 RenderWidgetHostViewGtk::RenderWidgetHostViewGtk(RenderWidgetHost* widget_host) 840 RenderWidgetHostViewGtk::RenderWidgetHostViewGtk(RenderWidgetHost* widget_host)
385 : host_(widget_host), 841 : host_(widget_host),
386 about_to_validate_and_paint_(false), 842 about_to_validate_and_paint_(false),
387 is_hidden_(false), 843 is_hidden_(false),
388 is_loading_(false), 844 is_loading_(false),
389 is_showing_context_menu_(false), 845 is_showing_context_menu_(false),
390 parent_host_view_(NULL), 846 parent_host_view_(NULL),
391 parent_(NULL), 847 parent_(NULL),
392 is_popup_first_mouse_release_(true), 848 is_popup_first_mouse_release_(true),
393 im_context_(NULL), 849 was_focused_before_grab_(false) {
394 im_is_composing_cjk_text_(false),
395 im_modifier_state_(0) {
396 host_->set_view(this); 850 host_->set_view(this);
397 } 851 }
398 852
399 RenderWidgetHostViewGtk::~RenderWidgetHostViewGtk() { 853 RenderWidgetHostViewGtk::~RenderWidgetHostViewGtk() {
400 if (im_context_)
401 g_object_unref(im_context_);
402 view_.Destroy(); 854 view_.Destroy();
403 } 855 }
404 856
405 void RenderWidgetHostViewGtk::InitAsChild() { 857 void RenderWidgetHostViewGtk::InitAsChild() {
406 view_.Own(RenderWidgetHostViewGtkWidget::CreateNewWidget(this)); 858 view_.Own(RenderWidgetHostViewGtkWidget::CreateNewWidget(this));
407 plugin_container_manager_.set_host_widget(view_.get()); 859 plugin_container_manager_.set_host_widget(view_.get());
408 gtk_widget_show(view_.get()); 860 gtk_widget_show(view_.get());
409 } 861 }
410 862
411 void RenderWidgetHostViewGtk::InitAsPopup( 863 void RenderWidgetHostViewGtk::InitAsPopup(
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 1004
553 void RenderWidgetHostViewGtk::SetIsLoading(bool is_loading) { 1005 void RenderWidgetHostViewGtk::SetIsLoading(bool is_loading) {
554 is_loading_ = is_loading; 1006 is_loading_ = is_loading;
555 // Only call ShowCurrentCursor() when it will actually change the cursor. 1007 // Only call ShowCurrentCursor() when it will actually change the cursor.
556 if (current_cursor_.GetCursorType() == GDK_LAST_CURSOR) 1008 if (current_cursor_.GetCursorType() == GDK_LAST_CURSOR)
557 ShowCurrentCursor(); 1009 ShowCurrentCursor();
558 } 1010 }
559 1011
560 void RenderWidgetHostViewGtk::IMEUpdateStatus(int control, 1012 void RenderWidgetHostViewGtk::IMEUpdateStatus(int control,
561 const gfx::Rect& caret_rect) { 1013 const gfx::Rect& caret_rect) {
562 // The renderer has updated its IME status. 1014 im_context_->UpdateStatus(control, caret_rect);
563 // Control the GtkIMContext object according to this status.
564 if (!im_context_)
565 return;
566
567 if (control == IME_DISABLE) {
568 // TODO(hbono): this code just resets the GtkIMContext object.
569 // Should we prevent sending key events to the GtkIMContext object
570 // (or unref it) when we disable IMEs?
571 gtk_im_context_reset(im_context_);
572 gtk_im_context_set_cursor_location(im_context_, NULL);
573 } else {
574 // TODO(hbono): we should finish (not reset) an ongoing composition
575 // when |control| is IME_COMPLETE_COMPOSITION.
576
577 // Updates the position of the IME candidate window.
578 // The position sent from the renderer is a relative one, so we need to
579 // attach the GtkIMContext object to this window before changing the
580 // position.
581 GdkRectangle cursor_rect(caret_rect.ToGdkRectangle());
582 gtk_im_context_set_cursor_location(im_context_, &cursor_rect);
583 }
584 } 1015 }
585 1016
586 void RenderWidgetHostViewGtk::DidPaintRect(const gfx::Rect& rect) { 1017 void RenderWidgetHostViewGtk::DidPaintRect(const gfx::Rect& rect) {
587 if (is_hidden_) 1018 if (is_hidden_)
588 return; 1019 return;
589 1020
590 if (about_to_validate_and_paint_) 1021 if (about_to_validate_and_paint_)
591 invalid_rect_ = invalid_rect_.Union(rect); 1022 invalid_rect_ = invalid_rect_.Union(rect);
592 else 1023 else
593 Paint(rect); 1024 Paint(rect);
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 } 1191 }
761 } 1192 }
762 1193
763 void RenderWidgetHostViewGtk::PluginProcessCrashed(base::ProcessId pid) { 1194 void RenderWidgetHostViewGtk::PluginProcessCrashed(base::ProcessId pid) {
764 for (PluginPidMap::iterator i = plugin_pid_map_.find(pid); 1195 for (PluginPidMap::iterator i = plugin_pid_map_.find(pid);
765 i != plugin_pid_map_.end() && i->first == pid; ++i) { 1196 i != plugin_pid_map_.end() && i->first == pid; ++i) {
766 plugin_container_manager_.DestroyPluginContainer(i->second); 1197 plugin_container_manager_.DestroyPluginContainer(i->second);
767 } 1198 }
768 plugin_pid_map_.erase(pid); 1199 plugin_pid_map_.erase(pid);
769 } 1200 }
OLDNEW
« no previous file with comments | « chrome/browser/renderer_host/render_widget_host_view_gtk.h ('k') | webkit/api/src/gtk/WebInputEventFactory.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698