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

Side by Side Diff: views/ime/input_method_gtk.cc

Issue 6823055: Consolidate OnKeyEvent and InputMethod code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Refactor X2Event handling, nix Windows InputMethodDelegate, etc. Created 9 years, 8 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "views/ime/input_method_gtk.h" 5 #include "views/ime/input_method_gtk.h"
6 6
7 #include <gdk/gdk.h> 7 #include <gdk/gdk.h>
8 #include <gdk/gdkkeysyms.h> 8 #include <gdk/gdkkeysyms.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 UpdateContextFocusState(); 102 UpdateContextFocusState();
103 } 103 }
104 104
105 void InputMethodGtk::OnBlur() { 105 void InputMethodGtk::OnBlur() {
106 DCHECK(widget_focused()); 106 DCHECK(widget_focused());
107 ConfirmCompositionText(); 107 ConfirmCompositionText();
108 InputMethodBase::OnBlur(); 108 InputMethodBase::OnBlur();
109 UpdateContextFocusState(); 109 UpdateContextFocusState();
110 } 110 }
111 111
112 void InputMethodGtk::DispatchKeyEvent(const KeyEvent& key) { 112 bool InputMethodGtk::OnKeyEvent(const KeyEvent& key) {
113 DCHECK(key.type() == ui::ET_KEY_PRESSED || key.type() == ui::ET_KEY_RELEASED);
113 suppress_next_result_ = false; 114 suppress_next_result_ = false;
114 115
115 // We should bypass |context_| and |context_simple_| only if there is no 116 // We should bypass |context_| and |context_simple_| only if there is no
116 // text input client is focused at all. Otherwise, we should always send the 117 // text input client focused. Otherwise, always send the key event to either
117 // key event to either |context_| or |context_simple_| even if the text input 118 // |context_| or |context_simple_| even if the text input type is
118 // type is ui::TEXT_INPUT_TYPE_NONE, to make sure we can get correct character 119 // ui::TEXT_INPUT_TYPE_NONE, to make sure we can get correct character result.
119 // result. 120 if (!GetTextInputClient())
120 if (!GetTextInputClient()) { 121 return false;
121 DispatchKeyEventPostIME(key);
122 return;
123 }
124 122
125 handling_key_event_ = true; 123 handling_key_event_ = true;
126 composition_changed_ = false; 124 composition_changed_ = false;
127 result_text_.clear(); 125 result_text_.clear();
128 126
129 GdkEvent* event = key.native_event(); 127 // If it's a fake key event, then we need to synthesize a GdkEventKey.
130 DCHECK(!event || event->type == GDK_KEY_PRESS || 128 GdkEvent* event = key.native_event() ? key.native_event() :
131 event->type == GDK_KEY_RELEASE); 129 SynthesizeGdkEventKey(key);
132 130 gboolean filtered = gtk_im_context_filter_keypress(
133 // If it's a fake key event, then we need to synthesis a GdkEventKey. 131 context_focused_ ? context_ : context_simple_, &event->key);
134 bool need_free_event = false;
135 if (!event) {
136 event = SynthesizeGdkEventKey(key);
137 need_free_event = true;
138 }
139
140 gboolean filtered = false;
141 if (context_focused_)
142 filtered = gtk_im_context_filter_keypress(context_, &event->key);
143 else
144 filtered = gtk_im_context_filter_keypress(context_simple_, &event->key);
145 132
146 handling_key_event_ = false; 133 handling_key_event_ = false;
147 134
148 const View* old_focused_view = focused_view(); 135 const View* old_focused_view = focused_view();
149 if (key.type() == ui::ET_KEY_PRESSED && filtered) 136 if (key.type() == ui::ET_KEY_PRESSED && filtered)
150 ProcessFilteredKeyPressEvent(key); 137 ProcessFilteredKeyPressEvent(key);
151 138
152 // In case the focus was changed by the key event. 139 // Ensure no focus change from processing the key event.
153 if (old_focused_view != focused_view()) 140 if (old_focused_view == focused_view()) {
154 return; 141 if (HasInputMethodResult())
142 ProcessInputMethodResult(key, filtered);
143 // Ensure no focus change sending input method results to the focused View.
144 if (old_focused_view == focused_view()) {
145 if (key.type() == ui::ET_KEY_PRESSED && !filtered)
146 ProcessUnfilteredKeyPressEvent(key);
147 else if (key.type() == ui::ET_KEY_RELEASED)
148 DispatchKeyEventPostIME(key);
149 }
150 }
155 151
156 if (HasInputMethodResult()) 152 // Free the synthesized event if there was no underlying native event.
157 ProcessInputMethodResult(key, filtered); 153 if (event != key.native_event())
158
159 // In case the focus was changed when sending input method results to the
160 // focused View.
161 if (old_focused_view != focused_view())
162 return;
163
164 if (key.type() == ui::ET_KEY_PRESSED && !filtered)
165 ProcessUnfilteredKeyPressEvent(key);
166 else if (key.type() == ui::ET_KEY_RELEASED)
167 DispatchKeyEventPostIME(key);
168
169 if (need_free_event)
170 gdk_event_free(event); 154 gdk_event_free(event);
155 return true;
171 } 156 }
172 157
173 void InputMethodGtk::OnTextInputTypeChanged(View* view) { 158 void InputMethodGtk::OnTextInputTypeChanged(View* view) {
174 if (IsViewFocused(view)) { 159 if (IsViewFocused(view)) {
175 DCHECK(!composing_text_); 160 DCHECK(!composing_text_);
176 UpdateContextFocusState(); 161 UpdateContextFocusState();
177 } 162 }
178 } 163 }
179 164
180 void InputMethodGtk::OnCaretBoundsChanged(View* view) { 165 void InputMethodGtk::OnCaretBoundsChanged(View* view) {
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 void InputMethodGtk::SendFakeProcessKeyEvent(bool pressed) const { 338 void InputMethodGtk::SendFakeProcessKeyEvent(bool pressed) const {
354 KeyEvent key(pressed ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED, 339 KeyEvent key(pressed ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED,
355 ui::VKEY_PROCESSKEY, 0); 340 ui::VKEY_PROCESSKEY, 0);
356 DispatchKeyEventPostIME(key); 341 DispatchKeyEventPostIME(key);
357 } 342 }
358 343
359 GdkEvent* InputMethodGtk::SynthesizeGdkEventKey(const KeyEvent& key) const { 344 GdkEvent* InputMethodGtk::SynthesizeGdkEventKey(const KeyEvent& key) const {
360 guint keyval = 345 guint keyval =
361 ui::GdkKeyCodeForWindowsKeyCode(key.key_code(), key.IsShiftDown()); 346 ui::GdkKeyCodeForWindowsKeyCode(key.key_code(), key.IsShiftDown());
362 guint state = 0; 347 guint state = 0;
363 if (key.IsShiftDown()) 348 state |= key.IsShiftDown() ? GDK_SHIFT_MASK : 0;
364 state |= GDK_SHIFT_MASK; 349 state |= key.IsControlDown() ? GDK_CONTROL_MASK : 0;
365 if (key.IsControlDown()) 350 state |= key.IsAltDown() ? GDK_MOD1_MASK : 0;
366 state |= GDK_CONTROL_MASK; 351 state |= key.IsCapsLockDown() ? GDK_LOCK_MASK : 0;
367 if (key.IsAltDown())
368 state |= GDK_MOD1_MASK;
369 if (key.IsCapsLockDown())
370 state |= GDK_LOCK_MASK;
371 352
372 DCHECK(widget()->GetNativeView()->window); 353 DCHECK(widget()->GetNativeView()->window);
373 return ui::SynthesizeKeyEvent(widget()->GetNativeView()->window, 354 return ui::SynthesizeKeyEvent(widget()->GetNativeView()->window,
374 key.type() == ui::ET_KEY_PRESSED, 355 key.type() == ui::ET_KEY_PRESSED,
375 keyval, state); 356 keyval, state);
376 } 357 }
377 358
378 void InputMethodGtk::OnCommit(GtkIMContext* context, gchar* text) { 359 void InputMethodGtk::OnCommit(GtkIMContext* context, gchar* text) {
379 if (suppress_next_result_) { 360 if (suppress_next_result_) {
380 suppress_next_result_ = false; 361 suppress_next_result_ = false;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 gtk_im_context_set_client_window(context_simple_, widget->window); 439 gtk_im_context_set_client_window(context_simple_, widget->window);
459 } 440 }
460 } 441 }
461 442
462 void InputMethodGtk::OnWidgetUnrealize(GtkWidget* widget) { 443 void InputMethodGtk::OnWidgetUnrealize(GtkWidget* widget) {
463 gtk_im_context_set_client_window(context_, NULL); 444 gtk_im_context_set_client_window(context_, NULL);
464 gtk_im_context_set_client_window(context_simple_, NULL); 445 gtk_im_context_set_client_window(context_simple_, NULL);
465 } 446 }
466 447
467 } // namespace views 448 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698