| 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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 int 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<int>( |
| 193 label_width, gfx::GetStringWidth(*iter, font_list_)); |
| 193 } | 194 } |
| 194 | 195 |
| 195 label_width += GetInsets().width(); | 196 label_width += GetInsets().width(); |
| 196 | 197 |
| 197 if (max_width > 0) | 198 if (max_width > 0) |
| 198 label_width = std::min(label_width, max_width); | 199 label_width = std::min(label_width, max_width); |
| 199 | 200 |
| 200 SetBounds(x(), y(), label_width, 0); | 201 SetBounds(x(), y(), label_width, 0); |
| 201 SizeToPreferredSize(); | 202 SizeToPreferredSize(); |
| 202 } | 203 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 return View::GetHeightForWidth(w); | 243 return View::GetHeightForWidth(w); |
| 243 | 244 |
| 244 w = std::max(0, w - GetInsets().width()); | 245 w = std::max(0, w - GetInsets().width()); |
| 245 | 246 |
| 246 for (size_t i = 0; i < cached_heights_.size(); ++i) { | 247 for (size_t i = 0; i < cached_heights_.size(); ++i) { |
| 247 const gfx::Size& s = cached_heights_[i]; | 248 const gfx::Size& s = cached_heights_[i]; |
| 248 if (s.width() == w) | 249 if (s.width() == w) |
| 249 return s.height() + GetInsets().height(); | 250 return s.height() + GetInsets().height(); |
| 250 } | 251 } |
| 251 | 252 |
| 252 int cache_width = w; | 253 float fw = w; |
| 253 | 254 float h = font_list_.GetHeight(); |
| 254 int h = font_list_.GetHeight(); | |
| 255 const int flags = ComputeDrawStringFlags(); | 255 const int flags = ComputeDrawStringFlags(); |
| 256 gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, line_height_, flags); | 256 gfx::Canvas::SizeStringToFit(text_, font_list_, &fw, &h, line_height_, flags); |
| 257 cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h); | 257 cached_heights_[cached_heights_cursor_] = gfx::Size(w, h); |
| 258 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; | 258 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; |
| 259 return h + GetInsets().height(); | 259 return h + GetInsets().height(); |
| 260 } | 260 } |
| 261 | 261 |
| 262 const char* Label::GetClassName() const { | 262 const char* Label::GetClassName() const { |
| 263 return kViewClassName; | 263 return kViewClassName; |
| 264 } | 264 } |
| 265 | 265 |
| 266 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) { | 266 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) { |
| 267 // Bail out if the label does not contain the point. | 267 // 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; | 322 gfx::Rect focus_bounds = text_bounds; |
| 323 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); | 323 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); |
| 324 canvas->DrawFocusRect(focus_bounds); | 324 canvas->DrawFocusRect(focus_bounds); |
| 325 } | 325 } |
| 326 } | 326 } |
| 327 | 327 |
| 328 gfx::Size Label::GetTextSize() const { | 328 gfx::Size Label::GetTextSize() const { |
| 329 if (!text_size_valid_) { | 329 if (!text_size_valid_) { |
| 330 // For single-line strings, we supply the largest possible width, because | 330 // For single-line strings, we supply the largest possible width, because |
| 331 // while adding NO_ELLIPSIS to the flags works on Windows for forcing | 331 // 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 | 332 // SizeStringToFit() to calculate the desired width, it doesn't seem to work |
| 333 // on Linux. | 333 // on Linux. |
| 334 int w = is_multi_line_ ? | 334 float w = is_multi_line_ ? |
| 335 GetAvailableRect().width() : std::numeric_limits<int>::max(); | 335 GetAvailableRect().width() : std::numeric_limits<int>::max(); |
| 336 int h = font_list_.GetHeight(); | 336 float h = font_list_.GetHeight(); |
| 337 // For single-line strings, ignore the available width and calculate how | 337 // For single-line strings, ignore the available width and calculate how |
| 338 // wide the text wants to be. | 338 // wide the text wants to be. |
| 339 int flags = ComputeDrawStringFlags(); | 339 int flags = ComputeDrawStringFlags(); |
| 340 if (!is_multi_line_) | 340 if (!is_multi_line_) |
| 341 flags |= gfx::Canvas::NO_ELLIPSIS; | 341 flags |= gfx::Canvas::NO_ELLIPSIS; |
| 342 gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, line_height_, flags); | 342 gfx::Canvas::SizeStringToFit( |
| 343 text_, font_list_, &w, &h, line_height_, flags); |
| 343 text_size_.SetSize(w, h); | 344 text_size_.SetSize(w, h); |
| 344 text_size_valid_ = true; | 345 text_size_valid_ = true; |
| 345 } | 346 } |
| 346 | 347 |
| 347 return text_size_; | 348 return text_size_; |
| 348 } | 349 } |
| 349 | 350 |
| 350 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 351 void Label::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 351 text_size_valid_ &= !is_multi_line_; | 352 text_size_valid_ &= !is_multi_line_; |
| 352 } | 353 } |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 for (int i = 0; i < kCachedSizeLimit; ++i) | 535 for (int i = 0; i < kCachedSizeLimit; ++i) |
| 535 cached_heights_[i] = gfx::Size(); | 536 cached_heights_[i] = gfx::Size(); |
| 536 } | 537 } |
| 537 | 538 |
| 538 bool Label::ShouldShowDefaultTooltip() const { | 539 bool Label::ShouldShowDefaultTooltip() const { |
| 539 return !is_multi_line_ && | 540 return !is_multi_line_ && |
| 540 gfx::GetStringWidth(text_, font_list_) > GetAvailableRect().width(); | 541 gfx::GetStringWidth(text_, font_list_) > GetAvailableRect().width(); |
| 541 } | 542 } |
| 542 | 543 |
| 543 } // namespace views | 544 } // namespace views |
| OLD | NEW |