OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "views/controls/textfield/textfield_views_model.h" | 5 #include "views/controls/textfield/textfield_views_model.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 return text_.substr( | 217 return text_.substr( |
218 std::min(cursor_pos_, selection_begin_), | 218 std::min(cursor_pos_, selection_begin_), |
219 std::abs(static_cast<long>(cursor_pos_ - selection_begin_))); | 219 std::abs(static_cast<long>(cursor_pos_ - selection_begin_))); |
220 } | 220 } |
221 | 221 |
222 void TextfieldViewsModel::SelectAll() { | 222 void TextfieldViewsModel::SelectAll() { |
223 cursor_pos_ = 0; | 223 cursor_pos_ = 0; |
224 selection_begin_ = text_.length(); | 224 selection_begin_ = text_.length(); |
225 } | 225 } |
226 | 226 |
227 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
| |
228 // First we setup selection_begin_ and cursor_pos_. There are so many cases | |
229 // because we try to emulate what select-word looks like in a gtk textfield. | |
230 // See associated testcase for different cases. | |
231 if (cursor_pos_ > 0 && cursor_pos_ < text_.length()) { | |
232 if (isalnum(text_[cursor_pos_])) { | |
233 selection_begin_ = cursor_pos_; | |
234 cursor_pos_++; | |
235 } else | |
236 selection_begin_ = cursor_pos_ - 1; | |
237 } else if (cursor_pos_ == 0) { | |
238 selection_begin_ = cursor_pos_; | |
239 if (text_.length() > 0) | |
240 cursor_pos_++; | |
241 } else { | |
242 selection_begin_ = cursor_pos_ - 1; | |
243 } | |
244 | |
245 // Now we move selection_begin_ to beginning of selection. Selection boundary | |
246 // is defined as the position where we have alpha-num character on one side | |
247 // and non-alpha-num char on the other side. | |
248 for (; selection_begin_ > 0; selection_begin_--) { | |
249 if (IsPositionAtWordSelectionBoundary(selection_begin_)) | |
250 break; | |
251 } | |
252 | |
253 // Now we move cursor_pos_ to end of selection. Selection boundary | |
254 // is defined as the position where we have alpha-num character on one side | |
255 // and non-alpha-num char on the other side. | |
256 for (; cursor_pos_ < text_.length(); cursor_pos_++) { | |
257 if (IsPositionAtWordSelectionBoundary(cursor_pos_)) | |
258 break; | |
259 } | |
260 } | |
261 | |
227 void TextfieldViewsModel::ClearSelection() { | 262 void TextfieldViewsModel::ClearSelection() { |
228 selection_begin_ = cursor_pos_; | 263 selection_begin_ = cursor_pos_; |
229 } | 264 } |
230 | 265 |
231 bool TextfieldViewsModel::Cut() { | 266 bool TextfieldViewsModel::Cut() { |
232 if (HasSelection()) { | 267 if (HasSelection()) { |
233 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate | 268 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate |
234 ->GetClipboard()).WriteText(GetSelectedText()); | 269 ->GetClipboard()).WriteText(GetSelectedText()); |
235 DeleteSelection(); | 270 DeleteSelection(); |
236 return true; | 271 return true; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 } | 309 } |
275 | 310 |
276 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { | 311 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { |
277 DCHECK(end >= begin); | 312 DCHECK(end >= begin); |
278 if (is_password_) { | 313 if (is_password_) { |
279 return string16(end - begin, '*'); | 314 return string16(end - begin, '*'); |
280 } | 315 } |
281 return text_.substr(begin, end - begin); | 316 return text_.substr(begin, end - begin); |
282 } | 317 } |
283 | 318 |
319 bool TextfieldViewsModel::IsPositionAtWordSelectionBoundary(size_t pos) { | |
320 return (isalnum(text_[pos - 1]) && !isalnum(text_[pos])) || | |
321 (!isalnum(text_[pos - 1]) && isalnum(text_[pos])); | |
322 } | |
323 | |
284 } // namespace views | 324 } // namespace views |
OLD | NEW |