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::OnFocus() {
36 system_toplevel_window_focused_
= true;
39 void InputMethodBase::OnBlur() {
40 system_toplevel_window_focused_
= false;
43 void InputMethodBase::SetFocusedTextInputClient(TextInputClient
* client
) {
44 SetFocusedTextInputClientInternal(client
);
47 void InputMethodBase::DetachTextInputClient(TextInputClient
* client
) {
48 if (text_input_client_
!= client
)
50 SetFocusedTextInputClientInternal(NULL
);
53 TextInputClient
* InputMethodBase::GetTextInputClient() const {
54 if (switches::IsTextInputFocusManagerEnabled())
55 return TextInputFocusManager::GetInstance()->GetFocusedTextInputClient();
57 return system_toplevel_window_focused_
? text_input_client_
: NULL
;
60 void InputMethodBase::OnTextInputTypeChanged(const TextInputClient
* client
) {
61 if (!IsTextInputClientFocused(client
))
63 NotifyTextInputStateChanged(client
);
66 TextInputType
InputMethodBase::GetTextInputType() const {
67 TextInputClient
* client
= GetTextInputClient();
68 return client
? client
->GetTextInputType() : TEXT_INPUT_TYPE_NONE
;
71 TextInputMode
InputMethodBase::GetTextInputMode() const {
72 TextInputClient
* client
= GetTextInputClient();
73 return client
? client
->GetTextInputMode() : TEXT_INPUT_MODE_DEFAULT
;
76 int InputMethodBase::GetTextInputFlags() const {
77 TextInputClient
* client
= GetTextInputClient();
78 return client
? client
->GetTextInputFlags() : 0;
81 bool InputMethodBase::CanComposeInline() const {
82 TextInputClient
* client
= GetTextInputClient();
83 return client
? client
->CanComposeInline() : true;
86 void InputMethodBase::ShowImeIfNeeded() {
87 FOR_EACH_OBSERVER(InputMethodObserver
, observer_list_
, OnShowImeIfNeeded());
90 void InputMethodBase::AddObserver(InputMethodObserver
* observer
) {
91 observer_list_
.AddObserver(observer
);
94 void InputMethodBase::RemoveObserver(InputMethodObserver
* observer
) {
95 observer_list_
.RemoveObserver(observer
);
98 bool InputMethodBase::IsTextInputClientFocused(const TextInputClient
* client
) {
99 return client
&& (client
== GetTextInputClient());
102 bool InputMethodBase::IsTextInputTypeNone() const {
103 return GetTextInputType() == TEXT_INPUT_TYPE_NONE
;
106 void InputMethodBase::OnInputMethodChanged() const {
107 TextInputClient
* client
= GetTextInputClient();
108 if (!IsTextInputTypeNone())
109 client
->OnInputMethodChanged();
112 bool InputMethodBase::DispatchKeyEventPostIME(
113 const ui::KeyEvent
& event
) const {
117 return delegate_
->DispatchKeyEventPostIME(event
);
120 void InputMethodBase::NotifyTextInputStateChanged(
121 const TextInputClient
* client
) {
122 FOR_EACH_OBSERVER(InputMethodObserver
,
124 OnTextInputStateChanged(client
));
127 void InputMethodBase::NotifyTextInputCaretBoundsChanged(
128 const TextInputClient
* client
) {
130 InputMethodObserver
, observer_list_
, OnCaretBoundsChanged(client
));
133 void InputMethodBase::SetFocusedTextInputClientInternal(
134 TextInputClient
* client
) {
135 if (switches::IsTextInputFocusManagerEnabled())
138 TextInputClient
* old
= text_input_client_
;
141 OnWillChangeFocusedClient(old
, client
);
142 text_input_client_
= client
; // NULL allowed.
143 OnDidChangeFocusedClient(old
, client
);
144 NotifyTextInputStateChanged(text_input_client_
);
147 void InputMethodBase::OnCandidateWindowShown() {
148 base::MessageLoop::current()->PostTask(
150 base::Bind(&InputMethodBase::CandidateWindowShownCallback
, AsWeakPtr()));
153 void InputMethodBase::OnCandidateWindowUpdated() {
154 base::MessageLoop::current()->PostTask(
156 base::Bind(&InputMethodBase::CandidateWindowUpdatedCallback
,
160 void InputMethodBase::OnCandidateWindowHidden() {
161 base::MessageLoop::current()->PostTask(
163 base::Bind(&InputMethodBase::CandidateWindowHiddenCallback
, AsWeakPtr()));
166 void InputMethodBase::CandidateWindowShownCallback() {
167 if (TextInputClient
* text_input_client
= GetTextInputClient())
168 text_input_client
->OnCandidateWindowShown();
171 void InputMethodBase::CandidateWindowUpdatedCallback() {
172 if (TextInputClient
* text_input_client
= GetTextInputClient())
173 text_input_client
->OnCandidateWindowUpdated();
176 void InputMethodBase::CandidateWindowHiddenCallback() {
177 if (TextInputClient
* text_input_client
= GetTextInputClient())
178 text_input_client
->OnCandidateWindowHidden();