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 void Paint(const View
& view
, gfx::Canvas
* canvas
) override
;
24 gfx::Insets
GetInsets() const override
;
25 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, insets_
.top(), insets_
.left(),
48 view
.height() - insets_
.height()), color_
);
50 canvas
->FillRect(gfx::Rect(0, view
.height() - insets_
.bottom(), view
.width(),
51 insets_
.bottom()), color_
);
53 canvas
->FillRect(gfx::Rect(view
.width() - insets_
.right(), insets_
.top(),
54 insets_
.right(), view
.height() - insets_
.height()),
58 gfx::Insets
SidedSolidBorder::GetInsets() const {
62 gfx::Size
SidedSolidBorder::GetMinimumSize() const {
63 return gfx::Size(insets_
.width(), insets_
.height());
66 // A variation of SidedSolidBorder, where each side has the same thickness.
67 class SolidBorder
: public SidedSolidBorder
{
69 SolidBorder(int thickness
, SkColor color
)
70 : SidedSolidBorder(thickness
, thickness
, thickness
, thickness
, color
) {
74 DISALLOW_COPY_AND_ASSIGN(SolidBorder
);
77 class EmptyBorder
: public Border
{
79 EmptyBorder(int top
, int left
, int bottom
, int right
)
80 : insets_(top
, left
, bottom
, right
) {}
82 // Overridden from Border:
83 void Paint(const View
& view
, gfx::Canvas
* canvas
) override
{}
85 gfx::Insets
GetInsets() const override
{ return insets_
; }
87 gfx::Size
GetMinimumSize() const override
{ return gfx::Size(); }
90 const gfx::Insets insets_
;
92 DISALLOW_COPY_AND_ASSIGN(EmptyBorder
);
95 class BorderPainter
: public Border
{
97 explicit BorderPainter(Painter
* painter
, const gfx::Insets
& insets
)
103 ~BorderPainter() override
{}
105 // Overridden from Border:
106 void Paint(const View
& view
, gfx::Canvas
* canvas
) override
{
107 Painter::PaintPainterAt(canvas
, painter_
.get(), view
.GetLocalBounds());
110 gfx::Insets
GetInsets() const override
{ return insets_
; }
112 gfx::Size
GetMinimumSize() const override
{
113 return painter_
->GetMinimumSize();
117 scoped_ptr
<Painter
> painter_
;
118 const gfx::Insets insets_
;
120 DISALLOW_COPY_AND_ASSIGN(BorderPainter
);
132 scoped_ptr
<Border
> Border::NullBorder() {
137 scoped_ptr
<Border
> Border::CreateSolidBorder(int thickness
, SkColor color
) {
138 return make_scoped_ptr(new SolidBorder(thickness
, color
));
142 scoped_ptr
<Border
> Border::CreateEmptyBorder(int top
,
146 return make_scoped_ptr(new EmptyBorder(top
, left
, bottom
, right
));
150 scoped_ptr
<Border
> Border::CreateSolidSidedBorder(int top
,
155 return make_scoped_ptr(new SidedSolidBorder(top
, left
, bottom
, right
, color
));
159 scoped_ptr
<Border
> Border::CreateBorderPainter(Painter
* painter
,
160 const gfx::Insets
& insets
) {
161 return make_scoped_ptr(new BorderPainter(painter
, insets
));