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