Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(592)

Side by Side Diff: ui/views/layout/box_layout.cc

Issue 9465042: views: Add option in BoxLayout to spread blank space among the child views. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/views/layout/box_layout.h ('k') | ui/views/layout/box_layout_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
OLDNEW
« no previous file with comments | « ui/views/layout/box_layout.h ('k') | ui/views/layout/box_layout_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698