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