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