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/button/label_button.h" | 5 #include "ui/views/controls/button/label_button.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "ui/gfx/animation/throb_animation.h" | 9 #include "ui/gfx/animation/throb_animation.h" |
10 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 } // namespace | 52 } // namespace |
53 | 53 |
54 namespace views { | 54 namespace views { |
55 | 55 |
56 // static | 56 // static |
57 const int LabelButton::kHoverAnimationDurationMs = 170; | 57 const int LabelButton::kHoverAnimationDurationMs = 170; |
58 | 58 |
59 // static | 59 // static |
60 const char LabelButton::kViewClassName[] = "LabelButton"; | 60 const char LabelButton::kViewClassName[] = "LabelButton"; |
61 | 61 |
62 LabelButton::LabelButton(ButtonListener* listener, const base::string16& text) | 62 LabelButton::LabelButton(ButtonListener* listener) |
63 : CustomButton(listener), | 63 : CustomButton(listener), |
64 image_(new ImageView()), | 64 image_(new ImageView()), |
65 label_(new Label()), | 65 label_(new Label()), |
66 cached_normal_font_list_(GetDefaultNormalFontList()), | 66 cached_normal_font_list_(GetDefaultNormalFontList()), |
67 cached_bold_font_list_(GetDefaultBoldFontList()), | 67 cached_bold_font_list_(GetDefaultBoldFontList()), |
68 button_state_images_(), | 68 button_state_images_(), |
69 button_state_colors_(), | 69 button_state_colors_(), |
70 explicitly_set_colors_(), | 70 explicitly_set_colors_(), |
71 is_default_(false), | 71 is_default_(false), |
72 style_(STYLE_TEXTBUTTON), | 72 style_(STYLE_COUNT), // Something invalid to ensure Init() is called. |
73 border_is_themed_border_(true), | 73 border_is_themed_border_(true), |
74 image_label_spacing_(kSpacing) { | 74 image_label_spacing_(kSpacing) { |
75 SetAnimationDuration(kHoverAnimationDurationMs); | 75 SetAnimationDuration(kHoverAnimationDurationMs); |
76 SetText(text); | |
77 | 76 |
78 AddChildView(image_); | 77 AddChildView(image_); |
79 image_->set_interactive(false); | 78 image_->set_interactive(false); |
80 | 79 |
81 AddChildView(label_); | 80 AddChildView(label_); |
82 label_->SetFontList(cached_normal_font_list_); | 81 label_->SetFontList(cached_normal_font_list_); |
83 label_->SetAutoColorReadabilityEnabled(false); | 82 label_->SetAutoColorReadabilityEnabled(false); |
84 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 83 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
85 | |
86 // Initialize the colors, border, and layout. | |
87 SetStyle(style_); | |
88 | |
89 SetAccessibleName(text); | |
90 } | 84 } |
91 | 85 |
92 LabelButton::~LabelButton() {} | 86 LabelButton::LabelButton(ButtonListener* listener, const base::string16& text) |
| 87 : LabelButton(listener) { |
| 88 InitAsTextbutton(text); |
| 89 } |
| 90 |
| 91 LabelButton::~LabelButton() { |
| 92 } |
| 93 |
| 94 void LabelButton::InitAsTextbutton(const base::string16& text) { |
| 95 SetText(text); |
| 96 SetStyle(STYLE_TEXTBUTTON); |
| 97 } |
| 98 |
| 99 void LabelButton::InitAsButton(const base::string16& text) { |
| 100 SetText(text); |
| 101 SetStyle(STYLE_BUTTON); |
| 102 } |
93 | 103 |
94 const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { | 104 const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { |
95 if (for_state != STATE_NORMAL && button_state_images_[for_state].isNull()) | 105 if (for_state != STATE_NORMAL && button_state_images_[for_state].isNull()) |
96 return button_state_images_[STATE_NORMAL]; | 106 return button_state_images_[STATE_NORMAL]; |
97 return button_state_images_[for_state]; | 107 return button_state_images_[for_state]; |
98 } | 108 } |
99 | 109 |
100 void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) { | 110 void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) { |
101 button_state_images_[for_state] = image; | 111 button_state_images_[for_state] = image; |
102 UpdateImage(); | 112 UpdateImage(); |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 border_is_themed_border_ = false; | 359 border_is_themed_border_ = false; |
350 View::SetBorder(border.Pass()); | 360 View::SetBorder(border.Pass()); |
351 ResetCachedPreferredSize(); | 361 ResetCachedPreferredSize(); |
352 } | 362 } |
353 | 363 |
354 gfx::Rect LabelButton::GetChildAreaBounds() { | 364 gfx::Rect LabelButton::GetChildAreaBounds() { |
355 return GetLocalBounds(); | 365 return GetLocalBounds(); |
356 } | 366 } |
357 | 367 |
358 void LabelButton::OnPaint(gfx::Canvas* canvas) { | 368 void LabelButton::OnPaint(gfx::Canvas* canvas) { |
| 369 DCHECK_NE(STYLE_COUNT, style_) << "LabelButton::Init() not called."; |
359 View::OnPaint(canvas); | 370 View::OnPaint(canvas); |
360 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); | 371 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); |
361 } | 372 } |
362 | 373 |
363 void LabelButton::OnFocus() { | 374 void LabelButton::OnFocus() { |
364 View::OnFocus(); | 375 View::OnFocus(); |
365 // Typically the border renders differently when focused. | 376 // Typically the border renders differently when focused. |
366 SchedulePaint(); | 377 SchedulePaint(); |
367 } | 378 } |
368 | 379 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 GetExtraParams(params); | 527 GetExtraParams(params); |
517 return ui::NativeTheme::kHovered; | 528 return ui::NativeTheme::kHovered; |
518 } | 529 } |
519 | 530 |
520 void LabelButton::ResetCachedPreferredSize() { | 531 void LabelButton::ResetCachedPreferredSize() { |
521 cached_preferred_size_valid_ = false; | 532 cached_preferred_size_valid_ = false; |
522 cached_preferred_size_ = gfx::Size(); | 533 cached_preferred_size_ = gfx::Size(); |
523 } | 534 } |
524 | 535 |
525 } // namespace views | 536 } // namespace views |
OLD | NEW |