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.
5 #ifndef UI_VIEWS_LAYOUT_BOX_LAYOUT_H_
6 #define UI_VIEWS_LAYOUT_BOX_LAYOUT_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "ui/gfx/geometry/insets.h"
13 #include "ui/views/layout/layout_manager.h"
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
{
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.
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
,
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 ~BoxLayout() override
;
68 void set_main_axis_alignment(MainAxisAlignment main_axis_alignment
) {
69 main_axis_alignment_
= main_axis_alignment
;
72 void set_cross_axis_alignment(CrossAxisAlignment cross_axis_alignment
) {
73 cross_axis_alignment_
= cross_axis_alignment
;
76 void set_inside_border_insets(const gfx::Insets
& insets
) {
77 inside_border_insets_
= insets
;
80 void set_minimum_cross_axis_size(int size
) {
81 minimum_cross_axis_size_
= size
;
84 // Sets the flex weight for the given |view|. Using the preferred size as
85 // the basis, free space along the main axis is distributed to views in the
86 // ratio of their flex weights. Similarly, if the views will overflow the
87 // parent, space is subtracted in these ratios.
89 // A flex of 0 means this view is not resized. Flex values must not be
91 void SetFlexForView(const View
* view
, int flex
);
93 // Clears the flex for the given |view|, causing it to use the default
95 void ClearFlexForView(const View
* view
);
97 // Sets the flex for views to use when none is specified.
98 void SetDefaultFlex(int default_flex
);
100 // Overridden from views::LayoutManager:
101 void Installed(View
* host
) override
;
102 void Uninstalled(View
* host
) override
;
103 void ViewRemoved(View
* host
, View
* view
) override
;
104 void Layout(View
* host
) override
;
105 gfx::Size
GetPreferredSize(const View
* host
) const override
;
106 int GetPreferredHeightForWidth(const View
* host
, int width
) const override
;
109 // Returns the flex for the specified |view|.
110 int GetFlexForView(const View
* view
) const;
112 // Returns the size and position along the main axis of |rect|.
113 int MainAxisSize(const gfx::Rect
& rect
) const;
114 int MainAxisPosition(const gfx::Rect
& rect
) const;
116 // Sets the size and position along the main axis of |rect|.
117 void SetMainAxisSize(int size
, gfx::Rect
* rect
) const;
118 void SetMainAxisPosition(int position
, gfx::Rect
* rect
) const;
120 // Returns the size and position along the cross axis of |rect|.
121 int CrossAxisSize(const gfx::Rect
& rect
) const;
122 int CrossAxisPosition(const gfx::Rect
& rect
) const;
124 // Sets the size and position along the cross axis of |rect|.
125 void SetCrossAxisSize(int size
, gfx::Rect
* rect
) const;
126 void SetCrossAxisPosition(int size
, gfx::Rect
* rect
) const;
128 // Returns the main axis size for the given view. |child_area_width| is needed
129 // to calculate the height of the view when the orientation is vertical.
130 int MainAxisSizeForView(const View
* view
, int child_area_width
) const;
132 // Returns the cross axis size for the given view.
133 int CrossAxisSizeForView(const View
* view
) const;
135 // The preferred size for the dialog given the width of the child area.
136 gfx::Size
GetPreferredSizeForChildWidth(const View
* host
,
137 int child_area_width
) const;
139 // The amount of space the layout requires in addition to any space for the
141 gfx::Size
NonChildSize(const View
* host
) const;
143 const Orientation orientation_
;
145 // Spacing between child views and host view border.
146 gfx::Insets inside_border_insets_
;
148 // Spacing to put in between child views.
149 const int between_child_spacing_
;
151 // The alignment of children in the main axis. This is
152 // MAIN_AXIS_ALIGNMENT_START by default.
153 MainAxisAlignment main_axis_alignment_
;
155 // The alignment of children in the cross axis. This is
156 // CROSS_AXIS_ALIGNMENT_STRETCH by default.
157 CrossAxisAlignment cross_axis_alignment_
;
159 // A map of views to their flex weights.
160 std::map
<const View
*, int> flex_map_
;
162 // The flex weight for views if none is set. Defaults to 0.
165 // The minimum cross axis size for the layout.
166 int minimum_cross_axis_size_
;
168 // The view that this BoxLayout is managing the layout for.
171 DISALLOW_IMPLICIT_CONSTRUCTORS(BoxLayout
);
176 #endif // UI_VIEWS_LAYOUT_BOX_LAYOUT_H_