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/border.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "ui/gfx/canvas.h"
     10 #include "ui/views/painter.h"
     11 
     12 namespace views {
     13 
     14 namespace {
     15 
     16 // A simple border with different thicknesses on each side and single color.
     17 class SidedSolidBorder : public Border {
     18  public:
     19   SidedSolidBorder(int top, int left, int bottom, int right, SkColor color);
     20 
     21   // Overridden from Border:
     22   virtual void Paint(const View& view, gfx::Canvas* canvas) OVERRIDE;
     23   virtual gfx::Insets GetInsets() const OVERRIDE;
     24 
     25  private:
     26   const SkColor color_;
     27   const gfx::Insets insets_;
     28 
     29   DISALLOW_COPY_AND_ASSIGN(SidedSolidBorder);
     30 };
     31 
     32 SidedSolidBorder::SidedSolidBorder(int top,
     33                                    int left,
     34                                    int bottom,
     35                                    int right,
     36                                    SkColor color)
     37     : color_(color),
     38       insets_(top, left, bottom, right) {
     39 }
     40 
     41 void SidedSolidBorder::Paint(const View& view, gfx::Canvas* canvas) {
     42   // Top border.
     43   canvas->FillRect(gfx::Rect(0, 0, view.width(), insets_.top()), color_);
     44   // Left border.
     45   canvas->FillRect(gfx::Rect(0, 0, insets_.left(), view.height()), color_);
     46   // Bottom border.
     47   canvas->FillRect(gfx::Rect(0, view.height() - insets_.bottom(), view.width(),
     48                              insets_.bottom()), color_);
     49   // Right border.
     50   canvas->FillRect(gfx::Rect(view.width() - insets_.right(), 0, insets_.right(),
     51                              view.height()), color_);
     52 }
     53 
     54 gfx::Insets SidedSolidBorder::GetInsets() const {
     55   return insets_;
     56 }
     57 
     58 // A variation of SidedSolidBorder, where each side has the same thickness.
     59 class SolidBorder : public SidedSolidBorder {
     60  public:
     61   SolidBorder(int thickness, SkColor color)
     62       : SidedSolidBorder(thickness, thickness, thickness, thickness, color) {
     63   }
     64 
     65  private:
     66   DISALLOW_COPY_AND_ASSIGN(SolidBorder);
     67 };
     68 
     69 class EmptyBorder : public Border {
     70  public:
     71   EmptyBorder(int top, int left, int bottom, int right)
     72       : insets_(top, left, bottom, right) {}
     73 
     74   // Overridden from Border:
     75   virtual void Paint(const View& view, gfx::Canvas* canvas) OVERRIDE {}
     76 
     77   virtual gfx::Insets GetInsets() const OVERRIDE {
     78     return insets_;
     79   }
     80 
     81  private:
     82   const gfx::Insets insets_;
     83 
     84   DISALLOW_COPY_AND_ASSIGN(EmptyBorder);
     85 };
     86 
     87 class BorderPainter : public Border {
     88  public:
     89   explicit BorderPainter(Painter* painter, const gfx::Insets& insets)
     90       : painter_(painter),
     91         insets_(insets) {
     92     DCHECK(painter);
     93   }
     94 
     95   virtual ~BorderPainter() {}
     96 
     97   // Overridden from Border:
     98   virtual void Paint(const View& view, gfx::Canvas* canvas) OVERRIDE {
     99     Painter::PaintPainterAt(canvas, painter_.get(), view.GetLocalBounds());
    100   }
    101 
    102   virtual gfx::Insets GetInsets() const OVERRIDE {
    103     return insets_;
    104   }
    105 
    106  private:
    107   scoped_ptr<Painter> painter_;
    108   const gfx::Insets insets_;
    109 
    110   DISALLOW_COPY_AND_ASSIGN(BorderPainter);
    111 };
    112 
    113 }  // namespace
    114 
    115 Border::Border() {
    116 }
    117 
    118 Border::~Border() {
    119 }
    120 
    121 // static
    122 Border* Border::CreateSolidBorder(int thickness, SkColor color) {
    123   return new SolidBorder(thickness, color);
    124 }
    125 
    126 // static
    127 Border* Border::CreateEmptyBorder(int top, int left, int bottom, int right) {
    128   return new EmptyBorder(top, left, bottom, right);
    129 }
    130 
    131 // static
    132 Border* Border::CreateSolidSidedBorder(int top,
    133                                        int left,
    134                                        int bottom,
    135                                        int right,
    136                                        SkColor color) {
    137   return new SidedSolidBorder(top, left, bottom, right, color);
    138 }
    139 
    140 // static
    141 Border* Border::CreateBorderPainter(Painter* painter,
    142                                     const gfx::Insets& insets) {
    143   return new BorderPainter(painter, insets);
    144 }
    145 
    146 TextButtonBorder* Border::AsTextButtonBorder() {
    147   return NULL;
    148 }
    149 
    150 const TextButtonBorder* Border::AsTextButtonBorder() const {
    151   return NULL;
    152 }
    153 
    154 }  // namespace views
    155