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/input_method.h"
9 #include "ui/base/ime/text_input_type.h"
10 #include "ui/gfx/range/range.h"
11 #include "ui/views/controls/prefix_delegate.h"
12 #include "ui/views/widget/widget.h"
18 const int64 kTimeBeforeClearingMS
= 1000;
20 void ConvertRectToScreen(const views::View
* src
, gfx::Rect
* r
) {
23 gfx::Point new_origin
= r
->origin();
24 views::View::ConvertPointToScreen(src
, &new_origin
);
25 r
->set_origin(new_origin
);
30 PrefixSelector::PrefixSelector(PrefixDelegate
* delegate
)
31 : prefix_delegate_(delegate
) {
34 PrefixSelector::~PrefixSelector() {
37 void PrefixSelector::OnViewBlur() {
41 void PrefixSelector::SetCompositionText(
42 const ui::CompositionText
& composition
) {
45 void PrefixSelector::ConfirmCompositionText() {
48 void PrefixSelector::ClearCompositionText() {
51 void PrefixSelector::InsertText(const base::string16
& text
) {
55 void PrefixSelector::InsertChar(base::char16 ch
, int flags
) {
56 OnTextInput(base::string16(1, ch
));
59 ui::TextInputType
PrefixSelector::GetTextInputType() const {
60 return ui::TEXT_INPUT_TYPE_TEXT
;
63 ui::TextInputMode
PrefixSelector::GetTextInputMode() const {
64 return ui::TEXT_INPUT_MODE_DEFAULT
;
67 int PrefixSelector::GetTextInputFlags() const {
71 bool PrefixSelector::CanComposeInline() const {
75 gfx::Rect
PrefixSelector::GetCaretBounds() const {
76 gfx::Rect
rect(prefix_delegate_
->GetVisibleBounds().origin(), gfx::Size());
77 // TextInputClient::GetCaretBounds is expected to return a value in screen
79 ConvertRectToScreen(prefix_delegate_
, &rect
);
83 bool PrefixSelector::GetCompositionCharacterBounds(uint32 index
,
84 gfx::Rect
* rect
) const {
85 // TextInputClient::GetCompositionCharacterBounds is expected to fill |rect|
86 // in screen coordinates and GetCaretBounds returns screen coordinates.
87 *rect
= GetCaretBounds();
91 bool PrefixSelector::HasCompositionText() const {
95 bool PrefixSelector::GetTextRange(gfx::Range
* range
) const {
96 *range
= gfx::Range();
100 bool PrefixSelector::GetCompositionTextRange(gfx::Range
* range
) const {
101 *range
= gfx::Range();
105 bool PrefixSelector::GetSelectionRange(gfx::Range
* range
) const {
106 *range
= gfx::Range();
110 bool PrefixSelector::SetSelectionRange(const gfx::Range
& range
) {
114 bool PrefixSelector::DeleteRange(const gfx::Range
& range
) {
118 bool PrefixSelector::GetTextFromRange(const gfx::Range
& range
,
119 base::string16
* text
) const {
123 void PrefixSelector::OnInputMethodChanged() {
127 bool PrefixSelector::ChangeTextDirectionAndLayoutAlignment(
128 base::i18n::TextDirection direction
) {
132 void PrefixSelector::ExtendSelectionAndDelete(size_t before
, size_t after
) {
135 void PrefixSelector::EnsureCaretInRect(const gfx::Rect
& rect
) {
138 bool PrefixSelector::IsEditCommandEnabled(int command_id
) {
142 void PrefixSelector::SetEditCommandForNextKeyEvent(int command_id
) {
145 void PrefixSelector::OnTextInput(const base::string16
& text
) {
146 // Small hack to filter out 'tab' and 'enter' input, as the expectation is
147 // that they are control characters and will not affect the currently-active
149 if (text
.length() == 1 &&
150 (text
[0] == L
'\t' || text
[0] == L
'\r' || text
[0] == L
'\n'))
153 const int row_count
= prefix_delegate_
->GetRowCount();
157 // Search for |text| if it has been a while since the user typed, otherwise
158 // append |text| to |current_text_| and search for that. If it has been a
159 // while search after the current row, otherwise search starting from the
161 int row
= std::max(0, prefix_delegate_
->GetSelectedRow());
162 const base::TimeTicks
now(base::TimeTicks::Now());
163 if ((now
- time_of_last_key_
).InMilliseconds() < kTimeBeforeClearingMS
) {
164 current_text_
+= text
;
166 current_text_
= text
;
167 if (prefix_delegate_
->GetSelectedRow() >= 0)
168 row
= (row
+ 1) % row_count
;
170 time_of_last_key_
= now
;
172 const int start_row
= row
;
173 const base::string16
lower_text(base::i18n::ToLower(current_text_
));
175 if (TextAtRowMatchesText(row
, lower_text
)) {
176 prefix_delegate_
->SetSelectedRow(row
);
179 row
= (row
+ 1) % row_count
;
180 } while (row
!= start_row
);
183 bool PrefixSelector::TextAtRowMatchesText(int row
,
184 const base::string16
& lower_text
) {
185 const base::string16
model_text(
186 base::i18n::ToLower(prefix_delegate_
->GetTextForRow(row
)));
187 return (model_text
.size() >= lower_text
.size()) &&
188 (model_text
.compare(0, lower_text
.size(), lower_text
) == 0);
191 void PrefixSelector::ClearText() {
192 current_text_
.clear();
193 time_of_last_key_
= base::TimeTicks();