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 : line_height_multiplier_(1.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::SetLineHeightMultiplier(double multiplier) { | |
138 if (multiplier < 1.0) | |
sky
2014/06/25 23:29:09
DCHECK
Garrett Casto
2014/06/25 23:47:25
Done.
| |
139 return; | |
140 | |
141 line_height_multiplier_ = multiplier; | |
142 PreferredSizeChanged(); | |
143 } | |
144 | |
136 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { | 145 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { |
137 displayed_on_background_color_ = color; | 146 displayed_on_background_color_ = color; |
138 displayed_on_background_color_set_ = true; | 147 displayed_on_background_color_set_ = true; |
139 } | 148 } |
140 | 149 |
141 gfx::Insets StyledLabel::GetInsets() const { | 150 gfx::Insets StyledLabel::GetInsets() const { |
142 gfx::Insets insets = View::GetInsets(); | 151 gfx::Insets insets = View::GetInsets(); |
143 | 152 |
144 // We need a focus border iff we contain a link that will have a focus border. | 153 // 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. | 154 // 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) { | 195 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { |
187 if (!dry_run) { | 196 if (!dry_run) { |
188 RemoveAllChildViews(true); | 197 RemoveAllChildViews(true); |
189 link_targets_.clear(); | 198 link_targets_.clear(); |
190 } | 199 } |
191 | 200 |
192 width -= GetInsets().width(); | 201 width -= GetInsets().width(); |
193 if (width <= 0 || text_.empty()) | 202 if (width <= 0 || text_.empty()) |
194 return gfx::Size(); | 203 return gfx::Size(); |
195 | 204 |
196 const int line_height = CalculateLineHeight(font_list_); | 205 const int line_height = GetLineHeight(); |
197 // The index of the line we're on. | 206 // The index of the line we're on. |
198 int line = 0; | 207 int line = 0; |
199 // The x position (in pixels) of the line we're on, relative to content | 208 // The x position (in pixels) of the line we're on, relative to content |
200 // bounds. | 209 // bounds. |
201 int x = 0; | 210 int x = 0; |
202 | 211 |
203 base::string16 remaining_string = text_; | 212 base::string16 remaining_string = text_; |
204 StyleRanges::const_iterator current_range = style_ranges_.begin(); | 213 StyleRanges::const_iterator current_range = style_ranges_.begin(); |
205 | 214 |
206 // Iterate over the text, creating a bunch of labels and links and laying them | 215 // 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 | 297 |
289 if (displayed_on_background_color_set_) | 298 if (displayed_on_background_color_set_) |
290 label->SetBackgroundColor(displayed_on_background_color_); | 299 label->SetBackgroundColor(displayed_on_background_color_); |
291 label->SetAutoColorReadabilityEnabled(auto_color_readability_enabled_); | 300 label->SetAutoColorReadabilityEnabled(auto_color_readability_enabled_); |
292 | 301 |
293 // Calculate the size of the optional focus border, and overlap by that | 302 // Calculate the size of the optional focus border, and overlap by that |
294 // amount. Otherwise, "<a>link</a>," will render as "link ,". | 303 // amount. Otherwise, "<a>link</a>," will render as "link ,". |
295 gfx::Insets focus_border_insets(label->GetInsets()); | 304 gfx::Insets focus_border_insets(label->GetInsets()); |
296 focus_border_insets += -label->View::GetInsets(); | 305 focus_border_insets += -label->View::GetInsets(); |
297 const gfx::Size view_size = label->GetPreferredSize(); | 306 const gfx::Size view_size = label->GetPreferredSize(); |
298 DCHECK_EQ(line_height, view_size.height() - focus_border_insets.height()); | |
299 if (!dry_run) { | 307 if (!dry_run) { |
300 label->SetBoundsRect(gfx::Rect( | 308 label->SetBoundsRect(gfx::Rect( |
301 gfx::Point(GetInsets().left() + x - focus_border_insets.left(), | 309 gfx::Point(GetInsets().left() + x - focus_border_insets.left(), |
302 GetInsets().top() + line * line_height - | 310 GetInsets().top() + GetOffsetForLine(line) - |
303 focus_border_insets.top()), | 311 focus_border_insets.top()), |
304 view_size)); | 312 view_size)); |
305 AddChildView(label.release()); | 313 AddChildView(label.release()); |
306 } | 314 } |
307 x += view_size.width() - focus_border_insets.width(); | 315 x += view_size.width() - focus_border_insets.width(); |
308 | 316 |
309 remaining_string = remaining_string.substr(chunk.size()); | 317 remaining_string = remaining_string.substr(chunk.size()); |
310 } | 318 } |
311 | 319 |
312 return gfx::Size(width, (line + 1) * line_height + GetInsets().height()); | 320 return gfx::Size( |
321 width, | |
322 (line + 1) * line_height + GetInsets().height()); | |
323 } | |
324 | |
325 int StyledLabel::GetLineHeight() const { | |
326 return CalculateLineHeight(font_list_) * line_height_multiplier_; | |
327 } | |
328 | |
329 int StyledLabel::GetOffsetForLine(int line) const { | |
330 int default_line_height = CalculateLineHeight(font_list_); | |
331 int line_height = GetLineHeight(); | |
332 | |
333 // Center text in the space available. | |
334 int interline_offset = (line_height - default_line_height) / 2; | |
335 return line_height * line + interline_offset; | |
313 } | 336 } |
314 | 337 |
315 } // namespace views | 338 } // namespace views |
OLD | NEW |