OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #include "views/layout/box_layout.h" | |
6 | |
7 #include "ui/gfx/insets.h" | |
8 #include "ui/gfx/rect.h" | |
9 #include "views/view.h" | |
10 | |
11 namespace views { | |
12 | |
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 } | |
22 | |
23 BoxLayout::~BoxLayout() { | |
24 } | |
25 | |
26 void BoxLayout::Layout(View* host) { | |
27 gfx::Rect child_area(host->GetLocalBounds()); | |
28 child_area.Inset(host->GetInsets()); | |
29 child_area.Inset(inside_border_horizontal_spacing_, | |
30 inside_border_vertical_spacing_); | |
31 int x = child_area.x(); | |
32 int y = child_area.y(); | |
33 for (int i = 0; i < host->child_count(); ++i) { | |
34 View* child = host->child_at(i); | |
35 if (child->IsVisible()) { | |
36 gfx::Rect bounds(x, y, child_area.width(), child_area.height()); | |
37 gfx::Size size(child->GetPreferredSize()); | |
38 if (orientation_ == kHorizontal) { | |
39 bounds.set_width(size.width()); | |
40 x += size.width() + between_child_spacing_; | |
41 } else { | |
42 bounds.set_height(size.height()); | |
43 y += size.height() + between_child_spacing_; | |
44 } | |
45 // Clamp child view bounds to |child_area|. | |
46 child->SetBoundsRect(bounds.Intersect(child_area)); | |
47 } | |
48 } | |
49 } | |
50 | |
51 gfx::Size BoxLayout::GetPreferredSize(View* host) { | |
52 gfx::Rect bounds; | |
53 int position = 0; | |
54 for (int i = 0; i < host->child_count(); ++i) { | |
55 View* child = host->child_at(i); | |
56 if (child->IsVisible()) { | |
57 gfx::Size size(child->GetPreferredSize()); | |
58 if (orientation_ == kHorizontal) { | |
59 gfx::Rect child_bounds(position, 0, size.width(), size.height()); | |
60 bounds = bounds.Union(child_bounds); | |
61 position += size.width(); | |
62 } else { | |
63 gfx::Rect child_bounds(0, position, size.width(), size.height()); | |
64 bounds = bounds.Union(child_bounds); | |
65 position += size.height(); | |
66 } | |
67 position += between_child_spacing_; | |
68 } | |
69 } | |
70 gfx::Insets insets(host->GetInsets()); | |
71 return gfx::Size( | |
72 bounds.width() + insets.width() + 2 * inside_border_horizontal_spacing_, | |
73 bounds.height() + insets.height() + 2 * inside_border_vertical_spacing_); | |
74 } | |
75 | |
76 } // namespace views | |
OLD | NEW |