Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/gfx/render_text.h" | 5 #include "ui/gfx/render_text.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" |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 | 407 |
| 408 const Rect& RenderText::GetUpdatedCursorBounds() { | 408 const Rect& RenderText::GetUpdatedCursorBounds() { |
| 409 UpdateCachedBoundsAndOffset(); | 409 UpdateCachedBoundsAndOffset(); |
| 410 return cursor_bounds_; | 410 return cursor_bounds_; |
| 411 } | 411 } |
| 412 | 412 |
| 413 size_t RenderText::GetIndexOfNextGrapheme(size_t position) { | 413 size_t RenderText::GetIndexOfNextGrapheme(size_t position) { |
| 414 return IndexOfAdjacentGrapheme(position, true); | 414 return IndexOfAdjacentGrapheme(position, true); |
| 415 } | 415 } |
| 416 | 416 |
| 417 SelectionModel RenderText::GetSelectionModelForSelectionStart() { | |
| 418 size_t selection_start = GetSelectionStart(); | |
| 419 size_t selection_end = GetCursorPosition(); | |
| 420 if (selection_start < selection_end) | |
| 421 return SelectionModel(selection_start, | |
|
msw
2011/09/29 02:20:21
You can use the single-argument ctor here for the
xji
2011/10/03 23:18:57
left is as explicit.
| |
| 422 selection_start, | |
| 423 SelectionModel::LEADING); | |
| 424 else if (selection_start > selection_end) | |
| 425 return SelectionModel(selection_start, | |
| 426 GetIndexOfPreviousGrapheme(selection_start), | |
| 427 SelectionModel::TRAILING); | |
| 428 return selection_model_; | |
| 429 } | |
| 430 | |
| 417 RenderText::RenderText() | 431 RenderText::RenderText() |
| 418 : text_(), | 432 : text_(), |
| 419 selection_model_(), | 433 selection_model_(), |
| 420 cursor_bounds_(), | 434 cursor_bounds_(), |
| 421 cursor_visible_(false), | 435 cursor_visible_(false), |
| 422 insert_mode_(true), | 436 insert_mode_(true), |
| 423 composition_range_(ui::Range::InvalidRange()), | 437 composition_range_(ui::Range::InvalidRange()), |
| 424 style_ranges_(), | 438 style_ranges_(), |
| 425 default_style_(), | 439 default_style_(), |
| 426 display_rect_(), | 440 display_rect_(), |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 560 Point p(point.Add(display_rect().origin())); | 574 Point p(point.Add(display_rect().origin())); |
| 561 p = p.Add(GetUpdatedDisplayOffset()); | 575 p = p.Add(GetUpdatedDisplayOffset()); |
| 562 if (base::i18n::IsRTL()) | 576 if (base::i18n::IsRTL()) |
| 563 p.Offset(display_rect().width() - GetStringWidth() - 1, 0); | 577 p.Offset(display_rect().width() - GetStringWidth() - 1, 0); |
| 564 return p; | 578 return p; |
| 565 } | 579 } |
| 566 | 580 |
| 567 void RenderText::SetSelectionModel(const SelectionModel& selection_model) { | 581 void RenderText::SetSelectionModel(const SelectionModel& selection_model) { |
| 568 DCHECK_LE(selection_model.selection_start(), text().length()); | 582 DCHECK_LE(selection_model.selection_start(), text().length()); |
| 569 selection_model_.set_selection_start(selection_model.selection_start()); | 583 selection_model_.set_selection_start(selection_model.selection_start()); |
| 570 DCHECK_LE(selection_model.selection_end(), text().length()); | 584 size_t selection_end = selection_model.selection_end(); |
|
msw
2011/09/29 02:20:21
Can you add a RenderTextTest for this new behavior
xji
2011/10/03 23:18:57
this function is reverted.
| |
| 571 selection_model_.set_selection_end(selection_model.selection_end()); | 585 DCHECK_LE(selection_end, text().length()); |
| 572 DCHECK_LT(selection_model.caret_pos(), | 586 selection_model_.set_selection_end(selection_end); |
| 573 std::max(text().length(), static_cast<size_t>(1))); | 587 if (selection_model.caret_placement() == |
| 574 selection_model_.set_caret_pos(selection_model.caret_pos()); | 588 gfx::SelectionModel::TRAILING_OF_PREVIOUS_GRAPHEME) { |
| 575 selection_model_.set_caret_placement(selection_model.caret_placement()); | 589 selection_model_.set_caret_pos(GetIndexOfPreviousGrapheme(selection_end)); |
|
msw
2011/09/29 02:20:21
You need to check if GetIndexOfPreviousGrapheme re
| |
| 590 selection_model_.set_caret_placement(gfx::SelectionModel::TRAILING); | |
| 591 } else { | |
| 592 DCHECK_LT(selection_model.caret_pos(), | |
| 593 std::max(text().length(), static_cast<size_t>(1))); | |
| 594 selection_model_.set_caret_pos(selection_model.caret_pos()); | |
| 595 selection_model_.set_caret_placement(selection_model.caret_placement()); | |
| 596 } | |
| 576 | 597 |
| 577 cached_bounds_and_offset_valid_ = false; | 598 cached_bounds_and_offset_valid_ = false; |
| 578 } | 599 } |
| 579 | 600 |
| 580 void RenderText::MoveCursorTo(size_t position, bool select) { | 601 void RenderText::MoveCursorTo(size_t position, bool select) { |
| 581 size_t cursor = std::min(position, text().length()); | 602 size_t cursor = std::min(position, text().length()); |
| 582 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); | 603 size_t caret_pos = GetIndexOfPreviousGrapheme(cursor); |
| 583 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? | 604 SelectionModel::CaretPlacement placement = (caret_pos == cursor) ? |
| 584 SelectionModel::LEADING : SelectionModel::TRAILING; | 605 SelectionModel::LEADING : SelectionModel::TRAILING; |
| 585 size_t selection_start = select ? GetSelectionStart() : cursor; | 606 size_t selection_start = select ? GetSelectionStart() : cursor; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 615 // TODO(xji): have similar problem as above when overflow character is a | 636 // TODO(xji): have similar problem as above when overflow character is a |
| 616 // LTR character. | 637 // LTR character. |
| 617 // | 638 // |
| 618 // Pan to show the cursor when it overflows to the left. | 639 // Pan to show the cursor when it overflows to the left. |
| 619 delta_offset = display_rect_.x() - cursor_bounds_.x(); | 640 delta_offset = display_rect_.x() - cursor_bounds_.x(); |
| 620 } | 641 } |
| 621 display_offset_.Offset(delta_offset, 0); | 642 display_offset_.Offset(delta_offset, 0); |
| 622 cursor_bounds_.Offset(delta_offset, 0); | 643 cursor_bounds_.Offset(delta_offset, 0); |
| 623 } | 644 } |
| 624 | 645 |
| 625 SelectionModel RenderText::GetSelectionModelForSelectionStart() { | |
| 626 size_t selection_start = GetSelectionStart(); | |
| 627 size_t selection_end = GetCursorPosition(); | |
| 628 if (selection_start < selection_end) | |
| 629 return SelectionModel(selection_start, | |
| 630 selection_start, | |
| 631 SelectionModel::LEADING); | |
| 632 else if (selection_start > selection_end) | |
| 633 return SelectionModel(selection_start, | |
| 634 GetIndexOfPreviousGrapheme(selection_start), | |
| 635 SelectionModel::TRAILING); | |
| 636 return selection_model_; | |
| 637 } | |
| 638 | |
| 639 } // namespace gfx | 646 } // namespace gfx |
| OLD | NEW |