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 "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 "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 render_text_->MoveCursorLeft(break_type, select); | 373 render_text_->MoveCursorLeft(break_type, select); |
374 } | 374 } |
375 | 375 |
376 void TextfieldViewsModel::MoveCursorRight(gfx::BreakType break_type, | 376 void TextfieldViewsModel::MoveCursorRight(gfx::BreakType break_type, |
377 bool select) { | 377 bool select) { |
378 if (HasCompositionText()) | 378 if (HasCompositionText()) |
379 ConfirmCompositionText(); | 379 ConfirmCompositionText(); |
380 render_text_->MoveCursorRight(break_type, select); | 380 render_text_->MoveCursorRight(break_type, select); |
381 } | 381 } |
382 | 382 |
383 bool TextfieldViewsModel::MoveCursorTo(size_t pos, bool select) { | 383 bool TextfieldViewsModel::MoveCursorTo(const gfx::SelectionModel& selection, |
msw
2011/08/01 05:02:23
We'll need to reconsider this function similarly t
| |
384 bool select) { | |
384 if (HasCompositionText()) | 385 if (HasCompositionText()) |
385 ConfirmCompositionText(); | 386 ConfirmCompositionText(); |
386 return render_text_->MoveCursorTo(pos, select); | 387 return render_text_->MoveCursorTo(selection, select); |
387 } | 388 } |
388 | 389 |
389 bool TextfieldViewsModel::MoveCursorTo(const gfx::Point& point, bool select) { | 390 bool TextfieldViewsModel::MoveCursorTo(const gfx::Point& point, bool select) { |
390 if (HasCompositionText()) | 391 if (HasCompositionText()) |
391 ConfirmCompositionText(); | 392 ConfirmCompositionText(); |
392 return render_text_->MoveCursorTo(point, select); | 393 return render_text_->MoveCursorTo(point, select); |
393 } | 394 } |
394 | 395 |
395 std::vector<gfx::Rect> TextfieldViewsModel::GetSelectionBounds() const { | 396 std::vector<gfx::Rect> TextfieldViewsModel::GetSelectionBounds() const { |
396 return render_text_->GetSubstringBounds(render_text_->GetSelection()); | 397 return render_text_->GetSubstringBounds(render_text_->GetSelectionStart(), |
398 render_text_->GetCursorPosition()); | |
397 } | 399 } |
398 | 400 |
399 string16 TextfieldViewsModel::GetSelectedText() const { | 401 string16 TextfieldViewsModel::GetSelectedText() const { |
400 ui::Range selection = render_text_->GetSelection(); | 402 return GetText().substr(render_text_->MinOfSelection(), |
401 return GetText().substr(selection.GetMin(), selection.length()); | 403 std::abs(static_cast<long>(render_text_->GetCursorPosition() - |
msw
2011/08/01 05:02:23
why the static cast to long? Shouldn't this be a s
xji
2011/08/01 23:38:42
is (size_t - size_t) a size_t? then, you need to c
msw
2011/08/02 01:29:06
The call to std::abs implicitly casts the subtract
| |
404 render_text_->GetSelectionStart()))); | |
402 } | 405 } |
403 | 406 |
404 void TextfieldViewsModel::GetSelectedRange(ui::Range* range) const { | 407 void TextfieldViewsModel::GetSelectedRange(ui::Range* range) const { |
405 *range = render_text_->GetSelection(); | 408 *range = ui::Range(render_text_->GetSelectionStart(), |
409 render_text_->GetCursorPosition()); | |
406 } | 410 } |
407 | 411 |
408 void TextfieldViewsModel::SelectRange(const ui::Range& range) { | 412 void TextfieldViewsModel::SelectRange(const ui::Range& range) { |
409 if (HasCompositionText()) | 413 if (HasCompositionText()) |
410 ConfirmCompositionText(); | 414 ConfirmCompositionText(); |
411 render_text_->SetSelection(range); | 415 render_text_->SetSelection(range.start(), range.end()); |
412 } | 416 } |
413 | 417 |
414 void TextfieldViewsModel::SelectAll() { | 418 void TextfieldViewsModel::SelectAll() { |
415 if (HasCompositionText()) | 419 if (HasCompositionText()) |
416 ConfirmCompositionText(); | 420 ConfirmCompositionText(); |
417 render_text_->SelectAll(); | 421 render_text_->SelectAll(); |
418 } | 422 } |
419 | 423 |
420 void TextfieldViewsModel::SelectWord() { | 424 void TextfieldViewsModel::SelectWord() { |
421 if (HasCompositionText()) | 425 if (HasCompositionText()) |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
485 | 489 |
486 bool TextfieldViewsModel::Cut() { | 490 bool TextfieldViewsModel::Cut() { |
487 if (!HasCompositionText() && HasSelection()) { | 491 if (!HasCompositionText() && HasSelection()) { |
488 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate | 492 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate |
489 ->GetClipboard()).WriteText(GetSelectedText()); | 493 ->GetClipboard()).WriteText(GetSelectedText()); |
490 // A trick to let undo/redo handle cursor correctly. | 494 // A trick to let undo/redo handle cursor correctly. |
491 // Undoing CUT moves the cursor to the end of the change rather | 495 // Undoing CUT moves the cursor to the end of the change rather |
492 // than beginning, unlike Delete/Backspace. | 496 // than beginning, unlike Delete/Backspace. |
493 // TODO(oshima): Change Delete/Backspace to use DeleteSelection, | 497 // TODO(oshima): Change Delete/Backspace to use DeleteSelection, |
494 // update DeleteEdit and remove this trick. | 498 // update DeleteEdit and remove this trick. |
495 ui::Range selection = render_text_->GetSelection(); | 499 render_text_->SetSelection(render_text_->GetCursorPosition(), |
496 render_text_->SetSelection(ui::Range(selection.end(), selection.start())); | 500 render_text_->GetSelectionStart()); |
497 DeleteSelection(); | 501 DeleteSelection(); |
498 return true; | 502 return true; |
499 } | 503 } |
500 return false; | 504 return false; |
501 } | 505 } |
502 | 506 |
503 void TextfieldViewsModel::Copy() { | 507 void TextfieldViewsModel::Copy() { |
504 if (!HasCompositionText() && HasSelection()) { | 508 if (!HasCompositionText() && HasSelection()) { |
505 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate | 509 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate |
506 ->GetClipboard()).WriteText(GetSelectedText()); | 510 ->GetClipboard()).WriteText(GetSelectedText()); |
507 } | 511 } |
508 } | 512 } |
509 | 513 |
510 bool TextfieldViewsModel::Paste() { | 514 bool TextfieldViewsModel::Paste() { |
511 string16 result; | 515 string16 result; |
512 views::ViewsDelegate::views_delegate->GetClipboard() | 516 views::ViewsDelegate::views_delegate->GetClipboard() |
513 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &result); | 517 ->ReadText(ui::Clipboard::BUFFER_STANDARD, &result); |
514 if (!result.empty()) { | 518 if (!result.empty()) { |
515 InsertTextInternal(result, false); | 519 InsertTextInternal(result, false); |
516 return true; | 520 return true; |
517 } | 521 } |
518 return false; | 522 return false; |
519 } | 523 } |
520 | 524 |
521 bool TextfieldViewsModel::HasSelection() const { | 525 bool TextfieldViewsModel::HasSelection() const { |
522 return !render_text_->GetSelection().is_empty(); | 526 return !render_text_->EmptySelection(); |
523 } | 527 } |
524 | 528 |
525 void TextfieldViewsModel::DeleteSelection() { | 529 void TextfieldViewsModel::DeleteSelection() { |
526 DCHECK(!HasCompositionText()); | 530 DCHECK(!HasCompositionText()); |
527 DCHECK(HasSelection()); | 531 DCHECK(HasSelection()); |
528 ui::Range selection = render_text_->GetSelection(); | 532 ExecuteAndRecordDelete(render_text_->GetSelectionStart(), |
529 ExecuteAndRecordDelete(selection.start(), selection.end(), false); | 533 render_text_->GetCursorPosition(), false); |
530 } | 534 } |
531 | 535 |
532 void TextfieldViewsModel::DeleteSelectionAndInsertTextAt( | 536 void TextfieldViewsModel::DeleteSelectionAndInsertTextAt( |
533 const string16& text, size_t position) { | 537 const string16& text, size_t position) { |
534 if (HasCompositionText()) | 538 if (HasCompositionText()) |
535 CancelCompositionText(); | 539 CancelCompositionText(); |
536 ExecuteAndRecordReplace(DO_NOT_MERGE, | 540 ExecuteAndRecordReplace(DO_NOT_MERGE, |
537 GetCursorPosition(), | 541 GetCursorPosition(), |
538 position + text.length(), | 542 position + text.length(), |
539 text, | 543 text, |
(...skipping 21 matching lines...) Expand all Loading... | |
561 return; | 565 return; |
562 | 566 |
563 size_t cursor = GetCursorPosition(); | 567 size_t cursor = GetCursorPosition(); |
564 string16 new_text = GetText(); | 568 string16 new_text = GetText(); |
565 render_text_->SetText(new_text.insert(cursor, composition.text)); | 569 render_text_->SetText(new_text.insert(cursor, composition.text)); |
566 ui::Range range(cursor, cursor + composition.text.length()); | 570 ui::Range range(cursor, cursor + composition.text.length()); |
567 render_text_->SetCompositionRange(range); | 571 render_text_->SetCompositionRange(range); |
568 // TODO(msw): Support multiple composition underline ranges. | 572 // TODO(msw): Support multiple composition underline ranges. |
569 | 573 |
570 if (composition.selection.IsValid()) | 574 if (composition.selection.IsValid()) |
571 render_text_->SetSelection(ui::Range( | 575 render_text_->SetSelection( |
572 std::min(range.start() + composition.selection.start(), range.end()), | 576 std::min(range.start() + composition.selection.start(), range.end()), |
573 std::min(range.start() + composition.selection.end(), range.end()))); | 577 std::min(range.start() + composition.selection.end(), range.end())); |
574 else | 578 else |
575 render_text_->SetCursorPosition(range.end()); | 579 render_text_->SetCursorPosition(range.end()); |
576 } | 580 } |
577 | 581 |
578 void TextfieldViewsModel::ConfirmCompositionText() { | 582 void TextfieldViewsModel::ConfirmCompositionText() { |
579 DCHECK(HasCompositionText()); | 583 DCHECK(HasCompositionText()); |
580 ui::Range range = render_text_->GetCompositionRange(); | 584 ui::Range range = render_text_->GetCompositionRange(); |
581 string16 text = GetText().substr(range.start(), range.length()); | 585 string16 text = GetText().substr(range.start(), range.length()); |
582 // TODO(oshima): current behavior on ChromeOS is a bit weird and not | 586 // TODO(oshima): current behavior on ChromeOS is a bit weird and not |
583 // sure exactly how this should work. Find out and fix if necessary. | 587 // sure exactly how this should work. Find out and fix if necessary. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
633 ExecuteAndRecordInsert(text, mergeable); | 637 ExecuteAndRecordInsert(text, mergeable); |
634 } | 638 } |
635 } | 639 } |
636 | 640 |
637 void TextfieldViewsModel::ReplaceTextInternal(const string16& text, | 641 void TextfieldViewsModel::ReplaceTextInternal(const string16& text, |
638 bool mergeable) { | 642 bool mergeable) { |
639 if (HasCompositionText()) { | 643 if (HasCompositionText()) { |
640 CancelCompositionText(); | 644 CancelCompositionText(); |
641 } else if (!HasSelection()) { | 645 } else if (!HasSelection()) { |
642 size_t cursor = GetCursorPosition(); | 646 size_t cursor = GetCursorPosition(); |
643 render_text_->SetSelection(ui::Range(cursor + text.length(), cursor)); | 647 render_text_->SetSelection(cursor + text.length(), cursor); |
644 } | 648 } |
645 // Edit history is recorded in InsertText. | 649 // Edit history is recorded in InsertText. |
646 InsertTextInternal(text, mergeable); | 650 InsertTextInternal(text, mergeable); |
647 } | 651 } |
648 | 652 |
649 void TextfieldViewsModel::ClearEditHistory() { | 653 void TextfieldViewsModel::ClearEditHistory() { |
650 STLDeleteContainerPointers(edit_history_.begin(), | 654 STLDeleteContainerPointers(edit_history_.begin(), |
651 edit_history_.end()); | 655 edit_history_.end()); |
652 edit_history_.clear(); | 656 edit_history_.clear(); |
653 current_edit_ = edit_history_.end(); | 657 current_edit_ = edit_history_.end(); |
(...skipping 21 matching lines...) Expand all Loading... | |
675 bool backward = from > to; | 679 bool backward = from > to; |
676 Edit* edit = new DeleteEdit(mergeable, text, old_text_start, backward); | 680 Edit* edit = new DeleteEdit(mergeable, text, old_text_start, backward); |
677 bool delete_edit = AddOrMergeEditHistory(edit); | 681 bool delete_edit = AddOrMergeEditHistory(edit); |
678 edit->Redo(this); | 682 edit->Redo(this); |
679 if (delete_edit) | 683 if (delete_edit) |
680 delete edit; | 684 delete edit; |
681 } | 685 } |
682 | 686 |
683 void TextfieldViewsModel::ExecuteAndRecordReplaceSelection( | 687 void TextfieldViewsModel::ExecuteAndRecordReplaceSelection( |
684 MergeType merge_type, const string16& new_text) { | 688 MergeType merge_type, const string16& new_text) { |
685 size_t new_text_start = render_text_->GetSelection().GetMin(); | 689 size_t new_text_start = render_text_->MinOfSelection(); |
686 size_t new_cursor_pos = new_text_start + new_text.length(); | 690 size_t new_cursor_pos = new_text_start + new_text.length(); |
687 ExecuteAndRecordReplace(merge_type, | 691 ExecuteAndRecordReplace(merge_type, |
688 GetCursorPosition(), | 692 GetCursorPosition(), |
689 new_cursor_pos, | 693 new_cursor_pos, |
690 new_text, | 694 new_text, |
691 new_text_start); | 695 new_text_start); |
692 } | 696 } |
693 | 697 |
694 void TextfieldViewsModel::ExecuteAndRecordReplace(MergeType merge_type, | 698 void TextfieldViewsModel::ExecuteAndRecordReplace(MergeType merge_type, |
695 size_t old_cursor_pos, | 699 size_t old_cursor_pos, |
696 size_t new_cursor_pos, | 700 size_t new_cursor_pos, |
697 const string16& new_text, | 701 const string16& new_text, |
698 size_t new_text_start) { | 702 size_t new_text_start) { |
699 size_t old_text_start = render_text_->GetSelection().GetMin(); | 703 size_t old_text_start = render_text_->MinOfSelection(); |
700 bool backward = render_text_->GetSelection().is_reversed(); | 704 bool backward = render_text_->GetSelectionStart() > |
705 render_text_->GetCursorPosition(); | |
701 Edit* edit = new ReplaceEdit(merge_type, | 706 Edit* edit = new ReplaceEdit(merge_type, |
702 GetSelectedText(), | 707 GetSelectedText(), |
703 old_cursor_pos, | 708 old_cursor_pos, |
704 old_text_start, | 709 old_text_start, |
705 backward, | 710 backward, |
706 new_cursor_pos, | 711 new_cursor_pos, |
707 new_text, | 712 new_text, |
708 new_text_start); | 713 new_text_start); |
709 bool delete_edit = AddOrMergeEditHistory(edit); | 714 bool delete_edit = AddOrMergeEditHistory(edit); |
710 edit->Redo(this); | 715 edit->Redo(this); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
752 if (delete_from != delete_to) | 757 if (delete_from != delete_to) |
753 render_text_->SetText(text.erase(delete_from, delete_to - delete_from)); | 758 render_text_->SetText(text.erase(delete_from, delete_to - delete_from)); |
754 if (!new_text.empty()) | 759 if (!new_text.empty()) |
755 render_text_->SetText(text.insert(new_text_insert_at, new_text)); | 760 render_text_->SetText(text.insert(new_text_insert_at, new_text)); |
756 render_text_->SetCursorPosition(new_cursor_pos); | 761 render_text_->SetCursorPosition(new_cursor_pos); |
757 // TODO(oshima): mac selects the text that is just undone (but gtk doesn't). | 762 // TODO(oshima): mac selects the text that is just undone (but gtk doesn't). |
758 // This looks fine feature and we may want to do the same. | 763 // This looks fine feature and we may want to do the same. |
759 } | 764 } |
760 | 765 |
761 } // namespace views | 766 } // namespace views |
OLD | NEW |