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 background_image_ = gfx::ImageSkia(); |
46 background_image_.src_mask_ = mask ? *mask : gfx::ImageSkia(); | 45 return; |
47 background_image_.result_ = gfx::ImageSkia(); | 46 } |
| 47 |
| 48 background_image_ = gfx::ImageSkiaOperations::CreateButtonBackground(color, |
| 49 *image, *mask); |
48 } | 50 } |
49 | 51 |
50 void ImageButton::SetOverlayImage(const gfx::ImageSkia* image) { | 52 void ImageButton::SetOverlayImage(const gfx::ImageSkia* image) { |
51 if (!image) { | 53 if (!image) { |
52 overlay_image_ = gfx::ImageSkia(); | 54 overlay_image_ = gfx::ImageSkia(); |
53 return; | 55 return; |
54 } | 56 } |
55 overlay_image_ = *image; | 57 overlay_image_ = *image; |
56 } | 58 } |
57 | 59 |
(...skipping 10 matching lines...) Expand all Loading... |
68 gfx::Size ImageButton::GetPreferredSize() { | 70 gfx::Size ImageButton::GetPreferredSize() { |
69 if (!images_[BS_NORMAL].isNull()) | 71 if (!images_[BS_NORMAL].isNull()) |
70 return gfx::Size(images_[BS_NORMAL].width(), images_[BS_NORMAL].height()); | 72 return gfx::Size(images_[BS_NORMAL].width(), images_[BS_NORMAL].height()); |
71 return preferred_size_; | 73 return preferred_size_; |
72 } | 74 } |
73 | 75 |
74 void ImageButton::OnPaint(gfx::Canvas* canvas) { | 76 void ImageButton::OnPaint(gfx::Canvas* canvas) { |
75 // Call the base class first to paint any background/borders. | 77 // Call the base class first to paint any background/borders. |
76 View::OnPaint(canvas); | 78 View::OnPaint(canvas); |
77 | 79 |
78 ui::ScaleFactor current_device_scale_factor = canvas->scale_factor(); | 80 gfx::ImageSkia img = GetImageToPaint(); |
79 gfx::ImageSkia img = GetImageToPaint(current_device_scale_factor); | |
80 | 81 |
81 if (!img.isNull()) { | 82 if (!img.isNull()) { |
82 int x = 0, y = 0; | 83 int x = 0, y = 0; |
83 | 84 |
84 if (h_alignment_ == ALIGN_CENTER) | 85 if (h_alignment_ == ALIGN_CENTER) |
85 x = (width() - img.width()) / 2; | 86 x = (width() - img.width()) / 2; |
86 else if (h_alignment_ == ALIGN_RIGHT) | 87 else if (h_alignment_ == ALIGN_RIGHT) |
87 x = width() - img.width(); | 88 x = width() - img.width(); |
88 | 89 |
89 if (v_alignment_ == ALIGN_MIDDLE) | 90 if (v_alignment_ == ALIGN_MIDDLE) |
90 y = (height() - img.height()) / 2; | 91 y = (height() - img.height()) / 2; |
91 else if (v_alignment_ == ALIGN_BOTTOM) | 92 else if (v_alignment_ == ALIGN_BOTTOM) |
92 y = height() - img.height(); | 93 y = height() - img.height(); |
93 | 94 |
94 if (!background_image_.result_.HasRepresentation( | 95 if (!background_image_.isNull()) |
95 current_device_scale_factor)) { | 96 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 | 97 |
101 canvas->DrawImageInt(img, x, y); | 98 canvas->DrawImageInt(img, x, y); |
102 | 99 |
103 if (!overlay_image_.empty()) | 100 if (!overlay_image_.empty()) |
104 canvas->DrawImageInt(overlay_image_, x, y); | 101 canvas->DrawImageInt(overlay_image_, x, y); |
105 } | 102 } |
106 OnPaintFocusBorder(canvas); | 103 OnPaintFocusBorder(canvas); |
107 } | 104 } |
108 | 105 |
109 //////////////////////////////////////////////////////////////////////////////// | 106 //////////////////////////////////////////////////////////////////////////////// |
110 // ImageButton, protected: | 107 // ImageButton, protected: |
111 | 108 |
112 gfx::ImageSkia ImageButton::GetImageToPaint(ui::ScaleFactor scale_factor) { | 109 gfx::ImageSkia ImageButton::GetImageToPaint() { |
113 gfx::ImageSkia img; | 110 gfx::ImageSkia img; |
114 | 111 |
115 if (!images_[BS_HOT].isNull() && hover_animation_->is_animating()) { | 112 if (!images_[BS_HOT].isNull() && hover_animation_->is_animating()) { |
116 gfx::ImageSkiaRep normal_image_rep = images_[BS_NORMAL].GetRepresentation( | 113 img = gfx::ImageSkiaOperations::CreateBlendedImage(images_[BS_NORMAL], |
117 scale_factor); | 114 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 { | 115 } else { |
128 img = images_[state_]; | 116 img = images_[state_]; |
129 } | 117 } |
130 | 118 |
131 return !img.isNull() ? img : images_[BS_NORMAL]; | 119 return !img.isNull() ? img : images_[BS_NORMAL]; |
132 } | 120 } |
133 | 121 |
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 //////////////////////////////////////////////////////////////////////////////// | 122 //////////////////////////////////////////////////////////////////////////////// |
153 // ToggleImageButton, public: | 123 // ToggleImageButton, public: |
154 | 124 |
155 ToggleImageButton::ToggleImageButton(ButtonListener* listener) | 125 ToggleImageButton::ToggleImageButton(ButtonListener* listener) |
156 : ImageButton(listener), | 126 : ImageButton(listener), |
157 toggled_(false) { | 127 toggled_(false) { |
158 } | 128 } |
159 | 129 |
160 ToggleImageButton::~ToggleImageButton() { | 130 ToggleImageButton::~ToggleImageButton() { |
161 } | 131 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 178 |
209 bool ToggleImageButton::GetTooltipText(const gfx::Point& p, | 179 bool ToggleImageButton::GetTooltipText(const gfx::Point& p, |
210 string16* tooltip) const { | 180 string16* tooltip) const { |
211 if (!toggled_ || toggled_tooltip_text_.empty()) | 181 if (!toggled_ || toggled_tooltip_text_.empty()) |
212 return Button::GetTooltipText(p, tooltip); | 182 return Button::GetTooltipText(p, tooltip); |
213 | 183 |
214 *tooltip = toggled_tooltip_text_; | 184 *tooltip = toggled_tooltip_text_; |
215 return true; | 185 return true; |
216 } | 186 } |
217 | 187 |
218 /////////////////////////////////////////////////////////////////////////////// | |
219 // struct BackgroundImageGenerationInfo | |
220 ImageButton::BackgroundImageGenerationInfo::BackgroundImageGenerationInfo() | |
221 : src_color_(0) { | |
222 } | |
223 | |
224 ImageButton::BackgroundImageGenerationInfo::~BackgroundImageGenerationInfo() { | |
225 } | |
226 | |
227 } // namespace views | 188 } // namespace views |
OLD | NEW |