Roll skia to 1951
[chromium-blink-merge.git] / views / background.cc
blobf9eb2135fc09bea31b88bbe719fc6b4224bf15db
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"
15 namespace views {
17 // SolidBackground is a trivial Background implementation that fills the
18 // background in a solid color.
19 class SolidBackground : public Background {
20 public:
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());
31 private:
32 DISALLOW_COPY_AND_ASSIGN(SolidBackground);
35 class BackgroundPainter : public Background {
36 public:
37 BackgroundPainter(bool owns_painter, Painter* painter)
38 : owns_painter_(owns_painter), painter_(painter) {
39 DCHECK(painter);
42 virtual ~BackgroundPainter() {
43 if (owns_painter_)
44 delete painter_;
48 void Paint(gfx::Canvas* canvas, View* view) const {
49 Painter::PaintPainterAt(0, 0, view->width(), view->height(), canvas,
50 painter_);
53 private:
54 bool owns_painter_;
55 Painter* painter_;
57 DISALLOW_COPY_AND_ASSIGN(BackgroundPainter);
60 Background::Background()
61 : color_(SK_ColorWHITE)
62 #if defined(OS_WIN)
63 , native_control_brush_(NULL)
64 #endif
68 Background::~Background() {
69 #if defined(OS_WIN)
70 DeleteObject(native_control_brush_);
71 #endif
74 void Background::SetNativeControlColor(SkColor color) {
75 color_ = color;
76 #if defined(OS_WIN)
77 DeleteObject(native_control_brush_);
78 native_control_brush_ = NULL;
79 #endif
82 #if defined(OS_WIN)
83 HBRUSH Background::GetNativeControlBrush() const {
84 if (!native_control_brush_)
85 native_control_brush_ = CreateSolidBrush(skia::SkColorToCOLORREF(color_));
86 return native_control_brush_;
88 #endif
90 //static
91 Background* Background::CreateSolidBackground(const SkColor& color) {
92 return new SolidBackground(color);
95 //static
96 Background* Background::CreateStandardPanelBackground() {
97 return CreateVerticalGradientBackground(SkColorSetRGB(246, 250, 255),
98 SkColorSetRGB(219, 235, 255));
101 //static
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));
109 return background;
112 //static
113 Background* Background::CreateBackgroundPainter(bool owns_painter,
114 Painter* painter) {
115 return new BackgroundPainter(owns_painter, painter);
118 } // namespace views