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 <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 | 10 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 // readable over it. | 83 // readable over it. |
84 void SetDisplayedOnBackgroundColor(SkColor color); | 84 void SetDisplayedOnBackgroundColor(SkColor color); |
85 SkColor displayed_on_background_color() const { | 85 SkColor displayed_on_background_color() const { |
86 return displayed_on_background_color_; | 86 return displayed_on_background_color_; |
87 } | 87 } |
88 | 88 |
89 void set_auto_color_readability_enabled(bool auto_color_readability) { | 89 void set_auto_color_readability_enabled(bool auto_color_readability) { |
90 auto_color_readability_enabled_ = auto_color_readability; | 90 auto_color_readability_enabled_ = auto_color_readability; |
91 } | 91 } |
92 | 92 |
| 93 // Resizes the label so its width is set to the width of the longest line and |
| 94 // its height deduced accordingly. |
| 95 // This is only intended for multi-line labels and is useful when the label's |
| 96 // text contains several lines separated with \n. |
| 97 // |max_width| is the maximum width that will be used (longer lines will be |
| 98 // wrapped). If 0, no maximum width is enforced. |
| 99 void SizeToFit(int max_width); |
| 100 |
93 // View implementation: | 101 // View implementation: |
94 gfx::Insets GetInsets() const override; | 102 gfx::Insets GetInsets() const override; |
95 int GetHeightForWidth(int w) const override; | 103 int GetHeightForWidth(int w) const override; |
96 void Layout() override; | 104 void Layout() override; |
97 void PreferredSizeChanged() override; | 105 void PreferredSizeChanged() override; |
98 | 106 |
99 // LinkListener implementation: | 107 // LinkListener implementation: |
100 void LinkClicked(Link* source, int event_flags) override; | 108 void LinkClicked(Link* source, int event_flags) override; |
101 | 109 |
102 private: | 110 private: |
103 struct StyleRange { | 111 struct StyleRange { |
104 StyleRange(const gfx::Range& range, | 112 StyleRange(const gfx::Range& range, |
105 const RangeStyleInfo& style_info) | 113 const RangeStyleInfo& style_info) |
106 : range(range), | 114 : range(range), |
107 style_info(style_info) { | 115 style_info(style_info) { |
108 } | 116 } |
109 ~StyleRange() {} | 117 ~StyleRange() {} |
110 | 118 |
111 bool operator<(const StyleRange& other) const; | 119 bool operator<(const StyleRange& other) const; |
112 | 120 |
113 gfx::Range range; | 121 gfx::Range range; |
114 RangeStyleInfo style_info; | 122 RangeStyleInfo style_info; |
115 }; | 123 }; |
116 typedef std::list<StyleRange> StyleRanges; | 124 typedef std::list<StyleRange> StyleRanges; |
117 | 125 |
118 // Calculates how to layout child views, creates them and sets their size | 126 // Calculates how to layout child views, creates them and sets their size and |
119 // and position. |width| is the horizontal space, in pixels, that the view | 127 // position. |width| is the horizontal space, in pixels, that the view has to |
120 // has to work with. If |dry_run| is true, the view hierarchy is not touched. | 128 // work with. If |dry_run| is true, the view hierarchy is not touched. Caches |
121 // The return value is the necessary size. | 129 // the results in |calculated_size_|, |width_at_last_layout_|, and |
| 130 // |width_at_last_size_calculation_|. Returns the needed size. |
122 gfx::Size CalculateAndDoLayout(int width, bool dry_run); | 131 gfx::Size CalculateAndDoLayout(int width, bool dry_run); |
123 | 132 |
124 // The text to display. | 133 // The text to display. |
125 base::string16 text_; | 134 base::string16 text_; |
126 | 135 |
127 // Fonts used to display text. Can be augmented by RangeStyleInfo. | 136 // Fonts used to display text. Can be augmented by RangeStyleInfo. |
128 gfx::FontList font_list_; | 137 gfx::FontList font_list_; |
129 | 138 |
130 // Line height. | 139 // Line height. |
131 int specified_line_height_; | 140 int specified_line_height_; |
132 | 141 |
133 // The default style to use for any part of the text that isn't within | 142 // The default style to use for any part of the text that isn't within |
134 // a range in |style_ranges_|. | 143 // a range in |style_ranges_|. |
135 RangeStyleInfo default_style_info_; | 144 RangeStyleInfo default_style_info_; |
136 | 145 |
137 // The listener that will be informed of link clicks. | 146 // The listener that will be informed of link clicks. |
138 StyledLabelListener* listener_; | 147 StyledLabelListener* listener_; |
139 | 148 |
140 // The ranges that should be linkified, sorted by start position. | 149 // The ranges that should be linkified, sorted by start position. |
141 StyleRanges style_ranges_; | 150 StyleRanges style_ranges_; |
142 | 151 |
143 // A mapping from a view to the range it corresponds to in |text_|. Only views | 152 // A mapping from a view to the range it corresponds to in |text_|. Only views |
144 // that correspond to ranges with is_link style set will be added to the map. | 153 // that correspond to ranges with is_link style set will be added to the map. |
145 std::map<View*, gfx::Range> link_targets_; | 154 std::map<View*, gfx::Range> link_targets_; |
146 | 155 |
147 // This variable saves the result of the last GetHeightForWidth call in order | 156 // This variable saves the result of the last GetHeightForWidth call in order |
148 // to avoid repeated calculation. | 157 // to avoid repeated calculation. |
149 mutable gfx::Size calculated_size_; | 158 mutable gfx::Size calculated_size_; |
| 159 mutable int width_at_last_size_calculation_; |
150 int width_at_last_layout_; | 160 int width_at_last_layout_; |
151 | 161 |
152 // Background color on which the label is drawn, for auto color readability. | 162 // Background color on which the label is drawn, for auto color readability. |
153 SkColor displayed_on_background_color_; | 163 SkColor displayed_on_background_color_; |
154 bool displayed_on_background_color_set_; | 164 bool displayed_on_background_color_set_; |
155 | 165 |
156 // Controls whether the text is automatically re-colored to be readable on the | 166 // Controls whether the text is automatically re-colored to be readable on the |
157 // background. | 167 // background. |
158 bool auto_color_readability_enabled_; | 168 bool auto_color_readability_enabled_; |
159 | 169 |
160 DISALLOW_COPY_AND_ASSIGN(StyledLabel); | 170 DISALLOW_COPY_AND_ASSIGN(StyledLabel); |
161 }; | 171 }; |
162 | 172 |
163 } // namespace views | 173 } // namespace views |
164 | 174 |
165 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_ | 175 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_ |
OLD | NEW |