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 #include "ui/views/layout/box_layout.h"
7 #include "ui/gfx/insets.h"
8 #include "ui/gfx/rect.h"
9 #include "ui/views/view.h"
13 BoxLayout::BoxLayout(BoxLayout::Orientation orientation
,
14 int inside_border_horizontal_spacing
,
15 int inside_border_vertical_spacing
,
16 int between_child_spacing
)
17 : orientation_(orientation
),
18 inside_border_horizontal_spacing_(inside_border_horizontal_spacing
),
19 inside_border_vertical_spacing_(inside_border_vertical_spacing
),
20 between_child_spacing_(between_child_spacing
),
21 spread_blank_space_(false) {
24 BoxLayout::~BoxLayout() {
27 void BoxLayout::Layout(View
* host
) {
28 gfx::Rect
child_area(host
->GetLocalBounds());
29 child_area
.Inset(host
->GetInsets());
30 child_area
.Inset(inside_border_horizontal_spacing_
,
31 inside_border_vertical_spacing_
);
32 int x
= child_area
.x();
33 int y
= child_area
.y();
36 if (spread_blank_space_
) {
39 for (int i
= 0; i
< host
->child_count(); ++i
) {
40 View
* child
= host
->child_at(i
);
41 if (!child
->visible())
43 if (orientation_
== kHorizontal
)
44 total
+= child
->GetPreferredSize().width() + between_child_spacing_
;
46 total
+= child
->GetPreferredSize().height() + between_child_spacing_
;
51 total
-= between_child_spacing_
;
52 if (orientation_
== kHorizontal
)
53 padding
= (child_area
.width() - total
) / visible
;
55 padding
= (child_area
.height() - total
) / visible
;
62 for (int i
= 0; i
< host
->child_count(); ++i
) {
63 View
* child
= host
->child_at(i
);
64 if (child
->visible()) {
65 gfx::Rect
bounds(x
, y
, child_area
.width(), child_area
.height());
66 gfx::Size
size(child
->GetPreferredSize());
67 if (orientation_
== kHorizontal
) {
68 bounds
.set_width(size
.width() + padding
);
69 x
+= size
.width() + between_child_spacing_
+ padding
;
71 bounds
.set_height(size
.height() + padding
);
72 y
+= size
.height() + between_child_spacing_
+ padding
;
74 // Clamp child view bounds to |child_area|.
75 bounds
.Intersect(child_area
);
76 child
->SetBoundsRect(bounds
);
81 gfx::Size
BoxLayout::GetPreferredSize(View
* host
) {
84 for (int i
= 0; i
< host
->child_count(); ++i
) {
85 View
* child
= host
->child_at(i
);
86 if (child
->visible()) {
87 gfx::Size
size(child
->GetPreferredSize());
88 if (orientation_
== kHorizontal
) {
89 gfx::Rect
child_bounds(position
, 0, size
.width(), size
.height());
90 bounds
.Union(child_bounds
);
91 position
+= size
.width();
93 gfx::Rect
child_bounds(0, position
, size
.width(), size
.height());
94 bounds
.Union(child_bounds
);
95 position
+= size
.height();
97 position
+= between_child_spacing_
;
100 gfx::Insets
insets(host
->GetInsets());
102 bounds
.width() + insets
.width() + 2 * inside_border_horizontal_spacing_
,
103 bounds
.height() + insets
.height() + 2 * inside_border_vertical_spacing_
);