OLD | NEW |
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 "ui/views/ime/mock_input_method.h" | 5 #include "ui/views/ime/mock_input_method.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "ui/base/ime/text_input_client.h" | 9 #include "ui/base/ime/text_input_client.h" |
10 #include "ui/events/event.h" | 10 #include "ui/events/event.h" |
11 #include "ui/events/keycodes/keyboard_codes.h" | 11 #include "ui/events/keycodes/keyboard_codes.h" |
12 #include "ui/views/widget/widget.h" | 12 #include "ui/views/widget/widget.h" |
13 | 13 |
14 namespace views { | 14 namespace views { |
15 | 15 |
16 MockInputMethod::MockInputMethod() | 16 MockInputMethod::MockInputMethod() |
17 : composition_changed_(false), | 17 : untranslated_ime_message_called_(false), |
18 focus_changed_(false), | |
19 untranslated_ime_message_called_(false), | |
20 text_input_type_changed_(false), | 18 text_input_type_changed_(false), |
21 caret_bounds_changed_(false), | 19 cancel_composition_called_(false) { |
22 cancel_composition_called_(false), | |
23 input_locale_changed_(false), | |
24 locale_("en-US"), | |
25 active_(true) { | |
26 } | 20 } |
27 | 21 |
28 MockInputMethod::MockInputMethod(internal::InputMethodDelegate* delegate) | 22 MockInputMethod::MockInputMethod(internal::InputMethodDelegate* delegate) |
29 : composition_changed_(false), | 23 : untranslated_ime_message_called_(false), |
30 focus_changed_(false), | |
31 untranslated_ime_message_called_(false), | |
32 text_input_type_changed_(false), | 24 text_input_type_changed_(false), |
33 caret_bounds_changed_(false), | 25 cancel_composition_called_(false) { |
34 cancel_composition_called_(false), | |
35 input_locale_changed_(false), | |
36 locale_("en-US"), | |
37 active_(true) { | |
38 SetDelegate(delegate); | 26 SetDelegate(delegate); |
39 } | 27 } |
40 | 28 |
41 MockInputMethod::~MockInputMethod() { | 29 MockInputMethod::~MockInputMethod() { |
42 } | 30 } |
43 | 31 |
44 void MockInputMethod::Init(Widget* widget) { | |
45 InputMethodBase::Init(widget); | |
46 } | |
47 | |
48 void MockInputMethod::OnFocus() {} | 32 void MockInputMethod::OnFocus() {} |
49 | 33 |
50 void MockInputMethod::OnBlur() {} | 34 void MockInputMethod::OnBlur() {} |
51 | 35 |
52 bool MockInputMethod::OnUntranslatedIMEMessage( | 36 bool MockInputMethod::OnUntranslatedIMEMessage( |
53 const base::NativeEvent& event, | 37 const base::NativeEvent& event, |
54 NativeEventResult* result) { | 38 NativeEventResult* result) { |
55 untranslated_ime_message_called_ = true; | 39 untranslated_ime_message_called_ = true; |
56 if (result) | 40 if (result) |
57 *result = InputMethod::NativeEventResult(); | 41 *result = InputMethod::NativeEventResult(); |
58 return false; | 42 return false; |
59 } | 43 } |
60 | 44 |
61 void MockInputMethod::DispatchKeyEvent(const ui::KeyEvent& key) { | 45 void MockInputMethod::DispatchKeyEvent(const ui::KeyEvent& key) { |
62 bool handled = (composition_changed_ || result_text_.length()) && | 46 bool handled = !IsTextInputTypeNone() && HasMockComposition(); |
63 !IsTextInputTypeNone(); | |
64 | 47 |
65 ClearStates(); | 48 ClearStates(); |
66 if (handled) { | 49 if (handled) { |
67 DCHECK(!key.is_char()); | 50 DCHECK(!key.is_char()); |
68 ui::KeyEvent mock_key(ui::ET_KEY_PRESSED, | 51 ui::KeyEvent mock_key(ui::ET_KEY_PRESSED, |
69 ui::VKEY_PROCESSKEY, | 52 ui::VKEY_PROCESSKEY, |
70 key.flags()); | 53 key.flags()); |
71 DispatchKeyEventPostIME(mock_key); | 54 DispatchKeyEventPostIME(mock_key); |
72 } else { | 55 } else { |
73 DispatchKeyEventPostIME(key); | 56 DispatchKeyEventPostIME(key); |
74 } | 57 } |
75 | 58 |
76 if (focus_changed_) | |
77 return; | |
78 | |
79 ui::TextInputClient* client = GetTextInputClient(); | 59 ui::TextInputClient* client = GetTextInputClient(); |
80 if (client) { | 60 if (client) { |
81 if (handled) { | 61 if (handled) { |
82 if (result_text_.length()) | 62 InsertMockComposition(); |
83 client->InsertText(result_text_); | |
84 if (composition_changed_) { | |
85 if (composition_.text.length()) | |
86 client->SetCompositionText(composition_); | |
87 else | |
88 client->ClearCompositionText(); | |
89 } | |
90 } else if (key.type() == ui::ET_KEY_PRESSED) { | 63 } else if (key.type() == ui::ET_KEY_PRESSED) { |
91 base::char16 ch = key.GetCharacter(); | 64 base::char16 ch = key.GetCharacter(); |
92 client->InsertChar(ch, key.flags()); | 65 client->InsertChar(ch, key.flags()); |
93 } | 66 } |
94 } | 67 } |
95 | 68 |
96 ClearResult(); | 69 ClearMockComposition(); |
97 } | 70 } |
98 | 71 |
99 void MockInputMethod::OnTextInputTypeChanged(View* view) { | 72 void MockInputMethod::OnTextInputTypeChanged(View* view) { |
100 if (IsViewFocused(view)) | 73 if (IsViewFocused(view)) |
101 text_input_type_changed_ = true; | 74 text_input_type_changed_ = true; |
102 InputMethodBase::OnTextInputTypeChanged(view); | 75 InputMethodBase::OnTextInputTypeChanged(view); |
103 } | 76 } |
104 | 77 |
105 void MockInputMethod::OnCaretBoundsChanged(View* view) { | 78 void MockInputMethod::OnCaretBoundsChanged(View* view) { |
106 if (IsViewFocused(view)) | |
107 caret_bounds_changed_ = true; | |
108 } | 79 } |
109 | 80 |
110 void MockInputMethod::CancelComposition(View* view) { | 81 void MockInputMethod::CancelComposition(View* view) { |
111 if (IsViewFocused(view)) { | 82 if (IsViewFocused(view)) { |
112 cancel_composition_called_ = true; | 83 cancel_composition_called_ = true; |
113 ClearResult(); | 84 ClearMockComposition(); |
114 } | 85 } |
115 } | 86 } |
116 | 87 |
117 void MockInputMethod::OnInputLocaleChanged() { | 88 void MockInputMethod::OnInputLocaleChanged() { |
118 input_locale_changed_ = true; | |
119 } | 89 } |
120 | 90 |
121 std::string MockInputMethod::GetInputLocale() { | 91 std::string MockInputMethod::GetInputLocale() { |
122 return locale_; | 92 return "en-US"; |
123 } | 93 } |
124 | 94 |
125 bool MockInputMethod::IsActive() { | 95 bool MockInputMethod::IsActive() { |
126 return active_; | 96 return true; |
127 } | 97 } |
128 | 98 |
129 bool MockInputMethod::IsCandidatePopupOpen() const { | 99 bool MockInputMethod::IsCandidatePopupOpen() const { |
130 return false; | 100 return false; |
131 } | 101 } |
132 | 102 |
133 void MockInputMethod::ShowImeIfNeeded() { | 103 void MockInputMethod::ShowImeIfNeeded() { |
134 } | 104 } |
135 | 105 |
136 bool MockInputMethod::IsMock() const { | 106 bool MockInputMethod::IsMock() const { |
137 return true; | 107 return true; |
138 } | 108 } |
139 | 109 |
140 void MockInputMethod::OnWillChangeFocus(View* focused_before, View* focused) { | 110 void MockInputMethod::OnWillChangeFocus(View* focused_before, View* focused) { |
141 ui::TextInputClient* client = GetTextInputClient(); | 111 ui::TextInputClient* client = GetTextInputClient(); |
142 if (client && client->HasCompositionText()) | 112 if (client && client->HasCompositionText()) |
143 client->ConfirmCompositionText(); | 113 client->ConfirmCompositionText(); |
144 focus_changed_ = true; | 114 ClearMockComposition(); |
145 ClearResult(); | |
146 } | 115 } |
147 | 116 |
148 void MockInputMethod::Clear() { | 117 void MockInputMethod::Clear() { |
149 ClearStates(); | 118 ClearStates(); |
150 ClearResult(); | 119 ClearMockComposition(); |
151 } | 120 } |
152 | 121 |
153 void MockInputMethod::SetCompositionTextForNextKey( | 122 void MockInputMethod::SetCompositionTextForNextKey( |
154 const ui::CompositionText& composition) { | 123 const ui::CompositionText& composition) { |
155 composition_changed_ = true; | |
156 composition_ = composition; | 124 composition_ = composition; |
157 } | 125 } |
158 | 126 |
159 void MockInputMethod::SetResultTextForNextKey(const base::string16& result) { | 127 void MockInputMethod::SetResultTextForNextKey(const base::string16& result) { |
160 result_text_ = result; | 128 result_text_ = result; |
161 } | 129 } |
162 | 130 |
163 void MockInputMethod::SetInputLocale(const std::string& locale) { | 131 void MockInputMethod::InsertMockComposition() { |
164 if (locale_ != locale) { | 132 ui::TextInputClient* client = GetTextInputClient(); |
165 locale_ = locale; | 133 if (result_text_.length()) |
166 OnInputMethodChanged(); | 134 client->InsertText(result_text_); |
167 } | 135 |
| 136 if (composition_.text.length()) |
| 137 client->SetCompositionText(composition_); |
| 138 else |
| 139 client->ClearCompositionText(); |
168 } | 140 } |
169 | 141 |
170 void MockInputMethod::SetActive(bool active) { | 142 bool MockInputMethod::HasMockComposition() { |
171 if (active_ != active) { | 143 return composition_.text.length() || result_text_.length(); |
172 active_ = active; | 144 } |
173 OnInputMethodChanged(); | 145 |
174 } | 146 void MockInputMethod::ClearMockComposition() { |
| 147 composition_.Clear(); |
| 148 result_text_.clear(); |
175 } | 149 } |
176 | 150 |
177 void MockInputMethod::ClearStates() { | 151 void MockInputMethod::ClearStates() { |
178 focus_changed_ = false; | |
179 untranslated_ime_message_called_ = false; | 152 untranslated_ime_message_called_ = false; |
180 text_input_type_changed_ = false; | 153 text_input_type_changed_ = false; |
181 caret_bounds_changed_ = false; | |
182 cancel_composition_called_ = false; | 154 cancel_composition_called_ = false; |
183 input_locale_changed_ = false; | |
184 } | |
185 | |
186 void MockInputMethod::ClearResult() { | |
187 composition_.Clear(); | |
188 composition_changed_ = false; | |
189 result_text_.clear(); | |
190 } | 155 } |
191 | 156 |
192 } // namespace views | 157 } // namespace views |
OLD | NEW |