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 "app/clipboard/clipboard.h" |
| 10 #include "app/clipboard/scoped_clipboard_writer.h" |
9 #include "base/i18n/break_iterator.h" | 11 #include "base/i18n/break_iterator.h" |
10 #include "base/logging.h" | 12 #include "base/logging.h" |
11 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
12 #include "gfx/font.h" | 14 #include "gfx/font.h" |
| 15 #include "views/views_delegate.h" |
13 | 16 |
14 namespace views { | 17 namespace views { |
15 | 18 |
16 TextfieldViewsModel::TextfieldViewsModel() | 19 TextfieldViewsModel::TextfieldViewsModel() |
17 : cursor_pos_(0), | 20 : cursor_pos_(0), |
18 selection_begin_(0), | 21 selection_begin_(0), |
19 is_password_(false) { | 22 is_password_(false) { |
20 } | 23 } |
21 | 24 |
22 TextfieldViewsModel::~TextfieldViewsModel() { | 25 TextfieldViewsModel::~TextfieldViewsModel() { |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 | 219 |
217 void TextfieldViewsModel::SelectAll() { | 220 void TextfieldViewsModel::SelectAll() { |
218 cursor_pos_ = 0; | 221 cursor_pos_ = 0; |
219 selection_begin_ = text_.length(); | 222 selection_begin_ = text_.length(); |
220 } | 223 } |
221 | 224 |
222 void TextfieldViewsModel::ClearSelection() { | 225 void TextfieldViewsModel::ClearSelection() { |
223 selection_begin_ = cursor_pos_; | 226 selection_begin_ = cursor_pos_; |
224 } | 227 } |
225 | 228 |
| 229 bool TextfieldViewsModel::Cut() { |
| 230 if (HasSelection()) { |
| 231 ScopedClipboardWriter(views::ViewsDelegate::views_delegate->GetClipboard()) |
| 232 .WriteText(GetSelectedText()); |
| 233 DeleteSelection(); |
| 234 return true; |
| 235 } |
| 236 return false; |
| 237 } |
| 238 |
| 239 void TextfieldViewsModel::Copy() { |
| 240 if (HasSelection()) { |
| 241 ScopedClipboardWriter(views::ViewsDelegate::views_delegate->GetClipboard()) |
| 242 .WriteText(GetSelectedText()); |
| 243 } |
| 244 } |
| 245 |
| 246 bool TextfieldViewsModel::Paste() { |
| 247 string16 result; |
| 248 views::ViewsDelegate::views_delegate->GetClipboard() |
| 249 ->ReadText(Clipboard::BUFFER_STANDARD, &result); |
| 250 if (!result.empty()) { |
| 251 if (HasSelection()) |
| 252 DeleteSelection(); |
| 253 text_.insert(cursor_pos_, result); |
| 254 cursor_pos_ += result.length(); |
| 255 ClearSelection(); |
| 256 return true; |
| 257 } |
| 258 return false; |
| 259 } |
| 260 |
226 bool TextfieldViewsModel::HasSelection() const { | 261 bool TextfieldViewsModel::HasSelection() const { |
227 return selection_begin_ != cursor_pos_; | 262 return selection_begin_ != cursor_pos_; |
228 } | 263 } |
229 | 264 |
230 void TextfieldViewsModel::DeleteSelection() { | 265 void TextfieldViewsModel::DeleteSelection() { |
231 DCHECK(HasSelection()); | 266 DCHECK(HasSelection()); |
232 size_t n = std::abs(static_cast<long>(cursor_pos_ - selection_begin_)); | 267 size_t n = std::abs(static_cast<long>(cursor_pos_ - selection_begin_)); |
233 size_t begin = std::min(cursor_pos_, selection_begin_); | 268 size_t begin = std::min(cursor_pos_, selection_begin_); |
234 text_.erase(begin, n); | 269 text_.erase(begin, n); |
235 cursor_pos_ = begin; | 270 cursor_pos_ = begin; |
236 ClearSelection(); | 271 ClearSelection(); |
237 } | 272 } |
238 | 273 |
239 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { | 274 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { |
240 DCHECK(end >= begin); | 275 DCHECK(end >= begin); |
241 if (is_password_) { | 276 if (is_password_) { |
242 return string16(end - begin, '*'); | 277 return string16(end - begin, '*'); |
243 } | 278 } |
244 return text_.substr(begin, end - begin); | 279 return text_.substr(begin, end - begin); |
245 } | 280 } |
246 | 281 |
247 } // namespace views | 282 } // namespace views |
OLD | NEW |