| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef UI_VIEWS_CONTROLS_STYLED_LABEL_H_ | 5 #ifndef UI_VIEWS_CONTROLS_STYLED_LABEL_H_ |
| 6 #define UI_VIEWS_CONTROLS_STYLED_LABEL_H_ | 6 #define UI_VIEWS_CONTROLS_STYLED_LABEL_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <queue> | 9 #include <queue> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "third_party/skia/include/core/SkColor.h" |
| 13 #include "ui/base/range/range.h" | 14 #include "ui/base/range/range.h" |
| 14 #include "ui/gfx/size.h" | 15 #include "ui/gfx/size.h" |
| 15 #include "ui/views/controls/link_listener.h" | 16 #include "ui/views/controls/link_listener.h" |
| 16 #include "ui/views/view.h" | 17 #include "ui/views/view.h" |
| 17 | 18 |
| 18 namespace views { | 19 namespace views { |
| 19 | 20 |
| 20 class Link; | 21 class Link; |
| 21 class StyledLabelListener; | 22 class StyledLabelListener; |
| 22 | 23 |
| 23 // A class which can apply mixed styles to a block of text. Currently, text is | 24 // A class which can apply mixed styles to a block of text. Currently, text is |
| 24 // always multiline. Trailing whitespace in the styled label text is not | 25 // always multiline. Trailing whitespace in the styled label text is not |
| 25 // supported and will be trimmed on StyledLabel construction. Leading | 26 // supported and will be trimmed on StyledLabel construction. Leading |
| 26 // whitespace is respected, provided not only whitespace fits in the first line. | 27 // whitespace is respected, provided not only whitespace fits in the first line. |
| 27 // In this case, leading whitespace is ignored. | 28 // In this case, leading whitespace is ignored. |
| 28 class VIEWS_EXPORT StyledLabel : public View, public LinkListener { | 29 class VIEWS_EXPORT StyledLabel : public View, public LinkListener { |
| 29 public: | 30 public: |
| 30 // Parameters that define label style for a styled label's text range. | 31 // Parameters that define label style for a styled label's text range. |
| 31 struct VIEWS_EXPORT RangeStyleInfo { | 32 struct VIEWS_EXPORT RangeStyleInfo { |
| 32 RangeStyleInfo(); | 33 RangeStyleInfo(); |
| 33 ~RangeStyleInfo(); | 34 ~RangeStyleInfo(); |
| 34 | 35 |
| 35 // Creates a range style info with default values for link. | 36 // Creates a range style info with default values for link. |
| 36 static RangeStyleInfo CreateForLink(); | 37 static RangeStyleInfo CreateForLink(); |
| 37 | 38 |
| 38 // The font style that will be applied to the range. Should be a bitmask of | 39 // The font style that will be applied to the range. Should be a bitmask of |
| 39 // values defined in gfx::Font::FontStyle (BOLD, ITALIC, UNDERLINE). | 40 // values defined in gfx::Font::FontStyle (BOLD, ITALIC, UNDERLINE). |
| 40 int font_style; | 41 int font_style; |
| 41 | 42 |
| 43 // The text color for the range. |
| 44 SkColor color; |
| 45 |
| 42 // Tooltip for the range. | 46 // Tooltip for the range. |
| 43 string16 tooltip; | 47 string16 tooltip; |
| 44 | 48 |
| 45 // If set, the whole range will be put on a single line. | 49 // If set, the whole range will be put on a single line. |
| 46 bool disable_line_wrapping; | 50 bool disable_line_wrapping; |
| 47 | 51 |
| 48 // If set, the range will be created as a link. | 52 // If set, the range will be created as a link. |
| 49 bool is_link; | 53 bool is_link; |
| 50 }; | 54 }; |
| 51 | 55 |
| 52 // Note that any trailing whitespace in |text| will be trimmed. | 56 // Note that any trailing whitespace in |text| will be trimmed. |
| 53 StyledLabel(const string16& text, StyledLabelListener* listener); | 57 StyledLabel(const string16& text, StyledLabelListener* listener); |
| 54 virtual ~StyledLabel(); | 58 virtual ~StyledLabel(); |
| 55 | 59 |
| 56 // Sets the text to be displayed, and clears any previous styling. | 60 // Sets the text to be displayed, and clears any previous styling. |
| 57 void SetText(const string16& text); | 61 void SetText(const string16& text); |
| 58 | 62 |
| 59 // Marks the given range within |text_| with style defined by |style_info|. | 63 // Marks the given range within |text_| with style defined by |style_info|. |
| 60 // |range| must be contained in |text_|. | 64 // |range| must be contained in |text_|. |
| 61 void AddStyleRange(const ui::Range& range, const RangeStyleInfo& style_info); | 65 void AddStyleRange(const ui::Range& range, const RangeStyleInfo& style_info); |
| 62 | 66 |
| 67 // Sets the color of the background on which the label is drawn. This won't |
| 68 // be explicitly drawn, but the label will force the text color to be |
| 69 // readable over it. |
| 70 void SetDisplayedOnBackgroundColor(SkColor color); |
| 71 SkColor displayed_on_background_color() const { |
| 72 return displayed_on_background_color_; |
| 73 } |
| 74 |
| 63 // View implementation: | 75 // View implementation: |
| 64 virtual gfx::Insets GetInsets() const OVERRIDE; | 76 virtual gfx::Insets GetInsets() const OVERRIDE; |
| 65 virtual int GetHeightForWidth(int w) OVERRIDE; | 77 virtual int GetHeightForWidth(int w) OVERRIDE; |
| 66 virtual void Layout() OVERRIDE; | 78 virtual void Layout() OVERRIDE; |
| 67 | 79 |
| 68 // LinkListener implementation: | 80 // LinkListener implementation: |
| 69 virtual void LinkClicked(Link* source, int event_flags) OVERRIDE; | 81 virtual void LinkClicked(Link* source, int event_flags) OVERRIDE; |
| 70 | 82 |
| 71 private: | 83 private: |
| 72 struct StyleRange { | 84 struct StyleRange { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 99 std::priority_queue<StyleRange> style_ranges_; | 111 std::priority_queue<StyleRange> style_ranges_; |
| 100 | 112 |
| 101 // A mapping from a view to the range it corresponds to in |text_|. Only views | 113 // A mapping from a view to the range it corresponds to in |text_|. Only views |
| 102 // that correspond to ranges with is_link style set will be added to the map. | 114 // that correspond to ranges with is_link style set will be added to the map. |
| 103 std::map<View*, ui::Range> link_targets_; | 115 std::map<View*, ui::Range> link_targets_; |
| 104 | 116 |
| 105 // This variable saves the result of the last GetHeightForWidth call in order | 117 // This variable saves the result of the last GetHeightForWidth call in order |
| 106 // to avoid repeated calculation. | 118 // to avoid repeated calculation. |
| 107 gfx::Size calculated_size_; | 119 gfx::Size calculated_size_; |
| 108 | 120 |
| 121 // Background color on which the label is drawn, for auto color readability. |
| 122 SkColor displayed_on_background_color_; |
| 123 bool displayed_on_background_color_set_; |
| 124 |
| 109 DISALLOW_COPY_AND_ASSIGN(StyledLabel); | 125 DISALLOW_COPY_AND_ASSIGN(StyledLabel); |
| 110 }; | 126 }; |
| 111 | 127 |
| 112 } // namespace views | 128 } // namespace views |
| 113 | 129 |
| 114 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_ | 130 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_ |
| OLD | NEW |