| 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 "ui/views/controls/textfield/textfield_views_model.h" | 5 #include "ui/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 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 | 274 |
| 275 ///////////////////////////////////////////////////////////////// | 275 ///////////////////////////////////////////////////////////////// |
| 276 // TextfieldViewsModel: public | 276 // TextfieldViewsModel: public |
| 277 | 277 |
| 278 TextfieldViewsModel::Delegate::~Delegate() { | 278 TextfieldViewsModel::Delegate::~Delegate() { |
| 279 } | 279 } |
| 280 | 280 |
| 281 TextfieldViewsModel::TextfieldViewsModel(Delegate* delegate) | 281 TextfieldViewsModel::TextfieldViewsModel(Delegate* delegate) |
| 282 : delegate_(delegate), | 282 : delegate_(delegate), |
| 283 render_text_(gfx::RenderText::CreateRenderText()), | 283 render_text_(gfx::RenderText::CreateRenderText()), |
| 284 is_password_(false), | |
| 285 current_edit_(edit_history_.end()) { | 284 current_edit_(edit_history_.end()) { |
| 286 } | 285 } |
| 287 | 286 |
| 288 TextfieldViewsModel::~TextfieldViewsModel() { | 287 TextfieldViewsModel::~TextfieldViewsModel() { |
| 289 ClearEditHistory(); | 288 ClearEditHistory(); |
| 290 ClearComposition(); | 289 ClearComposition(); |
| 291 } | 290 } |
| 292 | 291 |
| 293 const string16& TextfieldViewsModel::GetText() const { | 292 const string16& TextfieldViewsModel::GetText() const { |
| 294 return render_text_->text(); | 293 return render_text_->text(); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 if (current_edit_ == edit_history_.end()) | 497 if (current_edit_ == edit_history_.end()) |
| 499 current_edit_ = edit_history_.begin(); | 498 current_edit_ = edit_history_.begin(); |
| 500 else | 499 else |
| 501 current_edit_ ++; | 500 current_edit_ ++; |
| 502 string16 old = GetText(); | 501 string16 old = GetText(); |
| 503 size_t old_cursor = GetCursorPosition(); | 502 size_t old_cursor = GetCursorPosition(); |
| 504 (*current_edit_)->Redo(this); | 503 (*current_edit_)->Redo(this); |
| 505 return old != GetText() || old_cursor != GetCursorPosition(); | 504 return old != GetText() || old_cursor != GetCursorPosition(); |
| 506 } | 505 } |
| 507 | 506 |
| 508 string16 TextfieldViewsModel::GetVisibleText() const { | |
| 509 return GetVisibleText(0U, GetText().length()); | |
| 510 } | |
| 511 | |
| 512 bool TextfieldViewsModel::Cut() { | 507 bool TextfieldViewsModel::Cut() { |
| 513 if (!HasCompositionText() && HasSelection()) { | 508 if (!HasCompositionText() && HasSelection() && !render_text_->is_obscured()) { |
| 514 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate | 509 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate |
| 515 ->GetClipboard()).WriteText(GetSelectedText()); | 510 ->GetClipboard()).WriteText(GetSelectedText()); |
| 516 // A trick to let undo/redo handle cursor correctly. | 511 // A trick to let undo/redo handle cursor correctly. |
| 517 // Undoing CUT moves the cursor to the end of the change rather | 512 // Undoing CUT moves the cursor to the end of the change rather |
| 518 // than beginning, unlike Delete/Backspace. | 513 // than beginning, unlike Delete/Backspace. |
| 519 // TODO(oshima): Change Delete/Backspace to use DeleteSelection, | 514 // TODO(oshima): Change Delete/Backspace to use DeleteSelection, |
| 520 // update DeleteEdit and remove this trick. | 515 // update DeleteEdit and remove this trick. |
| 521 render_text_->SelectRange(ui::Range(render_text_->GetCursorPosition(), | 516 render_text_->SelectRange(ui::Range(render_text_->GetCursorPosition(), |
| 522 render_text_->GetSelectionStart())); | 517 render_text_->GetSelectionStart())); |
| 523 DeleteSelection(); | 518 DeleteSelection(); |
| 524 return true; | 519 return true; |
| 525 } | 520 } |
| 526 return false; | 521 return false; |
| 527 } | 522 } |
| 528 | 523 |
| 529 void TextfieldViewsModel::Copy() { | 524 void TextfieldViewsModel::Copy() { |
| 530 if (!HasCompositionText() && HasSelection()) { | 525 if (!HasCompositionText() && HasSelection() && !render_text_->is_obscured()) { |
| 531 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate | 526 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate |
| 532 ->GetClipboard()).WriteText(GetSelectedText()); | 527 ->GetClipboard()).WriteText(GetSelectedText()); |
| 533 } | 528 } |
| 534 } | 529 } |
| 535 | 530 |
| 536 bool TextfieldViewsModel::Paste() { | 531 bool TextfieldViewsModel::Paste() { |
| 537 string16 result; | 532 string16 result; |
| 538 views::ViewsDelegate::views_delegate->GetClipboard() | 533 views::ViewsDelegate::views_delegate->GetClipboard() |
| 539 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &result); | 534 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &result); |
| 540 if (!result.empty()) { | 535 if (!result.empty()) { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 *range = ui::Range(render_text_->GetCompositionRange()); | 631 *range = ui::Range(render_text_->GetCompositionRange()); |
| 637 } | 632 } |
| 638 | 633 |
| 639 bool TextfieldViewsModel::HasCompositionText() const { | 634 bool TextfieldViewsModel::HasCompositionText() const { |
| 640 return !render_text_->GetCompositionRange().is_empty(); | 635 return !render_text_->GetCompositionRange().is_empty(); |
| 641 } | 636 } |
| 642 | 637 |
| 643 ///////////////////////////////////////////////////////////////// | 638 ///////////////////////////////////////////////////////////////// |
| 644 // TextfieldViewsModel: private | 639 // TextfieldViewsModel: private |
| 645 | 640 |
| 646 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { | |
| 647 DCHECK(end >= begin); | |
| 648 if (is_password_) | |
| 649 return string16(end - begin, '*'); | |
| 650 return GetText().substr(begin, end - begin); | |
| 651 } | |
| 652 | |
| 653 void TextfieldViewsModel::InsertTextInternal(const string16& text, | 641 void TextfieldViewsModel::InsertTextInternal(const string16& text, |
| 654 bool mergeable) { | 642 bool mergeable) { |
| 655 if (HasCompositionText()) { | 643 if (HasCompositionText()) { |
| 656 CancelCompositionText(); | 644 CancelCompositionText(); |
| 657 ExecuteAndRecordInsert(text, mergeable); | 645 ExecuteAndRecordInsert(text, mergeable); |
| 658 } else if (HasSelection()) { | 646 } else if (HasSelection()) { |
| 659 ExecuteAndRecordReplaceSelection(mergeable ? MERGEABLE : DO_NOT_MERGE, | 647 ExecuteAndRecordReplaceSelection(mergeable ? MERGEABLE : DO_NOT_MERGE, |
| 660 text); | 648 text); |
| 661 } else { | 649 } else { |
| 662 ExecuteAndRecordInsert(text, mergeable); | 650 ExecuteAndRecordInsert(text, mergeable); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 if (delete_from != delete_to) | 778 if (delete_from != delete_to) |
| 791 render_text_->SetText(text.erase(delete_from, delete_to - delete_from)); | 779 render_text_->SetText(text.erase(delete_from, delete_to - delete_from)); |
| 792 if (!new_text.empty()) | 780 if (!new_text.empty()) |
| 793 render_text_->SetText(text.insert(new_text_insert_at, new_text)); | 781 render_text_->SetText(text.insert(new_text_insert_at, new_text)); |
| 794 render_text_->SetCursorPosition(new_cursor_pos); | 782 render_text_->SetCursorPosition(new_cursor_pos); |
| 795 // TODO(oshima): mac selects the text that is just undone (but gtk doesn't). | 783 // TODO(oshima): mac selects the text that is just undone (but gtk doesn't). |
| 796 // This looks fine feature and we may want to do the same. | 784 // This looks fine feature and we may want to do the same. |
| 797 } | 785 } |
| 798 | 786 |
| 799 } // namespace views | 787 } // namespace views |
| OLD | NEW |