| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_model.h" | 5 #include "ui/views/controls/textfield/textfield_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 ConfirmCompositionText(); | 337 ConfirmCompositionText(); |
| 338 size_t save = GetCursorPosition(); | 338 size_t save = GetCursorPosition(); |
| 339 MoveCursor(gfx::LINE_BREAK, render_text_->GetVisualDirectionOfLogicalEnd(), | 339 MoveCursor(gfx::LINE_BREAK, render_text_->GetVisualDirectionOfLogicalEnd(), |
| 340 gfx::SELECTION_NONE); | 340 gfx::SELECTION_NONE); |
| 341 InsertText(new_text); | 341 InsertText(new_text); |
| 342 render_text_->SetCursorPosition(save); | 342 render_text_->SetCursorPosition(save); |
| 343 ClearSelection(); | 343 ClearSelection(); |
| 344 } | 344 } |
| 345 | 345 |
| 346 bool TextfieldModel::Delete(bool add_to_kill_buffer) { | 346 bool TextfieldModel::Delete(bool add_to_kill_buffer) { |
| 347 // |add_to_kill_buffer| should never be true for an obscured textfield. |
| 348 DCHECK(!add_to_kill_buffer || !render_text_->obscured()); |
| 349 |
| 347 if (HasCompositionText()) { | 350 if (HasCompositionText()) { |
| 348 // No undo/redo for composition text. | 351 // No undo/redo for composition text. |
| 349 CancelCompositionText(); | 352 CancelCompositionText(); |
| 350 return true; | 353 return true; |
| 351 } | 354 } |
| 355 |
| 352 if (HasSelection()) { | 356 if (HasSelection()) { |
| 353 if (add_to_kill_buffer) | 357 if (add_to_kill_buffer) |
| 354 SetKillBuffer(GetSelectedText()); | 358 SetKillBuffer(GetSelectedText()); |
| 355 DeleteSelection(); | 359 DeleteSelection(); |
| 356 return true; | 360 return true; |
| 357 } | 361 } |
| 358 if (text().length() > GetCursorPosition()) { | 362 if (text().length() > GetCursorPosition()) { |
| 359 size_t cursor_position = GetCursorPosition(); | 363 size_t cursor_position = GetCursorPosition(); |
| 360 size_t next_grapheme_index = render_text_->IndexOfAdjacentGrapheme( | 364 size_t next_grapheme_index = render_text_->IndexOfAdjacentGrapheme( |
| 361 cursor_position, gfx::CURSOR_FORWARD); | 365 cursor_position, gfx::CURSOR_FORWARD); |
| 362 gfx::Range range_to_delete(cursor_position, next_grapheme_index); | 366 gfx::Range range_to_delete(cursor_position, next_grapheme_index); |
| 363 if (add_to_kill_buffer) | 367 if (add_to_kill_buffer) |
| 364 SetKillBuffer(GetTextFromRange(range_to_delete)); | 368 SetKillBuffer(GetTextFromRange(range_to_delete)); |
| 365 ExecuteAndRecordDelete(range_to_delete, true); | 369 ExecuteAndRecordDelete(range_to_delete, true); |
| 366 return true; | 370 return true; |
| 367 } | 371 } |
| 368 return false; | 372 return false; |
| 369 } | 373 } |
| 370 | 374 |
| 371 bool TextfieldModel::Backspace(bool add_to_kill_buffer) { | 375 bool TextfieldModel::Backspace(bool add_to_kill_buffer) { |
| 376 // |add_to_kill_buffer| should never be true for an obscured textfield. |
| 377 DCHECK(!add_to_kill_buffer || !render_text_->obscured()); |
| 378 |
| 372 if (HasCompositionText()) { | 379 if (HasCompositionText()) { |
| 373 // No undo/redo for composition text. | 380 // No undo/redo for composition text. |
| 374 CancelCompositionText(); | 381 CancelCompositionText(); |
| 375 return true; | 382 return true; |
| 376 } | 383 } |
| 384 |
| 377 if (HasSelection()) { | 385 if (HasSelection()) { |
| 378 if (add_to_kill_buffer) | 386 if (add_to_kill_buffer) |
| 379 SetKillBuffer(GetSelectedText()); | 387 SetKillBuffer(GetSelectedText()); |
| 380 DeleteSelection(); | 388 DeleteSelection(); |
| 381 return true; | 389 return true; |
| 382 } | 390 } |
| 383 size_t cursor_position = GetCursorPosition(); | 391 size_t cursor_position = GetCursorPosition(); |
| 384 if (cursor_position > 0) { | 392 if (cursor_position > 0) { |
| 385 // Delete one code point, which may be two UTF-16 words. | 393 // Delete one code point, which may be two UTF-16 words. |
| 386 size_t previous_grapheme_index = | 394 size_t previous_grapheme_index = |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 bool TextfieldModel::MoveCursorTo(const gfx::Point& point, bool select) { | 431 bool TextfieldModel::MoveCursorTo(const gfx::Point& point, bool select) { |
| 424 if (HasCompositionText()) | 432 if (HasCompositionText()) |
| 425 ConfirmCompositionText(); | 433 ConfirmCompositionText(); |
| 426 gfx::SelectionModel cursor = render_text_->FindCursorPosition(point); | 434 gfx::SelectionModel cursor = render_text_->FindCursorPosition(point); |
| 427 if (select) | 435 if (select) |
| 428 cursor.set_selection_start(render_text_->selection().start()); | 436 cursor.set_selection_start(render_text_->selection().start()); |
| 429 return render_text_->MoveCursorTo(cursor); | 437 return render_text_->MoveCursorTo(cursor); |
| 430 } | 438 } |
| 431 | 439 |
| 432 base::string16 TextfieldModel::GetSelectedText() const { | 440 base::string16 TextfieldModel::GetSelectedText() const { |
| 433 return text().substr(render_text_->selection().GetMin(), | 441 return GetTextFromRange(render_text_->selection()); |
| 434 render_text_->selection().length()); | |
| 435 } | 442 } |
| 436 | 443 |
| 437 void TextfieldModel::SelectRange(const gfx::Range& range) { | 444 void TextfieldModel::SelectRange(const gfx::Range& range) { |
| 438 if (HasCompositionText()) | 445 if (HasCompositionText()) |
| 439 ConfirmCompositionText(); | 446 ConfirmCompositionText(); |
| 440 render_text_->SelectRange(range); | 447 render_text_->SelectRange(range); |
| 441 } | 448 } |
| 442 | 449 |
| 443 void TextfieldModel::SelectSelectionModel(const gfx::SelectionModel& sel) { | 450 void TextfieldModel::SelectSelectionModel(const gfx::SelectionModel& sel) { |
| 444 if (HasCompositionText()) | 451 if (HasCompositionText()) |
| (...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 846 render_text_->SetCursorPosition(new_cursor_pos); | 853 render_text_->SetCursorPosition(new_cursor_pos); |
| 847 // TODO(oshima): Select text that was just undone, like Mac (but not GTK). | 854 // TODO(oshima): Select text that was just undone, like Mac (but not GTK). |
| 848 } | 855 } |
| 849 | 856 |
| 850 // static | 857 // static |
| 851 void TextfieldModel::ClearKillBuffer() { | 858 void TextfieldModel::ClearKillBuffer() { |
| 852 SetKillBuffer(base::string16()); | 859 SetKillBuffer(base::string16()); |
| 853 } | 860 } |
| 854 | 861 |
| 855 } // namespace views | 862 } // namespace views |
| OLD | NEW |