| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/border.h" | 5 #include "ui/views/border.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
| 10 #include "ui/views/painter.h" | 10 #include "ui/views/painter.h" |
| 11 #include "ui/views/view.h" | 11 #include "ui/views/view.h" |
| 12 | 12 |
| 13 namespace views { | 13 namespace views { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // A simple border with different thicknesses on each side and single color. | 17 // A simple border with different thicknesses on each side and single color. |
| 18 class SidedSolidBorder : public Border { | 18 class SolidSidedBorder : public Border { |
| 19 public: | 19 public: |
| 20 SidedSolidBorder(int top, int left, int bottom, int right, SkColor color); | 20 SolidSidedBorder(const gfx::Insets& insets, SkColor color); |
| 21 | 21 |
| 22 // Overridden from Border: | 22 // Overridden from Border: |
| 23 void Paint(const View& view, gfx::Canvas* canvas) override; | 23 void Paint(const View& view, gfx::Canvas* canvas) override; |
| 24 gfx::Insets GetInsets() const override; | 24 gfx::Insets GetInsets() const override; |
| 25 gfx::Size GetMinimumSize() const override; | 25 gfx::Size GetMinimumSize() const override; |
| 26 | 26 |
| 27 private: | 27 private: |
| 28 const gfx::Insets insets_; |
| 28 const SkColor color_; | 29 const SkColor color_; |
| 29 const gfx::Insets insets_; | |
| 30 | 30 |
| 31 DISALLOW_COPY_AND_ASSIGN(SidedSolidBorder); | 31 DISALLOW_COPY_AND_ASSIGN(SolidSidedBorder); |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 SidedSolidBorder::SidedSolidBorder(int top, | 34 SolidSidedBorder::SolidSidedBorder(const gfx::Insets& insets, SkColor color) |
| 35 int left, | 35 : insets_(insets), |
| 36 int bottom, | 36 color_(color) { |
| 37 int right, | |
| 38 SkColor color) | |
| 39 : color_(color), | |
| 40 insets_(top, left, bottom, right) { | |
| 41 } | 37 } |
| 42 | 38 |
| 43 void SidedSolidBorder::Paint(const View& view, gfx::Canvas* canvas) { | 39 void SolidSidedBorder::Paint(const View& view, gfx::Canvas* canvas) { |
| 44 // Top border. | 40 // Top border. |
| 45 canvas->FillRect(gfx::Rect(0, 0, view.width(), insets_.top()), color_); | 41 canvas->FillRect(gfx::Rect(0, 0, view.width(), insets_.top()), color_); |
| 46 // Left border. | 42 // Left border. |
| 47 canvas->FillRect(gfx::Rect(0, insets_.top(), insets_.left(), | 43 canvas->FillRect(gfx::Rect(0, insets_.top(), insets_.left(), |
| 48 view.height() - insets_.height()), color_); | 44 view.height() - insets_.height()), color_); |
| 49 // Bottom border. | 45 // Bottom border. |
| 50 canvas->FillRect(gfx::Rect(0, view.height() - insets_.bottom(), view.width(), | 46 canvas->FillRect(gfx::Rect(0, view.height() - insets_.bottom(), view.width(), |
| 51 insets_.bottom()), color_); | 47 insets_.bottom()), color_); |
| 52 // Right border. | 48 // Right border. |
| 53 canvas->FillRect(gfx::Rect(view.width() - insets_.right(), insets_.top(), | 49 canvas->FillRect(gfx::Rect(view.width() - insets_.right(), insets_.top(), |
| 54 insets_.right(), view.height() - insets_.height()), | 50 insets_.right(), view.height() - insets_.height()), |
| 55 color_); | 51 color_); |
| 56 } | 52 } |
| 57 | 53 |
| 58 gfx::Insets SidedSolidBorder::GetInsets() const { | 54 gfx::Insets SolidSidedBorder::GetInsets() const { |
| 59 return insets_; | 55 return insets_; |
| 60 } | 56 } |
| 61 | 57 |
| 62 gfx::Size SidedSolidBorder::GetMinimumSize() const { | 58 gfx::Size SolidSidedBorder::GetMinimumSize() const { |
| 63 return gfx::Size(insets_.width(), insets_.height()); | 59 return gfx::Size(insets_.width(), insets_.height()); |
| 64 } | 60 } |
| 65 | 61 |
| 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) { | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 DISALLOW_COPY_AND_ASSIGN(SolidBorder); | |
| 75 }; | |
| 76 | |
| 77 class EmptyBorder : public Border { | 62 class EmptyBorder : public Border { |
| 78 public: | 63 public: |
| 79 EmptyBorder(int top, int left, int bottom, int right) | 64 explicit EmptyBorder(const gfx::Insets& insets); |
| 80 : insets_(top, left, bottom, right) {} | |
| 81 | 65 |
| 82 // Overridden from Border: | 66 // Overridden from Border: |
| 83 void Paint(const View& view, gfx::Canvas* canvas) override {} | 67 void Paint(const View& view, gfx::Canvas* canvas) override; |
| 84 | 68 gfx::Insets GetInsets() const override ; |
| 85 gfx::Insets GetInsets() const override { return insets_; } | 69 gfx::Size GetMinimumSize() const override; |
| 86 | |
| 87 gfx::Size GetMinimumSize() const override { return gfx::Size(); } | |
| 88 | 70 |
| 89 private: | 71 private: |
| 90 const gfx::Insets insets_; | 72 const gfx::Insets insets_; |
| 91 | 73 |
| 92 DISALLOW_COPY_AND_ASSIGN(EmptyBorder); | 74 DISALLOW_COPY_AND_ASSIGN(EmptyBorder); |
| 93 }; | 75 }; |
| 94 | 76 |
| 77 EmptyBorder::EmptyBorder(const gfx::Insets& insets) : insets_(insets) { |
| 78 } |
| 79 |
| 80 void EmptyBorder::Paint(const View& view, gfx::Canvas* canvas) { |
| 81 } |
| 82 |
| 83 gfx::Insets EmptyBorder::GetInsets() const { |
| 84 return insets_; |
| 85 } |
| 86 |
| 87 gfx::Size EmptyBorder::GetMinimumSize() const { |
| 88 return gfx::Size(); |
| 89 } |
| 90 |
| 95 class BorderPainter : public Border { | 91 class BorderPainter : public Border { |
| 96 public: | 92 public: |
| 97 explicit BorderPainter(Painter* painter, const gfx::Insets& insets) | 93 BorderPainter(Painter* painter, const gfx::Insets& insets); |
| 98 : painter_(painter), | |
| 99 insets_(insets) { | |
| 100 DCHECK(painter); | |
| 101 } | |
| 102 | |
| 103 ~BorderPainter() override {} | |
| 104 | 94 |
| 105 // Overridden from Border: | 95 // Overridden from Border: |
| 106 void Paint(const View& view, gfx::Canvas* canvas) override { | 96 void Paint(const View& view, gfx::Canvas* canvas) override; |
| 107 Painter::PaintPainterAt(canvas, painter_.get(), view.GetLocalBounds()); | 97 gfx::Insets GetInsets() const override; |
| 108 } | 98 gfx::Size GetMinimumSize() const override; |
| 109 | |
| 110 gfx::Insets GetInsets() const override { return insets_; } | |
| 111 | |
| 112 gfx::Size GetMinimumSize() const override { | |
| 113 return painter_->GetMinimumSize(); | |
| 114 } | |
| 115 | 99 |
| 116 private: | 100 private: |
| 117 scoped_ptr<Painter> painter_; | 101 scoped_ptr<Painter> painter_; |
| 118 const gfx::Insets insets_; | 102 const gfx::Insets insets_; |
| 119 | 103 |
| 120 DISALLOW_COPY_AND_ASSIGN(BorderPainter); | 104 DISALLOW_COPY_AND_ASSIGN(BorderPainter); |
| 121 }; | 105 }; |
| 122 | 106 |
| 107 BorderPainter::BorderPainter(Painter* painter, const gfx::Insets& insets) |
| 108 : painter_(painter), |
| 109 insets_(insets) { |
| 110 DCHECK(painter); |
| 111 } |
| 112 |
| 113 void BorderPainter::Paint(const View& view, gfx::Canvas* canvas) { |
| 114 Painter::PaintPainterAt(canvas, painter_.get(), view.GetLocalBounds()); |
| 115 } |
| 116 |
| 117 gfx::Insets BorderPainter::GetInsets() const { |
| 118 return insets_; |
| 119 } |
| 120 |
| 121 gfx::Size BorderPainter::GetMinimumSize() const { |
| 122 return painter_->GetMinimumSize(); |
| 123 } |
| 124 |
| 123 } // namespace | 125 } // namespace |
| 124 | 126 |
| 125 Border::Border() { | 127 Border::Border() { |
| 126 } | 128 } |
| 127 | 129 |
| 128 Border::~Border() { | 130 Border::~Border() { |
| 129 } | 131 } |
| 130 | 132 |
| 131 // static | 133 // static |
| 132 scoped_ptr<Border> Border::NullBorder() { | 134 scoped_ptr<Border> Border::NullBorder() { |
| 133 return nullptr; | 135 return nullptr; |
| 134 } | 136 } |
| 135 | 137 |
| 136 // static | 138 // static |
| 137 scoped_ptr<Border> Border::CreateSolidBorder(int thickness, SkColor color) { | 139 scoped_ptr<Border> Border::CreateSolidBorder(int thickness, SkColor color) { |
| 138 return make_scoped_ptr(new SolidBorder(thickness, color)); | 140 return make_scoped_ptr(new SolidSidedBorder( |
| 141 gfx::Insets(thickness, thickness, thickness, thickness), color)); |
| 139 } | 142 } |
| 140 | 143 |
| 141 // static | 144 // static |
| 145 scoped_ptr<Border> Border::CreateEmptyBorder(const gfx::Insets& insets) { |
| 146 return make_scoped_ptr(new EmptyBorder(insets)); |
| 147 } |
| 148 |
| 149 // static |
| 142 scoped_ptr<Border> Border::CreateEmptyBorder(int top, | 150 scoped_ptr<Border> Border::CreateEmptyBorder(int top, |
| 143 int left, | 151 int left, |
| 144 int bottom, | 152 int bottom, |
| 145 int right) { | 153 int right) { |
| 146 return make_scoped_ptr(new EmptyBorder(top, left, bottom, right)); | 154 return CreateEmptyBorder(gfx::Insets(top, left, bottom, right)); |
| 147 } | 155 } |
| 148 | 156 |
| 149 // static | 157 // static |
| 150 scoped_ptr<Border> Border::CreateSolidSidedBorder(int top, | 158 scoped_ptr<Border> Border::CreateSolidSidedBorder(int top, |
| 151 int left, | 159 int left, |
| 152 int bottom, | 160 int bottom, |
| 153 int right, | 161 int right, |
| 154 SkColor color) { | 162 SkColor color) { |
| 155 return make_scoped_ptr(new SidedSolidBorder(top, left, bottom, right, color)); | 163 return make_scoped_ptr(new SolidSidedBorder( |
| 164 gfx::Insets(top, left, bottom, right), color)); |
| 156 } | 165 } |
| 157 | 166 |
| 158 // static | 167 // static |
| 159 scoped_ptr<Border> Border::CreateBorderPainter(Painter* painter, | 168 scoped_ptr<Border> Border::CreateBorderPainter(Painter* painter, |
| 160 const gfx::Insets& insets) { | 169 const gfx::Insets& insets) { |
| 161 return make_scoped_ptr(new BorderPainter(painter, insets)); | 170 return make_scoped_ptr(new BorderPainter(painter, insets)); |
| 162 } | 171 } |
| 163 | 172 |
| 164 } // namespace views | 173 } // namespace views |
| OLD | NEW |