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
3 // found in the LICENSE file.
5 #ifndef UI_VIEWS_LAYOUT_GRID_LAYOUT_H_
6 #define UI_VIEWS_LAYOUT_GRID_LAYOUT_H_
11 #include "base/compiler_specific.h"
12 #include "ui/gfx/geometry/insets.h"
13 #include "ui/views/layout/layout_manager.h"
14 #include "ui/views/view.h"
16 // GridLayout is a LayoutManager that positions child Views in a grid. You
17 // define the structure of the Grid first, then add the Views.
18 // The following creates a trivial grid with two columns separated by
19 // a column with padding:
20 // ColumnSet* columns = layout->AddColumnSet(0); // Give this column an
21 // // identifier of 0.
22 // columns->AddColumn(FILL, // Views are horizontally resized to fill column.
23 // FILL, // Views starting in this column are vertically
25 // 1, // This column has a resize weight of 1.
26 // USE_PREF, // Use the preferred size of the view.
27 // 0, // Ignored for USE_PREF.
28 // 0); // A minimum width of 0.
29 // columns->AddPaddingColumn(0, // The padding column is not resizable.
30 // 10); // And has a width of 10 pixels.
31 // columns->AddColumn(FILL, FILL, 0, USE_PREF, 0, 0);
33 // // First start a row.
34 // layout->StartRow(0, // This row isn't vertically resizable.
35 // 0); // The column set to use for this row.
36 // layout->AddView(v1);
37 // Notice you need not skip over padding columns, that's done for you.
38 // layout->AddView(v2);
40 // When adding a Column you give it the default alignment for all views
41 // originating in that column. You can override this for specific views
42 // when adding them. For example, the following forces a View to have
43 // a horizontal and vertical alignment of leading regardless of that defined
45 // layout->AddView(v1, 1, 1, LEADING, LEADING);
47 // If the View using GridLayout is given a size bigger than the preferred,
48 // columns and rows with a resize percent > 0 are resized. Each column/row
49 // is given resize_percent / total_resize_percent * extra_pixels extra
50 // pixels. Only Views with an Alignment of FILL are given extra space, others
51 // are aligned in the provided space.
53 // GridLayout allows you to define multiple column sets. When you start a
54 // new row you specify the id of the column set the row is to use.
56 // GridLayout allows you to force columns to have the same width. This is
57 // done using the LinkColumnSizes method.
59 // AddView takes care of adding the View to the View the GridLayout was
70 class VIEWS_EXPORT GridLayout
: public LayoutManager
{
72 // An enumeration of the possible alignments supported by GridLayout.
74 // Leading equates to left along the horizontal axis, and top along the
78 // Centers the view along the axis.
81 // Trailing equals to right along the horizontal axis, and bottom along
85 // The view is resized to fill the space.
88 // The view is aligned along the baseline. This is only valid for the
93 // An enumeration of the possible ways the size of a column may be obtained.
95 // The column size is fixed.
98 // The preferred size of the view is used to determine the column size.
102 explicit GridLayout(View
* host
);
103 ~GridLayout() override
;
105 // Creates a GridLayout with kPanel*Margin insets.
106 static GridLayout
* CreatePanel(View
* host
);
108 // Sets the insets. All views are placed relative to these offsets.
109 void SetInsets(int top
, int left
, int bottom
, int right
);
110 void SetInsets(const gfx::Insets
& insets
);
112 // Creates a new column set with the specified id and returns it.
113 // The id is later used when starting a new row.
114 // GridLayout takes ownership of the ColumnSet and will delete it when
115 // the GridLayout is deleted.
116 ColumnSet
* AddColumnSet(int id
);
118 // Returns the column set for the specified id, or NULL if one doesn't exist.
119 ColumnSet
* GetColumnSet(int id
);
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
);
126 // A convenience for AddPaddingRow followed by StartRow.
127 void StartRowWithPadding(float vertical_resize
, int column_set_id
,
128 float padding_resize
, int padding
);
130 // Starts a new row with the specified column set.
131 void StartRow(float vertical_resize
, int column_set_id
);
133 // Advances past columns. Use this when the current column should not
134 // contain any views.
135 void SkipColumns(int col_count
);
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
);
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
);
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
,
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
);
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 void Installed(View
* host
) override
;
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 void Uninstalled(View
* host
) override
;
171 // Notification that a view has been added.
172 void ViewAdded(View
* host
, View
* view
) override
;
174 // Notification that a view has been removed.
175 void ViewRemoved(View
* host
, View
* view
) override
;
177 // Layouts out the components.
178 void Layout(View
* host
) override
;
180 // Returns the preferred size for the GridLayout.
181 gfx::Size
GetPreferredSize(const View
* host
) const override
;
183 int GetPreferredHeightForWidth(const View
* host
, int width
) const override
;
185 void set_minimum_size(const gfx::Size
& size
) { minimum_size_
= size
; }
188 // As both Layout and GetPreferredSize need to do nearly the same thing,
189 // they both call into this method. This sizes the Columns/Rows as
190 // appropriate. If layout is true, width/height give the width/height the
191 // of the host, otherwise they are ignored.
192 void SizeRowsAndColumns(bool layout
,
195 gfx::Size
* pref
) const;
197 // Calculates the master columns of all the column sets. See Column for
198 // a description of what a master column is.
199 void CalculateMasterColumnsIfNecessary() const;
201 // This is called internally from AddView. It adds the ViewState to the
202 // appropriate structures, and updates internal fields such as next_column_.
203 void AddViewState(ViewState
* view_state
);
205 // Adds the Row to rows_, as well as updating next_column_,
206 // current_row_col_set ...
207 void AddRow(Row
* row
);
209 // As the name says, updates the remaining_height of the ViewState for
210 // all Rows the supplied ViewState touches.
211 void UpdateRemainingHeightFromRows(ViewState
* state
) const;
213 // If the view state's remaining height is > 0, it is distributed among
214 // the rows the view state touches. This is used during layout to make
215 // sure the Rows can accommodate a view.
216 void DistributeRemainingHeight(ViewState
* state
) const;
218 // Advances next_column_ past any padding columns.
219 void SkipPaddingColumns();
221 // Returns the column set of the last non-padding row.
222 ColumnSet
* GetLastValidColumnSet();
224 // The view we were created with. We don't own this.
227 // Whether or not we've calculated the master/linked columns.
228 mutable bool calculated_master_columns_
;
230 // Used to verify a view isn't added with a row span that expands into
231 // another column structure.
232 int remaining_row_span_
;
240 // Column set for the current row. This is null for padding rows.
241 ColumnSet
* current_row_col_set_
;
246 // Set to true when adding a View.
249 // ViewStates. This is ordered by row_span in ascending order.
250 mutable std::vector
<ViewState
*> view_states_
;
253 mutable std::vector
<ColumnSet
*> column_sets_
;
256 mutable std::vector
<Row
*> rows_
;
258 // Minimum preferred size.
259 gfx::Size minimum_size_
;
261 DISALLOW_COPY_AND_ASSIGN(GridLayout
);
264 // ColumnSet is used to define a set of columns. GridLayout may have any
265 // number of ColumnSets. You don't create a ColumnSet directly, instead
266 // use the AddColumnSet method of GridLayout.
267 class VIEWS_EXPORT ColumnSet
{
271 // Adds a column for padding. When adding views, padding columns are
272 // automatically skipped. For example, if you create a column set with
273 // two columns separated by a padding column, the second AddView automatically
274 // skips past the padding column. That is, to add two views, do:
275 // layout->AddView(v1); layout->AddView(v2);, not:
276 // layout->AddView(v1); layout->SkipColumns(1); layout->AddView(v2);
277 // See class description for details on |resize_percent|.
278 void AddPaddingColumn(float resize_percent
, int width
);
280 // Adds a column. The alignment gives the default alignment for views added
281 // with no explicit alignment. fixed_width gives a specific width for the
282 // column, and is only used if size_type == FIXED. min_width gives the
283 // minimum width for the column.
285 // If none of the columns in a columnset are resizable, the views are only
286 // made as wide as the widest views in each column, even if extra space is
287 // provided. In other words, GridLayout does not automatically resize views
288 // unless the column is marked as resizable.
289 // See class description for details on |resize_percent|.
290 void AddColumn(GridLayout::Alignment h_align
,
291 GridLayout::Alignment v_align
,
292 float resize_percent
,
293 GridLayout::SizeType size_type
,
297 // Forces the specified columns to have the same size. The size of
298 // linked columns is that of the max of the specified columns. This
299 // must end with -1. For example, the following forces the first and
300 // second column to have the same size:
301 // LinkColumnSizes(0, 1, -1);
302 void LinkColumnSizes(int first
, ...);
304 // ID of this ColumnSet.
305 int id() const { return id_
; }
307 int num_columns() const { return static_cast<int>(columns_
.size()); }
310 friend class GridLayout
;
312 explicit ColumnSet(int id
);
314 void AddColumn(GridLayout::Alignment h_align
,
315 GridLayout::Alignment v_align
,
316 float resize_percent
,
317 GridLayout::SizeType size_type
,
322 void AddViewState(ViewState
* view_state
);
324 // Set description of these.
325 void CalculateMasterColumns();
326 void AccumulateMasterColumns();
328 // Sets the size of each linked column to be the same.
329 void UnifySameSizedColumnSizes();
331 // Updates the remaining width field of the ViewState from that of the
332 // columns the view spans.
333 void UpdateRemainingWidth(ViewState
* view_state
);
335 // Makes sure the columns touched by view state are big enough for the
337 void DistributeRemainingWidth(ViewState
* view_state
);
339 // Returns the total size needed for this ColumnSet.
342 // Returns the width of the specified columns.
343 int GetColumnWidth(int start_col
, int col_span
);
345 // Updates the x coordinate of each column from the previous ones.
346 // NOTE: this doesn't include the insets.
347 void ResetColumnXCoordinates();
349 // Calculate the preferred width of each view in this column set, as well
350 // as updating the remaining_width.
351 void CalculateSize();
353 // Distributes delta amoung the resizable columns.
354 void Resize(int delta
);
356 // ID for this columnset.
360 std::vector
<Column
*> columns_
;
362 // The ViewStates. This is sorted based on column_span in ascending
364 std::vector
<ViewState
*> view_states_
;
366 // The master column of those columns that are linked. See Column
367 // for a description of what the master column is.
368 std::vector
<Column
*> master_columns_
;
370 DISALLOW_COPY_AND_ASSIGN(ColumnSet
);
375 #endif // UI_VIEWS_LAYOUT_GRID_LAYOUT_H_