| 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/text_button.h" | 5 #include "ui/views/controls/button/text_button.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "grit/ui_resources.h" | 10 #include "grit/ui_resources.h" |
| 11 #include "ui/base/animation/throb_animation.h" | 11 #include "ui/base/animation/throb_animation.h" |
| 12 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
| 13 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/image/image.h" | 14 #include "ui/gfx/image/image.h" |
| 15 #include "ui/views/controls/button/button.h" | 15 #include "ui/views/controls/button/button.h" |
| 16 #include "ui/views/events/event.h" | 16 #include "ui/views/events/event.h" |
| 17 #include "ui/views/widget/widget.h" | 17 #include "ui/views/widget/widget.h" |
| 18 | 18 |
| 19 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
| 20 #include "skia/ext/skia_utils_win.h" | 20 #include "skia/ext/skia_utils_win.h" |
| 21 #include "ui/base/native_theme/native_theme_win.h" | 21 #include "ui/gfx/native_theme_win.h" |
| 22 #include "ui/gfx/platform_font_win.h" | 22 #include "ui/gfx/platform_font_win.h" |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 namespace views { | 25 namespace views { |
| 26 | 26 |
| 27 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
| 28 // The min size in DLUs comes from | 28 // The min size in DLUs comes from |
| 29 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/c
h14e.asp | 29 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/c
h14e.asp |
| 30 static const int kMinWidthDLUs = 50; | 30 static const int kMinWidthDLUs = 50; |
| 31 static const int kMinHeightDLUs = 14; | 31 static const int kMinHeightDLUs = 14; |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 NativeThemeDelegate* delegate) | 201 NativeThemeDelegate* delegate) |
| 202 : delegate_(delegate) { | 202 : delegate_(delegate) { |
| 203 } | 203 } |
| 204 | 204 |
| 205 TextButtonNativeThemeBorder::~TextButtonNativeThemeBorder() { | 205 TextButtonNativeThemeBorder::~TextButtonNativeThemeBorder() { |
| 206 } | 206 } |
| 207 | 207 |
| 208 void TextButtonNativeThemeBorder::Paint(const View& view, | 208 void TextButtonNativeThemeBorder::Paint(const View& view, |
| 209 gfx::Canvas* canvas) const { | 209 gfx::Canvas* canvas) const { |
| 210 const TextButtonBase* tb = static_cast<const TextButton*>(&view); | 210 const TextButtonBase* tb = static_cast<const TextButton*>(&view); |
| 211 const ui::NativeTheme* native_theme = ui::NativeTheme::instance(); | 211 const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance(); |
| 212 ui::NativeTheme::Part part = delegate_->GetThemePart(); | 212 gfx::NativeTheme::Part part = delegate_->GetThemePart(); |
| 213 gfx::Rect rect(delegate_->GetThemePaintRect()); | 213 gfx::Rect rect(delegate_->GetThemePaintRect()); |
| 214 | 214 |
| 215 if (tb->show_multiple_icon_states() && | 215 if (tb->show_multiple_icon_states() && |
| 216 delegate_->GetThemeAnimation() != NULL && | 216 delegate_->GetThemeAnimation() != NULL && |
| 217 delegate_->GetThemeAnimation()->is_animating()) { | 217 delegate_->GetThemeAnimation()->is_animating()) { |
| 218 // Paint background state. | 218 // Paint background state. |
| 219 ui::NativeTheme::ExtraParams prev_extra; | 219 gfx::NativeTheme::ExtraParams prev_extra; |
| 220 ui::NativeTheme::State prev_state = | 220 gfx::NativeTheme::State prev_state = |
| 221 delegate_->GetBackgroundThemeState(&prev_extra); | 221 delegate_->GetBackgroundThemeState(&prev_extra); |
| 222 native_theme->Paint(canvas->sk_canvas(), part, prev_state, rect, | 222 native_theme->Paint(canvas->sk_canvas(), part, prev_state, rect, |
| 223 prev_extra); | 223 prev_extra); |
| 224 | 224 |
| 225 // Composite foreground state above it. | 225 // Composite foreground state above it. |
| 226 ui::NativeTheme::ExtraParams extra; | 226 gfx::NativeTheme::ExtraParams extra; |
| 227 ui::NativeTheme::State state = delegate_->GetForegroundThemeState(&extra); | 227 gfx::NativeTheme::State state = delegate_->GetForegroundThemeState(&extra); |
| 228 int alpha = delegate_->GetThemeAnimation()->CurrentValueBetween(0, 255); | 228 int alpha = delegate_->GetThemeAnimation()->CurrentValueBetween(0, 255); |
| 229 canvas->SaveLayerAlpha(static_cast<uint8>(alpha)); | 229 canvas->SaveLayerAlpha(static_cast<uint8>(alpha)); |
| 230 native_theme->Paint(canvas->sk_canvas(), part, state, rect, extra); | 230 native_theme->Paint(canvas->sk_canvas(), part, state, rect, extra); |
| 231 canvas->Restore(); | 231 canvas->Restore(); |
| 232 } else { | 232 } else { |
| 233 ui::NativeTheme::ExtraParams extra; | 233 gfx::NativeTheme::ExtraParams extra; |
| 234 ui::NativeTheme::State state = delegate_->GetThemeState(&extra); | 234 gfx::NativeTheme::State state = delegate_->GetThemeState(&extra); |
| 235 native_theme->Paint(canvas->sk_canvas(), part, state, rect, extra); | 235 native_theme->Paint(canvas->sk_canvas(), part, state, rect, extra); |
| 236 } | 236 } |
| 237 } | 237 } |
| 238 | 238 |
| 239 void TextButtonNativeThemeBorder::GetInsets(gfx::Insets* insets) const { | 239 void TextButtonNativeThemeBorder::GetInsets(gfx::Insets* insets) const { |
| 240 insets->Set(kPreferredNativeThemePaddingVertical, | 240 insets->Set(kPreferredNativeThemePaddingVertical, |
| 241 kPreferredNativeThemePaddingHorizontal, | 241 kPreferredNativeThemePaddingHorizontal, |
| 242 kPreferredNativeThemePaddingVertical, | 242 kPreferredNativeThemePaddingVertical, |
| 243 kPreferredNativeThemePaddingHorizontal); | 243 kPreferredNativeThemePaddingHorizontal); |
| 244 } | 244 } |
| 245 | 245 |
| 246 | 246 |
| 247 //////////////////////////////////////////////////////////////////////////////// | 247 //////////////////////////////////////////////////////////////////////////////// |
| 248 // TextButtonBase, public: | 248 // TextButtonBase, public: |
| 249 | 249 |
| 250 TextButtonBase::TextButtonBase(ButtonListener* listener, const string16& text) | 250 TextButtonBase::TextButtonBase(ButtonListener* listener, const string16& text) |
| 251 : CustomButton(listener), | 251 : CustomButton(listener), |
| 252 alignment_(ALIGN_LEFT), | 252 alignment_(ALIGN_LEFT), |
| 253 font_(ResourceBundle::GetSharedInstance().GetFont( | 253 font_(ResourceBundle::GetSharedInstance().GetFont( |
| 254 ResourceBundle::BaseFont)), | 254 ResourceBundle::BaseFont)), |
| 255 color_(ui::NativeTheme::instance()->GetSystemColor( | 255 color_(gfx::NativeTheme::instance()->GetSystemColor( |
| 256 ui::NativeTheme::kColorId_TextButtonEnabledColor)), | 256 gfx::NativeTheme::kColorId_TextButtonEnabledColor)), |
| 257 color_enabled_(ui::NativeTheme::instance()->GetSystemColor( | 257 color_enabled_(gfx::NativeTheme::instance()->GetSystemColor( |
| 258 ui::NativeTheme::kColorId_TextButtonEnabledColor)), | 258 gfx::NativeTheme::kColorId_TextButtonEnabledColor)), |
| 259 color_disabled_(ui::NativeTheme::instance()->GetSystemColor( | 259 color_disabled_(gfx::NativeTheme::instance()->GetSystemColor( |
| 260 ui::NativeTheme::kColorId_TextButtonDisabledColor)), | 260 gfx::NativeTheme::kColorId_TextButtonDisabledColor)), |
| 261 color_highlight_(ui::NativeTheme::instance()->GetSystemColor( | 261 color_highlight_(gfx::NativeTheme::instance()->GetSystemColor( |
| 262 ui::NativeTheme::kColorId_TextButtonHighlightColor)), | 262 gfx::NativeTheme::kColorId_TextButtonHighlightColor)), |
| 263 color_hover_(ui::NativeTheme::instance()->GetSystemColor( | 263 color_hover_(gfx::NativeTheme::instance()->GetSystemColor( |
| 264 ui::NativeTheme::kColorId_TextButtonHoverColor)), | 264 gfx::NativeTheme::kColorId_TextButtonHoverColor)), |
| 265 text_halo_color_(0), | 265 text_halo_color_(0), |
| 266 has_text_halo_(false), | 266 has_text_halo_(false), |
| 267 active_text_shadow_color_(0), | 267 active_text_shadow_color_(0), |
| 268 inactive_text_shadow_color_(0), | 268 inactive_text_shadow_color_(0), |
| 269 has_shadow_(false), | 269 has_shadow_(false), |
| 270 shadow_offset_(gfx::Point(1, 1)), | 270 shadow_offset_(gfx::Point(1, 1)), |
| 271 max_width_(0), | 271 max_width_(0), |
| 272 show_multiple_icon_states_(true), | 272 show_multiple_icon_states_(true), |
| 273 is_default_(false), | 273 is_default_(false), |
| 274 multi_line_(false), | 274 multi_line_(false), |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 case ALIGN_CENTER: | 451 case ALIGN_CENTER: |
| 452 flags |= gfx::Canvas::TEXT_ALIGN_CENTER; | 452 flags |= gfx::Canvas::TEXT_ALIGN_CENTER; |
| 453 break; | 453 break; |
| 454 } | 454 } |
| 455 } | 455 } |
| 456 | 456 |
| 457 return flags; | 457 return flags; |
| 458 } | 458 } |
| 459 | 459 |
| 460 void TextButtonBase::GetExtraParams( | 460 void TextButtonBase::GetExtraParams( |
| 461 ui::NativeTheme::ExtraParams* params) const { | 461 gfx::NativeTheme::ExtraParams* params) const { |
| 462 params->button.checked = false; | 462 params->button.checked = false; |
| 463 params->button.indeterminate = false; | 463 params->button.indeterminate = false; |
| 464 params->button.is_default = false; | 464 params->button.is_default = false; |
| 465 params->button.has_border = false; | 465 params->button.has_border = false; |
| 466 params->button.classic_state = 0; | 466 params->button.classic_state = 0; |
| 467 params->button.background_color = | 467 params->button.background_color = |
| 468 ui::NativeTheme::instance()->GetSystemColor( | 468 gfx::NativeTheme::instance()->GetSystemColor( |
| 469 ui::NativeTheme::kColorId_TextButtonBackgroundColor); | 469 gfx::NativeTheme::kColorId_TextButtonBackgroundColor); |
| 470 } | 470 } |
| 471 | 471 |
| 472 gfx::Rect TextButtonBase::GetContentBounds(int extra_width) const { | 472 gfx::Rect TextButtonBase::GetContentBounds(int extra_width) const { |
| 473 gfx::Insets insets = GetInsets(); | 473 gfx::Insets insets = GetInsets(); |
| 474 int available_width = width() - insets.width(); | 474 int available_width = width() - insets.width(); |
| 475 int content_width = text_size_.width() + extra_width; | 475 int content_width = text_size_.width() + extra_width; |
| 476 int content_x = 0; | 476 int content_x = 0; |
| 477 switch(alignment_) { | 477 switch(alignment_) { |
| 478 case ALIGN_LEFT: | 478 case ALIGN_LEFT: |
| 479 content_x = insets.left(); | 479 content_x = insets.left(); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 return kViewClassName; | 601 return kViewClassName; |
| 602 } | 602 } |
| 603 | 603 |
| 604 //////////////////////////////////////////////////////////////////////////////// | 604 //////////////////////////////////////////////////////////////////////////////// |
| 605 // TextButtonBase, NativeThemeDelegate overrides: | 605 // TextButtonBase, NativeThemeDelegate overrides: |
| 606 | 606 |
| 607 gfx::Rect TextButtonBase::GetThemePaintRect() const { | 607 gfx::Rect TextButtonBase::GetThemePaintRect() const { |
| 608 return GetLocalBounds(); | 608 return GetLocalBounds(); |
| 609 } | 609 } |
| 610 | 610 |
| 611 ui::NativeTheme::State TextButtonBase::GetThemeState( | 611 gfx::NativeTheme::State TextButtonBase::GetThemeState( |
| 612 ui::NativeTheme::ExtraParams* params) const { | 612 gfx::NativeTheme::ExtraParams* params) const { |
| 613 GetExtraParams(params); | 613 GetExtraParams(params); |
| 614 switch(state()) { | 614 switch(state()) { |
| 615 case BS_DISABLED: | 615 case BS_DISABLED: |
| 616 return ui::NativeTheme::kDisabled; | 616 return gfx::NativeTheme::kDisabled; |
| 617 case BS_NORMAL: | 617 case BS_NORMAL: |
| 618 return ui::NativeTheme::kNormal; | 618 return gfx::NativeTheme::kNormal; |
| 619 case BS_HOT: | 619 case BS_HOT: |
| 620 return ui::NativeTheme::kHovered; | 620 return gfx::NativeTheme::kHovered; |
| 621 case BS_PUSHED: | 621 case BS_PUSHED: |
| 622 return ui::NativeTheme::kPressed; | 622 return gfx::NativeTheme::kPressed; |
| 623 default: | 623 default: |
| 624 NOTREACHED() << "Unknown state: " << state(); | 624 NOTREACHED() << "Unknown state: " << state(); |
| 625 return ui::NativeTheme::kNormal; | 625 return gfx::NativeTheme::kNormal; |
| 626 } | 626 } |
| 627 } | 627 } |
| 628 | 628 |
| 629 const ui::Animation* TextButtonBase::GetThemeAnimation() const { | 629 const ui::Animation* TextButtonBase::GetThemeAnimation() const { |
| 630 #if defined(USE_AURA) | 630 #if defined(USE_AURA) |
| 631 return hover_animation_.get(); | 631 return hover_animation_.get(); |
| 632 #elif defined(OS_WIN) | 632 #elif defined(OS_WIN) |
| 633 return ui::NativeThemeWin::instance()->IsThemingActive() | 633 return gfx::NativeThemeWin::instance()->IsThemingActive() |
| 634 ? hover_animation_.get() : NULL; | 634 ? hover_animation_.get() : NULL; |
| 635 #else | 635 #else |
| 636 return hover_animation_.get(); | 636 return hover_animation_.get(); |
| 637 #endif | 637 #endif |
| 638 } | 638 } |
| 639 | 639 |
| 640 ui::NativeTheme::State TextButtonBase::GetBackgroundThemeState( | 640 gfx::NativeTheme::State TextButtonBase::GetBackgroundThemeState( |
| 641 ui::NativeTheme::ExtraParams* params) const { | 641 gfx::NativeTheme::ExtraParams* params) const { |
| 642 GetExtraParams(params); | 642 GetExtraParams(params); |
| 643 return ui::NativeTheme::kNormal; | 643 return gfx::NativeTheme::kNormal; |
| 644 } | 644 } |
| 645 | 645 |
| 646 ui::NativeTheme::State TextButtonBase::GetForegroundThemeState( | 646 gfx::NativeTheme::State TextButtonBase::GetForegroundThemeState( |
| 647 ui::NativeTheme::ExtraParams* params) const { | 647 gfx::NativeTheme::ExtraParams* params) const { |
| 648 GetExtraParams(params); | 648 GetExtraParams(params); |
| 649 return ui::NativeTheme::kHovered; | 649 return gfx::NativeTheme::kHovered; |
| 650 } | 650 } |
| 651 | 651 |
| 652 //////////////////////////////////////////////////////////////////////////////// | 652 //////////////////////////////////////////////////////////////////////////////// |
| 653 // | 653 // |
| 654 // TextButton | 654 // TextButton |
| 655 // | 655 // |
| 656 //////////////////////////////////////////////////////////////////////////////// | 656 //////////////////////////////////////////////////////////////////////////////// |
| 657 | 657 |
| 658 TextButton::TextButton(ButtonListener* listener, const string16& text) | 658 TextButton::TextButton(ButtonListener* listener, const string16& text) |
| 659 : TextButtonBase(listener, text), | 659 : TextButtonBase(listener, text), |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 } | 749 } |
| 750 | 750 |
| 751 void TextButton::OnPaintFocusBorder(gfx::Canvas* canvas) { | 751 void TextButton::OnPaintFocusBorder(gfx::Canvas* canvas) { |
| 752 if (HasFocus() && (focusable() || IsAccessibilityFocusable())) { | 752 if (HasFocus() && (focusable() || IsAccessibilityFocusable())) { |
| 753 gfx::Rect rect(GetLocalBounds()); | 753 gfx::Rect rect(GetLocalBounds()); |
| 754 rect.Inset(kFocusRectInset, kFocusRectInset); | 754 rect.Inset(kFocusRectInset, kFocusRectInset); |
| 755 canvas->DrawFocusRect(rect); | 755 canvas->DrawFocusRect(rect); |
| 756 } | 756 } |
| 757 } | 757 } |
| 758 | 758 |
| 759 ui::NativeTheme::Part TextButton::GetThemePart() const { | 759 gfx::NativeTheme::Part TextButton::GetThemePart() const { |
| 760 return ui::NativeTheme::kPushButton; | 760 return gfx::NativeTheme::kPushButton; |
| 761 } | 761 } |
| 762 | 762 |
| 763 void TextButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { | 763 void TextButton::GetExtraParams(gfx::NativeTheme::ExtraParams* params) const { |
| 764 TextButtonBase::GetExtraParams(params); | 764 TextButtonBase::GetExtraParams(params); |
| 765 params->button.is_default = is_default_; | 765 params->button.is_default = is_default_; |
| 766 } | 766 } |
| 767 | 767 |
| 768 gfx::Rect TextButton::GetTextBounds() const { | 768 gfx::Rect TextButton::GetTextBounds() const { |
| 769 int extra_width = 0; | 769 int extra_width = 0; |
| 770 | 770 |
| 771 const SkBitmap& icon = GetImageToPaint(); | 771 const SkBitmap& icon = GetImageToPaint(); |
| 772 if (icon.width() > 0) | 772 if (icon.width() > 0) |
| 773 extra_width = icon.width() + (text_.empty() ? 0 : icon_text_spacing_); | 773 extra_width = icon.width() + (text_.empty() ? 0 : icon_text_spacing_); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 841 gfx::Rect rect(GetLocalBounds()); | 841 gfx::Rect rect(GetLocalBounds()); |
| 842 rect.Inset(kFocusRectInset, kFocusRectInset); | 842 rect.Inset(kFocusRectInset, kFocusRectInset); |
| 843 canvas->DrawFocusRect(rect); | 843 canvas->DrawFocusRect(rect); |
| 844 } | 844 } |
| 845 #else | 845 #else |
| 846 TextButton::OnPaintFocusBorder(canvas); | 846 TextButton::OnPaintFocusBorder(canvas); |
| 847 #endif | 847 #endif |
| 848 } | 848 } |
| 849 | 849 |
| 850 void NativeTextButton::GetExtraParams( | 850 void NativeTextButton::GetExtraParams( |
| 851 ui::NativeTheme::ExtraParams* params) const { | 851 gfx::NativeTheme::ExtraParams* params) const { |
| 852 TextButton::GetExtraParams(params); | 852 TextButton::GetExtraParams(params); |
| 853 params->button.has_border = true; | 853 params->button.has_border = true; |
| 854 } | 854 } |
| 855 | 855 |
| 856 } // namespace views | 856 } // namespace views |
| OLD | NEW |