| 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/painter.h" | 5 #include "ui/views/painter.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "third_party/skia/include/effects/SkGradientShader.h" | 8 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 9 #include "ui/base/resource/resource_bundle.h" | 9 #include "ui/base/resource/resource_bundle.h" |
| 10 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| 11 #include "ui/gfx/image/image.h" | 11 #include "ui/gfx/image/image.h" |
| 12 #include "ui/gfx/image/image_skia.h" | 12 #include "ui/gfx/image/image_skia.h" |
| 13 #include "ui/gfx/insets.h" | 13 #include "ui/gfx/insets.h" |
| 14 #include "ui/gfx/point.h" | 14 #include "ui/gfx/point.h" |
| 15 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
| 16 | 16 |
| 17 namespace views { | 17 namespace views { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 class GradientPainter : public Painter { | 21 class GradientPainter : public Painter { |
| 22 public: | 22 public: |
| 23 GradientPainter(bool horizontal, SkColor top, SkColor bottom) | 23 GradientPainter(bool horizontal, |
| 24 : horizontal_(horizontal) { | 24 SkColor* colors, |
| 25 colors_[0] = top; | 25 SkScalar* pos, |
| 26 colors_[1] = bottom; | 26 size_t count) |
| 27 : horizontal_(horizontal), |
| 28 count_(count) { |
| 29 pos_ = new SkScalar[count_]; |
| 30 colors_ = new SkColor[count_]; |
| 31 |
| 32 for (size_t i = 0; i < count_; ++i) { |
| 33 pos_[i] = pos[i]; |
| 34 colors_[i] = colors[i]; |
| 35 } |
| 27 } | 36 } |
| 28 | 37 |
| 29 virtual ~GradientPainter() { | 38 virtual ~GradientPainter() { |
| 39 delete[] pos_; |
| 40 delete[] colors_; |
| 30 } | 41 } |
| 31 | 42 |
| 32 // Overridden from Painter: | 43 // Overridden from Painter: |
| 33 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE { | 44 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE { |
| 34 SkPaint paint; | 45 SkPaint paint; |
| 35 SkPoint p[2]; | 46 SkPoint p[2]; |
| 36 p[0].iset(0, 0); | 47 p[0].iset(0, 0); |
| 37 if (horizontal_) | 48 if (horizontal_) |
| 38 p[1].iset(size.width(), 0); | 49 p[1].iset(size.width(), 0); |
| 39 else | 50 else |
| 40 p[1].iset(0, size.height()); | 51 p[1].iset(0, size.height()); |
| 41 | 52 |
| 42 SkShader* s = SkGradientShader::CreateLinear(p, colors_, NULL, 2, | 53 SkShader* s = SkGradientShader::CreateLinear(p, colors_, pos_, count_, |
| 43 SkShader::kClamp_TileMode, NULL); | 54 SkShader::kClamp_TileMode, NULL); |
| 44 paint.setStyle(SkPaint::kFill_Style); | 55 paint.setStyle(SkPaint::kFill_Style); |
| 45 paint.setShader(s); | 56 paint.setShader(s); |
| 46 // Need to unref shader, otherwise never deleted. | 57 // Need to unref shader, otherwise never deleted. |
| 47 s->unref(); | 58 s->unref(); |
| 48 | 59 |
| 49 canvas->sk_canvas()->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0), | 60 canvas->sk_canvas()->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0), |
| 50 SkIntToScalar(size.width()), | 61 SkIntToScalar(size.width()), |
| 51 SkIntToScalar(size.height()), paint); | 62 SkIntToScalar(size.height()), paint); |
| 52 } | 63 } |
| 53 | 64 |
| 54 private: | 65 private: |
| 66 // If |horizontal_| is true then the gradiant is painted horizontally. |
| 55 bool horizontal_; | 67 bool horizontal_; |
| 56 SkColor colors_[2]; | 68 // The gradient colors. |
| 69 SkColor* colors_; |
| 70 // The relative positions of the corresponding gradient colors. |
| 71 SkScalar* pos_; |
| 72 // The number of elements in |colors_| and |pos_|. |
| 73 size_t count_; |
| 57 | 74 |
| 58 DISALLOW_COPY_AND_ASSIGN(GradientPainter); | 75 DISALLOW_COPY_AND_ASSIGN(GradientPainter); |
| 59 }; | 76 }; |
| 60 | 77 |
| 61 | 78 |
| 62 class ImagePainter : public Painter { | 79 class ImagePainter : public Painter { |
| 63 public: | 80 public: |
| 64 ImagePainter(const gfx::ImageSkia& image, | 81 ImagePainter(const gfx::ImageSkia& image, |
| 65 const gfx::Insets& insets, | 82 const gfx::Insets& insets, |
| 66 bool paint_center) | 83 bool paint_center) |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 const gfx::Rect& rect) { | 173 const gfx::Rect& rect) { |
| 157 DCHECK(canvas && painter); | 174 DCHECK(canvas && painter); |
| 158 canvas->Save(); | 175 canvas->Save(); |
| 159 canvas->Translate(rect.origin()); | 176 canvas->Translate(rect.origin()); |
| 160 painter->Paint(canvas, rect.size()); | 177 painter->Paint(canvas, rect.size()); |
| 161 canvas->Restore(); | 178 canvas->Restore(); |
| 162 } | 179 } |
| 163 | 180 |
| 164 // static | 181 // static |
| 165 Painter* Painter::CreateHorizontalGradient(SkColor c1, SkColor c2) { | 182 Painter* Painter::CreateHorizontalGradient(SkColor c1, SkColor c2) { |
| 166 return new GradientPainter(true, c1, c2); | 183 SkColor colors[2]; |
| 184 colors[0] = c1; |
| 185 colors[1] = c2; |
| 186 SkScalar pos[] = {0, 1}; |
| 187 return new GradientPainter(true, colors, pos, 2); |
| 167 } | 188 } |
| 168 | 189 |
| 169 // static | 190 // static |
| 170 Painter* Painter::CreateVerticalGradient(SkColor c1, SkColor c2) { | 191 Painter* Painter::CreateVerticalGradient(SkColor c1, SkColor c2) { |
| 171 return new GradientPainter(false, c1, c2); | 192 SkColor colors[2]; |
| 193 colors[0] = c1; |
| 194 colors[1] = c2; |
| 195 SkScalar pos[] = {0, 1}; |
| 196 return new GradientPainter(false, colors, pos, 2); |
| 172 } | 197 } |
| 173 | 198 |
| 174 // static | 199 // static |
| 200 Painter* Painter::CreateVerticalMultiColorGradient(SkColor* colors, |
| 201 SkScalar* pos, |
| 202 size_t count) { |
| 203 return new GradientPainter(false, colors, pos, count); |
| 204 } |
| 205 |
| 206 // static |
| 175 Painter* Painter::CreateImagePainter(const gfx::ImageSkia& image, | 207 Painter* Painter::CreateImagePainter(const gfx::ImageSkia& image, |
| 176 const gfx::Insets& insets, | 208 const gfx::Insets& insets, |
| 177 bool paint_center) { | 209 bool paint_center) { |
| 178 return new ImagePainter(image, insets, paint_center); | 210 return new ImagePainter(image, insets, paint_center); |
| 179 } | 211 } |
| 180 | 212 |
| 181 HorizontalPainter::HorizontalPainter(const int image_resource_names[]) { | 213 HorizontalPainter::HorizontalPainter(const int image_resource_names[]) { |
| 182 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 214 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 183 for (int i = 0; i < 3; ++i) | 215 for (int i = 0; i < 3; ++i) |
| 184 images_[i] = rb.GetImageNamed(image_resource_names[i]).ToImageSkia(); | 216 images_[i] = rb.GetImageNamed(image_resource_names[i]).ToImageSkia(); |
| 185 height_ = images_[LEFT]->height(); | 217 height_ = images_[LEFT]->height(); |
| 186 DCHECK(images_[LEFT]->height() == images_[RIGHT]->height() && | 218 DCHECK(images_[LEFT]->height() == images_[RIGHT]->height() && |
| 187 images_[LEFT]->height() == images_[CENTER]->height()); | 219 images_[LEFT]->height() == images_[CENTER]->height()); |
| 188 } | 220 } |
| 189 | 221 |
| 190 void HorizontalPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) { | 222 void HorizontalPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) { |
| 191 if (size.width() < (images_[LEFT]->width() + images_[CENTER]->width() + | 223 if (size.width() < (images_[LEFT]->width() + images_[CENTER]->width() + |
| 192 images_[RIGHT]->width())) { | 224 images_[RIGHT]->width())) { |
| 193 // No room to paint. | 225 // No room to paint. |
| 194 return; | 226 return; |
| 195 } | 227 } |
| 196 canvas->DrawImageInt(*images_[LEFT], 0, 0); | 228 canvas->DrawImageInt(*images_[LEFT], 0, 0); |
| 197 canvas->DrawImageInt(*images_[RIGHT], | 229 canvas->DrawImageInt(*images_[RIGHT], |
| 198 size.width() - images_[RIGHT]->width(), 0); | 230 size.width() - images_[RIGHT]->width(), 0); |
| 199 canvas->TileImageInt(*images_[CENTER], images_[LEFT]->width(), 0, | 231 canvas->TileImageInt(*images_[CENTER], images_[LEFT]->width(), 0, |
| 200 size.width() - images_[LEFT]->width() - images_[RIGHT]->width(), height_); | 232 size.width() - images_[LEFT]->width() - images_[RIGHT]->width(), height_); |
| 201 } | 233 } |
| 202 | 234 |
| 203 } // namespace views | 235 } // namespace views |
| OLD | NEW |