| 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" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 83 |
| 84 TextButtonBorder::TextButtonBorder() | 84 TextButtonBorder::TextButtonBorder() |
| 85 : vertical_padding_(kPreferredPaddingVertical) { | 85 : vertical_padding_(kPreferredPaddingVertical) { |
| 86 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 86 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 87 BorderImageSet normal_set = { | 87 BorderImageSet normal_set = { |
| 88 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | 88 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 89 }; | 89 }; |
| 90 set_normal_set(normal_set); | 90 set_normal_set(normal_set); |
| 91 | 91 |
| 92 BorderImageSet hot_set = { | 92 BorderImageSet hot_set = { |
| 93 rb.GetImageNamed(IDR_TEXTBUTTON_TOP_LEFT_H).ToSkBitmap(), | 93 rb.GetImageNamed(IDR_TEXTBUTTON_TOP_LEFT_H).ToImageSkia(), |
| 94 rb.GetImageNamed(IDR_TEXTBUTTON_TOP_H).ToSkBitmap(), | 94 rb.GetImageNamed(IDR_TEXTBUTTON_TOP_H).ToImageSkia(), |
| 95 rb.GetImageNamed(IDR_TEXTBUTTON_TOP_RIGHT_H).ToSkBitmap(), | 95 rb.GetImageNamed(IDR_TEXTBUTTON_TOP_RIGHT_H).ToImageSkia(), |
| 96 rb.GetImageNamed(IDR_TEXTBUTTON_LEFT_H).ToSkBitmap(), | 96 rb.GetImageNamed(IDR_TEXTBUTTON_LEFT_H).ToImageSkia(), |
| 97 rb.GetImageNamed(IDR_TEXTBUTTON_CENTER_H).ToSkBitmap(), | 97 rb.GetImageNamed(IDR_TEXTBUTTON_CENTER_H).ToImageSkia(), |
| 98 rb.GetImageNamed(IDR_TEXTBUTTON_RIGHT_H).ToSkBitmap(), | 98 rb.GetImageNamed(IDR_TEXTBUTTON_RIGHT_H).ToImageSkia(), |
| 99 rb.GetImageNamed(IDR_TEXTBUTTON_BOTTOM_LEFT_H).ToSkBitmap(), | 99 rb.GetImageNamed(IDR_TEXTBUTTON_BOTTOM_LEFT_H).ToImageSkia(), |
| 100 rb.GetImageNamed(IDR_TEXTBUTTON_BOTTOM_H).ToSkBitmap(), | 100 rb.GetImageNamed(IDR_TEXTBUTTON_BOTTOM_H).ToImageSkia(), |
| 101 rb.GetImageNamed(IDR_TEXTBUTTON_BOTTOM_RIGHT_H).ToSkBitmap(), | 101 rb.GetImageNamed(IDR_TEXTBUTTON_BOTTOM_RIGHT_H).ToImageSkia(), |
| 102 }; | 102 }; |
| 103 set_hot_set(hot_set); | 103 set_hot_set(hot_set); |
| 104 | 104 |
| 105 BorderImageSet pushed_set = { | 105 BorderImageSet pushed_set = { |
| 106 rb.GetImageNamed(IDR_TEXTBUTTON_TOP_LEFT_P).ToSkBitmap(), | 106 rb.GetImageNamed(IDR_TEXTBUTTON_TOP_LEFT_P).ToImageSkia(), |
| 107 rb.GetImageNamed(IDR_TEXTBUTTON_TOP_P).ToSkBitmap(), | 107 rb.GetImageNamed(IDR_TEXTBUTTON_TOP_P).ToImageSkia(), |
| 108 rb.GetImageNamed(IDR_TEXTBUTTON_TOP_RIGHT_P).ToSkBitmap(), | 108 rb.GetImageNamed(IDR_TEXTBUTTON_TOP_RIGHT_P).ToImageSkia(), |
| 109 rb.GetImageNamed(IDR_TEXTBUTTON_LEFT_P).ToSkBitmap(), | 109 rb.GetImageNamed(IDR_TEXTBUTTON_LEFT_P).ToImageSkia(), |
| 110 rb.GetImageNamed(IDR_TEXTBUTTON_CENTER_P).ToSkBitmap(), | 110 rb.GetImageNamed(IDR_TEXTBUTTON_CENTER_P).ToImageSkia(), |
| 111 rb.GetImageNamed(IDR_TEXTBUTTON_RIGHT_P).ToSkBitmap(), | 111 rb.GetImageNamed(IDR_TEXTBUTTON_RIGHT_P).ToImageSkia(), |
| 112 rb.GetImageNamed(IDR_TEXTBUTTON_BOTTOM_LEFT_P).ToSkBitmap(), | 112 rb.GetImageNamed(IDR_TEXTBUTTON_BOTTOM_LEFT_P).ToImageSkia(), |
| 113 rb.GetImageNamed(IDR_TEXTBUTTON_BOTTOM_P).ToSkBitmap(), | 113 rb.GetImageNamed(IDR_TEXTBUTTON_BOTTOM_P).ToImageSkia(), |
| 114 rb.GetImageNamed(IDR_TEXTBUTTON_BOTTOM_RIGHT_P).ToSkBitmap(), | 114 rb.GetImageNamed(IDR_TEXTBUTTON_BOTTOM_RIGHT_P).ToImageSkia(), |
| 115 }; | 115 }; |
| 116 set_pushed_set(pushed_set); | 116 set_pushed_set(pushed_set); |
| 117 } | 117 } |
| 118 | 118 |
| 119 TextButtonBorder::~TextButtonBorder() { | 119 TextButtonBorder::~TextButtonBorder() { |
| 120 } | 120 } |
| 121 | 121 |
| 122 | 122 |
| 123 //////////////////////////////////////////////////////////////////////////////// | 123 //////////////////////////////////////////////////////////////////////////////// |
| 124 // | 124 // |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 OnPaintBorder(canvas); | 507 OnPaintBorder(canvas); |
| 508 OnPaintFocusBorder(canvas); | 508 OnPaintFocusBorder(canvas); |
| 509 } | 509 } |
| 510 | 510 |
| 511 gfx::Rect text_bounds(GetTextBounds()); | 511 gfx::Rect text_bounds(GetTextBounds()); |
| 512 if (text_bounds.width() > 0) { | 512 if (text_bounds.width() > 0) { |
| 513 // Because the text button can (at times) draw multiple elements on the | 513 // Because the text button can (at times) draw multiple elements on the |
| 514 // canvas, we can not mirror the button by simply flipping the canvas as | 514 // canvas, we can not mirror the button by simply flipping the canvas as |
| 515 // doing this will mirror the text itself. Flipping the canvas will also | 515 // doing this will mirror the text itself. Flipping the canvas will also |
| 516 // make the icons look wrong because icons are almost always represented as | 516 // make the icons look wrong because icons are almost always represented as |
| 517 // direction-insensitive bitmaps and such bitmaps should never be flipped | 517 // direction-insensitive images and such images should never be flipped |
| 518 // horizontally. | 518 // horizontally. |
| 519 // | 519 // |
| 520 // Due to the above, we must perform the flipping manually for RTL UIs. | 520 // Due to the above, we must perform the flipping manually for RTL UIs. |
| 521 text_bounds.set_x(GetMirroredXForRect(text_bounds)); | 521 text_bounds.set_x(GetMirroredXForRect(text_bounds)); |
| 522 | 522 |
| 523 SkColor text_color = (show_multiple_icon_states_ && | 523 SkColor text_color = (show_multiple_icon_states_ && |
| 524 (state() == BS_HOT || state() == BS_PUSHED)) ? color_hover_ : color_; | 524 (state() == BS_HOT || state() == BS_PUSHED)) ? color_hover_ : color_; |
| 525 | 525 |
| 526 int draw_string_flags = gfx::Canvas::DefaultCanvasTextAlignment() | | 526 int draw_string_flags = gfx::Canvas::DefaultCanvasTextAlignment() | |
| 527 ComputeCanvasStringFlags(); | 527 ComputeCanvasStringFlags(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 has_hover_icon_(false), | 661 has_hover_icon_(false), |
| 662 has_pushed_icon_(false), | 662 has_pushed_icon_(false), |
| 663 icon_text_spacing_(kDefaultIconTextSpacing), | 663 icon_text_spacing_(kDefaultIconTextSpacing), |
| 664 ignore_minimum_size_(true) { | 664 ignore_minimum_size_(true) { |
| 665 set_border(new TextButtonBorder); | 665 set_border(new TextButtonBorder); |
| 666 } | 666 } |
| 667 | 667 |
| 668 TextButton::~TextButton() { | 668 TextButton::~TextButton() { |
| 669 } | 669 } |
| 670 | 670 |
| 671 void TextButton::SetIcon(const SkBitmap& icon) { | 671 void TextButton::SetIcon(const gfx::ImageSkia& icon) { |
| 672 icon_ = icon; | 672 icon_ = icon; |
| 673 SchedulePaint(); | 673 SchedulePaint(); |
| 674 } | 674 } |
| 675 | 675 |
| 676 void TextButton::SetHoverIcon(const SkBitmap& icon) { | 676 void TextButton::SetHoverIcon(const gfx::ImageSkia& icon) { |
| 677 icon_hover_ = icon; | 677 icon_hover_ = icon; |
| 678 has_hover_icon_ = true; | 678 has_hover_icon_ = true; |
| 679 SchedulePaint(); | 679 SchedulePaint(); |
| 680 } | 680 } |
| 681 | 681 |
| 682 void TextButton::SetPushedIcon(const SkBitmap& icon) { | 682 void TextButton::SetPushedIcon(const gfx::ImageSkia& icon) { |
| 683 icon_pushed_ = icon; | 683 icon_pushed_ = icon; |
| 684 has_pushed_icon_ = true; | 684 has_pushed_icon_ = true; |
| 685 SchedulePaint(); | 685 SchedulePaint(); |
| 686 } | 686 } |
| 687 | 687 |
| 688 gfx::Size TextButton::GetPreferredSize() { | 688 gfx::Size TextButton::GetPreferredSize() { |
| 689 gfx::Size prefsize(TextButtonBase::GetPreferredSize()); | 689 gfx::Size prefsize(TextButtonBase::GetPreferredSize()); |
| 690 prefsize.Enlarge(icon_.width(), 0); | 690 prefsize.Enlarge(icon_.width(), 0); |
| 691 prefsize.set_height(std::max(prefsize.height(), icon_.height())); | 691 prefsize.set_height(std::max(prefsize.height(), icon_.height())); |
| 692 | 692 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 710 platform_font->vertical_dlus_to_pixels(kMinHeightDLUs))); | 710 platform_font->vertical_dlus_to_pixels(kMinHeightDLUs))); |
| 711 } | 711 } |
| 712 #endif | 712 #endif |
| 713 | 713 |
| 714 return prefsize; | 714 return prefsize; |
| 715 } | 715 } |
| 716 | 716 |
| 717 void TextButton::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) { | 717 void TextButton::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) { |
| 718 TextButtonBase::PaintButton(canvas, mode); | 718 TextButtonBase::PaintButton(canvas, mode); |
| 719 | 719 |
| 720 const SkBitmap& icon = GetImageToPaint(); | 720 const gfx::ImageSkia& icon = GetImageToPaint(); |
| 721 | 721 |
| 722 if (icon.width() > 0) { | 722 if (icon.width() > 0) { |
| 723 gfx::Rect text_bounds = GetTextBounds(); | 723 gfx::Rect text_bounds = GetTextBounds(); |
| 724 int icon_x; | 724 int icon_x; |
| 725 int spacing = text_.empty() ? 0 : icon_text_spacing_; | 725 int spacing = text_.empty() ? 0 : icon_text_spacing_; |
| 726 if (icon_placement_ == ICON_ON_LEFT) { | 726 if (icon_placement_ == ICON_ON_LEFT) { |
| 727 icon_x = text_bounds.x() - icon.width() - spacing; | 727 icon_x = text_bounds.x() - icon.width() - spacing; |
| 728 } else { | 728 } else { |
| 729 icon_x = text_bounds.right() + spacing; | 729 icon_x = text_bounds.right() + spacing; |
| 730 } | 730 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 761 } | 761 } |
| 762 | 762 |
| 763 void TextButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { | 763 void TextButton::GetExtraParams(ui::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 gfx::ImageSkia& 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_); |
| 774 | 774 |
| 775 gfx::Rect bounds(GetContentBounds(extra_width)); | 775 gfx::Rect bounds(GetContentBounds(extra_width)); |
| 776 | 776 |
| 777 if (extra_width > 0) { | 777 if (extra_width > 0) { |
| 778 // Make sure the icon is always fully visible. | 778 // Make sure the icon is always fully visible. |
| 779 if (icon_placement_ == ICON_ON_LEFT) { | 779 if (icon_placement_ == ICON_ON_LEFT) { |
| 780 bounds.Inset(extra_width, 0, 0, 0); | 780 bounds.Inset(extra_width, 0, 0, 0); |
| 781 } else { | 781 } else { |
| 782 bounds.Inset(0, 0, extra_width, 0); | 782 bounds.Inset(0, 0, extra_width, 0); |
| 783 } | 783 } |
| 784 } | 784 } |
| 785 | 785 |
| 786 return bounds; | 786 return bounds; |
| 787 } | 787 } |
| 788 | 788 |
| 789 const SkBitmap& TextButton::GetImageToPaint() const { | 789 const gfx::ImageSkia& TextButton::GetImageToPaint() const { |
| 790 if (show_multiple_icon_states_) { | 790 if (show_multiple_icon_states_) { |
| 791 if (has_hover_icon_ && (state() == BS_HOT)) | 791 if (has_hover_icon_ && (state() == BS_HOT)) |
| 792 return icon_hover_; | 792 return icon_hover_; |
| 793 if (has_pushed_icon_ && (state() == BS_PUSHED)) | 793 if (has_pushed_icon_ && (state() == BS_PUSHED)) |
| 794 return icon_pushed_; | 794 return icon_pushed_; |
| 795 } | 795 } |
| 796 return icon_; | 796 return icon_; |
| 797 } | 797 } |
| 798 | 798 |
| 799 //////////////////////////////////////////////////////////////////////////////// | 799 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 #endif | 847 #endif |
| 848 } | 848 } |
| 849 | 849 |
| 850 void NativeTextButton::GetExtraParams( | 850 void NativeTextButton::GetExtraParams( |
| 851 ui::NativeTheme::ExtraParams* params) const { | 851 ui::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 |