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

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: unit test 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
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" 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
(...skipping 22 matching lines...) Expand all
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) {
sky 2013/03/13 14:03:03 You should also override GetPreferredSizeForWidth
Evan Stade 2013/03/13 23:49:56 done, although I didn't add an implementation for
82 gfx::Rect bounds; 83 gfx::Rect bounds;
83 int position = 0; 84
84 for (int i = 0; i < host->child_count(); ++i) { 85 if (orientation_ == kHorizontal) {
85 View* child = host->child_at(i); 86 int position = 0;
86 if (child->visible()) { 87 for (int i = 0; i < host->child_count(); ++i) {
88 View* child = host->child_at(i);
89 if (!child->visible())
90 continue;
91
87 gfx::Size size(child->GetPreferredSize()); 92 gfx::Size size(child->GetPreferredSize());
88 if (orientation_ == kHorizontal) { 93 gfx::Rect child_bounds(position, 0, size.width(), size.height());
89 gfx::Rect child_bounds(position, 0, size.width(), size.height()); 94 bounds.Union(child_bounds);
90 bounds.Union(child_bounds); 95 position += size.width() + between_child_spacing_;
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 } 96 }
97 } else {
98 int width = 0;
99 for (int i = 0; i < host->child_count(); ++i) {
100 View* child = host->child_at(i);
101 if (!child->visible())
102 continue;
103
104 width = std::max(width, child->GetPreferredSize().width());
105 }
106
107 int height = 0;
108 for (int i = 0; i < host->child_count(); ++i) {
109 View* child = host->child_at(i);
110 if (!child->visible())
111 continue;
112
113 height += child->GetHeightForWidth(width);
114 if (i != 0)
115 height += between_child_spacing_;
116 }
117
118 bounds.set_width(width);
119 bounds.set_height(height);
99 } 120 }
100 gfx::Insets insets(host->GetInsets()); 121 gfx::Insets insets(host->GetInsets());
101 return gfx::Size( 122 return gfx::Size(
102 bounds.width() + insets.width() + 2 * inside_border_horizontal_spacing_, 123 bounds.width() + insets.width() + 2 * inside_border_horizontal_spacing_,
103 bounds.height() + insets.height() + 2 * inside_border_vertical_spacing_); 124 bounds.height() + insets.height() + 2 * inside_border_vertical_spacing_);
104 } 125 }
105 126
106 } // namespace views 127 } // namespace views
OLDNEW
« no previous file with comments | « no previous file | ui/views/layout/box_layout_unittest.cc » ('j') | ui/views/layout/box_layout_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698