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 { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 if (bounds.height() > 0) | 91 if (bounds.height() > 0) |
92 y += bounds.height() + between_child_spacing_; | 92 y += bounds.height() + between_child_spacing_; |
93 } | 93 } |
94 // Clamp child view bounds to |child_area|. | 94 // Clamp child view bounds to |child_area|. |
95 bounds.Intersect(child_area); | 95 bounds.Intersect(child_area); |
96 child->SetBoundsRect(bounds); | 96 child->SetBoundsRect(bounds); |
97 } | 97 } |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 gfx::Size BoxLayout::GetPreferredSize(View* host) { | 101 gfx::Size BoxLayout::GetPreferredSize(const View* host) const { |
102 // Calculate the child views' preferred width. | 102 // Calculate the child views' preferred width. |
103 int width = 0; | 103 int width = 0; |
104 if (orientation_ == kVertical) { | 104 if (orientation_ == kVertical) { |
105 for (int i = 0; i < host->child_count(); ++i) { | 105 for (int i = 0; i < host->child_count(); ++i) { |
106 View* child = host->child_at(i); | 106 const View* child = host->child_at(i); |
107 if (!child->visible()) | 107 if (!child->visible()) |
108 continue; | 108 continue; |
109 | 109 |
110 width = std::max(width, child->GetPreferredSize().width()); | 110 width = std::max(width, child->GetPreferredSize().width()); |
111 } | 111 } |
112 } | 112 } |
113 | 113 |
114 return GetPreferredSizeForChildWidth(host, width); | 114 return GetPreferredSizeForChildWidth(host, width); |
115 } | 115 } |
116 | 116 |
117 int BoxLayout::GetPreferredHeightForWidth(View* host, int width) { | 117 int BoxLayout::GetPreferredHeightForWidth(const View* host, int width) const { |
118 int child_width = width - NonChildSize(host).width(); | 118 int child_width = width - NonChildSize(host).width(); |
119 return GetPreferredSizeForChildWidth(host, child_width).height(); | 119 return GetPreferredSizeForChildWidth(host, child_width).height(); |
120 } | 120 } |
121 | 121 |
122 int BoxLayout::MainAxisSize(const gfx::Rect& child_area) const { | 122 int BoxLayout::MainAxisSize(const gfx::Rect& child_area) const { |
123 return orientation_ == kHorizontal ? child_area.width() : child_area.height(); | 123 return orientation_ == kHorizontal ? child_area.width() : child_area.height(); |
124 } | 124 } |
125 | 125 |
126 int BoxLayout::MainAxisPosition(const gfx::Rect& child_area) const { | 126 int BoxLayout::MainAxisPosition(const gfx::Rect& child_area) const { |
127 return orientation_ == kHorizontal ? child_area.x() : child_area.y(); | 127 return orientation_ == kHorizontal ? child_area.x() : child_area.y(); |
128 } | 128 } |
129 | 129 |
130 void BoxLayout::SetMainAxisSize(int size, gfx::Rect* child_area) const { | 130 void BoxLayout::SetMainAxisSize(int size, gfx::Rect* child_area) const { |
131 if (orientation_ == kHorizontal) | 131 if (orientation_ == kHorizontal) |
132 child_area->set_width(size); | 132 child_area->set_width(size); |
133 else | 133 else |
134 child_area->set_height(size); | 134 child_area->set_height(size); |
135 } | 135 } |
136 | 136 |
137 void BoxLayout::SetMainAxisPosition(int position, gfx::Rect* child_area) const { | 137 void BoxLayout::SetMainAxisPosition(int position, gfx::Rect* child_area) const { |
138 if (orientation_ == kHorizontal) | 138 if (orientation_ == kHorizontal) |
139 child_area->set_x(position); | 139 child_area->set_x(position); |
140 else | 140 else |
141 child_area->set_y(position); | 141 child_area->set_y(position); |
142 } | 142 } |
143 | 143 |
144 gfx::Size BoxLayout::GetPreferredSizeForChildWidth(View* host, | 144 gfx::Size BoxLayout::GetPreferredSizeForChildWidth(const View* host, |
145 int child_area_width) { | 145 int child_area_width) const { |
146 gfx::Rect child_area_bounds; | 146 gfx::Rect child_area_bounds; |
147 | 147 |
148 if (orientation_ == kHorizontal) { | 148 if (orientation_ == kHorizontal) { |
149 // Horizontal layouts ignore |child_area_width|, meaning they mimic the | 149 // Horizontal layouts ignore |child_area_width|, meaning they mimic the |
150 // default behavior of GridLayout::GetPreferredHeightForWidth(). | 150 // default behavior of GridLayout::GetPreferredHeightForWidth(). |
151 // TODO(estade): fix this if it ever becomes a problem. | 151 // TODO(estade): fix this if it ever becomes a problem. |
152 int position = 0; | 152 int position = 0; |
153 for (int i = 0; i < host->child_count(); ++i) { | 153 for (int i = 0; i < host->child_count(); ++i) { |
154 View* child = host->child_at(i); | 154 const View* child = host->child_at(i); |
155 if (!child->visible()) | 155 if (!child->visible()) |
156 continue; | 156 continue; |
157 | 157 |
158 gfx::Size size(child->GetPreferredSize()); | 158 gfx::Size size(child->GetPreferredSize()); |
159 if (size.IsEmpty()) | 159 if (size.IsEmpty()) |
160 continue; | 160 continue; |
161 | 161 |
162 gfx::Rect child_bounds(position, 0, size.width(), size.height()); | 162 gfx::Rect child_bounds(position, 0, size.width(), size.height()); |
163 child_area_bounds.Union(child_bounds); | 163 child_area_bounds.Union(child_bounds); |
164 position += size.width() + between_child_spacing_; | 164 position += size.width() + between_child_spacing_; |
165 } | 165 } |
166 } else { | 166 } else { |
167 int height = 0; | 167 int height = 0; |
168 for (int i = 0; i < host->child_count(); ++i) { | 168 for (int i = 0; i < host->child_count(); ++i) { |
169 View* child = host->child_at(i); | 169 const View* child = host->child_at(i); |
170 if (!child->visible()) | 170 if (!child->visible()) |
171 continue; | 171 continue; |
172 | 172 |
173 int extra_height = child->GetHeightForWidth(child_area_width); | 173 int extra_height = child->GetHeightForWidth(child_area_width); |
174 // Only add |between_child_spacing_| if this is not the only child. | 174 // Only add |between_child_spacing_| if this is not the only child. |
175 if (height != 0 && extra_height > 0) | 175 if (height != 0 && extra_height > 0) |
176 height += between_child_spacing_; | 176 height += between_child_spacing_; |
177 height += extra_height; | 177 height += extra_height; |
178 } | 178 } |
179 | 179 |
180 child_area_bounds.set_width(child_area_width); | 180 child_area_bounds.set_width(child_area_width); |
181 child_area_bounds.set_height(height); | 181 child_area_bounds.set_height(height); |
182 } | 182 } |
183 | 183 |
184 gfx::Size non_child_size = NonChildSize(host); | 184 gfx::Size non_child_size = NonChildSize(host); |
185 return gfx::Size(child_area_bounds.width() + non_child_size.width(), | 185 return gfx::Size(child_area_bounds.width() + non_child_size.width(), |
186 child_area_bounds.height() + non_child_size.height()); | 186 child_area_bounds.height() + non_child_size.height()); |
187 } | 187 } |
188 | 188 |
189 gfx::Size BoxLayout::NonChildSize(View* host) { | 189 gfx::Size BoxLayout::NonChildSize(const View* host) const { |
190 gfx::Insets insets(host->GetInsets()); | 190 gfx::Insets insets(host->GetInsets()); |
191 return gfx::Size(insets.width() + inside_border_insets_.width(), | 191 return gfx::Size(insets.width() + inside_border_insets_.width(), |
192 insets.height() + inside_border_insets_.height()); | 192 insets.height() + inside_border_insets_.height()); |
193 } | 193 } |
194 | 194 |
195 } // namespace views | 195 } // namespace views |
OLD | NEW |