[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 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 |
| 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] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 10 | #include "ui/base/native_theme/native_theme.h" |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 11 | #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] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | // The spacing between the icon and text. |
[email protected] | c5de3366 | 2012-10-27 22:37:30 | [diff] [blame] | 23 | const int kSpacing = 5; |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 24 | |
| 25 | // The length of the hover fade animation. |
[email protected] | c5de3366 | 2012-10-27 22:37:30 | [diff] [blame] | 26 | const int kHoverAnimationDurationMs = 170; |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 27 | |
| 28 | } // namespace |
| 29 | |
[email protected] | c5de3366 | 2012-10-27 22:37:30 | [diff] [blame] | 30 | namespace views { |
| 31 | |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 32 | LabelButton::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] | c5de3366 | 2012-10-27 22:37:30 | [diff] [blame] | 38 | set_border(new LabelButtonBorder()); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 39 | // 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] | cc563f0 | 2012-11-07 17:37:36 | [diff] [blame] | 48 | label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 49 | |
| 50 | // Initialize the colors, border, and layout for a Views-themed button. |
| 51 | SetNativeTheme(false); |
| 52 | } |
| 53 | |
| 54 | LabelButton::~LabelButton() {} |
| 55 | |
| 56 | const 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 | |
| 62 | void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) { |
| 63 | button_state_images_[for_state] = image; |
| 64 | image_->SetImage(GetImage(state())); |
| 65 | } |
| 66 | |
| 67 | const string16& LabelButton::GetText() const { |
| 68 | return label_->text(); |
| 69 | } |
| 70 | |
| 71 | void LabelButton::SetText(const string16& text) { |
| 72 | label_->SetText(text); |
| 73 | } |
| 74 | |
| 75 | void 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] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 81 | explicitly_set_colors_[for_state] = true; |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | bool LabelButton::GetTextMultiLine() const { |
| 85 | return label_->is_multi_line(); |
| 86 | } |
| 87 | |
| 88 | void LabelButton::SetTextMultiLine(bool text_multi_line) { |
| 89 | label_->SetMultiLine(text_multi_line); |
| 90 | } |
| 91 | |
[email protected] | cfc888a | 2012-11-05 18:20:50 | [diff] [blame] | 92 | const gfx::Font& LabelButton::GetFont() const { |
| 93 | return label_->font(); |
| 94 | } |
| 95 | |
| 96 | void LabelButton::SetFont(const gfx::Font& font) { |
| 97 | label_->SetFont(font); |
| 98 | } |
| 99 | |
[email protected] | cc563f0 | 2012-11-07 17:37:36 | [diff] [blame] | 100 | gfx::HorizontalAlignment LabelButton::GetHorizontalAlignment() const { |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 101 | return label_->horizontal_alignment(); |
| 102 | } |
| 103 | |
[email protected] | cc563f0 | 2012-11-07 17:37:36 | [diff] [blame] | 104 | void LabelButton::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) { |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 105 | label_->SetHorizontalAlignment(alignment); |
| 106 | InvalidateLayout(); |
| 107 | } |
| 108 | |
| 109 | void 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 | |
| 117 | void LabelButton::SetNativeTheme(bool native_theme) { |
| 118 | native_theme_ = native_theme; |
| 119 | static_cast<LabelButtonBorder*>(border())->set_native_theme(native_theme); |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 120 | // Invalidate the layout to pickup the new insets from the border. |
| 121 | InvalidateLayout(); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 122 | |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 123 | ResetColorsFromNativeTheme(true); |
| 124 | } |
| 125 | |
| 126 | void LabelButton::ResetColorsFromNativeTheme(bool reset_all) { |
| 127 | const ui::NativeTheme* theme = GetNativeTheme(); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 128 | 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] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 135 | if (native_theme_ && theme == ui::NativeThemeWin::instance()) { |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 136 | 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] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 142 | 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] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void 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 | |
| 160 | gfx::Size LabelButton::GetPreferredSize() { |
| 161 | gfx::Size size(label_->GetPreferredSize()); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 162 | 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 | |
| 181 | void 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] | 1c4f28f | 2012-10-18 23:36:06 | [diff] [blame] | 189 | // 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] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 192 | 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] | cc563f0 | 2012-11-07 17:37:36 | [diff] [blame] | 195 | if (GetHorizontalAlignment() == gfx::ALIGN_CENTER) { |
[email protected] | 1c4f28f | 2012-10-18 23:36:06 | [diff] [blame] | 196 | label_size.set_width(std::min(label_size.width(), |
| 197 | label_->GetPreferredSize().width())); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | gfx::Point image_origin(child_area.origin()); |
| 202 | image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); |
[email protected] | cc563f0 | 2012-11-07 17:37:36 | [diff] [blame] | 203 | if (GetHorizontalAlignment() == gfx::ALIGN_CENTER) { |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 204 | 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] | cc563f0 | 2012-11-07 17:37:36 | [diff] [blame] | 207 | } else if (GetHorizontalAlignment() == gfx::ALIGN_RIGHT) { |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 208 | image_origin.Offset(child_area.width() - image_size.width(), 0); |
| 209 | } |
| 210 | |
| 211 | gfx::Point label_origin(child_area.origin()); |
[email protected] | cc563f0 | 2012-11-07 17:37:36 | [diff] [blame] | 212 | if (!image_size.IsEmpty() && GetHorizontalAlignment() != gfx::ALIGN_RIGHT) |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 213 | 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 | |
| 219 | std::string LabelButton::GetClassName() const { |
| 220 | return "views/LabelButton"; |
| 221 | } |
| 222 | |
| 223 | void LabelButton::ChildPreferredSizeChanged(View* child) { |
| 224 | PreferredSizeChanged(); |
| 225 | } |
| 226 | |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 227 | void LabelButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 228 | if (native_theme_) |
| 229 | ResetColorsFromNativeTheme(false); |
| 230 | } |
| 231 | |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 232 | ui::NativeTheme::Part LabelButton::GetThemePart() const { |
| 233 | return ui::NativeTheme::kPushButton; |
| 234 | } |
| 235 | |
| 236 | gfx::Rect LabelButton::GetThemePaintRect() const { |
| 237 | return GetLocalBounds(); |
| 238 | } |
| 239 | |
| 240 | ui::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 | |
| 253 | const ui::Animation* LabelButton::GetThemeAnimation() const { |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 254 | #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] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 259 | #endif |
| 260 | return hover_animation_.get(); |
| 261 | } |
| 262 | |
| 263 | ui::NativeTheme::State LabelButton::GetBackgroundThemeState( |
| 264 | ui::NativeTheme::ExtraParams* params) const { |
| 265 | GetExtraParams(params); |
| 266 | return ui::NativeTheme::kNormal; |
| 267 | } |
| 268 | |
| 269 | ui::NativeTheme::State LabelButton::GetForegroundThemeState( |
| 270 | ui::NativeTheme::ExtraParams* params) const { |
| 271 | GetExtraParams(params); |
| 272 | return ui::NativeTheme::kHovered; |
| 273 | } |
| 274 | |
| 275 | void 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] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 282 | params->button.background_color = GetNativeTheme()->GetSystemColor( |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 283 | ui::NativeTheme::kColorId_TextButtonBackgroundColor); |
| 284 | } |
| 285 | |
| 286 | } // namespace views |