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