OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "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_skia.h" | |
11 #include "ui/gfx/color_utils.h" | |
12 #include "ui/views/view.h" | |
13 #include "views/painter.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(const SkColor& color) { | |
22 SetNativeControlColor(color); | |
23 } | |
24 | |
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->GetSkCanvas()->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 virtual ~BackgroundPainter() { | |
43 if (owns_painter_) | |
44 delete painter_; | |
45 } | |
46 | |
47 | |
48 void Paint(gfx::Canvas* canvas, View* view) const { | |
49 Painter::PaintPainterAt(0, 0, view->width(), view->height(), canvas, | |
50 painter_); | |
51 } | |
52 | |
53 private: | |
54 bool owns_painter_; | |
55 Painter* painter_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(BackgroundPainter); | |
58 }; | |
59 | |
60 Background::Background() | |
61 : color_(SK_ColorWHITE) | |
62 #if defined(OS_WIN) | |
63 , native_control_brush_(NULL) | |
64 #endif | |
65 { | |
66 } | |
67 | |
68 Background::~Background() { | |
69 #if defined(OS_WIN) | |
70 DeleteObject(native_control_brush_); | |
71 #endif | |
72 } | |
73 | |
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 | |
80 } | |
81 | |
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_; | |
87 } | |
88 #endif | |
89 | |
90 //static | |
91 Background* Background::CreateSolidBackground(const SkColor& color) { | |
92 return new SolidBackground(color); | |
93 } | |
94 | |
95 //static | |
96 Background* Background::CreateStandardPanelBackground() { | |
97 return CreateVerticalGradientBackground(SkColorSetRGB(246, 250, 255), | |
98 SkColorSetRGB(219, 235, 255)); | |
99 } | |
100 | |
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)); | |
108 | |
109 return background; | |
110 } | |
111 | |
112 //static | |
113 Background* Background::CreateBackgroundPainter(bool owns_painter, | |
114 Painter* painter) { | |
115 return new BackgroundPainter(owns_painter, painter); | |
116 } | |
117 | |
118 } // namespace views | |
OLD | NEW |