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.
5 #include "ui/views/border.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/views/painter.h"
16 // A simple border with different thicknesses on each side and single color.
17 class SidedSolidBorder
: public Border
{
19 SidedSolidBorder(int top
, int left
, int bottom
, int right
, SkColor color
);
21 // Overridden from Border:
22 virtual void Paint(const View
& view
, gfx::Canvas
* canvas
) OVERRIDE
;
23 virtual gfx::Insets
GetInsets() const OVERRIDE
;
27 const gfx::Insets insets_
;
29 DISALLOW_COPY_AND_ASSIGN(SidedSolidBorder
);
32 SidedSolidBorder::SidedSolidBorder(int top
,
38 insets_(top
, left
, bottom
, right
) {
41 void SidedSolidBorder::Paint(const View
& view
, gfx::Canvas
* canvas
) {
43 canvas
->FillRect(gfx::Rect(0, 0, view
.width(), insets_
.top()), color_
);
45 canvas
->FillRect(gfx::Rect(0, 0, insets_
.left(), view
.height()), color_
);
47 canvas
->FillRect(gfx::Rect(0, view
.height() - insets_
.bottom(), view
.width(),
48 insets_
.bottom()), color_
);
50 canvas
->FillRect(gfx::Rect(view
.width() - insets_
.right(), 0, insets_
.right(),
51 view
.height()), color_
);
54 gfx::Insets
SidedSolidBorder::GetInsets() const {
58 // A variation of SidedSolidBorder, where each side has the same thickness.
59 class SolidBorder
: public SidedSolidBorder
{
61 SolidBorder(int thickness
, SkColor color
)
62 : SidedSolidBorder(thickness
, thickness
, thickness
, thickness
, color
) {
66 DISALLOW_COPY_AND_ASSIGN(SolidBorder
);
69 class EmptyBorder
: public Border
{
71 EmptyBorder(int top
, int left
, int bottom
, int right
)
72 : insets_(top
, left
, bottom
, right
) {}
74 // Overridden from Border:
75 virtual void Paint(const View
& view
, gfx::Canvas
* canvas
) OVERRIDE
{}
77 virtual gfx::Insets
GetInsets() const OVERRIDE
{
82 const gfx::Insets insets_
;
84 DISALLOW_COPY_AND_ASSIGN(EmptyBorder
);
87 class BorderPainter
: public Border
{
89 explicit BorderPainter(Painter
* painter
, const gfx::Insets
& insets
)
95 virtual ~BorderPainter() {}
97 // Overridden from Border:
98 virtual void Paint(const View
& view
, gfx::Canvas
* canvas
) OVERRIDE
{
99 Painter::PaintPainterAt(canvas
, painter_
.get(), view
.GetLocalBounds());
102 virtual gfx::Insets
GetInsets() const OVERRIDE
{
107 scoped_ptr
<Painter
> painter_
;
108 const gfx::Insets insets_
;
110 DISALLOW_COPY_AND_ASSIGN(BorderPainter
);
122 Border
* Border::CreateSolidBorder(int thickness
, SkColor color
) {
123 return new SolidBorder(thickness
, color
);
127 Border
* Border::CreateEmptyBorder(int top
, int left
, int bottom
, int right
) {
128 return new EmptyBorder(top
, left
, bottom
, right
);
132 Border
* Border::CreateSolidSidedBorder(int top
,
137 return new SidedSolidBorder(top
, left
, bottom
, right
, color
);
141 Border
* Border::CreateBorderPainter(Painter
* painter
,
142 const gfx::Insets
& insets
) {
143 return new BorderPainter(painter
, insets
);
146 TextButtonBorder
* Border::AsTextButtonBorder() {
150 const TextButtonBorder
* Border::AsTextButtonBorder() const {