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
|
// Copyright 2013 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/controls/prefix_selector.h"
#include "base/i18n/case_conversion.h"
#include "ui/base/ime/text_input_type.h"
#include "ui/gfx/range/range.h"
#include "ui/views/controls/prefix_delegate.h"
#include "ui/views/widget/widget.h"
namespace views {
namespace {
const int64 kTimeBeforeClearingMS = 1000;
void ConvertRectToScreen(const views::View* src, gfx::Rect* r) {
DCHECK(src);
gfx::Point new_origin = r->origin();
views::View::ConvertPointToScreen(src, &new_origin);
r->set_origin(new_origin);
}
} // namespace
PrefixSelector::PrefixSelector(PrefixDelegate* delegate)
: prefix_delegate_(delegate) {
}
PrefixSelector::~PrefixSelector() {
}
void PrefixSelector::OnViewBlur() {
ClearText();
}
void PrefixSelector::SetCompositionText(
const ui::CompositionText& composition) {
}
void PrefixSelector::ConfirmCompositionText() {
}
void PrefixSelector::ClearCompositionText() {
}
void PrefixSelector::InsertText(const base::string16& text) {
OnTextInput(text);
}
void PrefixSelector::InsertChar(base::char16 ch, int flags) {
OnTextInput(base::string16(1, ch));
}
gfx::NativeWindow PrefixSelector::GetAttachedWindow() const {
return prefix_delegate_->GetWidget()->GetNativeWindow();
}
ui::TextInputType PrefixSelector::GetTextInputType() const {
return ui::TEXT_INPUT_TYPE_TEXT;
}
ui::TextInputMode PrefixSelector::GetTextInputMode() const {
return ui::TEXT_INPUT_MODE_DEFAULT;
}
int PrefixSelector::GetTextInputFlags() const {
return 0;
}
bool PrefixSelector::CanComposeInline() const {
return false;
}
gfx::Rect PrefixSelector::GetCaretBounds() const {
gfx::Rect rect(prefix_delegate_->GetVisibleBounds().origin(), gfx::Size());
// TextInputClient::GetCaretBounds is expected to return a value in screen
// coordinates.
ConvertRectToScreen(prefix_delegate_, &rect);
return rect;
}
bool PrefixSelector::GetCompositionCharacterBounds(uint32 index,
gfx::Rect* rect) const {
// TextInputClient::GetCompositionCharacterBounds is expected to fill |rect|
// in screen coordinates and GetCaretBounds returns screen coordinates.
*rect = GetCaretBounds();
return false;
}
bool PrefixSelector::HasCompositionText() const {
return false;
}
bool PrefixSelector::GetTextRange(gfx::Range* range) const {
*range = gfx::Range();
return false;
}
bool PrefixSelector::GetCompositionTextRange(gfx::Range* range) const {
*range = gfx::Range();
return false;
}
bool PrefixSelector::GetSelectionRange(gfx::Range* range) const {
*range = gfx::Range();
return false;
}
bool PrefixSelector::SetSelectionRange(const gfx::Range& range) {
return false;
}
bool PrefixSelector::DeleteRange(const gfx::Range& range) {
return false;
}
bool PrefixSelector::GetTextFromRange(const gfx::Range& range,
base::string16* text) const {
return false;
}
void PrefixSelector::OnInputMethodChanged() {
ClearText();
}
bool PrefixSelector::ChangeTextDirectionAndLayoutAlignment(
base::i18n::TextDirection direction) {
return true;
}
void PrefixSelector::ExtendSelectionAndDelete(size_t before, size_t after) {
}
void PrefixSelector::EnsureCaretInRect(const gfx::Rect& rect) {
}
void PrefixSelector::OnCandidateWindowShown() {
}
void PrefixSelector::OnCandidateWindowUpdated() {
}
void PrefixSelector::OnCandidateWindowHidden() {
}
bool PrefixSelector::IsEditingCommandEnabled(int command_id) {
return false;
}
void PrefixSelector::ExecuteEditingCommand(int command_id) {
}
void PrefixSelector::OnTextInput(const base::string16& text) {
// Small hack to filter out 'tab' and 'enter' input, as the expectation is
// that they are control characters and will not affect the currently-active
// prefix.
if (text.length() == 1 &&
(text[0] == L'\t' || text[0] == L'\r' || text[0] == L'\n'))
return;
const int row_count = prefix_delegate_->GetRowCount();
if (row_count == 0)
return;
// Search for |text| if it has been a while since the user typed, otherwise
// append |text| to |current_text_| and search for that. If it has been a
// while search after the current row, otherwise search starting from the
// current row.
int row = std::max(0, prefix_delegate_->GetSelectedRow());
const base::TimeTicks now(base::TimeTicks::Now());
if ((now - time_of_last_key_).InMilliseconds() < kTimeBeforeClearingMS) {
current_text_ += text;
} else {
current_text_ = text;
if (prefix_delegate_->GetSelectedRow() >= 0)
row = (row + 1) % row_count;
}
time_of_last_key_ = now;
const int start_row = row;
const base::string16 lower_text(base::i18n::ToLower(current_text_));
do {
if (TextAtRowMatchesText(row, lower_text)) {
prefix_delegate_->SetSelectedRow(row);
return;
}
row = (row + 1) % row_count;
} while (row != start_row);
}
bool PrefixSelector::TextAtRowMatchesText(int row,
const base::string16& lower_text) {
const base::string16 model_text(
base::i18n::ToLower(prefix_delegate_->GetTextForRow(row)));
return (model_text.size() >= lower_text.size()) &&
(model_text.compare(0, lower_text.size(), lower_text) == 0);
}
void PrefixSelector::ClearText() {
current_text_.clear();
time_of_last_key_ = base::TimeTicks();
}
} // namespace views
|