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/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/i18n/break_iterator.h" | 10 #include "base/i18n/break_iterator.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/utf_offset_string_conversions.h" |
13 #include "third_party/skia/include/core/SkTypeface.h" | 14 #include "third_party/skia/include/core/SkTypeface.h" |
14 #include "third_party/skia/include/effects/SkGradientShader.h" | 15 #include "third_party/skia/include/effects/SkGradientShader.h" |
15 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
16 #include "ui/gfx/canvas_skia.h" | 17 #include "ui/gfx/canvas_skia.h" |
17 #include "ui/gfx/native_theme.h" | 18 #include "ui/gfx/native_theme.h" |
18 #include "unicode/uchar.h" | |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
| 22 // All chars are replaced by this char when the password style is set. |
| 23 // TODO(benrg): GTK uses the first of U+25CF, U+2022, U+2731, U+273A, '*' |
| 24 // that's available in the font (find_invisible_char() in gtkentry.c). |
| 25 const char16 kPasswordReplacementChar = '*'; |
| 26 |
22 // Color settings for text, backgrounds and cursor. | 27 // Color settings for text, backgrounds and cursor. |
23 // These are tentative, and should be derived from theme, system | 28 // These are tentative, and should be derived from theme, system |
24 // settings and current settings. | 29 // settings and current settings. |
25 // TODO(oshima): Change this to match the standard chrome | 30 // TODO(oshima): Change this to match the standard chrome |
26 // before dogfooding textfield views. | 31 // before dogfooding textfield views. |
27 const SkColor kCursorColor = SK_ColorBLACK; | 32 const SkColor kCursorColor = SK_ColorBLACK; |
28 | 33 |
29 #ifndef NDEBUG | 34 #ifndef NDEBUG |
30 // Check StyleRanges invariant conditions: sorted and non-overlapping ranges. | 35 // Check StyleRanges invariant conditions: sorted and non-overlapping ranges. |
31 void CheckStyleRanges(const gfx::StyleRanges& style_ranges, size_t length) { | 36 void CheckStyleRanges(const gfx::StyleRanges& style_ranges, size_t length) { |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 | 369 |
365 const Font& RenderText::GetFont() const { | 370 const Font& RenderText::GetFont() const { |
366 return font_list_.GetFonts()[0]; | 371 return font_list_.GetFonts()[0]; |
367 } | 372 } |
368 | 373 |
369 void RenderText::ToggleInsertMode() { | 374 void RenderText::ToggleInsertMode() { |
370 insert_mode_ = !insert_mode_; | 375 insert_mode_ = !insert_mode_; |
371 cached_bounds_and_offset_valid_ = false; | 376 cached_bounds_and_offset_valid_ = false; |
372 } | 377 } |
373 | 378 |
| 379 void RenderText::SetObscured(bool obscured) { |
| 380 if (obscured != obscured_) { |
| 381 obscured_ = obscured; |
| 382 cached_bounds_and_offset_valid_ = false; |
| 383 UpdateLayout(); |
| 384 } |
| 385 } |
| 386 |
374 void RenderText::SetDisplayRect(const Rect& r) { | 387 void RenderText::SetDisplayRect(const Rect& r) { |
375 display_rect_ = r; | 388 display_rect_ = r; |
376 cached_bounds_and_offset_valid_ = false; | 389 cached_bounds_and_offset_valid_ = false; |
377 UpdateLayout(); | 390 UpdateLayout(); |
378 } | 391 } |
379 | 392 |
380 size_t RenderText::GetCursorPosition() const { | 393 size_t RenderText::GetCursorPosition() const { |
381 return selection_model_.selection_end(); | 394 return selection_model_.selection_end(); |
382 } | 395 } |
383 | 396 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 SetSelectionModel(sel); | 500 SetSelectionModel(sel); |
488 } | 501 } |
489 | 502 |
490 void RenderText::SelectAll() { | 503 void RenderText::SelectAll() { |
491 SelectionModel sel = EdgeSelectionModel(CURSOR_RIGHT); | 504 SelectionModel sel = EdgeSelectionModel(CURSOR_RIGHT); |
492 sel.set_selection_start(EdgeSelectionModel(CURSOR_LEFT).selection_start()); | 505 sel.set_selection_start(EdgeSelectionModel(CURSOR_LEFT).selection_start()); |
493 SetSelectionModel(sel); | 506 SetSelectionModel(sel); |
494 } | 507 } |
495 | 508 |
496 void RenderText::SelectWord() { | 509 void RenderText::SelectWord() { |
| 510 if (obscured_) { |
| 511 SelectAll(); |
| 512 return; |
| 513 } |
| 514 |
497 size_t cursor_position = GetCursorPosition(); | 515 size_t cursor_position = GetCursorPosition(); |
498 | 516 |
499 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); | 517 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); |
500 bool success = iter.Init(); | 518 bool success = iter.Init(); |
501 DCHECK(success); | 519 DCHECK(success); |
502 if (!success) | 520 if (!success) |
503 return; | 521 return; |
504 | 522 |
505 size_t selection_start = cursor_position; | 523 size_t selection_start = cursor_position; |
506 for (; selection_start != 0; --selection_start) { | 524 for (; selection_start != 0; --selection_start) { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
605 return selection_model_; | 623 return selection_model_; |
606 } | 624 } |
607 | 625 |
608 RenderText::RenderText() | 626 RenderText::RenderText() |
609 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT), | 627 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT), |
610 cursor_enabled_(true), | 628 cursor_enabled_(true), |
611 cursor_visible_(false), | 629 cursor_visible_(false), |
612 insert_mode_(true), | 630 insert_mode_(true), |
613 focused_(false), | 631 focused_(false), |
614 composition_range_(ui::Range::InvalidRange()), | 632 composition_range_(ui::Range::InvalidRange()), |
| 633 obscured_(false), |
615 fade_head_(false), | 634 fade_head_(false), |
616 fade_tail_(false), | 635 fade_tail_(false), |
617 cached_bounds_and_offset_valid_(false) { | 636 cached_bounds_and_offset_valid_(false) { |
618 } | 637 } |
619 | 638 |
620 const Point& RenderText::GetUpdatedDisplayOffset() { | 639 const Point& RenderText::GetUpdatedDisplayOffset() { |
621 UpdateCachedBoundsAndOffset(); | 640 UpdateCachedBoundsAndOffset(); |
622 return display_offset_; | 641 return display_offset_; |
623 } | 642 } |
624 | 643 |
(...skipping 16 matching lines...) Expand all Loading... |
641 selection_model_.set_selection_start(model.selection_start()); | 660 selection_model_.set_selection_start(model.selection_start()); |
642 DCHECK_LE(model.selection_end(), text().length()); | 661 DCHECK_LE(model.selection_end(), text().length()); |
643 selection_model_.set_selection_end(model.selection_end()); | 662 selection_model_.set_selection_end(model.selection_end()); |
644 DCHECK_LT(model.caret_pos(), std::max<size_t>(text().length(), 1)); | 663 DCHECK_LT(model.caret_pos(), std::max<size_t>(text().length(), 1)); |
645 selection_model_.set_caret_pos(model.caret_pos()); | 664 selection_model_.set_caret_pos(model.caret_pos()); |
646 selection_model_.set_caret_placement(model.caret_placement()); | 665 selection_model_.set_caret_placement(model.caret_placement()); |
647 | 666 |
648 cached_bounds_and_offset_valid_ = false; | 667 cached_bounds_and_offset_valid_ = false; |
649 } | 668 } |
650 | 669 |
| 670 string16 RenderText::GetDisplayText() const { |
| 671 if (!obscured_) |
| 672 return text_; |
| 673 size_t obscured_text_length = |
| 674 static_cast<size_t>(Utf16IndexToOffset(text_, 0, text_.length())); |
| 675 return string16(obscured_text_length, kPasswordReplacementChar); |
| 676 } |
| 677 |
651 void RenderText::ApplyCompositionAndSelectionStyles( | 678 void RenderText::ApplyCompositionAndSelectionStyles( |
652 StyleRanges* style_ranges) { | 679 StyleRanges* style_ranges) { |
653 // TODO(msw): This pattern ought to be reconsidered; what about composition | 680 // TODO(msw): This pattern ought to be reconsidered; what about composition |
654 // and selection overlaps, retain existing local style features? | 681 // and selection overlaps, retain existing local style features? |
655 // Apply a composition style override to a copy of the style ranges. | 682 // Apply a composition style override to a copy of the style ranges. |
656 if (composition_range_.IsValid() && !composition_range_.is_empty()) { | 683 if (composition_range_.IsValid() && !composition_range_.is_empty()) { |
657 StyleRange composition_style(default_style_); | 684 StyleRange composition_style(default_style_); |
658 composition_style.underline = true; | 685 composition_style.underline = true; |
659 composition_style.range.set_start(composition_range_.start()); | 686 composition_style.range.set_start(composition_range_.start()); |
660 composition_style.range.set_end(composition_range_.end()); | 687 composition_style.range.set_end(composition_range_.end()); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 if (cursor_enabled() && cursor_visible() && focused()) { | 879 if (cursor_enabled() && cursor_visible() && focused()) { |
853 const Rect& bounds = GetUpdatedCursorBounds(); | 880 const Rect& bounds = GetUpdatedCursorBounds(); |
854 if (bounds.width() != 0) | 881 if (bounds.width() != 0) |
855 canvas->FillRect(bounds, kCursorColor); | 882 canvas->FillRect(bounds, kCursorColor); |
856 else | 883 else |
857 canvas->DrawRect(bounds, kCursorColor); | 884 canvas->DrawRect(bounds, kCursorColor); |
858 } | 885 } |
859 } | 886 } |
860 | 887 |
861 } // namespace gfx | 888 } // namespace gfx |
OLD | NEW |