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