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/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 height = (lines + 1) * line_height; | 136 height = (lines + 1) * line_height; |
137 } | 137 } |
138 | 138 |
139 // Try to ensure that the width is no smaller than the width of the text's | 139 // Try to ensure that the width is no smaller than the width of the text's |
140 // characters to avoid the http://crbug.com/237700 infinite loop. | 140 // characters to avoid the http://crbug.com/237700 infinite loop. |
141 // TODO(dharcourt): Remove when http://crbug.com/237700 is fixed. | 141 // TODO(dharcourt): Remove when http://crbug.com/237700 is fixed. |
142 width = std::max(width, 2 * font().GetStringWidth(UTF8ToUTF16("W"))); | 142 width = std::max(width, 2 * font().GetStringWidth(UTF8ToUTF16("W"))); |
143 | 143 |
144 // Wrap, using INT_MAX for -1 widths that indicate no wrapping. | 144 // Wrap, using INT_MAX for -1 widths that indicate no wrapping. |
145 std::vector<string16> wrapped; | 145 std::vector<string16> wrapped; |
146 gfx::ElideRectangleText(text(), font(), | 146 gfx::ElideRectangleText(text(), font_list(), |
147 (width < 0) ? std::numeric_limits<int>::max() : width, | 147 (width < 0) ? std::numeric_limits<int>::max() : width, |
148 height, gfx::WRAP_LONG_WORDS, &wrapped); | 148 height, gfx::WRAP_LONG_WORDS, &wrapped); |
149 | 149 |
150 // Elide if necessary. | 150 // Elide if necessary. |
151 if (lines > 0 && wrapped.size() > static_cast<unsigned int>(lines)) { | 151 if (lines > 0 && wrapped.size() > static_cast<unsigned int>(lines)) { |
152 // Add an ellipsis to the last line. If this ellipsis makes the last line | 152 // Add an ellipsis to the last line. If this ellipsis makes the last line |
153 // too wide, that line will be further elided by the gfx::ElideText below, | 153 // too wide, that line will be further elided by the gfx::ElideText below, |
154 // so for example "ABC" could become "ABC..." and then "AB...". | 154 // so for example "ABC" could become "ABC..." and then "AB...". |
155 string16 last = wrapped[lines - 1] + UTF8ToUTF16(gfx::kEllipsis); | 155 string16 last = wrapped[lines - 1] + UTF8ToUTF16(gfx::kEllipsis); |
156 if (width > 0 && font().GetStringWidth(last) > width) | 156 if (width > 0 && font().GetStringWidth(last) > width) |
157 last = gfx::ElideText(last, font(), width, gfx::ELIDE_AT_END); | 157 last = gfx::ElideText(last, font(), width, gfx::ELIDE_AT_END); |
158 wrapped.resize(lines - 1); | 158 wrapped.resize(lines - 1); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 if (size_cache_.size() >= kPreferredLinesCacheSize) { | 252 if (size_cache_.size() >= kPreferredLinesCacheSize) { |
253 size_cache_.erase(size_widths_and_lines_.back()); | 253 size_cache_.erase(size_widths_and_lines_.back()); |
254 size_widths_and_lines_.pop_back(); | 254 size_widths_and_lines_.pop_back(); |
255 } | 255 } |
256 size_cache_[width_and_lines] = size; | 256 size_cache_[width_and_lines] = size; |
257 size_widths_and_lines_.push_front(width_and_lines); | 257 size_widths_and_lines_.push_front(width_and_lines); |
258 } | 258 } |
259 | 259 |
260 // BoundedLabel /////////////////////////////////////////////////////////// | 260 // BoundedLabel /////////////////////////////////////////////////////////// |
261 | 261 |
262 BoundedLabel::BoundedLabel(const string16& text, gfx::Font font) | 262 BoundedLabel::BoundedLabel(const string16& text, const gfx::FontList& font_list) |
263 : line_limit_(-1) { | 263 : line_limit_(-1) { |
264 label_.reset(new InnerBoundedLabel(*this)); | 264 label_.reset(new InnerBoundedLabel(*this)); |
265 label_->SetFont(font); | 265 label_->SetFontList(font_list); |
266 label_->SetText(text); | 266 label_->SetText(text); |
267 } | 267 } |
268 | 268 |
269 BoundedLabel::BoundedLabel(const string16& text) | 269 BoundedLabel::BoundedLabel(const string16& text) |
270 : line_limit_(-1) { | 270 : line_limit_(-1) { |
271 label_.reset(new InnerBoundedLabel(*this)); | 271 label_.reset(new InnerBoundedLabel(*this)); |
272 label_->SetText(text); | 272 label_->SetText(text); |
273 } | 273 } |
274 | 274 |
275 BoundedLabel::~BoundedLabel() { | 275 BoundedLabel::~BoundedLabel() { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 | 338 |
339 void BoundedLabel::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 339 void BoundedLabel::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
340 label_->SetNativeTheme(theme); | 340 label_->SetNativeTheme(theme); |
341 } | 341 } |
342 | 342 |
343 string16 BoundedLabel::GetWrappedTextForTest(int width, int lines) { | 343 string16 BoundedLabel::GetWrappedTextForTest(int width, int lines) { |
344 return JoinString(label_->GetWrappedText(width, lines), '\n'); | 344 return JoinString(label_->GetWrappedText(width, lines), '\n'); |
345 } | 345 } |
346 | 346 |
347 } // namespace message_center | 347 } // namespace message_center |
OLD | NEW |