painter.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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#ifndef UI_VIEWS_PAINTER_H_ 6#define UI_VIEWS_PAINTER_H_ 7 8#include "base/basictypes.h" 9#include "base/compiler_specific.h" 10#include "third_party/skia/include/core/SkColor.h" 11#include "ui/views/views_export.h" 12 13namespace gfx { 14class Canvas; 15class ImageSkia; 16class Insets; 17class Rect; 18class Size; 19} 20 21// A macro to define arrays of IDR constants used with CreateImageGridPainter. 22#define IMAGE_GRID(x) { x ## _TOP_LEFT, x ## _TOP, x ## _TOP_RIGHT, \ 23 x ## _LEFT, x ## _CENTER, x ## _RIGHT, \ 24 x ## _BOTTOM_LEFT, x ## _BOTTOM, x ## _BOTTOM_RIGHT, } 25 26namespace views { 27 28// Painter, as the name implies, is responsible for painting in a particular 29// region. Think of Painter as a Border or Background that can be painted 30// in any region of a View. 31class VIEWS_EXPORT Painter { 32 public: 33 // A convenience method for painting a Painter in a particular region. 34 // This translates the canvas to x/y and paints the painter. 35 static void PaintPainterAt(gfx::Canvas* canvas, 36 Painter* painter, 37 const gfx::Rect& rect); 38 39 // Creates a painter that draws a gradient between the two colors. 40 static Painter* CreateHorizontalGradient(SkColor c1, SkColor c2); 41 static Painter* CreateVerticalGradient(SkColor c1, SkColor c2); 42 43 // Creates a painter that draws a multi-color gradient. |colors| contains the 44 // gradient colors and |pos| the relative positions of the colors. The first 45 // element in |pos| must be 0.0 and the last element 1.0. |count| contains 46 // the number of elements in |colors| and |pos|. 47 static Painter* CreateVerticalMultiColorGradient(SkColor* colors, 48 SkScalar* pos, 49 size_t count); 50 51 // Creates a painter that divides |image| into nine regions. The four corners 52 // are rendered at the size specified in insets (eg. the upper-left corner is 53 // rendered at 0 x 0 with a size of insets.left() x insets.top()). The center 54 // and edge images are stretched to fill the painted area. 55 static Painter* CreateImagePainter(const gfx::ImageSkia& image, 56 const gfx::Insets& insets); 57 58 // Creates a painter that paints images in a scalable grid. The images must 59 // share widths by column and heights by row. The corners are painted at full 60 // size, while center and edge images are stretched to fill the painted area. 61 // The center image may be zero (to be skipped). This ordering must be used: 62 // Top-Left/Top/Top-Right/Left/[Center]/Right/Bottom-Left/Bottom/Bottom-Right. 63 static Painter* CreateImageGridPainter(const int image_ids[]); 64 65 virtual ~Painter() {} 66 67 // Paints the painter in the specified region. 68 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) = 0; 69}; 70 71// HorizontalPainter paints 3 images into a box: left, center and right. The 72// left and right images are drawn to size at the left/right edges of the 73// region. The center is tiled in the remaining space. All images must have the 74// same height. 75class VIEWS_EXPORT HorizontalPainter : public Painter { 76 public: 77 // Constructs a new HorizontalPainter loading the specified image names. 78 // The images must be in the order left, right and center. 79 explicit HorizontalPainter(const int image_resource_names[]); 80 81 virtual ~HorizontalPainter() {} 82 83 // Paints the images. 84 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE; 85 86 // Height of the images. 87 int height() const { return height_; } 88 89 private: 90 // The image chunks. 91 enum BorderElements { 92 LEFT, 93 CENTER, 94 RIGHT 95 }; 96 97 // The height. 98 int height_; 99 // NOTE: the images are owned by ResourceBundle. Don't free them. 100 const gfx::ImageSkia* images_[3]; 101 102 DISALLOW_COPY_AND_ASSIGN(HorizontalPainter); 103}; 104 105} // namespace views 106 107#endif // UI_VIEWS_PAINTER_H_ 108