| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/controls/button/label_button.h" | 5 #include "ui/views/controls/button/label_button.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "grit/ui_resources.h" | 8 #include "grit/ui_resources.h" |
| 9 #include "ui/base/animation/throb_animation.h" | 9 #include "ui/base/animation/throb_animation.h" |
| 10 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // The spacing between the icon and text. | 22 // The spacing between the icon and text. |
| 23 const int kSpacing = 5; | 23 const int kSpacing = 5; |
| 24 | 24 |
| 25 // The length of the hover fade animation. | 25 // The length of the hover fade animation. |
| 26 const int kHoverAnimationDurationMs = 170; | 26 const int kHoverAnimationDurationMs = 170; |
| 27 | 27 |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 namespace views { | 30 namespace views { |
| 31 | 31 |
| 32 // static |
| 33 const char LabelButton::kViewClassName[] = "views/LabelButton"; |
| 34 |
| 32 LabelButton::LabelButton(ButtonListener* listener, const string16& text) | 35 LabelButton::LabelButton(ButtonListener* listener, const string16& text) |
| 33 : CustomButton(listener), | 36 : CustomButton(listener), |
| 34 image_(new ImageView()), | 37 image_(new ImageView()), |
| 35 label_(new Label(text)), | 38 label_(new Label(text)), |
| 36 button_state_images_(), | 39 button_state_images_(), |
| 37 button_state_colors_(), | 40 button_state_colors_(), |
| 38 explicitly_set_colors_(), | 41 explicitly_set_colors_(), |
| 39 is_default_(false), | 42 is_default_(false), |
| 40 native_theme_(false) { | 43 style_(STYLE_TEXTBUTTON) { |
| 41 set_border(new LabelButtonBorder()); | |
| 42 // Inset the button focus rect from the actual border; roughly match Windows. | |
| 43 set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3)); | |
| 44 SetAnimationDuration(kHoverAnimationDurationMs); | 44 SetAnimationDuration(kHoverAnimationDurationMs); |
| 45 | 45 |
| 46 AddChildView(image_); | 46 AddChildView(image_); |
| 47 image_->set_interactive(false); | 47 image_->set_interactive(false); |
| 48 | 48 |
| 49 AddChildView(label_); | 49 AddChildView(label_); |
| 50 label_->SetAutoColorReadabilityEnabled(false); | 50 label_->SetAutoColorReadabilityEnabled(false); |
| 51 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 51 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 52 | 52 |
| 53 // Initialize the colors, border, and layout for a Views-themed button. | 53 // Initialize the colors, border, and layout. |
| 54 SetNativeTheme(false); | 54 SetStyle(style_); |
| 55 } | 55 } |
| 56 | 56 |
| 57 LabelButton::~LabelButton() {} | 57 LabelButton::~LabelButton() {} |
| 58 | 58 |
| 59 const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { | 59 const gfx::ImageSkia& LabelButton::GetImage(ButtonState for_state) { |
| 60 if (for_state != STATE_NORMAL && button_state_images_[for_state].isNull()) | 60 if (for_state != STATE_NORMAL && button_state_images_[for_state].isNull()) |
| 61 return button_state_images_[STATE_NORMAL]; | 61 return button_state_images_[STATE_NORMAL]; |
| 62 return button_state_images_[for_state]; | 62 return button_state_images_[for_state]; |
| 63 } | 63 } |
| 64 | 64 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 } | 110 } |
| 111 | 111 |
| 112 void LabelButton::SetIsDefault(bool is_default) { | 112 void LabelButton::SetIsDefault(bool is_default) { |
| 113 if (is_default == is_default_) | 113 if (is_default == is_default_) |
| 114 return; | 114 return; |
| 115 is_default_ = is_default; | 115 is_default_ = is_default; |
| 116 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE); | 116 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE); |
| 117 is_default_ ? AddAccelerator(accel) : RemoveAccelerator(accel); | 117 is_default_ ? AddAccelerator(accel) : RemoveAccelerator(accel); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void LabelButton::SetNativeTheme(bool native_theme) { | 120 void LabelButton::SetStyle(ButtonStyle style) { |
| 121 native_theme_ = native_theme; | 121 style_ = style; |
| 122 LabelButtonBorder* border = new LabelButtonBorder(); | 122 set_border(new LabelButtonBorder(style)); |
| 123 border->set_native_theme(native_theme); | 123 // Inset the button focus rect from the actual border; roughly match Windows. |
| 124 set_border(border); | 124 set_focus_border(FocusBorder::CreateDashedFocusBorder(3, 3, 3, 3)); |
| 125 // Invalidate the layout to pickup the new insets from the border. | 125 // Invalidate the layout to pickup the new insets from the border. |
| 126 InvalidateLayout(); | 126 InvalidateLayout(); |
| 127 ResetColorsFromNativeTheme(); | 127 ResetColorsFromNativeTheme(); |
| 128 } | 128 } |
| 129 | 129 |
| 130 gfx::Size LabelButton::GetPreferredSize() { | 130 gfx::Size LabelButton::GetPreferredSize() { |
| 131 // Resize multi-line labels paired with images to use their available width. | 131 // Resize multi-line labels paired with images to use their available width. |
| 132 const gfx::Size image_size(image_->GetPreferredSize()); | 132 const gfx::Size image_size(image_->GetPreferredSize()); |
| 133 if (GetTextMultiLine() && !image_size.IsEmpty() && !GetText().empty() && | 133 if (GetTextMultiLine() && !image_size.IsEmpty() && !GetText().empty() && |
| 134 GetHorizontalAlignment() == gfx::ALIGN_CENTER) { | 134 GetHorizontalAlignment() == gfx::ALIGN_CENTER) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 148 min_size_ = size; | 148 min_size_ = size; |
| 149 | 149 |
| 150 // Return the largest known size clamped to the maximum size (if valid). | 150 // Return the largest known size clamped to the maximum size (if valid). |
| 151 if (max_size_.width() > 0) | 151 if (max_size_.width() > 0) |
| 152 size.set_width(std::min(max_size_.width(), size.width())); | 152 size.set_width(std::min(max_size_.width(), size.width())); |
| 153 if (max_size_.height() > 0) | 153 if (max_size_.height() > 0) |
| 154 size.set_height(std::min(max_size_.height(), size.height())); | 154 size.set_height(std::min(max_size_.height(), size.height())); |
| 155 return size; | 155 return size; |
| 156 } | 156 } |
| 157 | 157 |
| 158 std::string LabelButton::GetClassName() const { |
| 159 return kViewClassName; |
| 160 } |
| 161 |
| 158 void LabelButton::ResetColorsFromNativeTheme() { | 162 void LabelButton::ResetColorsFromNativeTheme() { |
| 159 const ui::NativeTheme* theme = GetNativeTheme(); | 163 const ui::NativeTheme* theme = GetNativeTheme(); |
| 160 SkColor colors[STATE_COUNT] = { | 164 SkColor colors[STATE_COUNT] = { |
| 161 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonEnabledColor), | 165 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonEnabledColor), |
| 162 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), | 166 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), |
| 163 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), | 167 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), |
| 164 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonDisabledColor), | 168 theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonDisabledColor), |
| 165 }; | 169 }; |
| 166 #if defined(OS_WIN) | 170 #if defined(OS_WIN) |
| 167 // Native Windows buttons do not change color on hover or when pressed. | 171 // Native Windows buttons do not change color on hover or when pressed. |
| 168 if (native_theme_ && theme == ui::NativeThemeWin::instance()) | 172 if (style() == STYLE_NATIVE_TEXTBUTTON && |
| 173 theme == ui::NativeThemeWin::instance()) |
| 169 colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL]; | 174 colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL]; |
| 170 #endif | 175 #endif |
| 171 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { | 176 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { |
| 172 if (!explicitly_set_colors_[state]) { | 177 if (!explicitly_set_colors_[state]) { |
| 173 SetTextColor(static_cast<ButtonState>(state), colors[state]); | 178 SetTextColor(static_cast<ButtonState>(state), colors[state]); |
| 174 explicitly_set_colors_[state] = false; | 179 explicitly_set_colors_[state] = false; |
| 175 } | 180 } |
| 176 } | 181 } |
| 177 } | 182 } |
| 178 | 183 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 } | 227 } |
| 223 | 228 |
| 224 gfx::Point label_origin(child_area.origin()); | 229 gfx::Point label_origin(child_area.origin()); |
| 225 if (!image_size.IsEmpty() && GetHorizontalAlignment() != gfx::ALIGN_RIGHT) | 230 if (!image_size.IsEmpty() && GetHorizontalAlignment() != gfx::ALIGN_RIGHT) |
| 226 label_origin.set_x(image_origin.x() + image_size.width() + kSpacing); | 231 label_origin.set_x(image_origin.x() + image_size.width() + kSpacing); |
| 227 | 232 |
| 228 image_->SetBoundsRect(gfx::Rect(image_origin, image_size)); | 233 image_->SetBoundsRect(gfx::Rect(image_origin, image_size)); |
| 229 label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); | 234 label_->SetBoundsRect(gfx::Rect(label_origin, label_size)); |
| 230 } | 235 } |
| 231 | 236 |
| 232 std::string LabelButton::GetClassName() const { | |
| 233 return "views/LabelButton"; | |
| 234 } | |
| 235 | |
| 236 void LabelButton::ChildPreferredSizeChanged(View* child) { | 237 void LabelButton::ChildPreferredSizeChanged(View* child) { |
| 237 PreferredSizeChanged(); | 238 PreferredSizeChanged(); |
| 238 } | 239 } |
| 239 | 240 |
| 240 void LabelButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 241 void LabelButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 241 ResetColorsFromNativeTheme(); | 242 ResetColorsFromNativeTheme(); |
| 242 } | 243 } |
| 243 | 244 |
| 244 ui::NativeTheme::Part LabelButton::GetThemePart() const { | 245 ui::NativeTheme::Part LabelButton::GetThemePart() const { |
| 245 return ui::NativeTheme::kPushButton; | 246 return ui::NativeTheme::kPushButton; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 257 case STATE_HOVERED: return ui::NativeTheme::kHovered; | 258 case STATE_HOVERED: return ui::NativeTheme::kHovered; |
| 258 case STATE_PRESSED: return ui::NativeTheme::kPressed; | 259 case STATE_PRESSED: return ui::NativeTheme::kPressed; |
| 259 case STATE_DISABLED: return ui::NativeTheme::kDisabled; | 260 case STATE_DISABLED: return ui::NativeTheme::kDisabled; |
| 260 case STATE_COUNT: NOTREACHED() << "Unknown state: " << state(); | 261 case STATE_COUNT: NOTREACHED() << "Unknown state: " << state(); |
| 261 } | 262 } |
| 262 return ui::NativeTheme::kNormal; | 263 return ui::NativeTheme::kNormal; |
| 263 } | 264 } |
| 264 | 265 |
| 265 const ui::Animation* LabelButton::GetThemeAnimation() const { | 266 const ui::Animation* LabelButton::GetThemeAnimation() const { |
| 266 #if defined(OS_WIN) | 267 #if defined(OS_WIN) |
| 267 if (native_theme_ && GetNativeTheme() == ui::NativeThemeWin::instance()) { | 268 if (style() == STYLE_NATIVE_TEXTBUTTON && |
| 269 GetNativeTheme() == ui::NativeThemeWin::instance()) { |
| 268 return ui::NativeThemeWin::instance()->IsThemingActive() ? | 270 return ui::NativeThemeWin::instance()->IsThemingActive() ? |
| 269 hover_animation_.get() : NULL; | 271 hover_animation_.get() : NULL; |
| 270 } | 272 } |
| 271 #endif | 273 #endif |
| 272 return hover_animation_.get(); | 274 return hover_animation_.get(); |
| 273 } | 275 } |
| 274 | 276 |
| 275 ui::NativeTheme::State LabelButton::GetBackgroundThemeState( | 277 ui::NativeTheme::State LabelButton::GetBackgroundThemeState( |
| 276 ui::NativeTheme::ExtraParams* params) const { | 278 ui::NativeTheme::ExtraParams* params) const { |
| 277 GetExtraParams(params); | 279 GetExtraParams(params); |
| 278 return ui::NativeTheme::kNormal; | 280 return ui::NativeTheme::kNormal; |
| 279 } | 281 } |
| 280 | 282 |
| 281 ui::NativeTheme::State LabelButton::GetForegroundThemeState( | 283 ui::NativeTheme::State LabelButton::GetForegroundThemeState( |
| 282 ui::NativeTheme::ExtraParams* params) const { | 284 ui::NativeTheme::ExtraParams* params) const { |
| 283 GetExtraParams(params); | 285 GetExtraParams(params); |
| 284 return ui::NativeTheme::kHovered; | 286 return ui::NativeTheme::kHovered; |
| 285 } | 287 } |
| 286 | 288 |
| 287 void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { | 289 void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
| 288 params->button.checked = false; | 290 params->button.checked = false; |
| 289 params->button.indeterminate = false; | 291 params->button.indeterminate = false; |
| 290 params->button.is_default = is_default_; | 292 params->button.is_default = is_default_; |
| 291 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); | 293 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); |
| 292 params->button.has_border = native_theme(); | 294 params->button.has_border = style() == STYLE_NATIVE_TEXTBUTTON; |
| 293 params->button.classic_state = 0; | 295 params->button.classic_state = 0; |
| 294 params->button.background_color = GetNativeTheme()->GetSystemColor( | 296 params->button.background_color = GetNativeTheme()->GetSystemColor( |
| 295 ui::NativeTheme::kColorId_TextButtonBackgroundColor); | 297 ui::NativeTheme::kColorId_TextButtonBackgroundColor); |
| 296 } | 298 } |
| 297 | 299 |
| 298 } // namespace views | 300 } // namespace views |
| OLD | NEW |