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" |
11 #include "ui/gfx/canvas.h" | 11 #include "ui/gfx/canvas.h" |
12 #include "ui/gfx/image/image.h" | 12 #include "ui/gfx/image/image.h" |
13 #include "ui/gfx/image/image_skia.h" | 13 #include "ui/gfx/image/image_skia.h" |
14 #include "ui/gfx/image/image_skia_operations.h" | 14 #include "ui/gfx/image/image_skia_operations.h" |
15 #include "ui/gfx/insets.h" | 15 #include "ui/gfx/insets.h" |
| 16 #include "ui/gfx/nine_image_painter.h" |
16 #include "ui/gfx/point.h" | 17 #include "ui/gfx/point.h" |
17 #include "ui/gfx/rect.h" | 18 #include "ui/gfx/rect.h" |
18 #include "ui/views/view.h" | 19 #include "ui/views/view.h" |
19 | 20 |
20 namespace views { | 21 namespace views { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // DashedFocusPainter ---------------------------------------------------------- | 25 // DashedFocusPainter ---------------------------------------------------------- |
25 | 26 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 public: | 167 public: |
167 // Constructs an ImagePainter with the specified image resource ids. | 168 // Constructs an ImagePainter with the specified image resource ids. |
168 // See CreateImageGridPainter()'s comment regarding image ID count and order. | 169 // See CreateImageGridPainter()'s comment regarding image ID count and order. |
169 explicit ImagePainter(const int image_ids[]); | 170 explicit ImagePainter(const int image_ids[]); |
170 | 171 |
171 // Constructs an ImagePainter with the specified image and insets. | 172 // Constructs an ImagePainter with the specified image and insets. |
172 ImagePainter(const gfx::ImageSkia& image, const gfx::Insets& insets); | 173 ImagePainter(const gfx::ImageSkia& image, const gfx::Insets& insets); |
173 | 174 |
174 virtual ~ImagePainter(); | 175 virtual ~ImagePainter(); |
175 | 176 |
176 // Returns true if the images are empty. | |
177 bool IsEmpty() const; | |
178 | |
179 // Painter: | 177 // Painter: |
180 virtual gfx::Size GetMinimumSize() const OVERRIDE; | 178 virtual gfx::Size GetMinimumSize() const OVERRIDE; |
181 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE; | 179 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE; |
182 | 180 |
183 private: | 181 private: |
184 // Stretches the given image over the specified canvas area. | 182 scoped_ptr<gfx::NineImagePainter> nine_painter_; |
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 | 183 |
199 DISALLOW_COPY_AND_ASSIGN(ImagePainter); | 184 DISALLOW_COPY_AND_ASSIGN(ImagePainter); |
200 }; | 185 }; |
201 | 186 |
202 ImagePainter::ImagePainter(const int image_ids[]) { | 187 ImagePainter::ImagePainter(const int image_ids[]) |
203 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 188 : nine_painter_(ui::CreateNineImagePainter(image_ids)) { |
204 for (size_t i = 0; i < 9; ++i) | |
205 if (image_ids[i] != 0) | |
206 images_[i] = *rb.GetImageSkiaNamed(image_ids[i]); | |
207 } | 189 } |
208 | 190 |
209 ImagePainter::ImagePainter(const gfx::ImageSkia& image, | 191 ImagePainter::ImagePainter(const gfx::ImageSkia& image, |
210 const gfx::Insets& insets) { | 192 const gfx::Insets& insets) |
211 DCHECK_GE(image.width(), insets.width()); | 193 : nine_painter_(new gfx::NineImagePainter(image, insets)) { |
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 } | 194 } |
227 | 195 |
228 ImagePainter::~ImagePainter() { | 196 ImagePainter::~ImagePainter() { |
229 } | 197 } |
230 | 198 |
231 bool ImagePainter::IsEmpty() const { | |
232 return images_[0].isNull(); | |
233 } | |
234 | |
235 gfx::Size ImagePainter::GetMinimumSize() const { | 199 gfx::Size ImagePainter::GetMinimumSize() const { |
236 return IsEmpty() ? gfx::Size() : gfx::Size( | 200 return nine_painter_->GetMinimumSize(); |
237 images_[0].width() + images_[1].width() + images_[2].width(), | |
238 images_[0].height() + images_[3].height() + images_[6].height()); | |
239 } | 201 } |
240 | 202 |
241 void ImagePainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) { | 203 void ImagePainter::Paint(gfx::Canvas* canvas, const gfx::Size& size) { |
242 if (IsEmpty()) | 204 nine_painter_->Paint(canvas, gfx::Rect(size)); |
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 | |
280 void 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 } | 205 } |
288 | 206 |
289 } // namespace | 207 } // namespace |
290 | 208 |
291 | 209 |
292 // Painter -------------------------------------------------------------------- | 210 // Painter -------------------------------------------------------------------- |
293 | 211 |
294 Painter::Painter() { | 212 Painter::Painter() { |
295 } | 213 } |
296 | 214 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 canvas->DrawImageInt(*images_[LEFT], 0, 0); | 314 canvas->DrawImageInt(*images_[LEFT], 0, 0); |
397 canvas->DrawImageInt(*images_[RIGHT], size.width() - images_[RIGHT]->width(), | 315 canvas->DrawImageInt(*images_[RIGHT], size.width() - images_[RIGHT]->width(), |
398 0); | 316 0); |
399 canvas->TileImageInt( | 317 canvas->TileImageInt( |
400 *images_[CENTER], images_[LEFT]->width(), 0, | 318 *images_[CENTER], images_[LEFT]->width(), 0, |
401 size.width() - images_[LEFT]->width() - images_[RIGHT]->width(), | 319 size.width() - images_[LEFT]->width() - images_[RIGHT]->width(), |
402 images_[LEFT]->height()); | 320 images_[LEFT]->height()); |
403 } | 321 } |
404 | 322 |
405 } // namespace views | 323 } // namespace views |
OLD | NEW |