Split vector icon commands into separate files aggregated at build time.
[chromium-blink-merge.git] / ui / views / controls / image_view.cc
blob70dd49a8d7b76c89bdf35296ee97e38043cf7dab
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"
16 namespace views {
18 namespace {
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();
28 } // namespace
30 // static
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),
39 interactive_(true),
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))
50 return;
52 last_painted_bitmap_pixels_ = NULL;
53 gfx::Size pref_size(GetPreferredSize());
54 image_ = img;
55 if (pref_size != GetPreferredSize())
56 PreferredSizeChanged();
57 SchedulePaint();
60 void ImageView::SetImage(const gfx::ImageSkia* image_skia) {
61 if (image_skia) {
62 SetImage(*image_skia);
63 } else {
64 gfx::ImageSkia t;
65 SetImage(t);
69 const gfx::ImageSkia& ImageView::GetImage() {
70 return image_;
73 void ImageView::SetVectorIcon(gfx::VectorIconId id,
74 SkColor color,
75 const gfx::Size& image_size) {
76 SetImageSize(image_size);
77 vector_id_ = id;
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());
106 return image_size;
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();
126 int x;
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;
141 int y;
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() {
153 View::OnFocus();
154 if (focus_painter_.get())
155 SchedulePaint();
158 void ImageView::OnBlur() {
159 View::OnBlur();
160 if (focus_painter_.get())
161 SchedulePaint();
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;
183 SchedulePaint();
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;
194 SchedulePaint();
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())
213 return false;
215 *tooltip = GetTooltipText();
216 return true;
219 bool ImageView::CanProcessEventsWithinSubtree() const {
220 return interactive_;
223 void ImageView::OnPaintImage(gfx::Canvas* canvas) {
224 last_paint_scale_ = canvas->image_scale();
225 last_painted_bitmap_pixels_ = NULL;
227 if (image_.isNull())
228 return;
230 gfx::Rect image_bounds(GetImageBounds());
231 if (image_bounds.IsEmpty())
232 return;
234 if (image_bounds.size() != gfx::Size(image_.width(), image_.height())) {
235 // Resize case
236 SkPaint paint;
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);
241 } else {
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)
249 return;
251 DCHECK(image_size_set_);
252 canvas->Translate(ComputeImageOrigin(image_size_).OffsetFromOrigin());
253 gfx::PaintVectorIcon(canvas, vector_id_, image_size_.width(), vector_color_);
256 } // namespace views