| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 SetText(text); | 76 SetText(text); |
| 77 | 77 |
| 78 AddChildView(image_); | 78 AddChildView(image_); |
| 79 image_->set_interactive(false); | 79 image_->set_interactive(false); |
| 80 | 80 |
| 81 AddChildView(label_); | 81 AddChildView(label_); |
| 82 label_->SetFontList(cached_normal_font_list_); | 82 label_->SetFontList(cached_normal_font_list_); |
| 83 label_->SetAutoColorReadabilityEnabled(false); | 83 label_->SetAutoColorReadabilityEnabled(false); |
| 84 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 84 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 85 | 85 |
| 86 // Initialize the colors, border, and layout. | 86 // Inset the button focus rect from the actual border; roughly match Windows. |
| 87 SetStyle(style_); | 87 SetFocusPainter( |
| 88 | 88 Painter::CreateDashedFocusPainterWithInsets(gfx::Insets(3, 3, 3, 3))); |
| 89 SetAccessibleName(text); | 89 SetAccessibleName(text); |
| 90 } | 90 } |
| 91 | 91 |
| 92 LabelButton::~LabelButton() {} | 92 LabelButton::~LabelButton() {} |
| 93 | 93 |
| 94 const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { | 94 const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { |
| 95 if (for_state != STATE_NORMAL && button_state_images_[for_state].isNull()) | 95 if (for_state != STATE_NORMAL && button_state_images_[for_state].isNull()) |
| 96 return button_state_images_[STATE_NORMAL]; | 96 return button_state_images_[STATE_NORMAL]; |
| 97 return button_state_images_[for_state]; | 97 return button_state_images_[for_state]; |
| 98 } | 98 } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 is_default_ ? AddAccelerator(accel) : RemoveAccelerator(accel); | 182 is_default_ ? AddAccelerator(accel) : RemoveAccelerator(accel); |
| 183 | 183 |
| 184 // STYLE_BUTTON uses bold text to indicate default buttons. | 184 // STYLE_BUTTON uses bold text to indicate default buttons. |
| 185 if (style_ == STYLE_BUTTON) { | 185 if (style_ == STYLE_BUTTON) { |
| 186 label_->SetFontList( | 186 label_->SetFontList( |
| 187 is_default ? cached_bold_font_list_ : cached_normal_font_list_); | 187 is_default ? cached_bold_font_list_ : cached_normal_font_list_); |
| 188 } | 188 } |
| 189 } | 189 } |
| 190 | 190 |
| 191 void LabelButton::SetStyle(ButtonStyle style) { | 191 void LabelButton::SetStyle(ButtonStyle style) { |
| 192 // All callers currently pass STYLE_BUTTON, and should only call this once, to |
| 193 // change from the default style. |
| 194 DCHECK_EQ(style, STYLE_BUTTON); |
| 195 DCHECK_EQ(style_, STYLE_TEXTBUTTON); |
| 196 DCHECK(!GetWidget()) << "Can't change button style after adding to a Widget."; |
| 197 |
| 192 style_ = style; | 198 style_ = style; |
| 193 // Inset the button focus rect from the actual border; roughly match Windows. | 199 |
| 194 if (style == STYLE_BUTTON) { | 200 SetFocusPainter(nullptr); |
| 195 SetFocusPainter(nullptr); | 201 label_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 196 } else { | 202 SetFocusable(true); |
| 197 SetFocusPainter(Painter::CreateDashedFocusPainterWithInsets( | 203 SetMinSize(gfx::Size(70, 33)); |
| 198 gfx::Insets(3, 3, 3, 3))); | 204 |
| 199 } | 205 // Themed borders will be set once the button is added to a Widget, since that |
| 200 if (style == STYLE_BUTTON) { | 206 // provides the value of GetNativeTheme(). |
| 201 label_->SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
| 202 SetFocusable(true); | |
| 203 } | |
| 204 if (style == STYLE_BUTTON) | |
| 205 SetMinSize(gfx::Size(70, 33)); | |
| 206 OnNativeThemeChanged(GetNativeTheme()); | |
| 207 ResetCachedPreferredSize(); | |
| 208 } | 207 } |
| 209 | 208 |
| 210 void LabelButton::SetImageLabelSpacing(int spacing) { | 209 void LabelButton::SetImageLabelSpacing(int spacing) { |
| 211 if (spacing == image_label_spacing_) | 210 if (spacing == image_label_spacing_) |
| 212 return; | 211 return; |
| 213 image_label_spacing_ = spacing; | 212 image_label_spacing_ = spacing; |
| 214 ResetCachedPreferredSize(); | 213 ResetCachedPreferredSize(); |
| 215 InvalidateLayout(); | 214 InvalidateLayout(); |
| 216 } | 215 } |
| 217 | 216 |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 GetExtraParams(params); | 515 GetExtraParams(params); |
| 517 return ui::NativeTheme::kHovered; | 516 return ui::NativeTheme::kHovered; |
| 518 } | 517 } |
| 519 | 518 |
| 520 void LabelButton::ResetCachedPreferredSize() { | 519 void LabelButton::ResetCachedPreferredSize() { |
| 521 cached_preferred_size_valid_ = false; | 520 cached_preferred_size_valid_ = false; |
| 522 cached_preferred_size_ = gfx::Size(); | 521 cached_preferred_size_ = gfx::Size(); |
| 523 } | 522 } |
| 524 | 523 |
| 525 } // namespace views | 524 } // namespace views |
| OLD | NEW |