Home | History | Annotate | Download | only in views
      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   virtual 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   virtual ~BackgroundPainter() {
     43     if (owns_painter_)
     44       delete painter_;
     45   }
     46 
     47 
     48   virtual void Paint(gfx::Canvas* canvas, View* view) const OVERRIDE {
     49     Painter::PaintPainterAt(canvas, painter_, view->GetLocalBounds());
     50   }
     51 
     52  private:
     53   bool owns_painter_;
     54   Painter* painter_;
     55 
     56   DISALLOW_COPY_AND_ASSIGN(BackgroundPainter);
     57 };
     58 
     59 Background::Background()
     60     : color_(SK_ColorWHITE)
     61 #if defined(OS_WIN)
     62     , native_control_brush_(NULL)
     63 #endif
     64 {
     65 }
     66 
     67 Background::~Background() {
     68 #if defined(OS_WIN)
     69   DeleteObject(native_control_brush_);
     70 #endif
     71 }
     72 
     73 void Background::SetNativeControlColor(SkColor color) {
     74   color_ = color;
     75 #if defined(OS_WIN)
     76   DeleteObject(native_control_brush_);
     77   native_control_brush_ = NULL;
     78 #endif
     79 }
     80 
     81 #if defined(OS_WIN)
     82 HBRUSH Background::GetNativeControlBrush() const {
     83   if (!native_control_brush_)
     84     native_control_brush_ = CreateSolidBrush(skia::SkColorToCOLORREF(color_));
     85   return native_control_brush_;
     86 }
     87 #endif
     88 
     89 //static
     90 Background* Background::CreateSolidBackground(SkColor color) {
     91   return new SolidBackground(color);
     92 }
     93 
     94 //static
     95 Background* Background::CreateStandardPanelBackground() {
     96   // TODO(beng): Should be in NativeTheme.
     97   return CreateSolidBackground(SK_ColorWHITE);
     98 }
     99 
    100 //static
    101 Background* Background::CreateVerticalGradientBackground(SkColor color1,
    102                                                          SkColor color2) {
    103   Background* background = CreateBackgroundPainter(
    104       true, Painter::CreateVerticalGradient(color1, color2));
    105   background->SetNativeControlColor(
    106       color_utils::AlphaBlend(color1, color2, 128));
    107 
    108   return background;
    109 }
    110 
    111 //static
    112 Background* Background::CreateVerticalMultiColorGradientBackground(
    113     SkColor* colors,
    114     SkScalar* pos,
    115     size_t count) {
    116   Background* background = CreateBackgroundPainter(
    117       true, Painter::CreateVerticalMultiColorGradient(colors, pos, count));
    118   background->SetNativeControlColor(
    119       color_utils::AlphaBlend(colors[0], colors[count-1], 128));
    120 
    121   return background;
    122 }
    123 
    124 //static
    125 Background* Background::CreateBackgroundPainter(bool owns_painter,
    126                                                 Painter* painter) {
    127   return new BackgroundPainter(owns_painter, painter);
    128 }
    129 
    130 }  // namespace views
    131