Fix cookie searchbox bug.
[chromium-blink-merge.git] / ui / views / layout / box_layout.cc
blob319efa770c3677a9a9a97fa545f9854be201be17
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"
11 namespace views {
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();
35 int padding = 0;
36 if (spread_blank_space_) {
37 int total = 0;
38 int visible = 0;
39 for (int i = 0; i < host->child_count(); ++i) {
40 View* child = host->child_at(i);
41 if (!child->visible())
42 continue;
43 if (orientation_ == kHorizontal)
44 total += child->GetPreferredSize().width() + between_child_spacing_;
45 else
46 total += child->GetPreferredSize().height() + between_child_spacing_;
47 ++visible;
50 if (visible) {
51 total -= between_child_spacing_;
52 if (orientation_ == kHorizontal)
53 padding = (child_area.width() - total) / visible;
54 else
55 padding = (child_area.height() - total) / visible;
57 if (padding < 0)
58 padding = 0;
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;
70 } else {
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) {
82 gfx::Rect bounds;
83 int position = 0;
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();
92 } else {
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());
101 return gfx::Size(
102 bounds.width() + insets.width() + 2 * inside_border_horizontal_spacing_,
103 bounds.height() + insets.height() + 2 * inside_border_vertical_spacing_);
106 } // namespace views