1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/ime/input_method_bridge.h"
#include "ui/base/events/event.h"
#include "ui/base/ime/input_method.h"
#include "ui/gfx/rect.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
namespace views {
InputMethodBridge::InputMethodBridge(internal::InputMethodDelegate* delegate,
ui::InputMethod* host)
: host_(host),
context_focused_(false) {
DCHECK(host_);
set_delegate(delegate);
}
InputMethodBridge::~InputMethodBridge() {
if (host_->GetTextInputClient() == this)
host_->SetFocusedTextInputClient(NULL);
}
void InputMethodBridge::Init(Widget* widget) {
InputMethodBase::Init(widget);
}
void InputMethodBridge::OnFocus() {
InputMethodBase::OnFocus();
// Ask the system-wide IME to send all TextInputClient messages to |this|
// object.
host_->SetFocusedTextInputClient(this);
// TODO(yusukes): We don't need to call OnTextInputTypeChanged() once we move
// text input type tracker code to ui::InputMethodBase.
if (GetFocusedView())
OnTextInputTypeChanged(GetFocusedView());
}
void InputMethodBridge::OnBlur() {
// win32 sends multiple focus lost events, ignore all but the first.
if (widget_focused())
return;
ConfirmCompositionText();
InputMethodBase::OnBlur();
if (host_->GetTextInputClient() == this)
host_->SetFocusedTextInputClient(NULL);
}
void InputMethodBridge::DispatchKeyEvent(const ui::KeyEvent& key) {
DCHECK(key.type() == ui::ET_KEY_PRESSED || key.type() == ui::ET_KEY_RELEASED);
// We can just dispatch the event here since the |key| is already processed by
// the system-wide IME.
DispatchKeyEventPostIME(key);
}
void InputMethodBridge::OnTextInputTypeChanged(View* view) {
if (IsViewFocused(view))
host_->OnTextInputTypeChanged(this);
InputMethodBase::OnTextInputTypeChanged(view);
}
void InputMethodBridge::OnCaretBoundsChanged(View* view) {
if (IsViewFocused(view) && !IsTextInputTypeNone())
host_->OnCaretBoundsChanged(this);
}
void InputMethodBridge::CancelComposition(View* view) {
if (IsViewFocused(view))
host_->CancelComposition(this);
}
std::string InputMethodBridge::GetInputLocale() {
return host_->GetInputLocale();
}
base::i18n::TextDirection InputMethodBridge::GetInputTextDirection() {
return host_->GetInputTextDirection();
}
bool InputMethodBridge::IsActive() {
return host_->IsActive();
}
// Overridden from TextInputClient. Forward an event from the system-wide IME
// to the text input |client|, which is e.g. views::NativeTextfieldViews.
void InputMethodBridge::SetCompositionText(
const ui::CompositionText& composition) {
TextInputClient* client = GetTextInputClient();
if (client)
client->SetCompositionText(composition);
}
void InputMethodBridge::ConfirmCompositionText() {
TextInputClient* client = GetTextInputClient();
if (client)
client->ConfirmCompositionText();
}
void InputMethodBridge::ClearCompositionText() {
TextInputClient* client = GetTextInputClient();
if (client)
client->ClearCompositionText();
}
void InputMethodBridge::InsertText(const string16& text) {
TextInputClient* client = GetTextInputClient();
if (client)
client->InsertText(text);
}
void InputMethodBridge::InsertChar(char16 ch, int flags) {
TextInputClient* client = GetTextInputClient();
if (client)
client->InsertChar(ch, flags);
}
ui::TextInputType InputMethodBridge::GetTextInputType() const {
TextInputClient* client = GetTextInputClient();
return client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
}
bool InputMethodBridge::CanComposeInline() const {
TextInputClient* client = GetTextInputClient();
return client ? client->CanComposeInline() : true;
}
gfx::Rect InputMethodBridge::ConvertRectToFocusedView(const gfx::Rect& rect) {
gfx::Point origin = rect.origin();
gfx::Point end = gfx::Point(rect.right(), rect.bottom());
View::ConvertPointToScreen(GetFocusedView(), &origin);
View::ConvertPointToScreen(GetFocusedView(), &end);
return gfx::Rect(origin.x(),
origin.y(),
end.x() - origin.x(),
end.y() - origin.y());
}
gfx::Rect InputMethodBridge::GetCaretBounds() {
TextInputClient* client = GetTextInputClient();
if (!client || !GetFocusedView())
return gfx::Rect();
const gfx::Rect rect = client->GetCaretBounds();
return ConvertRectToFocusedView(rect);
}
bool InputMethodBridge::GetCompositionCharacterBounds(uint32 index,
gfx::Rect* rect) {
DCHECK(rect);
TextInputClient* client = GetTextInputClient();
if (!client || !GetFocusedView())
return false;
gfx::Rect relative_rect;
if (!client->GetCompositionCharacterBounds(index, &relative_rect))
return false;
*rect = ConvertRectToFocusedView(relative_rect);
return true;
}
bool InputMethodBridge::HasCompositionText() {
TextInputClient* client = GetTextInputClient();
return client ? client->HasCompositionText() : false;
}
bool InputMethodBridge::GetTextRange(ui::Range* range) {
TextInputClient* client = GetTextInputClient();
return client ? client->GetTextRange(range) : false;
}
bool InputMethodBridge::GetCompositionTextRange(ui::Range* range) {
TextInputClient* client = GetTextInputClient();
return client ? client->GetCompositionTextRange(range) : false;
}
bool InputMethodBridge::GetSelectionRange(ui::Range* range) {
TextInputClient* client = GetTextInputClient();
return client ? client->GetSelectionRange(range) : false;
}
bool InputMethodBridge::SetSelectionRange(const ui::Range& range) {
TextInputClient* client = GetTextInputClient();
return client ? client->SetSelectionRange(range) : false;
}
bool InputMethodBridge::DeleteRange(const ui::Range& range) {
TextInputClient* client = GetTextInputClient();
return client ? client->DeleteRange(range) : false;
}
bool InputMethodBridge::GetTextFromRange(
const ui::Range& range, string16* text) {
TextInputClient* client = GetTextInputClient();
return client ? client->GetTextFromRange(range, text) : false;
}
void InputMethodBridge::OnInputMethodChanged() {
TextInputClient* client = GetTextInputClient();
if (client)
client->OnInputMethodChanged();
}
bool InputMethodBridge::ChangeTextDirectionAndLayoutAlignment(
base::i18n::TextDirection direction) {
TextInputClient* client = GetTextInputClient();
return client ?
client->ChangeTextDirectionAndLayoutAlignment(direction) : false;
}
void InputMethodBridge::ExtendSelectionAndDelete(size_t before, size_t after) {
TextInputClient* client = GetTextInputClient();
if (client)
client->ExtendSelectionAndDelete(before, after);
}
// Overridden from FocusChangeListener.
void InputMethodBridge::OnWillChangeFocus(View* focused_before, View* focused) {
ConfirmCompositionText();
}
void InputMethodBridge::OnDidChangeFocus(View* focused_before, View* focused) {
OnTextInputTypeChanged(GetFocusedView());
OnCaretBoundsChanged(GetFocusedView());
}
} // namespace views
|