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( |
193 label_width, | |
194 static_cast<int>(gfx::GetStringWidth(*iter, font_list_))); | |
msw
2013/09/27 21:54:48
Shouldn't this retain the floating point value? Ei
jianli
2013/10/01 00:32:58
I do not think fractional width will be needed for
| |
193 } | 195 } |
194 | 196 |
195 label_width += GetInsets().width(); | 197 label_width += GetInsets().width(); |
196 | 198 |
197 if (max_width > 0) | 199 if (max_width > 0) |
198 label_width = std::min(label_width, max_width); | 200 label_width = std::min(label_width, max_width); |
199 | 201 |
200 SetBounds(x(), y(), label_width, 0); | 202 SetBounds(x(), y(), label_width, 0); |
201 SizeToPreferredSize(); | 203 SizeToPreferredSize(); |
202 } | 204 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
242 return View::GetHeightForWidth(w); | 244 return View::GetHeightForWidth(w); |
243 | 245 |
244 w = std::max(0, w - GetInsets().width()); | 246 w = std::max(0, w - GetInsets().width()); |
245 | 247 |
246 for (size_t i = 0; i < cached_heights_.size(); ++i) { | 248 for (size_t i = 0; i < cached_heights_.size(); ++i) { |
247 const gfx::Size& s = cached_heights_[i]; | 249 const gfx::Size& s = cached_heights_[i]; |
248 if (s.width() == w) | 250 if (s.width() == w) |
249 return s.height() + GetInsets().height(); | 251 return s.height() + GetInsets().height(); |
250 } | 252 } |
251 | 253 |
252 int cache_width = w; | 254 float fw = w; |
msw
2013/09/27 21:54:48
|fw| isn't really used after its value is updated
jianli
2013/10/01 00:32:58
Though |fw| is not used after its value is update
msw
2013/10/01 02:42:48
Ah, now I understand; this code was already cachin
| |
253 | 255 float h = font_list_.GetHeight(); |
254 int h = font_list_.GetHeight(); | |
255 const int flags = ComputeDrawStringFlags(); | 256 const int flags = ComputeDrawStringFlags(); |
256 gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, line_height_, flags); | 257 gfx::Canvas::SizeStringInt(text_, font_list_, &fw, &h, line_height_, flags); |
257 cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h); | 258 cached_heights_[cached_heights_cursor_] = gfx::Size(w, h); |
258 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; | 259 cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; |
259 return h + GetInsets().height(); | 260 return h + GetInsets().height(); |
260 } | 261 } |
261 | 262 |
262 const char* Label::GetClassName() const { | 263 const char* Label::GetClassName() const { |
263 return kViewClassName; | 264 return kViewClassName; |
264 } | 265 } |
265 | 266 |
266 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) { | 267 View* Label::GetTooltipHandlerForPoint(const gfx::Point& point) { |
267 // Bail out if the label does not contain the point. | 268 // Bail out if the label does not contain the point. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
324 canvas->DrawFocusRect(focus_bounds); | 325 canvas->DrawFocusRect(focus_bounds); |
325 } | 326 } |
326 } | 327 } |
327 | 328 |
328 gfx::Size Label::GetTextSize() const { | 329 gfx::Size Label::GetTextSize() const { |
329 if (!text_size_valid_) { | 330 if (!text_size_valid_) { |
330 // For single-line strings, we supply the largest possible width, because | 331 // For single-line strings, we supply the largest possible width, because |
331 // while adding NO_ELLIPSIS to the flags works on Windows for forcing | 332 // 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 | 333 // SizeStringInt() to calculate the desired width, it doesn't seem to work |
333 // on Linux. | 334 // on Linux. |
334 int w = is_multi_line_ ? | 335 float w = is_multi_line_ ? |
335 GetAvailableRect().width() : std::numeric_limits<int>::max(); | 336 GetAvailableRect().width() : std::numeric_limits<int>::max(); |
336 int h = font_list_.GetHeight(); | 337 float h = font_list_.GetHeight(); |
337 // For single-line strings, ignore the available width and calculate how | 338 // For single-line strings, ignore the available width and calculate how |
338 // wide the text wants to be. | 339 // wide the text wants to be. |
339 int flags = ComputeDrawStringFlags(); | 340 int flags = ComputeDrawStringFlags(); |
340 if (!is_multi_line_) | 341 if (!is_multi_line_) |
341 flags |= gfx::Canvas::NO_ELLIPSIS; | 342 flags |= gfx::Canvas::NO_ELLIPSIS; |
342 gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, line_height_, flags); | 343 gfx::Canvas::SizeStringInt(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 |
(...skipping 187 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 |