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 13 namespace gfx { 14 class Canvas; 15 class ImageSkia; 16 class Insets; 17 class Rect; 18 class 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 26 namespace 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. 31 class VIEWS_EXPORT Painter { 32 public: 33 Painter(); 34 virtual ~Painter(); 35 36 // A convenience method for painting a Painter in a particular region. 37 // This translates the canvas to x/y and paints the painter. 38 static void PaintPainterAt(gfx::Canvas* canvas, 39 Painter* painter, 40 const gfx::Rect& rect); 41 42 // Creates a painter that draws a gradient between the two colors. 43 static Painter* CreateHorizontalGradient(SkColor c1, SkColor c2); 44 static Painter* CreateVerticalGradient(SkColor c1, SkColor c2); 45 46 // Creates a painter that draws a multi-color gradient. |colors| contains the 47 // gradient colors and |pos| the relative positions of the colors. The first 48 // element in |pos| must be 0.0 and the last element 1.0. |count| contains 49 // the number of elements in |colors| and |pos|. 50 static Painter* CreateVerticalMultiColorGradient(SkColor* colors, 51 SkScalar* pos, 52 size_t count); 53 54 // Creates a painter that divides |image| into nine regions. The four corners 55 // are rendered at the size specified in insets (eg. the upper-left corner is 56 // rendered at 0 x 0 with a size of insets.left() x insets.top()). The center 57 // and edge images are stretched to fill the painted area. 58 static Painter* CreateImagePainter(const gfx::ImageSkia& image, 59 const gfx::Insets& insets); 60 61 // Creates a painter that paints images in a scalable grid. The images must 62 // share widths by column and heights by row. The corners are painted at full 63 // size, while center and edge images are stretched to fill the painted area. 64 // The center image may be zero (to be skipped). This ordering must be used: 65 // Top-Left/Top/Top-Right/Left/[Center]/Right/Bottom-Left/Bottom/Bottom-Right. 66 static Painter* CreateImageGridPainter(const int image_ids[]); 67 68 // Returns the minimum size this painter can paint without obvious graphical 69 // problems (e.g. overlapping images). 70 virtual gfx::Size GetMinimumSize() const = 0; 71 72 // Paints the painter in the specified region. 73 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) = 0; 74 75 private: 76 DISALLOW_COPY_AND_ASSIGN(Painter); 77 }; 78 79 // HorizontalPainter paints 3 images into a box: left, center and right. The 80 // left and right images are drawn to size at the left/right edges of the 81 // region. The center is tiled in the remaining space. All images must have the 82 // same height. 83 class VIEWS_EXPORT HorizontalPainter : public Painter { 84 public: 85 // Constructs a new HorizontalPainter loading the specified image names. 86 // The images must be in the order left, right and center. 87 explicit HorizontalPainter(const int image_resource_names[]); 88 virtual ~HorizontalPainter(); 89 90 // Painter: 91 virtual gfx::Size GetMinimumSize() const OVERRIDE; 92 virtual void Paint(gfx::Canvas* canvas, const gfx::Size& size) OVERRIDE; 93 94 private: 95 // The image chunks. 96 enum BorderElements { 97 LEFT, 98 CENTER, 99 RIGHT 100 }; 101 102 // NOTE: the images are owned by ResourceBundle. Don't free them. 103 const gfx::ImageSkia* images_[3]; 104 105 DISALLOW_COPY_AND_ASSIGN(HorizontalPainter); 106 }; 107 108 } // namespace views 109 110 #endif // UI_VIEWS_PAINTER_H_ 111