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

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
« no previous file with comments | « ui/gfx/render_text.h ('k') | ui/gfx/render_text_linux.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(GetGlyphBounds(grapheme_start));
679 GetGlyphBounds(grapheme_start, &xspan, &height);
680 if (insert_mode) { 678 if (insert_mode) {
681 x = (caret_affinity == CURSOR_BACKWARD) ? xspan.end() : xspan.start(); 679 x = (caret_affinity == CURSOR_BACKWARD) ? xspan.end() : xspan.start();
682 } else { // overtype mode 680 } else { // overtype mode
683 x = xspan.GetMin(); 681 x = xspan.GetMin();
684 width = xspan.length(); 682 width = xspan.length();
685 } 683 }
686 } 684 }
687 height = std::min(height, display_rect().height()); 685 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 } 686 }
691 687
692 const Rect& RenderText::GetUpdatedCursorBounds() { 688 const Rect& RenderText::GetUpdatedCursorBounds() {
693 UpdateCachedBoundsAndOffset(); 689 UpdateCachedBoundsAndOffset();
694 return cursor_bounds_; 690 return cursor_bounds_;
695 } 691 }
696 692
697 size_t RenderText::IndexOfAdjacentGrapheme(size_t index, 693 size_t RenderText::IndexOfAdjacentGrapheme(size_t index,
698 LogicalCursorDirection direction) { 694 LogicalCursorDirection direction) {
699 if (index > text().length()) 695 if (index > text().length())
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
978 void RenderText::DrawSelection(Canvas* canvas) { 974 void RenderText::DrawSelection(Canvas* canvas) {
979 const SkColor color = focused() ? 975 const SkColor color = focused() ?
980 selection_background_focused_color_ : 976 selection_background_focused_color_ :
981 selection_background_unfocused_color_; 977 selection_background_unfocused_color_;
982 const std::vector<Rect> sel = GetSubstringBounds(selection()); 978 const std::vector<Rect> sel = GetSubstringBounds(selection());
983 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) 979 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i)
984 canvas->FillRect(*i, color); 980 canvas->FillRect(*i, color);
985 } 981 }
986 982
987 } // namespace gfx 983 } // namespace gfx
OLDNEW
« no previous file with comments | « 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