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/events/event.h"
17 InputMethodBase::InputMethodBase()
19 text_input_client_(NULL
),
20 system_toplevel_window_focused_(false) {
23 InputMethodBase::~InputMethodBase() {
24 FOR_EACH_OBSERVER(InputMethodObserver
,
26 OnInputMethodDestroyed(this));
29 void InputMethodBase::SetDelegate(internal::InputMethodDelegate
* delegate
) {
33 void InputMethodBase::OnFocus() {
34 system_toplevel_window_focused_
= true;
37 void InputMethodBase::OnBlur() {
38 system_toplevel_window_focused_
= false;
41 void InputMethodBase::SetFocusedTextInputClient(TextInputClient
* client
) {
42 SetFocusedTextInputClientInternal(client
);
45 void InputMethodBase::DetachTextInputClient(TextInputClient
* client
) {
46 if (text_input_client_
!= client
)
48 SetFocusedTextInputClientInternal(NULL
);
51 TextInputClient
* InputMethodBase::GetTextInputClient() const {
52 return system_toplevel_window_focused_
? text_input_client_
: NULL
;
55 void InputMethodBase::OnTextInputTypeChanged(const TextInputClient
* client
) {
56 if (!IsTextInputClientFocused(client
))
58 NotifyTextInputStateChanged(client
);
61 TextInputType
InputMethodBase::GetTextInputType() const {
62 TextInputClient
* client
= GetTextInputClient();
63 return client
? client
->GetTextInputType() : TEXT_INPUT_TYPE_NONE
;
66 TextInputMode
InputMethodBase::GetTextInputMode() const {
67 TextInputClient
* client
= GetTextInputClient();
68 return client
? client
->GetTextInputMode() : TEXT_INPUT_MODE_DEFAULT
;
71 int InputMethodBase::GetTextInputFlags() const {
72 TextInputClient
* client
= GetTextInputClient();
73 return client
? client
->GetTextInputFlags() : 0;
76 bool InputMethodBase::CanComposeInline() const {
77 TextInputClient
* client
= GetTextInputClient();
78 return client
? client
->CanComposeInline() : true;
81 void InputMethodBase::ShowImeIfNeeded() {
82 FOR_EACH_OBSERVER(InputMethodObserver
, observer_list_
, OnShowImeIfNeeded());
85 void InputMethodBase::AddObserver(InputMethodObserver
* observer
) {
86 observer_list_
.AddObserver(observer
);
89 void InputMethodBase::RemoveObserver(InputMethodObserver
* observer
) {
90 observer_list_
.RemoveObserver(observer
);
93 bool InputMethodBase::IsTextInputClientFocused(const TextInputClient
* client
) {
94 return client
&& (client
== GetTextInputClient());
97 bool InputMethodBase::IsTextInputTypeNone() const {
98 return GetTextInputType() == TEXT_INPUT_TYPE_NONE
;
101 void InputMethodBase::OnInputMethodChanged() const {
102 TextInputClient
* client
= GetTextInputClient();
103 if (!IsTextInputTypeNone())
104 client
->OnInputMethodChanged();
107 bool InputMethodBase::DispatchKeyEventPostIME(
108 const ui::KeyEvent
& event
) const {
112 return delegate_
->DispatchKeyEventPostIME(event
);
115 void InputMethodBase::NotifyTextInputStateChanged(
116 const TextInputClient
* client
) {
117 FOR_EACH_OBSERVER(InputMethodObserver
,
119 OnTextInputStateChanged(client
));
122 void InputMethodBase::NotifyTextInputCaretBoundsChanged(
123 const TextInputClient
* client
) {
125 InputMethodObserver
, observer_list_
, OnCaretBoundsChanged(client
));
128 void InputMethodBase::SetFocusedTextInputClientInternal(
129 TextInputClient
* client
) {
130 TextInputClient
* old
= text_input_client_
;
133 OnWillChangeFocusedClient(old
, client
);
134 text_input_client_
= client
; // NULL allowed.
135 OnDidChangeFocusedClient(old
, client
);
136 NotifyTextInputStateChanged(text_input_client_
);