1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "views/painter.h"
7 #include "base/logging.h"
8 #include "third_party/skia/include/core/SkBitmap.h"
9 #include "third_party/skia/include/effects/SkGradientShader.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/canvas_skia.h"
13 #include "ui/gfx/insets.h"
19 class GradientPainter
: public Painter
{
21 GradientPainter(bool horizontal
, const SkColor
& top
, const SkColor
& bottom
)
22 : horizontal_(horizontal
) {
27 virtual ~GradientPainter() {
30 void Paint(int w
, int h
, gfx::Canvas
* canvas
) {
33 p
[0].set(SkIntToScalar(0), SkIntToScalar(0));
35 p
[1].set(SkIntToScalar(w
), SkIntToScalar(0));
37 p
[1].set(SkIntToScalar(0), SkIntToScalar(h
));
40 SkGradientShader::CreateLinear(p
, colors_
, NULL
, 2,
41 SkShader::kClamp_TileMode
, NULL
);
42 paint
.setStyle(SkPaint::kFill_Style
);
44 // Need to unref shader, otherwise never deleted.
47 canvas
->AsCanvasSkia()->drawRectCoords(
48 SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(w
), SkIntToScalar(h
),
56 DISALLOW_COPY_AND_ASSIGN(GradientPainter
);
60 class ImagePainter
: public Painter
{
62 ImagePainter(const SkBitmap
& image
,
63 const gfx::Insets
& insets
,
67 paint_center_(paint_center
) {
68 DCHECK(image
.width() > insets
.width() &&
69 image
.height() > insets
.height());
73 virtual void Paint(int w
, int h
, gfx::Canvas
* canvas
) {
74 if (w
== image_
.width() && h
== image_
.height()) {
75 // Early out if the size we're to render at equals the size of the image.
76 canvas
->DrawBitmapInt(image_
, 0, 0);
80 canvas
->DrawBitmapInt(image_
, 0, 0, insets_
.left(), insets_
.top(),
81 0, 0, insets_
.left(), insets_
.top(), true);
83 canvas
->DrawBitmapInt(
85 insets_
.left(), 0, image_
.width() - insets_
.width(), insets_
.top(),
86 insets_
.left(), 0, w
- insets_
.width(), insets_
.top(), true);
88 canvas
->DrawBitmapInt(
90 image_
.width() - insets_
.right(), 0, insets_
.right(), insets_
.top(),
91 w
- insets_
.right(), 0, insets_
.right(), insets_
.top(), true);
93 canvas
->DrawBitmapInt(
95 image_
.width() - insets_
.right(), insets_
.top(),
96 insets_
.right(), image_
.height() - insets_
.height(),
97 w
- insets_
.right(), insets_
.top(), insets_
.right(),
98 h
- insets_
.height(), true);
100 canvas
->DrawBitmapInt(
102 image_
.width() - insets_
.right(), image_
.height() - insets_
.bottom(),
103 insets_
.right(), insets_
.bottom(),
104 w
- insets_
.right(), h
- insets_
.bottom(), insets_
.right(),
105 insets_
.bottom(), true);
107 canvas
->DrawBitmapInt(
109 insets_
.left(), image_
.height() - insets_
.bottom(),
110 image_
.width() - insets_
.width(), insets_
.bottom(),
111 insets_
.left(), h
- insets_
.bottom(), w
- insets_
.width(),
112 insets_
.bottom(), true);
114 canvas
->DrawBitmapInt(
116 0, image_
.height() - insets_
.bottom(), insets_
.left(),
118 0, h
- insets_
.bottom(), insets_
.left(), insets_
.bottom(), true);
120 canvas
->DrawBitmapInt(
122 0, insets_
.top(), insets_
.left(), image_
.height() - insets_
.height(),
123 0, insets_
.top(), insets_
.left(), h
- insets_
.height(), true);
126 canvas
->DrawBitmapInt(
128 insets_
.left(), insets_
.top(),
129 image_
.width() - insets_
.width(), image_
.height() - insets_
.height(),
130 insets_
.left(), insets_
.top(),
131 w
- insets_
.width(), h
- insets_
.height(), true);
136 const SkBitmap image_
;
137 const gfx::Insets insets_
;
140 DISALLOW_COPY_AND_ASSIGN(ImagePainter
);
146 void Painter::PaintPainterAt(int x
, int y
, int w
, int h
,
147 gfx::Canvas
* canvas
, Painter
* painter
) {
148 DCHECK(canvas
&& painter
);
152 canvas
->TranslateInt(x
, y
);
153 painter
->Paint(w
, h
, canvas
);
158 Painter
* Painter::CreateHorizontalGradient(SkColor c1
, SkColor c2
) {
159 return new GradientPainter(true, c1
, c2
);
163 Painter
* Painter::CreateVerticalGradient(SkColor c1
, SkColor c2
) {
164 return new GradientPainter(false, c1
, c2
);
168 Painter
* Painter::CreateImagePainter(const SkBitmap
& image
,
169 const gfx::Insets
& insets
,
171 return new ImagePainter(image
, insets
, paint_center
);
174 HorizontalPainter::HorizontalPainter(const int image_resource_names
[]) {
175 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
176 for (int i
= 0; i
< 3; ++i
)
177 images_
[i
] = rb
.GetBitmapNamed(image_resource_names
[i
]);
178 height_
= images_
[LEFT
]->height();
179 DCHECK(images_
[LEFT
]->height() == images_
[RIGHT
]->height() &&
180 images_
[LEFT
]->height() == images_
[CENTER
]->height());
183 void HorizontalPainter::Paint(int w
, int h
, gfx::Canvas
* canvas
) {
184 if (w
< (images_
[LEFT
]->width() + images_
[CENTER
]->width() +
185 images_
[RIGHT
]->width())) {
189 canvas
->DrawBitmapInt(*images_
[LEFT
], 0, 0);
190 canvas
->DrawBitmapInt(*images_
[RIGHT
], w
- images_
[RIGHT
]->width(), 0);
191 canvas
->TileImageInt(*images_
[CENTER
],
192 images_
[LEFT
]->width(),
194 w
- images_
[LEFT
]->width() - images_
[RIGHT
]->width(),