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 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 | 347 |
348 is_first_paint_text_ = false; | 348 is_first_paint_text_ = false; |
349 PaintText(canvas); | 349 PaintText(canvas); |
350 } else { | 350 } else { |
351 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. | 351 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. |
352 tracked_objects::ScopedTracker tracking_profile( | 352 tracked_objects::ScopedTracker tracking_profile( |
353 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::PaintText not first")); | 353 FROM_HERE_WITH_EXPLICIT_FUNCTION("431326 Label::PaintText not first")); |
354 | 354 |
355 PaintText(canvas); | 355 PaintText(canvas); |
356 } | 356 } |
357 if (HasFocus()) { | 357 if (HasFocus()) |
358 gfx::Rect focus_bounds = GetLocalBounds(); | 358 canvas->DrawFocusRect(GetFocusBounds()); |
359 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); | |
360 canvas->DrawFocusRect(focus_bounds); | |
361 } | |
362 } | 359 } |
363 | 360 |
364 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 361 void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
365 UpdateColorsFromTheme(theme); | 362 UpdateColorsFromTheme(theme); |
366 } | 363 } |
367 | 364 |
368 void Label::OnDeviceScaleFactorChanged(float device_scale_factor) { | 365 void Label::OnDeviceScaleFactorChanged(float device_scale_factor) { |
369 View::OnDeviceScaleFactorChanged(device_scale_factor); | 366 View::OnDeviceScaleFactorChanged(device_scale_factor); |
370 // When the device scale factor is changed, some font rendering parameters is | 367 // When the device scale factor is changed, some font rendering parameters is |
371 // changed (especially, hinting). The bounding box of the text has to be | 368 // changed (especially, hinting). The bounding box of the text has to be |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 render_text->SetCursorEnabled(false); | 423 render_text->SetCursorEnabled(false); |
427 render_text->SetText(text); | 424 render_text->SetText(text); |
428 return render_text.Pass(); | 425 return render_text.Pass(); |
429 } | 426 } |
430 | 427 |
431 void Label::MaybeBuildRenderTextLines() { | 428 void Label::MaybeBuildRenderTextLines() { |
432 if (!lines_.empty()) | 429 if (!lines_.empty()) |
433 return; | 430 return; |
434 | 431 |
435 gfx::Rect rect = GetContentsBounds(); | 432 gfx::Rect rect = GetContentsBounds(); |
| 433 if (focusable()) |
| 434 rect.Inset(kFocusBorderPadding, kFocusBorderPadding); |
436 if (rect.IsEmpty()) | 435 if (rect.IsEmpty()) |
437 return; | 436 return; |
438 | 437 |
439 gfx::HorizontalAlignment alignment = horizontal_alignment(); | 438 gfx::HorizontalAlignment alignment = horizontal_alignment(); |
440 gfx::DirectionalityMode directionality = render_text_->directionality_mode(); | 439 gfx::DirectionalityMode directionality = render_text_->directionality_mode(); |
441 if (multi_line()) { | 440 if (multi_line()) { |
442 // Force the directionality and alignment of the first line on other lines. | 441 // Force the directionality and alignment of the first line on other lines. |
443 bool rtl = | 442 bool rtl = |
444 render_text_->GetDisplayTextDirection() == base::i18n::RIGHT_TO_LEFT; | 443 render_text_->GetDisplayTextDirection() == base::i18n::RIGHT_TO_LEFT; |
445 if (alignment == gfx::ALIGN_TO_HEAD) | 444 if (alignment == gfx::ALIGN_TO_HEAD) |
(...skipping 26 matching lines...) Expand all Loading... |
472 lines_.push_back(line.release()); | 471 lines_.push_back(line.release()); |
473 rect.set_y(rect.y() + rect.height()); | 472 rect.set_y(rect.y() + rect.height()); |
474 } | 473 } |
475 // Append the remaining text to the last visible line. | 474 // Append the remaining text to the last visible line. |
476 for (size_t i = lines_.size(); i < lines.size(); ++i) | 475 for (size_t i = lines_.size(); i < lines.size(); ++i) |
477 lines_.back()->SetText(lines_.back()->text() + lines[i]); | 476 lines_.back()->SetText(lines_.back()->text() + lines[i]); |
478 } | 477 } |
479 RecalculateColors(); | 478 RecalculateColors(); |
480 } | 479 } |
481 | 480 |
| 481 gfx::Rect Label::GetFocusBounds() { |
| 482 MaybeBuildRenderTextLines(); |
| 483 |
| 484 gfx::Rect focus_bounds; |
| 485 if (lines_.empty()) { |
| 486 focus_bounds = gfx::Rect(GetTextSize()); |
| 487 } else { |
| 488 for (size_t i = 0; i < lines_.size(); ++i) { |
| 489 gfx::Point origin; |
| 490 origin += lines_[i]->GetLineOffset(0); |
| 491 focus_bounds.Union(gfx::Rect(origin, lines_[i]->GetStringSize())); |
| 492 } |
| 493 } |
| 494 |
| 495 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); |
| 496 focus_bounds.Intersect(GetLocalBounds()); |
| 497 return focus_bounds; |
| 498 } |
| 499 |
482 std::vector<base::string16> Label::GetLinesForWidth(int width) const { | 500 std::vector<base::string16> Label::GetLinesForWidth(int width) const { |
483 std::vector<base::string16> lines; | 501 std::vector<base::string16> lines; |
484 const gfx::WordWrapBehavior wrap = | 502 const gfx::WordWrapBehavior wrap = |
485 allow_character_break_ ? gfx::WRAP_LONG_WORDS : gfx::TRUNCATE_LONG_WORDS; | 503 allow_character_break_ ? gfx::WRAP_LONG_WORDS : gfx::TRUNCATE_LONG_WORDS; |
486 gfx::ElideRectangleText(render_text_->GetDisplayText(), font_list(), width, | 504 gfx::ElideRectangleText(render_text_->GetDisplayText(), font_list(), width, |
487 std::numeric_limits<int>::max(), wrap, &lines); | 505 std::numeric_limits<int>::max(), wrap, &lines); |
488 return lines; | 506 return lines; |
489 } | 507 } |
490 | 508 |
491 gfx::Size Label::GetTextSize() const { | 509 gfx::Size Label::GetTextSize() const { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 } | 572 } |
555 | 573 |
556 bool Label::ShouldShowDefaultTooltip() const { | 574 bool Label::ShouldShowDefaultTooltip() const { |
557 const gfx::Size text_size = GetTextSize(); | 575 const gfx::Size text_size = GetTextSize(); |
558 const gfx::Size size = GetContentsBounds().size(); | 576 const gfx::Size size = GetContentsBounds().size(); |
559 return !obscured() && (text_size.width() > size.width() || | 577 return !obscured() && (text_size.width() > size.width() || |
560 (multi_line() && text_size.height() > size.height())); | 578 (multi_line() && text_size.height() > size.height())); |
561 } | 579 } |
562 | 580 |
563 } // namespace views | 581 } // namespace views |
OLD | NEW |