Chromium Code Reviews| 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/accessibility/ax_view_state.h" | 10 #include "ui/accessibility/ax_view_state.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 return bitmap.getPixels(); | 24 return bitmap.getPixels(); |
| 25 } | 25 } |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 // static | 29 // static |
| 30 const char ImageView::kViewClassName[] = "ImageView"; | 30 const char ImageView::kViewClassName[] = "ImageView"; |
| 31 | 31 |
| 32 ImageView::ImageView() | 32 ImageView::ImageView() |
| 33 : image_size_set_(false), | 33 : image_size_set_(false), |
| 34 vector_id_(gfx::VectorIconId::VECTOR_ICON_NONE), | |
| 34 horiz_alignment_(CENTER), | 35 horiz_alignment_(CENTER), |
| 35 vert_alignment_(CENTER), | 36 vert_alignment_(CENTER), |
| 36 interactive_(true), | 37 interactive_(true), |
| 37 last_paint_scale_(0.f), | 38 last_paint_scale_(0.f), |
| 38 last_painted_bitmap_pixels_(NULL), | 39 last_painted_bitmap_pixels_(NULL), |
| 39 focus_painter_(Painter::CreateDashedFocusPainter()) { | 40 focus_painter_(Painter::CreateDashedFocusPainter()) { |
| 40 } | 41 } |
| 41 | 42 |
| 42 ImageView::~ImageView() { | 43 ImageView::~ImageView() { |
| 43 } | 44 } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 60 } else { | 61 } else { |
| 61 gfx::ImageSkia t; | 62 gfx::ImageSkia t; |
| 62 SetImage(t); | 63 SetImage(t); |
| 63 } | 64 } |
| 64 } | 65 } |
| 65 | 66 |
| 66 const gfx::ImageSkia& ImageView::GetImage() { | 67 const gfx::ImageSkia& ImageView::GetImage() { |
| 67 return image_; | 68 return image_; |
| 68 } | 69 } |
| 69 | 70 |
| 71 void ImageView::SetVectorIcon(gfx::VectorIconId id, | |
| 72 SkColor color, | |
| 73 gfx::Size& image_size) { | |
| 74 SetImageSize(image_size); | |
| 75 vector_id_ = id; | |
| 76 vector_color_ = color; | |
| 77 } | |
| 78 | |
| 70 void ImageView::SetImageSize(const gfx::Size& image_size) { | 79 void ImageView::SetImageSize(const gfx::Size& image_size) { |
| 71 image_size_set_ = true; | 80 image_size_set_ = true; |
| 72 image_size_ = image_size; | 81 image_size_ = image_size; |
| 73 PreferredSizeChanged(); | 82 PreferredSizeChanged(); |
| 74 } | 83 } |
| 75 | 84 |
| 76 bool ImageView::GetImageSize(gfx::Size* image_size) const { | |
| 77 DCHECK(image_size); | |
| 78 if (image_size_set_) | |
| 79 *image_size = image_size_; | |
| 80 return image_size_set_; | |
| 81 } | |
| 82 | |
| 83 gfx::Rect ImageView::GetImageBounds() const { | 85 gfx::Rect ImageView::GetImageBounds() const { |
| 84 gfx::Size image_size(image_size_set_ ? | 86 gfx::Size image_size(image_size_set_ ? |
| 85 image_size_ : gfx::Size(image_.width(), image_.height())); | 87 image_size_ : gfx::Size(image_.width(), image_.height())); |
| 86 return gfx::Rect(ComputeImageOrigin(image_size), image_size); | 88 return gfx::Rect(ComputeImageOrigin(image_size), image_size); |
| 87 } | 89 } |
| 88 | 90 |
| 89 void ImageView::ResetImageSize() { | 91 void ImageView::ResetImageSize() { |
| 90 image_size_set_ = false; | 92 image_size_set_ = false; |
| 91 } | 93 } |
| 92 | 94 |
| 93 void ImageView::SetFocusPainter(scoped_ptr<Painter> focus_painter) { | 95 void ImageView::SetFocusPainter(scoped_ptr<Painter> focus_painter) { |
| 94 focus_painter_ = focus_painter.Pass(); | 96 focus_painter_ = focus_painter.Pass(); |
| 95 } | 97 } |
| 96 | 98 |
| 97 gfx::Size ImageView::GetPreferredSize() const { | 99 gfx::Size ImageView::GetPreferredSize() const { |
| 98 gfx::Insets insets = GetInsets(); | 100 gfx::Insets insets = GetInsets(); |
| 99 if (image_size_set_) { | 101 if (image_size_set_) { |
| 100 gfx::Size image_size; | 102 gfx::Size image_size = image_size_; |
| 101 GetImageSize(&image_size); | |
| 102 image_size.Enlarge(insets.width(), insets.height()); | 103 image_size.Enlarge(insets.width(), insets.height()); |
| 103 return image_size; | 104 return image_size; |
| 104 } | 105 } |
| 105 return gfx::Size(image_.width() + insets.width(), | 106 return gfx::Size(image_.width() + insets.width(), |
| 106 image_.height() + insets.height()); | 107 image_.height() + insets.height()); |
| 107 } | 108 } |
| 108 | 109 |
| 109 bool ImageView::IsImageEqual(const gfx::ImageSkia& img) const { | 110 bool ImageView::IsImageEqual(const gfx::ImageSkia& img) const { |
| 110 // Even though we copy ImageSkia in SetImage() the backing store | 111 // Even though we copy ImageSkia in SetImage() the backing store |
| 111 // (ImageSkiaStorage) is not copied and may have changed since the last call | 112 // (ImageSkiaStorage) is not copied and may have changed since the last call |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 | 155 |
| 155 void ImageView::OnBlur() { | 156 void ImageView::OnBlur() { |
| 156 View::OnBlur(); | 157 View::OnBlur(); |
| 157 if (focus_painter_.get()) | 158 if (focus_painter_.get()) |
| 158 SchedulePaint(); | 159 SchedulePaint(); |
| 159 } | 160 } |
| 160 | 161 |
| 161 void ImageView::OnPaint(gfx::Canvas* canvas) { | 162 void ImageView::OnPaint(gfx::Canvas* canvas) { |
| 162 View::OnPaint(canvas); | 163 View::OnPaint(canvas); |
| 163 OnPaintImage(canvas); | 164 OnPaintImage(canvas); |
| 165 OnPaintVectorIcon(canvas); | |
| 164 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); | 166 Painter::PaintFocusPainter(this, canvas, focus_painter_.get()); |
| 165 } | 167 } |
| 166 | 168 |
| 167 void ImageView::GetAccessibleState(ui::AXViewState* state) { | 169 void ImageView::GetAccessibleState(ui::AXViewState* state) { |
| 168 state->role = ui::AX_ROLE_IMAGE; | 170 state->role = ui::AX_ROLE_IMAGE; |
| 169 state->name = tooltip_text_; | 171 state->name = tooltip_text_; |
| 170 } | 172 } |
| 171 | 173 |
| 172 const char* ImageView::GetClassName() const { | 174 const char* ImageView::GetClassName() const { |
| 173 return kViewClassName; | 175 return kViewClassName; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 paint.setFilterQuality(kLow_SkFilterQuality); | 235 paint.setFilterQuality(kLow_SkFilterQuality); |
| 234 canvas->DrawImageInt(image_, 0, 0, image_.width(), image_.height(), | 236 canvas->DrawImageInt(image_, 0, 0, image_.width(), image_.height(), |
| 235 image_bounds.x(), image_bounds.y(), image_bounds.width(), | 237 image_bounds.x(), image_bounds.y(), image_bounds.width(), |
| 236 image_bounds.height(), true, paint); | 238 image_bounds.height(), true, paint); |
| 237 } else { | 239 } else { |
| 238 canvas->DrawImageInt(image_, image_bounds.x(), image_bounds.y()); | 240 canvas->DrawImageInt(image_, image_bounds.x(), image_bounds.y()); |
| 239 } | 241 } |
| 240 last_painted_bitmap_pixels_ = GetBitmapPixels(image_, last_paint_scale_); | 242 last_painted_bitmap_pixels_ = GetBitmapPixels(image_, last_paint_scale_); |
| 241 } | 243 } |
| 242 | 244 |
| 245 void ImageView::OnPaintVectorIcon(gfx::Canvas* canvas) { | |
| 246 if (vector_id_ == gfx::VectorIconId::VECTOR_ICON_NONE) | |
| 247 return; | |
| 248 | |
| 249 DCHECK(image_size_set_); | |
| 250 gfx::Vector2d origin = ComputeImageOrigin(image_size_) - gfx::Point(); | |
|
Peter Kasting
2015/07/01 22:26:58
Nit: Use ComputeImageOrigin(image_size_).OffsetFro
Evan Stade
2015/07/02 00:08:06
Done.
| |
| 251 canvas->Translate(origin); | |
| 252 gfx::PaintVectorIcon(canvas, vector_id_, image_size_.width(), vector_color_); | |
| 253 } | |
| 254 | |
| 243 } // namespace views | 255 } // namespace views |
| OLD | NEW |