Add ICU message format support
[chromium-blink-merge.git] / ui / views / controls / prefix_selector.cc
blob9b84cfa63fd33844db80664b427bf5e96c9928d8
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"
14 namespace views {
16 namespace {
18 const int64 kTimeBeforeClearingMS = 1000;
20 void ConvertRectToScreen(const views::View* src, gfx::Rect* r) {
21 DCHECK(src);
23 gfx::Point new_origin = r->origin();
24 views::View::ConvertPointToScreen(src, &new_origin);
25 r->set_origin(new_origin);
28 } // namespace
30 PrefixSelector::PrefixSelector(PrefixDelegate* delegate)
31 : prefix_delegate_(delegate) {
34 PrefixSelector::~PrefixSelector() {
37 void PrefixSelector::OnViewBlur() {
38 ClearText();
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) {
52 OnTextInput(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 {
68 return 0;
71 bool PrefixSelector::CanComposeInline() const {
72 return false;
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
78 // coordinates.
79 ConvertRectToScreen(prefix_delegate_, &rect);
80 return 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();
88 return false;
91 bool PrefixSelector::HasCompositionText() const {
92 return false;
95 bool PrefixSelector::GetTextRange(gfx::Range* range) const {
96 *range = gfx::Range();
97 return false;
100 bool PrefixSelector::GetCompositionTextRange(gfx::Range* range) const {
101 *range = gfx::Range();
102 return false;
105 bool PrefixSelector::GetSelectionRange(gfx::Range* range) const {
106 *range = gfx::Range();
107 return false;
110 bool PrefixSelector::SetSelectionRange(const gfx::Range& range) {
111 return false;
114 bool PrefixSelector::DeleteRange(const gfx::Range& range) {
115 return false;
118 bool PrefixSelector::GetTextFromRange(const gfx::Range& range,
119 base::string16* text) const {
120 return false;
123 void PrefixSelector::OnInputMethodChanged() {
124 ClearText();
127 bool PrefixSelector::ChangeTextDirectionAndLayoutAlignment(
128 base::i18n::TextDirection direction) {
129 return true;
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) {
139 return false;
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
148 // prefix.
149 if (text.length() == 1 &&
150 (text[0] == L'\t' || text[0] == L'\r' || text[0] == L'\n'))
151 return;
153 const int row_count = prefix_delegate_->GetRowCount();
154 if (row_count == 0)
155 return;
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
160 // current row.
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;
165 } else {
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_));
174 do {
175 if (TextAtRowMatchesText(row, lower_text)) {
176 prefix_delegate_->SetSelectedRow(row);
177 return;
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();
196 } // namespace views