[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" |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 9 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | ffb15d1 | 2013-09-15 17:29:30 | [diff] [blame] | 10 | #include "ui/gfx/animation/throb_animation.h" |
[email protected] | 750f449 | 2013-12-25 18:17:09 | [diff] [blame^] | 11 | #include "ui/gfx/font_list.h" |
[email protected] | 8858a83 | 2013-06-19 14:46:30 | [diff] [blame] | 12 | #include "ui/gfx/sys_color_change_listener.h" |
[email protected] | 990e622 | 2012-11-16 13:31:18 | [diff] [blame] | 13 | #include "ui/native_theme/native_theme.h" |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 14 | #include "ui/views/controls/button/label_button_border.h" |
[email protected] | 1966d6d1 | 2013-12-05 04:19:35 | [diff] [blame] | 15 | #include "ui/views/painter.h" |
[email protected] | 6d11493 | 2013-04-24 02:38:20 | [diff] [blame] | 16 | #include "ui/views/window/dialog_delegate.h" |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 17 | |
| 18 | #if defined(OS_WIN) |
[email protected] | 990e622 | 2012-11-16 13:31:18 | [diff] [blame] | 19 | #include "ui/native_theme/native_theme_win.h" |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 20 | #endif |
| 21 | |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | // The spacing between the icon and text. |
[email protected] | c5de3366 | 2012-10-27 22:37:30 | [diff] [blame] | 25 | const int kSpacing = 5; |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 26 | |
[email protected] | 8858a83 | 2013-06-19 14:46:30 | [diff] [blame] | 27 | // Default text and shadow colors for STYLE_BUTTON. |
| 28 | const SkColor kStyleButtonTextColor = SK_ColorBLACK; |
| 29 | const SkColor kStyleButtonShadowColor = SK_ColorWHITE; |
| 30 | |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 31 | } // namespace |
| 32 | |
[email protected] | c5de3366 | 2012-10-27 22:37:30 | [diff] [blame] | 33 | namespace views { |
| 34 | |
[email protected] | fe22ba1 | 2013-02-21 13:43:48 | [diff] [blame] | 35 | // static |
[email protected] | c77910fa | 2013-12-11 09:03:30 | [diff] [blame] | 36 | const int LabelButton::kHoverAnimationDurationMs = 170; |
| 37 | |
| 38 | // static |
[email protected] | f4167cf | 2013-05-26 23:36:15 | [diff] [blame] | 39 | const char LabelButton::kViewClassName[] = "LabelButton"; |
[email protected] | fe22ba1 | 2013-02-21 13:43:48 | [diff] [blame] | 40 | |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 41 | LabelButton::LabelButton(ButtonListener* listener, const base::string16& text) |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 42 | : CustomButton(listener), |
| 43 | image_(new ImageView()), |
[email protected] | 3e4efae | 2013-04-05 13:17:36 | [diff] [blame] | 44 | label_(new Label()), |
[email protected] | 379bc98 | 2012-12-19 19:56:53 | [diff] [blame] | 45 | button_state_images_(), |
| 46 | button_state_colors_(), |
| 47 | explicitly_set_colors_(), |
[email protected] | e59fc52a | 2013-02-20 20:24:11 | [diff] [blame] | 48 | is_default_(false), |
[email protected] | fe22ba1 | 2013-02-21 13:43:48 | [diff] [blame] | 49 | style_(STYLE_TEXTBUTTON) { |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 50 | SetAnimationDuration(kHoverAnimationDurationMs); |
[email protected] | 3e4efae | 2013-04-05 13:17:36 | [diff] [blame] | 51 | SetText(text); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 52 | |
| 53 | AddChildView(image_); |
| 54 | image_->set_interactive(false); |
| 55 | |
| 56 | AddChildView(label_); |
| 57 | label_->SetAutoColorReadabilityEnabled(false); |
[email protected] | cc563f0 | 2012-11-07 17:37:36 | [diff] [blame] | 58 | label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 59 | |
[email protected] | fe22ba1 | 2013-02-21 13:43:48 | [diff] [blame] | 60 | // Initialize the colors, border, and layout. |
| 61 | SetStyle(style_); |
[email protected] | cd70b3e | 2013-04-03 20:01:37 | [diff] [blame] | 62 | |
| 63 | SetAccessibleName(text); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | LabelButton::~LabelButton() {} |
| 67 | |
| 68 | const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { |
[email protected] | fca8dc0 | 2012-11-14 16:25:56 | [diff] [blame] | 69 | if (for_state != STATE_NORMAL && button_state_images_[for_state].isNull()) |
| 70 | return button_state_images_[STATE_NORMAL]; |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 71 | return button_state_images_[for_state]; |
| 72 | } |
| 73 | |
| 74 | void LabelButton::SetImage(ButtonState for_state, const gfx::ImageSkia& image) { |
| 75 | button_state_images_[for_state] = image; |
[email protected] | 987b766 | 2013-05-23 10:45:49 | [diff] [blame] | 76 | UpdateImage(); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 79 | const base::string16& LabelButton::GetText() const { |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 80 | return label_->text(); |
| 81 | } |
| 82 | |
[email protected] | 2aadf21 | 2013-12-18 20:03:44 | [diff] [blame] | 83 | void LabelButton::SetText(const base::string16& text) { |
[email protected] | 3e4efae | 2013-04-05 13:17:36 | [diff] [blame] | 84 | SetAccessibleName(text); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 85 | label_->SetText(text); |
| 86 | } |
| 87 | |
| 88 | void LabelButton::SetTextColor(ButtonState for_state, SkColor color) { |
| 89 | button_state_colors_[for_state] = color; |
[email protected] | fca8dc0 | 2012-11-14 16:25:56 | [diff] [blame] | 90 | if (for_state == STATE_DISABLED) |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 91 | label_->SetDisabledColor(color); |
| 92 | else if (for_state == state()) |
| 93 | label_->SetEnabledColor(color); |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 94 | explicitly_set_colors_[for_state] = true; |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | bool LabelButton::GetTextMultiLine() const { |
| 98 | return label_->is_multi_line(); |
| 99 | } |
| 100 | |
| 101 | void LabelButton::SetTextMultiLine(bool text_multi_line) { |
| 102 | label_->SetMultiLine(text_multi_line); |
| 103 | } |
| 104 | |
[email protected] | 750f449 | 2013-12-25 18:17:09 | [diff] [blame^] | 105 | const gfx::FontList& LabelButton::GetFontList() const { |
| 106 | return label_->font_list(); |
| 107 | } |
| 108 | |
| 109 | void LabelButton::SetFontList(const gfx::FontList& font_list) { |
| 110 | label_->SetFontList(font_list); |
| 111 | } |
| 112 | |
[email protected] | cfc888a | 2012-11-05 18:20:50 | [diff] [blame] | 113 | const gfx::Font& LabelButton::GetFont() const { |
| 114 | return label_->font(); |
| 115 | } |
| 116 | |
| 117 | void LabelButton::SetFont(const gfx::Font& font) { |
| 118 | label_->SetFont(font); |
| 119 | } |
| 120 | |
[email protected] | 252cec3 | 2013-09-24 03:17:52 | [diff] [blame] | 121 | void LabelButton::SetElideBehavior(Label::ElideBehavior elide_behavior) { |
| 122 | label_->SetElideBehavior(elide_behavior); |
| 123 | } |
| 124 | |
[email protected] | cc563f0 | 2012-11-07 17:37:36 | [diff] [blame] | 125 | gfx::HorizontalAlignment LabelButton::GetHorizontalAlignment() const { |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 126 | return label_->horizontal_alignment(); |
| 127 | } |
| 128 | |
[email protected] | cc563f0 | 2012-11-07 17:37:36 | [diff] [blame] | 129 | void LabelButton::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) { |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 130 | label_->SetHorizontalAlignment(alignment); |
| 131 | InvalidateLayout(); |
| 132 | } |
| 133 | |
[email protected] | e59fc52a | 2013-02-20 20:24:11 | [diff] [blame] | 134 | void LabelButton::SetIsDefault(bool is_default) { |
| 135 | if (is_default == is_default_) |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 136 | return; |
[email protected] | e59fc52a | 2013-02-20 20:24:11 | [diff] [blame] | 137 | is_default_ = is_default; |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 138 | ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE); |
[email protected] | e59fc52a | 2013-02-20 20:24:11 | [diff] [blame] | 139 | is_default_ ? AddAccelerator(accel) : RemoveAccelerator(accel); |
[email protected] | 640bff1 | 2013-04-17 21:08:14 | [diff] [blame] | 140 | |
| 141 | // STYLE_BUTTON uses bold text to indicate default buttons. |
| 142 | if (style_ == STYLE_BUTTON) { |
[email protected] | 750f449 | 2013-12-25 18:17:09 | [diff] [blame^] | 143 | int style = label_->font_list().GetFontStyle(); |
[email protected] | 72dd2a5 | 2013-08-13 01:15:15 | [diff] [blame] | 144 | style = is_default ? style | gfx::Font::BOLD : style & ~gfx::Font::BOLD; |
[email protected] | 750f449 | 2013-12-25 18:17:09 | [diff] [blame^] | 145 | label_->SetFontList( |
| 146 | label_->font_list().DeriveFontListWithSizeDeltaAndStyle(0, style)); |
[email protected] | 640bff1 | 2013-04-17 21:08:14 | [diff] [blame] | 147 | } |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | fe22ba1 | 2013-02-21 13:43:48 | [diff] [blame] | 150 | void LabelButton::SetStyle(ButtonStyle style) { |
[email protected] | 640bff1 | 2013-04-17 21:08:14 | [diff] [blame] | 151 | // Use the new button style instead of the native button style. |
| 152 | // TODO(msw): Officialy deprecate and remove STYLE_NATIVE_TEXTBUTTON. |
[email protected] | b1554e4 | 2013-11-18 20:43:09 | [diff] [blame] | 153 | if (style == STYLE_NATIVE_TEXTBUTTON) |
[email protected] | 640bff1 | 2013-04-17 21:08:14 | [diff] [blame] | 154 | style = STYLE_BUTTON; |
| 155 | |
[email protected] | fe22ba1 | 2013-02-21 13:43:48 | [diff] [blame] | 156 | style_ = style; |
| 157 | set_border(new LabelButtonBorder(style)); |
| 158 | // Inset the button focus rect from the actual border; roughly match Windows. |
[email protected] | 1966d6d1 | 2013-12-05 04:19:35 | [diff] [blame] | 159 | if (style == STYLE_BUTTON) { |
| 160 | SetFocusPainter(scoped_ptr<Painter>()); |
| 161 | } else { |
| 162 | SetFocusPainter(Painter::CreateDashedFocusPainterWithInsets( |
| 163 | gfx::Insets(3, 3, 3, 3))); |
| 164 | } |
[email protected] | 640bff1 | 2013-04-17 21:08:14 | [diff] [blame] | 165 | if (style == STYLE_BUTTON || style == STYLE_NATIVE_TEXTBUTTON) { |
[email protected] | d0abd28 | 2013-02-28 23:59:08 | [diff] [blame] | 166 | label_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
[email protected] | 505df7b | 2013-12-16 14:07:19 | [diff] [blame] | 167 | SetFocusable(true); |
[email protected] | 640bff1 | 2013-04-17 21:08:14 | [diff] [blame] | 168 | } |
[email protected] | 8858a83 | 2013-06-19 14:46:30 | [diff] [blame] | 169 | if (style == STYLE_BUTTON) |
[email protected] | 3101272 | 2013-06-12 22:00:10 | [diff] [blame] | 170 | set_min_size(gfx::Size(70, 33)); |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 171 | // Invalidate the layout to pickup the new insets from the border. |
| 172 | InvalidateLayout(); |
[email protected] | 379bc98 | 2012-12-19 19:56:53 | [diff] [blame] | 173 | ResetColorsFromNativeTheme(); |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | 1966d6d1 | 2013-12-05 04:19:35 | [diff] [blame] | 176 | void LabelButton::SetFocusPainter(scoped_ptr<Painter> focus_painter) { |
| 177 | focus_painter_ = focus_painter.Pass(); |
| 178 | } |
| 179 | |
[email protected] | 7f39bb69 | 2013-01-07 23:08:16 | [diff] [blame] | 180 | gfx::Size LabelButton::GetPreferredSize() { |
[email protected] | 9baef62 | 2013-05-14 05:30:46 | [diff] [blame] | 181 | // Use a temporary label copy for sizing to avoid calculation side-effects. |
[email protected] | 750f449 | 2013-12-25 18:17:09 | [diff] [blame^] | 182 | const gfx::FontList& font_list = GetFontList(); |
| 183 | Label label(GetText(), font_list); |
[email protected] | 9baef62 | 2013-05-14 05:30:46 | [diff] [blame] | 184 | label.SetMultiLine(GetTextMultiLine()); |
[email protected] | 640bff1 | 2013-04-17 21:08:14 | [diff] [blame] | 185 | |
[email protected] | 5b7355b | 2013-07-17 04:44:54 | [diff] [blame] | 186 | if (style() == STYLE_BUTTON) { |
| 187 | // Some text appears wider when rendered normally than when rendered bold. |
| 188 | // Accommodate the widest, as buttons may show bold and shouldn't resize. |
| 189 | const int current_width = label.GetPreferredSize().width(); |
[email protected] | 750f449 | 2013-12-25 18:17:09 | [diff] [blame^] | 190 | label.SetFontList(font_list.DeriveFontListWithSizeDeltaAndStyle( |
| 191 | 0, font_list.GetFontStyle() ^ gfx::Font::BOLD)); |
[email protected] | 5b7355b | 2013-07-17 04:44:54 | [diff] [blame] | 192 | if (label.GetPreferredSize().width() < current_width) |
[email protected] | 750f449 | 2013-12-25 18:17:09 | [diff] [blame^] | 193 | label.SetFontList(font_list); |
[email protected] | 5b7355b | 2013-07-17 04:44:54 | [diff] [blame] | 194 | } |
| 195 | |
[email protected] | 9baef62 | 2013-05-14 05:30:46 | [diff] [blame] | 196 | // Resize multi-line labels given the current limited available width. |
[email protected] | 7f39bb69 | 2013-01-07 23:08:16 | [diff] [blame] | 197 | const gfx::Size image_size(image_->GetPreferredSize()); |
[email protected] | 9baef62 | 2013-05-14 05:30:46 | [diff] [blame] | 198 | const int image_width = image_size.width(); |
| 199 | if (GetTextMultiLine() && (width() > image_width + kSpacing)) |
| 200 | label.SizeToFit(width() - image_width - (image_width > 0 ? kSpacing : 0)); |
[email protected] | 7f39bb69 | 2013-01-07 23:08:16 | [diff] [blame] | 201 | |
| 202 | // Calculate the required size. |
[email protected] | 9baef62 | 2013-05-14 05:30:46 | [diff] [blame] | 203 | gfx::Size size(label.GetPreferredSize()); |
| 204 | if (image_width > 0 && size.width() > 0) |
[email protected] | 7f39bb69 | 2013-01-07 23:08:16 | [diff] [blame] | 205 | size.Enlarge(kSpacing, 0); |
[email protected] | a4a08d0 | 2013-05-30 00:18:00 | [diff] [blame] | 206 | size.SetToMax(gfx::Size(0, image_size.height())); |
[email protected] | 9baef62 | 2013-05-14 05:30:46 | [diff] [blame] | 207 | const gfx::Insets insets(GetInsets()); |
| 208 | size.Enlarge(image_size.width() + insets.width(), insets.height()); |
[email protected] | 640bff1 | 2013-04-17 21:08:14 | [diff] [blame] | 209 | |
[email protected] | 7ad32c1 | 2013-11-23 01:39:40 | [diff] [blame] | 210 | // Make the size at least as large as the minimum size needed by the border. |
| 211 | size.SetToMax(border()->GetMinimumSize()); |
| 212 | |
[email protected] | 7f39bb69 | 2013-01-07 23:08:16 | [diff] [blame] | 213 | // Increase the minimum size monotonically with the preferred size. |
[email protected] | a4a08d0 | 2013-05-30 00:18:00 | [diff] [blame] | 214 | size.SetToMax(min_size_); |
[email protected] | 7f39bb69 | 2013-01-07 23:08:16 | [diff] [blame] | 215 | min_size_ = size; |
| 216 | |
| 217 | // Return the largest known size clamped to the maximum size (if valid). |
| 218 | if (max_size_.width() > 0) |
| 219 | size.set_width(std::min(max_size_.width(), size.width())); |
| 220 | if (max_size_.height() > 0) |
| 221 | size.set_height(std::min(max_size_.height(), size.height())); |
| 222 | return size; |
| 223 | } |
| 224 | |
[email protected] | 987b766 | 2013-05-23 10:45:49 | [diff] [blame] | 225 | void LabelButton::Layout() { |
| 226 | gfx::HorizontalAlignment adjusted_alignment = GetHorizontalAlignment(); |
| 227 | if (base::i18n::IsRTL() && adjusted_alignment != gfx::ALIGN_CENTER) |
| 228 | adjusted_alignment = (adjusted_alignment == gfx::ALIGN_LEFT) ? |
| 229 | gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT; |
| 230 | |
| 231 | gfx::Rect child_area(GetLocalBounds()); |
| 232 | child_area.Inset(GetInsets()); |
| 233 | |
| 234 | gfx::Size image_size(image_->GetPreferredSize()); |
| 235 | image_size.set_width(std::min(image_size.width(), child_area.width())); |
| 236 | image_size.set_height(std::min(image_size.height(), child_area.height())); |
| 237 | |
| 238 | // The label takes any remaining width after sizing the image, unless both |
| 239 | // views are centered. In that case, using the tighter preferred label width |
| 240 | // avoids wasted space within the label that would look like awkward padding. |
| 241 | gfx::Size label_size(child_area.size()); |
| 242 | if (!image_size.IsEmpty() && !label_size.IsEmpty()) { |
| 243 | label_size.set_width( |
| 244 | std::max(child_area.width() - image_size.width() - kSpacing, 0)); |
| 245 | if (adjusted_alignment == gfx::ALIGN_CENTER) { |
| 246 | // Ensure multi-line labels paired with images use their available width. |
| 247 | if (GetTextMultiLine()) |
| 248 | label_->SizeToFit(label_size.width()); |
| 249 | label_size.set_width( |
| 250 | std::min(label_size.width(), label_->GetPreferredSize().width())); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | gfx::Point image_origin(child_area.origin()); |
| 255 | image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); |
| 256 | if (adjusted_alignment == gfx::ALIGN_CENTER) { |
| 257 | const int total_width = image_size.width() + label_size.width() + |
| 258 | ((image_size.width() > 0 && label_size.width() > 0) ? kSpacing : 0); |
| 259 | image_origin.Offset((child_area.width() - total_width) / 2, 0); |
| 260 | } else if (adjusted_alignment == gfx::ALIGN_RIGHT) { |
| 261 | image_origin.Offset(child_area.width() - image_size.width(), 0); |
| 262 | } |
| 263 | |
| 264 | gfx::Point label_origin(child_area.origin()); |
| 265 | if (!image_size.IsEmpty() &&adjusted_alignment != gfx::ALIGN_RIGHT) |
| 266 | label_origin.set_x(image_origin.x() + image_size.width() + kSpacing); |
| 267 | |
| 268 | image_->SetBoundsRect(gfx::Rect(image_origin, image_size)); |
| 269 | label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); |
| 270 | } |
| 271 | |
[email protected] | 707bd37 | 2013-05-09 08:32:27 | [diff] [blame] | 272 | const char* LabelButton::GetClassName() const { |
[email protected] | fe22ba1 | 2013-02-21 13:43:48 | [diff] [blame] | 273 | return kViewClassName; |
| 274 | } |
| 275 | |
[email protected] | 1966d6d1 | 2013-12-05 04:19:35 | [diff] [blame] | 276 | void LabelButton::OnPaint(gfx::Canvas* canvas) { |
| 277 | View::OnPaint(canvas); |
| 278 | Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); |
| 279 | } |
| 280 | |
[email protected] | 73ecc180 | 2013-12-04 07:09:49 | [diff] [blame] | 281 | void LabelButton::OnFocus() { |
| 282 | View::OnFocus(); |
| 283 | // Typically the border renders differently when focused. |
| 284 | SchedulePaint(); |
| 285 | } |
| 286 | |
| 287 | void LabelButton::OnBlur() { |
| 288 | View::OnBlur(); |
| 289 | // Typically the border renders differently when focused. |
| 290 | SchedulePaint(); |
| 291 | } |
| 292 | |
[email protected] | 987b766 | 2013-05-23 10:45:49 | [diff] [blame] | 293 | void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
| 294 | params->button.checked = false; |
| 295 | params->button.indeterminate = false; |
| 296 | params->button.is_default = is_default_; |
| 297 | params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); |
| 298 | params->button.has_border = style() == STYLE_NATIVE_TEXTBUTTON; |
| 299 | params->button.classic_state = 0; |
[email protected] | 8858a83 | 2013-06-19 14:46:30 | [diff] [blame] | 300 | params->button.background_color = label()->background_color(); |
[email protected] | 987b766 | 2013-05-23 10:45:49 | [diff] [blame] | 301 | } |
| 302 | |
[email protected] | 379bc98 | 2012-12-19 19:56:53 | [diff] [blame] | 303 | void LabelButton::ResetColorsFromNativeTheme() { |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 304 | const ui::NativeTheme* theme = GetNativeTheme(); |
[email protected] | fca8dc0 | 2012-11-14 16:25:56 | [diff] [blame] | 305 | SkColor colors[STATE_COUNT] = { |
[email protected] | 1b768f8f | 2013-05-17 01:16:52 | [diff] [blame] | 306 | theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor), |
| 307 | theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor), |
| 308 | theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor), |
| 309 | theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonDisabledColor), |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 310 | }; |
[email protected] | 644f4fb | 2013-03-01 21:14:34 | [diff] [blame] | 311 | |
| 312 | // Certain styles do not change text color when hovered or pressed. |
[email protected] | 8858a83 | 2013-06-19 14:46:30 | [diff] [blame] | 313 | bool constant_text_color = false; |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 314 | #if defined(OS_WIN) |
[email protected] | 644f4fb | 2013-03-01 21:14:34 | [diff] [blame] | 315 | constant_text_color |= (style() == STYLE_NATIVE_TEXTBUTTON && |
| 316 | theme == ui::NativeThemeWin::instance()); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 317 | #endif |
[email protected] | 8858a83 | 2013-06-19 14:46:30 | [diff] [blame] | 318 | |
[email protected] | 8858a83 | 2013-06-19 14:46:30 | [diff] [blame] | 319 | // Use hardcoded colors for inverted color scheme support and STYLE_BUTTON. |
| 320 | if (gfx::IsInvertedColorScheme()) { |
| 321 | constant_text_color = true; |
| 322 | colors[STATE_NORMAL] = SK_ColorWHITE; |
| 323 | label_->SetBackgroundColor(SK_ColorBLACK); |
| 324 | label_->SetAutoColorReadabilityEnabled(true); |
| 325 | label_->ClearEmbellishing(); |
| 326 | } else if (style() == STYLE_BUTTON) { |
| 327 | constant_text_color = true; |
| 328 | colors[STATE_NORMAL] = kStyleButtonTextColor; |
[email protected] | 763f83b | 2013-09-17 18:26:32 | [diff] [blame] | 329 | label_->SetBackgroundColor(theme->GetSystemColor( |
| 330 | ui::NativeTheme::kColorId_ButtonBackgroundColor)); |
[email protected] | 8858a83 | 2013-06-19 14:46:30 | [diff] [blame] | 331 | label_->SetAutoColorReadabilityEnabled(false); |
| 332 | label_->SetShadowColors(kStyleButtonShadowColor, kStyleButtonShadowColor); |
| 333 | label_->SetShadowOffset(0, 1); |
| 334 | } |
| 335 | |
[email protected] | 644f4fb | 2013-03-01 21:14:34 | [diff] [blame] | 336 | if (constant_text_color) |
| 337 | colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL]; |
| 338 | |
[email protected] | fca8dc0 | 2012-11-14 16:25:56 | [diff] [blame] | 339 | for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { |
[email protected] | 379bc98 | 2012-12-19 19:56:53 | [diff] [blame] | 340 | if (!explicitly_set_colors_[state]) { |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 341 | SetTextColor(static_cast<ButtonState>(state), colors[state]); |
| 342 | explicitly_set_colors_[state] = false; |
| 343 | } |
| 344 | } |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 345 | } |
| 346 | |
[email protected] | 763f83b | 2013-09-17 18:26:32 | [diff] [blame] | 347 | void LabelButton::UpdateImage() { |
| 348 | image_->SetImage(GetImage(state())); |
| 349 | } |
| 350 | |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 351 | void LabelButton::StateChanged() { |
| 352 | const gfx::Size previous_image_size(image_->GetPreferredSize()); |
[email protected] | 987b766 | 2013-05-23 10:45:49 | [diff] [blame] | 353 | UpdateImage(); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 354 | const SkColor color = button_state_colors_[state()]; |
[email protected] | fca8dc0 | 2012-11-14 16:25:56 | [diff] [blame] | 355 | if (state() != STATE_DISABLED && label_->enabled_color() != color) |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 356 | label_->SetEnabledColor(color); |
[email protected] | 4e88e35 | 2012-12-04 19:12:09 | [diff] [blame] | 357 | label_->SetEnabled(state() != STATE_DISABLED); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 358 | if (image_->GetPreferredSize() != previous_image_size) |
| 359 | Layout(); |
| 360 | } |
| 361 | |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 362 | void LabelButton::ChildPreferredSizeChanged(View* child) { |
| 363 | PreferredSizeChanged(); |
| 364 | } |
| 365 | |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 366 | void LabelButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
[email protected] | 379bc98 | 2012-12-19 19:56:53 | [diff] [blame] | 367 | ResetColorsFromNativeTheme(); |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 370 | ui::NativeTheme::Part LabelButton::GetThemePart() const { |
| 371 | return ui::NativeTheme::kPushButton; |
| 372 | } |
| 373 | |
| 374 | gfx::Rect LabelButton::GetThemePaintRect() const { |
| 375 | return GetLocalBounds(); |
| 376 | } |
| 377 | |
| 378 | ui::NativeTheme::State LabelButton::GetThemeState( |
| 379 | ui::NativeTheme::ExtraParams* params) const { |
| 380 | GetExtraParams(params); |
[email protected] | 987b766 | 2013-05-23 10:45:49 | [diff] [blame] | 381 | switch (state()) { |
[email protected] | fca8dc0 | 2012-11-14 16:25:56 | [diff] [blame] | 382 | case STATE_NORMAL: return ui::NativeTheme::kNormal; |
| 383 | case STATE_HOVERED: return ui::NativeTheme::kHovered; |
| 384 | case STATE_PRESSED: return ui::NativeTheme::kPressed; |
| 385 | case STATE_DISABLED: return ui::NativeTheme::kDisabled; |
| 386 | case STATE_COUNT: NOTREACHED() << "Unknown state: " << state(); |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 387 | } |
| 388 | return ui::NativeTheme::kNormal; |
| 389 | } |
| 390 | |
[email protected] | ffb15d1 | 2013-09-15 17:29:30 | [diff] [blame] | 391 | const gfx::Animation* LabelButton::GetThemeAnimation() const { |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 392 | #if defined(OS_WIN) |
[email protected] | fe22ba1 | 2013-02-21 13:43:48 | [diff] [blame] | 393 | if (style() == STYLE_NATIVE_TEXTBUTTON && |
| 394 | GetNativeTheme() == ui::NativeThemeWin::instance()) { |
[email protected] | 204b208 | 2012-11-07 17:53:03 | [diff] [blame] | 395 | return ui::NativeThemeWin::instance()->IsThemingActive() ? |
| 396 | hover_animation_.get() : NULL; |
| 397 | } |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 398 | #endif |
| 399 | return hover_animation_.get(); |
| 400 | } |
| 401 | |
| 402 | ui::NativeTheme::State LabelButton::GetBackgroundThemeState( |
| 403 | ui::NativeTheme::ExtraParams* params) const { |
| 404 | GetExtraParams(params); |
| 405 | return ui::NativeTheme::kNormal; |
| 406 | } |
| 407 | |
| 408 | ui::NativeTheme::State LabelButton::GetForegroundThemeState( |
| 409 | ui::NativeTheme::ExtraParams* params) const { |
| 410 | GetExtraParams(params); |
| 411 | return ui::NativeTheme::kHovered; |
| 412 | } |
| 413 | |
[email protected] | 70e983cc | 2012-10-16 21:08:22 | [diff] [blame] | 414 | } // namespace views |