painter.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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.
4
5#include "ui/views/painter.h"
6
7#include "base/logging.h"
8#include "base/memory/scoped_ptr.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/image/image.h"
13#include "ui/gfx/image/image_skia.h"
14#include "ui/gfx/image/image_skia_operations.h"
15#include "ui/gfx/insets.h"
16#include "ui/gfx/point.h"
17#include "ui/gfx/rect.h"
18#include "ui/views/view.h"
19
20namespace views {
21
22namespace {
23
24// DashedFocusPainter ----------------------------------------------------------
25
26class DashedFocusPainter : public Painter {
27 public:
28  explicit DashedFocusPainter(const gfx::Insets& insets);
29  virtual ~DashedFocusPainter();
30
31  // Painter:
32  virtual gfx::Size GetMinimumSize() const OVERRIDE;
33  virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE;
34
35 private:
36  const gfx::Insets insets_;
37
38  DISALLOW_COPY_AND_ASSIGN(DashedFocusPainter);
39};
40
41DashedFocusPainter::DashedFocusPainter(const gfx::Insets& insets)
42    : insets_(insets) {
43}
44
45DashedFocusPainter::~DashedFocusPainter() {
46}
47
48gfx::Size DashedFocusPainter::GetMinimumSize() const {
49  return gfx::Size();
50}
51
52void DashedFocusPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) {
53  gfx::Rect rect(size);
54  rect.Inset(insets_);
55  canvas->DrawFocusRect(rect);
56}
57
58// SolidFocusPainter -----------------------------------------------------------
59
60class SolidFocusPainter : public Painter {
61 public:
62  SolidFocusPainter(SkColor color, const gfx::Insets& insets);
63  virtual ~SolidFocusPainter();
64
65  // Painter:
66  virtual gfx::Size GetMinimumSize() const OVERRIDE;
67  virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE;
68
69 private:
70  const SkColor color_;
71  const gfx::Insets insets_;
72
73  DISALLOW_COPY_AND_ASSIGN(SolidFocusPainter);
74};
75
76SolidFocusPainter::SolidFocusPainter(SkColor color,
77                                     const gfx::Insets& insets)
78    : color_(color),
79      insets_(insets) {
80}
81
82SolidFocusPainter::~SolidFocusPainter() {
83}
84
85gfx::Size SolidFocusPainter::GetMinimumSize() const {
86  return gfx::Size();
87}
88
89void SolidFocusPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) {
90  gfx::Rect rect(size);
91  rect.Inset(insets_);
92  canvas->DrawSolidFocusRect(rect, color_);
93}
94
95// GradientPainter ------------------------------------------------------------
96
97class GradientPainter : public Painter {
98 public:
99  GradientPainter(bool horizontal,
100                  SkColor* colors,
101                  SkScalar* pos,
102                  size_t count);
103  virtual ~GradientPainter();
104
105  // Painter:
106  virtual gfx::Size GetMinimumSize() const OVERRIDE;
107  virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE;
108
109 private:
110  // If |horizontal_| is true then the gradient is painted horizontally.
111  bool horizontal_;
112  // The gradient colors.
113  scoped_ptr<SkColor[]> colors_;
114  // The relative positions of the corresponding gradient colors.
115  scoped_ptr<SkScalar[]> pos_;
116  // The number of elements in |colors_| and |pos_|.
117  size_t count_;
118
119  DISALLOW_COPY_AND_ASSIGN(GradientPainter);
120};
121
122GradientPainter::GradientPainter(bool horizontal,
123                                 SkColor* colors,
124                                 SkScalar* pos,
125                                 size_t count)
126    : horizontal_(horizontal),
127      colors_(new SkColor[count]),
128      pos_(new SkScalar[count]),
129      count_(count) {
130  for (size_t i = 0; i < count_; ++i) {
131    pos_[i] = pos[i];
132    colors_[i] = colors[i];
133  }
134}
135
136GradientPainter::~GradientPainter() {
137}
138
139gfx::Size GradientPainter::GetMinimumSize() const {
140  return gfx::Size();
141}
142
143void GradientPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) {
144  SkPaint paint;
145  SkPoint p[2];
146  p[0].iset(0, 0);
147  if (horizontal_)
148    p[1].iset(size.width(), 0);
149  else
150    p[1].iset(0, size.height());
151
152  skia::RefPtr<SkShader> s = skia::AdoptRef(SkGradientShader::CreateLinear(
153      p, colors_.get(), pos_.get(), count_, SkShader::kClamp_TileMode, NULL));
154  paint.setStyle(SkPaint::kFill_Style);
155  paint.setShader(s.get());
156
157  canvas->sk_canvas()->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0),
158                                      SkIntToScalar(size.width()),
159                                      SkIntToScalar(size.height()), paint);
160}
161
162// ImagePainter ---------------------------------------------------------------
163
164// ImagePainter stores and paints nine images as a scalable grid.
165class VIEWS_EXPORT ImagePainter : public Painter {
166 public:
167  // Constructs an ImagePainter with the specified image resource ids.
168  // See CreateImageGridPainter()'s comment regarding image ID count and order.
169  explicit ImagePainter(const int image_ids[]);
170
171  // Constructs an ImagePainter with the specified image and insets.
172  ImagePainter(const gfx::ImageSkia& image, const gfx::Insets& insets);
173
174  virtual ~ImagePainter();
175
176  // Returns true if the images are empty.
177  bool IsEmpty() const;
178
179  // Painter:
180  virtual gfx::Size GetMinimumSize() const OVERRIDE;
181  virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE;
182
183 private:
184  // Stretches the given image over the specified canvas area.
185  static void Fill(gfx::Canvas* c,
186                   const gfx::ImageSkia& i,
187                   int x,
188                   int y,
189                   int w,
190                   int h);
191
192  // Images are numbered as depicted below.
193  //  ____________________
194  // |__i0__|__i1__|__i2__|
195  // |__i3__|__i4__|__i5__|
196  // |__i6__|__i7__|__i8__|
197  gfx::ImageSkia images_[9];
198
199  DISALLOW_COPY_AND_ASSIGN(ImagePainter);
200};
201
202ImagePainter::ImagePainter(const int image_ids[]) {
203  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
204  for (size_t i = 0; i < 9; ++i)
205    if (image_ids[i] != 0)
206      images_[i] = *rb.GetImageSkiaNamed(image_ids[i]);
207}
208
209ImagePainter::ImagePainter(const gfx::ImageSkia& image,
210                           const gfx::Insets& insets) {
211  DCHECK_GE(image.width(), insets.width());
212  DCHECK_GE(image.height(), insets.height());
213
214  // Extract subsets of the original image to match the |images_| format.
215  const int x[] =
216      { 0, insets.left(), image.width() - insets.right(), image.width() };
217  const int y[] =
218      { 0, insets.top(), image.height() - insets.bottom(), image.height() };
219
220  for (size_t j = 0; j < 3; ++j) {
221    for (size_t i = 0; i < 3; ++i) {
222      images_[i + j * 3] = gfx::ImageSkiaOperations::ExtractSubset(image,
223          gfx::Rect(x[i], y[j], x[i + 1] - x[i], y[j + 1] - y[j]));
224    }
225  }
226}
227
228ImagePainter::~ImagePainter() {
229}
230
231bool ImagePainter::IsEmpty() const {
232  return images_[0].isNull();
233}
234
235gfx::Size ImagePainter::GetMinimumSize() const {
236  return IsEmpty() ? gfx::Size() : gfx::Size(
237      images_[0].width() + images_[1].width() + images_[2].width(),
238      images_[0].height() + images_[3].height() + images_[6].height());
239}
240
241void ImagePainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) {
242  if (IsEmpty())
243    return;
244
245  // In case the corners and edges don't all have the same width/height, we draw
246  // the center first, and extend it out in all directions to the edges of the
247  // images with the smallest widths/heights.  This way there will be no
248  // unpainted areas, though some corners or edges might overlap the center.
249  int w = size.width();
250  int i0w = images_[0].width();
251  int i2w = images_[2].width();
252  int i3w = images_[3].width();
253  int i5w = images_[5].width();
254  int i6w = images_[6].width();
255  int i8w = images_[8].width();
256  int i4x = std::min(std::min(i0w, i3w), i6w);
257  int i4w = w - i4x - std::min(std::min(i2w, i5w), i8w);
258  int h = size.height();
259  int i0h = images_[0].height();
260  int i1h = images_[1].height();
261  int i2h = images_[2].height();
262  int i6h = images_[6].height();
263  int i7h = images_[7].height();
264  int i8h = images_[8].height();
265  int i4y = std::min(std::min(i0h, i1h), i2h);
266  int i4h = h - i4y - std::min(std::min(i6h, i7h), i8h);
267  if (!images_[4].isNull())
268    Fill(canvas, images_[4], i4x, i4y, i4w, i4h);
269  canvas->DrawImageInt(images_[0], 0, 0);
270  Fill(canvas, images_[1], i0w, 0, w - i0w - i2w, i1h);
271  canvas->DrawImageInt(images_[2], w - i2w, 0);
272  Fill(canvas, images_[3], 0, i0h, i3w, h - i0h - i6h);
273  Fill(canvas, images_[5], w - i5w, i2h, i5w, h - i2h - i8h);
274  canvas->DrawImageInt(images_[6], 0, h - i6h);
275  Fill(canvas, images_[7], i6w, h - i7h, w - i6w - i8w, i7h);
276  canvas->DrawImageInt(images_[8], w - i8w, h - i8h);
277}
278
279// static
280void ImagePainter::Fill(gfx::Canvas* c,
281                        const gfx::ImageSkia& i,
282                        int x,
283                        int y,
284                        int w,
285                        int h) {
286  c->DrawImageInt(i, 0, 0, i.width(), i.height(), x, y, w, h, false);
287}
288
289}  // namespace
290
291
292// Painter --------------------------------------------------------------------
293
294Painter::Painter() {
295}
296
297Painter::~Painter() {
298}
299
300// static
301void Painter::PaintPainterAt(gfx::Canvas* canvas,
302                             Painter* painter,
303                             const gfx::Rect& rect) {
304  DCHECK(canvas && painter);
305  canvas->Save();
306  canvas->Translate(rect.OffsetFromOrigin());
307  painter->Paint(canvas, rect.size());
308  canvas->Restore();
309}
310
311// static
312void Painter::PaintFocusPainter(View* view,
313                                gfx::Canvas* canvas,
314                                Painter* focus_painter) {
315  if (focus_painter && view->HasFocus())
316    PaintPainterAt(canvas, focus_painter, view->GetLocalBounds());
317}
318
319// static
320Painter* Painter::CreateHorizontalGradient(SkColor c1, SkColor c2) {
321  SkColor colors[2];
322  colors[0] = c1;
323  colors[1] = c2;
324  SkScalar pos[] = {0, 1};
325  return new GradientPainter(true, colors, pos, 2);
326}
327
328// static
329Painter* Painter::CreateVerticalGradient(SkColor c1, SkColor c2) {
330  SkColor colors[2];
331  colors[0] = c1;
332  colors[1] = c2;
333  SkScalar pos[] = {0, 1};
334  return new GradientPainter(false, colors, pos, 2);
335}
336
337// static
338Painter* Painter::CreateVerticalMultiColorGradient(SkColor* colors,
339                                                   SkScalar* pos,
340                                                   size_t count) {
341  return new GradientPainter(false, colors, pos, count);
342}
343
344// static
345Painter* Painter::CreateImagePainter(const gfx::ImageSkia& image,
346                                     const gfx::Insets& insets) {
347  return new ImagePainter(image, insets);
348}
349
350// static
351Painter* Painter::CreateImageGridPainter(const int image_ids[]) {
352  return new ImagePainter(image_ids);
353}
354
355// static
356scoped_ptr<Painter> Painter::CreateDashedFocusPainter() {
357  return scoped_ptr<Painter>(new DashedFocusPainter(gfx::Insets())).Pass();
358}
359
360// static
361scoped_ptr<Painter> Painter::CreateDashedFocusPainterWithInsets(
362    const gfx::Insets& insets) {
363  return scoped_ptr<Painter>(new DashedFocusPainter(insets)).Pass();
364}
365
366// static
367scoped_ptr<Painter> Painter::CreateSolidFocusPainter(
368    SkColor color,
369    const gfx::Insets& insets) {
370  return scoped_ptr<Painter>(new SolidFocusPainter(color, insets)).Pass();
371}
372
373// HorizontalPainter ----------------------------------------------------------
374
375HorizontalPainter::HorizontalPainter(const int image_resource_names[]) {
376  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
377  for (int i = 0; i < 3; ++i)
378    images_[i] = rb.GetImageNamed(image_resource_names[i]).ToImageSkia();
379  DCHECK_EQ(images_[LEFT]->height(), images_[CENTER]->height());
380  DCHECK_EQ(images_[LEFT]->height(), images_[RIGHT]->height());
381}
382
383HorizontalPainter::~HorizontalPainter() {
384}
385
386gfx::Size HorizontalPainter::GetMinimumSize() const {
387  return gfx::Size(
388      images_[LEFT]->width() + images_[CENTER]->width() +
389          images_[RIGHT]->width(), images_[LEFT]->height());
390}
391
392void HorizontalPainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) {
393  if (size.width() < GetMinimumSize().width())
394    return;  // No room to paint.
395
396  canvas->DrawImageInt(*images_[LEFT], 0, 0);
397  canvas->DrawImageInt(*images_[RIGHT], size.width() - images_[RIGHT]->width(),
398                       0);
399  canvas->TileImageInt(
400      *images_[CENTER], images_[LEFT]->width(), 0,
401      size.width() - images_[LEFT]->width() - images_[RIGHT]->width(),
402      images_[LEFT]->height());
403}
404
405}  // namespace views
406