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

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

Issue 12575005: make BoxLayout use and respect GetHeightForWidth(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: BoxLayout::GetPreferredHeightForWidth Created 7 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) 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/insets.h"
8 #include "ui/gfx/rect.h" 7 #include "ui/gfx/rect.h"
9 #include "ui/views/view.h" 8 #include "ui/views/view.h"
10 9
11 namespace views { 10 namespace views {
12 11
13 BoxLayout::BoxLayout(BoxLayout::Orientation orientation, 12 BoxLayout::BoxLayout(BoxLayout::Orientation orientation,
14 int inside_border_horizontal_spacing, 13 int inside_border_horizontal_spacing,
15 int inside_border_vertical_spacing, 14 int inside_border_vertical_spacing,
16 int between_child_spacing) 15 int between_child_spacing)
17 : orientation_(orientation), 16 : orientation_(orientation),
18 inside_border_horizontal_spacing_(inside_border_horizontal_spacing), 17 inside_border_insets_(inside_border_vertical_spacing,
19 inside_border_vertical_spacing_(inside_border_vertical_spacing), 18 inside_border_horizontal_spacing,
19 inside_border_vertical_spacing,
20 inside_border_horizontal_spacing),
20 between_child_spacing_(between_child_spacing), 21 between_child_spacing_(between_child_spacing),
21 spread_blank_space_(false) { 22 spread_blank_space_(false) {
22 } 23 }
23 24
24 BoxLayout::~BoxLayout() { 25 BoxLayout::~BoxLayout() {
25 } 26 }
26 27
27 void BoxLayout::Layout(View* host) { 28 void BoxLayout::Layout(View* host) {
28 gfx::Rect child_area(host->GetLocalBounds()); 29 gfx::Rect child_area(host->GetLocalBounds());
29 child_area.Inset(host->GetInsets()); 30 child_area.Inset(host->GetInsets());
30 child_area.Inset(inside_border_horizontal_spacing_, 31 child_area.Inset(inside_border_insets_);
31 inside_border_vertical_spacing_);
32 int x = child_area.x(); 32 int x = child_area.x();
33 int y = child_area.y(); 33 int y = child_area.y();
34 34
35 int padding = 0; 35 int padding = 0;
36 if (spread_blank_space_) { 36 if (spread_blank_space_) {
37 int total = 0; 37 int total = 0;
38 int visible = 0; 38 int visible = 0;
39 for (int i = 0; i < host->child_count(); ++i) { 39 for (int i = 0; i < host->child_count(); ++i) {
40 View* child = host->child_at(i); 40 View* child = host->child_at(i);
41 if (!child->visible()) 41 if (!child->visible())
42 continue; 42 continue;
43 if (orientation_ == kHorizontal) 43 if (orientation_ == kHorizontal) {
44 total += child->GetPreferredSize().width() + between_child_spacing_; 44 total += child->GetPreferredSize().width() + between_child_spacing_;
45 else 45 } else {
46 total += child->GetPreferredSize().height() + between_child_spacing_; 46 total += child->GetHeightForWidth(child_area.width()) +
47 between_child_spacing_;
48 }
47 ++visible; 49 ++visible;
48 } 50 }
49 51
50 if (visible) { 52 if (visible) {
51 total -= between_child_spacing_; 53 total -= between_child_spacing_;
52 if (orientation_ == kHorizontal) 54 if (orientation_ == kHorizontal)
53 padding = (child_area.width() - total) / visible; 55 padding = (child_area.width() - total) / visible;
54 else 56 else
55 padding = (child_area.height() - total) / visible; 57 padding = (child_area.height() - total) / visible;
56 58
57 if (padding < 0) 59 if (padding < 0)
58 padding = 0; 60 padding = 0;
59 } 61 }
60 } 62 }
61 63
62 for (int i = 0; i < host->child_count(); ++i) { 64 for (int i = 0; i < host->child_count(); ++i) {
63 View* child = host->child_at(i); 65 View* child = host->child_at(i);
64 if (child->visible()) { 66 if (child->visible()) {
65 gfx::Rect bounds(x, y, child_area.width(), child_area.height()); 67 gfx::Rect bounds(x, y, child_area.width(), child_area.height());
66 gfx::Size size(child->GetPreferredSize());
67 if (orientation_ == kHorizontal) { 68 if (orientation_ == kHorizontal) {
68 bounds.set_width(size.width() + padding); 69 bounds.set_width(child->GetPreferredSize().width() + padding);
69 x += size.width() + between_child_spacing_ + padding; 70 x += bounds.width() + between_child_spacing_;
70 } else { 71 } else {
71 bounds.set_height(size.height() + padding); 72 bounds.set_height(child->GetHeightForWidth(bounds.width()) + padding);
72 y += size.height() + between_child_spacing_ + padding; 73 y += bounds.height() + between_child_spacing_;
73 } 74 }
74 // Clamp child view bounds to |child_area|. 75 // Clamp child view bounds to |child_area|.
75 bounds.Intersect(child_area); 76 bounds.Intersect(child_area);
76 child->SetBoundsRect(bounds); 77 child->SetBoundsRect(bounds);
77 } 78 }
78 } 79 }
79 } 80 }
80 81
81 gfx::Size BoxLayout::GetPreferredSize(View* host) { 82 gfx::Size BoxLayout::GetPreferredSize(View* host) {
82 gfx::Rect bounds; 83 // Calculate the child views' preferred width.
83 int position = 0; 84 int width = 0;
84 for (int i = 0; i < host->child_count(); ++i) { 85 if (orientation_ == kVertical) {
85 View* child = host->child_at(i); 86 for (int i = 0; i < host->child_count(); ++i) {
86 if (child->visible()) { 87 View* child = host->child_at(i);
87 gfx::Size size(child->GetPreferredSize()); 88 if (!child->visible())
88 if (orientation_ == kHorizontal) { 89 continue;
89 gfx::Rect child_bounds(position, 0, size.width(), size.height()); 90
90 bounds.Union(child_bounds); 91 width = std::max(width, child->GetPreferredSize().width());
91 position += size.width();
92 } else {
93 gfx::Rect child_bounds(0, position, size.width(), size.height());
94 bounds.Union(child_bounds);
95 position += size.height();
96 }
97 position += between_child_spacing_;
98 } 92 }
99 } 93 }
94
95 return GetPreferredSizeForChildWidth(host, width);
96 }
97
98 int BoxLayout::GetPreferredHeightForWidth(View* host, int width) {
99 int child_width = width - NonChildSize(host).width();
100 return GetPreferredSizeForChildWidth(host, child_width).height();
101 }
102
103 gfx::Size BoxLayout::GetPreferredSizeForChildWidth(View* host,
104 int child_area_width) {
105 gfx::Rect child_area_bounds;
106
107 if (orientation_ == kHorizontal) {
108 // Horizontal layouts ignore |child_area_width|, meaning they mimic the
109 // default behavior of GridLayout::GetPreferredHeightForWidth().
110 // TODO(estade): fix this if it ever becomes a problem.
111 int position = 0;
112 for (int i = 0; i < host->child_count(); ++i) {
113 View* child = host->child_at(i);
114 if (!child->visible())
115 continue;
116
117 gfx::Size size(child->GetPreferredSize());
118 gfx::Rect child_bounds(position, 0, size.width(), size.height());
119 child_area_bounds.Union(child_bounds);
120 position += size.width() + between_child_spacing_;
121 }
122 } else {
123 int height = 0;
124 for (int i = 0; i < host->child_count(); ++i) {
125 View* child = host->child_at(i);
126 if (!child->visible())
127 continue;
128
129 height += child->GetHeightForWidth(child_area_width);
130 if (i != 0)
131 height += between_child_spacing_;
132 }
133
134 child_area_bounds.set_width(child_area_width);
135 child_area_bounds.set_height(height);
136 }
137
138 gfx::Size non_child_size = NonChildSize(host);
139 return gfx::Size(child_area_bounds.width() + non_child_size.width(),
140 child_area_bounds.height() + non_child_size.height());
141 }
142
143 gfx::Size BoxLayout::NonChildSize(View* host) {
100 gfx::Insets insets(host->GetInsets()); 144 gfx::Insets insets(host->GetInsets());
101 return gfx::Size( 145 return gfx::Size(insets.width() + inside_border_insets_.width(),
102 bounds.width() + insets.width() + 2 * inside_border_horizontal_spacing_, 146 insets.height() + inside_border_insets_.height());
103 bounds.height() + insets.height() + 2 * inside_border_vertical_spacing_);
104 } 147 }
105 148
106 } // namespace views 149 } // 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