Chromium Code Reviews| Index: views/controls/textfield/textfield_views_model.cc |
| diff --git a/views/controls/textfield/textfield_views_model.cc b/views/controls/textfield/textfield_views_model.cc |
| index 7111ac25e6bbf8fb3f5f6b88771d45a6305c343f..c7624b4e6e64095acd86b403975d29bdcbd179cd 100644 |
| --- a/views/controls/textfield/textfield_views_model.cc |
| +++ b/views/controls/textfield/textfield_views_model.cc |
| @@ -224,6 +224,41 @@ void TextfieldViewsModel::SelectAll() { |
| selection_begin_ = text_.length(); |
| } |
| +void TextfieldViewsModel::SelectWord() { |
|
oshima
2011/01/18 19:52:54
Will this generate same word boundary that break i
varunjain
2011/01/25 04:06:40
no... particularly, the handling of non A-Z chars
|
| + // First we setup selection_begin_ and cursor_pos_. There are so many cases |
| + // because we try to emulate what select-word looks like in a gtk textfield. |
| + // See associated testcase for different cases. |
| + if (cursor_pos_ > 0 && cursor_pos_ < text_.length()) { |
| + if (isalnum(text_[cursor_pos_])) { |
| + selection_begin_ = cursor_pos_; |
| + cursor_pos_++; |
| + } else |
| + selection_begin_ = cursor_pos_ - 1; |
| + } else if (cursor_pos_ == 0) { |
| + selection_begin_ = cursor_pos_; |
| + if (text_.length() > 0) |
| + cursor_pos_++; |
| + } else { |
| + selection_begin_ = cursor_pos_ - 1; |
| + } |
| + |
| + // Now we move selection_begin_ to beginning of selection. Selection boundary |
| + // is defined as the position where we have alpha-num character on one side |
| + // and non-alpha-num char on the other side. |
| + for (; selection_begin_ > 0; selection_begin_--) { |
| + if (IsPositionAtWordSelectionBoundary(selection_begin_)) |
| + break; |
| + } |
| + |
| + // Now we move cursor_pos_ to end of selection. Selection boundary |
| + // is defined as the position where we have alpha-num character on one side |
| + // and non-alpha-num char on the other side. |
| + for (; cursor_pos_ < text_.length(); cursor_pos_++) { |
| + if (IsPositionAtWordSelectionBoundary(cursor_pos_)) |
| + break; |
| + } |
| +} |
| + |
| void TextfieldViewsModel::ClearSelection() { |
| selection_begin_ = cursor_pos_; |
| } |
| @@ -281,4 +316,9 @@ string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { |
| return text_.substr(begin, end - begin); |
| } |
| +bool TextfieldViewsModel::IsPositionAtWordSelectionBoundary(size_t pos) { |
| + return (isalnum(text_[pos - 1]) && !isalnum(text_[pos])) || |
| + (!isalnum(text_[pos - 1]) && isalnum(text_[pos])); |
| +} |
| + |
| } // namespace views |