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
3 // found in the LICENSE file.
5 #include "ui/views/controls/image_view.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "third_party/skia/include/core/SkPaint.h"
10 #include "ui/accessibility/ax_view_state.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/geometry/insets.h"
13 #include "ui/gfx/paint_vector_icon.h"
14 #include "ui/views/painter.h"
20 // Returns the pixels for the bitmap in |image| at scale |image_scale|.
21 void* GetBitmapPixels(const gfx::ImageSkia
& img
, float image_scale
) {
22 DCHECK_NE(0.0f
, image_scale
);
23 const SkBitmap
& bitmap
= img
.GetRepresentation(image_scale
).sk_bitmap();
24 SkAutoLockPixels
pixel_lock(bitmap
);
25 return bitmap
.getPixels();
31 const char ImageView::kViewClassName
[] = "ImageView";
33 ImageView::ImageView()
34 : image_size_set_(false),
35 vector_id_(gfx::VectorIconId::VECTOR_ICON_NONE
),
36 vector_color_(SK_ColorGREEN
),
37 horiz_alignment_(CENTER
),
38 vert_alignment_(CENTER
),
40 last_paint_scale_(0.f
),
41 last_painted_bitmap_pixels_(NULL
),
42 focus_painter_(Painter::CreateDashedFocusPainter()) {
45 ImageView::~ImageView() {
48 void ImageView::SetImage(const gfx::ImageSkia
& img
) {
49 if (IsImageEqual(img
))
52 last_painted_bitmap_pixels_
= NULL
;
53 gfx::Size
pref_size(GetPreferredSize());
55 if (pref_size
!= GetPreferredSize())
56 PreferredSizeChanged();
60 void ImageView::SetImage(const gfx::ImageSkia
* image_skia
) {
62 SetImage(*image_skia
);
69 const gfx::ImageSkia
& ImageView::GetImage() {
73 void ImageView::SetVectorIcon(gfx::VectorIconId id
,
75 const gfx::Size
& image_size
) {
76 SetImageSize(image_size
);
78 vector_color_
= color
;
81 void ImageView::SetImageSize(const gfx::Size
& image_size
) {
82 image_size_set_
= true;
83 image_size_
= image_size
;
84 PreferredSizeChanged();
87 gfx::Rect
ImageView::GetImageBounds() const {
88 gfx::Size
image_size(image_size_set_
?
89 image_size_
: gfx::Size(image_
.width(), image_
.height()));
90 return gfx::Rect(ComputeImageOrigin(image_size
), image_size
);
93 void ImageView::ResetImageSize() {
94 image_size_set_
= false;
97 void ImageView::SetFocusPainter(scoped_ptr
<Painter
> focus_painter
) {
98 focus_painter_
= focus_painter
.Pass();
101 gfx::Size
ImageView::GetPreferredSize() const {
102 gfx::Insets insets
= GetInsets();
103 if (image_size_set_
) {
104 gfx::Size image_size
= image_size_
;
105 image_size
.Enlarge(insets
.width(), insets
.height());
108 return gfx::Size(image_
.width() + insets
.width(),
109 image_
.height() + insets
.height());
112 bool ImageView::IsImageEqual(const gfx::ImageSkia
& img
) const {
113 // Even though we copy ImageSkia in SetImage() the backing store
114 // (ImageSkiaStorage) is not copied and may have changed since the last call
115 // to SetImage(). The expectation is that SetImage() with different pixels is
116 // treated as though the image changed. For this reason we compare not only
117 // the backing store but also the pixels of the last image we painted.
118 return image_
.BackedBySameObjectAs(img
) &&
119 last_paint_scale_
!= 0.0f
&&
120 last_painted_bitmap_pixels_
== GetBitmapPixels(img
, last_paint_scale_
);
123 gfx::Point
ImageView::ComputeImageOrigin(const gfx::Size
& image_size
) const {
124 gfx::Insets insets
= GetInsets();
127 // In order to properly handle alignment of images in RTL locales, we need
128 // to flip the meaning of trailing and leading. For example, if the
129 // horizontal alignment is set to trailing, then we'll use left alignment for
130 // the image instead of right alignment if the UI layout is RTL.
131 Alignment actual_horiz_alignment
= horiz_alignment_
;
132 if (base::i18n::IsRTL() && (horiz_alignment_
!= CENTER
))
133 actual_horiz_alignment
= (horiz_alignment_
== LEADING
) ? TRAILING
: LEADING
;
134 switch (actual_horiz_alignment
) {
135 case LEADING
: x
= insets
.left(); break;
136 case TRAILING
: x
= width() - insets
.right() - image_size
.width(); break;
137 case CENTER
: x
= (width() - image_size
.width()) / 2; break;
138 default: NOTREACHED(); x
= 0; break;
142 switch (vert_alignment_
) {
143 case LEADING
: y
= insets
.top(); break;
144 case TRAILING
: y
= height() - insets
.bottom() - image_size
.height(); break;
145 case CENTER
: y
= (height() - image_size
.height()) / 2; break;
146 default: NOTREACHED(); y
= 0; break;
149 return gfx::Point(x
, y
);
152 void ImageView::OnFocus() {
154 if (focus_painter_
.get())
158 void ImageView::OnBlur() {
160 if (focus_painter_
.get())
164 void ImageView::OnPaint(gfx::Canvas
* canvas
) {
165 View::OnPaint(canvas
);
166 OnPaintImage(canvas
);
167 OnPaintVectorIcon(canvas
);
168 Painter::PaintFocusPainter(this, canvas
, focus_painter_
.get());
171 void ImageView::GetAccessibleState(ui::AXViewState
* state
) {
172 state
->role
= ui::AX_ROLE_IMAGE
;
173 state
->name
= tooltip_text_
;
176 const char* ImageView::GetClassName() const {
177 return kViewClassName
;
180 void ImageView::SetHorizontalAlignment(Alignment ha
) {
181 if (ha
!= horiz_alignment_
) {
182 horiz_alignment_
= ha
;
187 ImageView::Alignment
ImageView::GetHorizontalAlignment() const {
188 return horiz_alignment_
;
191 void ImageView::SetVerticalAlignment(Alignment va
) {
192 if (va
!= vert_alignment_
) {
193 vert_alignment_
= va
;
198 ImageView::Alignment
ImageView::GetVerticalAlignment() const {
199 return vert_alignment_
;
202 void ImageView::SetTooltipText(const base::string16
& tooltip
) {
203 tooltip_text_
= tooltip
;
206 base::string16
ImageView::GetTooltipText() const {
207 return tooltip_text_
;
210 bool ImageView::GetTooltipText(const gfx::Point
& p
,
211 base::string16
* tooltip
) const {
212 if (tooltip_text_
.empty())
215 *tooltip
= GetTooltipText();
219 bool ImageView::CanProcessEventsWithinSubtree() const {
223 void ImageView::OnPaintImage(gfx::Canvas
* canvas
) {
224 last_paint_scale_
= canvas
->image_scale();
225 last_painted_bitmap_pixels_
= NULL
;
230 gfx::Rect
image_bounds(GetImageBounds());
231 if (image_bounds
.IsEmpty())
234 if (image_bounds
.size() != gfx::Size(image_
.width(), image_
.height())) {
237 paint
.setFilterQuality(kLow_SkFilterQuality
);
238 canvas
->DrawImageInt(image_
, 0, 0, image_
.width(), image_
.height(),
239 image_bounds
.x(), image_bounds
.y(), image_bounds
.width(),
240 image_bounds
.height(), true, paint
);
242 canvas
->DrawImageInt(image_
, image_bounds
.x(), image_bounds
.y());
244 last_painted_bitmap_pixels_
= GetBitmapPixels(image_
, last_paint_scale_
);
247 void ImageView::OnPaintVectorIcon(gfx::Canvas
* canvas
) {
248 if (vector_id_
== gfx::VectorIconId::VECTOR_ICON_NONE
)
251 DCHECK(image_size_set_
);
252 canvas
->Translate(ComputeImageOrigin(image_size_
).OffsetFromOrigin());
253 gfx::PaintVectorIcon(canvas
, vector_id_
, image_size_
.width(), vector_color_
);