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.
5 #include "ui/base/ime/input_method_base.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "ui/base/ime/input_method_delegate.h"
11 #include "ui/base/ime/input_method_observer.h"
12 #include "ui/base/ime/text_input_client.h"
13 #include "ui/base/ime/text_input_focus_manager.h"
14 #include "ui/base/ui_base_switches_util.h"
15 #include "ui/events/event.h"
19 InputMethodBase::InputMethodBase()
21 text_input_client_(NULL
),
22 system_toplevel_window_focused_(false) {
25 InputMethodBase::~InputMethodBase() {
26 FOR_EACH_OBSERVER(InputMethodObserver
,
28 OnInputMethodDestroyed(this));
31 void InputMethodBase::SetDelegate(internal::InputMethodDelegate
* delegate
) {
35 void InputMethodBase::Init(bool focused
) {
40 void InputMethodBase::OnFocus() {
41 DCHECK(!system_toplevel_window_focused_
);
42 system_toplevel_window_focused_
= true;
45 void InputMethodBase::OnBlur() {
46 DCHECK(system_toplevel_window_focused_
);
47 system_toplevel_window_focused_
= false;
50 void InputMethodBase::SetFocusedTextInputClient(TextInputClient
* client
) {
51 SetFocusedTextInputClientInternal(client
);
54 void InputMethodBase::DetachTextInputClient(TextInputClient
* client
) {
55 if (text_input_client_
!= client
)
57 SetFocusedTextInputClientInternal(NULL
);
60 TextInputClient
* InputMethodBase::GetTextInputClient() const {
61 if (switches::IsTextInputFocusManagerEnabled())
62 return TextInputFocusManager::GetInstance()->GetFocusedTextInputClient();
64 return system_toplevel_window_focused_
? text_input_client_
: NULL
;
67 void InputMethodBase::OnTextInputTypeChanged(const TextInputClient
* client
) {
68 if (!IsTextInputClientFocused(client
))
70 NotifyTextInputStateChanged(client
);
73 TextInputType
InputMethodBase::GetTextInputType() const {
74 TextInputClient
* client
= GetTextInputClient();
75 return client
? client
->GetTextInputType() : TEXT_INPUT_TYPE_NONE
;
78 TextInputMode
InputMethodBase::GetTextInputMode() const {
79 TextInputClient
* client
= GetTextInputClient();
80 return client
? client
->GetTextInputMode() : TEXT_INPUT_MODE_DEFAULT
;
83 int InputMethodBase::GetTextInputFlags() const {
84 TextInputClient
* client
= GetTextInputClient();
85 return client
? client
->GetTextInputFlags() : 0;
88 bool InputMethodBase::CanComposeInline() const {
89 TextInputClient
* client
= GetTextInputClient();
90 return client
? client
->CanComposeInline() : true;
93 void InputMethodBase::ShowImeIfNeeded() {
94 FOR_EACH_OBSERVER(InputMethodObserver
, observer_list_
, OnShowImeIfNeeded());
97 void InputMethodBase::AddObserver(InputMethodObserver
* observer
) {
98 observer_list_
.AddObserver(observer
);
101 void InputMethodBase::RemoveObserver(InputMethodObserver
* observer
) {
102 observer_list_
.RemoveObserver(observer
);
105 bool InputMethodBase::IsTextInputClientFocused(const TextInputClient
* client
) {
106 return client
&& (client
== GetTextInputClient());
109 bool InputMethodBase::IsTextInputTypeNone() const {
110 return GetTextInputType() == TEXT_INPUT_TYPE_NONE
;
113 void InputMethodBase::OnInputMethodChanged() const {
114 TextInputClient
* client
= GetTextInputClient();
115 if (!IsTextInputTypeNone())
116 client
->OnInputMethodChanged();
119 bool InputMethodBase::DispatchKeyEventPostIME(
120 const ui::KeyEvent
& event
) const {
124 return delegate_
->DispatchKeyEventPostIME(event
);
127 void InputMethodBase::NotifyTextInputStateChanged(
128 const TextInputClient
* client
) {
129 FOR_EACH_OBSERVER(InputMethodObserver
,
131 OnTextInputStateChanged(client
));
134 void InputMethodBase::NotifyTextInputCaretBoundsChanged(
135 const TextInputClient
* client
) {
137 InputMethodObserver
, observer_list_
, OnCaretBoundsChanged(client
));
140 void InputMethodBase::SetFocusedTextInputClientInternal(
141 TextInputClient
* client
) {
142 if (switches::IsTextInputFocusManagerEnabled())
145 TextInputClient
* old
= text_input_client_
;
148 OnWillChangeFocusedClient(old
, client
);
149 text_input_client_
= client
; // NULL allowed.
150 OnDidChangeFocusedClient(old
, client
);
151 NotifyTextInputStateChanged(text_input_client_
);
154 void InputMethodBase::OnCandidateWindowShown() {
155 base::MessageLoop::current()->PostTask(
157 base::Bind(&InputMethodBase::CandidateWindowShownCallback
, AsWeakPtr()));
160 void InputMethodBase::OnCandidateWindowUpdated() {
161 base::MessageLoop::current()->PostTask(
163 base::Bind(&InputMethodBase::CandidateWindowUpdatedCallback
,
167 void InputMethodBase::OnCandidateWindowHidden() {
168 base::MessageLoop::current()->PostTask(
170 base::Bind(&InputMethodBase::CandidateWindowHiddenCallback
, AsWeakPtr()));
173 void InputMethodBase::CandidateWindowShownCallback() {
174 if (TextInputClient
* text_input_client
= GetTextInputClient())
175 text_input_client
->OnCandidateWindowShown();
178 void InputMethodBase::CandidateWindowUpdatedCallback() {
179 if (TextInputClient
* text_input_client
= GetTextInputClient())
180 text_input_client
->OnCandidateWindowUpdated();
183 void InputMethodBase::CandidateWindowHiddenCallback() {
184 if (TextInputClient
* text_input_client
= GetTextInputClient())
185 text_input_client
->OnCandidateWindowHidden();