Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / views / background.cc
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
3 // found in the LICENSE file.
4
5 #include "ui/views/background.h"
6
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.h"
11 #include "ui/gfx/color_utils.h"
12 #include "ui/views/painter.h"
13 #include "ui/views/view.h"
14
15 namespace views {
16
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(SkColor color) {
22     SetNativeControlColor(color);
23   }
24
25   void Paint(gfx::Canvas* canvas, View* view) const override {
26     // Fill the background. Note that we don't constrain to the bounds as
27     // canvas is already clipped for us.
28     canvas->DrawColor(get_color());
29   }
30
31  private:
32   DISALLOW_COPY_AND_ASSIGN(SolidBackground);
33 };
34
35 class BackgroundPainter : public Background {
36  public:
37   BackgroundPainter(bool owns_painter, Painter* painter)
38       : owns_painter_(owns_painter), painter_(painter) {
39     DCHECK(painter);
40   }
41
42   ~BackgroundPainter() override {
43     if (owns_painter_)
44       delete painter_;
45   }
46
47   void Paint(gfx::Canvas* canvas, View* view) const override {
48     Painter::PaintPainterAt(canvas, painter_, view->GetLocalBounds());
49   }
50
51  private:
52   bool owns_painter_;
53   Painter* painter_;
54
55   DISALLOW_COPY_AND_ASSIGN(BackgroundPainter);
56 };
57
58 Background::Background()
59     : color_(SK_ColorWHITE)
60 #if defined(OS_WIN)
61     , native_control_brush_(NULL)
62 #endif
63 {
64 }
65
66 Background::~Background() {
67 #if defined(OS_WIN)
68   DeleteObject(native_control_brush_);
69 #endif
70 }
71
72 void Background::SetNativeControlColor(SkColor color) {
73   color_ = color;
74 #if defined(OS_WIN)
75   DeleteObject(native_control_brush_);
76   native_control_brush_ = NULL;
77 #endif
78 }
79
80 #if defined(OS_WIN)
81 HBRUSH Background::GetNativeControlBrush() const {
82   if (!native_control_brush_)
83     native_control_brush_ = CreateSolidBrush(skia::SkColorToCOLORREF(color_));
84   return native_control_brush_;
85 }
86 #endif
87
88 //static
89 Background* Background::CreateSolidBackground(SkColor color) {
90   return new SolidBackground(color);
91 }
92
93 //static
94 Background* Background::CreateStandardPanelBackground() {
95   // TODO(beng): Should be in NativeTheme.
96   return CreateSolidBackground(SK_ColorWHITE);
97 }
98
99 //static
100 Background* Background::CreateVerticalGradientBackground(SkColor color1,
101                                                          SkColor color2) {
102   Background* background = CreateBackgroundPainter(
103       true, Painter::CreateVerticalGradient(color1, color2));
104   background->SetNativeControlColor(
105       color_utils::AlphaBlend(color1, color2, 128));
106
107   return background;
108 }
109
110 //static
111 Background* Background::CreateVerticalMultiColorGradientBackground(
112     SkColor* colors,
113     SkScalar* pos,
114     size_t count) {
115   Background* background = CreateBackgroundPainter(
116       true, Painter::CreateVerticalMultiColorGradient(colors, pos, count));
117   background->SetNativeControlColor(
118       color_utils::AlphaBlend(colors[0], colors[count-1], 128));
119
120   return background;
121 }
122
123 //static
124 Background* Background::CreateBackgroundPainter(bool owns_painter,
125                                                 Painter* painter) {
126   return new BackgroundPainter(owns_painter, painter);
127 }
128
129 }  // namespace views