| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "cc/paint/paint_flags.h" | 11 #include "cc/paint/paint_flags.h" |
| 12 #include "ui/accessibility/ax_node_data.h" | 12 #include "ui/accessibility/ax_node_data.h" |
| 13 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/geometry/insets.h" | 14 #include "ui/gfx/geometry/insets.h" |
| 15 #include "ui/views/painter.h" | |
| 16 | 15 |
| 17 namespace views { | 16 namespace views { |
| 18 | 17 |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 // Returns the pixels for the bitmap in |image| at scale |image_scale|. | 20 // Returns the pixels for the bitmap in |image| at scale |image_scale|. |
| 22 void* GetBitmapPixels(const gfx::ImageSkia& img, float image_scale) { | 21 void* GetBitmapPixels(const gfx::ImageSkia& img, float image_scale) { |
| 23 DCHECK_NE(0.0f, image_scale); | 22 DCHECK_NE(0.0f, image_scale); |
| 24 const SkBitmap& bitmap = img.GetRepresentation(image_scale).sk_bitmap(); | 23 const SkBitmap& bitmap = img.GetRepresentation(image_scale).sk_bitmap(); |
| 25 SkAutoLockPixels pixel_lock(bitmap); | 24 SkAutoLockPixels pixel_lock(bitmap); |
| 26 return bitmap.getPixels(); | 25 return bitmap.getPixels(); |
| 27 } | 26 } |
| 28 | 27 |
| 29 } // namespace | 28 } // namespace |
| 30 | 29 |
| 31 // static | 30 // static |
| 32 const char ImageView::kViewClassName[] = "ImageView"; | 31 const char ImageView::kViewClassName[] = "ImageView"; |
| 33 | 32 |
| 34 ImageView::ImageView() | 33 ImageView::ImageView() |
| 35 : image_size_set_(false), | 34 : image_size_set_(false), |
| 36 horiz_alignment_(CENTER), | 35 horiz_alignment_(CENTER), |
| 37 vert_alignment_(CENTER), | 36 vert_alignment_(CENTER), |
| 38 interactive_(true), | |
| 39 last_paint_scale_(0.f), | 37 last_paint_scale_(0.f), |
| 40 last_painted_bitmap_pixels_(NULL), | 38 last_painted_bitmap_pixels_(NULL) {} |
| 41 focus_painter_(Painter::CreateDashedFocusPainter()) { | |
| 42 } | |
| 43 | 39 |
| 44 ImageView::~ImageView() { | 40 ImageView::~ImageView() {} |
| 45 } | |
| 46 | 41 |
| 47 void ImageView::SetImage(const gfx::ImageSkia& img) { | 42 void ImageView::SetImage(const gfx::ImageSkia& img) { |
| 48 if (IsImageEqual(img)) | 43 if (IsImageEqual(img)) |
| 49 return; | 44 return; |
| 50 | 45 |
| 51 last_painted_bitmap_pixels_ = NULL; | 46 last_painted_bitmap_pixels_ = NULL; |
| 52 gfx::Size pref_size(GetPreferredSize()); | 47 gfx::Size pref_size(GetPreferredSize()); |
| 53 image_ = img; | 48 image_ = img; |
| 54 if (pref_size != GetPreferredSize()) | 49 if (pref_size != GetPreferredSize()) |
| 55 PreferredSizeChanged(); | 50 PreferredSizeChanged(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 77 | 72 |
| 78 gfx::Rect ImageView::GetImageBounds() const { | 73 gfx::Rect ImageView::GetImageBounds() const { |
| 79 gfx::Size image_size = GetImageSize(); | 74 gfx::Size image_size = GetImageSize(); |
| 80 return gfx::Rect(ComputeImageOrigin(image_size), image_size); | 75 return gfx::Rect(ComputeImageOrigin(image_size), image_size); |
| 81 } | 76 } |
| 82 | 77 |
| 83 void ImageView::ResetImageSize() { | 78 void ImageView::ResetImageSize() { |
| 84 image_size_set_ = false; | 79 image_size_set_ = false; |
| 85 } | 80 } |
| 86 | 81 |
| 87 void ImageView::SetFocusPainter(std::unique_ptr<Painter> focus_painter) { | |
| 88 focus_painter_ = std::move(focus_painter); | |
| 89 } | |
| 90 | |
| 91 gfx::Size ImageView::GetPreferredSize() const { | 82 gfx::Size ImageView::GetPreferredSize() const { |
| 92 gfx::Size size = GetImageSize(); | 83 gfx::Size size = GetImageSize(); |
| 93 size.Enlarge(GetInsets().width(), GetInsets().height()); | 84 size.Enlarge(GetInsets().width(), GetInsets().height()); |
| 94 return size; | 85 return size; |
| 95 } | 86 } |
| 96 | 87 |
| 97 bool ImageView::IsImageEqual(const gfx::ImageSkia& img) const { | 88 bool ImageView::IsImageEqual(const gfx::ImageSkia& img) const { |
| 98 // Even though we copy ImageSkia in SetImage() the backing store | 89 // Even though we copy ImageSkia in SetImage() the backing store |
| 99 // (ImageSkiaStorage) is not copied and may have changed since the last call | 90 // (ImageSkiaStorage) is not copied and may have changed since the last call |
| 100 // to SetImage(). The expectation is that SetImage() with different pixels is | 91 // to SetImage(). The expectation is that SetImage() with different pixels is |
| (...skipping 30 matching lines...) Expand all Loading... |
| 131 switch (vert_alignment_) { | 122 switch (vert_alignment_) { |
| 132 case LEADING: y = insets.top(); break; | 123 case LEADING: y = insets.top(); break; |
| 133 case TRAILING: y = height() - insets.bottom() - image_size.height(); break; | 124 case TRAILING: y = height() - insets.bottom() - image_size.height(); break; |
| 134 case CENTER: y = (height() - image_size.height()) / 2; break; | 125 case CENTER: y = (height() - image_size.height()) / 2; break; |
| 135 default: NOTREACHED(); y = 0; break; | 126 default: NOTREACHED(); y = 0; break; |
| 136 } | 127 } |
| 137 | 128 |
| 138 return gfx::Point(x, y); | 129 return gfx::Point(x, y); |
| 139 } | 130 } |
| 140 | 131 |
| 141 void ImageView::OnFocus() { | |
| 142 View::OnFocus(); | |
| 143 if (focus_painter_.get()) | |
| 144 SchedulePaint(); | |
| 145 } | |
| 146 | |
| 147 void ImageView::OnBlur() { | |
| 148 View::OnBlur(); | |
| 149 if (focus_painter_.get()) | |
| 150 SchedulePaint(); | |
| 151 } | |
| 152 | |
| 153 void ImageView::OnPaint(gfx::Canvas* canvas) { | 132 void ImageView::OnPaint(gfx::Canvas* canvas) { |
| 154 View::OnPaint(canvas); | 133 View::OnPaint(canvas); |
| 155 OnPaintImage(canvas); | 134 OnPaintImage(canvas); |
| 156 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); | |
| 157 } | 135 } |
| 158 | 136 |
| 159 void ImageView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | 137 void ImageView::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| 160 node_data->role = ui::AX_ROLE_IMAGE; | 138 node_data->role = ui::AX_ROLE_IMAGE; |
| 161 node_data->SetName(tooltip_text_); | 139 node_data->SetName(tooltip_text_); |
| 162 } | 140 } |
| 163 | 141 |
| 164 const char* ImageView::GetClassName() const { | 142 const char* ImageView::GetClassName() const { |
| 165 return kViewClassName; | 143 return kViewClassName; |
| 166 } | 144 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 197 | 175 |
| 198 bool ImageView::GetTooltipText(const gfx::Point& p, | 176 bool ImageView::GetTooltipText(const gfx::Point& p, |
| 199 base::string16* tooltip) const { | 177 base::string16* tooltip) const { |
| 200 if (tooltip_text_.empty()) | 178 if (tooltip_text_.empty()) |
| 201 return false; | 179 return false; |
| 202 | 180 |
| 203 *tooltip = GetTooltipText(); | 181 *tooltip = GetTooltipText(); |
| 204 return true; | 182 return true; |
| 205 } | 183 } |
| 206 | 184 |
| 207 bool ImageView::CanProcessEventsWithinSubtree() const { | |
| 208 return interactive_; | |
| 209 } | |
| 210 | |
| 211 void ImageView::OnPaintImage(gfx::Canvas* canvas) { | 185 void ImageView::OnPaintImage(gfx::Canvas* canvas) { |
| 212 last_paint_scale_ = canvas->image_scale(); | 186 last_paint_scale_ = canvas->image_scale(); |
| 213 last_painted_bitmap_pixels_ = NULL; | 187 last_painted_bitmap_pixels_ = NULL; |
| 214 | 188 |
| 215 if (image_.isNull()) | 189 if (image_.isNull()) |
| 216 return; | 190 return; |
| 217 | 191 |
| 218 gfx::Rect image_bounds(GetImageBounds()); | 192 gfx::Rect image_bounds(GetImageBounds()); |
| 219 if (image_bounds.IsEmpty()) | 193 if (image_bounds.IsEmpty()) |
| 220 return; | 194 return; |
| 221 | 195 |
| 222 if (image_bounds.size() != gfx::Size(image_.width(), image_.height())) { | 196 if (image_bounds.size() != gfx::Size(image_.width(), image_.height())) { |
| 223 // Resize case | 197 // Resize case |
| 224 cc::PaintFlags flags; | 198 cc::PaintFlags flags; |
| 225 flags.setFilterQuality(kLow_SkFilterQuality); | 199 flags.setFilterQuality(kLow_SkFilterQuality); |
| 226 canvas->DrawImageInt(image_, 0, 0, image_.width(), image_.height(), | 200 canvas->DrawImageInt(image_, 0, 0, image_.width(), image_.height(), |
| 227 image_bounds.x(), image_bounds.y(), | 201 image_bounds.x(), image_bounds.y(), |
| 228 image_bounds.width(), image_bounds.height(), true, | 202 image_bounds.width(), image_bounds.height(), true, |
| 229 flags); | 203 flags); |
| 230 } else { | 204 } else { |
| 231 canvas->DrawImageInt(image_, image_bounds.x(), image_bounds.y()); | 205 canvas->DrawImageInt(image_, image_bounds.x(), image_bounds.y()); |
| 232 } | 206 } |
| 233 last_painted_bitmap_pixels_ = GetBitmapPixels(image_, last_paint_scale_); | 207 last_painted_bitmap_pixels_ = GetBitmapPixels(image_, last_paint_scale_); |
| 234 } | 208 } |
| 235 | 209 |
| 236 } // namespace views | 210 } // namespace views |
| OLD | NEW |