Chromium Code Reviews| 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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 | 143 |
| 144 void Label::SetObscured(bool obscured) { | 144 void Label::SetObscured(bool obscured) { |
| 145 if (this->obscured() == obscured) | 145 if (this->obscured() == obscured) |
| 146 return; | 146 return; |
| 147 is_first_paint_text_ = true; | 147 is_first_paint_text_ = true; |
| 148 render_text_->SetObscured(obscured); | 148 render_text_->SetObscured(obscured); |
| 149 ResetLayout(); | 149 ResetLayout(); |
| 150 } | 150 } |
| 151 | 151 |
| 152 void Label::SetAllowCharacterBreak(bool allow_character_break) { | 152 void Label::SetAllowCharacterBreak(bool allow_character_break) { |
| 153 if (allow_character_break_ == allow_character_break) | 153 if (render_text_->allow_character_break() == allow_character_break) |
| 154 return; | 154 return; |
| 155 is_first_paint_text_ = true; | 155 is_first_paint_text_ = true; |
| 156 allow_character_break_ = allow_character_break; | 156 render_text_->SetAllowCharacterBreak(allow_character_break); |
| 157 ResetLayout(); | 157 ResetLayout(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void Label::SetElideBehavior(gfx::ElideBehavior elide_behavior) { | 160 void Label::SetElideBehavior(gfx::ElideBehavior elide_behavior) { |
| 161 DCHECK(!multi_line() || (elide_behavior_ == gfx::ELIDE_TAIL || | 161 DCHECK(!multi_line() || (elide_behavior_ == gfx::ELIDE_TAIL || |
| 162 elide_behavior_ == gfx::NO_ELIDE)); | 162 elide_behavior_ == gfx::NO_ELIDE)); |
| 163 if (elide_behavior_ == elide_behavior) | 163 if (elide_behavior_ == elide_behavior) |
| 164 return; | 164 return; |
| 165 is_first_paint_text_ = true; | 165 is_first_paint_text_ = true; |
| 166 elide_behavior_ = elide_behavior; | 166 elide_behavior_ = elide_behavior; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 | 251 |
| 252 int Label::GetHeightForWidth(int w) const { | 252 int Label::GetHeightForWidth(int w) const { |
| 253 if (!visible() && collapse_when_hidden_) | 253 if (!visible() && collapse_when_hidden_) |
| 254 return 0; | 254 return 0; |
| 255 | 255 |
| 256 w -= GetInsets().width(); | 256 w -= GetInsets().width(); |
| 257 if (!multi_line() || text().empty() || w <= 0) | 257 if (!multi_line() || text().empty() || w <= 0) |
| 258 return std::max(line_height(), font_list().GetHeight()); | 258 return std::max(line_height(), font_list().GetHeight()); |
| 259 | 259 |
| 260 int height = 0; | 260 int height = 0; |
| 261 // RenderText doesn't support character breaks. | 261 // RenderText doesn't support character breaks. |
|
msw
2015/03/25 20:54:15
nit: remove this comment and TODO.
Jun Mukai
2015/03/26 01:35:08
Done.
| |
| 262 // TODO(mukai): remove this restriction. | 262 // TODO(mukai): remove this restriction. |
| 263 if (render_text_->MultilineSupported() && !allow_character_break_) { | 263 if (render_text_->MultilineSupported()) { |
| 264 // SetDisplayRect() has a side effect for later calls of GetStringSize(). | 264 // SetDisplayRect() has a side effect for later calls of GetStringSize(). |
| 265 // Be careful to invoke |render_text_->SetDisplayRect(gfx::Rect())| to | 265 // Be careful to invoke |render_text_->SetDisplayRect(gfx::Rect())| to |
| 266 // cancel this effect before the next time GetStringSize() is called. | 266 // cancel this effect before the next time GetStringSize() is called. |
| 267 // It would be beneficial not to cancel here, considering that some layout | 267 // It would be beneficial not to cancel here, considering that some layout |
| 268 // managers invoke GetHeightForWidth() for the same width multiple times | 268 // managers invoke GetHeightForWidth() for the same width multiple times |
| 269 // and |render_text_| can cache the height. | 269 // and |render_text_| can cache the height. |
| 270 render_text_->SetDisplayRect(gfx::Rect(0, 0, w, 0)); | 270 render_text_->SetDisplayRect(gfx::Rect(0, 0, w, 0)); |
| 271 height = render_text_->GetStringSize().height(); | 271 height = render_text_->GetStringSize().height(); |
| 272 } else { | 272 } else { |
| 273 std::vector<base::string16> lines = GetLinesForWidth(w); | 273 std::vector<base::string16> lines = GetLinesForWidth(w); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 386 render_text_->SetCursorEnabled(false); | 386 render_text_->SetCursorEnabled(false); |
| 387 | 387 |
| 388 elide_behavior_ = gfx::ELIDE_TAIL; | 388 elide_behavior_ = gfx::ELIDE_TAIL; |
| 389 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; | 389 enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; |
| 390 subpixel_rendering_enabled_ = true; | 390 subpixel_rendering_enabled_ = true; |
| 391 auto_color_readability_ = true; | 391 auto_color_readability_ = true; |
| 392 multi_line_ = false; | 392 multi_line_ = false; |
| 393 UpdateColorsFromTheme(ui::NativeTheme::instance()); | 393 UpdateColorsFromTheme(ui::NativeTheme::instance()); |
| 394 handles_tooltips_ = true; | 394 handles_tooltips_ = true; |
| 395 collapse_when_hidden_ = false; | 395 collapse_when_hidden_ = false; |
| 396 allow_character_break_ = false; | |
| 397 max_width_ = 0; | 396 max_width_ = 0; |
| 398 is_first_paint_text_ = true; | 397 is_first_paint_text_ = true; |
| 399 SetText(text); | 398 SetText(text); |
| 400 } | 399 } |
| 401 | 400 |
| 402 void Label::ResetLayout() { | 401 void Label::ResetLayout() { |
| 403 InvalidateLayout(); | 402 InvalidateLayout(); |
| 404 PreferredSizeChanged(); | 403 PreferredSizeChanged(); |
| 405 SchedulePaint(); | 404 SchedulePaint(); |
| 406 lines_.clear(); | 405 lines_.clear(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 444 if (alignment == gfx::ALIGN_TO_HEAD) | 443 if (alignment == gfx::ALIGN_TO_HEAD) |
| 445 alignment = rtl ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; | 444 alignment = rtl ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; |
| 446 directionality = | 445 directionality = |
| 447 rtl ? gfx::DIRECTIONALITY_FORCE_RTL : gfx::DIRECTIONALITY_FORCE_LTR; | 446 rtl ? gfx::DIRECTIONALITY_FORCE_RTL : gfx::DIRECTIONALITY_FORCE_LTR; |
| 448 } | 447 } |
| 449 | 448 |
| 450 // Text eliding is not supported for multi-lined Labels. | 449 // Text eliding is not supported for multi-lined Labels. |
| 451 // TODO(mukai): Add multi-lined elided text support. | 450 // TODO(mukai): Add multi-lined elided text support. |
| 452 gfx::ElideBehavior elide_behavior = | 451 gfx::ElideBehavior elide_behavior = |
| 453 multi_line() ? gfx::NO_ELIDE : elide_behavior_; | 452 multi_line() ? gfx::NO_ELIDE : elide_behavior_; |
| 454 if (!multi_line() || | 453 if (!multi_line() || render_text_->MultilineSupported()) { |
| 455 (render_text_->MultilineSupported() && !allow_character_break_)) { | |
| 456 scoped_ptr<gfx::RenderText> render_text = | 454 scoped_ptr<gfx::RenderText> render_text = |
| 457 CreateRenderText(text(), alignment, directionality, elide_behavior); | 455 CreateRenderText(text(), alignment, directionality, elide_behavior); |
| 458 render_text->SetDisplayRect(rect); | 456 render_text->SetDisplayRect(rect); |
| 459 render_text->SetMultiline(multi_line()); | 457 render_text->SetMultiline(multi_line()); |
| 460 lines_.push_back(render_text.release()); | 458 lines_.push_back(render_text.release()); |
| 461 } else { | 459 } else { |
| 462 std::vector<base::string16> lines = GetLinesForWidth(rect.width()); | 460 std::vector<base::string16> lines = GetLinesForWidth(rect.width()); |
| 463 if (lines.size() > 1) | 461 if (lines.size() > 1) |
| 464 rect.set_height(std::max(line_height(), font_list().GetHeight())); | 462 rect.set_height(std::max(line_height(), font_list().GetHeight())); |
| 465 | 463 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 497 return focus_bounds; | 495 return focus_bounds; |
| 498 } | 496 } |
| 499 | 497 |
| 500 std::vector<base::string16> Label::GetLinesForWidth(int width) const { | 498 std::vector<base::string16> Label::GetLinesForWidth(int width) const { |
| 501 std::vector<base::string16> lines; | 499 std::vector<base::string16> lines; |
| 502 // |width| can be 0 when getting the default text size, in that case | 500 // |width| can be 0 when getting the default text size, in that case |
| 503 // the ideal lines (i.e. broken at newline characters) are wanted. | 501 // the ideal lines (i.e. broken at newline characters) are wanted. |
| 504 if (width <= 0) { | 502 if (width <= 0) { |
| 505 base::SplitString(render_text_->GetDisplayText(), '\n', &lines); | 503 base::SplitString(render_text_->GetDisplayText(), '\n', &lines); |
| 506 } else { | 504 } else { |
| 507 const gfx::WordWrapBehavior wrap = allow_character_break_ | 505 const gfx::WordWrapBehavior wrap = render_text_->allow_character_break() |
| 508 ? gfx::WRAP_LONG_WORDS | 506 ? gfx::WRAP_LONG_WORDS |
| 509 : gfx::TRUNCATE_LONG_WORDS; | 507 : gfx::TRUNCATE_LONG_WORDS; |
| 510 gfx::ElideRectangleText(render_text_->GetDisplayText(), font_list(), width, | 508 gfx::ElideRectangleText(render_text_->GetDisplayText(), font_list(), width, |
| 511 std::numeric_limits<int>::max(), wrap, &lines); | 509 std::numeric_limits<int>::max(), wrap, &lines); |
| 512 } | 510 } |
| 513 return lines; | 511 return lines; |
| 514 } | 512 } |
| 515 | 513 |
| 516 gfx::Size Label::GetTextSize() const { | 514 gfx::Size Label::GetTextSize() const { |
| 517 gfx::Size size; | 515 gfx::Size size; |
| 518 if (text().empty()) { | 516 if (text().empty()) { |
| 519 size = gfx::Size(0, std::max(line_height(), font_list().GetHeight())); | 517 size = gfx::Size(0, std::max(line_height(), font_list().GetHeight())); |
| 520 } else if (!multi_line() || | 518 } else if (!multi_line() || render_text_->MultilineSupported()) { |
| 521 (render_text_->MultilineSupported() && !allow_character_break_)) { | |
| 522 // Cancel the display rect of |render_text_|. The display rect may be | 519 // Cancel the display rect of |render_text_|. The display rect may be |
| 523 // specified in GetHeightForWidth(), and specifying empty Rect cancels | 520 // specified in GetHeightForWidth(), and specifying empty Rect cancels |
| 524 // its effect. See also the comment in GetHeightForWidth(). | 521 // its effect. See also the comment in GetHeightForWidth(). |
| 525 // TODO(mukai): use gfx::Rect() to compute the ideal size rather than | 522 // TODO(mukai): use gfx::Rect() to compute the ideal size rather than |
| 526 // the current width(). See crbug.com/468494, crbug.com/467526, and | 523 // the current width(). See crbug.com/468494, crbug.com/467526, and |
| 527 // the comment for MultilinePreferredSizeTest in label_unittest.cc. | 524 // the comment for MultilinePreferredSizeTest in label_unittest.cc. |
| 528 render_text_->SetDisplayRect(gfx::Rect(0, 0, width(), 0)); | 525 render_text_->SetDisplayRect(gfx::Rect(0, 0, width(), 0)); |
| 529 size = render_text_->GetStringSize(); | 526 size = render_text_->GetStringSize(); |
| 530 } else { | 527 } else { |
| 531 // Get the natural text size, unelided and only wrapped on newlines. | 528 // Get the natural text size, unelided and only wrapped on newlines. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 581 } | 578 } |
| 582 | 579 |
| 583 bool Label::ShouldShowDefaultTooltip() const { | 580 bool Label::ShouldShowDefaultTooltip() const { |
| 584 const gfx::Size text_size = GetTextSize(); | 581 const gfx::Size text_size = GetTextSize(); |
| 585 const gfx::Size size = GetContentsBounds().size(); | 582 const gfx::Size size = GetContentsBounds().size(); |
| 586 return !obscured() && (text_size.width() > size.width() || | 583 return !obscured() && (text_size.width() > size.width() || |
| 587 (multi_line() && text_size.height() > size.height())); | 584 (multi_line() && text_size.height() > size.height())); |
| 588 } | 585 } |
| 589 | 586 |
| 590 } // namespace views | 587 } // namespace views |
| OLD | NEW |