blob: 1850afd999dd72e6b03b461d6f6e9e9a1f434afb [file] [log] [blame]
[email protected]70e983cc2012-10-16 21:08:221// 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
3// found in the LICENSE file.
4
5#include "ui/views/controls/button/label_button.h"
6
7#include "base/logging.h"
8#include "grit/ui_resources.h"
9#include "ui/base/animation/throb_animation.h"
10#include "ui/base/resource/resource_bundle.h"
[email protected]8858a832013-06-19 14:46:3011#include "ui/gfx/sys_color_change_listener.h"
[email protected]990e6222012-11-16 13:31:1812#include "ui/native_theme/native_theme.h"
[email protected]70e983cc2012-10-16 21:08:2213#include "ui/views/controls/button/label_button_border.h"
14#include "ui/views/focus_border.h"
[email protected]6d114932013-04-24 02:38:2015#include "ui/views/window/dialog_delegate.h"
[email protected]70e983cc2012-10-16 21:08:2216
17#if defined(OS_WIN)
[email protected]990e6222012-11-16 13:31:1818#include "ui/native_theme/native_theme_win.h"
[email protected]70e983cc2012-10-16 21:08:2219#endif
20
[email protected]70e983cc2012-10-16 21:08:2221namespace {
22
23// The spacing between the icon and text.
[email protected]c5de33662012-10-27 22:37:3024const int kSpacing = 5;
[email protected]70e983cc2012-10-16 21:08:2225
26// The length of the hover fade animation.
[email protected]c5de33662012-10-27 22:37:3027const int kHoverAnimationDurationMs = 170;
[email protected]70e983cc2012-10-16 21:08:2228
[email protected]8858a832013-06-19 14:46:3029// Default text and shadow colors for STYLE_BUTTON.
30const SkColor kStyleButtonTextColor = SK_ColorBLACK;
31const SkColor kStyleButtonShadowColor = SK_ColorWHITE;
32
[email protected]70e983cc2012-10-16 21:08:2233} // namespace
34
[email protected]c5de33662012-10-27 22:37:3035namespace views {
36
[email protected]fe22ba12013-02-21 13:43:4837// static
[email protected]f4167cf2013-05-26 23:36:1538const char LabelButton::kViewClassName[] = "LabelButton";
[email protected]fe22ba12013-02-21 13:43:4839
[email protected]70e983cc2012-10-16 21:08:2240LabelButton::LabelButton(ButtonListener* listener, const string16& text)
41 : CustomButton(listener),
42 image_(new ImageView()),
[email protected]3e4efae2013-04-05 13:17:3643 label_(new Label()),
[email protected]379bc982012-12-19 19:56:5344 button_state_images_(),
45 button_state_colors_(),
46 explicitly_set_colors_(),
[email protected]e59fc52a2013-02-20 20:24:1147 is_default_(false),
[email protected]fe22ba12013-02-21 13:43:4848 style_(STYLE_TEXTBUTTON) {
[email protected]70e983cc2012-10-16 21:08:2249 SetAnimationDuration(kHoverAnimationDurationMs);
[email protected]3e4efae2013-04-05 13:17:3650 SetText(text);
[email protected]70e983cc2012-10-16 21:08:2251
52 AddChildView(image_);
53 image_->set_interactive(false);
54
55 AddChildView(label_);
56 label_->SetAutoColorReadabilityEnabled(false);
[email protected]cc563f02012-11-07 17:37:3657 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
[email protected]70e983cc2012-10-16 21:08:2258
[email protected]fe22ba12013-02-21 13:43:4859 // Initialize the colors, border, and layout.
60 SetStyle(style_);
[email protected]cd70b3e2013-04-03 20:01:3761
62 SetAccessibleName(text);
[email protected]70e983cc2012-10-16 21:08:2263}
64
65LabelButton::~LabelButton() {}
66
67const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) {
[email protected]fca8dc02012-11-14 16:25:5668 if (for_state != STATE_NORMAL && button_state_images_[for_state].isNull())
69 return button_state_images_[STATE_NORMAL];
[email protected]70e983cc2012-10-16 21:08:2270 return button_state_images_[for_state];
71}
72
73void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) {
74 button_state_images_[for_state] = image;
[email protected]987b7662013-05-23 10:45:4975 UpdateImage();
[email protected]70e983cc2012-10-16 21:08:2276}
77
78const string16& LabelButton::GetText() const {
79 return label_->text();
80}
81
82void LabelButton::SetText(const string16& text) {
[email protected]3e4efae2013-04-05 13:17:3683 SetAccessibleName(text);
[email protected]70e983cc2012-10-16 21:08:2284 label_->SetText(text);
85}
86
87void LabelButton::SetTextColor(ButtonState for_state, SkColor color) {
88 button_state_colors_[for_state] = color;
[email protected]fca8dc02012-11-14 16:25:5689 if (for_state == STATE_DISABLED)
[email protected]70e983cc2012-10-16 21:08:2290 label_->SetDisabledColor(color);
91 else if (for_state == state())
92 label_->SetEnabledColor(color);
[email protected]204b2082012-11-07 17:53:0393 explicitly_set_colors_[for_state] = true;
[email protected]70e983cc2012-10-16 21:08:2294}
95
96bool LabelButton::GetTextMultiLine() const {
97 return label_->is_multi_line();
98}
99
100void LabelButton::SetTextMultiLine(bool text_multi_line) {
101 label_->SetMultiLine(text_multi_line);
102}
103
[email protected]cfc888a2012-11-05 18:20:50104const gfx::Font& LabelButton::GetFont() const {
105 return label_->font();
106}
107
108void LabelButton::SetFont(const gfx::Font& font) {
109 label_->SetFont(font);
110}
111
[email protected]cc563f02012-11-07 17:37:36112gfx::HorizontalAlignment LabelButton::GetHorizontalAlignment() const {
[email protected]70e983cc2012-10-16 21:08:22113 return label_->horizontal_alignment();
114}
115
[email protected]cc563f02012-11-07 17:37:36116void LabelButton::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) {
[email protected]70e983cc2012-10-16 21:08:22117 label_->SetHorizontalAlignment(alignment);
118 InvalidateLayout();
119}
120
[email protected]e59fc52a2013-02-20 20:24:11121void LabelButton::SetIsDefault(bool is_default) {
122 if (is_default == is_default_)
[email protected]70e983cc2012-10-16 21:08:22123 return;
[email protected]e59fc52a2013-02-20 20:24:11124 is_default_ = is_default;
[email protected]70e983cc2012-10-16 21:08:22125 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE);
[email protected]e59fc52a2013-02-20 20:24:11126 is_default_ ? AddAccelerator(accel) : RemoveAccelerator(accel);
[email protected]640bff12013-04-17 21:08:14127
128 // STYLE_BUTTON uses bold text to indicate default buttons.
129 if (style_ == STYLE_BUTTON) {
130 int style = label_->font().GetStyle();
131 style = is_default ? style | gfx::Font::BOLD : style & !gfx::Font::BOLD;
132 label_->SetFont(label_->font().DeriveFont(0, style));
133 }
[email protected]70e983cc2012-10-16 21:08:22134}
135
[email protected]fe22ba12013-02-21 13:43:48136void LabelButton::SetStyle(ButtonStyle style) {
[email protected]640bff12013-04-17 21:08:14137 // Use the new button style instead of the native button style.
138 // TODO(msw): Officialy deprecate and remove STYLE_NATIVE_TEXTBUTTON.
[email protected]6d114932013-04-24 02:38:20139 if (DialogDelegate::UseNewStyle() && style == STYLE_NATIVE_TEXTBUTTON)
[email protected]640bff12013-04-17 21:08:14140 style = STYLE_BUTTON;
141
[email protected]fe22ba12013-02-21 13:43:48142 style_ = style;
143 set_border(new LabelButtonBorder(style));
144 // Inset the button focus rect from the actual border; roughly match Windows.
[email protected]682189b2013-05-10 02:42:26145 set_focus_border(style == STYLE_BUTTON ?
146 NULL : FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3));
[email protected]640bff12013-04-17 21:08:14147 if (style == STYLE_BUTTON || style == STYLE_NATIVE_TEXTBUTTON) {
[email protected]d0abd282013-02-28 23:59:08148 label_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
[email protected]df798cb2013-04-05 06:27:52149 set_focusable(true);
[email protected]640bff12013-04-17 21:08:14150 }
[email protected]8858a832013-06-19 14:46:30151 if (style == STYLE_BUTTON)
[email protected]31012722013-06-12 22:00:10152 set_min_size(gfx::Size(70, 33));
[email protected]204b2082012-11-07 17:53:03153 // Invalidate the layout to pickup the new insets from the border.
154 InvalidateLayout();
[email protected]379bc982012-12-19 19:56:53155 ResetColorsFromNativeTheme();
[email protected]204b2082012-11-07 17:53:03156}
157
[email protected]7f39bb692013-01-07 23:08:16158gfx::Size LabelButton::GetPreferredSize() {
[email protected]640bff12013-04-17 21:08:14159 const gfx::Font font = label_->font();
[email protected]9baef622013-05-14 05:30:46160 // Use a temporary label copy for sizing to avoid calculation side-effects.
161 // Use bold text for STYLE_BUTTON to avoid changing size when made default.
162 Label label(label_->text(), style_ == STYLE_BUTTON ?
163 font.DeriveFont(0, font.GetStyle() | gfx::Font::BOLD) : font);
164 label.SetMultiLine(GetTextMultiLine());
[email protected]640bff12013-04-17 21:08:14165
[email protected]9baef622013-05-14 05:30:46166 // Resize multi-line labels given the current limited available width.
[email protected]7f39bb692013-01-07 23:08:16167 const gfx::Size image_size(image_->GetPreferredSize());
[email protected]9baef622013-05-14 05:30:46168 const int image_width = image_size.width();
169 if (GetTextMultiLine() && (width() > image_width + kSpacing))
170 label.SizeToFit(width() - image_width - (image_width > 0 ? kSpacing : 0));
[email protected]7f39bb692013-01-07 23:08:16171
172 // Calculate the required size.
[email protected]9baef622013-05-14 05:30:46173 gfx::Size size(label.GetPreferredSize());
174 if (image_width > 0 && size.width() > 0)
[email protected]7f39bb692013-01-07 23:08:16175 size.Enlarge(kSpacing, 0);
[email protected]a4a08d02013-05-30 00:18:00176 size.SetToMax(gfx::Size(0, image_size.height()));
[email protected]9baef622013-05-14 05:30:46177 const gfx::Insets insets(GetInsets());
178 size.Enlarge(image_size.width() + insets.width(), insets.height());
[email protected]640bff12013-04-17 21:08:14179
[email protected]7f39bb692013-01-07 23:08:16180 // Increase the minimum size monotonically with the preferred size.
[email protected]a4a08d02013-05-30 00:18:00181 size.SetToMax(min_size_);
[email protected]7f39bb692013-01-07 23:08:16182 min_size_ = size;
183
184 // Return the largest known size clamped to the maximum size (if valid).
185 if (max_size_.width() > 0)
186 size.set_width(std::min(max_size_.width(), size.width()));
187 if (max_size_.height() > 0)
188 size.set_height(std::min(max_size_.height(), size.height()));
189 return size;
190}
191
[email protected]987b7662013-05-23 10:45:49192void LabelButton::Layout() {
193 gfx::HorizontalAlignment adjusted_alignment = GetHorizontalAlignment();
194 if (base::i18n::IsRTL() && adjusted_alignment != gfx::ALIGN_CENTER)
195 adjusted_alignment = (adjusted_alignment == gfx::ALIGN_LEFT) ?
196 gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT;
197
198 gfx::Rect child_area(GetLocalBounds());
199 child_area.Inset(GetInsets());
200
201 gfx::Size image_size(image_->GetPreferredSize());
202 image_size.set_width(std::min(image_size.width(), child_area.width()));
203 image_size.set_height(std::min(image_size.height(), child_area.height()));
204
205 // The label takes any remaining width after sizing the image, unless both
206 // views are centered. In that case, using the tighter preferred label width
207 // avoids wasted space within the label that would look like awkward padding.
208 gfx::Size label_size(child_area.size());
209 if (!image_size.IsEmpty() && !label_size.IsEmpty()) {
210 label_size.set_width(
211 std::max(child_area.width() - image_size.width() - kSpacing, 0));
212 if (adjusted_alignment == gfx::ALIGN_CENTER) {
213 // Ensure multi-line labels paired with images use their available width.
214 if (GetTextMultiLine())
215 label_->SizeToFit(label_size.width());
216 label_size.set_width(
217 std::min(label_size.width(), label_->GetPreferredSize().width()));
218 }
219 }
220
221 gfx::Point image_origin(child_area.origin());
222 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2);
223 if (adjusted_alignment == gfx::ALIGN_CENTER) {
224 const int total_width = image_size.width() + label_size.width() +
225 ((image_size.width() > 0 && label_size.width() > 0) ? kSpacing : 0);
226 image_origin.Offset((child_area.width() - total_width) / 2, 0);
227 } else if (adjusted_alignment == gfx::ALIGN_RIGHT) {
228 image_origin.Offset(child_area.width() - image_size.width(), 0);
229 }
230
231 gfx::Point label_origin(child_area.origin());
232 if (!image_size.IsEmpty() &&adjusted_alignment != gfx::ALIGN_RIGHT)
233 label_origin.set_x(image_origin.x() + image_size.width() + kSpacing);
234
235 image_->SetBoundsRect(gfx::Rect(image_origin, image_size));
236 label_->SetBoundsRect(gfx::Rect(label_origin, label_size));
237}
238
[email protected]707bd372013-05-09 08:32:27239const char* LabelButton::GetClassName() const {
[email protected]fe22ba12013-02-21 13:43:48240 return kViewClassName;
241}
242
[email protected]987b7662013-05-23 10:45:49243void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const {
244 params->button.checked = false;
245 params->button.indeterminate = false;
246 params->button.is_default = is_default_;
247 params->button.is_focused = HasFocus() && IsAccessibilityFocusable();
248 params->button.has_border = style() == STYLE_NATIVE_TEXTBUTTON;
249 params->button.classic_state = 0;
[email protected]8858a832013-06-19 14:46:30250 params->button.background_color = label()->background_color();
[email protected]987b7662013-05-23 10:45:49251}
252
253void LabelButton::UpdateImage() {
254 image_->SetImage(GetImage(state()));
255}
256
[email protected]379bc982012-12-19 19:56:53257void LabelButton::ResetColorsFromNativeTheme() {
[email protected]204b2082012-11-07 17:53:03258 const ui::NativeTheme* theme = GetNativeTheme();
[email protected]fca8dc02012-11-14 16:25:56259 SkColor colors[STATE_COUNT] = {
[email protected]1b768f8f2013-05-17 01:16:52260 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor),
261 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor),
262 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor),
263 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonDisabledColor),
[email protected]70e983cc2012-10-16 21:08:22264 };
[email protected]644f4fb2013-03-01 21:14:34265
266 // Certain styles do not change text color when hovered or pressed.
[email protected]8858a832013-06-19 14:46:30267 bool constant_text_color = false;
[email protected]70e983cc2012-10-16 21:08:22268#if defined(OS_WIN)
[email protected]644f4fb2013-03-01 21:14:34269 constant_text_color |= (style() == STYLE_NATIVE_TEXTBUTTON &&
270 theme == ui::NativeThemeWin::instance());
[email protected]70e983cc2012-10-16 21:08:22271#endif
[email protected]8858a832013-06-19 14:46:30272
273 label_->SetBackgroundColor(theme->GetSystemColor(
274 ui::NativeTheme::kColorId_ButtonBackgroundColor));
275
276 // Use hardcoded colors for inverted color scheme support and STYLE_BUTTON.
277 if (gfx::IsInvertedColorScheme()) {
278 constant_text_color = true;
279 colors[STATE_NORMAL] = SK_ColorWHITE;
280 label_->SetBackgroundColor(SK_ColorBLACK);
281 label_->SetAutoColorReadabilityEnabled(true);
282 label_->ClearEmbellishing();
283 } else if (style() == STYLE_BUTTON) {
284 constant_text_color = true;
285 colors[STATE_NORMAL] = kStyleButtonTextColor;
286 label_->SetAutoColorReadabilityEnabled(false);
287 label_->SetShadowColors(kStyleButtonShadowColor, kStyleButtonShadowColor);
288 label_->SetShadowOffset(0, 1);
289 }
290
[email protected]644f4fb2013-03-01 21:14:34291 if (constant_text_color)
292 colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL];
293
[email protected]fca8dc02012-11-14 16:25:56294 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) {
[email protected]379bc982012-12-19 19:56:53295 if (!explicitly_set_colors_[state]) {
[email protected]204b2082012-11-07 17:53:03296 SetTextColor(static_cast<ButtonState>(state), colors[state]);
297 explicitly_set_colors_[state] = false;
298 }
299 }
[email protected]70e983cc2012-10-16 21:08:22300}
301
302void LabelButton::StateChanged() {
303 const gfx::Size previous_image_size(image_->GetPreferredSize());
[email protected]987b7662013-05-23 10:45:49304 UpdateImage();
[email protected]70e983cc2012-10-16 21:08:22305 const SkColor color = button_state_colors_[state()];
[email protected]fca8dc02012-11-14 16:25:56306 if (state() != STATE_DISABLED && label_->enabled_color() != color)
[email protected]70e983cc2012-10-16 21:08:22307 label_->SetEnabledColor(color);
[email protected]4e88e352012-12-04 19:12:09308 label_->SetEnabled(state() != STATE_DISABLED);
[email protected]70e983cc2012-10-16 21:08:22309 if (image_->GetPreferredSize() != previous_image_size)
310 Layout();
311}
312
[email protected]70e983cc2012-10-16 21:08:22313void LabelButton::ChildPreferredSizeChanged(View* child) {
314 PreferredSizeChanged();
315}
316
[email protected]204b2082012-11-07 17:53:03317void LabelButton::OnNativeThemeChanged(const ui::NativeTheme* theme) {
[email protected]379bc982012-12-19 19:56:53318 ResetColorsFromNativeTheme();
[email protected]204b2082012-11-07 17:53:03319}
320
[email protected]70e983cc2012-10-16 21:08:22321ui::NativeTheme::Part LabelButton::GetThemePart() const {
322 return ui::NativeTheme::kPushButton;
323}
324
325gfx::Rect LabelButton::GetThemePaintRect() const {
326 return GetLocalBounds();
327}
328
329ui::NativeTheme::State LabelButton::GetThemeState(
330 ui::NativeTheme::ExtraParams* params) const {
331 GetExtraParams(params);
[email protected]987b7662013-05-23 10:45:49332 switch (state()) {
[email protected]fca8dc02012-11-14 16:25:56333 case STATE_NORMAL: return ui::NativeTheme::kNormal;
334 case STATE_HOVERED: return ui::NativeTheme::kHovered;
335 case STATE_PRESSED: return ui::NativeTheme::kPressed;
336 case STATE_DISABLED: return ui::NativeTheme::kDisabled;
337 case STATE_COUNT: NOTREACHED() << "Unknown state: " << state();
[email protected]70e983cc2012-10-16 21:08:22338 }
339 return ui::NativeTheme::kNormal;
340}
341
342const ui::Animation* LabelButton::GetThemeAnimation() const {
[email protected]204b2082012-11-07 17:53:03343#if defined(OS_WIN)
[email protected]fe22ba12013-02-21 13:43:48344 if (style() == STYLE_NATIVE_TEXTBUTTON &&
345 GetNativeTheme() == ui::NativeThemeWin::instance()) {
[email protected]204b2082012-11-07 17:53:03346 return ui::NativeThemeWin::instance()->IsThemingActive() ?
347 hover_animation_.get() : NULL;
348 }
[email protected]70e983cc2012-10-16 21:08:22349#endif
350 return hover_animation_.get();
351}
352
353ui::NativeTheme::State LabelButton::GetBackgroundThemeState(
354 ui::NativeTheme::ExtraParams* params) const {
355 GetExtraParams(params);
356 return ui::NativeTheme::kNormal;
357}
358
359ui::NativeTheme::State LabelButton::GetForegroundThemeState(
360 ui::NativeTheme::ExtraParams* params) const {
361 GetExtraParams(params);
362 return ui::NativeTheme::kHovered;
363}
364
[email protected]70e983cc2012-10-16 21:08:22365} // namespace views