Automated Commit: Committing new LKGM version 7479.0.0 for chromeos.
[chromium-blink-merge.git] / ui / views / border.cc
blobed024762374e51f740383b55a76341e17fdf58c3
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"
13 namespace views {
15 namespace {
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);
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;
27 private:
28 const SkColor color_;
29 const gfx::Insets insets_;
31 DISALLOW_COPY_AND_ASSIGN(SidedSolidBorder);
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) {
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, insets_.top(), insets_.left(),
48 view.height() - insets_.height()), color_);
49 // Bottom border.
50 canvas->FillRect(gfx::Rect(0, view.height() - insets_.bottom(), view.width(),
51 insets_.bottom()), color_);
52 // Right border.
53 canvas->FillRect(gfx::Rect(view.width() - insets_.right(), insets_.top(),
54 insets_.right(), view.height() - insets_.height()),
55 color_);
58 gfx::Insets SidedSolidBorder::GetInsets() const {
59 return insets_;
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 {
68 public:
69 SolidBorder(int thickness, SkColor color)
70 : SidedSolidBorder(thickness, thickness, thickness, thickness, color) {
73 private:
74 DISALLOW_COPY_AND_ASSIGN(SolidBorder);
77 class EmptyBorder : public Border {
78 public:
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(); }
89 private:
90 const gfx::Insets insets_;
92 DISALLOW_COPY_AND_ASSIGN(EmptyBorder);
95 class BorderPainter : public Border {
96 public:
97 explicit BorderPainter(Painter* painter, const gfx::Insets& insets)
98 : painter_(painter),
99 insets_(insets) {
100 DCHECK(painter);
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();
116 private:
117 scoped_ptr<Painter> painter_;
118 const gfx::Insets insets_;
120 DISALLOW_COPY_AND_ASSIGN(BorderPainter);
123 } // namespace
125 Border::Border() {
128 Border::~Border() {
131 // static
132 scoped_ptr<Border> Border::NullBorder() {
133 return nullptr;
136 // static
137 scoped_ptr<Border> Border::CreateSolidBorder(int thickness, SkColor color) {
138 return make_scoped_ptr(new SolidBorder(thickness, color));
141 // static
142 scoped_ptr<Border> Border::CreateEmptyBorder(int top,
143 int left,
144 int bottom,
145 int right) {
146 return make_scoped_ptr(new EmptyBorder(top, left, bottom, right));
149 // static
150 scoped_ptr<Border> Border::CreateSolidSidedBorder(int top,
151 int left,
152 int bottom,
153 int right,
154 SkColor color) {
155 return make_scoped_ptr(new SidedSolidBorder(top, left, bottom, right, color));
158 // static
159 scoped_ptr<Border> Border::CreateBorderPainter(Painter* painter,
160 const gfx::Insets& insets) {
161 return make_scoped_ptr(new BorderPainter(painter, insets));
164 } // namespace views