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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 | 31 |
| 32 const gfx::FontList& GetDefaultFontList() { | 32 const gfx::FontList& GetDefaultFontList() { |
| 33 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 33 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 34 return rb.GetFontListWithDelta(ui::kLabelFontSizeDelta); | 34 return rb.GetFontListWithDelta(ui::kLabelFontSizeDelta); |
| 35 } | 35 } |
| 36 | 36 |
| 37 } // namespace | 37 } // namespace |
| 38 | 38 |
| 39 // static | 39 // static |
| 40 const char Label::kViewClassName[] = "Label"; | 40 const char Label::kViewClassName[] = "Label"; |
| 41 const int Label::kFocusBorderPadding = 1; | |
| 42 | 41 |
| 43 Label::Label() : Label(base::string16()) { | 42 Label::Label() : Label(base::string16()) { |
| 44 } | 43 } |
| 45 | 44 |
| 46 Label::Label(const base::string16& text) : Label(text, GetDefaultFontList()) { | 45 Label::Label(const base::string16& text) : Label(text, GetDefaultFontList()) { |
| 47 } | 46 } |
| 48 | 47 |
| 49 Label::Label(const base::string16& text, const gfx::FontList& font_list) { | 48 Label::Label(const base::string16& text, const gfx::FontList& font_list) { |
| 50 Init(text, font_list); | 49 Init(text, font_list); |
| 51 } | 50 } |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 if (lines_.empty()) | 203 if (lines_.empty()) |
| 205 return result; | 204 return result; |
| 206 result.append(lines_[0]->GetDisplayText()); | 205 result.append(lines_[0]->GetDisplayText()); |
| 207 for (size_t i = 1; i < lines_.size(); ++i) { | 206 for (size_t i = 1; i < lines_.size(); ++i) { |
| 208 result.append(1, '\n'); | 207 result.append(1, '\n'); |
| 209 result.append(lines_[i]->GetDisplayText()); | 208 result.append(lines_[i]->GetDisplayText()); |
| 210 } | 209 } |
| 211 return result; | 210 return result; |
| 212 } | 211 } |
| 213 | 212 |
| 214 gfx::Insets Label::GetInsets() const { | |
|
sky
2016/04/27 16:34:20
Is there a reason to remove this? Isn't is possibl
| |
| 215 gfx::Insets insets = View::GetInsets(); | |
| 216 if (focusable()) { | |
| 217 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding, | |
| 218 kFocusBorderPadding, kFocusBorderPadding); | |
| 219 } | |
| 220 return insets; | |
| 221 } | |
| 222 | |
| 223 int Label::GetBaseline() const { | 213 int Label::GetBaseline() const { |
| 224 return GetInsets().top() + font_list().GetBaseline(); | 214 return GetInsets().top() + font_list().GetBaseline(); |
| 225 } | 215 } |
| 226 | 216 |
| 227 gfx::Size Label::GetPreferredSize() const { | 217 gfx::Size Label::GetPreferredSize() const { |
| 228 // Return a size of (0, 0) if the label is not visible and if the | 218 // Return a size of (0, 0) if the label is not visible and if the |
| 229 // |collapse_when_hidden_| flag is set. | 219 // |collapse_when_hidden_| flag is set. |
| 230 // TODO(munjal): This logic probably belongs to the View class. But for now, | 220 // TODO(munjal): This logic probably belongs to the View class. But for now, |
| 231 // put it here since putting it in View class means all inheriting classes | 221 // put it here since putting it in View class means all inheriting classes |
| 232 // need to respect the |collapse_when_hidden_| flag. | 222 // need to respect the |collapse_when_hidden_| flag. |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 427 InvalidateLayout(); | 417 InvalidateLayout(); |
| 428 PreferredSizeChanged(); | 418 PreferredSizeChanged(); |
| 429 SchedulePaint(); | 419 SchedulePaint(); |
| 430 lines_.clear(); | 420 lines_.clear(); |
| 431 } | 421 } |
| 432 | 422 |
| 433 void Label::MaybeBuildRenderTextLines() { | 423 void Label::MaybeBuildRenderTextLines() { |
| 434 if (!lines_.empty()) | 424 if (!lines_.empty()) |
| 435 return; | 425 return; |
| 436 | 426 |
| 437 gfx::Rect rect = GetContentsBounds(); | 427 // GetContentBounds() does not use GetInsets() which may be overriden in Label |
|
sky
2016/04/28 00:05:56
ick. We should fix GetcontentsBounds to call GetIn
karandeepb
2016/04/28 00:13:34
Will assign a bug to myself and fix this.
| |
| 438 if (focusable()) | 428 // subclasses which are focusable, for eg, link. Hence use GetLocalBounds(). |
| 439 rect.Inset(kFocusBorderPadding, kFocusBorderPadding); | 429 gfx::Rect rect = GetLocalBounds(); |
| 430 rect.Inset(GetInsets()); | |
| 440 if (rect.IsEmpty()) | 431 if (rect.IsEmpty()) |
| 441 return; | 432 return; |
| 442 rect.Inset(-gfx::ShadowValue::GetMargin(shadows())); | 433 rect.Inset(-gfx::ShadowValue::GetMargin(shadows())); |
| 443 | 434 |
| 444 gfx::HorizontalAlignment alignment = horizontal_alignment(); | 435 gfx::HorizontalAlignment alignment = horizontal_alignment(); |
| 445 gfx::DirectionalityMode directionality = render_text_->directionality_mode(); | 436 gfx::DirectionalityMode directionality = render_text_->directionality_mode(); |
| 446 if (multi_line()) { | 437 if (multi_line()) { |
| 447 // Force the directionality and alignment of the first line on other lines. | 438 // Force the directionality and alignment of the first line on other lines. |
| 448 bool rtl = | 439 bool rtl = |
| 449 render_text_->GetDisplayTextDirection() == base::i18n::RIGHT_TO_LEFT; | 440 render_text_->GetDisplayTextDirection() == base::i18n::RIGHT_TO_LEFT; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 490 gfx::Rect focus_bounds; | 481 gfx::Rect focus_bounds; |
| 491 if (lines_.empty()) { | 482 if (lines_.empty()) { |
| 492 focus_bounds = gfx::Rect(GetTextSize()); | 483 focus_bounds = gfx::Rect(GetTextSize()); |
| 493 } else { | 484 } else { |
| 494 for (size_t i = 0; i < lines_.size(); ++i) { | 485 for (size_t i = 0; i < lines_.size(); ++i) { |
| 495 gfx::Point origin; | 486 gfx::Point origin; |
| 496 origin += lines_[i]->GetLineOffset(0); | 487 origin += lines_[i]->GetLineOffset(0); |
| 497 focus_bounds.Union(gfx::Rect(origin, lines_[i]->GetStringSize())); | 488 focus_bounds.Union(gfx::Rect(origin, lines_[i]->GetStringSize())); |
| 498 } | 489 } |
| 499 } | 490 } |
| 500 | 491 focus_bounds.Inset(-GetInsets()); |
|
karandeepb
2016/04/27 08:04:40
This will be different from the current value if t
| |
| 501 focus_bounds.Inset(-kFocusBorderPadding, -kFocusBorderPadding); | |
| 502 focus_bounds.Intersect(GetLocalBounds()); | 492 focus_bounds.Intersect(GetLocalBounds()); |
| 503 return focus_bounds; | 493 return focus_bounds; |
| 504 } | 494 } |
| 505 | 495 |
| 506 std::vector<base::string16> Label::GetLinesForWidth(int width) const { | 496 std::vector<base::string16> Label::GetLinesForWidth(int width) const { |
| 507 std::vector<base::string16> lines; | 497 std::vector<base::string16> lines; |
| 508 // |width| can be 0 when getting the default text size, in that case | 498 // |width| can be 0 when getting the default text size, in that case |
| 509 // the ideal lines (i.e. broken at newline characters) are wanted. | 499 // the ideal lines (i.e. broken at newline characters) are wanted. |
| 510 if (width <= 0) { | 500 if (width <= 0) { |
| 511 lines = base::SplitString( | 501 lines = base::SplitString( |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 587 } | 577 } |
| 588 | 578 |
| 589 bool Label::ShouldShowDefaultTooltip() const { | 579 bool Label::ShouldShowDefaultTooltip() const { |
| 590 const gfx::Size text_size = GetTextSize(); | 580 const gfx::Size text_size = GetTextSize(); |
| 591 const gfx::Size size = GetContentsBounds().size(); | 581 const gfx::Size size = GetContentsBounds().size(); |
| 592 return !obscured() && (text_size.width() > size.width() || | 582 return !obscured() && (text_size.width() > size.width() || |
| 593 (multi_line() && text_size.height() > size.height())); | 583 (multi_line() && text_size.height() > size.height())); |
| 594 } | 584 } |
| 595 | 585 |
| 596 } // namespace views | 586 } // namespace views |
| OLD | NEW |