Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / base / ime / input_method_base.cc
blob0dca37870e5ed3c2fc8e4a6645db78ed1db7742b
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"
7 #include "base/bind.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"
17 namespace ui {
19 InputMethodBase::InputMethodBase()
20 : delegate_(NULL),
21 text_input_client_(NULL),
22 system_toplevel_window_focused_(false) {
25 InputMethodBase::~InputMethodBase() {
26 FOR_EACH_OBSERVER(InputMethodObserver,
27 observer_list_,
28 OnInputMethodDestroyed(this));
31 void InputMethodBase::SetDelegate(internal::InputMethodDelegate* delegate) {
32 delegate_ = delegate;
35 void InputMethodBase::Init(bool focused) {
36 if (focused)
37 OnFocus();
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)
56 return;
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))
69 return;
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 {
121 if (!delegate_)
122 return false;
124 return delegate_->DispatchKeyEventPostIME(event);
127 void InputMethodBase::NotifyTextInputStateChanged(
128 const TextInputClient* client) {
129 FOR_EACH_OBSERVER(InputMethodObserver,
130 observer_list_,
131 OnTextInputStateChanged(client));
134 void InputMethodBase::NotifyTextInputCaretBoundsChanged(
135 const TextInputClient* client) {
136 FOR_EACH_OBSERVER(
137 InputMethodObserver, observer_list_, OnCaretBoundsChanged(client));
140 void InputMethodBase::SetFocusedTextInputClientInternal(
141 TextInputClient* client) {
142 if (switches::IsTextInputFocusManagerEnabled())
143 return;
145 TextInputClient* old = text_input_client_;
146 if (old == client)
147 return;
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(
156 FROM_HERE,
157 base::Bind(&InputMethodBase::CandidateWindowShownCallback, AsWeakPtr()));
160 void InputMethodBase::OnCandidateWindowUpdated() {
161 base::MessageLoop::current()->PostTask(
162 FROM_HERE,
163 base::Bind(&InputMethodBase::CandidateWindowUpdatedCallback,
164 AsWeakPtr()));
167 void InputMethodBase::OnCandidateWindowHidden() {
168 base::MessageLoop::current()->PostTask(
169 FROM_HERE,
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();
188 } // namespace ui