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"
11 #include "ui/views/view.h"
17 // A simple border with different thicknesses on each side and single color.
18 class SidedSolidBorder
: public Border
{
20 SidedSolidBorder(int top
, int left
, int bottom
, int right
, SkColor color
);
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
;
29 const gfx::Insets insets_
;
31 DISALLOW_COPY_AND_ASSIGN(SidedSolidBorder
);
34 SidedSolidBorder::SidedSolidBorder(int top
,
40 insets_(top
, left
, bottom
, right
) {
43 void SidedSolidBorder::Paint(const View
& view
, gfx::Canvas
* canvas
) {
45 canvas
->FillRect(gfx::Rect(0, 0, view
.width(), insets_
.top()), color_
);
47 canvas
->FillRect(gfx::Rect(0, 0, insets_
.left(), view
.height()), color_
);
49 canvas
->FillRect(gfx::Rect(0, view
.height() - insets_
.bottom(), view
.width(),
50 insets_
.bottom()), color_
);
52 canvas
->FillRect(gfx::Rect(view
.width() - insets_
.right(), 0, insets_
.right(),
53 view
.height()), color_
);
56 gfx::Insets
SidedSolidBorder::GetInsets() const {
60 gfx::Size
SidedSolidBorder::GetMinimumSize() const {
61 return gfx::Size(insets_
.width(), insets_
.height());
64 // A variation of SidedSolidBorder, where each side has the same thickness.
65 class SolidBorder
: public SidedSolidBorder
{
67 SolidBorder(int thickness
, SkColor color
)
68 : SidedSolidBorder(thickness
, thickness
, thickness
, thickness
, color
) {
72 DISALLOW_COPY_AND_ASSIGN(SolidBorder
);
75 class EmptyBorder
: public Border
{
77 EmptyBorder(int top
, int left
, int bottom
, int right
)
78 : insets_(top
, left
, bottom
, right
) {}
80 // Overridden from Border:
81 virtual void Paint(const View
& view
, gfx::Canvas
* canvas
) OVERRIDE
{}
83 virtual gfx::Insets
GetInsets() const OVERRIDE
{
87 virtual gfx::Size
GetMinimumSize() const OVERRIDE
{
92 const gfx::Insets insets_
;
94 DISALLOW_COPY_AND_ASSIGN(EmptyBorder
);
97 class BorderPainter
: public Border
{
99 explicit BorderPainter(Painter
* painter
, const gfx::Insets
& insets
)
105 virtual ~BorderPainter() {}
107 // Overridden from Border:
108 virtual void Paint(const View
& view
, gfx::Canvas
* canvas
) OVERRIDE
{
109 Painter::PaintPainterAt(canvas
, painter_
.get(), view
.GetLocalBounds());
112 virtual gfx::Insets
GetInsets() const OVERRIDE
{
116 virtual gfx::Size
GetMinimumSize() const OVERRIDE
{
117 return painter_
->GetMinimumSize();
121 scoped_ptr
<Painter
> painter_
;
122 const gfx::Insets insets_
;
124 DISALLOW_COPY_AND_ASSIGN(BorderPainter
);
136 scoped_ptr
<Border
> Border::NullBorder() {
137 return scoped_ptr
<Border
>();
141 scoped_ptr
<Border
> Border::CreateSolidBorder(int thickness
, SkColor color
) {
142 return scoped_ptr
<Border
>(new SolidBorder(thickness
, color
));
146 scoped_ptr
<Border
> Border::CreateEmptyBorder(int top
,
150 return scoped_ptr
<Border
>(new EmptyBorder(top
, left
, bottom
, right
));
154 scoped_ptr
<Border
> Border::CreateSolidSidedBorder(int top
,
159 return scoped_ptr
<Border
>(
160 new SidedSolidBorder(top
, left
, bottom
, right
, color
));
164 scoped_ptr
<Border
> Border::CreateBorderPainter(Painter
* painter
,
165 const gfx::Insets
& insets
) {
166 return scoped_ptr
<Border
>(new BorderPainter(painter
, insets
));