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/logging.h" | 7 #include "base/logging.h" |
8 #include "grit/ui_resources.h" | 8 #include "grit/ui_resources.h" |
9 #include "ui/base/animation/throb_animation.h" | 9 #include "ui/base/animation/throb_animation.h" |
10 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 label_->SetHorizontalAlignment(alignment); | 113 label_->SetHorizontalAlignment(alignment); |
114 InvalidateLayout(); | 114 InvalidateLayout(); |
115 } | 115 } |
116 | 116 |
117 void LabelButton::SetIsDefault(bool is_default) { | 117 void LabelButton::SetIsDefault(bool is_default) { |
118 if (is_default == is_default_) | 118 if (is_default == is_default_) |
119 return; | 119 return; |
120 is_default_ = is_default; | 120 is_default_ = is_default; |
121 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE); | 121 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE); |
122 is_default_ ? AddAccelerator(accel) : RemoveAccelerator(accel); | 122 is_default_ ? AddAccelerator(accel) : RemoveAccelerator(accel); |
| 123 |
| 124 // STYLE_BUTTON uses bold text to indicate default buttons. |
| 125 if (style_ == STYLE_BUTTON) { |
| 126 int style = label_->font().GetStyle(); |
| 127 style = is_default ? style | gfx::Font::BOLD : style & !gfx::Font::BOLD; |
| 128 label_->SetFont(label_->font().DeriveFont(0, style)); |
| 129 } |
123 } | 130 } |
124 | 131 |
125 void LabelButton::SetStyle(ButtonStyle style) { | 132 void LabelButton::SetStyle(ButtonStyle style) { |
| 133 // Use the new button style instead of the native button style. |
| 134 // TODO(msw): Officialy deprecate and remove STYLE_NATIVE_TEXTBUTTON. |
| 135 if (style == STYLE_NATIVE_TEXTBUTTON) |
| 136 style = STYLE_BUTTON; |
| 137 |
126 style_ = style; | 138 style_ = style; |
127 set_border(new LabelButtonBorder(style)); | 139 set_border(new LabelButtonBorder(style)); |
128 // Inset the button focus rect from the actual border; roughly match Windows. | 140 // Inset the button focus rect from the actual border; roughly match Windows. |
129 if (style == STYLE_TEXTBUTTON || style == STYLE_NATIVE_TEXTBUTTON) | 141 if (style == STYLE_TEXTBUTTON || style == STYLE_NATIVE_TEXTBUTTON) |
130 set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3)); | 142 set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3)); |
131 if (style == STYLE_BUTTON || style == STYLE_NATIVE_TEXTBUTTON) | 143 if (style == STYLE_BUTTON || style == STYLE_NATIVE_TEXTBUTTON) { |
132 label_->SetHorizontalAlignment(gfx::ALIGN_CENTER); | 144 label_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
133 if (style == STYLE_NATIVE_TEXTBUTTON) | |
134 set_focusable(true); | 145 set_focusable(true); |
| 146 } |
135 if (style == STYLE_BUTTON) { | 147 if (style == STYLE_BUTTON) { |
136 set_min_size(gfx::Size(70, 31)); | 148 set_min_size(gfx::Size(70, 31)); |
137 const SkColor color = GetNativeTheme()->GetSystemColor( | 149 const SkColor color = GetNativeTheme()->GetSystemColor( |
138 ui::NativeTheme::kColorId_WindowBackground); | 150 ui::NativeTheme::kColorId_WindowBackground); |
139 label_->SetShadowColors(color, color); | 151 label_->SetShadowColors(color, color); |
140 label_->SetShadowOffset(0, 1); | 152 label_->SetShadowOffset(0, 1); |
141 } | 153 } |
142 // Invalidate the layout to pickup the new insets from the border. | 154 // Invalidate the layout to pickup the new insets from the border. |
143 InvalidateLayout(); | 155 InvalidateLayout(); |
144 ResetColorsFromNativeTheme(); | 156 ResetColorsFromNativeTheme(); |
145 } | 157 } |
146 | 158 |
147 gfx::Size LabelButton::GetPreferredSize() { | 159 gfx::Size LabelButton::GetPreferredSize() { |
| 160 // Accomodate bold text in case this STYLE_BUTTON button is ever made default. |
| 161 const gfx::Font font = label_->font(); |
| 162 if (style_ == STYLE_BUTTON) |
| 163 label_->SetFont(font.DeriveFont(0, font.GetStyle() | gfx::Font::BOLD)); |
| 164 |
148 // Resize multi-line labels paired with images to use their available width. | 165 // Resize multi-line labels paired with images to use their available width. |
149 const gfx::Size image_size(image_->GetPreferredSize()); | 166 const gfx::Size image_size(image_->GetPreferredSize()); |
150 if (GetTextMultiLine() && !image_size.IsEmpty() && !GetText().empty() && | 167 if (GetTextMultiLine() && !image_size.IsEmpty() && !GetText().empty() && |
151 GetHorizontalAlignment() == gfx::ALIGN_CENTER) { | 168 GetHorizontalAlignment() == gfx::ALIGN_CENTER) { |
152 label_->SizeToFit(GetLocalBounds().width() - image_size.width() - kSpacing); | 169 label_->SizeToFit(GetLocalBounds().width() - image_size.width() - kSpacing); |
153 } | 170 } |
154 | 171 |
155 // Calculate the required size. | 172 // Calculate the required size. |
156 gfx::Size size(label_->GetPreferredSize()); | 173 gfx::Size size(label_->GetPreferredSize()); |
157 if (image_size.width() > 0 && size.width() > 0) | 174 if (image_size.width() > 0 && size.width() > 0) |
158 size.Enlarge(kSpacing, 0); | 175 size.Enlarge(kSpacing, 0); |
159 size.set_height(std::max(size.height(), image_size.height())); | 176 size.set_height(std::max(size.height(), image_size.height())); |
160 size.Enlarge(image_size.width() + GetInsets().width(), GetInsets().height()); | 177 size.Enlarge(image_size.width() + GetInsets().width(), GetInsets().height()); |
161 | 178 |
| 179 // Restore the label's original font without the temporary bold style. |
| 180 if (style_ == STYLE_BUTTON) |
| 181 label_->SetFont(font); |
| 182 |
162 // Increase the minimum size monotonically with the preferred size. | 183 // Increase the minimum size monotonically with the preferred size. |
163 size.SetSize(std::max(min_size_.width(), size.width()), | 184 size.SetSize(std::max(min_size_.width(), size.width()), |
164 std::max(min_size_.height(), size.height())); | 185 std::max(min_size_.height(), size.height())); |
165 min_size_ = size; | 186 min_size_ = size; |
166 | 187 |
167 // Return the largest known size clamped to the maximum size (if valid). | 188 // Return the largest known size clamped to the maximum size (if valid). |
168 if (max_size_.width() > 0) | 189 if (max_size_.width() > 0) |
169 size.set_width(std::min(max_size_.width(), size.width())); | 190 size.set_width(std::min(max_size_.width(), size.width())); |
170 if (max_size_.height() > 0) | 191 if (max_size_.height() > 0) |
171 size.set_height(std::min(max_size_.height(), size.height())); | 192 size.set_height(std::min(max_size_.height(), size.height())); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 params->button.indeterminate = false; | 333 params->button.indeterminate = false; |
313 params->button.is_default = is_default_; | 334 params->button.is_default = is_default_; |
314 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); | 335 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); |
315 params->button.has_border = style() == STYLE_NATIVE_TEXTBUTTON; | 336 params->button.has_border = style() == STYLE_NATIVE_TEXTBUTTON; |
316 params->button.classic_state = 0; | 337 params->button.classic_state = 0; |
317 params->button.background_color = GetNativeTheme()->GetSystemColor( | 338 params->button.background_color = GetNativeTheme()->GetSystemColor( |
318 ui::NativeTheme::kColorId_TextButtonBackgroundColor); | 339 ui::NativeTheme::kColorId_TextButtonBackgroundColor); |
319 } | 340 } |
320 | 341 |
321 } // namespace views | 342 } // namespace views |
OLD | NEW |