| 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 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 old_text_ += edit->old_text_; | 244 old_text_ += edit->old_text_; |
| 245 } | 245 } |
| 246 return true; | 246 return true; |
| 247 } | 247 } |
| 248 }; | 248 }; |
| 249 | 249 |
| 250 } // namespace internal | 250 } // namespace internal |
| 251 | 251 |
| 252 namespace { | 252 namespace { |
| 253 | 253 |
| 254 // Buffer containing the text to be inserted on executing yank command. This |
| 255 // is a singleton since it needs to be persisted across multiple textfields. |
| 256 base::string16 g_kill_buffer_; |
| 257 |
| 254 // Returns the first segment that is visually emphasized. Usually it's used for | 258 // Returns the first segment that is visually emphasized. Usually it's used for |
| 255 // representing the target clause (on Windows). Returns an invalid range if | 259 // representing the target clause (on Windows). Returns an invalid range if |
| 256 // there is no such a range. | 260 // there is no such a range. |
| 257 gfx::Range GetFirstEmphasizedRange(const ui::CompositionText& composition) { | 261 gfx::Range GetFirstEmphasizedRange(const ui::CompositionText& composition) { |
| 258 for (size_t i = 0; i < composition.underlines.size(); ++i) { | 262 for (size_t i = 0; i < composition.underlines.size(); ++i) { |
| 259 const ui::CompositionUnderline& underline = composition.underlines[i]; | 263 const ui::CompositionUnderline& underline = composition.underlines[i]; |
| 260 if (underline.thick) | 264 if (underline.thick) |
| 261 return gfx::Range(underline.start_offset, underline.end_offset); | 265 return gfx::Range(underline.start_offset, underline.end_offset); |
| 262 } | 266 } |
| 263 return gfx::Range::InvalidRange(); | 267 return gfx::Range::InvalidRange(); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 ConfirmCompositionText(); | 322 ConfirmCompositionText(); |
| 319 size_t save = GetCursorPosition(); | 323 size_t save = GetCursorPosition(); |
| 320 MoveCursor(gfx::LINE_BREAK, | 324 MoveCursor(gfx::LINE_BREAK, |
| 321 render_text_->GetVisualDirectionOfLogicalEnd(), | 325 render_text_->GetVisualDirectionOfLogicalEnd(), |
| 322 false); | 326 false); |
| 323 InsertText(new_text); | 327 InsertText(new_text); |
| 324 render_text_->SetCursorPosition(save); | 328 render_text_->SetCursorPosition(save); |
| 325 ClearSelection(); | 329 ClearSelection(); |
| 326 } | 330 } |
| 327 | 331 |
| 328 bool TextfieldModel::Delete() { | 332 bool TextfieldModel::Delete(bool add_to_kill_buffer) { |
| 329 if (HasCompositionText()) { | 333 if (HasCompositionText()) { |
| 330 // No undo/redo for composition text. | 334 // No undo/redo for composition text. |
| 331 CancelCompositionText(); | 335 CancelCompositionText(); |
| 332 return true; | 336 return true; |
| 333 } | 337 } |
| 334 if (HasSelection()) { | 338 if (HasSelection()) { |
| 339 if (add_to_kill_buffer) |
| 340 g_kill_buffer_ = GetSelectedText(); |
| 335 DeleteSelection(); | 341 DeleteSelection(); |
| 336 return true; | 342 return true; |
| 337 } | 343 } |
| 338 if (text().length() > GetCursorPosition()) { | 344 if (text().length() > GetCursorPosition()) { |
| 339 size_t cursor_position = GetCursorPosition(); | 345 size_t cursor_position = GetCursorPosition(); |
| 340 size_t next_grapheme_index = render_text_->IndexOfAdjacentGrapheme( | 346 size_t next_grapheme_index = render_text_->IndexOfAdjacentGrapheme( |
| 341 cursor_position, gfx::CURSOR_FORWARD); | 347 cursor_position, gfx::CURSOR_FORWARD); |
| 342 ExecuteAndRecordDelete(gfx::Range(cursor_position, next_grapheme_index), | 348 gfx::Range range_to_delete(cursor_position, next_grapheme_index); |
| 343 true); | 349 if (add_to_kill_buffer) |
| 350 g_kill_buffer_ = GetTextFromRange(range_to_delete); |
| 351 ExecuteAndRecordDelete(range_to_delete, true); |
| 344 return true; | 352 return true; |
| 345 } | 353 } |
| 346 return false; | 354 return false; |
| 347 } | 355 } |
| 348 | 356 |
| 349 bool TextfieldModel::Backspace() { | 357 bool TextfieldModel::Backspace(bool add_to_kill_buffer) { |
| 350 if (HasCompositionText()) { | 358 if (HasCompositionText()) { |
| 351 // No undo/redo for composition text. | 359 // No undo/redo for composition text. |
| 352 CancelCompositionText(); | 360 CancelCompositionText(); |
| 353 return true; | 361 return true; |
| 354 } | 362 } |
| 355 if (HasSelection()) { | 363 if (HasSelection()) { |
| 364 if (add_to_kill_buffer) |
| 365 g_kill_buffer_ = GetSelectedText(); |
| 356 DeleteSelection(); | 366 DeleteSelection(); |
| 357 return true; | 367 return true; |
| 358 } | 368 } |
| 359 size_t cursor_position = GetCursorPosition(); | 369 size_t cursor_position = GetCursorPosition(); |
| 360 if (cursor_position > 0) { | 370 if (cursor_position > 0) { |
| 361 // Delete one code point, which may be two UTF-16 words. | 371 // Delete one code point, which may be two UTF-16 words. |
| 362 size_t previous_char = gfx::UTF16OffsetToIndex(text(), cursor_position, -1); | 372 size_t previous_char = gfx::UTF16OffsetToIndex(text(), cursor_position, -1); |
| 363 ExecuteAndRecordDelete(gfx::Range(cursor_position, previous_char), true); | 373 gfx::Range range_to_delete(cursor_position, previous_char); |
| 374 if (add_to_kill_buffer) |
| 375 g_kill_buffer_ = GetTextFromRange(range_to_delete); |
| 376 ExecuteAndRecordDelete(range_to_delete, true); |
| 364 return true; | 377 return true; |
| 365 } | 378 } |
| 366 return false; | 379 return false; |
| 367 } | 380 } |
| 368 | 381 |
| 369 size_t TextfieldModel::GetCursorPosition() const { | 382 size_t TextfieldModel::GetCursorPosition() const { |
| 370 return render_text_->cursor_position(); | 383 return render_text_->cursor_position(); |
| 371 } | 384 } |
| 372 | 385 |
| 373 void TextfieldModel::MoveCursor(gfx::BreakType break_type, | 386 void TextfieldModel::MoveCursor(gfx::BreakType break_type, |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 | 533 |
| 521 base::string16 actual_text = base::CollapseWhitespace(text, false); | 534 base::string16 actual_text = base::CollapseWhitespace(text, false); |
| 522 // If the clipboard contains all whitespaces then paste a single space. | 535 // If the clipboard contains all whitespaces then paste a single space. |
| 523 if (actual_text.empty()) | 536 if (actual_text.empty()) |
| 524 actual_text = base::ASCIIToUTF16(" "); | 537 actual_text = base::ASCIIToUTF16(" "); |
| 525 | 538 |
| 526 InsertTextInternal(actual_text, false); | 539 InsertTextInternal(actual_text, false); |
| 527 return true; | 540 return true; |
| 528 } | 541 } |
| 529 | 542 |
| 543 bool TextfieldModel::Yank() { |
| 544 if (!g_kill_buffer_.empty() || HasSelection()) { |
| 545 InsertTextInternal(g_kill_buffer_, false); |
| 546 return true; |
| 547 } |
| 548 return false; |
| 549 } |
| 550 |
| 530 bool TextfieldModel::HasSelection() const { | 551 bool TextfieldModel::HasSelection() const { |
| 531 return !render_text_->selection().is_empty(); | 552 return !render_text_->selection().is_empty(); |
| 532 } | 553 } |
| 533 | 554 |
| 534 void TextfieldModel::DeleteSelection() { | 555 void TextfieldModel::DeleteSelection() { |
| 535 DCHECK(!HasCompositionText()); | 556 DCHECK(!HasCompositionText()); |
| 536 DCHECK(HasSelection()); | 557 DCHECK(HasSelection()); |
| 537 ExecuteAndRecordDelete(render_text_->selection(), false); | 558 ExecuteAndRecordDelete(render_text_->selection(), false); |
| 538 } | 559 } |
| 539 | 560 |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 774 ClearComposition(); | 795 ClearComposition(); |
| 775 if (delete_from != delete_to) | 796 if (delete_from != delete_to) |
| 776 render_text_->SetText(old_text.erase(delete_from, delete_to - delete_from)); | 797 render_text_->SetText(old_text.erase(delete_from, delete_to - delete_from)); |
| 777 if (!new_text.empty()) | 798 if (!new_text.empty()) |
| 778 render_text_->SetText(old_text.insert(new_text_insert_at, new_text)); | 799 render_text_->SetText(old_text.insert(new_text_insert_at, new_text)); |
| 779 render_text_->SetCursorPosition(new_cursor_pos); | 800 render_text_->SetCursorPosition(new_cursor_pos); |
| 780 // TODO(oshima): Select text that was just undone, like Mac (but not GTK). | 801 // TODO(oshima): Select text that was just undone, like Mac (but not GTK). |
| 781 } | 802 } |
| 782 | 803 |
| 783 } // namespace views | 804 } // namespace views |
| OLD | NEW |