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/input_method_bridge.h" | 5 #include "ui/views/ime/input_method_bridge.h" |
6 | 6 |
7 #include "ui/base/ime/input_method.h" | 7 #include "ui/base/ime/input_method.h" |
8 #include "ui/gfx/rect.h" | 8 #include "ui/gfx/rect.h" |
9 #include "ui/views/view.h" | 9 #include "ui/views/view.h" |
10 #include "ui/views/widget/widget.h" | 10 #include "ui/views/widget/widget.h" |
11 | 11 |
12 namespace views { | 12 namespace views { |
13 | 13 |
14 InputMethodBridge::InputMethodBridge(internal::InputMethodDelegate* delegate, | 14 InputMethodBridge::InputMethodBridge(internal::InputMethodDelegate* delegate, |
15 ui::InputMethod* host) | 15 ui::InputMethod* host) |
16 : host_(host), | 16 : host_(host), |
17 context_focused_(false) { | 17 context_focused_(false) { |
18 DCHECK(host_); | |
19 set_delegate(delegate); | 18 set_delegate(delegate); |
20 } | 19 } |
21 | 20 |
22 InputMethodBridge::~InputMethodBridge() { | 21 InputMethodBridge::~InputMethodBridge() { |
23 if (host_->GetTextInputClient() == this) | 22 if (host_ && host_->GetTextInputClient() == this) |
24 host_->SetFocusedTextInputClient(NULL); | 23 host_->SetFocusedTextInputClient(NULL); |
25 } | 24 } |
26 | 25 |
27 void InputMethodBridge::Init(Widget* widget) { | 26 void InputMethodBridge::Init(Widget* widget) { |
28 InputMethodBase::Init(widget); | 27 InputMethodBase::Init(widget); |
29 } | 28 } |
30 | 29 |
31 void InputMethodBridge::OnFocus() { | 30 void InputMethodBridge::OnFocus() { |
32 DCHECK(!widget_focused()); | 31 DCHECK(!widget_focused()); |
33 InputMethodBase::OnFocus(); | 32 InputMethodBase::OnFocus(); |
34 | 33 |
35 // Ask the system-wide IME to send all TextInputClient messages to |this| | 34 // Ask the system-wide IME to send all TextInputClient messages to |this| |
36 // object. | 35 // object. |
37 host_->SetFocusedTextInputClient(this); | 36 if (host_) |
| 37 host_->SetFocusedTextInputClient(this); |
38 | 38 |
39 // TODO(yusukes): We don't need to call OnTextInputTypeChanged() once we move | 39 // TODO(yusukes): We don't need to call OnTextInputTypeChanged() once we move |
40 // text input type tracker code to ui::InputMethodBase. | 40 // text input type tracker code to ui::InputMethodBase. |
41 if (GetFocusedView()) | 41 if (GetFocusedView()) |
42 OnTextInputTypeChanged(GetFocusedView()); | 42 OnTextInputTypeChanged(GetFocusedView()); |
43 } | 43 } |
44 | 44 |
45 void InputMethodBridge::OnBlur() { | 45 void InputMethodBridge::OnBlur() { |
46 DCHECK(widget_focused()); | 46 DCHECK(widget_focused()); |
47 | 47 |
48 ConfirmCompositionText(); | 48 ConfirmCompositionText(); |
49 InputMethodBase::OnBlur(); | 49 InputMethodBase::OnBlur(); |
50 if (host_->GetTextInputClient() == this) | 50 if (host_ && host_->GetTextInputClient() == this) |
51 host_->SetFocusedTextInputClient(NULL); | 51 host_->SetFocusedTextInputClient(NULL); |
52 } | 52 } |
53 | 53 |
54 void InputMethodBridge::DispatchKeyEvent(const KeyEvent& key) { | 54 void InputMethodBridge::DispatchKeyEvent(const KeyEvent& key) { |
55 DCHECK(key.type() == ui::ET_KEY_PRESSED || key.type() == ui::ET_KEY_RELEASED); | 55 DCHECK(key.type() == ui::ET_KEY_PRESSED || key.type() == ui::ET_KEY_RELEASED); |
56 DCHECK(widget_focused()); | 56 DCHECK(widget_focused()); |
57 | 57 |
58 // We can just dispatch the event here since the |key| is already processed by | 58 // We can just dispatch the event here since the |key| is already processed by |
59 // the system-wide IME. | 59 // the system-wide IME. |
60 DispatchKeyEventPostIME(key); | 60 DispatchKeyEventPostIME(key); |
61 } | 61 } |
62 | 62 |
63 void InputMethodBridge::OnTextInputTypeChanged(View* view) { | 63 void InputMethodBridge::OnTextInputTypeChanged(View* view) { |
64 if (IsViewFocused(view)) | 64 if (host_ && IsViewFocused(view)) |
65 host_->OnTextInputTypeChanged(this); | 65 host_->OnTextInputTypeChanged(this); |
66 InputMethodBase::OnTextInputTypeChanged(view); | 66 InputMethodBase::OnTextInputTypeChanged(view); |
67 } | 67 } |
68 | 68 |
69 void InputMethodBridge::OnCaretBoundsChanged(View* view) { | 69 void InputMethodBridge::OnCaretBoundsChanged(View* view) { |
70 if (IsViewFocused(view) && !IsTextInputTypeNone()) | 70 if (host_ && IsViewFocused(view) && !IsTextInputTypeNone()) |
71 host_->OnCaretBoundsChanged(this); | 71 host_->OnCaretBoundsChanged(this); |
72 } | 72 } |
73 | 73 |
74 void InputMethodBridge::CancelComposition(View* view) { | 74 void InputMethodBridge::CancelComposition(View* view) { |
75 if (IsViewFocused(view)) | 75 if (host_ && IsViewFocused(view)) |
76 host_->CancelComposition(this); | 76 host_->CancelComposition(this); |
77 } | 77 } |
78 | 78 |
79 std::string InputMethodBridge::GetInputLocale() { | 79 std::string InputMethodBridge::GetInputLocale() { |
80 return host_->GetInputLocale(); | 80 return host_ ? host_->GetInputLocale() : ""; |
81 } | 81 } |
82 | 82 |
83 base::i18n::TextDirection InputMethodBridge::GetInputTextDirection() { | 83 base::i18n::TextDirection InputMethodBridge::GetInputTextDirection() { |
84 return host_->GetInputTextDirection(); | 84 return host_ ? host_->GetInputTextDirection() : base::i18n::UNKNOWN_DIRECTION; |
85 } | 85 } |
86 | 86 |
87 bool InputMethodBridge::IsActive() { | 87 bool InputMethodBridge::IsActive() { |
88 return host_->IsActive(); | 88 return host_ ? host_->IsActive() : false; |
89 } | 89 } |
90 | 90 |
91 // Overridden from TextInputClient. Forward an event from the system-wide IME | 91 // Overridden from TextInputClient. Forward an event from the system-wide IME |
92 // to the text input |client|, which is e.g. views::NativeTextfieldViews. | 92 // to the text input |client|, which is e.g. views::NativeTextfieldViews. |
93 void InputMethodBridge::SetCompositionText( | 93 void InputMethodBridge::SetCompositionText( |
94 const ui::CompositionText& composition) { | 94 const ui::CompositionText& composition) { |
95 TextInputClient* client = GetTextInputClient(); | 95 TextInputClient* client = GetTextInputClient(); |
96 if (client) | 96 if (client) |
97 client->SetCompositionText(composition); | 97 client->SetCompositionText(composition); |
98 } | 98 } |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 void InputMethodBridge::OnWillChangeFocus(View* focused_before, View* focused) { | 195 void InputMethodBridge::OnWillChangeFocus(View* focused_before, View* focused) { |
196 ConfirmCompositionText(); | 196 ConfirmCompositionText(); |
197 } | 197 } |
198 | 198 |
199 void InputMethodBridge::OnDidChangeFocus(View* focused_before, View* focused) { | 199 void InputMethodBridge::OnDidChangeFocus(View* focused_before, View* focused) { |
200 OnTextInputTypeChanged(GetFocusedView()); | 200 OnTextInputTypeChanged(GetFocusedView()); |
201 OnCaretBoundsChanged(GetFocusedView()); | 201 OnCaretBoundsChanged(GetFocusedView()); |
202 } | 202 } |
203 | 203 |
204 } // namespace views | 204 } // namespace views |
OLD | NEW |