Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / wm / core / image_grid.h
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_WM_CORE_IMAGE_GRID_H_
6 #define UI_WM_CORE_IMAGE_GRID_H_
7
8 #include "base/basictypes.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "ui/compositor/layer.h"
12 #include "ui/compositor/layer_delegate.h"
13 #include "ui/gfx/rect.h"
14 #include "ui/gfx/size.h"
15 #include "ui/wm/wm_export.h"
16
17 namespace gfx {
18 class Image;
19 }  // namespace gfx
20
21 namespace wm {
22
23 // An ImageGrid is a 3x3 array of ui::Layers, each containing an image.
24 //
25 // As the grid is resized, its images fill the requested space:
26 // - corner images are not scaled
27 // - top and bottom images are scaled horizontally
28 // - left and right images are scaled vertically
29 // - the center image is scaled in both directions
30 //
31 // If one of the non-center images is smaller than the largest images in its
32 // row or column, it will be aligned with the outside of the grid.  For
33 // example, given 4x4 top-left and top-right images and a 1x2 top images:
34 //
35 //   +--------+---------------------+--------+
36 //   |        |         top         |        |
37 //   | top-   +---------------------+  top-  +
38 //   | left   |                     | right  |
39 //   +----+---+                     +---+----+
40 //   |    |                             |    |
41 //   ...
42 //
43 // This may seem odd at first, but it lets ImageGrid be used to draw shadows
44 // with curved corners that extend inwards beyond a window's borders.  In the
45 // below example, the top-left corner image is overlaid on top of the window's
46 // top-left corner:
47 //
48 //   +---------+-----------------------
49 //   |    ..xxx|XXXXXXXXXXXXXXXXXX
50 //   |  .xXXXXX|XXXXXXXXXXXXXXXXXX_____
51 //   | .xXX    |                    ^ window's top edge
52 //   | .xXX    |
53 //   +---------+
54 //   | xXX|
55 //   | xXX|< window's left edge
56 //   | xXX|
57 //   ...
58 //
59 class WM_EXPORT ImageGrid {
60  public:
61   // Helper class for use by tests.
62   class WM_EXPORT TestAPI {
63    public:
64     TestAPI(ImageGrid* grid) : grid_(grid) {}
65
66     gfx::Rect top_left_clip_rect() const {
67       return grid_->top_left_painter_->clip_rect_;
68     }
69     gfx::Rect top_right_clip_rect() const {
70       return grid_->top_right_painter_->clip_rect_;
71     }
72     gfx::Rect bottom_left_clip_rect() const {
73       return grid_->bottom_left_painter_->clip_rect_;
74     }
75     gfx::Rect bottom_right_clip_rect() const {
76       return grid_->bottom_right_painter_->clip_rect_;
77     }
78
79     // Returns |layer|'s bounds after applying the layer's current transform.
80     gfx::RectF GetTransformedLayerBounds(const ui::Layer& layer);
81
82    private:
83     ImageGrid* grid_;  // not owned
84
85     DISALLOW_COPY_AND_ASSIGN(TestAPI);
86   };
87
88   ImageGrid();
89   ~ImageGrid();
90
91   ui::Layer* layer() { return layer_.get(); }
92   int top_image_height() const { return top_image_height_; }
93   int bottom_image_height() const { return bottom_image_height_; }
94   int left_image_width() const { return left_image_width_; }
95   int right_image_width() const { return right_image_width_; }
96
97   // Visible to allow independent layer animations and for testing.
98   ui::Layer* top_left_layer() const { return top_left_layer_.get(); }
99   ui::Layer* top_layer() const { return top_layer_.get(); }
100   ui::Layer* top_right_layer() const { return top_right_layer_.get(); }
101   ui::Layer* left_layer() const { return left_layer_.get(); }
102   ui::Layer* center_layer() const { return center_layer_.get(); }
103   ui::Layer* right_layer() const { return right_layer_.get(); }
104   ui::Layer* bottom_left_layer() const { return bottom_left_layer_.get(); }
105   ui::Layer* bottom_layer() const { return bottom_layer_.get(); }
106   ui::Layer* bottom_right_layer() const { return bottom_right_layer_.get(); }
107
108   // Sets the grid to display the passed-in images (any of which can be NULL).
109   // Ownership of the images remains with the caller.  May be called more than
110   // once to switch images.
111   void SetImages(const gfx::Image* top_left_image,
112                  const gfx::Image* top_image,
113                  const gfx::Image* top_right_image,
114                  const gfx::Image* left_image,
115                  const gfx::Image* center_image,
116                  const gfx::Image* right_image,
117                  const gfx::Image* bottom_left_image,
118                  const gfx::Image* bottom_image,
119                  const gfx::Image* bottom_right_image);
120
121   void SetSize(const gfx::Size& size);
122
123   // Sets the grid to a position and size such that the inner edges of the top,
124   // bottom, left and right images will be flush with |content_bounds_in_dip|.
125   void SetContentBounds(const gfx::Rect& content_bounds_in_dip);
126
127  private:
128   // Delegate responsible for painting a specific image on a layer.
129   class ImagePainter : public ui::LayerDelegate {
130    public:
131     ImagePainter(const gfx::Image* image) : image_(image) {}
132     virtual ~ImagePainter() {}
133
134     // Clips |layer| to |clip_rect|.  Triggers a repaint if the clipping
135     // rectangle has changed.  An empty rectangle disables clipping.
136     void SetClipRect(const gfx::Rect& clip_rect, ui::Layer* layer);
137
138     // ui::LayerDelegate implementation:
139     virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE;
140     virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE;
141     virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE;
142
143    private:
144     friend class TestAPI;
145
146     const gfx::Image* image_;  // not owned
147
148     gfx::Rect clip_rect_;
149
150     DISALLOW_COPY_AND_ASSIGN(ImagePainter);
151   };
152
153   // Returns the dimensions of |image| if non-NULL or gfx::Size(0, 0) otherwise.
154   static gfx::Size GetImageSize(const gfx::Image* image);
155
156   // Returns true if |layer|'s bounds don't fit within |size|.
157   static bool LayerExceedsSize(const ui::Layer* layer, const gfx::Size& size);
158
159   // Sets |layer_ptr| and |painter_ptr| to display |image| and adds the
160   // passed-in layer to |layer_|.  If image is NULL resets |layer_ptr| and
161   // |painter_ptr| and removes any existing layer from |layer_|.
162   void SetImage(const gfx::Image* image,
163                 scoped_ptr<ui::Layer>* layer_ptr,
164                 scoped_ptr<ImagePainter>* painter_ptr);
165
166   // Sets the scaling for the transform applied to a layer.  The left, top,
167   // right and bottom layers are stretched to the height or width of the
168   // center image.
169   void ScaleWidth(gfx::Size center,
170                   ui::Layer* layer,
171                   gfx::Transform& transform);
172   void ScaleHeight(gfx::Size center,
173                    ui::Layer* layer,
174                    gfx::Transform& transform);
175
176   // Layer that contains all of the image layers.
177   scoped_ptr<ui::Layer> layer_;
178
179   // The grid's dimensions.
180   gfx::Size size_;
181
182   // Heights and widths of the images displayed by |top_layer_|,
183   // |bottom_layer_|, |left_layer_|, and |right_layer_|.
184   int top_image_height_;
185   int bottom_image_height_;
186   int left_image_width_;
187   int right_image_width_;
188
189   // Heights of the tallest images in the top and bottom rows and the widest
190   // images in the left and right columns.  Note that we may have less actual
191   // space than this available if the images are large and |size_| is small.
192   int base_top_row_height_;
193   int base_bottom_row_height_;
194   int base_left_column_width_;
195   int base_right_column_width_;
196
197   // Layers used to display the various images.  Children of |layer_|.
198   // Positions for which no images were supplied are NULL.
199   scoped_ptr<ui::Layer> top_left_layer_;
200   scoped_ptr<ui::Layer> top_layer_;
201   scoped_ptr<ui::Layer> top_right_layer_;
202   scoped_ptr<ui::Layer> left_layer_;
203   scoped_ptr<ui::Layer> center_layer_;
204   scoped_ptr<ui::Layer> right_layer_;
205   scoped_ptr<ui::Layer> bottom_left_layer_;
206   scoped_ptr<ui::Layer> bottom_layer_;
207   scoped_ptr<ui::Layer> bottom_right_layer_;
208
209   // Delegates responsible for painting the above layers.
210   // Positions for which no images were supplied are NULL.
211   scoped_ptr<ImagePainter> top_left_painter_;
212   scoped_ptr<ImagePainter> top_painter_;
213   scoped_ptr<ImagePainter> top_right_painter_;
214   scoped_ptr<ImagePainter> left_painter_;
215   scoped_ptr<ImagePainter> center_painter_;
216   scoped_ptr<ImagePainter> right_painter_;
217   scoped_ptr<ImagePainter> bottom_left_painter_;
218   scoped_ptr<ImagePainter> bottom_painter_;
219   scoped_ptr<ImagePainter> bottom_right_painter_;
220
221   DISALLOW_COPY_AND_ASSIGN(ImageGrid);
222 };
223
224 }  // namespace wm
225
226 #endif  // UI_WM_CORE_IMAGE_GRID_H_