Revert of Fix missing GN dependencies. (patchset #4 id:60001 of https://codereview...
[chromium-blink-merge.git] / ui / base / ime / input_method_base.cc
blob26c2d4097e6f8008649dbfaa9d2424ca15caac2e
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::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)
49 return;
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))
62 return;
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 {
114 if (!delegate_)
115 return false;
117 return delegate_->DispatchKeyEventPostIME(event);
120 void InputMethodBase::NotifyTextInputStateChanged(
121 const TextInputClient* client) {
122 FOR_EACH_OBSERVER(InputMethodObserver,
123 observer_list_,
124 OnTextInputStateChanged(client));
127 void InputMethodBase::NotifyTextInputCaretBoundsChanged(
128 const TextInputClient* client) {
129 FOR_EACH_OBSERVER(
130 InputMethodObserver, observer_list_, OnCaretBoundsChanged(client));
133 void InputMethodBase::SetFocusedTextInputClientInternal(
134 TextInputClient* client) {
135 if (switches::IsTextInputFocusManagerEnabled())
136 return;
138 TextInputClient* old = text_input_client_;
139 if (old == client)
140 return;
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(
149 FROM_HERE,
150 base::Bind(&InputMethodBase::CandidateWindowShownCallback, AsWeakPtr()));
153 void InputMethodBase::OnCandidateWindowUpdated() {
154 base::MessageLoop::current()->PostTask(
155 FROM_HERE,
156 base::Bind(&InputMethodBase::CandidateWindowUpdatedCallback,
157 AsWeakPtr()));
160 void InputMethodBase::OnCandidateWindowHidden() {
161 base::MessageLoop::current()->PostTask(
162 FROM_HERE,
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();
181 } // namespace ui