| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "views/painter.h" | |
| 6 | |
| 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" | |
| 14 #include "ui/gfx/point.h" | |
| 15 | |
| 16 namespace views { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 class GradientPainter : public Painter { | |
| 21 public: | |
| 22 GradientPainter(bool horizontal, const SkColor& top, const SkColor& bottom) | |
| 23 : horizontal_(horizontal) { | |
| 24 colors_[0] = top; | |
| 25 colors_[1] = bottom; | |
| 26 } | |
| 27 | |
| 28 virtual ~GradientPainter() { | |
| 29 } | |
| 30 | |
| 31 void Paint(int w, int h, gfx::Canvas* canvas) { | |
| 32 SkPaint paint; | |
| 33 SkPoint p[2]; | |
| 34 p[0].set(SkIntToScalar(0), SkIntToScalar(0)); | |
| 35 if (horizontal_) | |
| 36 p[1].set(SkIntToScalar(w), SkIntToScalar(0)); | |
| 37 else | |
| 38 p[1].set(SkIntToScalar(0), SkIntToScalar(h)); | |
| 39 | |
| 40 SkShader* s = | |
| 41 SkGradientShader::CreateLinear(p, colors_, NULL, 2, | |
| 42 SkShader::kClamp_TileMode, NULL); | |
| 43 paint.setStyle(SkPaint::kFill_Style); | |
| 44 paint.setShader(s); | |
| 45 // Need to unref shader, otherwise never deleted. | |
| 46 s->unref(); | |
| 47 | |
| 48 canvas->GetSkCanvas()->drawRectCoords( | |
| 49 SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(w), SkIntToScalar(h), | |
| 50 paint); | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 bool horizontal_; | |
| 55 SkColor colors_[2]; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(GradientPainter); | |
| 58 }; | |
| 59 | |
| 60 | |
| 61 class ImagePainter : public Painter { | |
| 62 public: | |
| 63 ImagePainter(const SkBitmap& image, | |
| 64 const gfx::Insets& insets, | |
| 65 bool paint_center) | |
| 66 : image_(image), | |
| 67 insets_(insets), | |
| 68 paint_center_(paint_center) { | |
| 69 DCHECK(image.width() > insets.width() && | |
| 70 image.height() > insets.height()); | |
| 71 } | |
| 72 | |
| 73 // Paints the images. | |
| 74 virtual void Paint(int w, int h, gfx::Canvas* canvas) { | |
| 75 if (w == image_.width() && h == image_.height()) { | |
| 76 // Early out if the size we're to render at equals the size of the image. | |
| 77 canvas->DrawBitmapInt(image_, 0, 0); | |
| 78 return; | |
| 79 } | |
| 80 // Upper left. | |
| 81 canvas->DrawBitmapInt(image_, 0, 0, insets_.left(), insets_.top(), | |
| 82 0, 0, insets_.left(), insets_.top(), true); | |
| 83 // Top edge. | |
| 84 canvas->DrawBitmapInt( | |
| 85 image_, | |
| 86 insets_.left(), 0, image_.width() - insets_.width(), insets_.top(), | |
| 87 insets_.left(), 0, w - insets_.width(), insets_.top(), true); | |
| 88 // Upper right. | |
| 89 canvas->DrawBitmapInt( | |
| 90 image_, | |
| 91 image_.width() - insets_.right(), 0, insets_.right(), insets_.top(), | |
| 92 w - insets_.right(), 0, insets_.right(), insets_.top(), true); | |
| 93 // Right edge. | |
| 94 canvas->DrawBitmapInt( | |
| 95 image_, | |
| 96 image_.width() - insets_.right(), insets_.top(), | |
| 97 insets_.right(), image_.height() - insets_.height(), | |
| 98 w - insets_.right(), insets_.top(), insets_.right(), | |
| 99 h - insets_.height(), true); | |
| 100 // Bottom right. | |
| 101 canvas->DrawBitmapInt( | |
| 102 image_, | |
| 103 image_.width() - insets_.right(), image_.height() - insets_.bottom(), | |
| 104 insets_.right(), insets_.bottom(), | |
| 105 w - insets_.right(), h - insets_.bottom(), insets_.right(), | |
| 106 insets_.bottom(), true); | |
| 107 // Bottom edge. | |
| 108 canvas->DrawBitmapInt( | |
| 109 image_, | |
| 110 insets_.left(), image_.height() - insets_.bottom(), | |
| 111 image_.width() - insets_.width(), insets_.bottom(), | |
| 112 insets_.left(), h - insets_.bottom(), w - insets_.width(), | |
| 113 insets_.bottom(), true); | |
| 114 // Bottom left. | |
| 115 canvas->DrawBitmapInt( | |
| 116 image_, | |
| 117 0, image_.height() - insets_.bottom(), insets_.left(), | |
| 118 insets_.bottom(), | |
| 119 0, h - insets_.bottom(), insets_.left(), insets_.bottom(), true); | |
| 120 // Left. | |
| 121 canvas->DrawBitmapInt( | |
| 122 image_, | |
| 123 0, insets_.top(), insets_.left(), image_.height() - insets_.height(), | |
| 124 0, insets_.top(), insets_.left(), h - insets_.height(), true); | |
| 125 // Center. | |
| 126 if (paint_center_) { | |
| 127 canvas->DrawBitmapInt( | |
| 128 image_, | |
| 129 insets_.left(), insets_.top(), | |
| 130 image_.width() - insets_.width(), image_.height() - insets_.height(), | |
| 131 insets_.left(), insets_.top(), | |
| 132 w - insets_.width(), h - insets_.height(), true); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 private: | |
| 137 const SkBitmap image_; | |
| 138 const gfx::Insets insets_; | |
| 139 bool paint_center_; | |
| 140 | |
| 141 DISALLOW_COPY_AND_ASSIGN(ImagePainter); | |
| 142 }; | |
| 143 | |
| 144 } // namespace | |
| 145 | |
| 146 // static | |
| 147 void Painter::PaintPainterAt(int x, int y, int w, int h, | |
| 148 gfx::Canvas* canvas, Painter* painter) { | |
| 149 DCHECK(canvas && painter); | |
| 150 if (w < 0 || h < 0) | |
| 151 return; | |
| 152 canvas->Save(); | |
| 153 canvas->Translate(gfx::Point(x, y)); | |
| 154 painter->Paint(w, h, canvas); | |
| 155 canvas->Restore(); | |
| 156 } | |
| 157 | |
| 158 // static | |
| 159 Painter* Painter::CreateHorizontalGradient(SkColor c1, SkColor c2) { | |
| 160 return new GradientPainter(true, c1, c2); | |
| 161 } | |
| 162 | |
| 163 // static | |
| 164 Painter* Painter::CreateVerticalGradient(SkColor c1, SkColor c2) { | |
| 165 return new GradientPainter(false, c1, c2); | |
| 166 } | |
| 167 | |
| 168 // static | |
| 169 Painter* Painter::CreateImagePainter(const SkBitmap& image, | |
| 170 const gfx::Insets& insets, | |
| 171 bool paint_center) { | |
| 172 return new ImagePainter(image, insets, paint_center); | |
| 173 } | |
| 174 | |
| 175 HorizontalPainter::HorizontalPainter(const int image_resource_names[]) { | |
| 176 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 177 for (int i = 0; i < 3; ++i) | |
| 178 images_[i] = rb.GetBitmapNamed(image_resource_names[i]); | |
| 179 height_ = images_[LEFT]->height(); | |
| 180 DCHECK(images_[LEFT]->height() == images_[RIGHT]->height() && | |
| 181 images_[LEFT]->height() == images_[CENTER]->height()); | |
| 182 } | |
| 183 | |
| 184 void HorizontalPainter::Paint(int w, int h, gfx::Canvas* canvas) { | |
| 185 if (w < (images_[LEFT]->width() + images_[CENTER]->width() + | |
| 186 images_[RIGHT]->width())) { | |
| 187 // No room to paint. | |
| 188 return; | |
| 189 } | |
| 190 canvas->DrawBitmapInt(*images_[LEFT], 0, 0); | |
| 191 canvas->DrawBitmapInt(*images_[RIGHT], w - images_[RIGHT]->width(), 0); | |
| 192 canvas->TileImageInt(*images_[CENTER], | |
| 193 images_[LEFT]->width(), | |
| 194 0, | |
| 195 w - images_[LEFT]->width() - images_[RIGHT]->width(), | |
| 196 height_); | |
| 197 } | |
| 198 | |
| 199 } // namespace views | |
| OLD | NEW |