| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 Rect clip_rect(display_rect()); | 604 Rect clip_rect(display_rect()); |
| 605 clip_rect.Inset(ShadowValue::GetMargin(text_shadows_)); | 605 clip_rect.Inset(ShadowValue::GetMargin(text_shadows_)); |
| 606 | 606 |
| 607 canvas->Save(); | 607 canvas->Save(); |
| 608 canvas->ClipRect(clip_rect); | 608 canvas->ClipRect(clip_rect); |
| 609 } | 609 } |
| 610 | 610 |
| 611 if (!text().empty()) | 611 if (!text().empty()) |
| 612 DrawSelection(canvas); | 612 DrawSelection(canvas); |
| 613 | 613 |
| 614 DrawCursor(canvas); | 614 if (cursor_enabled() && cursor_visible() && focused()) |
| 615 DrawCursor(canvas, selection_model_); |
| 615 | 616 |
| 616 if (!text().empty()) | 617 if (!text().empty()) |
| 617 DrawVisualText(canvas); | 618 DrawVisualText(canvas); |
| 618 | 619 |
| 619 if (clip_to_display_rect()) | 620 if (clip_to_display_rect()) |
| 620 canvas->Restore(); | 621 canvas->Restore(); |
| 621 } | 622 } |
| 622 | 623 |
| 624 void RenderText::DrawCursor(Canvas* canvas, const SelectionModel& position) { |
| 625 // Paint cursor. Replace cursor is drawn as rectangle for now. |
| 626 // TODO(msw): Draw a better cursor with a better indication of association. |
| 627 canvas->FillRect(GetCursorBounds(position, true), cursor_color_); |
| 628 } |
| 629 |
| 623 void RenderText::DrawSelectedText(Canvas* canvas) { | 630 void RenderText::DrawSelectedText(Canvas* canvas) { |
| 624 EnsureLayout(); | 631 EnsureLayout(); |
| 625 const std::vector<Rect> sel = GetSubstringBounds(selection()); | 632 const std::vector<Rect> sel = GetSubstringBounds(selection()); |
| 626 for (size_t i = 0; i < sel.size(); ++i) { | 633 for (size_t i = 0; i < sel.size(); ++i) { |
| 627 canvas->Save(); | 634 canvas->Save(); |
| 628 canvas->ClipRect(sel[i]); | 635 canvas->ClipRect(sel[i]); |
| 629 DrawVisualText(canvas); | 636 DrawVisualText(canvas); |
| 630 canvas->Restore(); | 637 canvas->Restore(); |
| 631 } | 638 } |
| 632 } | 639 } |
| 633 | 640 |
| 634 Rect RenderText::GetCursorBounds(const SelectionModel& caret, | 641 Rect RenderText::GetCursorBounds(const SelectionModel& caret, |
| 635 bool insert_mode) { | 642 bool insert_mode) { |
| 636 EnsureLayout(); | 643 EnsureLayout(); |
| 637 | 644 |
| 638 size_t caret_pos = caret.caret_pos(); | 645 size_t caret_pos = caret.caret_pos(); |
| 646 DCHECK(IsCursorablePosition(caret_pos)); |
| 639 // In overtype mode, ignore the affinity and always indicate that we will | 647 // In overtype mode, ignore the affinity and always indicate that we will |
| 640 // overtype the next character. | 648 // overtype the next character. |
| 641 LogicalCursorDirection caret_affinity = | 649 LogicalCursorDirection caret_affinity = |
| 642 insert_mode ? caret.caret_affinity() : CURSOR_FORWARD; | 650 insert_mode ? caret.caret_affinity() : CURSOR_FORWARD; |
| 643 int x = 0, width = 1, height = 0; | 651 int x = 0, width = 1, height = 0; |
| 644 if (caret_pos == (caret_affinity == CURSOR_BACKWARD ? 0 : text().length())) { | 652 if (caret_pos == (caret_affinity == CURSOR_BACKWARD ? 0 : text().length())) { |
| 645 // The caret is attached to the boundary. Always return a 1-dip width caret, | 653 // The caret is attached to the boundary. Always return a 1-dip width caret, |
| 646 // since there is nothing to overtype. | 654 // since there is nothing to overtype. |
| 647 Size size = GetStringSize(); | 655 Size size = GetStringSize(); |
| 648 if ((GetTextDirection() == base::i18n::RIGHT_TO_LEFT) == (caret_pos == 0)) | 656 if ((GetTextDirection() == base::i18n::RIGHT_TO_LEFT) == (caret_pos == 0)) |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 } | 964 } |
| 957 | 965 |
| 958 void RenderText::DrawSelection(Canvas* canvas) { | 966 void RenderText::DrawSelection(Canvas* canvas) { |
| 959 const SkColor color = focused() ? selection_background_focused_color_ : | 967 const SkColor color = focused() ? selection_background_focused_color_ : |
| 960 selection_background_unfocused_color_; | 968 selection_background_unfocused_color_; |
| 961 const std::vector<Rect> sel = GetSubstringBounds(selection()); | 969 const std::vector<Rect> sel = GetSubstringBounds(selection()); |
| 962 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) | 970 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) |
| 963 canvas->FillRect(*i, color); | 971 canvas->FillRect(*i, color); |
| 964 } | 972 } |
| 965 | 973 |
| 966 void RenderText::DrawCursor(Canvas* canvas) { | |
| 967 // Paint cursor. Replace cursor is drawn as rectangle for now. | |
| 968 // TODO(msw): Draw a better cursor with a better indication of association. | |
| 969 if (cursor_enabled() && cursor_visible() && focused()) { | |
| 970 canvas->FillRect(GetUpdatedCursorBounds(), | |
| 971 insert_mode_ ? cursor_color_ : selection_background_unfocused_color_); | |
| 972 } | |
| 973 } | |
| 974 | |
| 975 } // namespace gfx | 974 } // namespace gfx |
| OLD | NEW |