Add ICU message format support
[chromium-blink-merge.git] / ui / views / controls / image_view.cc
blobef496e6dab00f2d785cb1480a1500dcc027de6f7
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/views/painter.h"
15 namespace views {
17 namespace {
19 // Returns the pixels for the bitmap in |image| at scale |image_scale|.
20 void* GetBitmapPixels(const gfx::ImageSkia& img, float image_scale) {
21 DCHECK_NE(0.0f, image_scale);
22 const SkBitmap& bitmap = img.GetRepresentation(image_scale).sk_bitmap();
23 SkAutoLockPixels pixel_lock(bitmap);
24 return bitmap.getPixels();
27 } // namespace
29 // static
30 const char ImageView::kViewClassName[] = "ImageView";
32 ImageView::ImageView()
33 : image_size_set_(false),
34 horiz_alignment_(CENTER),
35 vert_alignment_(CENTER),
36 interactive_(true),
37 last_paint_scale_(0.f),
38 last_painted_bitmap_pixels_(NULL),
39 focus_painter_(Painter::CreateDashedFocusPainter()) {
42 ImageView::~ImageView() {
45 void ImageView::SetImage(const gfx::ImageSkia& img) {
46 if (IsImageEqual(img))
47 return;
49 last_painted_bitmap_pixels_ = NULL;
50 gfx::Size pref_size(GetPreferredSize());
51 image_ = img;
52 if (pref_size != GetPreferredSize())
53 PreferredSizeChanged();
54 SchedulePaint();
57 void ImageView::SetImage(const gfx::ImageSkia* image_skia) {
58 if (image_skia) {
59 SetImage(*image_skia);
60 } else {
61 gfx::ImageSkia t;
62 SetImage(t);
66 const gfx::ImageSkia& ImageView::GetImage() {
67 return image_;
70 void ImageView::SetImageSize(const gfx::Size& image_size) {
71 image_size_set_ = true;
72 image_size_ = image_size;
73 PreferredSizeChanged();
76 gfx::Rect ImageView::GetImageBounds() const {
77 gfx::Size image_size = GetImageSize();
78 return gfx::Rect(ComputeImageOrigin(image_size), image_size);
81 void ImageView::ResetImageSize() {
82 image_size_set_ = false;
85 void ImageView::SetFocusPainter(scoped_ptr<Painter> focus_painter) {
86 focus_painter_ = focus_painter.Pass();
89 gfx::Size ImageView::GetPreferredSize() const {
90 gfx::Size size = GetImageSize();
91 size.Enlarge(GetInsets().width(), GetInsets().height());
92 return size;
95 bool ImageView::IsImageEqual(const gfx::ImageSkia& img) const {
96 // Even though we copy ImageSkia in SetImage() the backing store
97 // (ImageSkiaStorage) is not copied and may have changed since the last call
98 // to SetImage(). The expectation is that SetImage() with different pixels is
99 // treated as though the image changed. For this reason we compare not only
100 // the backing store but also the pixels of the last image we painted.
101 return image_.BackedBySameObjectAs(img) &&
102 last_paint_scale_ != 0.0f &&
103 last_painted_bitmap_pixels_ == GetBitmapPixels(img, last_paint_scale_);
106 gfx::Size ImageView::GetImageSize() const {
107 return image_size_set_ ? image_size_ : image_.size();
110 gfx::Point ImageView::ComputeImageOrigin(const gfx::Size& image_size) const {
111 gfx::Insets insets = GetInsets();
113 int x;
114 // In order to properly handle alignment of images in RTL locales, we need
115 // to flip the meaning of trailing and leading. For example, if the
116 // horizontal alignment is set to trailing, then we'll use left alignment for
117 // the image instead of right alignment if the UI layout is RTL.
118 Alignment actual_horiz_alignment = horiz_alignment_;
119 if (base::i18n::IsRTL() && (horiz_alignment_ != CENTER))
120 actual_horiz_alignment = (horiz_alignment_ == LEADING) ? TRAILING : LEADING;
121 switch (actual_horiz_alignment) {
122 case LEADING: x = insets.left(); break;
123 case TRAILING: x = width() - insets.right() - image_size.width(); break;
124 case CENTER: x = (width() - image_size.width()) / 2; break;
125 default: NOTREACHED(); x = 0; break;
128 int y;
129 switch (vert_alignment_) {
130 case LEADING: y = insets.top(); break;
131 case TRAILING: y = height() - insets.bottom() - image_size.height(); break;
132 case CENTER: y = (height() - image_size.height()) / 2; break;
133 default: NOTREACHED(); y = 0; break;
136 return gfx::Point(x, y);
139 void ImageView::OnFocus() {
140 View::OnFocus();
141 if (focus_painter_.get())
142 SchedulePaint();
145 void ImageView::OnBlur() {
146 View::OnBlur();
147 if (focus_painter_.get())
148 SchedulePaint();
151 void ImageView::OnPaint(gfx::Canvas* canvas) {
152 View::OnPaint(canvas);
153 OnPaintImage(canvas);
154 Painter::PaintFocusPainter(this, canvas, focus_painter_.get());
157 void ImageView::GetAccessibleState(ui::AXViewState* state) {
158 state->role = ui::AX_ROLE_IMAGE;
159 state->name = tooltip_text_;
162 const char* ImageView::GetClassName() const {
163 return kViewClassName;
166 void ImageView::SetHorizontalAlignment(Alignment ha) {
167 if (ha != horiz_alignment_) {
168 horiz_alignment_ = ha;
169 SchedulePaint();
173 ImageView::Alignment ImageView::GetHorizontalAlignment() const {
174 return horiz_alignment_;
177 void ImageView::SetVerticalAlignment(Alignment va) {
178 if (va != vert_alignment_) {
179 vert_alignment_ = va;
180 SchedulePaint();
184 ImageView::Alignment ImageView::GetVerticalAlignment() const {
185 return vert_alignment_;
188 void ImageView::SetTooltipText(const base::string16& tooltip) {
189 tooltip_text_ = tooltip;
192 base::string16 ImageView::GetTooltipText() const {
193 return tooltip_text_;
196 bool ImageView::GetTooltipText(const gfx::Point& p,
197 base::string16* tooltip) const {
198 if (tooltip_text_.empty())
199 return false;
201 *tooltip = GetTooltipText();
202 return true;
205 bool ImageView::CanProcessEventsWithinSubtree() const {
206 return interactive_;
209 void ImageView::OnPaintImage(gfx::Canvas* canvas) {
210 last_paint_scale_ = canvas->image_scale();
211 last_painted_bitmap_pixels_ = NULL;
213 if (image_.isNull())
214 return;
216 gfx::Rect image_bounds(GetImageBounds());
217 if (image_bounds.IsEmpty())
218 return;
220 if (image_bounds.size() != gfx::Size(image_.width(), image_.height())) {
221 // Resize case
222 SkPaint paint;
223 paint.setFilterQuality(kLow_SkFilterQuality);
224 canvas->DrawImageInt(image_, 0, 0, image_.width(), image_.height(),
225 image_bounds.x(), image_bounds.y(), image_bounds.width(),
226 image_bounds.height(), true, paint);
227 } else {
228 canvas->DrawImageInt(image_, image_bounds.x(), image_bounds.y());
230 last_painted_bitmap_pixels_ = GetBitmapPixels(image_, last_paint_scale_);
233 } // namespace views