OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/base/ime/input_method_observer.h" |
8 #include "ui/events/event.h" | 9 #include "ui/events/event.h" |
9 #include "ui/gfx/rect.h" | 10 #include "ui/gfx/rect.h" |
10 #include "ui/views/view.h" | 11 #include "ui/views/view.h" |
11 #include "ui/views/widget/widget.h" | 12 #include "ui/views/widget/widget.h" |
12 | 13 |
13 namespace views { | 14 namespace views { |
14 | 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 virtual ~HostObserver(); |
| 25 |
| 26 virtual void OnTextInputTypeChanged( |
| 27 const ui::TextInputClient* client) OVERRIDE {} |
| 28 virtual void OnFocus() OVERRIDE {} |
| 29 virtual void OnBlur() OVERRIDE {} |
| 30 virtual void OnUntranslatedIMEMessage( |
| 31 const base::NativeEvent& event) OVERRIDE {} |
| 32 virtual void OnCaretBoundsChanged( |
| 33 const ui::TextInputClient* client) OVERRIDE {} |
| 34 virtual void OnInputLocaleChanged() OVERRIDE {} |
| 35 virtual void OnTextInputStateChanged( |
| 36 const ui::TextInputClient* client) OVERRIDE {} |
| 37 virtual void OnInputMethodDestroyed( |
| 38 const ui::InputMethod* input_method) OVERRIDE; |
| 39 |
| 40 private: |
| 41 InputMethodBridge* bridge_; |
| 42 |
| 43 DISALLOW_COPY_AND_ASSIGN(HostObserver); |
| 44 }; |
| 45 |
| 46 InputMethodBridge::HostObserver::HostObserver(InputMethodBridge* bridge) |
| 47 : bridge_(bridge) { |
| 48 bridge_->host_->AddObserver(this); |
| 49 } |
| 50 |
| 51 InputMethodBridge::HostObserver::~HostObserver() { |
| 52 if (bridge_->host_) |
| 53 bridge_->host_->RemoveObserver(this); |
| 54 } |
| 55 |
| 56 void InputMethodBridge::HostObserver::OnInputMethodDestroyed( |
| 57 const ui::InputMethod* input_method) { |
| 58 DCHECK_EQ(bridge_->host_, input_method); |
| 59 bridge_->host_->RemoveObserver(this); |
| 60 bridge_->host_ = NULL; |
| 61 } |
| 62 |
| 63 // InputMethodBridge class ----------------------------------------------------- |
| 64 |
15 InputMethodBridge::InputMethodBridge(internal::InputMethodDelegate* delegate, | 65 InputMethodBridge::InputMethodBridge(internal::InputMethodDelegate* delegate, |
16 ui::InputMethod* host, | 66 ui::InputMethod* host, |
17 bool shared_input_method) | 67 bool shared_input_method) |
18 : host_(host), | 68 : host_(host), |
19 shared_input_method_(shared_input_method) { | 69 shared_input_method_(shared_input_method) { |
20 DCHECK(host_); | 70 DCHECK(host_); |
21 SetDelegate(delegate); | 71 SetDelegate(delegate); |
| 72 |
| 73 host_observer_.reset(new HostObserver(this)); |
22 } | 74 } |
23 | 75 |
24 InputMethodBridge::~InputMethodBridge() { | 76 InputMethodBridge::~InputMethodBridge() { |
25 // By the time we get here it's very likely |widget_|'s NativeWidget has been | 77 // By the time we get here it's very likely |widget_|'s NativeWidget has been |
26 // destroyed. This means any calls to |widget_| that go to the NativeWidget, | 78 // destroyed. This means any calls to |widget_| that go to the NativeWidget, |
27 // such as IsActive(), will crash. SetFocusedTextInputClient() may callback to | 79 // such as IsActive(), will crash. SetFocusedTextInputClient() may callback to |
28 // this and go into |widget_|. NULL out |widget_| so we don't attempt to use | 80 // this and go into |widget_|. NULL out |widget_| so we don't attempt to use |
29 // it. | 81 // it. |
30 DetachFromWidget(); | 82 DetachFromWidget(); |
31 host_->DetachTextInputClient(this); | 83 |
| 84 // Host input method might have been destroyed at this point. |
| 85 if (host_) |
| 86 host_->DetachTextInputClient(this); |
32 } | 87 } |
33 | 88 |
34 void InputMethodBridge::OnFocus() { | 89 void InputMethodBridge::OnFocus() { |
| 90 DCHECK(host_); |
| 91 |
35 // Direct the shared IME to send TextInputClient messages to |this| object. | 92 // Direct the shared IME to send TextInputClient messages to |this| object. |
36 if (shared_input_method_ || !host_->GetTextInputClient()) | 93 if (shared_input_method_ || !host_->GetTextInputClient()) |
37 host_->SetFocusedTextInputClient(this); | 94 host_->SetFocusedTextInputClient(this); |
38 | 95 |
39 // TODO(yusukes): We don't need to call OnTextInputTypeChanged() once we move | 96 // TODO(yusukes): We don't need to call OnTextInputTypeChanged() once we move |
40 // text input type tracker code to ui::InputMethodBase. | 97 // text input type tracker code to ui::InputMethodBase. |
41 if (GetFocusedView()) | 98 if (GetFocusedView()) |
42 OnTextInputTypeChanged(GetFocusedView()); | 99 OnTextInputTypeChanged(GetFocusedView()); |
43 } | 100 } |
44 | 101 |
45 void InputMethodBridge::OnBlur() { | 102 void InputMethodBridge::OnBlur() { |
| 103 DCHECK(host_); |
| 104 |
46 if (HasCompositionText()) { | 105 if (HasCompositionText()) { |
47 ConfirmCompositionText(); | 106 ConfirmCompositionText(); |
48 host_->CancelComposition(this); | 107 host_->CancelComposition(this); |
49 } | 108 } |
50 | 109 |
51 if (host_->GetTextInputClient() == this) | 110 if (host_->GetTextInputClient() == this) |
52 host_->SetFocusedTextInputClient(NULL); | 111 host_->SetFocusedTextInputClient(NULL); |
53 } | 112 } |
54 | 113 |
55 bool InputMethodBridge::OnUntranslatedIMEMessage(const base::NativeEvent& event, | 114 bool InputMethodBridge::OnUntranslatedIMEMessage(const base::NativeEvent& event, |
56 NativeEventResult* result) { | 115 NativeEventResult* result) { |
| 116 DCHECK(host_); |
| 117 |
57 return host_->OnUntranslatedIMEMessage(event, result); | 118 return host_->OnUntranslatedIMEMessage(event, result); |
58 } | 119 } |
59 | 120 |
60 void InputMethodBridge::DispatchKeyEvent(const ui::KeyEvent& key) { | 121 void InputMethodBridge::DispatchKeyEvent(const ui::KeyEvent& key) { |
61 DCHECK(key.type() == ui::ET_KEY_PRESSED || key.type() == ui::ET_KEY_RELEASED); | 122 DCHECK(key.type() == ui::ET_KEY_PRESSED || key.type() == ui::ET_KEY_RELEASED); |
62 | 123 |
63 // We can just dispatch the event here since the |key| is already processed by | 124 // We can just dispatch the event here since the |key| is already processed by |
64 // the system-wide IME. | 125 // the system-wide IME. |
65 DispatchKeyEventPostIME(key); | 126 DispatchKeyEventPostIME(key); |
66 } | 127 } |
67 | 128 |
68 void InputMethodBridge::OnTextInputTypeChanged(View* view) { | 129 void InputMethodBridge::OnTextInputTypeChanged(View* view) { |
| 130 DCHECK(host_); |
| 131 |
69 if (IsViewFocused(view)) | 132 if (IsViewFocused(view)) |
70 host_->OnTextInputTypeChanged(this); | 133 host_->OnTextInputTypeChanged(this); |
71 InputMethodBase::OnTextInputTypeChanged(view); | 134 InputMethodBase::OnTextInputTypeChanged(view); |
72 } | 135 } |
73 | 136 |
74 void InputMethodBridge::OnCaretBoundsChanged(View* view) { | 137 void InputMethodBridge::OnCaretBoundsChanged(View* view) { |
| 138 DCHECK(host_); |
| 139 |
75 if (IsViewFocused(view) && !IsTextInputTypeNone()) | 140 if (IsViewFocused(view) && !IsTextInputTypeNone()) |
76 host_->OnCaretBoundsChanged(this); | 141 host_->OnCaretBoundsChanged(this); |
77 } | 142 } |
78 | 143 |
79 void InputMethodBridge::CancelComposition(View* view) { | 144 void InputMethodBridge::CancelComposition(View* view) { |
| 145 DCHECK(host_); |
| 146 |
80 if (IsViewFocused(view)) | 147 if (IsViewFocused(view)) |
81 host_->CancelComposition(this); | 148 host_->CancelComposition(this); |
82 } | 149 } |
83 | 150 |
84 void InputMethodBridge::OnInputLocaleChanged() { | 151 void InputMethodBridge::OnInputLocaleChanged() { |
85 return host_->OnInputLocaleChanged(); | 152 DCHECK(host_); |
| 153 |
| 154 host_->OnInputLocaleChanged(); |
86 } | 155 } |
87 | 156 |
88 std::string InputMethodBridge::GetInputLocale() { | 157 std::string InputMethodBridge::GetInputLocale() { |
| 158 DCHECK(host_); |
| 159 |
89 return host_->GetInputLocale(); | 160 return host_->GetInputLocale(); |
90 } | 161 } |
91 | 162 |
92 base::i18n::TextDirection InputMethodBridge::GetInputTextDirection() { | 163 base::i18n::TextDirection InputMethodBridge::GetInputTextDirection() { |
| 164 DCHECK(host_); |
| 165 |
93 return host_->GetInputTextDirection(); | 166 return host_->GetInputTextDirection(); |
94 } | 167 } |
95 | 168 |
96 bool InputMethodBridge::IsActive() { | 169 bool InputMethodBridge::IsActive() { |
| 170 DCHECK(host_); |
| 171 |
97 return host_->IsActive(); | 172 return host_->IsActive(); |
98 } | 173 } |
99 | 174 |
100 bool InputMethodBridge::IsCandidatePopupOpen() const { | 175 bool InputMethodBridge::IsCandidatePopupOpen() const { |
| 176 DCHECK(host_); |
| 177 |
101 return host_->IsCandidatePopupOpen(); | 178 return host_->IsCandidatePopupOpen(); |
102 } | 179 } |
103 | 180 |
104 // Overridden from TextInputClient. Forward an event from the system-wide IME | 181 // Overridden from TextInputClient. Forward an event from the system-wide IME |
105 // to the text input |client|, which is e.g. views::NativeTextfieldViews. | 182 // to the text input |client|, which is e.g. views::NativeTextfieldViews. |
106 void InputMethodBridge::SetCompositionText( | 183 void InputMethodBridge::SetCompositionText( |
107 const ui::CompositionText& composition) { | 184 const ui::CompositionText& composition) { |
108 TextInputClient* client = GetTextInputClient(); | 185 TextInputClient* client = GetTextInputClient(); |
109 if (client) | 186 if (client) |
110 client->SetCompositionText(composition); | 187 client->SetCompositionText(composition); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 OnTextInputTypeChanged(focused); | 324 OnTextInputTypeChanged(focused); |
248 OnCaretBoundsChanged(focused); | 325 OnCaretBoundsChanged(focused); |
249 } | 326 } |
250 | 327 |
251 ui::InputMethod* InputMethodBridge::GetHostInputMethod() const { | 328 ui::InputMethod* InputMethodBridge::GetHostInputMethod() const { |
252 return host_; | 329 return host_; |
253 } | 330 } |
254 | 331 |
255 | 332 |
256 } // namespace views | 333 } // namespace views |
OLD | NEW |