OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 selection_begin_ = GetSafePosition(range.start()); | 228 selection_begin_ = GetSafePosition(range.start()); |
229 cursor_pos_ = GetSafePosition(range.end()); | 229 cursor_pos_ = GetSafePosition(range.end()); |
230 } | 230 } |
231 | 231 |
232 void TextfieldViewsModel::SelectAll() { | 232 void TextfieldViewsModel::SelectAll() { |
233 // SelectAll selects towards the end. | 233 // SelectAll selects towards the end. |
234 cursor_pos_ = text_.length(); | 234 cursor_pos_ = text_.length(); |
235 selection_begin_ = 0; | 235 selection_begin_ = 0; |
236 } | 236 } |
237 | 237 |
| 238 void TextfieldViewsModel::SelectWord() { |
| 239 // First we setup selection_begin_ and cursor_pos_. There are so many cases |
| 240 // because we try to emulate what select-word looks like in a gtk textfield. |
| 241 // See associated testcase for different cases. |
| 242 if (cursor_pos_ > 0 && cursor_pos_ < text_.length()) { |
| 243 if (isalnum(text_[cursor_pos_])) { |
| 244 selection_begin_ = cursor_pos_; |
| 245 cursor_pos_++; |
| 246 } else |
| 247 selection_begin_ = cursor_pos_ - 1; |
| 248 } else if (cursor_pos_ == 0) { |
| 249 selection_begin_ = cursor_pos_; |
| 250 if (text_.length() > 0) |
| 251 cursor_pos_++; |
| 252 } else { |
| 253 selection_begin_ = cursor_pos_ - 1; |
| 254 } |
| 255 |
| 256 // Now we move selection_begin_ to beginning of selection. Selection boundary |
| 257 // is defined as the position where we have alpha-num character on one side |
| 258 // and non-alpha-num char on the other side. |
| 259 for (; selection_begin_ > 0; selection_begin_--) { |
| 260 if (IsPositionAtWordSelectionBoundary(selection_begin_)) |
| 261 break; |
| 262 } |
| 263 |
| 264 // Now we move cursor_pos_ to end of selection. Selection boundary |
| 265 // is defined as the position where we have alpha-num character on one side |
| 266 // and non-alpha-num char on the other side. |
| 267 for (; cursor_pos_ < text_.length(); cursor_pos_++) { |
| 268 if (IsPositionAtWordSelectionBoundary(cursor_pos_)) |
| 269 break; |
| 270 } |
| 271 } |
| 272 |
238 void TextfieldViewsModel::ClearSelection() { | 273 void TextfieldViewsModel::ClearSelection() { |
239 selection_begin_ = cursor_pos_; | 274 selection_begin_ = cursor_pos_; |
240 } | 275 } |
241 | 276 |
242 bool TextfieldViewsModel::Cut() { | 277 bool TextfieldViewsModel::Cut() { |
243 if (HasSelection()) { | 278 if (HasSelection()) { |
244 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate | 279 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate |
245 ->GetClipboard()).WriteText(GetSelectedText()); | 280 ->GetClipboard()).WriteText(GetSelectedText()); |
246 DeleteSelection(); | 281 DeleteSelection(); |
247 return true; | 282 return true; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 } | 320 } |
286 | 321 |
287 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { | 322 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { |
288 DCHECK(end >= begin); | 323 DCHECK(end >= begin); |
289 if (is_password_) { | 324 if (is_password_) { |
290 return string16(end - begin, '*'); | 325 return string16(end - begin, '*'); |
291 } | 326 } |
292 return text_.substr(begin, end - begin); | 327 return text_.substr(begin, end - begin); |
293 } | 328 } |
294 | 329 |
| 330 bool TextfieldViewsModel::IsPositionAtWordSelectionBoundary(size_t pos) { |
| 331 return (isalnum(text_[pos - 1]) && !isalnum(text_[pos])) || |
| 332 (!isalnum(text_[pos - 1]) && isalnum(text_[pos])); |
| 333 } |
| 334 |
295 size_t TextfieldViewsModel::GetSafePosition(size_t position) const { | 335 size_t TextfieldViewsModel::GetSafePosition(size_t position) const { |
296 if (position > text_.length()) { | 336 if (position > text_.length()) { |
297 return text_.length(); | 337 return text_.length(); |
298 } | 338 } |
299 return position; | 339 return position; |
300 } | 340 } |
301 | 341 |
302 } // namespace views | 342 } // namespace views |
OLD | NEW |