OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/layout/box_layout.h" | 5 #include "ui/views/layout/box_layout.h" |
6 | 6 |
7 #include "ui/gfx/rect.h" | 7 #include "ui/gfx/rect.h" |
8 #include "ui/views/view.h" | 8 #include "ui/views/view.h" |
9 | 9 |
10 namespace views { | 10 namespace views { |
11 | 11 |
12 BoxLayout::BoxLayout(BoxLayout::Orientation orientation, | 12 BoxLayout::BoxLayout(BoxLayout::Orientation orientation, |
13 int inside_border_horizontal_spacing, | 13 int inside_border_horizontal_spacing, |
14 int inside_border_vertical_spacing, | 14 int inside_border_vertical_spacing, |
15 int between_child_spacing) | 15 int between_child_spacing) |
16 : orientation_(orientation), | 16 : orientation_(orientation), |
17 inside_border_insets_(inside_border_vertical_spacing, | 17 inside_border_insets_(inside_border_vertical_spacing, |
18 inside_border_horizontal_spacing, | 18 inside_border_horizontal_spacing, |
19 inside_border_vertical_spacing, | 19 inside_border_vertical_spacing, |
20 inside_border_horizontal_spacing), | 20 inside_border_horizontal_spacing), |
21 between_child_spacing_(between_child_spacing), | 21 between_child_spacing_(between_child_spacing), |
22 spread_blank_space_(false) { | 22 main_axis_alignment_(MAIN_AXIS_ALIGNMENT_START) { |
23 } | 23 } |
24 | 24 |
25 BoxLayout::~BoxLayout() { | 25 BoxLayout::~BoxLayout() { |
26 } | 26 } |
27 | 27 |
28 void BoxLayout::Layout(View* host) { | 28 void BoxLayout::Layout(View* host) { |
29 gfx::Rect child_area(host->GetLocalBounds()); | 29 gfx::Rect child_area(host->GetLocalBounds()); |
30 child_area.Inset(host->GetInsets()); | 30 child_area.Inset(host->GetInsets()); |
31 child_area.Inset(inside_border_insets_); | 31 child_area.Inset(inside_border_insets_); |
32 int x = child_area.x(); | |
33 int y = child_area.y(); | |
34 | 32 |
35 int padding = 0; | 33 int padding = 0; |
36 if (spread_blank_space_) { | 34 if (main_axis_alignment_ != MAIN_AXIS_ALIGNMENT_START) { |
37 int total = 0; | 35 int total_main_axis_size = 0; |
38 int visible = 0; | 36 int num_visible = 0; |
39 for (int i = 0; i < host->child_count(); ++i) { | 37 for (int i = 0; i < host->child_count(); ++i) { |
40 View* child = host->child_at(i); | 38 View* child = host->child_at(i); |
41 if (!child->visible()) | 39 if (!child->visible()) |
42 continue; | 40 continue; |
43 if (orientation_ == kHorizontal) { | 41 if (orientation_ == kHorizontal) { |
44 total += child->GetPreferredSize().width() + between_child_spacing_; | 42 total_main_axis_size += |
| 43 child->GetPreferredSize().width() + between_child_spacing_; |
45 } else { | 44 } else { |
46 total += child->GetHeightForWidth(child_area.width()) + | 45 total_main_axis_size += child->GetHeightForWidth(child_area.width()) + |
47 between_child_spacing_; | 46 between_child_spacing_; |
48 } | 47 } |
49 ++visible; | 48 ++num_visible; |
50 } | 49 } |
51 | 50 |
52 if (visible) { | 51 if (num_visible) { |
53 total -= between_child_spacing_; | 52 total_main_axis_size -= between_child_spacing_; |
54 if (orientation_ == kHorizontal) | 53 int free_space = MainAxisSize(child_area) - total_main_axis_size; |
55 padding = (child_area.width() - total) / visible; | 54 int position = MainAxisPosition(child_area); |
56 else | 55 int size = MainAxisSize(child_area); |
57 padding = (child_area.height() - total) / visible; | 56 switch (main_axis_alignment_) { |
58 | 57 case MAIN_AXIS_ALIGNMENT_FILL: |
59 if (padding < 0) | 58 padding = std::max(free_space / num_visible, 0); |
60 padding = 0; | 59 break; |
| 60 case MAIN_AXIS_ALIGNMENT_CENTER: |
| 61 position += free_space / 2; |
| 62 size = total_main_axis_size; |
| 63 break; |
| 64 case MAIN_AXIS_ALIGNMENT_END: |
| 65 position += free_space; |
| 66 size = total_main_axis_size; |
| 67 break; |
| 68 default: |
| 69 NOTREACHED(); |
| 70 break; |
| 71 } |
| 72 gfx::Rect new_child_area(child_area); |
| 73 SetMainAxisPosition(position, &new_child_area); |
| 74 SetMainAxisSize(size, &new_child_area); |
| 75 child_area.Intersect(new_child_area); |
61 } | 76 } |
62 } | 77 } |
63 | 78 |
| 79 int x = child_area.x(); |
| 80 int y = child_area.y(); |
64 for (int i = 0; i < host->child_count(); ++i) { | 81 for (int i = 0; i < host->child_count(); ++i) { |
65 View* child = host->child_at(i); | 82 View* child = host->child_at(i); |
66 if (child->visible()) { | 83 if (child->visible()) { |
67 gfx::Rect bounds(x, y, child_area.width(), child_area.height()); | 84 gfx::Rect bounds(x, y, child_area.width(), child_area.height()); |
68 if (orientation_ == kHorizontal) { | 85 if (orientation_ == kHorizontal) { |
69 bounds.set_width(child->GetPreferredSize().width() + padding); | 86 bounds.set_width(child->GetPreferredSize().width() + padding); |
70 if (bounds.width() > 0) | 87 if (bounds.width() > 0) |
71 x += bounds.width() + between_child_spacing_; | 88 x += bounds.width() + between_child_spacing_; |
72 } else { | 89 } else { |
73 bounds.set_height(child->GetHeightForWidth(bounds.width()) + padding); | 90 bounds.set_height(child->GetHeightForWidth(bounds.width()) + padding); |
(...skipping 21 matching lines...) Expand all Loading... |
95 } | 112 } |
96 | 113 |
97 return GetPreferredSizeForChildWidth(host, width); | 114 return GetPreferredSizeForChildWidth(host, width); |
98 } | 115 } |
99 | 116 |
100 int BoxLayout::GetPreferredHeightForWidth(View* host, int width) { | 117 int BoxLayout::GetPreferredHeightForWidth(View* host, int width) { |
101 int child_width = width - NonChildSize(host).width(); | 118 int child_width = width - NonChildSize(host).width(); |
102 return GetPreferredSizeForChildWidth(host, child_width).height(); | 119 return GetPreferredSizeForChildWidth(host, child_width).height(); |
103 } | 120 } |
104 | 121 |
| 122 int BoxLayout::MainAxisSize(const gfx::Rect& child_area) const { |
| 123 return orientation_ == kHorizontal ? child_area.width() : child_area.height(); |
| 124 } |
| 125 |
| 126 int BoxLayout::MainAxisPosition(const gfx::Rect& child_area) const { |
| 127 return orientation_ == kHorizontal ? child_area.x() : child_area.y(); |
| 128 } |
| 129 |
| 130 void BoxLayout::SetMainAxisSize(int size, gfx::Rect* child_area) const { |
| 131 if (orientation_ == kHorizontal) |
| 132 child_area->set_width(size); |
| 133 else |
| 134 child_area->set_height(size); |
| 135 } |
| 136 |
| 137 void BoxLayout::SetMainAxisPosition(int position, gfx::Rect* child_area) const { |
| 138 if (orientation_ == kHorizontal) |
| 139 child_area->set_x(position); |
| 140 else |
| 141 child_area->set_y(position); |
| 142 } |
| 143 |
105 gfx::Size BoxLayout::GetPreferredSizeForChildWidth(View* host, | 144 gfx::Size BoxLayout::GetPreferredSizeForChildWidth(View* host, |
106 int child_area_width) { | 145 int child_area_width) { |
107 gfx::Rect child_area_bounds; | 146 gfx::Rect child_area_bounds; |
108 | 147 |
109 if (orientation_ == kHorizontal) { | 148 if (orientation_ == kHorizontal) { |
110 // Horizontal layouts ignore |child_area_width|, meaning they mimic the | 149 // Horizontal layouts ignore |child_area_width|, meaning they mimic the |
111 // default behavior of GridLayout::GetPreferredHeightForWidth(). | 150 // default behavior of GridLayout::GetPreferredHeightForWidth(). |
112 // TODO(estade): fix this if it ever becomes a problem. | 151 // TODO(estade): fix this if it ever becomes a problem. |
113 int position = 0; | 152 int position = 0; |
114 for (int i = 0; i < host->child_count(); ++i) { | 153 for (int i = 0; i < host->child_count(); ++i) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 child_area_bounds.height() + non_child_size.height()); | 186 child_area_bounds.height() + non_child_size.height()); |
148 } | 187 } |
149 | 188 |
150 gfx::Size BoxLayout::NonChildSize(View* host) { | 189 gfx::Size BoxLayout::NonChildSize(View* host) { |
151 gfx::Insets insets(host->GetInsets()); | 190 gfx::Insets insets(host->GetInsets()); |
152 return gfx::Size(insets.width() + inside_border_insets_.width(), | 191 return gfx::Size(insets.width() + inside_border_insets_.width(), |
153 insets.height() + inside_border_insets_.height()); | 192 insets.height() + inside_border_insets_.height()); |
154 } | 193 } |
155 | 194 |
156 } // namespace views | 195 } // namespace views |
OLD | NEW |