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_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "ui/gfx/insets.h"
11 #include "ui/views/layout/layout_manager.h"
22 // A Layout manager that arranges child views vertically or horizontally in a
23 // side-by-side fashion with spacing around and between the child views. The
24 // child views are always sized according to their preferred size. If the
25 // host's bounds provide insufficient space, child views will be clamped.
26 // Excess space will not be distributed.
27 class VIEWS_EXPORT BoxLayout
: public LayoutManager
{
34 // This specifies where along the main axis the children should be laid out.
35 // e.g. a horizontal layout of MAIN_AXIS_ALIGNMENT_END will result in the
36 // child views being right-aligned.
37 enum MainAxisAlignment
{
38 MAIN_AXIS_ALIGNMENT_START
,
39 MAIN_AXIS_ALIGNMENT_CENTER
,
40 MAIN_AXIS_ALIGNMENT_END
,
42 // This distributes extra space among the child views. This increases the
43 // size of child views along the main axis rather than the space between
45 MAIN_AXIS_ALIGNMENT_FILL
,
46 // TODO(calamity): Add MAIN_AXIS_ALIGNMENT_JUSTIFY which spreads blank space
47 // in-between the child views.
50 // This specifies where along the cross axis the children should be laid out.
51 // e.g. a horizontal layout of CROSS_AXIS_ALIGNMENT_END will result in the
52 // child views being bottom-aligned.
53 enum CrossAxisAlignment
{
54 // This causes the child view to stretch to fit the host in the cross axis.
55 CROSS_AXIS_ALIGNMENT_STRETCH
,
56 CROSS_AXIS_ALIGNMENT_START
,
57 CROSS_AXIS_ALIGNMENT_CENTER
,
58 CROSS_AXIS_ALIGNMENT_END
,
61 // Use |inside_border_horizontal_spacing| and
62 // |inside_border_vertical_spacing| to add additional space between the child
63 // view area and the host view border. |between_child_spacing| controls the
64 // space in between child views.
65 BoxLayout(Orientation orientation
,
66 int inside_border_horizontal_spacing
,
67 int inside_border_vertical_spacing
,
68 int between_child_spacing
);
71 void set_main_axis_alignment(MainAxisAlignment main_axis_alignment
) {
72 main_axis_alignment_
= main_axis_alignment
;
75 void set_cross_axis_alignment(CrossAxisAlignment cross_axis_alignment
) {
76 cross_axis_alignment_
= cross_axis_alignment
;
79 void set_inside_border_insets(const gfx::Insets
& insets
) {
80 inside_border_insets_
= insets
;
83 // Overridden from views::LayoutManager:
84 virtual void Layout(View
* host
) OVERRIDE
;
85 virtual gfx::Size
GetPreferredSize(const View
* host
) const OVERRIDE
;
86 virtual int GetPreferredHeightForWidth(const View
* host
,
87 int width
) const OVERRIDE
;
90 // Returns the size and position along the main axis of |rect|.
91 int MainAxisSize(const gfx::Rect
& rect
) const;
92 int MainAxisPosition(const gfx::Rect
& rect
) const;
94 // Sets the size and position along the main axis of |rect|.
95 void SetMainAxisSize(int size
, gfx::Rect
* rect
) const;
96 void SetMainAxisPosition(int position
, gfx::Rect
* rect
) const;
98 // Returns the size and position along the cross axis of |rect|.
99 int CrossAxisSize(const gfx::Rect
& rect
) const;
100 int CrossAxisPosition(const gfx::Rect
& rect
) const;
102 // Sets the size and position along the cross axis of |rect|.
103 void SetCrossAxisSize(int size
, gfx::Rect
* rect
) const;
104 void SetCrossAxisPosition(int size
, gfx::Rect
* rect
) const;
106 // Returns the main axis size for the given view. |child_area_width| is needed
107 // to calculate the height of the view when the orientation is vertical.
108 int MainAxisSizeForView(const View
* view
, int child_area_width
) const;
110 // Returns the cross axis size for the given view.
111 int CrossAxisSizeForView(const View
* view
) const;
113 // The preferred size for the dialog given the width of the child area.
114 gfx::Size
GetPreferredSizeForChildWidth(const View
* host
,
115 int child_area_width
) const;
117 // The amount of space the layout requires in addition to any space for the
119 gfx::Size
NonChildSize(const View
* host
) const;
121 const Orientation orientation_
;
123 // Spacing between child views and host view border.
124 gfx::Insets inside_border_insets_
;
126 // Spacing to put in between child views.
127 const int between_child_spacing_
;
129 // The alignment of children in the main axis. This is
130 // MAIN_AXIS_ALIGNMENT_START by default.
131 MainAxisAlignment main_axis_alignment_
;
133 // The alignment of children in the cross axis. This is
134 // CROSS_AXIS_ALIGNMENT_STRETCH by default.
135 CrossAxisAlignment cross_axis_alignment_
;
137 DISALLOW_IMPLICIT_CONSTRUCTORS(BoxLayout
);
142 #endif // UI_VIEWS_LAYOUT_BOX_LAYOUT_H_