Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: views/controls/textfield/textfield_views_model.cc

Issue 6318004: Add TextRange and related methods to Textfield Views. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 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"
11 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #include "gfx/font.h" 12 #include "gfx/font.h"
13 #include "ui/base/clipboard/clipboard.h" 13 #include "ui/base/clipboard/clipboard.h"
14 #include "ui/base/clipboard/scoped_clipboard_writer.h" 14 #include "ui/base/clipboard/scoped_clipboard_writer.h"
15 #include "views/views_delegate.h" 15 #include "views/views_delegate.h"
16 #include "views/controls/textfield/textfield.h"
16 17
17 namespace views { 18 namespace views {
18 19
19 TextfieldViewsModel::TextfieldViewsModel() 20 TextfieldViewsModel::TextfieldViewsModel()
20 : cursor_pos_(0), 21 : cursor_pos_(0),
21 selection_begin_(0), 22 selection_begin_(0),
22 is_password_(false) { 23 is_password_(false) {
23 } 24 }
24 25
25 TextfieldViewsModel::~TextfieldViewsModel() { 26 TextfieldViewsModel::~TextfieldViewsModel() {
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 return gfx::Rect(x, 0, x_end - x, h); 213 return gfx::Rect(x, 0, x_end - x, h);
213 } 214 }
214 } 215 }
215 216
216 string16 TextfieldViewsModel::GetSelectedText() const { 217 string16 TextfieldViewsModel::GetSelectedText() const {
217 return text_.substr( 218 return text_.substr(
218 std::min(cursor_pos_, selection_begin_), 219 std::min(cursor_pos_, selection_begin_),
219 std::abs(static_cast<long>(cursor_pos_ - selection_begin_))); 220 std::abs(static_cast<long>(cursor_pos_ - selection_begin_)));
220 } 221 }
221 222
223 void TextfieldViewsModel::GetSelectedRange(TextRange* range) const {
224 range->SetRange(selection_begin_, cursor_pos_);
225 }
226
227 void TextfieldViewsModel::SelectRange(const TextRange& range) {
228 selection_begin_ = GetSafePosition(range.start());
229 cursor_pos_ = GetSafePosition(range.end());
230 }
231
222 void TextfieldViewsModel::SelectAll() { 232 void TextfieldViewsModel::SelectAll() {
223 cursor_pos_ = 0; 233 cursor_pos_ = 0;
224 selection_begin_ = text_.length(); 234 selection_begin_ = text_.length();
225 } 235 }
226 236
227 void TextfieldViewsModel::ClearSelection() { 237 void TextfieldViewsModel::ClearSelection() {
228 selection_begin_ = cursor_pos_; 238 selection_begin_ = cursor_pos_;
229 } 239 }
230 240
231 bool TextfieldViewsModel::Cut() { 241 bool TextfieldViewsModel::Cut() {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 } 284 }
275 285
276 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { 286 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const {
277 DCHECK(end >= begin); 287 DCHECK(end >= begin);
278 if (is_password_) { 288 if (is_password_) {
279 return string16(end - begin, '*'); 289 return string16(end - begin, '*');
280 } 290 }
281 return text_.substr(begin, end - begin); 291 return text_.substr(begin, end - begin);
282 } 292 }
283 293
294 size_t TextfieldViewsModel::GetSafePosition(size_t position) const {
295 if (position > text_.length()) {
296 return text_.length();
297 }
298 return position;
299 }
300
284 } // namespace views 301 } // namespace views
OLDNEW
« no previous file with comments | « views/controls/textfield/textfield_views_model.h ('k') | views/controls/textfield/textfield_views_model_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698