OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/utf_offset_string_conversions.h" |
12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
13 #include "ui/base/clipboard/clipboard.h" | 14 #include "ui/base/clipboard/clipboard.h" |
14 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 15 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
15 #include "ui/base/range/range.h" | 16 #include "ui/base/range/range.h" |
16 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
17 #include "ui/gfx/font.h" | 18 #include "ui/gfx/font.h" |
18 #include "ui/gfx/render_text.h" | 19 #include "ui/gfx/render_text.h" |
19 #include "ui/views/controls/textfield/textfield.h" | 20 #include "ui/views/controls/textfield/textfield.h" |
20 #include "ui/views/views_delegate.h" | 21 #include "ui/views/views_delegate.h" |
21 | 22 |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 bool TextfieldViewsModel::Backspace() { | 355 bool TextfieldViewsModel::Backspace() { |
355 if (HasCompositionText()) { | 356 if (HasCompositionText()) { |
356 // No undo/redo for composition text. | 357 // No undo/redo for composition text. |
357 CancelCompositionText(); | 358 CancelCompositionText(); |
358 return true; | 359 return true; |
359 } | 360 } |
360 if (HasSelection()) { | 361 if (HasSelection()) { |
361 DeleteSelection(); | 362 DeleteSelection(); |
362 return true; | 363 return true; |
363 } | 364 } |
364 if (GetCursorPosition() > 0) { | 365 size_t cursor_position = GetCursorPosition(); |
365 size_t cursor_position = GetCursorPosition(); | 366 if (cursor_position > 0) { |
366 ExecuteAndRecordDelete(cursor_position, cursor_position - 1, true); | 367 size_t previous_char = Utf16OffsetToIndex(GetText(), cursor_position, -1); |
| 368 ExecuteAndRecordDelete(cursor_position, previous_char, true); |
367 return true; | 369 return true; |
368 } | 370 } |
369 return false; | 371 return false; |
370 } | 372 } |
371 | 373 |
372 size_t TextfieldViewsModel::GetCursorPosition() const { | 374 size_t TextfieldViewsModel::GetCursorPosition() const { |
373 return render_text_->GetCursorPosition(); | 375 return render_text_->GetCursorPosition(); |
374 } | 376 } |
375 | 377 |
376 void TextfieldViewsModel::MoveCursor(gfx::BreakType break_type, | 378 void TextfieldViewsModel::MoveCursor(gfx::BreakType break_type, |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 current_edit_ = edit_history_.begin(); | 493 current_edit_ = edit_history_.begin(); |
492 else | 494 else |
493 current_edit_ ++; | 495 current_edit_ ++; |
494 string16 old = GetText(); | 496 string16 old = GetText(); |
495 size_t old_cursor = GetCursorPosition(); | 497 size_t old_cursor = GetCursorPosition(); |
496 (*current_edit_)->Redo(this); | 498 (*current_edit_)->Redo(this); |
497 return old != GetText() || old_cursor != GetCursorPosition(); | 499 return old != GetText() || old_cursor != GetCursorPosition(); |
498 } | 500 } |
499 | 501 |
500 bool TextfieldViewsModel::Cut() { | 502 bool TextfieldViewsModel::Cut() { |
501 if (!HasCompositionText() && HasSelection()) { | 503 if (!HasCompositionText() && HasSelection() && !render_text_->is_obscured()) { |
502 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate | 504 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate |
503 ->GetClipboard()).WriteText(GetSelectedText()); | 505 ->GetClipboard()).WriteText(GetSelectedText()); |
504 // A trick to let undo/redo handle cursor correctly. | 506 // A trick to let undo/redo handle cursor correctly. |
505 // Undoing CUT moves the cursor to the end of the change rather | 507 // Undoing CUT moves the cursor to the end of the change rather |
506 // than beginning, unlike Delete/Backspace. | 508 // than beginning, unlike Delete/Backspace. |
507 // TODO(oshima): Change Delete/Backspace to use DeleteSelection, | 509 // TODO(oshima): Change Delete/Backspace to use DeleteSelection, |
508 // update DeleteEdit and remove this trick. | 510 // update DeleteEdit and remove this trick. |
509 render_text_->SelectRange(ui::Range(render_text_->GetCursorPosition(), | 511 render_text_->SelectRange(ui::Range(render_text_->GetCursorPosition(), |
510 render_text_->GetSelectionStart())); | 512 render_text_->GetSelectionStart())); |
511 DeleteSelection(); | 513 DeleteSelection(); |
512 return true; | 514 return true; |
513 } | 515 } |
514 return false; | 516 return false; |
515 } | 517 } |
516 | 518 |
517 bool TextfieldViewsModel::Copy() { | 519 bool TextfieldViewsModel::Copy() { |
518 if (!HasCompositionText() && HasSelection()) { | 520 if (!HasCompositionText() && HasSelection() && !render_text_->is_obscured()) { |
519 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate | 521 ui::ScopedClipboardWriter(views::ViewsDelegate::views_delegate |
520 ->GetClipboard()).WriteText(GetSelectedText()); | 522 ->GetClipboard()).WriteText(GetSelectedText()); |
521 return true; | 523 return true; |
522 } | 524 } |
523 return false; | 525 return false; |
524 } | 526 } |
525 | 527 |
526 bool TextfieldViewsModel::Paste() { | 528 bool TextfieldViewsModel::Paste() { |
527 string16 result; | 529 string16 result; |
528 views::ViewsDelegate::views_delegate->GetClipboard() | 530 views::ViewsDelegate::views_delegate->GetClipboard() |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 if (delete_from != delete_to) | 776 if (delete_from != delete_to) |
775 render_text_->SetText(text.erase(delete_from, delete_to - delete_from)); | 777 render_text_->SetText(text.erase(delete_from, delete_to - delete_from)); |
776 if (!new_text.empty()) | 778 if (!new_text.empty()) |
777 render_text_->SetText(text.insert(new_text_insert_at, new_text)); | 779 render_text_->SetText(text.insert(new_text_insert_at, new_text)); |
778 render_text_->SetCursorPosition(new_cursor_pos); | 780 render_text_->SetCursorPosition(new_cursor_pos); |
779 // TODO(oshima): mac selects the text that is just undone (but gtk doesn't). | 781 // TODO(oshima): mac selects the text that is just undone (but gtk doesn't). |
780 // This looks fine feature and we may want to do the same. | 782 // This looks fine feature and we may want to do the same. |
781 } | 783 } |
782 | 784 |
783 } // namespace views | 785 } // namespace views |
OLD | NEW |