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/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, SkColor top, SkColor bottom, double start, |
|
msw
2012/08/08 17:12:20
nit: one param per line
markusheintz_
2012/08/08 21:53:55
Done.
| |
| 24 : horizontal_(horizontal) { | 24 double end) |
| 25 : horizontal_(horizontal), | |
| 26 start_(start), | |
| 27 end_(end) { | |
| 25 colors_[0] = top; | 28 colors_[0] = top; |
| 26 colors_[1] = bottom; | 29 colors_[1] = bottom; |
| 27 } | 30 } |
| 28 | 31 |
| 29 virtual ~GradientPainter() { | 32 virtual ~GradientPainter() { |
| 30 } | 33 } |
| 31 | 34 |
| 32 // Overridden from Painter: | 35 // Overridden from Painter: |
| 33 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE { | 36 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE { |
| 34 SkPaint paint; | 37 SkPaint paint; |
| 35 SkPoint p[2]; | 38 SkPoint p[2]; |
| 36 p[0].iset(0, 0); | 39 if (horizontal_) { |
| 37 if (horizontal_) | 40 p[0].iset(size.width() * start_, 0); |
| 38 p[1].iset(size.width(), 0); | 41 p[1].iset(size.width() * end_, 0); |
| 39 else | 42 } else { |
| 40 p[1].iset(0, size.height()); | 43 p[0].iset(0, size.height() * start_); |
| 44 p[1].iset(0, size.height() * end_); | |
| 45 } | |
| 41 | 46 |
| 42 SkShader* s = SkGradientShader::CreateLinear(p, colors_, NULL, 2, | 47 SkShader* s = SkGradientShader::CreateLinear(p, colors_, NULL, 2, |
| 43 SkShader::kClamp_TileMode, NULL); | 48 SkShader::kClamp_TileMode, NULL); |
| 44 paint.setStyle(SkPaint::kFill_Style); | 49 paint.setStyle(SkPaint::kFill_Style); |
| 45 paint.setShader(s); | 50 paint.setShader(s); |
| 46 // Need to unref shader, otherwise never deleted. | 51 // Need to unref shader, otherwise never deleted. |
| 47 s->unref(); | 52 s->unref(); |
| 48 | 53 |
| 49 canvas->sk_canvas()->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0), | 54 canvas->sk_canvas()->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0), |
| 50 SkIntToScalar(size.width()), | 55 SkIntToScalar(size.width()), |
| 51 SkIntToScalar(size.height()), paint); | 56 SkIntToScalar(size.height()), paint); |
| 52 } | 57 } |
| 53 | 58 |
| 54 private: | 59 private: |
| 60 // If |horizontal_| is true then the gradiant is painted horizontally. | |
| 55 bool horizontal_; | 61 bool horizontal_; |
| 62 // The start and the end color of the gradient. | |
| 56 SkColor colors_[2]; | 63 SkColor colors_[2]; |
| 64 // The start of the gradient specified as percentage of the total size of the | |
|
msw
2012/08/08 17:12:20
nit: s/percentage/ratio/, ditto below
markusheintz_
2012/08/08 21:53:55
obsolete
| |
| 65 // background. | |
| 66 double start_; | |
|
msw
2012/08/08 17:12:20
Consider using SkScalar/floats here, instead of do
markusheintz_
2012/08/08 21:53:55
Done.
| |
| 67 // The end of the gradient specified as percentage of the total size of the | |
| 68 // background. | |
| 69 double end_; | |
| 57 | 70 |
| 58 DISALLOW_COPY_AND_ASSIGN(GradientPainter); | 71 DISALLOW_COPY_AND_ASSIGN(GradientPainter); |
| 59 }; | 72 }; |
| 60 | 73 |
| 61 | 74 |
| 62 class ImagePainter : public Painter { | 75 class ImagePainter : public Painter { |
| 63 public: | 76 public: |
| 64 ImagePainter(const gfx::ImageSkia& image, | 77 ImagePainter(const gfx::ImageSkia& image, |
| 65 const gfx::Insets& insets, | 78 const gfx::Insets& insets, |
| 66 bool paint_center) | 79 bool paint_center) |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 const gfx::Rect& rect) { | 169 const gfx::Rect& rect) { |
| 157 DCHECK(canvas && painter); | 170 DCHECK(canvas && painter); |
| 158 canvas->Save(); | 171 canvas->Save(); |
| 159 canvas->Translate(rect.origin()); | 172 canvas->Translate(rect.origin()); |
| 160 painter->Paint(canvas, rect.size()); | 173 painter->Paint(canvas, rect.size()); |
| 161 canvas->Restore(); | 174 canvas->Restore(); |
| 162 } | 175 } |
| 163 | 176 |
| 164 // static | 177 // static |
| 165 Painter* Painter::CreateHorizontalGradient(SkColor c1, SkColor c2) { | 178 Painter* Painter::CreateHorizontalGradient(SkColor c1, SkColor c2) { |
| 166 return new GradientPainter(true, c1, c2); | 179 return new GradientPainter(true, c1, c2, 0.0f, 1.0f); |
| 167 } | 180 } |
| 168 | 181 |
| 169 // static | 182 // static |
| 170 Painter* Painter::CreateVerticalGradient(SkColor c1, SkColor c2) { | 183 Painter* Painter::CreateVerticalGradient(SkColor c1, SkColor c2) { |
| 171 return new GradientPainter(false, c1, c2); | 184 return new GradientPainter(false, c1, c2, 0.0f, 1.0f); |
| 172 } | 185 } |
| 173 | 186 |
| 174 // static | 187 // static |
| 188 Painter* Painter::CreateVerticalGradientRange( | |
| 189 SkColor c1, SkColor c2, float start, float end) { | |
|
msw
2012/08/08 17:12:20
nit: one param per line
markusheintz_
2012/08/08 21:53:55
Done.
| |
| 190 return new GradientPainter(false, c1, c2, start, end); | |
| 191 } | |
| 192 | |
| 193 // static | |
| 175 Painter* Painter::CreateImagePainter(const gfx::ImageSkia& image, | 194 Painter* Painter::CreateImagePainter(const gfx::ImageSkia& image, |
| 176 const gfx::Insets& insets, | 195 const gfx::Insets& insets, |
| 177 bool paint_center) { | 196 bool paint_center) { |
| 178 return new ImagePainter(image, insets, paint_center); | 197 return new ImagePainter(image, insets, paint_center); |
| 179 } | 198 } |
| 180 | 199 |
| 181 HorizontalPainter::HorizontalPainter(const int image_resource_names[]) { | 200 HorizontalPainter::HorizontalPainter(const int image_resource_names[]) { |
| 182 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 201 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 183 for (int i = 0; i < 3; ++i) | 202 for (int i = 0; i < 3; ++i) |
| 184 images_[i] = rb.GetImageNamed(image_resource_names[i]).ToImageSkia(); | 203 images_[i] = rb.GetImageNamed(image_resource_names[i]).ToImageSkia(); |
| 185 height_ = images_[LEFT]->height(); | 204 height_ = images_[LEFT]->height(); |
| 186 DCHECK(images_[LEFT]->height() == images_[RIGHT]->height() && | 205 DCHECK(images_[LEFT]->height() == images_[RIGHT]->height() && |
| 187 images_[LEFT]->height() == images_[CENTER]->height()); | 206 images_[LEFT]->height() == images_[CENTER]->height()); |
| 188 } | 207 } |
| 189 | 208 |
| 190 void HorizontalPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) { | 209 void HorizontalPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) { |
| 191 if (size.width() < (images_[LEFT]->width() + images_[CENTER]->width() + | 210 if (size.width() < (images_[LEFT]->width() + images_[CENTER]->width() + |
| 192 images_[RIGHT]->width())) { | 211 images_[RIGHT]->width())) { |
| 193 // No room to paint. | 212 // No room to paint. |
| 194 return; | 213 return; |
| 195 } | 214 } |
| 196 canvas->DrawImageInt(*images_[LEFT], 0, 0); | 215 canvas->DrawImageInt(*images_[LEFT], 0, 0); |
| 197 canvas->DrawImageInt(*images_[RIGHT], | 216 canvas->DrawImageInt(*images_[RIGHT], |
| 198 size.width() - images_[RIGHT]->width(), 0); | 217 size.width() - images_[RIGHT]->width(), 0); |
| 199 canvas->TileImageInt(*images_[CENTER], images_[LEFT]->width(), 0, | 218 canvas->TileImageInt(*images_[CENTER], images_[LEFT]->width(), 0, |
| 200 size.width() - images_[LEFT]->width() - images_[RIGHT]->width(), height_); | 219 size.width() - images_[LEFT]->width() - images_[RIGHT]->width(), height_); |
| 201 } | 220 } |
| 202 | 221 |
| 203 } // namespace views | 222 } // namespace views |
| OLD | NEW |