| 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/image_button.h" | 5 #include "ui/views/controls/button/image_button.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "ui/base/animation/throb_animation.h" | 8 #include "ui/base/animation/throb_animation.h" |
| 9 #include "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
| 10 #include "ui/gfx/skbitmap_operations.h" | 10 #include "ui/gfx/skbitmap_operations.h" |
| 11 | 11 |
| 12 namespace views { | 12 namespace views { |
| 13 | 13 |
| 14 static const int kDefaultWidth = 16; // Default button width if no theme. | 14 static const int kDefaultWidth = 16; // Default button width if no theme. |
| 15 static const int kDefaultHeight = 14; // Default button height if no theme. | 15 static const int kDefaultHeight = 14; // Default button height if no theme. |
| 16 | 16 |
| 17 //////////////////////////////////////////////////////////////////////////////// | 17 //////////////////////////////////////////////////////////////////////////////// |
| 18 // ImageButton, public: | 18 // ImageButton, public: |
| 19 | 19 |
| 20 ImageButton::ImageButton(ButtonListener* listener) | 20 ImageButton::ImageButton(ButtonListener* listener) |
| 21 : CustomButton(listener), | 21 : CustomButton(listener), |
| 22 h_alignment_(ALIGN_LEFT), | 22 h_alignment_(ALIGN_LEFT), |
| 23 v_alignment_(ALIGN_TOP), | 23 v_alignment_(ALIGN_TOP), |
| 24 preferred_size_(kDefaultWidth, kDefaultHeight) { | 24 preferred_size_(kDefaultWidth, kDefaultHeight) { |
| 25 // By default, we request that the gfx::Canvas passed to our View::OnPaint() | 25 // By default, we request that the gfx::Canvas passed to our View::OnPaint() |
| 26 // implementation is flipped horizontally so that the button's bitmaps are | 26 // implementation is flipped horizontally so that the button's images are |
| 27 // mirrored when the UI directionality is right-to-left. | 27 // mirrored when the UI directionality is right-to-left. |
| 28 EnableCanvasFlippingForRTLUI(true); | 28 EnableCanvasFlippingForRTLUI(true); |
| 29 } | 29 } |
| 30 | 30 |
| 31 ImageButton::~ImageButton() { | 31 ImageButton::~ImageButton() { |
| 32 } | 32 } |
| 33 | 33 |
| 34 void ImageButton::SetImage(ButtonState state, const gfx::ImageSkia* image) { | 34 void ImageButton::SetImage(ButtonState state, const gfx::ImageSkia* image) { |
| 35 images_[state] = image ? *image : gfx::ImageSkia(); | 35 images_[state] = image ? *image : gfx::ImageSkia(); |
| 36 PreferredSizeChanged(); | 36 PreferredSizeChanged(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 void ImageButton::SetBackground(SkColor color, | 39 void ImageButton::SetBackground(SkColor color, |
| 40 const gfx::ImageSkia* image, | 40 const gfx::ImageSkia* image, |
| 41 const gfx::ImageSkia* mask) { | 41 const gfx::ImageSkia* mask) { |
| 42 if (!image || !mask) { | 42 if (!image || !mask) { |
| 43 background_image_.reset(); | 43 background_image_ = gfx::ImageSkia(); |
| 44 return; | 44 return; |
| 45 } | 45 } |
| 46 | 46 |
| 47 background_image_ = | 47 background_image_ = |
| 48 SkBitmapOperations::CreateButtonBackground(color, *image, *mask); | 48 SkBitmapOperations::CreateButtonBackground(color, *image, *mask); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void ImageButton::SetOverlayImage(const SkBitmap* image) { | 51 void ImageButton::SetOverlayImage(const gfx::ImageSkia* image) { |
| 52 if (!image) { | 52 if (!image) { |
| 53 overlay_image_.reset(); | 53 overlay_image_ = gfx::ImageSkia(); |
| 54 return; | 54 return; |
| 55 } | 55 } |
| 56 overlay_image_ = *image; | 56 overlay_image_ = *image; |
| 57 } | 57 } |
| 58 | 58 |
| 59 void ImageButton::SetImageAlignment(HorizontalAlignment h_align, | 59 void ImageButton::SetImageAlignment(HorizontalAlignment h_align, |
| 60 VerticalAlignment v_align) { | 60 VerticalAlignment v_align) { |
| 61 h_alignment_ = h_align; | 61 h_alignment_ = h_align; |
| 62 v_alignment_ = v_align; | 62 v_alignment_ = v_align; |
| 63 SchedulePaint(); | 63 SchedulePaint(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 //////////////////////////////////////////////////////////////////////////////// | 66 //////////////////////////////////////////////////////////////////////////////// |
| 67 // ImageButton, View overrides: | 67 // ImageButton, View overrides: |
| 68 | 68 |
| 69 gfx::Size ImageButton::GetPreferredSize() { | 69 gfx::Size ImageButton::GetPreferredSize() { |
| 70 if (!images_[BS_NORMAL].isNull()) | 70 if (!images_[BS_NORMAL].isNull()) |
| 71 return gfx::Size(images_[BS_NORMAL].width(), images_[BS_NORMAL].height()); | 71 return gfx::Size(images_[BS_NORMAL].width(), images_[BS_NORMAL].height()); |
| 72 return preferred_size_; | 72 return preferred_size_; |
| 73 } | 73 } |
| 74 | 74 |
| 75 void ImageButton::OnPaint(gfx::Canvas* canvas) { | 75 void ImageButton::OnPaint(gfx::Canvas* canvas) { |
| 76 // Call the base class first to paint any background/borders. | 76 // Call the base class first to paint any background/borders. |
| 77 View::OnPaint(canvas); | 77 View::OnPaint(canvas); |
| 78 | 78 |
| 79 SkBitmap img = GetImageToPaint(); | 79 gfx::ImageSkia img = GetImageToPaint(); |
| 80 | 80 |
| 81 if (!img.isNull()) { | 81 if (!img.isNull()) { |
| 82 int x = 0, y = 0; | 82 int x = 0, y = 0; |
| 83 | 83 |
| 84 if (h_alignment_ == ALIGN_CENTER) | 84 if (h_alignment_ == ALIGN_CENTER) |
| 85 x = (width() - img.width()) / 2; | 85 x = (width() - img.width()) / 2; |
| 86 else if (h_alignment_ == ALIGN_RIGHT) | 86 else if (h_alignment_ == ALIGN_RIGHT) |
| 87 x = width() - img.width(); | 87 x = width() - img.width(); |
| 88 | 88 |
| 89 if (v_alignment_ == ALIGN_MIDDLE) | 89 if (v_alignment_ == ALIGN_MIDDLE) |
| 90 y = (height() - img.height()) / 2; | 90 y = (height() - img.height()) / 2; |
| 91 else if (v_alignment_ == ALIGN_BOTTOM) | 91 else if (v_alignment_ == ALIGN_BOTTOM) |
| 92 y = height() - img.height(); | 92 y = height() - img.height(); |
| 93 | 93 |
| 94 if (!background_image_.empty()) | 94 if (!background_image_.empty()) |
| 95 canvas->DrawBitmapInt(background_image_, x, y); | 95 canvas->DrawBitmapInt(background_image_, x, y); |
| 96 | 96 |
| 97 canvas->DrawBitmapInt(img, x, y); | 97 canvas->DrawBitmapInt(img, x, y); |
| 98 | 98 |
| 99 if (!overlay_image_.empty()) | 99 if (!overlay_image_.empty()) |
| 100 canvas->DrawBitmapInt(overlay_image_, x, y); | 100 canvas->DrawBitmapInt(overlay_image_, x, y); |
| 101 } | 101 } |
| 102 OnPaintFocusBorder(canvas); | 102 OnPaintFocusBorder(canvas); |
| 103 } | 103 } |
| 104 | 104 |
| 105 //////////////////////////////////////////////////////////////////////////////// | 105 //////////////////////////////////////////////////////////////////////////////// |
| 106 // ImageButton, protected: | 106 // ImageButton, protected: |
| 107 | 107 |
| 108 SkBitmap ImageButton::GetImageToPaint() { | 108 gfx::ImageSkia ImageButton::GetImageToPaint() { |
| 109 SkBitmap img; | 109 gfx::ImageSkia img; |
| 110 | 110 |
| 111 if (!images_[BS_HOT].isNull() && hover_animation_->is_animating()) { | 111 if (!images_[BS_HOT].isNull() && hover_animation_->is_animating()) { |
| 112 img = SkBitmapOperations::CreateBlendedBitmap(images_[BS_NORMAL], | 112 img = SkBitmapOperations::CreateBlendedBitmap(images_[BS_NORMAL], |
| 113 images_[BS_HOT], hover_animation_->GetCurrentValue()); | 113 images_[BS_HOT], hover_animation_->GetCurrentValue()); |
| 114 } else { | 114 } else { |
| 115 img = images_[state_]; | 115 img = images_[state_]; |
| 116 } | 116 } |
| 117 | 117 |
| 118 return !img.isNull() ? img : images_[BS_NORMAL]; | 118 return !img.isNull() ? img : images_[BS_NORMAL]; |
| 119 } | 119 } |
| 120 | 120 |
| 121 //////////////////////////////////////////////////////////////////////////////// | 121 //////////////////////////////////////////////////////////////////////////////// |
| 122 // ToggleImageButton, public: | 122 // ToggleImageButton, public: |
| 123 | 123 |
| 124 ToggleImageButton::ToggleImageButton(ButtonListener* listener) | 124 ToggleImageButton::ToggleImageButton(ButtonListener* listener) |
| 125 : ImageButton(listener), | 125 : ImageButton(listener), |
| 126 toggled_(false) { | 126 toggled_(false) { |
| 127 } | 127 } |
| 128 | 128 |
| 129 ToggleImageButton::~ToggleImageButton() { | 129 ToggleImageButton::~ToggleImageButton() { |
| 130 } | 130 } |
| 131 | 131 |
| 132 void ToggleImageButton::SetToggled(bool toggled) { | 132 void ToggleImageButton::SetToggled(bool toggled) { |
| 133 if (toggled == toggled_) | 133 if (toggled == toggled_) |
| 134 return; | 134 return; |
| 135 | 135 |
| 136 for (int i = 0; i < BS_COUNT; ++i) { | 136 for (int i = 0; i < BS_COUNT; ++i) { |
| 137 SkBitmap temp = images_[i]; | 137 gfx::ImageSkia temp = images_[i]; |
| 138 images_[i] = alternate_images_[i]; | 138 images_[i] = alternate_images_[i]; |
| 139 alternate_images_[i] = temp; | 139 alternate_images_[i] = temp; |
| 140 } | 140 } |
| 141 toggled_ = toggled; | 141 toggled_ = toggled; |
| 142 SchedulePaint(); | 142 SchedulePaint(); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void ToggleImageButton::SetToggledImage(ButtonState state, | 145 void ToggleImageButton::SetToggledImage(ButtonState state, |
| 146 const gfx::ImageSkia* image) { | 146 const gfx::ImageSkia* image) { |
| 147 if (toggled_) { | 147 if (toggled_) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 178 bool ToggleImageButton::GetTooltipText(const gfx::Point& p, | 178 bool ToggleImageButton::GetTooltipText(const gfx::Point& p, |
| 179 string16* tooltip) const { | 179 string16* tooltip) const { |
| 180 if (!toggled_ || toggled_tooltip_text_.empty()) | 180 if (!toggled_ || toggled_tooltip_text_.empty()) |
| 181 return Button::GetTooltipText(p, tooltip); | 181 return Button::GetTooltipText(p, tooltip); |
| 182 | 182 |
| 183 *tooltip = toggled_tooltip_text_; | 183 *tooltip = toggled_tooltip_text_; |
| 184 return true; | 184 return true; |
| 185 } | 185 } |
| 186 | 186 |
| 187 } // namespace views | 187 } // namespace views |
| OLD | NEW |