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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 if (width == 0 || lines == 0) | 106 if (width == 0 || lines == 0) |
107 return gfx::Size(); | 107 return gfx::Size(); |
108 std::pair<int, int> key(width, lines); | 108 std::pair<int, int> key(width, lines); |
109 gfx::Size size = GetCachedSize(key); | 109 gfx::Size size = GetCachedSize(key); |
110 if (size.height() == std::numeric_limits<int>::max()) { | 110 if (size.height() == std::numeric_limits<int>::max()) { |
111 gfx::Insets insets = owner_->GetInsets(); | 111 gfx::Insets insets = owner_->GetInsets(); |
112 int text_width = (width < 0) ? std::numeric_limits<int>::max() : | 112 int text_width = (width < 0) ? std::numeric_limits<int>::max() : |
113 std::max(width - insets.width(), 0); | 113 std::max(width - insets.width(), 0); |
114 int text_height = std::numeric_limits<int>::max(); | 114 int text_height = std::numeric_limits<int>::max(); |
115 std::vector<base::string16> wrapped = GetWrappedText(text_width, lines); | 115 std::vector<base::string16> wrapped = GetWrappedText(text_width, lines); |
116 gfx::Canvas::SizeStringInt( | 116 gfx::Canvas::SizeStringInt(JoinString(wrapped, '\n'), font_list(), |
117 base::JoinString(wrapped, base::ASCIIToUTF16("\n")), | 117 &text_width, &text_height, |
118 font_list(), &text_width, &text_height, owner_->GetLineHeight(), | 118 owner_->GetLineHeight(), |
119 GetTextFlags()); | 119 GetTextFlags()); |
120 size.set_width(text_width + insets.width()); | 120 size.set_width(text_width + insets.width()); |
121 size.set_height(text_height + insets.height()); | 121 size.set_height(text_height + insets.height()); |
122 SetCachedSize(key, size); | 122 SetCachedSize(key, size); |
123 } | 123 } |
124 return size; | 124 return size; |
125 } | 125 } |
126 | 126 |
127 std::vector<base::string16> InnerBoundedLabel::GetWrappedText(int width, | 127 std::vector<base::string16> InnerBoundedLabel::GetWrappedText(int width, |
128 int lines) { | 128 int lines) { |
129 // Short circuit simple case. | 129 // Short circuit simple case. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 170 |
171 void InnerBoundedLabel::OnPaint(gfx::Canvas* canvas) { | 171 void InnerBoundedLabel::OnPaint(gfx::Canvas* canvas) { |
172 views::Label::OnPaintBackground(canvas); | 172 views::Label::OnPaintBackground(canvas); |
173 views::Label::OnPaintBorder(canvas); | 173 views::Label::OnPaintBorder(canvas); |
174 int lines = owner_->GetLineLimit(); | 174 int lines = owner_->GetLineLimit(); |
175 int height = GetSizeForWidthAndLines(width(), lines).height(); | 175 int height = GetSizeForWidthAndLines(width(), lines).height(); |
176 if (height > 0) { | 176 if (height > 0) { |
177 gfx::Rect bounds(width(), height); | 177 gfx::Rect bounds(width(), height); |
178 bounds.Inset(owner_->GetInsets()); | 178 bounds.Inset(owner_->GetInsets()); |
179 if (bounds.width() != wrapped_text_width_ || lines != wrapped_text_lines_) { | 179 if (bounds.width() != wrapped_text_width_ || lines != wrapped_text_lines_) { |
180 wrapped_text_ = base::JoinString(GetWrappedText(bounds.width(), lines), | 180 wrapped_text_ = JoinString(GetWrappedText(bounds.width(), lines), '\n'); |
181 base::ASCIIToUTF16("\n")); | |
182 wrapped_text_width_ = bounds.width(); | 181 wrapped_text_width_ = bounds.width(); |
183 wrapped_text_lines_ = lines; | 182 wrapped_text_lines_ = lines; |
184 } | 183 } |
185 bounds.set_x(GetMirroredXForRect(bounds)); | 184 bounds.set_x(GetMirroredXForRect(bounds)); |
186 canvas->DrawStringRectWithFlags( | 185 canvas->DrawStringRectWithFlags( |
187 wrapped_text_, font_list(), enabled_color(), bounds, GetTextFlags()); | 186 wrapped_text_, font_list(), enabled_color(), bounds, GetTextFlags()); |
188 } | 187 } |
189 } | 188 } |
190 | 189 |
191 void InnerBoundedLabel::SetText(const base::string16& new_text) { | 190 void InnerBoundedLabel::SetText(const base::string16& new_text) { |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 void BoundedLabel::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 336 void BoundedLabel::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
338 label_->SetBoundsRect(bounds()); | 337 label_->SetBoundsRect(bounds()); |
339 views::View::OnBoundsChanged(previous_bounds); | 338 views::View::OnBoundsChanged(previous_bounds); |
340 } | 339 } |
341 | 340 |
342 void BoundedLabel::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 341 void BoundedLabel::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
343 label_->SetNativeTheme(theme); | 342 label_->SetNativeTheme(theme); |
344 } | 343 } |
345 | 344 |
346 base::string16 BoundedLabel::GetWrappedTextForTest(int width, int lines) { | 345 base::string16 BoundedLabel::GetWrappedTextForTest(int width, int lines) { |
347 return base::JoinString(label_->GetWrappedText(width, lines), | 346 return JoinString(label_->GetWrappedText(width, lines), '\n'); |
348 base::ASCIIToUTF16("\n")); | |
349 } | 347 } |
350 | 348 |
351 } // namespace message_center | 349 } // namespace message_center |
OLD | NEW |