| 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/screen.h" | 10 #include "ui/gfx/image/image_skia_operations.h" |
| 11 #include "ui/gfx/skbitmap_operations.h" | |
| 12 #include "ui/views/widget/widget.h" | 11 #include "ui/views/widget/widget.h" |
| 13 | 12 |
| 14 namespace views { | 13 namespace views { |
| 15 | 14 |
| 16 static const int kDefaultWidth = 16; // Default button width if no theme. | 15 static const int kDefaultWidth = 16; // Default button width if no theme. |
| 17 static const int kDefaultHeight = 14; // Default button height if no theme. | 16 static const int kDefaultHeight = 14; // Default button height if no theme. |
| 18 | 17 |
| 19 //////////////////////////////////////////////////////////////////////////////// | 18 //////////////////////////////////////////////////////////////////////////////// |
| 20 // ImageButton, public: | 19 // ImageButton, public: |
| 21 | 20 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 34 } | 33 } |
| 35 | 34 |
| 36 void ImageButton::SetImage(ButtonState state, const gfx::ImageSkia* image) { | 35 void ImageButton::SetImage(ButtonState state, const gfx::ImageSkia* image) { |
| 37 images_[state] = image ? *image : gfx::ImageSkia(); | 36 images_[state] = image ? *image : gfx::ImageSkia(); |
| 38 PreferredSizeChanged(); | 37 PreferredSizeChanged(); |
| 39 } | 38 } |
| 40 | 39 |
| 41 void ImageButton::SetBackground(SkColor color, | 40 void ImageButton::SetBackground(SkColor color, |
| 42 const gfx::ImageSkia* image, | 41 const gfx::ImageSkia* image, |
| 43 const gfx::ImageSkia* mask) { | 42 const gfx::ImageSkia* mask) { |
| 44 background_image_.src_color_ = color; | 43 if (image == NULL || mask == NULL) |
| 45 background_image_.src_image_ = image ? *image : gfx::ImageSkia(); | 44 return; |
| 46 background_image_.src_mask_ = mask ? *mask : gfx::ImageSkia(); | 45 |
| 47 background_image_.result_ = gfx::ImageSkia(); | 46 background_image_ = gfx::ImageSkiaOperations::CreateButtonBackground(color, |
| 47 *image, *mask); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void ImageButton::SetOverlayImage(const gfx::ImageSkia* image) { | 50 void ImageButton::SetOverlayImage(const gfx::ImageSkia* image) { |
| 51 if (!image) { | 51 if (!image) { |
| 52 overlay_image_ = gfx::ImageSkia(); | 52 overlay_image_ = gfx::ImageSkia(); |
| 53 return; | 53 return; |
| 54 } | 54 } |
| 55 overlay_image_ = *image; | 55 overlay_image_ = *image; |
| 56 } | 56 } |
| 57 | 57 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 68 gfx::Size ImageButton::GetPreferredSize() { | 68 gfx::Size ImageButton::GetPreferredSize() { |
| 69 if (!images_[BS_NORMAL].isNull()) | 69 if (!images_[BS_NORMAL].isNull()) |
| 70 return gfx::Size(images_[BS_NORMAL].width(), images_[BS_NORMAL].height()); | 70 return gfx::Size(images_[BS_NORMAL].width(), images_[BS_NORMAL].height()); |
| 71 return preferred_size_; | 71 return preferred_size_; |
| 72 } | 72 } |
| 73 | 73 |
| 74 void ImageButton::OnPaint(gfx::Canvas* canvas) { | 74 void ImageButton::OnPaint(gfx::Canvas* canvas) { |
| 75 // Call the base class first to paint any background/borders. | 75 // Call the base class first to paint any background/borders. |
| 76 View::OnPaint(canvas); | 76 View::OnPaint(canvas); |
| 77 | 77 |
| 78 ui::ScaleFactor current_device_scale_factor = canvas->scale_factor(); | 78 gfx::ImageSkia img = GetImageToPaint(); |
| 79 gfx::ImageSkia img = GetImageToPaint(current_device_scale_factor); | |
| 80 | 79 |
| 81 if (!img.isNull()) { | 80 if (!img.isNull()) { |
| 82 int x = 0, y = 0; | 81 int x = 0, y = 0; |
| 83 | 82 |
| 84 if (h_alignment_ == ALIGN_CENTER) | 83 if (h_alignment_ == ALIGN_CENTER) |
| 85 x = (width() - img.width()) / 2; | 84 x = (width() - img.width()) / 2; |
| 86 else if (h_alignment_ == ALIGN_RIGHT) | 85 else if (h_alignment_ == ALIGN_RIGHT) |
| 87 x = width() - img.width(); | 86 x = width() - img.width(); |
| 88 | 87 |
| 89 if (v_alignment_ == ALIGN_MIDDLE) | 88 if (v_alignment_ == ALIGN_MIDDLE) |
| 90 y = (height() - img.height()) / 2; | 89 y = (height() - img.height()) / 2; |
| 91 else if (v_alignment_ == ALIGN_BOTTOM) | 90 else if (v_alignment_ == ALIGN_BOTTOM) |
| 92 y = height() - img.height(); | 91 y = height() - img.height(); |
| 93 | 92 |
| 94 if (!background_image_.result_.HasRepresentation( | 93 if (!background_image_.isNull()) |
| 95 current_device_scale_factor)) { | 94 canvas->DrawImageInt(background_image_, x, y); |
| 96 UpdateButtonBackground(current_device_scale_factor); | |
| 97 } | |
| 98 if (!background_image_.result_.empty()) | |
| 99 canvas->DrawImageInt(background_image_.result_, x, y); | |
| 100 | 95 |
| 101 canvas->DrawImageInt(img, x, y); | 96 canvas->DrawImageInt(img, x, y); |
| 102 | 97 |
| 103 if (!overlay_image_.empty()) | 98 if (!overlay_image_.empty()) |
| 104 canvas->DrawImageInt(overlay_image_, x, y); | 99 canvas->DrawImageInt(overlay_image_, x, y); |
| 105 } | 100 } |
| 106 OnPaintFocusBorder(canvas); | 101 OnPaintFocusBorder(canvas); |
| 107 } | 102 } |
| 108 | 103 |
| 109 //////////////////////////////////////////////////////////////////////////////// | 104 //////////////////////////////////////////////////////////////////////////////// |
| 110 // ImageButton, protected: | 105 // ImageButton, protected: |
| 111 | 106 |
| 112 gfx::ImageSkia ImageButton::GetImageToPaint(ui::ScaleFactor scale_factor) { | 107 gfx::ImageSkia ImageButton::GetImageToPaint() { |
| 113 gfx::ImageSkia img; | 108 gfx::ImageSkia img; |
| 114 | 109 |
| 115 if (!images_[BS_HOT].isNull() && hover_animation_->is_animating()) { | 110 if (!images_[BS_HOT].isNull() && hover_animation_->is_animating()) { |
| 116 gfx::ImageSkiaRep normal_image_rep = images_[BS_NORMAL].GetRepresentation( | 111 img = gfx::ImageSkiaOperations::CreateBlendedImage(images_[BS_NORMAL], |
| 117 scale_factor); | 112 images_[BS_HOT], hover_animation_->GetCurrentValue()); |
| 118 gfx::ImageSkiaRep hot_image_rep = images_[BS_HOT].GetRepresentation( | |
| 119 scale_factor); | |
| 120 DCHECK_EQ(normal_image_rep.scale_factor(), hot_image_rep.scale_factor()); | |
| 121 SkBitmap blended_bitmap = SkBitmapOperations::CreateBlendedBitmap( | |
| 122 normal_image_rep.sk_bitmap(), | |
| 123 hot_image_rep.sk_bitmap(), | |
| 124 hover_animation_->GetCurrentValue()); | |
| 125 img = gfx::ImageSkia(gfx::ImageSkiaRep(blended_bitmap, | |
| 126 normal_image_rep.scale_factor())); | |
| 127 } else { | 113 } else { |
| 128 img = images_[state_]; | 114 img = images_[state_]; |
| 129 } | 115 } |
| 130 | 116 |
| 131 return !img.isNull() ? img : images_[BS_NORMAL]; | 117 return !img.isNull() ? img : images_[BS_NORMAL]; |
| 132 } | 118 } |
| 133 | 119 |
| 134 void ImageButton::UpdateButtonBackground(ui::ScaleFactor scale_factor) { | |
| 135 gfx::ImageSkiaRep image_rep = | |
| 136 background_image_.src_image_.GetRepresentation(scale_factor); | |
| 137 gfx::ImageSkiaRep mask_image_rep = | |
| 138 background_image_.src_mask_.GetRepresentation(scale_factor); | |
| 139 if (image_rep.is_null() || mask_image_rep.is_null() || | |
| 140 background_image_.result_.HasRepresentation(image_rep.scale_factor())) { | |
| 141 return; | |
| 142 } | |
| 143 DCHECK_EQ(image_rep.scale_factor(), mask_image_rep.scale_factor()); | |
| 144 SkBitmap result = SkBitmapOperations::CreateButtonBackground( | |
| 145 background_image_.src_color_, | |
| 146 image_rep.sk_bitmap(), | |
| 147 mask_image_rep.sk_bitmap()); | |
| 148 background_image_.result_.AddRepresentation(gfx::ImageSkiaRep( | |
| 149 result, image_rep.scale_factor())); | |
| 150 } | |
| 151 | |
| 152 //////////////////////////////////////////////////////////////////////////////// | 120 //////////////////////////////////////////////////////////////////////////////// |
| 153 // ToggleImageButton, public: | 121 // ToggleImageButton, public: |
| 154 | 122 |
| 155 ToggleImageButton::ToggleImageButton(ButtonListener* listener) | 123 ToggleImageButton::ToggleImageButton(ButtonListener* listener) |
| 156 : ImageButton(listener), | 124 : ImageButton(listener), |
| 157 toggled_(false) { | 125 toggled_(false) { |
| 158 } | 126 } |
| 159 | 127 |
| 160 ToggleImageButton::~ToggleImageButton() { | 128 ToggleImageButton::~ToggleImageButton() { |
| 161 } | 129 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 | 176 |
| 209 bool ToggleImageButton::GetTooltipText(const gfx::Point& p, | 177 bool ToggleImageButton::GetTooltipText(const gfx::Point& p, |
| 210 string16* tooltip) const { | 178 string16* tooltip) const { |
| 211 if (!toggled_ || toggled_tooltip_text_.empty()) | 179 if (!toggled_ || toggled_tooltip_text_.empty()) |
| 212 return Button::GetTooltipText(p, tooltip); | 180 return Button::GetTooltipText(p, tooltip); |
| 213 | 181 |
| 214 *tooltip = toggled_tooltip_text_; | 182 *tooltip = toggled_tooltip_text_; |
| 215 return true; | 183 return true; |
| 216 } | 184 } |
| 217 | 185 |
| 218 /////////////////////////////////////////////////////////////////////////////// | |
| 219 // struct BackgroundImageGenerationInfo | |
| 220 ImageButton::BackgroundImageGenerationInfo::BackgroundImageGenerationInfo() | |
| 221 : src_color_(0) { | |
| 222 } | |
| 223 | |
| 224 ImageButton::BackgroundImageGenerationInfo::~BackgroundImageGenerationInfo() { | |
| 225 } | |
| 226 | |
| 227 } // namespace views | 186 } // namespace views |
| OLD | NEW |