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() && !render_text_->is_obscured()) { |
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(), |
518 render_text_->GetSelectionStart())); | 513 render_text_->GetSelectionStart())); |
519 DeleteSelection(); | 514 DeleteSelection(); |
520 return true; | 515 return true; |
521 } | 516 } |
522 return false; | 517 return false; |
523 } | 518 } |
524 | 519 |
525 void TextfieldViewsModel::Copy() { | 520 void TextfieldViewsModel::Copy() { |
526 if (!HasCompositionText() && HasSelection()) { | 521 if (!HasCompositionText() && HasSelection() && !render_text_->is_obscured()) { |
527 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate | 522 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate |
528 ->GetClipboard()).WriteText(GetSelectedText()); | 523 ->GetClipboard()).WriteText(GetSelectedText()); |
529 } | 524 } |
530 } | 525 } |
531 | 526 |
532 bool TextfieldViewsModel::Paste() { | 527 bool TextfieldViewsModel::Paste() { |
533 string16 result; | 528 string16 result; |
534 views::ViewsDelegate::views_delegate->GetClipboard() | 529 views::ViewsDelegate::views_delegate->GetClipboard() |
535 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &result); | 530 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &result); |
536 if (!result.empty()) { | 531 if (!result.empty()) { |
(...skipping 95 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 |