1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/controls/textfield/textfield_model.h"
#include <algorithm>
#include "base/logging.h"
#include "base/stl_util.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "ui/gfx/range/range.h"
#include "ui/gfx/utf16_indexing.h"
namespace views {
namespace internal {
// Edit holds state information to undo/redo editing changes. Editing operations
// are merged when possible, like when characters are typed in sequence. Calling
// Commit() marks an edit as an independent operation that shouldn't be merged.
class Edit {
public:
enum Type {
INSERT_EDIT,
DELETE_EDIT,
REPLACE_EDIT,
};
virtual ~Edit() {}
// Revert the change made by this edit in |model|.
void Undo(TextfieldModel* model) {
model->ModifyText(new_text_start_, new_text_end(),
old_text_, old_text_start_,
old_cursor_pos_);
}
// Apply the change of this edit to the |model|.
void Redo(TextfieldModel* model) {
model->ModifyText(old_text_start_, old_text_end(),
new_text_, new_text_start_,
new_cursor_pos_);
}
// Try to merge the |edit| into this edit and returns true on success. The
// merged edit will be deleted after redo and should not be reused.
bool Merge(const Edit* edit) {
// Don't merge if previous edit is DELETE. This happens when a
// user deletes characters then hits return. In this case, the
// delete should be treated as separate edit that can be undone
// and should not be merged with the replace edit.
if (type_ != DELETE_EDIT && edit->force_merge()) {
MergeReplace(edit);
return true;
}
return mergeable() && edit->mergeable() && DoMerge(edit);
}
// Commits the edit and marks as un-mergeable.
void Commit() { merge_type_ = DO_NOT_MERGE; }
private:
friend class InsertEdit;
friend class ReplaceEdit;
friend class DeleteEdit;
Edit(Type type,
MergeType merge_type,
size_t old_cursor_pos,
const base::string16& old_text,
size_t old_text_start,
bool delete_backward,
size_t new_cursor_pos,
const base::string16& new_text,
size_t new_text_start)
: type_(type),
merge_type_(merge_type),
old_cursor_pos_(old_cursor_pos),
old_text_(old_text),
old_text_start_(old_text_start),
delete_backward_(delete_backward),
new_cursor_pos_(new_cursor_pos),
new_text_(new_text),
new_text_start_(new_text_start) {
}
// Each type of edit provides its own specific merge implementation.
virtual bool DoMerge(const Edit* edit) = 0;
Type type() const { return type_; }
// Can this edit be merged?
bool mergeable() const { return merge_type_ == MERGEABLE; }
// Should this edit be forcibly merged with the previous edit?
bool force_merge() const { return merge_type_ == FORCE_MERGE; }
// Returns the end index of the |old_text_|.
size_t old_text_end() const { return old_text_start_ + old_text_.length(); }
// Returns the end index of the |new_text_|.
size_t new_text_end() const { return new_text_start_ + new_text_.length(); }
// Merge the replace edit into the current edit. This handles the special case
// where an omnibox autocomplete string is set after a new character is typed.
void MergeReplace(const Edit* edit) {
CHECK_EQ(REPLACE_EDIT, edit->type_);
CHECK_EQ(0U, edit->old_text_start_);
CHECK_EQ(0U, edit->new_text_start_);
base::string16 old_text = edit->old_text_;
old_text.erase(new_text_start_, new_text_.length());
old_text.insert(old_text_start_, old_text_);
// SetText() replaces entire text. Set |old_text_| to the entire
// replaced text with |this| edit undone.
old_text_ = old_text;
old_text_start_ = edit->old_text_start_;
delete_backward_ = false;
new_text_ = edit->new_text_;
new_text_start_ = edit->new_text_start_;
merge_type_ = DO_NOT_MERGE;
}
Type type_;
// True if the edit can be marged.
MergeType merge_type_;
// Old cursor position.
size_t old_cursor_pos_;
// Deleted text by this edit.
base::string16 old_text_;
// The index of |old_text_|.
size_t old_text_start_;
// True if the deletion is made backward.
bool delete_backward_;
// New cursor position.
size_t new_cursor_pos_;
// Added text.
base::string16 new_text_;
// The index of |new_text_|
size_t new_text_start_;
DISALLOW_COPY_AND_ASSIGN(Edit);
};
class InsertEdit : public Edit {
public:
InsertEdit(bool mergeable, const base::string16& new_text, size_t at)
: Edit(INSERT_EDIT,
mergeable ? MERGEABLE : DO_NOT_MERGE,
at /* old cursor */,
base::string16(),
at,
false /* N/A */,
at + new_text.length() /* new cursor */,
new_text,
at) {
}
// Edit implementation.
bool DoMerge(const Edit* edit) override {
if (edit->type() != INSERT_EDIT || new_text_end() != edit->new_text_start_)
return false;
// If continuous edit, merge it.
// TODO(oshima): gtk splits edits between whitespace. Find out what
// we want to here and implement if necessary.
new_text_ += edit->new_text_;
new_cursor_pos_ = edit->new_cursor_pos_;
return true;
}
};
class ReplaceEdit : public Edit {
public:
ReplaceEdit(MergeType merge_type,
const base::string16& old_text,
size_t old_cursor_pos,
size_t old_text_start,
bool backward,
size_t new_cursor_pos,
const base::string16& new_text,
size_t new_text_start)
: Edit(REPLACE_EDIT, merge_type,
old_cursor_pos,
old_text,
old_text_start,
backward,
new_cursor_pos,
new_text,
new_text_start) {
}
// Edit implementation.
bool DoMerge(const Edit* edit) override {
if (edit->type() == DELETE_EDIT ||
new_text_end() != edit->old_text_start_ ||
edit->old_text_start_ != edit->new_text_start_)
return false;
old_text_ += edit->old_text_;
new_text_ += edit->new_text_;
new_cursor_pos_ = edit->new_cursor_pos_;
return true;
}
};
class DeleteEdit : public Edit {
public:
DeleteEdit(bool mergeable,
const base::string16& text,
size_t text_start,
bool backward)
: Edit(DELETE_EDIT,
mergeable ? MERGEABLE : DO_NOT_MERGE,
(backward ? text_start + text.length() : text_start),
text,
text_start,
backward,
text_start,
base::string16(),
text_start) {
}
// Edit implementation.
bool DoMerge(const Edit* edit) override {
if (edit->type() != DELETE_EDIT)
return false;
if (delete_backward_) {
// backspace can be merged only with backspace at the same position.
if (!edit->delete_backward_ || old_text_start_ != edit->old_text_end())
return false;
old_text_start_ = edit->old_text_start_;
old_text_ = edit->old_text_ + old_text_;
new_cursor_pos_ = edit->new_cursor_pos_;
} else {
// delete can be merged only with delete at the same position.
if (edit->delete_backward_ || old_text_start_ != edit->old_text_start_)
return false;
old_text_ += edit->old_text_;
}
return true;
}
};
} // namespace internal
namespace {
// Returns the first segment that is visually emphasized. Usually it's used for
// representing the target clause (on Windows). Returns an invalid range if
// there is no such a range.
gfx::Range GetFirstEmphasizedRange(const ui::CompositionText& composition) {
for (size_t i = 0; i < composition.underlines.size(); ++i) {
const ui::CompositionUnderline& underline = composition.underlines[i];
if (underline.thick)
return gfx::Range(underline.start_offset, underline.end_offset);
}
return gfx::Range::InvalidRange();
}
} // namespace
using internal::Edit;
using internal::DeleteEdit;
using internal::InsertEdit;
using internal::ReplaceEdit;
using internal::MergeType;
using internal::DO_NOT_MERGE;
using internal::FORCE_MERGE;
using internal::MERGEABLE;
/////////////////////////////////////////////////////////////////
// TextfieldModel: public
TextfieldModel::Delegate::~Delegate() {}
TextfieldModel::TextfieldModel(Delegate* delegate)
: delegate_(delegate),
render_text_(gfx::RenderText::CreateInstanceForEditing()),
current_edit_(edit_history_.end()) {
}
TextfieldModel::~TextfieldModel() {
ClearEditHistory();
ClearComposition();
}
bool TextfieldModel::SetText(const base::string16& new_text) {
bool changed = false;
if (HasCompositionText()) {
ConfirmCompositionText();
changed = true;
}
if (text() != new_text) {
if (changed) // No need to remember composition.
Undo();
size_t old_cursor = GetCursorPosition();
// SetText moves the cursor to the end.
size_t new_cursor = new_text.length();
SelectAll(false);
// If there is a composition text, don't merge with previous edit.
// Otherwise, force merge the edits.
ExecuteAndRecordReplace(changed ? DO_NOT_MERGE : FORCE_MERGE,
old_cursor, new_cursor, new_text, 0U);
render_text_->SetCursorPosition(new_cursor);
}
ClearSelection();
return changed;
}
void TextfieldModel::Append(const base::string16& new_text) {
if (HasCompositionText())
ConfirmCompositionText();
size_t save = GetCursorPosition();
MoveCursor(gfx::LINE_BREAK,
render_text_->GetVisualDirectionOfLogicalEnd(),
false);
InsertText(new_text);
render_text_->SetCursorPosition(save);
ClearSelection();
}
bool TextfieldModel::Delete() {
if (HasCompositionText()) {
// No undo/redo for composition text.
CancelCompositionText();
return true;
}
if (HasSelection()) {
DeleteSelection();
return true;
}
if (text().length() > GetCursorPosition()) {
size_t cursor_position = GetCursorPosition();
size_t next_grapheme_index = render_text_->IndexOfAdjacentGrapheme(
cursor_position, gfx::CURSOR_FORWARD);
ExecuteAndRecordDelete(gfx::Range(cursor_position, next_grapheme_index),
true);
return true;
}
return false;
}
bool TextfieldModel::Backspace() {
if (HasCompositionText()) {
// No undo/redo for composition text.
CancelCompositionText();
return true;
}
if (HasSelection()) {
DeleteSelection();
return true;
}
size_t cursor_position = GetCursorPosition();
if (cursor_position > 0) {
// Delete one code point, which may be two UTF-16 words.
size_t previous_char = gfx::UTF16OffsetToIndex(text(), cursor_position, -1);
ExecuteAndRecordDelete(gfx::Range(cursor_position, previous_char), true);
return true;
}
return false;
}
size_t TextfieldModel::GetCursorPosition() const {
return render_text_->cursor_position();
}
void TextfieldModel::MoveCursor(gfx::BreakType break_type,
gfx::VisualCursorDirection direction,
bool select) {
if (HasCompositionText())
ConfirmCompositionText();
render_text_->MoveCursor(break_type, direction, select);
}
bool TextfieldModel::MoveCursorTo(const gfx::SelectionModel& cursor) {
if (HasCompositionText()) {
ConfirmCompositionText();
// ConfirmCompositionText() updates cursor position. Need to reflect it in
// the SelectionModel parameter of MoveCursorTo().
gfx::Range range(render_text_->selection().start(), cursor.caret_pos());
if (!range.is_empty())
return render_text_->SelectRange(range);
return render_text_->MoveCursorTo(
gfx::SelectionModel(cursor.caret_pos(), cursor.caret_affinity()));
}
return render_text_->MoveCursorTo(cursor);
}
bool TextfieldModel::MoveCursorTo(const gfx::Point& point, bool select) {
if (HasCompositionText())
ConfirmCompositionText();
gfx::SelectionModel cursor = render_text_->FindCursorPosition(point);
if (select)
cursor.set_selection_start(render_text_->selection().start());
return render_text_->MoveCursorTo(cursor);
}
base::string16 TextfieldModel::GetSelectedText() const {
return text().substr(render_text_->selection().GetMin(),
render_text_->selection().length());
}
void TextfieldModel::SelectRange(const gfx::Range& range) {
if (HasCompositionText())
ConfirmCompositionText();
render_text_->SelectRange(range);
}
void TextfieldModel::SelectSelectionModel(const gfx::SelectionModel& sel) {
if (HasCompositionText())
ConfirmCompositionText();
render_text_->MoveCursorTo(sel);
}
void TextfieldModel::SelectAll(bool reversed) {
if (HasCompositionText())
ConfirmCompositionText();
render_text_->SelectAll(reversed);
}
void TextfieldModel::SelectWord() {
if (HasCompositionText())
ConfirmCompositionText();
render_text_->SelectWord();
}
void TextfieldModel::ClearSelection() {
if (HasCompositionText())
ConfirmCompositionText();
render_text_->ClearSelection();
}
bool TextfieldModel::CanUndo() {
return edit_history_.size() && current_edit_ != edit_history_.end();
}
bool TextfieldModel::CanRedo() {
if (!edit_history_.size())
return false;
// There is no redo iff the current edit is the last element in the history.
EditHistory::iterator iter = current_edit_;
return iter == edit_history_.end() || // at the top.
++iter != edit_history_.end();
}
bool TextfieldModel::Undo() {
if (!CanUndo())
return false;
DCHECK(!HasCompositionText());
if (HasCompositionText())
CancelCompositionText();
base::string16 old = text();
size_t old_cursor = GetCursorPosition();
(*current_edit_)->Commit();
(*current_edit_)->Undo(this);
if (current_edit_ == edit_history_.begin())
current_edit_ = edit_history_.end();
else
current_edit_--;
return old != text() || old_cursor != GetCursorPosition();
}
bool TextfieldModel::Redo() {
if (!CanRedo())
return false;
DCHECK(!HasCompositionText());
if (HasCompositionText())
CancelCompositionText();
if (current_edit_ == edit_history_.end())
current_edit_ = edit_history_.begin();
else
current_edit_ ++;
base::string16 old = text();
size_t old_cursor = GetCursorPosition();
(*current_edit_)->Redo(this);
return old != text() || old_cursor != GetCursorPosition();
}
bool TextfieldModel::Cut() {
if (!HasCompositionText() && HasSelection() && !render_text_->obscured()) {
ui::ScopedClipboardWriter(
ui::CLIPBOARD_TYPE_COPY_PASTE).WriteText(GetSelectedText());
// A trick to let undo/redo handle cursor correctly.
// Undoing CUT moves the cursor to the end of the change rather
// than beginning, unlike Delete/Backspace.
// TODO(oshima): Change Delete/Backspace to use DeleteSelection,
// update DeleteEdit and remove this trick.
const gfx::Range& selection = render_text_->selection();
render_text_->SelectRange(gfx::Range(selection.end(), selection.start()));
DeleteSelection();
return true;
}
return false;
}
bool TextfieldModel::Copy() {
if (!HasCompositionText() && HasSelection() && !render_text_->obscured()) {
ui::ScopedClipboardWriter(
ui::CLIPBOARD_TYPE_COPY_PASTE).WriteText(GetSelectedText());
return true;
}
return false;
}
bool TextfieldModel::Paste() {
base::string16 result;
ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE,
&result);
if (result.empty())
return false;
InsertTextInternal(result, false);
return true;
}
bool TextfieldModel::HasSelection() const {
return !render_text_->selection().is_empty();
}
void TextfieldModel::DeleteSelection() {
DCHECK(!HasCompositionText());
DCHECK(HasSelection());
ExecuteAndRecordDelete(render_text_->selection(), false);
}
void TextfieldModel::DeleteSelectionAndInsertTextAt(
const base::string16& new_text,
size_t position) {
if (HasCompositionText())
CancelCompositionText();
ExecuteAndRecordReplace(DO_NOT_MERGE,
GetCursorPosition(),
position + new_text.length(),
new_text,
position);
}
base::string16 TextfieldModel::GetTextFromRange(const gfx::Range& range) const {
if (range.IsValid() && range.GetMin() < text().length())
return text().substr(range.GetMin(), range.length());
return base::string16();
}
void TextfieldModel::GetTextRange(gfx::Range* range) const {
*range = gfx::Range(0, text().length());
}
void TextfieldModel::SetCompositionText(
const ui::CompositionText& composition) {
if (HasCompositionText())
CancelCompositionText();
else if (HasSelection())
DeleteSelection();
if (composition.text.empty())
return;
size_t cursor = GetCursorPosition();
base::string16 new_text = text();
render_text_->SetText(new_text.insert(cursor, composition.text));
gfx::Range range(cursor, cursor + composition.text.length());
render_text_->SetCompositionRange(range);
gfx::Range emphasized_range = GetFirstEmphasizedRange(composition);
if (emphasized_range.IsValid()) {
// This is a workaround due to the lack of support in RenderText to draw
// a thick underline. In a composition returned from an IME, the segment
// emphasized by a thick underline usually represents the target clause.
// Because the target clause is more important than the actual selection
// range (or caret position) in the composition here we use a selection-like
// marker instead to show this range.
// TODO(yukawa, msw): Support thick underlines and remove this workaround.
render_text_->SelectRange(gfx::Range(
cursor + emphasized_range.GetMin(),
cursor + emphasized_range.GetMax()));
} else if (!composition.selection.is_empty()) {
render_text_->SelectRange(gfx::Range(
cursor + composition.selection.GetMin(),
cursor + composition.selection.GetMax()));
} else {
render_text_->SetCursorPosition(cursor + composition.selection.end());
}
}
void TextfieldModel::ConfirmCompositionText() {
DCHECK(HasCompositionText());
gfx::Range range = render_text_->GetCompositionRange();
base::string16 composition = text().substr(range.start(), range.length());
// TODO(oshima): current behavior on ChromeOS is a bit weird and not
// sure exactly how this should work. Find out and fix if necessary.
AddOrMergeEditHistory(new InsertEdit(false, composition, range.start()));
render_text_->SetCursorPosition(range.end());
ClearComposition();
if (delegate_)
delegate_->OnCompositionTextConfirmedOrCleared();
}
void TextfieldModel::CancelCompositionText() {
DCHECK(HasCompositionText());
gfx::Range range = render_text_->GetCompositionRange();
ClearComposition();
base::string16 new_text = text();
render_text_->SetText(new_text.erase(range.start(), range.length()));
render_text_->SetCursorPosition(range.start());
if (delegate_)
delegate_->OnCompositionTextConfirmedOrCleared();
}
void TextfieldModel::ClearComposition() {
render_text_->SetCompositionRange(gfx::Range::InvalidRange());
}
void TextfieldModel::GetCompositionTextRange(gfx::Range* range) const {
*range = gfx::Range(render_text_->GetCompositionRange());
}
bool TextfieldModel::HasCompositionText() const {
return !render_text_->GetCompositionRange().is_empty();
}
void TextfieldModel::ClearEditHistory() {
STLDeleteElements(&edit_history_);
current_edit_ = edit_history_.end();
}
/////////////////////////////////////////////////////////////////
// TextfieldModel: private
void TextfieldModel::InsertTextInternal(const base::string16& new_text,
bool mergeable) {
if (HasCompositionText()) {
CancelCompositionText();
ExecuteAndRecordInsert(new_text, mergeable);
} else if (HasSelection()) {
ExecuteAndRecordReplaceSelection(mergeable ? MERGEABLE : DO_NOT_MERGE,
new_text);
} else {
ExecuteAndRecordInsert(new_text, mergeable);
}
}
void TextfieldModel::ReplaceTextInternal(const base::string16& new_text,
bool mergeable) {
if (HasCompositionText()) {
CancelCompositionText();
} else if (!HasSelection()) {
size_t cursor = GetCursorPosition();
const gfx::SelectionModel& model = render_text_->selection_model();
// When there is no selection, the default is to replace the next grapheme
// with |new_text|. So, need to find the index of next grapheme first.
size_t next =
render_text_->IndexOfAdjacentGrapheme(cursor, gfx::CURSOR_FORWARD);
if (next == model.caret_pos())
render_text_->MoveCursorTo(model);
else
render_text_->SelectRange(gfx::Range(next, model.caret_pos()));
}
// Edit history is recorded in InsertText.
InsertTextInternal(new_text, mergeable);
}
void TextfieldModel::ClearRedoHistory() {
if (edit_history_.begin() == edit_history_.end())
return;
if (current_edit_ == edit_history_.end()) {
ClearEditHistory();
return;
}
EditHistory::iterator delete_start = current_edit_;
delete_start++;
STLDeleteContainerPointers(delete_start, edit_history_.end());
edit_history_.erase(delete_start, edit_history_.end());
}
void TextfieldModel::ExecuteAndRecordDelete(gfx::Range range, bool mergeable) {
size_t old_text_start = range.GetMin();
const base::string16 old_text = text().substr(old_text_start, range.length());
bool backward = range.is_reversed();
Edit* edit = new DeleteEdit(mergeable, old_text, old_text_start, backward);
bool delete_edit = AddOrMergeEditHistory(edit);
edit->Redo(this);
if (delete_edit)
delete edit;
}
void TextfieldModel::ExecuteAndRecordReplaceSelection(
MergeType merge_type,
const base::string16& new_text) {
size_t new_text_start = render_text_->selection().GetMin();
size_t new_cursor_pos = new_text_start + new_text.length();
ExecuteAndRecordReplace(merge_type,
GetCursorPosition(),
new_cursor_pos,
new_text,
new_text_start);
}
void TextfieldModel::ExecuteAndRecordReplace(MergeType merge_type,
size_t old_cursor_pos,
size_t new_cursor_pos,
const base::string16& new_text,
size_t new_text_start) {
size_t old_text_start = render_text_->selection().GetMin();
bool backward = render_text_->selection().is_reversed();
Edit* edit = new ReplaceEdit(merge_type,
GetSelectedText(),
old_cursor_pos,
old_text_start,
backward,
new_cursor_pos,
new_text,
new_text_start);
bool delete_edit = AddOrMergeEditHistory(edit);
edit->Redo(this);
if (delete_edit)
delete edit;
}
void TextfieldModel::ExecuteAndRecordInsert(const base::string16& new_text,
bool mergeable) {
Edit* edit = new InsertEdit(mergeable, new_text, GetCursorPosition());
bool delete_edit = AddOrMergeEditHistory(edit);
edit->Redo(this);
if (delete_edit)
delete edit;
}
bool TextfieldModel::AddOrMergeEditHistory(Edit* edit) {
ClearRedoHistory();
if (current_edit_ != edit_history_.end() && (*current_edit_)->Merge(edit)) {
// If a current edit exists and has been merged with a new edit, don't add
// to the history, and return true to delete |edit| after redo.
return true;
}
edit_history_.push_back(edit);
if (current_edit_ == edit_history_.end()) {
// If there is no redoable edit, this is the 1st edit because RedoHistory
// has been already deleted.
DCHECK_EQ(1u, edit_history_.size());
current_edit_ = edit_history_.begin();
} else {
current_edit_++;
}
return false;
}
void TextfieldModel::ModifyText(size_t delete_from,
size_t delete_to,
const base::string16& new_text,
size_t new_text_insert_at,
size_t new_cursor_pos) {
DCHECK_LE(delete_from, delete_to);
base::string16 old_text = text();
ClearComposition();
if (delete_from != delete_to)
render_text_->SetText(old_text.erase(delete_from, delete_to - delete_from));
if (!new_text.empty())
render_text_->SetText(old_text.insert(new_text_insert_at, new_text));
render_text_->SetCursorPosition(new_cursor_pos);
// TODO(oshima): Select text that was just undone, like Mac (but not GTK).
}
} // namespace views
|