blob: 1de7867d896fe27476229e92c1c7ced1912bc086 [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"
[email protected]204b2082012-11-07 17:53:0310#include "ui/base/native_theme/native_theme.h"
[email protected]70e983cc2012-10-16 21:08:2211#include "ui/base/resource/resource_bundle.h"
12#include "ui/views/controls/button/label_button_border.h"
13#include "ui/views/focus_border.h"
14
15#if defined(OS_WIN)
16#include "ui/gfx/color_utils.h"
17#include "ui/base/native_theme/native_theme_win.h"
18#endif
19
[email protected]70e983cc2012-10-16 21:08:2220namespace {
21
22// The spacing between the icon and text.
[email protected]c5de33662012-10-27 22:37:3023const int kSpacing = 5;
[email protected]70e983cc2012-10-16 21:08:2224
25// The length of the hover fade animation.
[email protected]c5de33662012-10-27 22:37:3026const int kHoverAnimationDurationMs = 170;
[email protected]70e983cc2012-10-16 21:08:2227
28} // namespace
29
[email protected]c5de33662012-10-27 22:37:3030namespace views {
31
[email protected]70e983cc2012-10-16 21:08:2232LabelButton::LabelButton(ButtonListener* listener, const string16& text)
33 : CustomButton(listener),
34 image_(new ImageView()),
35 label_(new Label(text)),
36 default_button_(false),
37 native_theme_(false) {
[email protected]c5de33662012-10-27 22:37:3038 set_border(new LabelButtonBorder());
[email protected]70e983cc2012-10-16 21:08:2239 // Inset the button focus rect from the actual border; roughly match Windows.
40 set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3));
41 SetAnimationDuration(kHoverAnimationDurationMs);
42
43 AddChildView(image_);
44 image_->set_interactive(false);
45
46 AddChildView(label_);
47 label_->SetAutoColorReadabilityEnabled(false);
[email protected]cc563f02012-11-07 17:37:3648 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
[email protected]70e983cc2012-10-16 21:08:2249
50 // Initialize the colors, border, and layout for a Views-themed button.
51 SetNativeTheme(false);
52}
53
54LabelButton::~LabelButton() {}
55
56const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) {
57 if (for_state != BS_NORMAL && button_state_images_[for_state].isNull())
58 return button_state_images_[BS_NORMAL];
59 return button_state_images_[for_state];
60}
61
62void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) {
63 button_state_images_[for_state] = image;
64 image_->SetImage(GetImage(state()));
65}
66
67const string16& LabelButton::GetText() const {
68 return label_->text();
69}
70
71void LabelButton::SetText(const string16& text) {
72 label_->SetText(text);
73}
74
75void LabelButton::SetTextColor(ButtonState for_state, SkColor color) {
76 button_state_colors_[for_state] = color;
77 if (for_state == BS_DISABLED)
78 label_->SetDisabledColor(color);
79 else if (for_state == state())
80 label_->SetEnabledColor(color);
[email protected]204b2082012-11-07 17:53:0381 explicitly_set_colors_[for_state] = true;
[email protected]70e983cc2012-10-16 21:08:2282}
83
84bool LabelButton::GetTextMultiLine() const {
85 return label_->is_multi_line();
86}
87
88void LabelButton::SetTextMultiLine(bool text_multi_line) {
89 label_->SetMultiLine(text_multi_line);
90}
91
[email protected]cfc888a2012-11-05 18:20:5092const gfx::Font& LabelButton::GetFont() const {
93 return label_->font();
94}
95
96void LabelButton::SetFont(const gfx::Font& font) {
97 label_->SetFont(font);
98}
99
[email protected]cc563f02012-11-07 17:37:36100gfx::HorizontalAlignment LabelButton::GetHorizontalAlignment() const {
[email protected]70e983cc2012-10-16 21:08:22101 return label_->horizontal_alignment();
102}
103
[email protected]cc563f02012-11-07 17:37:36104void LabelButton::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) {
[email protected]70e983cc2012-10-16 21:08:22105 label_->SetHorizontalAlignment(alignment);
106 InvalidateLayout();
107}
108
109void LabelButton::SetDefaultButton(bool default_button) {
110 if (default_button == default_button_)
111 return;
112 default_button_ = default_button;
113 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE);
114 default_button_ ? AddAccelerator(accel) : RemoveAccelerator(accel);
115}
116
117void LabelButton::SetNativeTheme(bool native_theme) {
118 native_theme_ = native_theme;
119 static_cast<LabelButtonBorder*>(border())->set_native_theme(native_theme);
[email protected]204b2082012-11-07 17:53:03120 // Invalidate the layout to pickup the new insets from the border.
121 InvalidateLayout();
[email protected]70e983cc2012-10-16 21:08:22122
[email protected]204b2082012-11-07 17:53:03123 ResetColorsFromNativeTheme(true);
124}
125
126void LabelButton::ResetColorsFromNativeTheme(bool reset_all) {
127 const ui::NativeTheme* theme = GetNativeTheme();
[email protected]70e983cc2012-10-16 21:08:22128 SkColor colors[BS_COUNT] = {
129 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonEnabledColor),
130 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor),
131 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor),
132 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonDisabledColor),
133 };
134#if defined(OS_WIN)
[email protected]204b2082012-11-07 17:53:03135 if (native_theme_ && theme == ui::NativeThemeWin::instance()) {
[email protected]70e983cc2012-10-16 21:08:22136 colors[BS_NORMAL] = color_utils::GetSysSkColor(COLOR_BTNTEXT);
137 colors[BS_HOT] = color_utils::GetSysSkColor(COLOR_BTNTEXT);
138 colors[BS_PUSHED] = color_utils::GetSysSkColor(COLOR_BTNTEXT);
139 colors[BS_DISABLED] = color_utils::GetSysSkColor(COLOR_GRAYTEXT);
140 }
141#endif
[email protected]204b2082012-11-07 17:53:03142 for (size_t state = BS_NORMAL; state < BS_COUNT; ++state) {
143 if (reset_all || !explicitly_set_colors_[state]) {
144 SetTextColor(static_cast<ButtonState>(state), colors[state]);
145 explicitly_set_colors_[state] = false;
146 }
147 }
[email protected]70e983cc2012-10-16 21:08:22148}
149
150void LabelButton::StateChanged() {
151 const gfx::Size previous_image_size(image_->GetPreferredSize());
152 image_->SetImage(GetImage(state()));
153 const SkColor color = button_state_colors_[state()];
154 if (state() != BS_DISABLED && label_->enabled_color() != color)
155 label_->SetEnabledColor(color);
156 if (image_->GetPreferredSize() != previous_image_size)
157 Layout();
158}
159
160gfx::Size LabelButton::GetPreferredSize() {
161 gfx::Size size(label_->GetPreferredSize());
[email protected]70e983cc2012-10-16 21:08:22162 const gfx::Size image_size(image_->GetPreferredSize());
163 if (image_size.width() > 0 && size.width() > 0)
164 size.Enlarge(kSpacing, 0);
165 size.set_height(std::max(size.height(), image_size.height()));
166 size.Enlarge(image_size.width() + GetInsets().width(), GetInsets().height());
167
168 // Increase the minimum size monotonically with the preferred size.
169 size.SetSize(std::max(min_size_.width(), size.width()),
170 std::max(min_size_.height(), size.height()));
171 min_size_ = size;
172
173 // Return the largest known size clamped to the maximum size (if valid).
174 if (max_size_.width() > 0)
175 size.set_width(std::min(max_size_.width(), size.width()));
176 if (max_size_.height() > 0)
177 size.set_height(std::min(max_size_.height(), size.height()));
178 return size;
179}
180
181void LabelButton::Layout() {
182 gfx::Rect child_area(GetLocalBounds());
183 child_area.Inset(GetInsets());
184
185 gfx::Size image_size(image_->GetPreferredSize());
186 image_size.set_width(std::min(image_size.width(), child_area.width()));
187 image_size.set_height(std::min(image_size.height(), child_area.height()));
188
[email protected]1c4f28f2012-10-18 23:36:06189 // The label takes any remaining width after sizing the image, unless both
190 // views are centered. In that case, using the tighter preferred label width
191 // avoids wasted space within the label that would look like awkward padding.
[email protected]70e983cc2012-10-16 21:08:22192 gfx::Size label_size(child_area.size());
193 if (!image_size.IsEmpty() && !label_size.IsEmpty()) {
194 label_size.set_width(child_area.width() - image_size.width() - kSpacing);
[email protected]cc563f02012-11-07 17:37:36195 if (GetHorizontalAlignment() == gfx::ALIGN_CENTER) {
[email protected]1c4f28f2012-10-18 23:36:06196 label_size.set_width(std::min(label_size.width(),
197 label_->GetPreferredSize().width()));
[email protected]70e983cc2012-10-16 21:08:22198 }
199 }
200
201 gfx::Point image_origin(child_area.origin());
202 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2);
[email protected]cc563f02012-11-07 17:37:36203 if (GetHorizontalAlignment() == gfx::ALIGN_CENTER) {
[email protected]70e983cc2012-10-16 21:08:22204 const int total_width = image_size.width() + label_size.width() +
205 ((image_size.width() > 0 && label_size.width() > 0) ? kSpacing : 0);
206 image_origin.Offset((child_area.width() - total_width) / 2, 0);
[email protected]cc563f02012-11-07 17:37:36207 } else if (GetHorizontalAlignment() == gfx::ALIGN_RIGHT) {
[email protected]70e983cc2012-10-16 21:08:22208 image_origin.Offset(child_area.width() - image_size.width(), 0);
209 }
210
211 gfx::Point label_origin(child_area.origin());
[email protected]cc563f02012-11-07 17:37:36212 if (!image_size.IsEmpty() && GetHorizontalAlignment() != gfx::ALIGN_RIGHT)
[email protected]70e983cc2012-10-16 21:08:22213 label_origin.set_x(image_origin.x() + image_size.width() + kSpacing);
214
215 image_->SetBoundsRect(gfx::Rect(image_origin, image_size));
216 label_->SetBoundsRect(gfx::Rect(label_origin, label_size));
217}
218
219std::string LabelButton::GetClassName() const {
220 return "views/LabelButton";
221}
222
223void LabelButton::ChildPreferredSizeChanged(View* child) {
224 PreferredSizeChanged();
225}
226
[email protected]204b2082012-11-07 17:53:03227void LabelButton::OnNativeThemeChanged(const ui::NativeTheme* theme) {
228 if (native_theme_)
229 ResetColorsFromNativeTheme(false);
230}
231
[email protected]70e983cc2012-10-16 21:08:22232ui::NativeTheme::Part LabelButton::GetThemePart() const {
233 return ui::NativeTheme::kPushButton;
234}
235
236gfx::Rect LabelButton::GetThemePaintRect() const {
237 return GetLocalBounds();
238}
239
240ui::NativeTheme::State LabelButton::GetThemeState(
241 ui::NativeTheme::ExtraParams* params) const {
242 GetExtraParams(params);
243 switch(state()) {
244 case BS_NORMAL: return ui::NativeTheme::kNormal;
245 case BS_HOT: return ui::NativeTheme::kHovered;
246 case BS_PUSHED: return ui::NativeTheme::kPressed;
247 case BS_DISABLED: return ui::NativeTheme::kDisabled;
248 case BS_COUNT: NOTREACHED() << "Unknown state: " << state();
249 }
250 return ui::NativeTheme::kNormal;
251}
252
253const ui::Animation* LabelButton::GetThemeAnimation() const {
[email protected]204b2082012-11-07 17:53:03254#if defined(OS_WIN)
255 if (native_theme_ && GetNativeTheme() == ui::NativeThemeWin::instance()) {
256 return ui::NativeThemeWin::instance()->IsThemingActive() ?
257 hover_animation_.get() : NULL;
258 }
[email protected]70e983cc2012-10-16 21:08:22259#endif
260 return hover_animation_.get();
261}
262
263ui::NativeTheme::State LabelButton::GetBackgroundThemeState(
264 ui::NativeTheme::ExtraParams* params) const {
265 GetExtraParams(params);
266 return ui::NativeTheme::kNormal;
267}
268
269ui::NativeTheme::State LabelButton::GetForegroundThemeState(
270 ui::NativeTheme::ExtraParams* params) const {
271 GetExtraParams(params);
272 return ui::NativeTheme::kHovered;
273}
274
275void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const {
276 params->button.checked = false;
277 params->button.indeterminate = false;
278 params->button.is_default = default_button();
279 params->button.is_focused = HasFocus() && IsAccessibilityFocusable();
280 params->button.has_border = native_theme();
281 params->button.classic_state = 0;
[email protected]204b2082012-11-07 17:53:03282 params->button.background_color = GetNativeTheme()->GetSystemColor(
[email protected]70e983cc2012-10-16 21:08:22283 ui::NativeTheme::kColorId_TextButtonBackgroundColor);
284}
285
286} // namespace views