Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(655)

Side by Side Diff: ui/gfx/render_text.cc

Issue 15746013: Fix cursor positioning regression from r201136. GetCursorPos() shouldn't assume (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 646 matching lines...) Expand 10 before | Expand all | Expand 10 after
657 Rect RenderText::GetCursorBounds(const SelectionModel& caret, 657 Rect RenderText::GetCursorBounds(const SelectionModel& caret,
658 bool insert_mode) { 658 bool insert_mode) {
659 EnsureLayout(); 659 EnsureLayout();
660 660
661 size_t caret_pos = caret.caret_pos(); 661 size_t caret_pos = caret.caret_pos();
662 DCHECK(IsCursorablePosition(caret_pos)); 662 DCHECK(IsCursorablePosition(caret_pos));
663 // In overtype mode, ignore the affinity and always indicate that we will 663 // In overtype mode, ignore the affinity and always indicate that we will
664 // overtype the next character. 664 // overtype the next character.
665 LogicalCursorDirection caret_affinity = 665 LogicalCursorDirection caret_affinity =
666 insert_mode ? caret.caret_affinity() : CURSOR_FORWARD; 666 insert_mode ? caret.caret_affinity() : CURSOR_FORWARD;
667 int x = 0, width = 1, height = 0; 667 int x = 0, width = 1;
668 Size size = GetStringSize();
668 if (caret_pos == (caret_affinity == CURSOR_BACKWARD ? 0 : text().length())) { 669 if (caret_pos == (caret_affinity == CURSOR_BACKWARD ? 0 : text().length())) {
669 // The caret is attached to the boundary. Always return a 1-dip width caret, 670 // The caret is attached to the boundary. Always return a 1-dip width caret,
670 // since there is nothing to overtype. 671 // since there is nothing to overtype.
671 Size size = GetStringSize();
672 if ((GetTextDirection() == base::i18n::RIGHT_TO_LEFT) == (caret_pos == 0)) 672 if ((GetTextDirection() == base::i18n::RIGHT_TO_LEFT) == (caret_pos == 0))
673 x = size.width(); 673 x = size.width();
674 height = size.height();
675 } else { 674 } else {
676 size_t grapheme_start = (caret_affinity == CURSOR_FORWARD) ? 675 size_t grapheme_start = (caret_affinity == CURSOR_FORWARD) ?
677 caret_pos : IndexOfAdjacentGrapheme(caret_pos, CURSOR_BACKWARD); 676 caret_pos : IndexOfAdjacentGrapheme(caret_pos, CURSOR_BACKWARD);
678 ui::Range xspan; 677 ui::Range xspan;
679 GetGlyphBounds(grapheme_start, &xspan, &height); 678 GetGlyphBounds(grapheme_start, &xspan);
680 if (insert_mode) { 679 if (insert_mode) {
681 x = (caret_affinity == CURSOR_BACKWARD) ? xspan.end() : xspan.start(); 680 x = (caret_affinity == CURSOR_BACKWARD) ? xspan.end() : xspan.start();
682 } else { // overtype mode 681 } else { // overtype mode
683 x = xspan.GetMin(); 682 x = xspan.GetMin();
684 width = xspan.length(); 683 width = xspan.length();
685 } 684 }
686 } 685 }
687 height = std::min(height, display_rect().height()); 686 return Rect(ToViewPoint(Point(x, 0)), Size(width, size.height()));
688 int y = (display_rect().height() - height) / 2;
689 return Rect(ToViewPoint(Point(x, y)), Size(width, height));
690 } 687 }
691 688
692 const Rect& RenderText::GetUpdatedCursorBounds() { 689 const Rect& RenderText::GetUpdatedCursorBounds() {
693 UpdateCachedBoundsAndOffset(); 690 UpdateCachedBoundsAndOffset();
694 return cursor_bounds_; 691 return cursor_bounds_;
695 } 692 }
696 693
697 size_t RenderText::IndexOfAdjacentGrapheme(size_t index, 694 size_t RenderText::IndexOfAdjacentGrapheme(size_t index,
698 LogicalCursorDirection direction) { 695 LogicalCursorDirection direction) {
699 if (index > text().length()) 696 if (index > text().length())
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
978 void RenderText::DrawSelection(Canvas* canvas) { 975 void RenderText::DrawSelection(Canvas* canvas) {
979 const SkColor color = focused() ? 976 const SkColor color = focused() ?
980 selection_background_focused_color_ : 977 selection_background_focused_color_ :
981 selection_background_unfocused_color_; 978 selection_background_unfocused_color_;
982 const std::vector<Rect> sel = GetSubstringBounds(selection()); 979 const std::vector<Rect> sel = GetSubstringBounds(selection());
983 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) 980 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i)
984 canvas->FillRect(*i, color); 981 canvas->FillRect(*i, color);
985 } 982 }
986 983
987 } // namespace gfx 984 } // namespace gfx
OLDNEW
« ui/gfx/render_text.h ('K') | « ui/gfx/render_text.h ('k') | ui/gfx/render_text_linux.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698