1 // Copyright 2013 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/views/controls/prefix_selector.h"
7 #include "base/i18n/case_conversion.h"
8 #include "ui/base/ime/text_input_type.h"
9 #include "ui/gfx/range/range.h"
10 #include "ui/views/controls/prefix_delegate.h"
11 #include "ui/views/widget/widget.h"
17 const int64 kTimeBeforeClearingMS
= 1000;
19 void ConvertRectToScreen(const views::View
* src
, gfx::Rect
* r
) {
22 gfx::Point new_origin
= r
->origin();
23 views::View::ConvertPointToScreen(src
, &new_origin
);
24 r
->set_origin(new_origin
);
29 PrefixSelector::PrefixSelector(PrefixDelegate
* delegate
)
30 : prefix_delegate_(delegate
) {
33 PrefixSelector::~PrefixSelector() {
36 void PrefixSelector::OnViewBlur() {
40 void PrefixSelector::SetCompositionText(
41 const ui::CompositionText
& composition
) {
44 void PrefixSelector::ConfirmCompositionText() {
47 void PrefixSelector::ClearCompositionText() {
50 void PrefixSelector::InsertText(const base::string16
& text
) {
54 void PrefixSelector::InsertChar(base::char16 ch
, int flags
) {
55 OnTextInput(base::string16(1, ch
));
58 gfx::NativeWindow
PrefixSelector::GetAttachedWindow() const {
59 return prefix_delegate_
->GetWidget()->GetNativeWindow();
62 ui::TextInputType
PrefixSelector::GetTextInputType() const {
63 return ui::TEXT_INPUT_TYPE_TEXT
;
66 ui::TextInputMode
PrefixSelector::GetTextInputMode() const {
67 return ui::TEXT_INPUT_MODE_DEFAULT
;
70 int PrefixSelector::GetTextInputFlags() const {
74 bool PrefixSelector::CanComposeInline() const {
78 gfx::Rect
PrefixSelector::GetCaretBounds() const {
79 gfx::Rect
rect(prefix_delegate_
->GetVisibleBounds().origin(), gfx::Size());
80 // TextInputClient::GetCaretBounds is expected to return a value in screen
82 ConvertRectToScreen(prefix_delegate_
, &rect
);
86 bool PrefixSelector::GetCompositionCharacterBounds(uint32 index
,
87 gfx::Rect
* rect
) const {
88 // TextInputClient::GetCompositionCharacterBounds is expected to fill |rect|
89 // in screen coordinates and GetCaretBounds returns screen coordinates.
90 *rect
= GetCaretBounds();
94 bool PrefixSelector::HasCompositionText() const {
98 bool PrefixSelector::GetTextRange(gfx::Range
* range
) const {
99 *range
= gfx::Range();
103 bool PrefixSelector::GetCompositionTextRange(gfx::Range
* range
) const {
104 *range
= gfx::Range();
108 bool PrefixSelector::GetSelectionRange(gfx::Range
* range
) const {
109 *range
= gfx::Range();
113 bool PrefixSelector::SetSelectionRange(const gfx::Range
& range
) {
117 bool PrefixSelector::DeleteRange(const gfx::Range
& range
) {
121 bool PrefixSelector::GetTextFromRange(const gfx::Range
& range
,
122 base::string16
* text
) const {
126 void PrefixSelector::OnInputMethodChanged() {
130 bool PrefixSelector::ChangeTextDirectionAndLayoutAlignment(
131 base::i18n::TextDirection direction
) {
135 void PrefixSelector::ExtendSelectionAndDelete(size_t before
, size_t after
) {
138 void PrefixSelector::EnsureCaretInRect(const gfx::Rect
& rect
) {
141 void PrefixSelector::OnCandidateWindowShown() {
144 void PrefixSelector::OnCandidateWindowUpdated() {
147 void PrefixSelector::OnCandidateWindowHidden() {
150 bool PrefixSelector::IsEditCommandEnabled(int command_id
) {
154 void PrefixSelector::SetEditCommandForNextKeyEvent(int command_id
) {
157 void PrefixSelector::OnTextInput(const base::string16
& text
) {
158 // Small hack to filter out 'tab' and 'enter' input, as the expectation is
159 // that they are control characters and will not affect the currently-active
161 if (text
.length() == 1 &&
162 (text
[0] == L
'\t' || text
[0] == L
'\r' || text
[0] == L
'\n'))
165 const int row_count
= prefix_delegate_
->GetRowCount();
169 // Search for |text| if it has been a while since the user typed, otherwise
170 // append |text| to |current_text_| and search for that. If it has been a
171 // while search after the current row, otherwise search starting from the
173 int row
= std::max(0, prefix_delegate_
->GetSelectedRow());
174 const base::TimeTicks
now(base::TimeTicks::Now());
175 if ((now
- time_of_last_key_
).InMilliseconds() < kTimeBeforeClearingMS
) {
176 current_text_
+= text
;
178 current_text_
= text
;
179 if (prefix_delegate_
->GetSelectedRow() >= 0)
180 row
= (row
+ 1) % row_count
;
182 time_of_last_key_
= now
;
184 const int start_row
= row
;
185 const base::string16
lower_text(base::i18n::ToLower(current_text_
));
187 if (TextAtRowMatchesText(row
, lower_text
)) {
188 prefix_delegate_
->SetSelectedRow(row
);
191 row
= (row
+ 1) % row_count
;
192 } while (row
!= start_row
);
195 bool PrefixSelector::TextAtRowMatchesText(int row
,
196 const base::string16
& lower_text
) {
197 const base::string16
model_text(
198 base::i18n::ToLower(prefix_delegate_
->GetTextForRow(row
)));
199 return (model_text
.size() >= lower_text
.size()) &&
200 (model_text
.compare(0, lower_text
.size(), lower_text
) == 0);
203 void PrefixSelector::ClearText() {
204 current_text_
.clear();
205 time_of_last_key_
= base::TimeTicks();