summaryrefslogtreecommitdiffstats
path: root/views/painter.cc
blob: bcc65cd28f7859ce45183335df28c7b525c1cb6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "views/painter.h"

#include "app/gfx/canvas.h"
#include "app/gfx/insets.h"
#include "app/resource_bundle.h"
#include "base/logging.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/effects/SkGradientShader.h"

namespace views {

namespace {

class GradientPainter : public Painter {
 public:
  GradientPainter(bool horizontal, const SkColor& top, const SkColor& bottom)
      : horizontal_(horizontal) {
    colors_[0] = top;
    colors_[1] = bottom;
  }

  virtual ~GradientPainter() {
  }

  void Paint(int w, int h, gfx::Canvas* canvas) {
    SkPaint paint;
    SkPoint p[2];
    p[0].set(SkIntToScalar(0), SkIntToScalar(0));
    if (horizontal_)
      p[1].set(SkIntToScalar(w), SkIntToScalar(0));
    else
      p[1].set(SkIntToScalar(0), SkIntToScalar(h));

    SkShader* s =
        SkGradientShader::CreateLinear(p, colors_, NULL, 2,
                                       SkShader::kClamp_TileMode, NULL);
    paint.setStyle(SkPaint::kFill_Style);
    paint.setShader(s);
    // Need to unref shader, otherwise never deleted.
    s->unref();

    canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0),
                           SkIntToScalar(w), SkIntToScalar(h), paint);
  }

 private:
  bool horizontal_;
  SkColor colors_[2];

  DISALLOW_EVIL_CONSTRUCTORS(GradientPainter);
};


class ImagePainter : public Painter {
 public:
  ImagePainter(const SkBitmap& image,
               const gfx::Insets& insets,
               bool paint_center)
      : image_(image),
        insets_(insets),
        paint_center_(paint_center) {
    DCHECK(image.width() > insets.width() &&
           image.height() > insets.height());
  }

  // Paints the images.
  virtual void Paint(int w, int h, gfx::Canvas* canvas) {
    if (w == image_.width() && h == image_.height()) {
      // Early out if the size we're to render at equals the size of the image.
      canvas->DrawBitmapInt(image_, 0, 0);
      return;
    }
    // Upper left.
    canvas->DrawBitmapInt(image_, 0, 0, insets_.left(), insets_.top(),
                          0, 0, insets_.left(), insets_.top(), true);
    // Top edge.
    canvas->DrawBitmapInt(
        image_,
        insets_.left(), 0, image_.width() - insets_.width(), insets_.top(),
        insets_.left(), 0, w - insets_.width(), insets_.top(), true);
    // Upper right.
    canvas->DrawBitmapInt(
        image_,
        image_.width() - insets_.right(), 0, insets_.right(), insets_.top(),
        w - insets_.right(), 0, insets_.right(), insets_.top(), true);
    // Right edge.
    canvas->DrawBitmapInt(
        image_,
        image_.width() - insets_.right(), insets_.top(),
        insets_.right(), image_.height() - insets_.height(),
        w - insets_.right(), insets_.top(), insets_.right(),
        h - insets_.height(), true);
    // Bottom right.
    canvas->DrawBitmapInt(
        image_,
        image_.width() - insets_.right(), image_.height() - insets_.bottom(),
        insets_.right(), insets_.bottom(),
        w - insets_.right(), h - insets_.bottom(), insets_.right(),
        insets_.bottom(), true);
    // Bottom edge.
    canvas->DrawBitmapInt(
        image_,
        insets_.left(), image_.height() - insets_.bottom(),
        image_.width() - insets_.width(), insets_.bottom(),
        insets_.left(), h - insets_.bottom(), w - insets_.width(),
        insets_.bottom(), true);
    // Bottom left.
    canvas->DrawBitmapInt(
        image_,
        0, image_.height() - insets_.bottom(), insets_.left(),
        insets_.bottom(),
        0, h - insets_.bottom(), insets_.left(), insets_.bottom(), true);
    // Left.
    canvas->DrawBitmapInt(
        image_,
        0, insets_.top(), insets_.left(), image_.height() - insets_.height(),
        0, insets_.top(), insets_.left(), h - insets_.height(), true);
    // Center.
    if (paint_center_) {
      canvas->DrawBitmapInt(
          image_,
          insets_.top(), insets_.left(),
          image_.width() - insets_.width(), image_.height() - insets_.height(),
          insets_.left(), insets_.top(),
          w - insets_.width(), h - insets_.height(), true);
    }
  }

 private:
  const SkBitmap image_;
  const gfx::Insets insets_;
  bool paint_center_;

  DISALLOW_COPY_AND_ASSIGN(ImagePainter);
};

}  // namespace

// static
void Painter::PaintPainterAt(int x, int y, int w, int h,
                             gfx::Canvas* canvas, Painter* painter) {
  DCHECK(canvas && painter);
  if (w < 0 || h < 0)
    return;
  canvas->save();
  canvas->TranslateInt(x, y);
  painter->Paint(w, h, canvas);
  canvas->restore();
}

// static
Painter* Painter::CreateHorizontalGradient(SkColor c1, SkColor c2) {
  return new GradientPainter(true, c1, c2);
}

// static
Painter* Painter::CreateVerticalGradient(SkColor c1, SkColor c2) {
  return new GradientPainter(false, c1, c2);
}

// static
Painter* Painter::CreateImagePainter(const SkBitmap& image,
                                     const gfx::Insets& insets,
                                     bool paint_center) {
  return new ImagePainter(image, insets, paint_center);
}

HorizontalPainter::HorizontalPainter(const int image_resource_names[]) {
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  for (int i = 0; i < 3; ++i)
    images_[i] = rb.GetBitmapNamed(image_resource_names[i]);
  height_ = images_[LEFT]->height();
  DCHECK(images_[LEFT]->height() == images_[RIGHT]->height() &&
         images_[LEFT]->height() == images_[CENTER]->height());
}

void HorizontalPainter::Paint(int w, int h, gfx::Canvas* canvas) {
  if (w < (images_[LEFT]->width() + images_[CENTER]->width() +
            images_[RIGHT]->width())) {
    // No room to paint.
    return;
  }
  canvas->DrawBitmapInt(*images_[LEFT], 0, 0);
  canvas->DrawBitmapInt(*images_[RIGHT], w - images_[RIGHT]->width(), 0);
  canvas->TileImageInt(*images_[CENTER],
                       images_[LEFT]->width(),
                       0,
                       w - images_[LEFT]->width() - images_[RIGHT]->width(),
                       height_);
}

}  // namespace views