 Chromium Code Reviews
 Chromium Code Reviews Issue 6004010:
  Implement clipboard features in views textfield.  (Closed) 
  Base URL: http://git.chromium.org/git/chromium.git@trunk
    
  
    Issue 6004010:
  Implement clipboard features in views textfield.  (Closed) 
  Base URL: http://git.chromium.org/git/chromium.git@trunk| 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::HandleClipboardEvents(app::KeyboardCode key_code) { | |
| 
rjkroege
2011/01/05 15:31:22
please remember that cut&paste is also invokable f
 
varunjain
2011/01/05 16:19:31
Done.
 | |
| 230 Clipboard* clipboard = views::ViewsDelegate::views_delegate->GetClipboard(); | |
| 231 bool text_changed = false; | |
| 232 string16 result; | |
| 233 switch (key_code) { | |
| 234 case app::VKEY_C: | |
| 235 if (HasSelection()) { | |
| 236 ScopedClipboardWriter scw(clipboard); | |
| 237 scw.WriteText(GetSelectedText()); | |
| 238 } | |
| 239 break; | |
| 240 case app::VKEY_X: | |
| 241 if (HasSelection()) { | |
| 242 ScopedClipboardWriter scw(clipboard); | |
| 243 scw.WriteText(GetSelectedText()); | |
| 244 DeleteSelection(); | |
| 245 text_changed = true; | |
| 246 } | |
| 247 break; | |
| 248 case app::VKEY_V: | |
| 249 clipboard->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 text_changed = true; | |
| 257 } | |
| 258 break; | |
| 259 default: | |
| 260 break; | |
| 261 } | |
| 262 return text_changed; | |
| 263 } | |
| 264 | |
| 226 bool TextfieldViewsModel::HasSelection() const { | 265 bool TextfieldViewsModel::HasSelection() const { | 
| 227 return selection_begin_ != cursor_pos_; | 266 return selection_begin_ != cursor_pos_; | 
| 228 } | 267 } | 
| 229 | 268 | 
| 230 void TextfieldViewsModel::DeleteSelection() { | 269 void TextfieldViewsModel::DeleteSelection() { | 
| 231 DCHECK(HasSelection()); | 270 DCHECK(HasSelection()); | 
| 232 size_t n = std::abs(static_cast<long>(cursor_pos_ - selection_begin_)); | 271 size_t n = std::abs(static_cast<long>(cursor_pos_ - selection_begin_)); | 
| 233 size_t begin = std::min(cursor_pos_, selection_begin_); | 272 size_t begin = std::min(cursor_pos_, selection_begin_); | 
| 234 text_.erase(begin, n); | 273 text_.erase(begin, n); | 
| 235 cursor_pos_ = begin; | 274 cursor_pos_ = begin; | 
| 236 ClearSelection(); | 275 ClearSelection(); | 
| 237 } | 276 } | 
| 238 | 277 | 
| 239 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { | 278 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { | 
| 240 DCHECK(end >= begin); | 279 DCHECK(end >= begin); | 
| 241 if (is_password_) { | 280 if (is_password_) { | 
| 242 return string16(end - begin, '*'); | 281 return string16(end - begin, '*'); | 
| 243 } | 282 } | 
| 244 return text_.substr(begin, end - begin); | 283 return text_.substr(begin, end - begin); | 
| 245 } | 284 } | 
| 246 | 285 | 
| 247 } // namespace views | 286 } // namespace views | 
| OLD | NEW |