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