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