| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/views/controls/label.h" | 5 #include "ui/views/controls/label.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 void Label::SetTooltipText(const string16& tooltip_text) { | 179 void Label::SetTooltipText(const string16& tooltip_text) { |
| 180 tooltip_text_ = tooltip_text; | 180 tooltip_text_ = tooltip_text; |
| 181 } | 181 } |
| 182 | 182 |
| 183 void Label::SizeToFit(int max_width) { | 183 void Label::SizeToFit(int max_width) { |
| 184 DCHECK(is_multi_line_); | 184 DCHECK(is_multi_line_); |
| 185 | 185 |
| 186 std::vector<string16> lines; | 186 std::vector<string16> lines; |
| 187 base::SplitString(text_, '\n', &lines); | 187 base::SplitString(text_, '\n', &lines); |
| 188 | 188 |
| 189 int label_width = 0; | 189 float label_width = 0; |
| 190 for (std::vector<string16>::const_iterator iter = lines.begin(); | 190 for (std::vector<string16>::const_iterator iter = lines.begin(); |
| 191 iter != lines.end(); ++iter) { | 191 iter != lines.end(); ++iter) { |
| 192 label_width = std::max(label_width, gfx::GetStringWidth(*iter, font_list_)); | 192 label_width = std::max(label_width, gfx::GetStringWidth(*iter, font_list_)); |
| 193 } | 193 } |
| 194 | 194 |
| 195 label_width += GetInsets().width(); | 195 label_width += GetInsets().width(); |
| 196 | 196 |
| 197 if (max_width > 0) | 197 if (max_width > 0) |
| 198 label_width = std::min(label_width, max_width); | 198 label_width = std::min<float>(label_width, max_width); |
| 199 | 199 |
| 200 SetBounds(x(), y(), label_width, 0); | 200 SetBounds(x(), y(), std::ceil(label_width), 0); |
| 201 SizeToPreferredSize(); | 201 SizeToPreferredSize(); |
| 202 } | 202 } |
| 203 | 203 |
| 204 void Label::SetHasFocusBorder(bool has_focus_border) { | 204 void Label::SetHasFocusBorder(bool has_focus_border) { |
| 205 has_focus_border_ = has_focus_border; | 205 has_focus_border_ = has_focus_border; |
| 206 if (is_multi_line_) { | 206 if (is_multi_line_) { |
| 207 ResetCachedSize(); | 207 ResetCachedSize(); |
| 208 PreferredSizeChanged(); | 208 PreferredSizeChanged(); |
| 209 } | 209 } |
| 210 } | 210 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 return View::GetHeightForWidth(w); | 242 return View::GetHeightForWidth(w); |
| 243 | 243 |
| 244 w = std::max(0, w - GetInsets().width()); | 244 w = std::max(0, w - GetInsets().width()); |
| 245 | 245 |
| 246 for (size_t i = 0; i < cached_heights_.size(); ++i) { | 246 for (size_t i = 0; i < cached_heights_.size(); ++i) { |
| 247 const gfx::Size& s = cached_heights_[i]; | 247 const gfx::Size& s = cached_heights_[i]; |
| 248 if (s.width() == w) | 248 if (s.width() == w) |
| 249 return s.height() + GetInsets().height(); | 249 return s.height() + GetInsets().height(); |
| 250 } | 250 } |
| 251 | 251 |
| 252 int cache_width = w; | 252 float fw = w; |
| 253 | 253 float h = font_list_.GetHeight(); |
| 254 int h = font_list_.GetHeight(); | |
| 255 const int flags = ComputeDrawStringFlags(); | 254 const int flags = ComputeDrawStringFlags(); |
| 256 gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, line_height_, flags); | 255 gfx::Canvas::SizeStringToFit(text_, font_list_, &fw, &h, line_height_, flags); |
| 257 cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h); | 256 cached_heights_[cached_heights_cursor_] = gfx::Size(w, h); |
| 258 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; | 257 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; |
| 259 return h + GetInsets().height(); | 258 return h + GetInsets().height(); |
| 260 } | 259 } |
| 261 | 260 |
| 262 const char* Label::GetClassName() const { | 261 const char* Label::GetClassName() const { |
| 263 return kViewClassName; | 262 return kViewClassName; |
| 264 } | 263 } |
| 265 | 264 |
| 266 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) { | 265 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) { |
| 267 // Bail out if the label does not contain the point. | 266 // Bail out if the label does not contain the point. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 gfx::Rect focus_bounds = text_bounds; | 321 gfx::Rect focus_bounds = text_bounds; |
| 323 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); | 322 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); |
| 324 canvas->DrawFocusRect(focus_bounds); | 323 canvas->DrawFocusRect(focus_bounds); |
| 325 } | 324 } |
| 326 } | 325 } |
| 327 | 326 |
| 328 gfx::Size Label::GetTextSize() const { | 327 gfx::Size Label::GetTextSize() const { |
| 329 if (!text_size_valid_) { | 328 if (!text_size_valid_) { |
| 330 // For single-line strings, we supply the largest possible width, because | 329 // For single-line strings, we supply the largest possible width, because |
| 331 // while adding NO_ELLIPSIS to the flags works on Windows for forcing | 330 // while adding NO_ELLIPSIS to the flags works on Windows for forcing |
| 332 // SizeStringInt() to calculate the desired width, it doesn't seem to work | 331 // SizeStringToFit() to calculate the desired width, it doesn't seem to work |
| 333 // on Linux. | 332 // on Linux. |
| 334 int w = is_multi_line_ ? | 333 float w = is_multi_line_ ? |
| 335 GetAvailableRect().width() : std::numeric_limits<int>::max(); | 334 GetAvailableRect().width() : std::numeric_limits<int>::max(); |
| 336 int h = font_list_.GetHeight(); | 335 float h = font_list_.GetHeight(); |
| 337 // For single-line strings, ignore the available width and calculate how | 336 // For single-line strings, ignore the available width and calculate how |
| 338 // wide the text wants to be. | 337 // wide the text wants to be. |
| 339 int flags = ComputeDrawStringFlags(); | 338 int flags = ComputeDrawStringFlags(); |
| 340 if (!is_multi_line_) | 339 if (!is_multi_line_) |
| 341 flags |= gfx::Canvas::NO_ELLIPSIS; | 340 flags |= gfx::Canvas::NO_ELLIPSIS; |
| 342 gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, line_height_, flags); | 341 gfx::Canvas::SizeStringToFit( |
| 342 text_, font_list_, &w, &h, line_height_, flags); |
| 343 text_size_.SetSize(w, h); | 343 text_size_.SetSize(w, h); |
| 344 text_size_valid_ = true; | 344 text_size_valid_ = true; |
| 345 } | 345 } |
| 346 | 346 |
| 347 return text_size_; | 347 return text_size_; |
| 348 } | 348 } |
| 349 | 349 |
| 350 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 350 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 351 text_size_valid_ &= !is_multi_line_; | 351 text_size_valid_ &= !is_multi_line_; |
| 352 } | 352 } |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 for (int i = 0; i < kCachedSizeLimit; ++i) | 534 for (int i = 0; i < kCachedSizeLimit; ++i) |
| 535 cached_heights_[i] = gfx::Size(); | 535 cached_heights_[i] = gfx::Size(); |
| 536 } | 536 } |
| 537 | 537 |
| 538 bool Label::ShouldShowDefaultTooltip() const { | 538 bool Label::ShouldShowDefaultTooltip() const { |
| 539 return !is_multi_line_ && | 539 return !is_multi_line_ && |
| 540 gfx::GetStringWidth(text_, font_list_) > GetAvailableRect().width(); | 540 gfx::GetStringWidth(text_, font_list_) > GetAvailableRect().width(); |
| 541 } | 541 } |
| 542 | 542 |
| 543 } // namespace views | 543 } // namespace views |
| OLD | NEW |