| 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 Init(text); |
| 89 } |
| 90 |
| 91 LabelButton::~LabelButton() { |
| 92 } |
| 93 |
| 94 void LabelButton::InitAsButton(const base::string16& text) { |
| 95 SetDefaultStyle(STYLE_BUTTON); |
| 96 Init(text); |
| 97 } |
| 98 |
| 99 void LabelButton::Init(const base::string16& text) { |
| 100 SetText(text); |
| 101 SetStyle(style_ == STYLE_COUNT ? STYLE_TEXTBUTTON : style_); |
| 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 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 scoped_ptr<LabelButtonBorder> LabelButton::CreateDefaultBorder() const { | 354 scoped_ptr<LabelButtonBorder> LabelButton::CreateDefaultBorder() const { |
| 345 return make_scoped_ptr(new LabelButtonBorder(style_)); | 355 return make_scoped_ptr(new LabelButtonBorder(style_)); |
| 346 } | 356 } |
| 347 | 357 |
| 348 void LabelButton::SetBorder(scoped_ptr<Border> border) { | 358 void LabelButton::SetBorder(scoped_ptr<Border> border) { |
| 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 |
| 364 void LabelButton::SetDefaultStyle(ButtonStyle style) { |
| 365 style_ = style; |
| 366 } |
| 367 |
| 354 gfx::Rect LabelButton::GetChildAreaBounds() { | 368 gfx::Rect LabelButton::GetChildAreaBounds() { |
| 355 return GetLocalBounds(); | 369 return GetLocalBounds(); |
| 356 } | 370 } |
| 357 | 371 |
| 358 void LabelButton::OnPaint(gfx::Canvas* canvas) { | 372 void LabelButton::OnPaint(gfx::Canvas* canvas) { |
| 373 DCHECK_NE(STYLE_COUNT, style_) << "LabelButton::Init() not called."; |
| 359 View::OnPaint(canvas); | 374 View::OnPaint(canvas); |
| 360 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); | 375 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); |
| 361 } | 376 } |
| 362 | 377 |
| 363 void LabelButton::OnFocus() { | 378 void LabelButton::OnFocus() { |
| 364 View::OnFocus(); | 379 View::OnFocus(); |
| 365 // Typically the border renders differently when focused. | 380 // Typically the border renders differently when focused. |
| 366 SchedulePaint(); | 381 SchedulePaint(); |
| 367 } | 382 } |
| 368 | 383 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 GetExtraParams(params); | 531 GetExtraParams(params); |
| 517 return ui::NativeTheme::kHovered; | 532 return ui::NativeTheme::kHovered; |
| 518 } | 533 } |
| 519 | 534 |
| 520 void LabelButton::ResetCachedPreferredSize() { | 535 void LabelButton::ResetCachedPreferredSize() { |
| 521 cached_preferred_size_valid_ = false; | 536 cached_preferred_size_valid_ = false; |
| 522 cached_preferred_size_ = gfx::Size(); | 537 cached_preferred_size_ = gfx::Size(); |
| 523 } | 538 } |
| 524 | 539 |
| 525 } // namespace views | 540 } // namespace views |
| OLD | NEW |