| 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 if (bounds.height() > 0) | 74 if (bounds.height() > 0) |
| 75 y += bounds.height() + between_child_spacing_; | 75 y += bounds.height() + between_child_spacing_; |
| 76 } | 76 } |
| 77 // Clamp child view bounds to |child_area|. | 77 // Clamp child view bounds to |child_area|. |
| 78 bounds.Intersect(child_area); | 78 bounds.Intersect(child_area); |
| 79 child->SetBoundsRect(bounds); | 79 child->SetBoundsRect(bounds); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 } | 82 } |
| 83 | 83 |
| 84 gfx::Size BoxLayout::GetPreferredSize(View* host) { | 84 gfx::Size BoxLayout::GetPreferredSize(const View* host) const { |
| 85 // Calculate the child views' preferred width. | 85 // Calculate the child views' preferred width. |
| 86 int width = 0; | 86 int width = 0; |
| 87 if (orientation_ == kVertical) { | 87 if (orientation_ == kVertical) { |
| 88 for (int i = 0; i < host->child_count(); ++i) { | 88 for (int i = 0; i < host->child_count(); ++i) { |
| 89 View* child = host->child_at(i); | 89 const View* child = host->child_at(i); |
| 90 if (!child->visible()) | 90 if (!child->visible()) |
| 91 continue; | 91 continue; |
| 92 | 92 |
| 93 width = std::max(width, child->GetPreferredSize().width()); | 93 width = std::max(width, child->GetPreferredSize().width()); |
| 94 } | 94 } |
| 95 } | 95 } |
| 96 | 96 |
| 97 return GetPreferredSizeForChildWidth(host, width); | 97 return GetPreferredSizeForChildWidth(host, width); |
| 98 } | 98 } |
| 99 | 99 |
| 100 int BoxLayout::GetPreferredHeightForWidth(View* host, int width) { | 100 int BoxLayout::GetPreferredHeightForWidth(const View* host, int width) const { |
| 101 int child_width = width - NonChildSize(host).width(); | 101 int child_width = width - NonChildSize(host).width(); |
| 102 return GetPreferredSizeForChildWidth(host, child_width).height(); | 102 return GetPreferredSizeForChildWidth(host, child_width).height(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 gfx::Size BoxLayout::GetPreferredSizeForChildWidth(View* host, | 105 gfx::Size BoxLayout::GetPreferredSizeForChildWidth(const View* host, |
| 106 int child_area_width) { | 106 int child_area_width) const { |
| 107 gfx::Rect child_area_bounds; | 107 gfx::Rect child_area_bounds; |
| 108 | 108 |
| 109 if (orientation_ == kHorizontal) { | 109 if (orientation_ == kHorizontal) { |
| 110 // Horizontal layouts ignore |child_area_width|, meaning they mimic the | 110 // Horizontal layouts ignore |child_area_width|, meaning they mimic the |
| 111 // default behavior of GridLayout::GetPreferredHeightForWidth(). | 111 // default behavior of GridLayout::GetPreferredHeightForWidth(). |
| 112 // TODO(estade): fix this if it ever becomes a problem. | 112 // TODO(estade): fix this if it ever becomes a problem. |
| 113 int position = 0; | 113 int position = 0; |
| 114 for (int i = 0; i < host->child_count(); ++i) { | 114 for (int i = 0; i < host->child_count(); ++i) { |
| 115 View* child = host->child_at(i); | 115 const View* child = host->child_at(i); |
| 116 if (!child->visible()) | 116 if (!child->visible()) |
| 117 continue; | 117 continue; |
| 118 | 118 |
| 119 gfx::Size size(child->GetPreferredSize()); | 119 gfx::Size size(child->GetPreferredSize()); |
| 120 if (size.IsEmpty()) | 120 if (size.IsEmpty()) |
| 121 continue; | 121 continue; |
| 122 | 122 |
| 123 gfx::Rect child_bounds(position, 0, size.width(), size.height()); | 123 gfx::Rect child_bounds(position, 0, size.width(), size.height()); |
| 124 child_area_bounds.Union(child_bounds); | 124 child_area_bounds.Union(child_bounds); |
| 125 position += size.width() + between_child_spacing_; | 125 position += size.width() + between_child_spacing_; |
| 126 } | 126 } |
| 127 } else { | 127 } else { |
| 128 int height = 0; | 128 int height = 0; |
| 129 for (int i = 0; i < host->child_count(); ++i) { | 129 for (int i = 0; i < host->child_count(); ++i) { |
| 130 View* child = host->child_at(i); | 130 const View* child = host->child_at(i); |
| 131 if (!child->visible()) | 131 if (!child->visible()) |
| 132 continue; | 132 continue; |
| 133 | 133 |
| 134 int extra_height = child->GetHeightForWidth(child_area_width); | 134 int extra_height = child->GetHeightForWidth(child_area_width); |
| 135 // Only add |between_child_spacing_| if this is not the only child. | 135 // Only add |between_child_spacing_| if this is not the only child. |
| 136 if (height != 0 && extra_height > 0) | 136 if (height != 0 && extra_height > 0) |
| 137 height += between_child_spacing_; | 137 height += between_child_spacing_; |
| 138 height += extra_height; | 138 height += extra_height; |
| 139 } | 139 } |
| 140 | 140 |
| 141 child_area_bounds.set_width(child_area_width); | 141 child_area_bounds.set_width(child_area_width); |
| 142 child_area_bounds.set_height(height); | 142 child_area_bounds.set_height(height); |
| 143 } | 143 } |
| 144 | 144 |
| 145 gfx::Size non_child_size = NonChildSize(host); | 145 gfx::Size non_child_size = NonChildSize(host); |
| 146 return gfx::Size(child_area_bounds.width() + non_child_size.width(), | 146 return gfx::Size(child_area_bounds.width() + non_child_size.width(), |
| 147 child_area_bounds.height() + non_child_size.height()); | 147 child_area_bounds.height() + non_child_size.height()); |
| 148 } | 148 } |
| 149 | 149 |
| 150 gfx::Size BoxLayout::NonChildSize(View* host) { | 150 gfx::Size BoxLayout::NonChildSize(const View* host) const { |
| 151 gfx::Insets insets(host->GetInsets()); | 151 gfx::Insets insets(host->GetInsets()); |
| 152 return gfx::Size(insets.width() + inside_border_insets_.width(), | 152 return gfx::Size(insets.width() + inside_border_insets_.width(), |
| 153 insets.height() + inside_border_insets_.height()); | 153 insets.height() + inside_border_insets_.height()); |
| 154 } | 154 } |
| 155 | 155 |
| 156 } // namespace views | 156 } // namespace views |
| OLD | NEW |