| 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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 return (limit < 0 || lines <= limit) ? lines : limit; | 99 return (limit < 0 || lines <= limit) ? lines : limit; |
| 100 } | 100 } |
| 101 | 101 |
| 102 gfx::Size InnerBoundedLabel::GetSizeForWidthAndLines(int width, int lines) { | 102 gfx::Size InnerBoundedLabel::GetSizeForWidthAndLines(int width, int lines) { |
| 103 if (width == 0 || lines == 0) | 103 if (width == 0 || lines == 0) |
| 104 return gfx::Size(); | 104 return gfx::Size(); |
| 105 std::pair<int, int> key(width, lines); | 105 std::pair<int, int> key(width, lines); |
| 106 gfx::Size size = GetCachedSize(key); | 106 gfx::Size size = GetCachedSize(key); |
| 107 if (size.height() == std::numeric_limits<int>::max()) { | 107 if (size.height() == std::numeric_limits<int>::max()) { |
| 108 gfx::Insets insets = owner_->GetInsets(); | 108 gfx::Insets insets = owner_->GetInsets(); |
| 109 int text_width = (width < 0) ? std::numeric_limits<int>::max() : | 109 float text_width = (width < 0) ? std::numeric_limits<int>::max() : |
| 110 std::max(width - insets.width(), 0); | 110 std::max(width - insets.width(), 0); |
| 111 int text_height = std::numeric_limits<int>::max(); | 111 float text_height = std::numeric_limits<int>::max(); |
| 112 std::vector<string16> wrapped = GetWrappedText(text_width, lines); | 112 std::vector<string16> wrapped = GetWrappedText(text_width, lines); |
| 113 gfx::Canvas::SizeStringInt(JoinString(wrapped, '\n'), font(), | 113 gfx::Canvas::SizeStringToFit(JoinString(wrapped, '\n'), font(), |
| 114 &text_width, &text_height, | 114 &text_width, &text_height, |
| 115 owner_->GetLineHeight(), | 115 owner_->GetLineHeight(), |
| 116 GetTextFlags()); | 116 GetTextFlags()); |
| 117 size.set_width(text_width + insets.width()); | 117 size.set_width(text_width + insets.width()); |
| 118 size.set_height(text_height + insets.height()); | 118 size.set_height(text_height + insets.height()); |
| 119 SetCachedSize(key, size); | 119 SetCachedSize(key, size); |
| 120 } | 120 } |
| 121 return size; | 121 return size; |
| 122 } | 122 } |
| 123 | 123 |
| 124 std::vector<string16> InnerBoundedLabel::GetWrappedText(int width, int lines) { | 124 std::vector<string16> InnerBoundedLabel::GetWrappedText(int width, int lines) { |
| 125 // Short circuit simple case. | 125 // Short circuit simple case. |
| 126 if (width == 0 || lines == 0) | 126 if (width == 0 || lines == 0) |
| 127 return std::vector<string16>(); | 127 return std::vector<string16>(); |
| 128 | 128 |
| 129 // Restrict line limit to ensure (lines + 1) * line_height <= INT_MAX and | 129 // Restrict line limit to ensure (lines + 1) * line_height <= INT_MAX and |
| 130 // use it to calculate a reasonable text height. | 130 // use it to calculate a reasonable text height. |
| 131 int height = std::numeric_limits<int>::max(); | 131 int height = std::numeric_limits<int>::max(); |
| 132 if (lines > 0) { | 132 if (lines > 0) { |
| 133 int line_height = std::max(font().GetHeight(), 2); // At least 2 pixels. | 133 int line_height = std::max(font().GetHeight(), 2); // At least 2 pixels. |
| 134 int max_lines = std::numeric_limits<int>::max() / line_height - 1; | 134 int max_lines = std::numeric_limits<int>::max() / line_height - 1; |
| 135 lines = std::min(lines, max_lines); | 135 lines = std::min(lines, max_lines); |
| 136 height = (lines + 1) * line_height; | 136 height = (lines + 1) * line_height; |
| 137 } | 137 } |
| 138 | 138 |
| 139 // If |width| is -1, it means no wrapping. |
| 140 float wrap_width = (width < 0) ? FLT_MAX : width; |
| 141 |
| 139 // Try to ensure that the width is no smaller than the width of the text's | 142 // 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. | 143 // characters to avoid the http://crbug.com/237700 infinite loop. |
| 141 // TODO(dharcourt): Remove when http://crbug.com/237700 is fixed. | 144 // TODO(dharcourt): Remove when http://crbug.com/237700 is fixed. |
| 142 width = std::max(width, 2 * font().GetStringWidth(UTF8ToUTF16("W"))); | 145 wrap_width = |
| 146 std::max(wrap_width, 2 * font().GetStringWidth(UTF8ToUTF16("W"))); |
| 143 | 147 |
| 144 // Wrap, using INT_MAX for -1 widths that indicate no wrapping. | 148 // Wrap the text. |
| 145 std::vector<string16> wrapped; | 149 std::vector<string16> wrapped; |
| 146 gfx::ElideRectangleText(text(), font_list(), | 150 gfx::ElideRectangleText(text(), font_list(), wrap_width, |
| 147 (width < 0) ? std::numeric_limits<int>::max() : width, | |
| 148 height, gfx::WRAP_LONG_WORDS, &wrapped); | 151 height, gfx::WRAP_LONG_WORDS, &wrapped); |
| 149 | 152 |
| 150 // Elide if necessary. | 153 // Elide if necessary. |
| 151 if (lines > 0 && wrapped.size() > static_cast<unsigned int>(lines)) { | 154 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 | 155 // 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, | 156 // too wide, that line will be further elided by the gfx::ElideText below, |
| 154 // so for example "ABC" could become "ABC..." and then "AB...". | 157 // so for example "ABC" could become "ABC..." and then "AB...". |
| 155 string16 last = wrapped[lines - 1] + UTF8ToUTF16(gfx::kEllipsis); | 158 string16 last = wrapped[lines - 1] + UTF8ToUTF16(gfx::kEllipsis); |
| 156 if (width > 0 && font().GetStringWidth(last) > width) | 159 if (width > 0 && font().GetStringWidth(last) > width) |
| 157 last = gfx::ElideText(last, font(), width, gfx::ELIDE_AT_END); | 160 last = gfx::ElideText(last, font(), width, gfx::ELIDE_AT_END); |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 | 341 |
| 339 void BoundedLabel::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 342 void BoundedLabel::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 340 label_->SetNativeTheme(theme); | 343 label_->SetNativeTheme(theme); |
| 341 } | 344 } |
| 342 | 345 |
| 343 string16 BoundedLabel::GetWrappedTextForTest(int width, int lines) { | 346 string16 BoundedLabel::GetWrappedTextForTest(int width, int lines) { |
| 344 return JoinString(label_->GetWrappedText(width, lines), '\n'); | 347 return JoinString(label_->GetWrappedText(width, lines), '\n'); |
| 345 } | 348 } |
| 346 | 349 |
| 347 } // namespace message_center | 350 } // namespace message_center |
| OLD | NEW |