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/painter.h" | 5 #include "ui/views/painter.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 "third_party/skia/include/effects/SkGradientShader.h" | 9 #include "third_party/skia/include/effects/SkGradientShader.h" |
10 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 ImagePainter(const gfx::ImageSkia& image, const gfx::Insets& insets); | 102 ImagePainter(const gfx::ImageSkia& image, const gfx::Insets& insets); |
103 | 103 |
104 virtual ~ImagePainter(); | 104 virtual ~ImagePainter(); |
105 | 105 |
106 // Returns true if the images are empty. | 106 // Returns true if the images are empty. |
107 bool IsEmpty() const; | 107 bool IsEmpty() const; |
108 | 108 |
109 // Painter: | 109 // Painter: |
110 virtual gfx::Size GetMinimumSize() const OVERRIDE; | 110 virtual gfx::Size GetMinimumSize() const OVERRIDE; |
111 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE; | 111 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE; |
| 112 virtual void PaintWithSkPaint(gfx::Canvas* canvas, |
| 113 const gfx::Size& size, |
| 114 const SkPaint& paint) OVERRIDE; |
112 | 115 |
113 private: | 116 private: |
114 // Stretches the given image over the specified canvas area. | 117 // Stretches the given image over the specified canvas area. |
115 static void Fill(gfx::Canvas* c, | 118 static void Fill(gfx::Canvas* c, |
116 const gfx::ImageSkia& i, | 119 const gfx::ImageSkia& i, |
117 int x, | 120 int x, |
118 int y, | 121 int y, |
119 int w, | 122 int w, |
120 int h); | 123 int h, |
| 124 const SkPaint& paint); |
121 | 125 |
122 // Images are numbered as depicted below. | 126 // Images are numbered as depicted below. |
123 // ____________________ | 127 // ____________________ |
124 // |__i0__|__i1__|__i2__| | 128 // |__i0__|__i1__|__i2__| |
125 // |__i3__|__i4__|__i5__| | 129 // |__i3__|__i4__|__i5__| |
126 // |__i6__|__i7__|__i8__| | 130 // |__i6__|__i7__|__i8__| |
127 gfx::ImageSkia images_[9]; | 131 gfx::ImageSkia images_[9]; |
128 | 132 |
129 DISALLOW_COPY_AND_ASSIGN(ImagePainter); | 133 DISALLOW_COPY_AND_ASSIGN(ImagePainter); |
130 }; | 134 }; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 return images_[0].isNull(); | 166 return images_[0].isNull(); |
163 } | 167 } |
164 | 168 |
165 gfx::Size ImagePainter::GetMinimumSize() const { | 169 gfx::Size ImagePainter::GetMinimumSize() const { |
166 return IsEmpty() ? gfx::Size() : gfx::Size( | 170 return IsEmpty() ? gfx::Size() : gfx::Size( |
167 images_[0].width() + images_[1].width() + images_[2].width(), | 171 images_[0].width() + images_[1].width() + images_[2].width(), |
168 images_[0].height() + images_[3].height() + images_[6].height()); | 172 images_[0].height() + images_[3].height() + images_[6].height()); |
169 } | 173 } |
170 | 174 |
171 void ImagePainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) { | 175 void ImagePainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) { |
| 176 PaintWithSkPaint(canvas, size, SkPaint()); |
| 177 } |
| 178 |
| 179 void ImagePainter::PaintWithSkPaint(gfx::Canvas* canvas, |
| 180 const gfx::Size& size, |
| 181 const SkPaint& paint) { |
172 if (IsEmpty()) | 182 if (IsEmpty()) |
173 return; | 183 return; |
174 | 184 |
175 // In case the corners and edges don't all have the same width/height, we draw | 185 // In case the corners and edges don't all have the same width/height, we draw |
176 // the center first, and extend it out in all directions to the edges of the | 186 // the center first, and extend it out in all directions to the edges of the |
177 // images with the smallest widths/heights. This way there will be no | 187 // images with the smallest widths/heights. This way there will be no |
178 // unpainted areas, though some corners or edges might overlap the center. | 188 // unpainted areas, though some corners or edges might overlap the center. |
179 int w = size.width(); | 189 int w = size.width(); |
180 int i0w = images_[0].width(); | 190 int i0w = images_[0].width(); |
181 int i2w = images_[2].width(); | 191 int i2w = images_[2].width(); |
182 int i3w = images_[3].width(); | 192 int i3w = images_[3].width(); |
183 int i5w = images_[5].width(); | 193 int i5w = images_[5].width(); |
184 int i6w = images_[6].width(); | 194 int i6w = images_[6].width(); |
185 int i8w = images_[8].width(); | 195 int i8w = images_[8].width(); |
186 int i4x = std::min(std::min(i0w, i3w), i6w); | 196 int i4x = std::min(std::min(i0w, i3w), i6w); |
187 int i4w = w - i4x - std::min(std::min(i2w, i5w), i8w); | 197 int i4w = w - i4x - std::min(std::min(i2w, i5w), i8w); |
188 int h = size.height(); | 198 int h = size.height(); |
189 int i0h = images_[0].height(); | 199 int i0h = images_[0].height(); |
190 int i1h = images_[1].height(); | 200 int i1h = images_[1].height(); |
191 int i2h = images_[2].height(); | 201 int i2h = images_[2].height(); |
192 int i6h = images_[6].height(); | 202 int i6h = images_[6].height(); |
193 int i7h = images_[7].height(); | 203 int i7h = images_[7].height(); |
194 int i8h = images_[8].height(); | 204 int i8h = images_[8].height(); |
195 int i4y = std::min(std::min(i0h, i1h), i2h); | 205 int i4y = std::min(std::min(i0h, i1h), i2h); |
196 int i4h = h - i4y - std::min(std::min(i6h, i7h), i8h); | 206 int i4h = h - i4y - std::min(std::min(i6h, i7h), i8h); |
197 if (!images_[4].isNull()) | 207 if (!images_[4].isNull()) |
198 Fill(canvas, images_[4], i4x, i4y, i4w, i4h); | 208 Fill(canvas, images_[4], i4x, i4y, i4w, i4h, paint); |
199 canvas->DrawImageInt(images_[0], 0, 0); | 209 canvas->DrawImageInt(images_[0], 0, 0, paint); |
200 Fill(canvas, images_[1], i0w, 0, w - i0w - i2w, i1h); | 210 Fill(canvas, images_[1], i0w, 0, w - i0w - i2w, i1h, paint); |
201 canvas->DrawImageInt(images_[2], w - i2w, 0); | 211 canvas->DrawImageInt(images_[2], w - i2w, 0, paint); |
202 Fill(canvas, images_[3], 0, i0h, i3w, h - i0h - i6h); | 212 Fill(canvas, images_[3], 0, i0h, i3w, h - i0h - i6h, paint); |
203 Fill(canvas, images_[5], w - i5w, i2h, i5w, h - i2h - i8h); | 213 Fill(canvas, images_[5], w - i5w, i2h, i5w, h - i2h - i8h, paint); |
204 canvas->DrawImageInt(images_[6], 0, h - i6h); | 214 canvas->DrawImageInt(images_[6], 0, h - i6h, paint); |
205 Fill(canvas, images_[7], i6w, h - i7h, w - i6w - i8w, i7h); | 215 Fill(canvas, images_[7], i6w, h - i7h, w - i6w - i8w, i7h, paint); |
206 canvas->DrawImageInt(images_[8], w - i8w, h - i8h); | 216 canvas->DrawImageInt(images_[8], w - i8w, h - i8h, paint); |
207 } | 217 } |
208 | 218 |
209 // static | 219 // static |
210 void ImagePainter::Fill(gfx::Canvas* c, | 220 void ImagePainter::Fill(gfx::Canvas* c, |
211 const gfx::ImageSkia& i, | 221 const gfx::ImageSkia& i, |
212 int x, | 222 int x, |
213 int y, | 223 int y, |
214 int w, | 224 int w, |
215 int h) { | 225 int h, |
216 c->DrawImageInt(i, 0, 0, i.width(), i.height(), x, y, w, h, false); | 226 const SkPaint& paint) { |
| 227 c->DrawImageInt(i, 0, 0, i.width(), i.height(), x, y, w, h, false, paint); |
217 } | 228 } |
218 | 229 |
219 } // namespace | 230 } // namespace |
220 | 231 |
221 | 232 |
222 // Painter -------------------------------------------------------------------- | 233 // Painter -------------------------------------------------------------------- |
223 | 234 |
224 Painter::Painter() { | 235 Painter::Painter() { |
225 } | 236 } |
226 | 237 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 Painter* Painter::CreateImagePainter(const gfx::ImageSkia& image, | 278 Painter* Painter::CreateImagePainter(const gfx::ImageSkia& image, |
268 const gfx::Insets& insets) { | 279 const gfx::Insets& insets) { |
269 return new ImagePainter(image, insets); | 280 return new ImagePainter(image, insets); |
270 } | 281 } |
271 | 282 |
272 // static | 283 // static |
273 Painter* Painter::CreateImageGridPainter(const int image_ids[]) { | 284 Painter* Painter::CreateImageGridPainter(const int image_ids[]) { |
274 return new ImagePainter(image_ids); | 285 return new ImagePainter(image_ids); |
275 } | 286 } |
276 | 287 |
| 288 void Painter::PaintWithSkPaint(gfx::Canvas* canvas, |
| 289 const gfx::Size& size, |
| 290 const SkPaint& paint) { |
| 291 // Ignore SkPaint options by default, applicable painters should add support. |
| 292 Paint(canvas, size); |
| 293 } |
| 294 |
277 | 295 |
278 // HorizontalPainter ---------------------------------------------------------- | 296 // HorizontalPainter ---------------------------------------------------------- |
279 | 297 |
280 HorizontalPainter::HorizontalPainter(const int image_resource_names[]) { | 298 HorizontalPainter::HorizontalPainter(const int image_resource_names[]) { |
281 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 299 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
282 for (int i = 0; i < 3; ++i) | 300 for (int i = 0; i < 3; ++i) |
283 images_[i] = rb.GetImageNamed(image_resource_names[i]).ToImageSkia(); | 301 images_[i] = rb.GetImageNamed(image_resource_names[i]).ToImageSkia(); |
284 DCHECK_EQ(images_[LEFT]->height(), images_[CENTER]->height()); | 302 DCHECK_EQ(images_[LEFT]->height(), images_[CENTER]->height()); |
285 DCHECK_EQ(images_[LEFT]->height(), images_[RIGHT]->height()); | 303 DCHECK_EQ(images_[LEFT]->height(), images_[RIGHT]->height()); |
286 } | 304 } |
(...skipping 14 matching lines...) Expand all Loading... |
301 canvas->DrawImageInt(*images_[LEFT], 0, 0); | 319 canvas->DrawImageInt(*images_[LEFT], 0, 0); |
302 canvas->DrawImageInt(*images_[RIGHT], size.width() - images_[RIGHT]->width(), | 320 canvas->DrawImageInt(*images_[RIGHT], size.width() - images_[RIGHT]->width(), |
303 0); | 321 0); |
304 canvas->TileImageInt( | 322 canvas->TileImageInt( |
305 *images_[CENTER], images_[LEFT]->width(), 0, | 323 *images_[CENTER], images_[LEFT]->width(), 0, |
306 size.width() - images_[LEFT]->width() - images_[RIGHT]->width(), | 324 size.width() - images_[LEFT]->width() - images_[RIGHT]->width(), |
307 images_[LEFT]->height()); | 325 images_[LEFT]->height()); |
308 } | 326 } |
309 | 327 |
310 } // namespace views | 328 } // namespace views |
OLD | NEW |