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