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 int count) |
msw
2012/08/08 23:13:53
nit: ditto for size_t
markusheintz_
2012/08/09 01:00:15
Done.
| |
27 : horizontal_(horizontal), count_(count) { | |
msw
2012/08/08 23:13:53
nit: I think if the params and initializer list do
markusheintz_
2012/08/09 01:00:15
Done.
| |
28 pos_ = new SkScalar[count_]; | |
29 colors_ = new SkColor[count_]; | |
30 | |
31 for (int i = 0; i < count_; ++i) { | |
msw
2012/08/08 23:13:53
nit: ditto for size_t
markusheintz_
2012/08/09 01:00:15
Done.
| |
32 pos_[i] = pos[i]; | |
33 colors_[i] = colors[i]; | |
34 } | |
27 } | 35 } |
28 | 36 |
29 virtual ~GradientPainter() { | 37 virtual ~GradientPainter() { |
38 delete [] pos_; | |
msw
2012/08/08 23:13:53
nit: no space in "delete[]", same below.
markusheintz_
2012/08/09 01:00:15
Done.
| |
39 delete [] colors_; | |
30 } | 40 } |
31 | 41 |
32 // Overridden from Painter: | 42 // Overridden from Painter: |
33 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE { | 43 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE { |
34 SkPaint paint; | 44 SkPaint paint; |
35 SkPoint p[2]; | 45 SkPoint p[2]; |
36 p[0].iset(0, 0); | 46 p[0].iset(0, 0); |
37 if (horizontal_) | 47 if (horizontal_) { |
38 p[1].iset(size.width(), 0); | 48 p[1].iset(size.width(), 0); |
39 else | 49 } else { |
40 p[1].iset(0, size.height()); | 50 p[1].iset(0, size.height()); |
51 } | |
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 relatove positions of the corresponding gradient colors. | |
msw
2012/08/08 23:13:53
nit: relative
markusheintz_
2012/08/09 01:00:15
Done.
| |
71 SkScalar* pos_; | |
72 // The number of elements in |colors_| and |pos_|. | |
73 int 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::CreateVerticalGradientRange( | |
msw
2012/08/08 23:13:53
nit: use MultiColor / Range terminology consistent
markusheintz_
2012/08/09 01:00:15
Done.
| |
201 SkColor* colors, SkScalar* pos, int count) { | |
msw
2012/08/08 23:13:53
nit: one param per line
markusheintz_
2012/08/09 01:00:15
Done.
| |
202 return new GradientPainter(false, colors, pos, count); | |
203 } | |
204 | |
205 // static | |
175 Painter* Painter::CreateImagePainter(const gfx::ImageSkia& image, | 206 Painter* Painter::CreateImagePainter(const gfx::ImageSkia& image, |
176 const gfx::Insets& insets, | 207 const gfx::Insets& insets, |
177 bool paint_center) { | 208 bool paint_center) { |
178 return new ImagePainter(image, insets, paint_center); | 209 return new ImagePainter(image, insets, paint_center); |
179 } | 210 } |
180 | 211 |
181 HorizontalPainter::HorizontalPainter(const int image_resource_names[]) { | 212 HorizontalPainter::HorizontalPainter(const int image_resource_names[]) { |
182 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 213 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
183 for (int i = 0; i < 3; ++i) | 214 for (int i = 0; i < 3; ++i) |
184 images_[i] = rb.GetImageNamed(image_resource_names[i]).ToImageSkia(); | 215 images_[i] = rb.GetImageNamed(image_resource_names[i]).ToImageSkia(); |
185 height_ = images_[LEFT]->height(); | 216 height_ = images_[LEFT]->height(); |
186 DCHECK(images_[LEFT]->height() == images_[RIGHT]->height() && | 217 DCHECK(images_[LEFT]->height() == images_[RIGHT]->height() && |
187 images_[LEFT]->height() == images_[CENTER]->height()); | 218 images_[LEFT]->height() == images_[CENTER]->height()); |
188 } | 219 } |
189 | 220 |
190 void HorizontalPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) { | 221 void HorizontalPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) { |
191 if (size.width() < (images_[LEFT]->width() + images_[CENTER]->width() + | 222 if (size.width() < (images_[LEFT]->width() + images_[CENTER]->width() + |
192 images_[RIGHT]->width())) { | 223 images_[RIGHT]->width())) { |
193 // No room to paint. | 224 // No room to paint. |
194 return; | 225 return; |
195 } | 226 } |
196 canvas->DrawImageInt(*images_[LEFT], 0, 0); | 227 canvas->DrawImageInt(*images_[LEFT], 0, 0); |
197 canvas->DrawImageInt(*images_[RIGHT], | 228 canvas->DrawImageInt(*images_[RIGHT], |
198 size.width() - images_[RIGHT]->width(), 0); | 229 size.width() - images_[RIGHT]->width(), 0); |
199 canvas->TileImageInt(*images_[CENTER], images_[LEFT]->width(), 0, | 230 canvas->TileImageInt(*images_[CENTER], images_[LEFT]->width(), 0, |
200 size.width() - images_[LEFT]->width() - images_[RIGHT]->width(), height_); | 231 size.width() - images_[LEFT]->width() - images_[RIGHT]->width(), height_); |
201 } | 232 } |
202 | 233 |
203 } // namespace views | 234 } // namespace views |
OLD | NEW |