| 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 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 | 270 |
| 271 ///////////////////////////////////////////////////////////////// | 271 ///////////////////////////////////////////////////////////////// |
| 272 // TextfieldViewsModel: public | 272 // TextfieldViewsModel: public |
| 273 | 273 |
| 274 TextfieldViewsModel::Delegate::~Delegate() { | 274 TextfieldViewsModel::Delegate::~Delegate() { |
| 275 } | 275 } |
| 276 | 276 |
| 277 TextfieldViewsModel::TextfieldViewsModel(Delegate* delegate) | 277 TextfieldViewsModel::TextfieldViewsModel(Delegate* delegate) |
| 278 : delegate_(delegate), | 278 : delegate_(delegate), |
| 279 render_text_(gfx::RenderText::CreateRenderText()), | 279 render_text_(gfx::RenderText::CreateRenderText()), |
| 280 is_password_(false), | |
| 281 current_edit_(edit_history_.end()) { | 280 current_edit_(edit_history_.end()) { |
| 282 } | 281 } |
| 283 | 282 |
| 284 TextfieldViewsModel::~TextfieldViewsModel() { | 283 TextfieldViewsModel::~TextfieldViewsModel() { |
| 285 ClearEditHistory(); | 284 ClearEditHistory(); |
| 286 ClearComposition(); | 285 ClearComposition(); |
| 287 } | 286 } |
| 288 | 287 |
| 289 const string16& TextfieldViewsModel::GetText() const { | 288 const string16& TextfieldViewsModel::GetText() const { |
| 290 return render_text_->text(); | 289 return render_text_->text(); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 if (current_edit_ == edit_history_.end()) | 493 if (current_edit_ == edit_history_.end()) |
| 495 current_edit_ = edit_history_.begin(); | 494 current_edit_ = edit_history_.begin(); |
| 496 else | 495 else |
| 497 current_edit_ ++; | 496 current_edit_ ++; |
| 498 string16 old = GetText(); | 497 string16 old = GetText(); |
| 499 size_t old_cursor = GetCursorPosition(); | 498 size_t old_cursor = GetCursorPosition(); |
| 500 (*current_edit_)->Redo(this); | 499 (*current_edit_)->Redo(this); |
| 501 return old != GetText() || old_cursor != GetCursorPosition(); | 500 return old != GetText() || old_cursor != GetCursorPosition(); |
| 502 } | 501 } |
| 503 | 502 |
| 504 string16 TextfieldViewsModel::GetVisibleText() const { | |
| 505 return GetVisibleText(0U, GetText().length()); | |
| 506 } | |
| 507 | |
| 508 bool TextfieldViewsModel::Cut() { | 503 bool TextfieldViewsModel::Cut() { |
| 509 if (!HasCompositionText() && HasSelection()) { | 504 if (!HasCompositionText() && HasSelection()) { |
| 510 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate | 505 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate |
| 511 ->GetClipboard()).WriteText(GetSelectedText()); | 506 ->GetClipboard()).WriteText(GetSelectedText()); |
| 512 // A trick to let undo/redo handle cursor correctly. | 507 // A trick to let undo/redo handle cursor correctly. |
| 513 // Undoing CUT moves the cursor to the end of the change rather | 508 // Undoing CUT moves the cursor to the end of the change rather |
| 514 // than beginning, unlike Delete/Backspace. | 509 // than beginning, unlike Delete/Backspace. |
| 515 // TODO(oshima): Change Delete/Backspace to use DeleteSelection, | 510 // TODO(oshima): Change Delete/Backspace to use DeleteSelection, |
| 516 // update DeleteEdit and remove this trick. | 511 // update DeleteEdit and remove this trick. |
| 517 render_text_->SelectRange(ui::Range(render_text_->GetCursorPosition(), | 512 render_text_->SelectRange(ui::Range(render_text_->GetCursorPosition(), |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 *range = ui::Range(render_text_->GetCompositionRange()); | 627 *range = ui::Range(render_text_->GetCompositionRange()); |
| 633 } | 628 } |
| 634 | 629 |
| 635 bool TextfieldViewsModel::HasCompositionText() const { | 630 bool TextfieldViewsModel::HasCompositionText() const { |
| 636 return !render_text_->GetCompositionRange().is_empty(); | 631 return !render_text_->GetCompositionRange().is_empty(); |
| 637 } | 632 } |
| 638 | 633 |
| 639 ///////////////////////////////////////////////////////////////// | 634 ///////////////////////////////////////////////////////////////// |
| 640 // TextfieldViewsModel: private | 635 // TextfieldViewsModel: private |
| 641 | 636 |
| 642 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { | |
| 643 DCHECK(end >= begin); | |
| 644 if (is_password_) | |
| 645 return string16(end - begin, '*'); | |
| 646 return GetText().substr(begin, end - begin); | |
| 647 } | |
| 648 | |
| 649 void TextfieldViewsModel::InsertTextInternal(const string16& text, | 637 void TextfieldViewsModel::InsertTextInternal(const string16& text, |
| 650 bool mergeable) { | 638 bool mergeable) { |
| 651 if (HasCompositionText()) { | 639 if (HasCompositionText()) { |
| 652 CancelCompositionText(); | 640 CancelCompositionText(); |
| 653 ExecuteAndRecordInsert(text, mergeable); | 641 ExecuteAndRecordInsert(text, mergeable); |
| 654 } else if (HasSelection()) { | 642 } else if (HasSelection()) { |
| 655 ExecuteAndRecordReplaceSelection(mergeable ? MERGEABLE : DO_NOT_MERGE, | 643 ExecuteAndRecordReplaceSelection(mergeable ? MERGEABLE : DO_NOT_MERGE, |
| 656 text); | 644 text); |
| 657 } else { | 645 } else { |
| 658 ExecuteAndRecordInsert(text, mergeable); | 646 ExecuteAndRecordInsert(text, mergeable); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 786 if (delete_from != delete_to) | 774 if (delete_from != delete_to) |
| 787 render_text_->SetText(text.erase(delete_from, delete_to - delete_from)); | 775 render_text_->SetText(text.erase(delete_from, delete_to - delete_from)); |
| 788 if (!new_text.empty()) | 776 if (!new_text.empty()) |
| 789 render_text_->SetText(text.insert(new_text_insert_at, new_text)); | 777 render_text_->SetText(text.insert(new_text_insert_at, new_text)); |
| 790 render_text_->SetCursorPosition(new_cursor_pos); | 778 render_text_->SetCursorPosition(new_cursor_pos); |
| 791 // TODO(oshima): mac selects the text that is just undone (but gtk doesn't). | 779 // TODO(oshima): mac selects the text that is just undone (but gtk doesn't). |
| 792 // This looks fine feature and we may want to do the same. | 780 // This looks fine feature and we may want to do the same. |
| 793 } | 781 } |
| 794 | 782 |
| 795 } // namespace views | 783 } // namespace views |
| OLD | NEW |