1 // Copyright (c) 2011 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/background.h"
7 #include "base/logging.h"
8 #include "skia/ext/skia_utils_win.h"
9 #include "third_party/skia/include/core/SkPaint.h"
10 #include "ui/gfx/canvas_skia.h"
11 #include "ui/gfx/color_utils.h"
12 #include "views/painter.h"
13 #include "views/view.h"
17 // SolidBackground is a trivial Background implementation that fills the
18 // background in a solid color.
19 class SolidBackground
: public Background
{
21 explicit SolidBackground(const SkColor
& color
) {
22 SetNativeControlColor(color
);
25 void Paint(gfx::Canvas
* canvas
, View
* view
) const {
26 // Fill the background. Note that we don't constrain to the bounds as
27 // canvas is already clipped for us.
28 canvas
->AsCanvasSkia()->drawColor(get_color());
32 DISALLOW_COPY_AND_ASSIGN(SolidBackground
);
35 class BackgroundPainter
: public Background
{
37 BackgroundPainter(bool owns_painter
, Painter
* painter
)
38 : owns_painter_(owns_painter
), painter_(painter
) {
42 virtual ~BackgroundPainter() {
48 void Paint(gfx::Canvas
* canvas
, View
* view
) const {
49 Painter::PaintPainterAt(0, 0, view
->width(), view
->height(), canvas
,
57 DISALLOW_COPY_AND_ASSIGN(BackgroundPainter
);
60 Background::Background()
61 : color_(SK_ColorWHITE
)
63 , native_control_brush_(NULL
)
68 Background::~Background() {
70 DeleteObject(native_control_brush_
);
74 void Background::SetNativeControlColor(SkColor color
) {
77 DeleteObject(native_control_brush_
);
78 native_control_brush_
= NULL
;
83 HBRUSH
Background::GetNativeControlBrush() const {
84 if (!native_control_brush_
)
85 native_control_brush_
= CreateSolidBrush(skia::SkColorToCOLORREF(color_
));
86 return native_control_brush_
;
91 Background
* Background::CreateSolidBackground(const SkColor
& color
) {
92 return new SolidBackground(color
);
96 Background
* Background::CreateStandardPanelBackground() {
97 return CreateVerticalGradientBackground(SkColorSetRGB(246, 250, 255),
98 SkColorSetRGB(219, 235, 255));
102 Background
* Background::CreateVerticalGradientBackground(
103 const SkColor
& color1
, const SkColor
& color2
) {
104 Background
* background
= CreateBackgroundPainter(
105 true, Painter::CreateVerticalGradient(color1
, color2
));
106 background
->SetNativeControlColor(
107 color_utils::AlphaBlend(color1
, color2
, 128));
113 Background
* Background::CreateBackgroundPainter(bool owns_painter
,
115 return new BackgroundPainter(owns_painter
, painter
);