| 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/image_view.h" | 5 #include "ui/views/controls/image_view.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "third_party/skia/include/core/SkPaint.h" | 9 #include "third_party/skia/include/core/SkPaint.h" |
| 10 #include "ui/base/accessibility/accessible_view_state.h" | 10 #include "ui/base/accessibility/accessible_view_state.h" |
| 11 #include "ui/gfx/canvas.h" | 11 #include "ui/gfx/canvas.h" |
| 12 #include "ui/gfx/insets.h" | 12 #include "ui/gfx/insets.h" |
| 13 | 13 |
| 14 namespace views { | 14 namespace views { |
| 15 | 15 |
| 16 ImageView::ImageView() | 16 ImageView::ImageView() |
| 17 : image_size_set_(false), | 17 : image_size_set_(false), |
| 18 horiz_alignment_(CENTER), | 18 horiz_alignment_(CENTER), |
| 19 vert_alignment_(CENTER), | 19 vert_alignment_(CENTER), |
| 20 interactive_(true) { | 20 interactive_(true), |
| 21 last_paint_scale_(0.f), |
| 22 last_painted_bitmap_pixels_(NULL) { |
| 21 } | 23 } |
| 22 | 24 |
| 23 ImageView::~ImageView() { | 25 ImageView::~ImageView() { |
| 24 } | 26 } |
| 25 | 27 |
| 26 void ImageView::SetImage(const gfx::ImageSkia& img) { | 28 void ImageView::SetImage(const gfx::ImageSkia& img) { |
| 27 if (image_.BackedBySameObjectAs(img)) | 29 if (IsImageEqual(img)) |
| 28 return; | 30 return; |
| 29 | 31 |
| 32 last_painted_bitmap_pixels_ = NULL; |
| 30 gfx::Size pref_size(GetPreferredSize()); | 33 gfx::Size pref_size(GetPreferredSize()); |
| 31 image_ = img; | 34 image_ = img; |
| 32 if (pref_size != GetPreferredSize()) | 35 if (pref_size != GetPreferredSize()) |
| 33 PreferredSizeChanged(); | 36 PreferredSizeChanged(); |
| 34 SchedulePaint(); | 37 SchedulePaint(); |
| 35 } | 38 } |
| 36 | 39 |
| 37 void ImageView::SetImage(const gfx::ImageSkia* image_skia) { | 40 void ImageView::SetImage(const gfx::ImageSkia* image_skia) { |
| 38 if (image_skia) { | 41 if (image_skia) { |
| 39 SetImage(*image_skia); | 42 SetImage(*image_skia); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 if (image_size_set_) { | 78 if (image_size_set_) { |
| 76 gfx::Size image_size; | 79 gfx::Size image_size; |
| 77 GetImageSize(&image_size); | 80 GetImageSize(&image_size); |
| 78 image_size.Enlarge(insets.width(), insets.height()); | 81 image_size.Enlarge(insets.width(), insets.height()); |
| 79 return image_size; | 82 return image_size; |
| 80 } | 83 } |
| 81 return gfx::Size(image_.width() + insets.width(), | 84 return gfx::Size(image_.width() + insets.width(), |
| 82 image_.height() + insets.height()); | 85 image_.height() + insets.height()); |
| 83 } | 86 } |
| 84 | 87 |
| 88 bool ImageView::IsImageEqual(const gfx::ImageSkia& img) const { |
| 89 // Even though we copy ImageSkia in SetImage() the backing store |
| 90 // (ImageSkiaStorage) is not copied and may have changed since the last call |
| 91 // to SetImage(). The expectation is that SetImage() with different pixels is |
| 92 // treated as though the image changed. For this reason we compare not only |
| 93 // the backing store but also the pixels of the last image we painted. |
| 94 return image_.BackedBySameObjectAs(img) && |
| 95 last_paint_scale_ != 0.0f && |
| 96 last_painted_bitmap_pixels_ == |
| 97 img.GetRepresentation(last_paint_scale_).sk_bitmap().getPixels(); |
| 98 } |
| 99 |
| 85 gfx::Point ImageView::ComputeImageOrigin(const gfx::Size& image_size) const { | 100 gfx::Point ImageView::ComputeImageOrigin(const gfx::Size& image_size) const { |
| 86 gfx::Insets insets = GetInsets(); | 101 gfx::Insets insets = GetInsets(); |
| 87 | 102 |
| 88 int x; | 103 int x; |
| 89 // In order to properly handle alignment of images in RTL locales, we need | 104 // In order to properly handle alignment of images in RTL locales, we need |
| 90 // to flip the meaning of trailing and leading. For example, if the | 105 // to flip the meaning of trailing and leading. For example, if the |
| 91 // horizontal alignment is set to trailing, then we'll use left alignment for | 106 // horizontal alignment is set to trailing, then we'll use left alignment for |
| 92 // the image instead of right alignment if the UI layout is RTL. | 107 // the image instead of right alignment if the UI layout is RTL. |
| 93 Alignment actual_horiz_alignment = horiz_alignment_; | 108 Alignment actual_horiz_alignment = horiz_alignment_; |
| 94 if (base::i18n::IsRTL() && (horiz_alignment_ != CENTER)) | 109 if (base::i18n::IsRTL() && (horiz_alignment_ != CENTER)) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 107 case CENTER: y = (height() - image_size.height()) / 2; break; | 122 case CENTER: y = (height() - image_size.height()) / 2; break; |
| 108 default: NOTREACHED(); y = 0; break; | 123 default: NOTREACHED(); y = 0; break; |
| 109 } | 124 } |
| 110 | 125 |
| 111 return gfx::Point(x, y); | 126 return gfx::Point(x, y); |
| 112 } | 127 } |
| 113 | 128 |
| 114 void ImageView::OnPaint(gfx::Canvas* canvas) { | 129 void ImageView::OnPaint(gfx::Canvas* canvas) { |
| 115 View::OnPaint(canvas); | 130 View::OnPaint(canvas); |
| 116 | 131 |
| 132 last_paint_scale_ = canvas->image_scale(); |
| 133 last_painted_bitmap_pixels_ = NULL; |
| 134 |
| 117 if (image_.isNull()) | 135 if (image_.isNull()) |
| 118 return; | 136 return; |
| 119 | 137 |
| 120 gfx::Rect image_bounds(GetImageBounds()); | 138 gfx::Rect image_bounds(GetImageBounds()); |
| 121 if (image_bounds.IsEmpty()) | 139 if (image_bounds.IsEmpty()) |
| 122 return; | 140 return; |
| 123 | 141 |
| 124 if (image_bounds.size() != gfx::Size(image_.width(), image_.height())) { | 142 if (image_bounds.size() != gfx::Size(image_.width(), image_.height())) { |
| 125 // Resize case | 143 // Resize case |
| 126 SkPaint paint; | 144 SkPaint paint; |
| 127 paint.setFilterBitmap(true); | 145 paint.setFilterBitmap(true); |
| 128 canvas->DrawImageInt(image_, 0, 0, image_.width(), image_.height(), | 146 canvas->DrawImageInt(image_, 0, 0, image_.width(), image_.height(), |
| 129 image_bounds.x(), image_bounds.y(), image_bounds.width(), | 147 image_bounds.x(), image_bounds.y(), image_bounds.width(), |
| 130 image_bounds.height(), true, paint); | 148 image_bounds.height(), true, paint); |
| 131 } else { | 149 } else { |
| 132 canvas->DrawImageInt(image_, image_bounds.x(), image_bounds.y()); | 150 canvas->DrawImageInt(image_, image_bounds.x(), image_bounds.y()); |
| 133 } | 151 } |
| 152 last_painted_bitmap_pixels_ = |
| 153 image_.GetRepresentation(last_paint_scale_).sk_bitmap().getPixels(); |
| 134 } | 154 } |
| 135 | 155 |
| 136 void ImageView::GetAccessibleState(ui::AccessibleViewState* state) { | 156 void ImageView::GetAccessibleState(ui::AccessibleViewState* state) { |
| 137 state->role = ui::AccessibilityTypes::ROLE_GRAPHIC; | 157 state->role = ui::AccessibilityTypes::ROLE_GRAPHIC; |
| 138 state->name = tooltip_text_; | 158 state->name = tooltip_text_; |
| 139 } | 159 } |
| 140 | 160 |
| 141 void ImageView::SetHorizontalAlignment(Alignment ha) { | 161 void ImageView::SetHorizontalAlignment(Alignment ha) { |
| 142 if (ha != horiz_alignment_) { | 162 if (ha != horiz_alignment_) { |
| 143 horiz_alignment_ = ha; | 163 horiz_alignment_ = ha; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 174 | 194 |
| 175 *tooltip = GetTooltipText(); | 195 *tooltip = GetTooltipText(); |
| 176 return true; | 196 return true; |
| 177 } | 197 } |
| 178 | 198 |
| 179 bool ImageView::HitTestRect(const gfx::Rect& rect) const { | 199 bool ImageView::HitTestRect(const gfx::Rect& rect) const { |
| 180 return interactive_ ? View::HitTestRect(rect) : false; | 200 return interactive_ ? View::HitTestRect(rect) : false; |
| 181 } | 201 } |
| 182 | 202 |
| 183 } // namespace views | 203 } // namespace views |
| OLD | NEW |