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 bool InputMethodBase::CanComposeInline() const {
84 TextInputClient
* client
= GetTextInputClient();
85 return client
? client
->CanComposeInline() : true;
88 void InputMethodBase::ShowImeIfNeeded() {
89 FOR_EACH_OBSERVER(InputMethodObserver
, observer_list_
, OnShowImeIfNeeded());
92 void InputMethodBase::AddObserver(InputMethodObserver
* observer
) {
93 observer_list_
.AddObserver(observer
);
96 void InputMethodBase::RemoveObserver(InputMethodObserver
* observer
) {
97 observer_list_
.RemoveObserver(observer
);
100 bool InputMethodBase::IsTextInputClientFocused(const TextInputClient
* client
) {
101 return client
&& (client
== GetTextInputClient());
104 bool InputMethodBase::IsTextInputTypeNone() const {
105 return GetTextInputType() == TEXT_INPUT_TYPE_NONE
;
108 void InputMethodBase::OnInputMethodChanged() const {
109 TextInputClient
* client
= GetTextInputClient();
110 if (!IsTextInputTypeNone())
111 client
->OnInputMethodChanged();
114 bool InputMethodBase::DispatchKeyEventPostIME(
115 const ui::KeyEvent
& event
) const {
119 return delegate_
->DispatchKeyEventPostIME(event
);
122 void InputMethodBase::NotifyTextInputStateChanged(
123 const TextInputClient
* client
) {
124 FOR_EACH_OBSERVER(InputMethodObserver
,
126 OnTextInputStateChanged(client
));
129 void InputMethodBase::SetFocusedTextInputClientInternal(
130 TextInputClient
* client
) {
131 if (switches::IsTextInputFocusManagerEnabled())
134 TextInputClient
* old
= text_input_client_
;
137 OnWillChangeFocusedClient(old
, client
);
138 text_input_client_
= client
; // NULL allowed.
139 OnDidChangeFocusedClient(old
, client
);
140 NotifyTextInputStateChanged(text_input_client_
);
143 void InputMethodBase::OnCandidateWindowShown() {
144 base::MessageLoop::current()->PostTask(
146 base::Bind(&InputMethodBase::CandidateWindowShownCallback
, AsWeakPtr()));
149 void InputMethodBase::OnCandidateWindowUpdated() {
150 base::MessageLoop::current()->PostTask(
152 base::Bind(&InputMethodBase::CandidateWindowUpdatedCallback
,
156 void InputMethodBase::OnCandidateWindowHidden() {
157 base::MessageLoop::current()->PostTask(
159 base::Bind(&InputMethodBase::CandidateWindowHiddenCallback
, AsWeakPtr()));
162 void InputMethodBase::CandidateWindowShownCallback() {
163 if (TextInputClient
* text_input_client
= GetTextInputClient())
164 text_input_client
->OnCandidateWindowShown();
167 void InputMethodBase::CandidateWindowUpdatedCallback() {
168 if (TextInputClient
* text_input_client
= GetTextInputClient())
169 text_input_client
->OnCandidateWindowUpdated();
172 void InputMethodBase::CandidateWindowHiddenCallback() {
173 if (TextInputClient
* text_input_client
= GetTextInputClient())
174 text_input_client
->OnCandidateWindowHidden();