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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 : specified_line_height_(0), | 97 : specified_line_height_(0), |
98 listener_(listener), | 98 listener_(listener), |
| 99 width_at_last_layout_(0), |
| 100 displayed_on_background_color_(SkColorSetRGB(0xFF, 0xFF, 0xFF)), |
99 displayed_on_background_color_set_(false), | 101 displayed_on_background_color_set_(false), |
100 auto_color_readability_enabled_(true) { | 102 auto_color_readability_enabled_(true) { |
101 base::TrimWhitespace(text, base::TRIM_TRAILING, &text_); | 103 base::TrimWhitespace(text, base::TRIM_TRAILING, &text_); |
102 } | 104 } |
103 | 105 |
104 StyledLabel::~StyledLabel() {} | 106 StyledLabel::~StyledLabel() {} |
105 | 107 |
106 void StyledLabel::SetText(const base::string16& text) { | 108 void StyledLabel::SetText(const base::string16& text) { |
107 text_ = text; | 109 text_ = text; |
108 style_ranges_.clear(); | 110 style_ranges_.clear(); |
(...skipping 24 matching lines...) Expand all Loading... |
133 default_style_info_ = style_info; | 135 default_style_info_ = style_info; |
134 PreferredSizeChanged(); | 136 PreferredSizeChanged(); |
135 } | 137 } |
136 | 138 |
137 void StyledLabel::SetLineHeight(int line_height) { | 139 void StyledLabel::SetLineHeight(int line_height) { |
138 specified_line_height_ = line_height; | 140 specified_line_height_ = line_height; |
139 PreferredSizeChanged(); | 141 PreferredSizeChanged(); |
140 } | 142 } |
141 | 143 |
142 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { | 144 void StyledLabel::SetDisplayedOnBackgroundColor(SkColor color) { |
| 145 if (displayed_on_background_color_ == color && |
| 146 displayed_on_background_color_set_) |
| 147 return; |
| 148 |
143 displayed_on_background_color_ = color; | 149 displayed_on_background_color_ = color; |
144 displayed_on_background_color_set_ = true; | 150 displayed_on_background_color_set_ = true; |
| 151 |
| 152 for (int i = 0, count = child_count(); i < count; ++i) { |
| 153 DCHECK((child_at(i)->GetClassName() == Label::kViewClassName) || |
| 154 (child_at(i)->GetClassName() == Link::kViewClassName)); |
| 155 static_cast<Label*>(child_at(i))->SetBackgroundColor(color); |
| 156 } |
145 } | 157 } |
146 | 158 |
147 gfx::Insets StyledLabel::GetInsets() const { | 159 gfx::Insets StyledLabel::GetInsets() const { |
148 gfx::Insets insets = View::GetInsets(); | 160 gfx::Insets insets = View::GetInsets(); |
149 | 161 |
150 // We need a focus border iff we contain a link that will have a focus border. | 162 // We need a focus border iff we contain a link that will have a focus border. |
151 // That in turn will be true only if the link is non-empty. | 163 // That in turn will be true only if the link is non-empty. |
152 for (StyleRanges::const_iterator i(style_ranges_.begin()); | 164 for (StyleRanges::const_iterator i(style_ranges_.begin()); |
153 i != style_ranges_.end(); ++i) { | 165 i != style_ranges_.end(); ++i) { |
154 if (i->style_info.is_link && !i->range.is_empty()) { | 166 if (i->style_info.is_link && !i->range.is_empty()) { |
155 const gfx::Insets focus_border_padding( | 167 const gfx::Insets focus_border_padding( |
156 Label::kFocusBorderPadding, Label::kFocusBorderPadding, | 168 Label::kFocusBorderPadding, Label::kFocusBorderPadding, |
157 Label::kFocusBorderPadding, Label::kFocusBorderPadding); | 169 Label::kFocusBorderPadding, Label::kFocusBorderPadding); |
158 insets += focus_border_padding; | 170 insets += focus_border_padding; |
159 break; | 171 break; |
160 } | 172 } |
161 } | 173 } |
162 | 174 |
163 return insets; | 175 return insets; |
164 } | 176 } |
165 | 177 |
166 int StyledLabel::GetHeightForWidth(int w) const { | 178 int StyledLabel::GetHeightForWidth(int w) const { |
167 if (w != calculated_size_.width()) { | 179 // TODO(erg): Munge the const-ness of the style label. CalculateAndDoLayout |
168 // TODO(erg): Munge the const-ness of the style label. CalculateAndDoLayout | 180 // doesn't actually make any changes to member variables when |dry_run| is |
169 // doesn't actually make any changes to member variables when |dry_run| is | 181 // set to true. In general, the mutating and non-mutating parts shouldn't |
170 // set to true. In general, the mutating and non-mutating parts shouldn't | 182 // be in the same codepath. |
171 // be in the same codepath. | 183 calculated_size_ = |
172 calculated_size_ = | 184 const_cast<StyledLabel*>(this)->CalculateAndDoLayout(w, true); |
173 const_cast<StyledLabel*>(this)->CalculateAndDoLayout(w, true); | |
174 } | |
175 return calculated_size_.height(); | 185 return calculated_size_.height(); |
176 } | 186 } |
177 | 187 |
178 void StyledLabel::Layout() { | 188 void StyledLabel::Layout() { |
179 calculated_size_ = CalculateAndDoLayout(GetLocalBounds().width(), false); | 189 calculated_size_ = CalculateAndDoLayout(GetLocalBounds().width(), false); |
| 190 width_at_last_layout_ = calculated_size_.width(); |
180 } | 191 } |
181 | 192 |
182 void StyledLabel::PreferredSizeChanged() { | 193 void StyledLabel::PreferredSizeChanged() { |
183 calculated_size_ = gfx::Size(); | 194 calculated_size_ = gfx::Size(); |
184 View::PreferredSizeChanged(); | 195 View::PreferredSizeChanged(); |
185 } | 196 } |
186 | 197 |
187 void StyledLabel::LinkClicked(Link* source, int event_flags) { | 198 void StyledLabel::LinkClicked(Link* source, int event_flags) { |
188 if (listener_) | 199 if (listener_) |
189 listener_->StyledLabelLinkClicked(link_targets_[source], event_flags); | 200 listener_->StyledLabelLinkClicked(link_targets_[source], event_flags); |
190 } | 201 } |
191 | 202 |
192 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { | 203 gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { |
| 204 width -= GetInsets().width(); |
| 205 if (width == calculated_size_.width() && |
| 206 (dry_run || width_at_last_layout_ == width)) |
| 207 return calculated_size_; |
| 208 |
193 if (!dry_run) { | 209 if (!dry_run) { |
194 RemoveAllChildViews(true); | 210 RemoveAllChildViews(true); |
195 link_targets_.clear(); | 211 link_targets_.clear(); |
196 } | 212 } |
197 | 213 |
198 width -= GetInsets().width(); | |
199 if (width <= 0 || text_.empty()) | 214 if (width <= 0 || text_.empty()) |
200 return gfx::Size(); | 215 return gfx::Size(); |
201 | 216 |
202 const int line_height = specified_line_height_ > 0 ? specified_line_height_ | 217 const int line_height = specified_line_height_ > 0 ? specified_line_height_ |
203 : CalculateLineHeight(font_list_); | 218 : CalculateLineHeight(font_list_); |
204 // The index of the line we're on. | 219 // The index of the line we're on. |
205 int line = 0; | 220 int line = 0; |
206 // The x position (in pixels) of the line we're on, relative to content | 221 // The x position (in pixels) of the line we're on, relative to content |
207 // bounds. | 222 // bounds. |
208 int x = 0; | 223 int x = 0; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 } | 331 } |
317 | 332 |
318 // The user-specified line height only applies to interline spacing, so the | 333 // The user-specified line height only applies to interline spacing, so the |
319 // final line's height is unaffected. | 334 // final line's height is unaffected. |
320 int total_height = line * line_height + | 335 int total_height = line * line_height + |
321 CalculateLineHeight(font_list_) + GetInsets().height(); | 336 CalculateLineHeight(font_list_) + GetInsets().height(); |
322 return gfx::Size(width, total_height); | 337 return gfx::Size(width, total_height); |
323 } | 338 } |
324 | 339 |
325 } // namespace views | 340 } // namespace views |
OLD | NEW |