Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / views / layout / box_layout.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_VIEWS_LAYOUT_BOX_LAYOUT_H_
6 #define UI_VIEWS_LAYOUT_BOX_LAYOUT_H_
7
8 #include <map>
9
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "ui/gfx/insets.h"
13 #include "ui/views/layout/layout_manager.h"
14
15 namespace gfx {
16 class Rect;
17 class Size;
18 }
19
20 namespace views {
21
22 class View;
23
24 // A Layout manager that arranges child views vertically or horizontally in a
25 // side-by-side fashion with spacing around and between the child views. The
26 // child views are always sized according to their preferred size. If the
27 // host's bounds provide insufficient space, child views will be clamped.
28 // Excess space will not be distributed.
29 class VIEWS_EXPORT BoxLayout : public LayoutManager {
30  public:
31   enum Orientation {
32     kHorizontal,
33     kVertical,
34   };
35
36   // This specifies where along the main axis the children should be laid out.
37   // e.g. a horizontal layout of MAIN_AXIS_ALIGNMENT_END will result in the
38   // child views being right-aligned.
39   enum MainAxisAlignment {
40     MAIN_AXIS_ALIGNMENT_START,
41     MAIN_AXIS_ALIGNMENT_CENTER,
42     MAIN_AXIS_ALIGNMENT_END,
43     // TODO(calamity): Add MAIN_AXIS_ALIGNMENT_JUSTIFY which spreads blank space
44     // in-between the child views.
45   };
46
47   // This specifies where along the cross axis the children should be laid out.
48   // e.g. a horizontal layout of CROSS_AXIS_ALIGNMENT_END will result in the
49   // child views being bottom-aligned.
50   enum CrossAxisAlignment {
51     // This causes the child view to stretch to fit the host in the cross axis.
52     CROSS_AXIS_ALIGNMENT_STRETCH,
53     CROSS_AXIS_ALIGNMENT_START,
54     CROSS_AXIS_ALIGNMENT_CENTER,
55     CROSS_AXIS_ALIGNMENT_END,
56   };
57
58   // Use |inside_border_horizontal_spacing| and
59   // |inside_border_vertical_spacing| to add additional space between the child
60   // view area and the host view border. |between_child_spacing| controls the
61   // space in between child views.
62   BoxLayout(Orientation orientation,
63             int inside_border_horizontal_spacing,
64             int inside_border_vertical_spacing,
65             int between_child_spacing);
66   virtual ~BoxLayout();
67
68   void set_main_axis_alignment(MainAxisAlignment main_axis_alignment) {
69     main_axis_alignment_ = main_axis_alignment;
70   }
71
72   void set_cross_axis_alignment(CrossAxisAlignment cross_axis_alignment) {
73     cross_axis_alignment_ = cross_axis_alignment;
74   }
75
76   void set_inside_border_insets(const gfx::Insets& insets) {
77     inside_border_insets_ = insets;
78   }
79
80   // Sets the flex weight for the given |view|. Using the preferred size as
81   // the basis, free space along the main axis is distributed to views in the
82   // ratio of their flex weights. Similarly, if the views will overflow the
83   // parent, space is subtracted in these ratios.
84   //
85   // A flex of 0 means this view is not resized. Flex values must not be
86   // negative.
87   void SetFlexForView(const View* view, int flex);
88
89   // Clears the flex for the given |view|, causing it to use the default
90   // flex.
91   void ClearFlexForView(const View* view);
92
93   // Sets the flex for views to use when none is specified.
94   void SetDefaultFlex(int default_flex);
95
96   // Overridden from views::LayoutManager:
97   virtual void Installed(View* host) OVERRIDE;
98   virtual void Uninstalled(View* host) OVERRIDE;
99   virtual void ViewRemoved(View* host, View* view) OVERRIDE;
100   virtual void Layout(View* host) OVERRIDE;
101   virtual gfx::Size GetPreferredSize(const View* host) const OVERRIDE;
102   virtual int GetPreferredHeightForWidth(const View* host,
103                                          int width) const OVERRIDE;
104
105  private:
106   // Returns the flex for the specified |view|.
107   int GetFlexForView(const View* view) const;
108
109   // Returns the size and position along the main axis of |rect|.
110   int MainAxisSize(const gfx::Rect& rect) const;
111   int MainAxisPosition(const gfx::Rect& rect) const;
112
113   // Sets the size and position along the main axis of |rect|.
114   void SetMainAxisSize(int size, gfx::Rect* rect) const;
115   void SetMainAxisPosition(int position, gfx::Rect* rect) const;
116
117   // Returns the size and position along the cross axis of |rect|.
118   int CrossAxisSize(const gfx::Rect& rect) const;
119   int CrossAxisPosition(const gfx::Rect& rect) const;
120
121   // Sets the size and position along the cross axis of |rect|.
122   void SetCrossAxisSize(int size, gfx::Rect* rect) const;
123   void SetCrossAxisPosition(int size, gfx::Rect* rect) const;
124
125   // Returns the main axis size for the given view. |child_area_width| is needed
126   // to calculate the height of the view when the orientation is vertical.
127   int MainAxisSizeForView(const View* view, int child_area_width) const;
128
129   // Returns the cross axis size for the given view.
130   int CrossAxisSizeForView(const View* view) const;
131
132   // The preferred size for the dialog given the width of the child area.
133   gfx::Size GetPreferredSizeForChildWidth(const View* host,
134                                           int child_area_width) const;
135
136   // The amount of space the layout requires in addition to any space for the
137   // child views.
138   gfx::Size NonChildSize(const View* host) const;
139
140   const Orientation orientation_;
141
142   // Spacing between child views and host view border.
143   gfx::Insets inside_border_insets_;
144
145   // Spacing to put in between child views.
146   const int between_child_spacing_;
147
148   // The alignment of children in the main axis. This is
149   // MAIN_AXIS_ALIGNMENT_START by default.
150   MainAxisAlignment main_axis_alignment_;
151
152   // The alignment of children in the cross axis. This is
153   // CROSS_AXIS_ALIGNMENT_STRETCH by default.
154   CrossAxisAlignment cross_axis_alignment_;
155
156   // A map of views to their flex weights.
157   std::map<const View*, int> flex_map_;
158
159   // The flex weight for views if none is set. Defaults to 0.
160   int default_flex_;
161
162   // The view that this BoxLayout is managing the layout for.
163   views::View* host_;
164
165   DISALLOW_IMPLICIT_CONSTRUCTORS(BoxLayout);
166 };
167
168 } // namespace views
169
170 #endif  // UI_VIEWS_LAYOUT_BOX_LAYOUT_H_