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 // 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<int>(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_list(), | 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 |
(...skipping 185 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 |