Allow TCP proxy for SSHClient extensions ID
[chromium-blink-merge.git] / views / painter.cc
blob5a82bff559b13e5298da08715a10a2a7524e4667
1 // Copyright (c) 2009 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 "views/painter.h"
7 #include "base/logging.h"
8 #include "third_party/skia/include/core/SkBitmap.h"
9 #include "third_party/skia/include/effects/SkGradientShader.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/canvas_skia.h"
13 #include "ui/gfx/insets.h"
15 namespace views {
17 namespace {
19 class GradientPainter : public Painter {
20 public:
21 GradientPainter(bool horizontal, const SkColor& top, const SkColor& bottom)
22 : horizontal_(horizontal) {
23 colors_[0] = top;
24 colors_[1] = bottom;
27 virtual ~GradientPainter() {
30 void Paint(int w, int h, gfx::Canvas* canvas) {
31 SkPaint paint;
32 SkPoint p[2];
33 p[0].set(SkIntToScalar(0), SkIntToScalar(0));
34 if (horizontal_)
35 p[1].set(SkIntToScalar(w), SkIntToScalar(0));
36 else
37 p[1].set(SkIntToScalar(0), SkIntToScalar(h));
39 SkShader* s =
40 SkGradientShader::CreateLinear(p, colors_, NULL, 2,
41 SkShader::kClamp_TileMode, NULL);
42 paint.setStyle(SkPaint::kFill_Style);
43 paint.setShader(s);
44 // Need to unref shader, otherwise never deleted.
45 s->unref();
47 canvas->AsCanvasSkia()->drawRectCoords(
48 SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(w), SkIntToScalar(h),
49 paint);
52 private:
53 bool horizontal_;
54 SkColor colors_[2];
56 DISALLOW_COPY_AND_ASSIGN(GradientPainter);
60 class ImagePainter : public Painter {
61 public:
62 ImagePainter(const SkBitmap& image,
63 const gfx::Insets& insets,
64 bool paint_center)
65 : image_(image),
66 insets_(insets),
67 paint_center_(paint_center) {
68 DCHECK(image.width() > insets.width() &&
69 image.height() > insets.height());
72 // Paints the images.
73 virtual void Paint(int w, int h, gfx::Canvas* canvas) {
74 if (w == image_.width() && h == image_.height()) {
75 // Early out if the size we're to render at equals the size of the image.
76 canvas->DrawBitmapInt(image_, 0, 0);
77 return;
79 // Upper left.
80 canvas->DrawBitmapInt(image_, 0, 0, insets_.left(), insets_.top(),
81 0, 0, insets_.left(), insets_.top(), true);
82 // Top edge.
83 canvas->DrawBitmapInt(
84 image_,
85 insets_.left(), 0, image_.width() - insets_.width(), insets_.top(),
86 insets_.left(), 0, w - insets_.width(), insets_.top(), true);
87 // Upper right.
88 canvas->DrawBitmapInt(
89 image_,
90 image_.width() - insets_.right(), 0, insets_.right(), insets_.top(),
91 w - insets_.right(), 0, insets_.right(), insets_.top(), true);
92 // Right edge.
93 canvas->DrawBitmapInt(
94 image_,
95 image_.width() - insets_.right(), insets_.top(),
96 insets_.right(), image_.height() - insets_.height(),
97 w - insets_.right(), insets_.top(), insets_.right(),
98 h - insets_.height(), true);
99 // Bottom right.
100 canvas->DrawBitmapInt(
101 image_,
102 image_.width() - insets_.right(), image_.height() - insets_.bottom(),
103 insets_.right(), insets_.bottom(),
104 w - insets_.right(), h - insets_.bottom(), insets_.right(),
105 insets_.bottom(), true);
106 // Bottom edge.
107 canvas->DrawBitmapInt(
108 image_,
109 insets_.left(), image_.height() - insets_.bottom(),
110 image_.width() - insets_.width(), insets_.bottom(),
111 insets_.left(), h - insets_.bottom(), w - insets_.width(),
112 insets_.bottom(), true);
113 // Bottom left.
114 canvas->DrawBitmapInt(
115 image_,
116 0, image_.height() - insets_.bottom(), insets_.left(),
117 insets_.bottom(),
118 0, h - insets_.bottom(), insets_.left(), insets_.bottom(), true);
119 // Left.
120 canvas->DrawBitmapInt(
121 image_,
122 0, insets_.top(), insets_.left(), image_.height() - insets_.height(),
123 0, insets_.top(), insets_.left(), h - insets_.height(), true);
124 // Center.
125 if (paint_center_) {
126 canvas->DrawBitmapInt(
127 image_,
128 insets_.left(), insets_.top(),
129 image_.width() - insets_.width(), image_.height() - insets_.height(),
130 insets_.left(), insets_.top(),
131 w - insets_.width(), h - insets_.height(), true);
135 private:
136 const SkBitmap image_;
137 const gfx::Insets insets_;
138 bool paint_center_;
140 DISALLOW_COPY_AND_ASSIGN(ImagePainter);
143 } // namespace
145 // static
146 void Painter::PaintPainterAt(int x, int y, int w, int h,
147 gfx::Canvas* canvas, Painter* painter) {
148 DCHECK(canvas && painter);
149 if (w < 0 || h < 0)
150 return;
151 canvas->Save();
152 canvas->TranslateInt(x, y);
153 painter->Paint(w, h, canvas);
154 canvas->Restore();
157 // static
158 Painter* Painter::CreateHorizontalGradient(SkColor c1, SkColor c2) {
159 return new GradientPainter(true, c1, c2);
162 // static
163 Painter* Painter::CreateVerticalGradient(SkColor c1, SkColor c2) {
164 return new GradientPainter(false, c1, c2);
167 // static
168 Painter* Painter::CreateImagePainter(const SkBitmap& image,
169 const gfx::Insets& insets,
170 bool paint_center) {
171 return new ImagePainter(image, insets, paint_center);
174 HorizontalPainter::HorizontalPainter(const int image_resource_names[]) {
175 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
176 for (int i = 0; i < 3; ++i)
177 images_[i] = rb.GetBitmapNamed(image_resource_names[i]);
178 height_ = images_[LEFT]->height();
179 DCHECK(images_[LEFT]->height() == images_[RIGHT]->height() &&
180 images_[LEFT]->height() == images_[CENTER]->height());
183 void HorizontalPainter::Paint(int w, int h, gfx::Canvas* canvas) {
184 if (w < (images_[LEFT]->width() + images_[CENTER]->width() +
185 images_[RIGHT]->width())) {
186 // No room to paint.
187 return;
189 canvas->DrawBitmapInt(*images_[LEFT], 0, 0);
190 canvas->DrawBitmapInt(*images_[RIGHT], w - images_[RIGHT]->width(), 0);
191 canvas->TileImageInt(*images_[CENTER],
192 images_[LEFT]->width(),
194 w - images_[LEFT]->width() - images_[RIGHT]->width(),
195 height_);
198 } // namespace views