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