blob: 887fbe9fc7eeafe122ef5d4f0815d8958eb424df [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"
11#include "ui/views/controls/button/label_button_border.h"
12#include "ui/views/focus_border.h"
13
14#if defined(OS_WIN)
15#include "ui/gfx/color_utils.h"
16#include "ui/base/native_theme/native_theme_win.h"
17#endif
18
[email protected]70e983cc2012-10-16 21:08:2219namespace {
20
21// The spacing between the icon and text.
[email protected]c5de33662012-10-27 22:37:3022const int kSpacing = 5;
[email protected]70e983cc2012-10-16 21:08:2223
24// The length of the hover fade animation.
[email protected]c5de33662012-10-27 22:37:3025const int kHoverAnimationDurationMs = 170;
[email protected]70e983cc2012-10-16 21:08:2226
27} // namespace
28
[email protected]c5de33662012-10-27 22:37:3029namespace views {
30
[email protected]70e983cc2012-10-16 21:08:2231LabelButton::LabelButton(ButtonListener* listener, const string16& text)
32 : CustomButton(listener),
33 image_(new ImageView()),
34 label_(new Label(text)),
35 default_button_(false),
36 native_theme_(false) {
[email protected]c5de33662012-10-27 22:37:3037 set_border(new LabelButtonBorder());
[email protected]70e983cc2012-10-16 21:08:2238 // Inset the button focus rect from the actual border; roughly match Windows.
39 set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3));
40 SetAnimationDuration(kHoverAnimationDurationMs);
41
42 AddChildView(image_);
43 image_->set_interactive(false);
44
45 AddChildView(label_);
46 label_->SetAutoColorReadabilityEnabled(false);
47 label_->SetHorizontalAlignment(Label::ALIGN_LEFT);
[email protected]70e983cc2012-10-16 21:08:2248
49 // Initialize the colors, border, and layout for a Views-themed button.
50 SetNativeTheme(false);
51}
52
53LabelButton::~LabelButton() {}
54
55const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) {
56 if (for_state != BS_NORMAL && button_state_images_[for_state].isNull())
57 return button_state_images_[BS_NORMAL];
58 return button_state_images_[for_state];
59}
60
61void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) {
62 button_state_images_[for_state] = image;
63 image_->SetImage(GetImage(state()));
64}
65
66const string16& LabelButton::GetText() const {
67 return label_->text();
68}
69
70void LabelButton::SetText(const string16& text) {
71 label_->SetText(text);
72}
73
74void LabelButton::SetTextColor(ButtonState for_state, SkColor color) {
75 button_state_colors_[for_state] = color;
76 if (for_state == BS_DISABLED)
77 label_->SetDisabledColor(color);
78 else if (for_state == state())
79 label_->SetEnabledColor(color);
80}
81
82bool LabelButton::GetTextMultiLine() const {
83 return label_->is_multi_line();
84}
85
86void LabelButton::SetTextMultiLine(bool text_multi_line) {
87 label_->SetMultiLine(text_multi_line);
88}
89
[email protected]cfc888a2012-11-05 18:20:5090const gfx::Font& LabelButton::GetFont() const {
91 return label_->font();
92}
93
94void LabelButton::SetFont(const gfx::Font& font) {
95 label_->SetFont(font);
96}
97
[email protected]70e983cc2012-10-16 21:08:2298Label::Alignment LabelButton::GetHorizontalAlignment() const {
99 return label_->horizontal_alignment();
100}
101
102void LabelButton::SetHorizontalAlignment(Label::Alignment alignment) {
103 label_->SetHorizontalAlignment(alignment);
104 InvalidateLayout();
105}
106
107void LabelButton::SetDefaultButton(bool default_button) {
108 if (default_button == default_button_)
109 return;
110 default_button_ = default_button;
111 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE);
112 default_button_ ? AddAccelerator(accel) : RemoveAccelerator(accel);
113}
114
115void LabelButton::SetNativeTheme(bool native_theme) {
116 native_theme_ = native_theme;
117 static_cast<LabelButtonBorder*>(border())->set_native_theme(native_theme);
118
119 const ui::NativeTheme* theme = ui::NativeTheme::instance();
120 SkColor colors[BS_COUNT] = {
121 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonEnabledColor),
122 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor),
123 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor),
124 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonDisabledColor),
125 };
126#if defined(OS_WIN)
127 if (native_theme) {
128 colors[BS_NORMAL] = color_utils::GetSysSkColor(COLOR_BTNTEXT);
129 colors[BS_HOT] = color_utils::GetSysSkColor(COLOR_BTNTEXT);
130 colors[BS_PUSHED] = color_utils::GetSysSkColor(COLOR_BTNTEXT);
131 colors[BS_DISABLED] = color_utils::GetSysSkColor(COLOR_GRAYTEXT);
132 }
133#endif
134 for (size_t state = BS_NORMAL; state < BS_COUNT; ++state)
135 SetTextColor(static_cast<ButtonState>(state), colors[state]);
136
137 // Invalidate the layout to pickup the new insets from the border.
138 InvalidateLayout();
139}
140
141void LabelButton::StateChanged() {
142 const gfx::Size previous_image_size(image_->GetPreferredSize());
143 image_->SetImage(GetImage(state()));
144 const SkColor color = button_state_colors_[state()];
145 if (state() != BS_DISABLED && label_->enabled_color() != color)
146 label_->SetEnabledColor(color);
147 if (image_->GetPreferredSize() != previous_image_size)
148 Layout();
149}
150
151gfx::Size LabelButton::GetPreferredSize() {
152 gfx::Size size(label_->GetPreferredSize());
[email protected]70e983cc2012-10-16 21:08:22153 const gfx::Size image_size(image_->GetPreferredSize());
154 if (image_size.width() > 0 && size.width() > 0)
155 size.Enlarge(kSpacing, 0);
156 size.set_height(std::max(size.height(), image_size.height()));
157 size.Enlarge(image_size.width() + GetInsets().width(), GetInsets().height());
158
159 // Increase the minimum size monotonically with the preferred size.
160 size.SetSize(std::max(min_size_.width(), size.width()),
161 std::max(min_size_.height(), size.height()));
162 min_size_ = size;
163
164 // Return the largest known size clamped to the maximum size (if valid).
165 if (max_size_.width() > 0)
166 size.set_width(std::min(max_size_.width(), size.width()));
167 if (max_size_.height() > 0)
168 size.set_height(std::min(max_size_.height(), size.height()));
169 return size;
170}
171
172void LabelButton::Layout() {
173 gfx::Rect child_area(GetLocalBounds());
174 child_area.Inset(GetInsets());
175
176 gfx::Size image_size(image_->GetPreferredSize());
177 image_size.set_width(std::min(image_size.width(), child_area.width()));
178 image_size.set_height(std::min(image_size.height(), child_area.height()));
179
[email protected]1c4f28f2012-10-18 23:36:06180 // The label takes any remaining width after sizing the image, unless both
181 // views are centered. In that case, using the tighter preferred label width
182 // avoids wasted space within the label that would look like awkward padding.
[email protected]70e983cc2012-10-16 21:08:22183 gfx::Size label_size(child_area.size());
184 if (!image_size.IsEmpty() && !label_size.IsEmpty()) {
185 label_size.set_width(child_area.width() - image_size.width() - kSpacing);
186 if (GetHorizontalAlignment() == Label::ALIGN_CENTER) {
[email protected]1c4f28f2012-10-18 23:36:06187 label_size.set_width(std::min(label_size.width(),
188 label_->GetPreferredSize().width()));
[email protected]70e983cc2012-10-16 21:08:22189 }
190 }
191
192 gfx::Point image_origin(child_area.origin());
193 image_origin.Offset(0, (child_area.height() - image_size.height()) / 2);
194 if (GetHorizontalAlignment() == Label::ALIGN_CENTER) {
195 const int total_width = image_size.width() + label_size.width() +
196 ((image_size.width() > 0 && label_size.width() > 0) ? kSpacing : 0);
197 image_origin.Offset((child_area.width() - total_width) / 2, 0);
198 } else if (GetHorizontalAlignment() == Label::ALIGN_RIGHT) {
199 image_origin.Offset(child_area.width() - image_size.width(), 0);
200 }
201
202 gfx::Point label_origin(child_area.origin());
203 if (!image_size.IsEmpty() && GetHorizontalAlignment() != Label::ALIGN_RIGHT)
204 label_origin.set_x(image_origin.x() + image_size.width() + kSpacing);
205
206 image_->SetBoundsRect(gfx::Rect(image_origin, image_size));
207 label_->SetBoundsRect(gfx::Rect(label_origin, label_size));
208}
209
210std::string LabelButton::GetClassName() const {
211 return "views/LabelButton";
212}
213
214void LabelButton::ChildPreferredSizeChanged(View* child) {
215 PreferredSizeChanged();
216}
217
218ui::NativeTheme::Part LabelButton::GetThemePart() const {
219 return ui::NativeTheme::kPushButton;
220}
221
222gfx::Rect LabelButton::GetThemePaintRect() const {
223 return GetLocalBounds();
224}
225
226ui::NativeTheme::State LabelButton::GetThemeState(
227 ui::NativeTheme::ExtraParams* params) const {
228 GetExtraParams(params);
229 switch(state()) {
230 case BS_NORMAL: return ui::NativeTheme::kNormal;
231 case BS_HOT: return ui::NativeTheme::kHovered;
232 case BS_PUSHED: return ui::NativeTheme::kPressed;
233 case BS_DISABLED: return ui::NativeTheme::kDisabled;
234 case BS_COUNT: NOTREACHED() << "Unknown state: " << state();
235 }
236 return ui::NativeTheme::kNormal;
237}
238
239const ui::Animation* LabelButton::GetThemeAnimation() const {
240#if defined(OS_WIN) && !defined(USE_AURA)
[email protected]e43c9662012-10-22 18:56:27241 if (native_theme() && !ui::NativeThemeWin::instance()->IsThemingActive())
[email protected]70e983cc2012-10-16 21:08:22242 return NULL;
243#endif
244 return hover_animation_.get();
245}
246
247ui::NativeTheme::State LabelButton::GetBackgroundThemeState(
248 ui::NativeTheme::ExtraParams* params) const {
249 GetExtraParams(params);
250 return ui::NativeTheme::kNormal;
251}
252
253ui::NativeTheme::State LabelButton::GetForegroundThemeState(
254 ui::NativeTheme::ExtraParams* params) const {
255 GetExtraParams(params);
256 return ui::NativeTheme::kHovered;
257}
258
259void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const {
260 params->button.checked = false;
261 params->button.indeterminate = false;
262 params->button.is_default = default_button();
263 params->button.is_focused = HasFocus() && IsAccessibilityFocusable();
264 params->button.has_border = native_theme();
265 params->button.classic_state = 0;
266 params->button.background_color = ui::NativeTheme::instance()->GetSystemColor(
267 ui::NativeTheme::kColorId_TextButtonBackgroundColor);
268}
269
270} // namespace views