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 #ifndef VIEWS_LAYOUT_GRID_LAYOUT_H_ | 5 #ifndef VIEWS_LAYOUT_GRID_LAYOUT_H_ |
6 #define VIEWS_LAYOUT_GRID_LAYOUT_H_ | 6 #define VIEWS_LAYOUT_GRID_LAYOUT_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include "ui/views/layout/grid_layout.h" |
10 #include <vector> | 10 // TODO(tfarina): remove this file once all includes have been updated. |
11 | |
12 #include "views/layout/layout_manager.h" | |
13 #include "views/view.h" | |
14 | |
15 namespace gfx { | |
16 class Insets; | |
17 } | |
18 | |
19 // GridLayout is a LayoutManager that positions child Views in a grid. You | |
20 // define the structure of the Grid first, then add the Views. | |
21 // The following creates a trivial grid with two columns separated by | |
22 // a column with padding: | |
23 // ColumnSet* columns = layout->AddColumnSet(0); // Give this column an | |
24 // // identifier of 0. | |
25 // columns->AddColumn(FILL, // Views are horizontally resized to fill column. | |
26 // FILL, // Views starting in this column are vertically | |
27 // // resized. | |
28 // 1, // This column has a resize weight of 1. | |
29 // USE_PREF, // Use the preferred size of the view. | |
30 // 0, // Ignored for USE_PREF. | |
31 // 0); // A minimum width of 0. | |
32 // columns->AddPaddingColumn(0, // The padding column is not resizable. | |
33 // 10); // And has a width of 10 pixels. | |
34 // columns->AddColumn(FILL, FILL, 0, USE_PREF, 0, 0); | |
35 // Now add the views: | |
36 // // First start a row. | |
37 // layout->StartRow(0, // This row isn't vertically resizable. | |
38 // 0); // The column set to use for this row. | |
39 // layout->AddView(v1); | |
40 // Notice you need not skip over padding columns, that's done for you. | |
41 // layout->AddView(v2); | |
42 // | |
43 // When adding a Column you give it the default alignment for all views | |
44 // originating in that column. You can override this for specific views | |
45 // when adding them. For example, the following forces a View to have | |
46 // a horizontal and vertical alignment of leading regardless of that defined | |
47 // for the column: | |
48 // layout->AddView(v1, 1, 1, LEADING, LEADING); | |
49 // | |
50 // If the View using GridLayout is given a size bigger than the preferred, | |
51 // columns and rows with a resize percent > 0 are resized. Each column/row | |
52 // is given resize_percent / total_resize_percent * extra_pixels extra | |
53 // pixels. Only Views with an Alignment of FILL are given extra space, others | |
54 // are aligned in the provided space. | |
55 // | |
56 // GridLayout allows you to define multiple column sets. When you start a | |
57 // new row you specify the id of the column set the row is to use. | |
58 // | |
59 // GridLayout allows you to force columns to have the same width. This is | |
60 // done using the LinkColumnSizes method. | |
61 // | |
62 // AddView takes care of adding the View to the View the GridLayout was | |
63 // created with. | |
64 namespace views { | |
65 | |
66 class Column; | |
67 class ColumnSet; | |
68 class Row; | |
69 class View; | |
70 | |
71 struct ViewState; | |
72 | |
73 class VIEWS_EXPORT GridLayout : public LayoutManager { | |
74 public: | |
75 // An enumeration of the possible alignments supported by GridLayout. | |
76 enum Alignment { | |
77 // Leading equates to left along the horizontal axis, and top along the | |
78 // vertical axis. | |
79 LEADING, | |
80 | |
81 // Centers the view along the axis. | |
82 CENTER, | |
83 | |
84 // Trailing equals to right along the horizontal axis, and bottom along | |
85 // the vertical axis. | |
86 TRAILING, | |
87 | |
88 // The view is resized to fill the space. | |
89 FILL, | |
90 | |
91 // The view is aligned along the baseline. This is only valid for the | |
92 // vertical axis. | |
93 BASELINE | |
94 }; | |
95 | |
96 // An enumeration of the possible ways the size of a column may be obtained. | |
97 enum SizeType { | |
98 // The column size is fixed. | |
99 FIXED, | |
100 | |
101 // The preferred size of the view is used to determine the column size. | |
102 USE_PREF | |
103 }; | |
104 | |
105 explicit GridLayout(View* host); | |
106 virtual ~GridLayout(); | |
107 | |
108 // Creates a GridLayout with kPanel*Margin insets. | |
109 static GridLayout* CreatePanel(View* host); | |
110 | |
111 // Sets the insets. All views are placed relative to these offsets. | |
112 void SetInsets(int top, int left, int bottom, int right); | |
113 void SetInsets(const gfx::Insets& insets); | |
114 | |
115 // Creates a new column set with the specified id and returns it. | |
116 // The id is later used when starting a new row. | |
117 // GridLayout takes ownership of the ColumnSet and will delete it when | |
118 // the GridLayout is deleted. | |
119 ColumnSet* AddColumnSet(int id); | |
120 | |
121 // Adds a padding row. Padding rows typically don't have any views, and | |
122 // but are used to provide vertical white space between views. | |
123 // Size specifies the height of the row. | |
124 void AddPaddingRow(float vertical_resize, int size); | |
125 | |
126 // A convenience for AddPaddingRow followed by StartRow. | |
127 void StartRowWithPadding(float vertical_resize, int column_set_id, | |
128 float padding_resize, int padding); | |
129 | |
130 // Starts a new row with the specified column set. | |
131 void StartRow(float vertical_resize, int column_set_id); | |
132 | |
133 // Advances past columns. Use this when the current column should not | |
134 // contain any views. | |
135 void SkipColumns(int col_count); | |
136 | |
137 // Adds a view using the default alignment from the column. The added | |
138 // view has a column and row span of 1. | |
139 // As a convenience this adds the view to the host. The view becomes owned | |
140 // by the host, and NOT this GridLayout. | |
141 void AddView(View* view); | |
142 | |
143 // Adds a view using the default alignment from the column. | |
144 // As a convenience this adds the view to the host. The view becomes owned | |
145 // by the host, and NOT this GridLayout. | |
146 void AddView(View* view, int col_span, int row_span); | |
147 | |
148 // Adds a view with the specified alignment and spans. | |
149 // As a convenience this adds the view to the host. The view becomes owned | |
150 // by the host, and NOT this GridLayout. | |
151 void AddView(View* view, int col_span, int row_span, Alignment h_align, | |
152 Alignment v_align); | |
153 | |
154 // Adds a view with the specified alignment and spans. If | |
155 // pref_width/pref_height is > 0 then the preferred width/height of the view | |
156 // is fixed to the specified value. | |
157 // As a convenience this adds the view to the host. The view becomes owned | |
158 // by the host, and NOT this GridLayout. | |
159 void AddView(View* view, int col_span, int row_span, | |
160 Alignment h_align, Alignment v_align, | |
161 int pref_width, int pref_height); | |
162 | |
163 // Notification we've been installed on a particular host. Checks that host | |
164 // is the same as the View supplied in the constructor. | |
165 virtual void Installed(View* host); | |
166 | |
167 // Notification we've been uninstalled on a particular host. Checks that host | |
168 // is the same as the View supplied in the constructor. | |
169 virtual void Uninstalled(View* host); | |
170 | |
171 // Notification that a view has been added. | |
172 virtual void ViewAdded(View* host, View* view); | |
173 | |
174 // Notification that a view has been removed. | |
175 virtual void ViewRemoved(View* host, View* view); | |
176 | |
177 // Layouts out the components. | |
178 virtual void Layout(View* host); | |
179 | |
180 // Returns the preferred size for the GridLayout. | |
181 virtual gfx::Size GetPreferredSize(View* host); | |
182 | |
183 virtual int GetPreferredHeightForWidth(View* host, int width); | |
184 | |
185 private: | |
186 // As both Layout and GetPreferredSize need to do nearly the same thing, | |
187 // they both call into this method. This sizes the Columns/Rows as | |
188 // appropriate. If layout is true, width/height give the width/height the | |
189 // of the host, otherwise they are ignored. | |
190 void SizeRowsAndColumns(bool layout, int width, int height, gfx::Size* pref); | |
191 | |
192 // Calculates the master columns of all the column sets. See Column for | |
193 // a description of what a master column is. | |
194 void CalculateMasterColumnsIfNecessary(); | |
195 | |
196 // This is called internally from AddView. It adds the ViewState to the | |
197 // appropriate structures, and updates internal fields such as next_column_. | |
198 void AddViewState(ViewState* view_state); | |
199 | |
200 // Returns the column set for the specified id, or NULL if one doesn't exist. | |
201 ColumnSet* GetColumnSet(int id); | |
202 | |
203 // Adds the Row to rows_, as well as updating next_column_, | |
204 // current_row_col_set ... | |
205 void AddRow(Row* row); | |
206 | |
207 // As the name says, updates the remaining_height of the ViewState for | |
208 // all Rows the supplied ViewState touches. | |
209 void UpdateRemainingHeightFromRows(ViewState* state); | |
210 | |
211 // If the view state's remaining height is > 0, it is distributed among | |
212 // the rows the view state touches. This is used during layout to make | |
213 // sure the Rows can accommodate a view. | |
214 void DistributeRemainingHeight(ViewState* state); | |
215 | |
216 // Advances next_column_ past any padding columns. | |
217 void SkipPaddingColumns(); | |
218 | |
219 // Returns the column set of the last non-padding row. | |
220 ColumnSet* GetLastValidColumnSet(); | |
221 | |
222 // The view we were created with. We don't own this. | |
223 View* const host_; | |
224 | |
225 // Whether or not we've calculated the master/linked columns. | |
226 bool calculated_master_columns_; | |
227 | |
228 // Used to verify a view isn't added with a row span that expands into | |
229 // another column structure. | |
230 int remaining_row_span_; | |
231 | |
232 // Current row. | |
233 int current_row_; | |
234 | |
235 // Current column. | |
236 int next_column_; | |
237 | |
238 // Column set for the current row. This is null for padding rows. | |
239 ColumnSet* current_row_col_set_; | |
240 | |
241 // Insets. | |
242 int top_inset_; | |
243 int bottom_inset_; | |
244 int left_inset_; | |
245 int right_inset_; | |
246 | |
247 // Set to true when adding a View. | |
248 bool adding_view_; | |
249 | |
250 // ViewStates. This is ordered by row_span in ascending order. | |
251 std::vector<ViewState*> view_states_; | |
252 | |
253 // ColumnSets. | |
254 std::vector<ColumnSet*> column_sets_; | |
255 | |
256 // Rows. | |
257 std::vector<Row*> rows_; | |
258 | |
259 DISALLOW_COPY_AND_ASSIGN(GridLayout); | |
260 }; | |
261 | |
262 // ColumnSet is used to define a set of columns. GridLayout may have any | |
263 // number of ColumnSets. You don't create a ColumnSet directly, instead | |
264 // use the AddColumnSet method of GridLayout. | |
265 class VIEWS_EXPORT ColumnSet { | |
266 public: | |
267 ~ColumnSet(); | |
268 | |
269 // Adds a column for padding. When adding views, padding columns are | |
270 // automatically skipped. For example, if you create a column set with | |
271 // two columns separated by a padding column, the first AddView automatically | |
272 // skips past the padding column. That is, to add two views, do: | |
273 // layout->AddView(v1); layout->AddView(v2);, not: | |
274 // layout->AddView(v1); layout->SkipColumns(1); layout->AddView(v2); | |
275 void AddPaddingColumn(float resize_percent, int width); | |
276 | |
277 // Adds a column. The alignment gives the default alignment for views added | |
278 // with no explicit alignment. fixed_width gives a specific width for the | |
279 // column, and is only used if size_type == FIXED. min_width gives the | |
280 // minimum width for the column. | |
281 // | |
282 // If none of the columns in a columnset are resizable, the views are only | |
283 // made as wide as the widest views in each column, even if extra space is | |
284 // provided. In other words, GridLayout does not automatically resize views | |
285 // unless the column is marked as resizable. | |
286 void AddColumn(GridLayout::Alignment h_align, | |
287 GridLayout::Alignment v_align, | |
288 float resize_percent, | |
289 GridLayout::SizeType size_type, | |
290 int fixed_width, | |
291 int min_width); | |
292 | |
293 // Forces the specified columns to have the same size. The size of | |
294 // linked columns is that of the max of the specified columns. This | |
295 // must end with -1. For example, the following forces the first and | |
296 // second column to have the same size: | |
297 // LinkColumnSizes(0, 1, -1); | |
298 void LinkColumnSizes(int first, ...); | |
299 | |
300 // ID of this ColumnSet. | |
301 int id() const { return id_; } | |
302 | |
303 int num_columns() { return static_cast<int>(columns_.size()); } | |
304 | |
305 private: | |
306 friend class GridLayout; | |
307 | |
308 explicit ColumnSet(int id); | |
309 | |
310 void AddColumn(GridLayout::Alignment h_align, | |
311 GridLayout::Alignment v_align, | |
312 float resize_percent, | |
313 GridLayout::SizeType size_type, | |
314 int fixed_width, | |
315 int min_width, | |
316 bool is_padding); | |
317 | |
318 void AddViewState(ViewState* view_state); | |
319 | |
320 // Set description of these. | |
321 void CalculateMasterColumns(); | |
322 void AccumulateMasterColumns(); | |
323 | |
324 // Sets the size of each linked column to be the same. | |
325 void UnifySameSizedColumnSizes(); | |
326 | |
327 // Updates the remaining width field of the ViewState from that of the | |
328 // columns the view spans. | |
329 void UpdateRemainingWidth(ViewState* view_state); | |
330 | |
331 // Makes sure the columns touched by view state are big enough for the | |
332 // view. | |
333 void DistributeRemainingWidth(ViewState* view_state); | |
334 | |
335 // Returns the total size needed for this ColumnSet. | |
336 int LayoutWidth(); | |
337 | |
338 // Returns the width of the specified columns. | |
339 int GetColumnWidth(int start_col, int col_span); | |
340 | |
341 // Updates the x coordinate of each column from the previous ones. | |
342 // NOTE: this doesn't include the insets. | |
343 void ResetColumnXCoordinates(); | |
344 | |
345 // Calculate the preferred width of each view in this column set, as well | |
346 // as updating the remaining_width. | |
347 void CalculateSize(); | |
348 | |
349 // Distributes delta amoung the resizable columns. | |
350 void Resize(int delta); | |
351 | |
352 // ID for this columnset. | |
353 const int id_; | |
354 | |
355 // The columns. | |
356 std::vector<Column*> columns_; | |
357 | |
358 // The ViewStates. This is sorted based on column_span in ascending | |
359 // order. | |
360 std::vector<ViewState*> view_states_; | |
361 | |
362 // The master column of those columns that are linked. See Column | |
363 // for a description of what the master column is. | |
364 std::vector<Column*> master_columns_; | |
365 | |
366 DISALLOW_COPY_AND_ASSIGN(ColumnSet); | |
367 }; | |
368 | |
369 } // namespace views | |
370 | 11 |
371 #endif // VIEWS_LAYOUT_GRID_LAYOUT_H_ | 12 #endif // VIEWS_LAYOUT_GRID_LAYOUT_H_ |
OLD | NEW |