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 #include "ui/views/controls/styled_label.h" | 5 #include "ui/views/controls/styled_label.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "ui/gfx/font_list.h" | 10 #include "ui/gfx/font_list.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 bool StyledLabel::StyleRange::operator<( | 87 bool StyledLabel::StyleRange::operator<( |
88 const StyledLabel::StyleRange& other) const { | 88 const StyledLabel::StyleRange& other) const { |
89 return range.start() < other.range.start(); | 89 return range.start() < other.range.start(); |
90 } | 90 } |
91 | 91 |
92 | 92 |
93 // StyledLabel ---------------------------------------------------------------- | 93 // StyledLabel ---------------------------------------------------------------- |
94 | 94 |
95 StyledLabel::StyledLabel(const base::string16& text, | 95 StyledLabel::StyledLabel(const base::string16& text, |
96 StyledLabelListener* listener) | 96 StyledLabelListener* listener) |
97 : listener_(listener), | 97 : specified_line_height_(0), |
98 listener_(listener), | |
98 displayed_on_background_color_set_(false), | 99 displayed_on_background_color_set_(false), |
99 auto_color_readability_enabled_(true) { | 100 auto_color_readability_enabled_(true) { |
100 base::TrimWhitespace(text, base::TRIM_TRAILING, &text_); | 101 base::TrimWhitespace(text, base::TRIM_TRAILING, &text_); |
101 } | 102 } |
102 | 103 |
103 StyledLabel::~StyledLabel() {} | 104 StyledLabel::~StyledLabel() {} |
104 | 105 |
105 void StyledLabel::SetText(const base::string16& text) { | 106 void StyledLabel::SetText(const base::string16& text) { |
106 text_ = text; | 107 text_ = text; |
107 style_ranges_.clear(); | 108 style_ranges_.clear(); |
(...skipping 18 matching lines...) Expand all Loading... | |
126 style_ranges_.merge(new_range); | 127 style_ranges_.merge(new_range); |
127 | 128 |
128 PreferredSizeChanged(); | 129 PreferredSizeChanged(); |
129 } | 130 } |
130 | 131 |
131 void StyledLabel::SetDefaultStyle(const RangeStyleInfo& style_info) { | 132 void StyledLabel::SetDefaultStyle(const RangeStyleInfo& style_info) { |
132 default_style_info_ = style_info; | 133 default_style_info_ = style_info; |
133 PreferredSizeChanged(); | 134 PreferredSizeChanged(); |
134 } | 135 } |
135 | 136 |
137 void StyledLabel::SetLineHeight(int line_height) { | |
138 specified_line_height_ = line_height; | |
139 PreferredSizeChanged(); | |
140 } | |
141 | |
136 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { | 142 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { |
137 displayed_on_background_color_ = color; | 143 displayed_on_background_color_ = color; |
138 displayed_on_background_color_set_ = true; | 144 displayed_on_background_color_set_ = true; |
139 } | 145 } |
140 | 146 |
141 gfx::Insets StyledLabel::GetInsets() const { | 147 gfx::Insets StyledLabel::GetInsets() const { |
142 gfx::Insets insets = View::GetInsets(); | 148 gfx::Insets insets = View::GetInsets(); |
143 | 149 |
144 // We need a focus border iff we contain a link that will have a focus border. | 150 // We need a focus border iff we contain a link that will have a focus border. |
145 // That in turn will be true only if the link is non-empty. | 151 // That in turn will be true only if the link is non-empty. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { | 192 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { |
187 if (!dry_run) { | 193 if (!dry_run) { |
188 RemoveAllChildViews(true); | 194 RemoveAllChildViews(true); |
189 link_targets_.clear(); | 195 link_targets_.clear(); |
190 } | 196 } |
191 | 197 |
192 width -= GetInsets().width(); | 198 width -= GetInsets().width(); |
193 if (width <= 0 || text_.empty()) | 199 if (width <= 0 || text_.empty()) |
194 return gfx::Size(); | 200 return gfx::Size(); |
195 | 201 |
196 const int line_height = CalculateLineHeight(font_list_); | 202 const int line_height = specified_line_height_ > 0 ? specified_line_height_ |
203 : CalculateLineHeight(font_list_); | |
197 // The index of the line we're on. | 204 // The index of the line we're on. |
198 int line = 0; | 205 int line = 0; |
199 // The x position (in pixels) of the line we're on, relative to content | 206 // The x position (in pixels) of the line we're on, relative to content |
200 // bounds. | 207 // bounds. |
201 int x = 0; | 208 int x = 0; |
202 | 209 |
203 base::string16 remaining_string = text_; | 210 base::string16 remaining_string = text_; |
204 StyleRanges::const_iterator current_range = style_ranges_.begin(); | 211 StyleRanges::const_iterator current_range = style_ranges_.begin(); |
205 | 212 |
206 // Iterate over the text, creating a bunch of labels and links and laying them | 213 // Iterate over the text, creating a bunch of labels and links and laying them |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
288 | 295 |
289 if (displayed_on_background_color_set_) | 296 if (displayed_on_background_color_set_) |
290 label->SetBackgroundColor(displayed_on_background_color_); | 297 label->SetBackgroundColor(displayed_on_background_color_); |
291 label->SetAutoColorReadabilityEnabled(auto_color_readability_enabled_); | 298 label->SetAutoColorReadabilityEnabled(auto_color_readability_enabled_); |
292 | 299 |
293 // Calculate the size of the optional focus border, and overlap by that | 300 // Calculate the size of the optional focus border, and overlap by that |
294 // amount. Otherwise, "<a>link</a>," will render as "link ,". | 301 // amount. Otherwise, "<a>link</a>," will render as "link ,". |
295 gfx::Insets focus_border_insets(label->GetInsets()); | 302 gfx::Insets focus_border_insets(label->GetInsets()); |
296 focus_border_insets += -label->View::GetInsets(); | 303 focus_border_insets += -label->View::GetInsets(); |
297 const gfx::Size view_size = label->GetPreferredSize(); | 304 const gfx::Size view_size = label->GetPreferredSize(); |
298 DCHECK_EQ(line_height, view_size.height() - focus_border_insets.height()); | |
299 if (!dry_run) { | 305 if (!dry_run) { |
300 label->SetBoundsRect(gfx::Rect( | 306 label->SetBoundsRect(gfx::Rect( |
301 gfx::Point(GetInsets().left() + x - focus_border_insets.left(), | 307 gfx::Point(GetInsets().left() + x - focus_border_insets.left(), |
302 GetInsets().top() + line * line_height - | 308 GetInsets().top() + line * line_height - |
303 focus_border_insets.top()), | 309 focus_border_insets.top()), |
304 view_size)); | 310 view_size)); |
305 AddChildView(label.release()); | 311 AddChildView(label.release()); |
306 } | 312 } |
307 x += view_size.width() - focus_border_insets.width(); | 313 x += view_size.width() - focus_border_insets.width(); |
308 | 314 |
309 remaining_string = remaining_string.substr(chunk.size()); | 315 remaining_string = remaining_string.substr(chunk.size()); |
310 } | 316 } |
311 | 317 |
312 return gfx::Size(width, (line + 1) * line_height + GetInsets().height()); | 318 // The last line shouldn't extend past the text, so use CalculateLineHeight() |
Evan Stade
2014/07/15 22:46:28
"The last line shouldn't extend past the text"
It
Garrett Casto
2014/07/16 07:38:23
Done.
| |
319 // instead of the possibly user specified |line_height| for that line. | |
320 int total_height = line * line_height + | |
321 CalculateLineHeight(font_list_) + GetInsets().height(); | |
322 return gfx::Size(width, total_height); | |
313 } | 323 } |
314 | 324 |
315 } // namespace views | 325 } // namespace views |
OLD | NEW |