This source file includes following definitions.
- ConvertRectToScreen
- OnViewBlur
- SetCompositionText
- ConfirmCompositionText
- ClearCompositionText
- InsertText
- InsertChar
- GetAttachedWindow
- GetTextInputType
- GetTextInputMode
- CanComposeInline
- GetCaretBounds
- GetCompositionCharacterBounds
- HasCompositionText
- GetTextRange
- GetCompositionTextRange
- GetSelectionRange
- SetSelectionRange
- DeleteRange
- GetTextFromRange
- OnInputMethodChanged
- ChangeTextDirectionAndLayoutAlignment
- ExtendSelectionAndDelete
- EnsureCaretInRect
- OnCandidateWindowShown
- OnCandidateWindowUpdated
- OnCandidateWindowHidden
- OnTextInput
- TextAtRowMatchesText
- ClearText
#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);
}
}
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;
}
bool PrefixSelector::CanComposeInline() const {
return false;
}
gfx::Rect PrefixSelector::GetCaretBounds() const {
gfx::Rect rect(prefix_delegate_->GetVisibleBounds().origin(), gfx::Size());
ConvertRectToScreen(prefix_delegate_, &rect);
return rect;
}
bool PrefixSelector::GetCompositionCharacterBounds(uint32 index,
gfx::Rect* rect) const {
*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() {
}
void PrefixSelector::OnTextInput(const base::string16& text) {
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;
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();
}
}