| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/insets.h" | 7 #include "ui/gfx/insets.h" |
| 8 #include "ui/gfx/rect.h" | 8 #include "ui/gfx/rect.h" |
| 9 #include "ui/views/view.h" | 9 #include "ui/views/view.h" |
| 10 | 10 |
| 11 namespace views { | 11 namespace views { |
| 12 | 12 |
| 13 BoxLayout::BoxLayout(BoxLayout::Orientation orientation, | 13 BoxLayout::BoxLayout(BoxLayout::Orientation orientation, |
| 14 int inside_border_horizontal_spacing, | 14 int inside_border_horizontal_spacing, |
| 15 int inside_border_vertical_spacing, | 15 int inside_border_vertical_spacing, |
| 16 int between_child_spacing) | 16 int between_child_spacing) |
| 17 : orientation_(orientation), | 17 : orientation_(orientation), |
| 18 inside_border_horizontal_spacing_(inside_border_horizontal_spacing), | 18 inside_border_horizontal_spacing_(inside_border_horizontal_spacing), |
| 19 inside_border_vertical_spacing_(inside_border_vertical_spacing), | 19 inside_border_vertical_spacing_(inside_border_vertical_spacing), |
| 20 between_child_spacing_(between_child_spacing) { | 20 between_child_spacing_(between_child_spacing), |
| 21 spread_blank_space_(false) { |
| 21 } | 22 } |
| 22 | 23 |
| 23 BoxLayout::~BoxLayout() { | 24 BoxLayout::~BoxLayout() { |
| 24 } | 25 } |
| 25 | 26 |
| 26 void BoxLayout::Layout(View* host) { | 27 void BoxLayout::Layout(View* host) { |
| 27 gfx::Rect child_area(host->GetLocalBounds()); | 28 gfx::Rect child_area(host->GetLocalBounds()); |
| 28 child_area.Inset(host->GetInsets()); | 29 child_area.Inset(host->GetInsets()); |
| 29 child_area.Inset(inside_border_horizontal_spacing_, | 30 child_area.Inset(inside_border_horizontal_spacing_, |
| 30 inside_border_vertical_spacing_); | 31 inside_border_vertical_spacing_); |
| 31 int x = child_area.x(); | 32 int x = child_area.x(); |
| 32 int y = child_area.y(); | 33 int y = child_area.y(); |
| 34 |
| 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; |
| 48 } |
| 49 |
| 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; |
| 56 |
| 57 if (padding < 0) |
| 58 padding = 0; |
| 59 } |
| 60 } |
| 61 |
| 33 for (int i = 0; i < host->child_count(); ++i) { | 62 for (int i = 0; i < host->child_count(); ++i) { |
| 34 View* child = host->child_at(i); | 63 View* child = host->child_at(i); |
| 35 if (child->visible()) { | 64 if (child->visible()) { |
| 36 gfx::Rect bounds(x, y, child_area.width(), child_area.height()); | 65 gfx::Rect bounds(x, y, child_area.width(), child_area.height()); |
| 37 gfx::Size size(child->GetPreferredSize()); | 66 gfx::Size size(child->GetPreferredSize()); |
| 38 if (orientation_ == kHorizontal) { | 67 if (orientation_ == kHorizontal) { |
| 39 bounds.set_width(size.width()); | 68 bounds.set_width(size.width() + padding); |
| 40 x += size.width() + between_child_spacing_; | 69 x += size.width() + between_child_spacing_ + padding; |
| 41 } else { | 70 } else { |
| 42 bounds.set_height(size.height()); | 71 bounds.set_height(size.height() + padding); |
| 43 y += size.height() + between_child_spacing_; | 72 y += size.height() + between_child_spacing_ + padding; |
| 44 } | 73 } |
| 45 // Clamp child view bounds to |child_area|. | 74 // Clamp child view bounds to |child_area|. |
| 46 child->SetBoundsRect(bounds.Intersect(child_area)); | 75 child->SetBoundsRect(bounds.Intersect(child_area)); |
| 47 } | 76 } |
| 48 } | 77 } |
| 49 } | 78 } |
| 50 | 79 |
| 51 gfx::Size BoxLayout::GetPreferredSize(View* host) { | 80 gfx::Size BoxLayout::GetPreferredSize(View* host) { |
| 52 gfx::Rect bounds; | 81 gfx::Rect bounds; |
| 53 int position = 0; | 82 int position = 0; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 67 position += between_child_spacing_; | 96 position += between_child_spacing_; |
| 68 } | 97 } |
| 69 } | 98 } |
| 70 gfx::Insets insets(host->GetInsets()); | 99 gfx::Insets insets(host->GetInsets()); |
| 71 return gfx::Size( | 100 return gfx::Size( |
| 72 bounds.width() + insets.width() + 2 * inside_border_horizontal_spacing_, | 101 bounds.width() + insets.width() + 2 * inside_border_horizontal_spacing_, |
| 73 bounds.height() + insets.height() + 2 * inside_border_vertical_spacing_); | 102 bounds.height() + insets.height() + 2 * inside_border_vertical_spacing_); |
| 74 } | 103 } |
| 75 | 104 |
| 76 } // namespace views | 105 } // namespace views |
| OLD | NEW |