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/message_center/views/bounded_label.h" | 5 #include "ui/message_center/views/bounded_label.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 return gfx::Size(); | 106 return gfx::Size(); |
107 std::pair<int, int> key(width, lines); | 107 std::pair<int, int> key(width, lines); |
108 gfx::Size size = GetCachedSize(key); | 108 gfx::Size size = GetCachedSize(key); |
109 if (size.height() == std::numeric_limits<int>::max()) { | 109 if (size.height() == std::numeric_limits<int>::max()) { |
110 gfx::Insets insets = owner_->GetInsets(); | 110 gfx::Insets insets = owner_->GetInsets(); |
111 int text_width = (width < 0) ? std::numeric_limits<int>::max() : | 111 int text_width = (width < 0) ? std::numeric_limits<int>::max() : |
112 std::max(width - insets.width(), 0); | 112 std::max(width - insets.width(), 0); |
113 int text_height = std::numeric_limits<int>::max(); | 113 int text_height = std::numeric_limits<int>::max(); |
114 std::vector<string16> wrapped = GetWrappedText(text_width, lines); | 114 std::vector<string16> wrapped = GetWrappedText(text_width, lines); |
115 gfx::Canvas::SizeStringInt(JoinString(wrapped, '\n'), font(), | 115 gfx::Canvas::SizeStringInt(JoinString(wrapped, '\n'), font(), |
116 &text_width, &text_height, GetTextFlags()); | 116 &text_width, &text_height, |
| 117 owner_->GetLineHeight(), |
| 118 GetTextFlags()); |
117 size.set_width(text_width + insets.width()); | 119 size.set_width(text_width + insets.width()); |
118 size.set_height(text_height + insets.height()); | 120 size.set_height(text_height + insets.height()); |
119 SetCachedSize(key, size); | 121 SetCachedSize(key, size); |
120 } | 122 } |
121 return size; | 123 return size; |
122 } | 124 } |
123 | 125 |
124 std::vector<string16> InnerBoundedLabel::GetWrappedText(int width, int lines) { | 126 std::vector<string16> InnerBoundedLabel::GetWrappedText(int width, int lines) { |
125 // Short circuit simple case. | 127 // Short circuit simple case. |
126 if (width == 0 || lines == 0) | 128 if (width == 0 || lines == 0) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 | 162 |
161 void InnerBoundedLabel::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 163 void InnerBoundedLabel::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
162 ClearCaches(); | 164 ClearCaches(); |
163 views::Label::OnBoundsChanged(previous_bounds); | 165 views::Label::OnBoundsChanged(previous_bounds); |
164 } | 166 } |
165 | 167 |
166 void InnerBoundedLabel::OnPaint(gfx::Canvas* canvas) { | 168 void InnerBoundedLabel::OnPaint(gfx::Canvas* canvas) { |
167 views::Label::OnPaintBackground(canvas); | 169 views::Label::OnPaintBackground(canvas); |
168 views::Label::OnPaintFocusBorder(canvas); | 170 views::Label::OnPaintFocusBorder(canvas); |
169 views::Label::OnPaintBorder(canvas); | 171 views::Label::OnPaintBorder(canvas); |
170 int lines = owner_->line_limit(); | 172 int lines = owner_->GetLineLimit(); |
171 int height = GetSizeForWidthAndLines(width(), lines).height(); | 173 int height = GetSizeForWidthAndLines(width(), lines).height(); |
172 if (height > 0) { | 174 if (height > 0) { |
173 gfx::Rect bounds(width(), height); | 175 gfx::Rect bounds(width(), height); |
174 bounds.Inset(owner_->GetInsets()); | 176 bounds.Inset(owner_->GetInsets()); |
175 if (bounds.width() != wrapped_text_width_ || lines != wrapped_text_lines_) { | 177 if (bounds.width() != wrapped_text_width_ || lines != wrapped_text_lines_) { |
176 wrapped_text_ = JoinString(GetWrappedText(bounds.width(), lines), '\n'); | 178 wrapped_text_ = JoinString(GetWrappedText(bounds.width(), lines), '\n'); |
177 wrapped_text_width_ = bounds.width(); | 179 wrapped_text_width_ = bounds.width(); |
178 wrapped_text_lines_ = lines; | 180 wrapped_text_lines_ = lines; |
179 } | 181 } |
180 PaintText(canvas, wrapped_text_, bounds, GetTextFlags()); | 182 PaintText(canvas, wrapped_text_, bounds, GetTextFlags()); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 } | 270 } |
269 | 271 |
270 BoundedLabel::~BoundedLabel() { | 272 BoundedLabel::~BoundedLabel() { |
271 } | 273 } |
272 | 274 |
273 void BoundedLabel::SetColors(SkColor textColor, SkColor backgroundColor) { | 275 void BoundedLabel::SetColors(SkColor textColor, SkColor backgroundColor) { |
274 label_->SetEnabledColor(textColor); | 276 label_->SetEnabledColor(textColor); |
275 label_->SetBackgroundColor(backgroundColor); | 277 label_->SetBackgroundColor(backgroundColor); |
276 } | 278 } |
277 | 279 |
| 280 void BoundedLabel::SetLineHeight(int height) { |
| 281 label_->SetLineHeight(height); |
| 282 } |
| 283 |
278 void BoundedLabel::SetLineLimit(int lines) { | 284 void BoundedLabel::SetLineLimit(int lines) { |
279 line_limit_ = std::max(lines, -1); | 285 line_limit_ = std::max(lines, -1); |
280 } | 286 } |
281 | 287 |
| 288 int BoundedLabel::GetLineHeight() const { |
| 289 return label_->line_height(); |
| 290 } |
| 291 |
| 292 int BoundedLabel::GetLineLimit() const { |
| 293 return line_limit_; |
| 294 } |
| 295 |
282 int BoundedLabel::GetLinesForWidthAndLimit(int width, int limit) { | 296 int BoundedLabel::GetLinesForWidthAndLimit(int width, int limit) { |
283 return visible() ? label_->GetLinesForWidthAndLimit(width, limit) : 0; | 297 return visible() ? label_->GetLinesForWidthAndLimit(width, limit) : 0; |
284 } | 298 } |
285 | 299 |
286 gfx::Size BoundedLabel::GetSizeForWidthAndLines(int width, int lines) { | 300 gfx::Size BoundedLabel::GetSizeForWidthAndLines(int width, int lines) { |
287 return visible() ? | 301 return visible() ? |
288 label_->GetSizeForWidthAndLines(width, lines) : gfx::Size(); | 302 label_->GetSizeForWidthAndLines(width, lines) : gfx::Size(); |
289 } | 303 } |
290 | 304 |
291 int BoundedLabel::GetBaseline() const { | 305 int BoundedLabel::GetBaseline() const { |
(...skipping 29 matching lines...) Expand all Loading... |
321 | 335 |
322 void BoundedLabel::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 336 void BoundedLabel::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
323 label_->SetNativeTheme(theme); | 337 label_->SetNativeTheme(theme); |
324 } | 338 } |
325 | 339 |
326 string16 BoundedLabel::GetWrappedTextForTest(int width, int lines) { | 340 string16 BoundedLabel::GetWrappedTextForTest(int width, int lines) { |
327 return JoinString(label_->GetWrappedText(width, lines), '\n'); | 341 return JoinString(label_->GetWrappedText(width, lines), '\n'); |
328 } | 342 } |
329 | 343 |
330 } // namespace message_center | 344 } // namespace message_center |
OLD | NEW |