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.
4
5 #include "ui/views/controls/prefix_selector.h"
6
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"
12
13 namespace views {
14
15 namespace {
16
17 const int64 kTimeBeforeClearingMS = 1000;
18
ConvertRectToScreen(const views::View * src,gfx::Rect * r)19 void ConvertRectToScreen(const views::View* src, gfx::Rect* r) {
20 DCHECK(src);
21
22 gfx::Point new_origin = r->origin();
23 views::View::ConvertPointToScreen(src, &new_origin);
24 r->set_origin(new_origin);
25 }
26
27 } // namespace
28
PrefixSelector(PrefixDelegate * delegate)29 PrefixSelector::PrefixSelector(PrefixDelegate* delegate)
30 : prefix_delegate_(delegate) {
31 }
32
~PrefixSelector()33 PrefixSelector::~PrefixSelector() {
34 }
35
OnViewBlur()36 void PrefixSelector::OnViewBlur() {
37 ClearText();
38 }
39
SetCompositionText(const ui::CompositionText & composition)40 void PrefixSelector::SetCompositionText(
41 const ui::CompositionText& composition) {
42 }
43
ConfirmCompositionText()44 void PrefixSelector::ConfirmCompositionText() {
45 }
46
ClearCompositionText()47 void PrefixSelector::ClearCompositionText() {
48 }
49
InsertText(const string16 & text)50 void PrefixSelector::InsertText(const string16& text) {
51 OnTextInput(text);
52 }
53
InsertChar(char16 ch,int flags)54 void PrefixSelector::InsertChar(char16 ch, int flags) {
55 OnTextInput(string16(1, ch));
56 }
57
GetAttachedWindow() const58 gfx::NativeWindow PrefixSelector::GetAttachedWindow() const {
59 return prefix_delegate_->GetWidget()->GetNativeWindow();
60 }
61
GetTextInputType() const62 ui::TextInputType PrefixSelector::GetTextInputType() const {
63 return ui::TEXT_INPUT_TYPE_TEXT;
64 }
65
GetTextInputMode() const66 ui::TextInputMode PrefixSelector::GetTextInputMode() const {
67 return ui::TEXT_INPUT_MODE_DEFAULT;
68 }
69
CanComposeInline() const70 bool PrefixSelector::CanComposeInline() const {
71 return false;
72 }
73
GetCaretBounds() const74 gfx::Rect PrefixSelector::GetCaretBounds() const {
75 gfx::Rect rect(prefix_delegate_->GetVisibleBounds().origin(), gfx::Size());
76 // TextInputClient::GetCaretBounds is expected to return a value in screen
77 // coordinates.
78 ConvertRectToScreen(prefix_delegate_, &rect);
79 return rect;
80 }
81
GetCompositionCharacterBounds(uint32 index,gfx::Rect * rect) const82 bool PrefixSelector::GetCompositionCharacterBounds(uint32 index,
83 gfx::Rect* rect) const {
84 // TextInputClient::GetCompositionCharacterBounds is expected to fill |rect|
85 // in screen coordinates and GetCaretBounds returns screen coordinates.
86 *rect = GetCaretBounds();
87 return false;
88 }
89
HasCompositionText() const90 bool PrefixSelector::HasCompositionText() const {
91 return false;
92 }
93
GetTextRange(gfx::Range * range) const94 bool PrefixSelector::GetTextRange(gfx::Range* range) const {
95 *range = gfx::Range();
96 return false;
97 }
98
GetCompositionTextRange(gfx::Range * range) const99 bool PrefixSelector::GetCompositionTextRange(gfx::Range* range) const {
100 *range = gfx::Range();
101 return false;
102 }
103
GetSelectionRange(gfx::Range * range) const104 bool PrefixSelector::GetSelectionRange(gfx::Range* range) const {
105 *range = gfx::Range();
106 return false;
107 }
108
SetSelectionRange(const gfx::Range & range)109 bool PrefixSelector::SetSelectionRange(const gfx::Range& range) {
110 return false;
111 }
112
DeleteRange(const gfx::Range & range)113 bool PrefixSelector::DeleteRange(const gfx::Range& range) {
114 return false;
115 }
116
GetTextFromRange(const gfx::Range & range,string16 * text) const117 bool PrefixSelector::GetTextFromRange(const gfx::Range& range,
118 string16* text) const {
119 return false;
120 }
121
OnInputMethodChanged()122 void PrefixSelector::OnInputMethodChanged() {
123 ClearText();
124 }
125
ChangeTextDirectionAndLayoutAlignment(base::i18n::TextDirection direction)126 bool PrefixSelector::ChangeTextDirectionAndLayoutAlignment(
127 base::i18n::TextDirection direction) {
128 return true;
129 }
130
ExtendSelectionAndDelete(size_t before,size_t after)131 void PrefixSelector::ExtendSelectionAndDelete(size_t before, size_t after) {
132 }
133
EnsureCaretInRect(const gfx::Rect & rect)134 void PrefixSelector::EnsureCaretInRect(const gfx::Rect& rect) {
135 }
136
OnCandidateWindowShown()137 void PrefixSelector::OnCandidateWindowShown() {
138 }
139
OnCandidateWindowUpdated()140 void PrefixSelector::OnCandidateWindowUpdated() {
141 }
142
OnCandidateWindowHidden()143 void PrefixSelector::OnCandidateWindowHidden() {
144 }
145
OnTextInput(const string16 & text)146 void PrefixSelector::OnTextInput(const string16& text) {
147 // Small hack to filter out 'tab' and 'enter' input, as the expectation is
148 // that they are control characters and will not affect the currently-active
149 // prefix.
150 if (text.length() == 1 &&
151 (text[0] == L'\t' || text[0] == L'\r' || text[0] == L'\n'))
152 return;
153
154 const int row_count = prefix_delegate_->GetRowCount();
155 if (row_count == 0)
156 return;
157
158 // Search for |text| if it has been a while since the user typed, otherwise
159 // append |text| to |current_text_| and search for that. If it has been a
160 // while search after the current row, otherwise search starting from the
161 // current row.
162 int row = std::max(0, prefix_delegate_->GetSelectedRow());
163 const base::TimeTicks now(base::TimeTicks::Now());
164 if ((now - time_of_last_key_).InMilliseconds() < kTimeBeforeClearingMS) {
165 current_text_ += text;
166 } else {
167 current_text_ = text;
168 if (prefix_delegate_->GetSelectedRow() >= 0)
169 row = (row + 1) % row_count;
170 }
171 time_of_last_key_ = now;
172
173 const int start_row = row;
174 const string16 lower_text(base::i18n::ToLower(current_text_));
175 do {
176 if (TextAtRowMatchesText(row, current_text_)) {
177 prefix_delegate_->SetSelectedRow(row);
178 return;
179 }
180 row = (row + 1) % row_count;
181 } while (row != start_row);
182 }
183
TextAtRowMatchesText(int row,const string16 & lower_text)184 bool PrefixSelector::TextAtRowMatchesText(int row,
185 const string16& lower_text) {
186 const string16 model_text(
187 base::i18n::ToLower(prefix_delegate_->GetTextForRow(row)));
188 return (model_text.size() >= lower_text.size()) &&
189 (model_text.compare(0, lower_text.size(), lower_text) == 0);
190 }
191
ClearText()192 void PrefixSelector::ClearText() {
193 current_text_.clear();
194 time_of_last_key_ = base::TimeTicks();
195 }
196
197 } // namespace views
198