Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / base / ime / input_method_base.cc
blobdd11947b29a82fc6bc1560dd8854bf20e54ffd09
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 system_toplevel_window_focused_ = true;
44 void InputMethodBase::OnBlur() {
45 system_toplevel_window_focused_ = false;
48 void InputMethodBase::SetFocusedTextInputClient(TextInputClient* client) {
49 SetFocusedTextInputClientInternal(client);
52 void InputMethodBase::DetachTextInputClient(TextInputClient* client) {
53 if (text_input_client_ != client)
54 return;
55 SetFocusedTextInputClientInternal(NULL);
58 TextInputClient* InputMethodBase::GetTextInputClient() const {
59 if (switches::IsTextInputFocusManagerEnabled())
60 return TextInputFocusManager::GetInstance()->GetFocusedTextInputClient();
62 return system_toplevel_window_focused_ ? text_input_client_ : NULL;
65 void InputMethodBase::OnTextInputTypeChanged(const TextInputClient* client) {
66 if (!IsTextInputClientFocused(client))
67 return;
68 NotifyTextInputStateChanged(client);
71 TextInputType InputMethodBase::GetTextInputType() const {
72 TextInputClient* client = GetTextInputClient();
73 return client ? client->GetTextInputType() : TEXT_INPUT_TYPE_NONE;
76 TextInputMode InputMethodBase::GetTextInputMode() const {
77 TextInputClient* client = GetTextInputClient();
78 return client ? client->GetTextInputMode() : TEXT_INPUT_MODE_DEFAULT;
81 int InputMethodBase::GetTextInputFlags() const {
82 TextInputClient* client = GetTextInputClient();
83 return client ? client->GetTextInputFlags() : 0;
86 bool InputMethodBase::CanComposeInline() const {
87 TextInputClient* client = GetTextInputClient();
88 return client ? client->CanComposeInline() : true;
91 void InputMethodBase::ShowImeIfNeeded() {
92 FOR_EACH_OBSERVER(InputMethodObserver, observer_list_, OnShowImeIfNeeded());
95 void InputMethodBase::AddObserver(InputMethodObserver* observer) {
96 observer_list_.AddObserver(observer);
99 void InputMethodBase::RemoveObserver(InputMethodObserver* observer) {
100 observer_list_.RemoveObserver(observer);
103 bool InputMethodBase::IsTextInputClientFocused(const TextInputClient* client) {
104 return client && (client == GetTextInputClient());
107 bool InputMethodBase::IsTextInputTypeNone() const {
108 return GetTextInputType() == TEXT_INPUT_TYPE_NONE;
111 void InputMethodBase::OnInputMethodChanged() const {
112 TextInputClient* client = GetTextInputClient();
113 if (!IsTextInputTypeNone())
114 client->OnInputMethodChanged();
117 bool InputMethodBase::DispatchKeyEventPostIME(
118 const ui::KeyEvent& event) const {
119 if (!delegate_)
120 return false;
122 return delegate_->DispatchKeyEventPostIME(event);
125 void InputMethodBase::NotifyTextInputStateChanged(
126 const TextInputClient* client) {
127 FOR_EACH_OBSERVER(InputMethodObserver,
128 observer_list_,
129 OnTextInputStateChanged(client));
132 void InputMethodBase::NotifyTextInputCaretBoundsChanged(
133 const TextInputClient* client) {
134 FOR_EACH_OBSERVER(
135 InputMethodObserver, observer_list_, OnCaretBoundsChanged(client));
138 void InputMethodBase::SetFocusedTextInputClientInternal(
139 TextInputClient* client) {
140 if (switches::IsTextInputFocusManagerEnabled())
141 return;
143 TextInputClient* old = text_input_client_;
144 if (old == client)
145 return;
146 OnWillChangeFocusedClient(old, client);
147 text_input_client_ = client; // NULL allowed.
148 OnDidChangeFocusedClient(old, client);
149 NotifyTextInputStateChanged(text_input_client_);
152 void InputMethodBase::OnCandidateWindowShown() {
153 base::MessageLoop::current()->PostTask(
154 FROM_HERE,
155 base::Bind(&InputMethodBase::CandidateWindowShownCallback, AsWeakPtr()));
158 void InputMethodBase::OnCandidateWindowUpdated() {
159 base::MessageLoop::current()->PostTask(
160 FROM_HERE,
161 base::Bind(&InputMethodBase::CandidateWindowUpdatedCallback,
162 AsWeakPtr()));
165 void InputMethodBase::OnCandidateWindowHidden() {
166 base::MessageLoop::current()->PostTask(
167 FROM_HERE,
168 base::Bind(&InputMethodBase::CandidateWindowHiddenCallback, AsWeakPtr()));
171 void InputMethodBase::CandidateWindowShownCallback() {
172 if (TextInputClient* text_input_client = GetTextInputClient())
173 text_input_client->OnCandidateWindowShown();
176 void InputMethodBase::CandidateWindowUpdatedCallback() {
177 if (TextInputClient* text_input_client = GetTextInputClient())
178 text_input_client->OnCandidateWindowUpdated();
181 void InputMethodBase::CandidateWindowHiddenCallback() {
182 if (TextInputClient* text_input_client = GetTextInputClient())
183 text_input_client->OnCandidateWindowHidden();
186 } // namespace ui