OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/views/ime/input_method_bridge.h" | |
6 | |
7 #include "ui/base/ime/input_method.h" | |
8 #include "ui/base/ime/input_method_observer.h" | |
9 #include "ui/events/event.h" | |
10 #include "ui/gfx/geometry/rect.h" | |
11 #include "ui/views/view.h" | |
12 #include "ui/views/widget/widget.h" | |
13 | |
14 namespace views { | |
15 | |
16 // InputMethodBridge::HostObserver class --------------------------------------- | |
17 | |
18 // An observer class for observing the host input method. When the host input | |
19 // method is destroyed, it will null out the |host_| field on the | |
20 // InputMethodBridge object. | |
21 class InputMethodBridge::HostObserver : public ui::InputMethodObserver { | |
22 public: | |
23 explicit HostObserver(InputMethodBridge* bridge); | |
24 ~HostObserver() override; | |
25 | |
26 void OnTextInputTypeChanged(const ui::TextInputClient* client) override {} | |
27 void OnFocus() override {} | |
28 void OnBlur() override {} | |
29 void OnCaretBoundsChanged(const ui::TextInputClient* client) override {} | |
30 void OnTextInputStateChanged(const ui::TextInputClient* client) override {} | |
31 void OnInputMethodDestroyed(const ui::InputMethod* input_method) override; | |
32 void OnShowImeIfNeeded() override {} | |
33 | |
34 private: | |
35 InputMethodBridge* bridge_; | |
36 | |
37 DISALLOW_COPY_AND_ASSIGN(HostObserver); | |
38 }; | |
39 | |
40 InputMethodBridge::HostObserver::HostObserver(InputMethodBridge* bridge) | |
41 : bridge_(bridge) { | |
42 bridge_->host_->AddObserver(this); | |
43 } | |
44 | |
45 InputMethodBridge::HostObserver::~HostObserver() { | |
46 if (bridge_->host_) | |
47 bridge_->host_->RemoveObserver(this); | |
48 } | |
49 | |
50 void InputMethodBridge::HostObserver::OnInputMethodDestroyed( | |
51 const ui::InputMethod* input_method) { | |
52 DCHECK_EQ(bridge_->host_, input_method); | |
53 bridge_->host_->RemoveObserver(this); | |
54 bridge_->host_ = NULL; | |
55 } | |
56 | |
57 // InputMethodBridge class ----------------------------------------------------- | |
58 | |
59 InputMethodBridge::InputMethodBridge(internal::InputMethodDelegate* delegate, | |
60 ui::InputMethod* host, | |
61 bool shared_input_method) | |
62 : host_(host), | |
63 shared_input_method_(shared_input_method) { | |
64 DCHECK(host_); | |
65 SetDelegate(delegate); | |
66 | |
67 host_observer_.reset(new HostObserver(this)); | |
68 } | |
69 | |
70 InputMethodBridge::~InputMethodBridge() { | |
71 // By the time we get here it's very likely |widget_|'s NativeWidget has been | |
72 // destroyed. This means any calls to |widget_| that go to the NativeWidget, | |
73 // such as IsActive(), will crash. SetFocusedTextInputClient() may callback to | |
74 // this and go into |widget_|. NULL out |widget_| so we don't attempt to use | |
75 // it. | |
76 DetachFromWidget(); | |
77 | |
78 // Host input method might have been destroyed at this point. | |
79 if (host_) | |
80 host_->DetachTextInputClient(this); | |
81 } | |
82 | |
83 void InputMethodBridge::OnFocus() { | |
84 if (!host_) // |host_| could be NULL after OnInputMethodDestroyed. | |
85 return; | |
86 | |
87 // Direct the shared IME to send TextInputClient messages to |this| object. | |
88 if (shared_input_method_ || !host_->GetTextInputClient()) | |
89 host_->SetFocusedTextInputClient(this); | |
90 | |
91 // TODO(yusukes): We don't need to call OnTextInputTypeChanged() once we move | |
92 // text input type tracker code to ui::InputMethodBase. | |
93 if (GetFocusedView()) { | |
94 OnTextInputTypeChanged(GetFocusedView()); | |
95 OnCaretBoundsChanged(GetFocusedView()); | |
96 } | |
97 } | |
98 | |
99 void InputMethodBridge::OnBlur() { | |
100 if (!host_) // |host_| could be NULL after OnInputMethodDestroyed. | |
101 return; | |
102 | |
103 if (HasCompositionText()) { | |
104 ConfirmCompositionText(); | |
105 host_->CancelComposition(this); | |
106 } | |
107 | |
108 if (host_->GetTextInputClient() == this) | |
109 host_->SetFocusedTextInputClient(NULL); | |
110 } | |
111 | |
112 bool InputMethodBridge::OnUntranslatedIMEMessage(const base::NativeEvent& event, | |
113 NativeEventResult* result) { | |
114 DCHECK(host_); | |
115 | |
116 return host_->OnUntranslatedIMEMessage(event, result); | |
117 } | |
118 | |
119 void InputMethodBridge::DispatchKeyEvent(const ui::KeyEvent& key) { | |
120 DCHECK(key.type() == ui::ET_KEY_PRESSED || key.type() == ui::ET_KEY_RELEASED); | |
121 | |
122 // We can just dispatch the event here since the |key| is already processed by | |
123 // the system-wide IME. | |
124 DispatchKeyEventPostIME(key); | |
125 } | |
126 | |
127 void InputMethodBridge::OnTextInputTypeChanged(View* view) { | |
128 DCHECK(host_); | |
129 | |
130 if (IsViewFocused(view)) | |
131 host_->OnTextInputTypeChanged(this); | |
132 InputMethodBase::OnTextInputTypeChanged(view); | |
133 } | |
134 | |
135 void InputMethodBridge::OnCaretBoundsChanged(View* view) { | |
136 DCHECK(host_); | |
137 | |
138 if (IsViewFocused(view) && !IsTextInputTypeNone()) | |
139 host_->OnCaretBoundsChanged(this); | |
140 } | |
141 | |
142 void InputMethodBridge::CancelComposition(View* view) { | |
143 DCHECK(host_); | |
144 | |
145 if (IsViewFocused(view)) | |
146 host_->CancelComposition(this); | |
147 } | |
148 | |
149 void InputMethodBridge::OnInputLocaleChanged() { | |
150 DCHECK(host_); | |
151 | |
152 host_->OnInputLocaleChanged(); | |
153 } | |
154 | |
155 std::string InputMethodBridge::GetInputLocale() { | |
156 DCHECK(host_); | |
157 | |
158 return host_->GetInputLocale(); | |
159 } | |
160 | |
161 bool InputMethodBridge::IsActive() { | |
162 DCHECK(host_); | |
163 | |
164 return host_->IsActive(); | |
165 } | |
166 | |
167 bool InputMethodBridge::IsCandidatePopupOpen() const { | |
168 DCHECK(host_); | |
169 | |
170 return host_->IsCandidatePopupOpen(); | |
171 } | |
172 | |
173 void InputMethodBridge::ShowImeIfNeeded() { | |
174 DCHECK(host_); | |
175 host_->ShowImeIfNeeded(); | |
176 } | |
177 | |
178 // Overridden from TextInputClient. Forward an event from the system-wide IME | |
179 // to the text input |client|, which is e.g. views::Textfield. | |
180 void InputMethodBridge::SetCompositionText( | |
181 const ui::CompositionText& composition) { | |
182 TextInputClient* client = GetTextInputClient(); | |
183 if (client) | |
184 client->SetCompositionText(composition); | |
185 } | |
186 | |
187 void InputMethodBridge::ConfirmCompositionText() { | |
188 TextInputClient* client = GetTextInputClient(); | |
189 if (client) | |
190 client->ConfirmCompositionText(); | |
191 } | |
192 | |
193 void InputMethodBridge::ClearCompositionText() { | |
194 TextInputClient* client = GetTextInputClient(); | |
195 if (client) | |
196 client->ClearCompositionText(); | |
197 } | |
198 | |
199 void InputMethodBridge::InsertText(const base::string16& text) { | |
200 TextInputClient* client = GetTextInputClient(); | |
201 if (client) | |
202 client->InsertText(text); | |
203 } | |
204 | |
205 void InputMethodBridge::InsertChar(base::char16 ch, int flags) { | |
206 TextInputClient* client = GetTextInputClient(); | |
207 if (client) | |
208 client->InsertChar(ch, flags); | |
209 } | |
210 | |
211 gfx::NativeWindow InputMethodBridge::GetAttachedWindow() const { | |
212 TextInputClient* client = GetTextInputClient(); | |
213 return client ? | |
214 client->GetAttachedWindow() : static_cast<gfx::NativeWindow>(NULL); | |
215 } | |
216 | |
217 ui::TextInputType InputMethodBridge::GetTextInputType() const { | |
218 TextInputClient* client = GetTextInputClient(); | |
219 return client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; | |
220 } | |
221 | |
222 ui::TextInputMode InputMethodBridge::GetTextInputMode() const { | |
223 TextInputClient* client = GetTextInputClient(); | |
224 return client ? client->GetTextInputMode() : ui::TEXT_INPUT_MODE_DEFAULT; | |
225 } | |
226 | |
227 int InputMethodBridge::GetTextInputFlags() const { | |
228 TextInputClient* client = GetTextInputClient(); | |
229 return client ? client->GetTextInputFlags() : 0; | |
230 } | |
231 | |
232 bool InputMethodBridge::CanComposeInline() const { | |
233 TextInputClient* client = GetTextInputClient(); | |
234 return client ? client->CanComposeInline() : true; | |
235 } | |
236 | |
237 gfx::Rect InputMethodBridge::GetCaretBounds() const { | |
238 TextInputClient* client = GetTextInputClient(); | |
239 if (!client) | |
240 return gfx::Rect(); | |
241 | |
242 return client->GetCaretBounds(); | |
243 } | |
244 | |
245 bool InputMethodBridge::GetCompositionCharacterBounds(uint32 index, | |
246 gfx::Rect* rect) const { | |
247 DCHECK(rect); | |
248 TextInputClient* client = GetTextInputClient(); | |
249 if (!client) | |
250 return false; | |
251 | |
252 return client->GetCompositionCharacterBounds(index, rect); | |
253 } | |
254 | |
255 bool InputMethodBridge::HasCompositionText() const { | |
256 TextInputClient* client = GetTextInputClient(); | |
257 return client ? client->HasCompositionText() : false; | |
258 } | |
259 | |
260 bool InputMethodBridge::GetTextRange(gfx::Range* range) const { | |
261 TextInputClient* client = GetTextInputClient(); | |
262 return client ? client->GetTextRange(range) : false; | |
263 } | |
264 | |
265 bool InputMethodBridge::GetCompositionTextRange(gfx::Range* range) const { | |
266 TextInputClient* client = GetTextInputClient(); | |
267 return client ? client->GetCompositionTextRange(range) : false; | |
268 } | |
269 | |
270 bool InputMethodBridge::GetSelectionRange(gfx::Range* range) const { | |
271 TextInputClient* client = GetTextInputClient(); | |
272 return client ? client->GetSelectionRange(range) : false; | |
273 } | |
274 | |
275 bool InputMethodBridge::SetSelectionRange(const gfx::Range& range) { | |
276 TextInputClient* client = GetTextInputClient(); | |
277 return client ? client->SetSelectionRange(range) : false; | |
278 } | |
279 | |
280 bool InputMethodBridge::DeleteRange(const gfx::Range& range) { | |
281 TextInputClient* client = GetTextInputClient(); | |
282 return client ? client->DeleteRange(range) : false; | |
283 } | |
284 | |
285 bool InputMethodBridge::GetTextFromRange(const gfx::Range& range, | |
286 base::string16* text) const { | |
287 TextInputClient* client = GetTextInputClient(); | |
288 return client ? client->GetTextFromRange(range, text) : false; | |
289 } | |
290 | |
291 void InputMethodBridge::OnInputMethodChanged() { | |
292 TextInputClient* client = GetTextInputClient(); | |
293 if (client) | |
294 client->OnInputMethodChanged(); | |
295 } | |
296 | |
297 bool InputMethodBridge::ChangeTextDirectionAndLayoutAlignment( | |
298 base::i18n::TextDirection direction) { | |
299 TextInputClient* client = GetTextInputClient(); | |
300 return client ? | |
301 client->ChangeTextDirectionAndLayoutAlignment(direction) : false; | |
302 } | |
303 | |
304 void InputMethodBridge::ExtendSelectionAndDelete(size_t before, size_t after) { | |
305 TextInputClient* client = GetTextInputClient(); | |
306 if (client) | |
307 client->ExtendSelectionAndDelete(before, after); | |
308 } | |
309 | |
310 void InputMethodBridge::EnsureCaretInRect(const gfx::Rect& rect) { | |
311 TextInputClient* client = GetTextInputClient(); | |
312 if (client) | |
313 client->EnsureCaretInRect(rect); | |
314 } | |
315 | |
316 bool InputMethodBridge::IsEditCommandEnabled(int command_id) { | |
317 TextInputClient* client = GetTextInputClient(); | |
318 return client ? client->IsEditCommandEnabled(command_id) : false; | |
319 } | |
320 | |
321 void InputMethodBridge::SetEditCommandForNextKeyEvent(int command_id) { | |
322 TextInputClient* client = GetTextInputClient(); | |
323 if (client) | |
324 client->SetEditCommandForNextKeyEvent(command_id); | |
325 } | |
326 | |
327 // Overridden from FocusChangeListener. | |
328 void InputMethodBridge::OnWillChangeFocus(View* focused_before, View* focused) { | |
329 if (HasCompositionText()) { | |
330 ConfirmCompositionText(); | |
331 CancelComposition(focused_before); | |
332 } | |
333 } | |
334 | |
335 void InputMethodBridge::OnDidChangeFocus(View* focused_before, View* focused) { | |
336 DCHECK_EQ(GetFocusedView(), focused); | |
337 OnTextInputTypeChanged(focused); | |
338 OnCaretBoundsChanged(focused); | |
339 } | |
340 | |
341 ui::InputMethod* InputMethodBridge::GetHostInputMethod() const { | |
342 return host_; | |
343 } | |
344 | |
345 | |
346 } // namespace views | |
OLD | NEW |