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.
4
5#ifndef UI_VIEWS_LAYOUT_GRID_LAYOUT_H_
6#define UI_VIEWS_LAYOUT_GRID_LAYOUT_H_
7
8#include <string>
9#include <vector>
10
11#include "base/compiler_specific.h"
12#include "ui/gfx/insets.h"
13#include "ui/views/layout/layout_manager.h"
14#include "ui/views/view.h"
15
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
24//                          // resized.
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);
32// Now add the views:
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);
39//
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
44// for the column:
45// layout->AddView(v1, 1, 1, LEADING, LEADING);
46//
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.
52//
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.
55//
56// GridLayout allows you to force columns to have the same width. This is
57// done using the LinkColumnSizes method.
58//
59// AddView takes care of adding the View to the View the GridLayout was
60// created with.
61namespace views {
62
63class Column;
64class ColumnSet;
65class Row;
66class View;
67
68struct ViewState;
69
70class VIEWS_EXPORT GridLayout : public LayoutManager {
71 public:
72  // An enumeration of the possible alignments supported by GridLayout.
73  enum Alignment {
74    // Leading equates to left along the horizontal axis, and top along the
75    // vertical axis.
76    LEADING,
77
78    // Centers the view along the axis.
79    CENTER,
80
81    // Trailing equals to right along the horizontal axis, and bottom along
82    // the vertical axis.
83    TRAILING,
84
85    // The view is resized to fill the space.
86    FILL,
87
88    // The view is aligned along the baseline. This is only valid for the
89    // vertical axis.
90    BASELINE
91  };
92
93  // An enumeration of the possible ways the size of a column may be obtained.
94  enum SizeType {
95    // The column size is fixed.
96    FIXED,
97
98    // The preferred size of the view is used to determine the column size.
99    USE_PREF
100  };
101
102  explicit GridLayout(View* host);
103  virtual ~GridLayout();
104
105  // Creates a GridLayout with kPanel*Margin insets.
106  static GridLayout* CreatePanel(View* host);
107
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);
111
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);
117
118  // Returns the column set for the specified id, or NULL if one doesn't exist.
119  ColumnSet* GetColumnSet(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) OVERRIDE;
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) OVERRIDE;
170
171  // Notification that a view has been added.
172  virtual void ViewAdded(View* host, View* view) OVERRIDE;
173
174  // Notification that a view has been removed.
175  virtual void ViewRemoved(View* host, View* view) OVERRIDE;
176
177  // Layouts out the components.
178  virtual void Layout(View* host) OVERRIDE;
179
180  // Returns the preferred size for the GridLayout.
181  virtual gfx::Size GetPreferredSize(const View* host) const OVERRIDE;
182
183  virtual int GetPreferredHeightForWidth(const View* host,
184                                         int width) const OVERRIDE;
185
186  void set_minimum_size(const gfx::Size& size) { minimum_size_ = size; }
187
188 private:
189  // As both Layout and GetPreferredSize need to do nearly the same thing,
190  // they both call into this method. This sizes the Columns/Rows as
191  // appropriate. If layout is true, width/height give the width/height the
192  // of the host, otherwise they are ignored.
193  void SizeRowsAndColumns(bool layout,
194                          int width,
195                          int height,
196                          gfx::Size* pref) const;
197
198  // Calculates the master columns of all the column sets. See Column for
199  // a description of what a master column is.
200  void CalculateMasterColumnsIfNecessary() const;
201
202  // This is called internally from AddView. It adds the ViewState to the
203  // appropriate structures, and updates internal fields such as next_column_.
204  void AddViewState(ViewState* view_state);
205
206  // Adds the Row to rows_, as well as updating next_column_,
207  // current_row_col_set ...
208  void AddRow(Row* row);
209
210  // As the name says, updates the remaining_height of the ViewState for
211  // all Rows the supplied ViewState touches.
212  void UpdateRemainingHeightFromRows(ViewState* state) const;
213
214  // If the view state's remaining height is > 0, it is distributed among
215  // the rows the view state touches. This is used during layout to make
216  // sure the Rows can accommodate a view.
217  void DistributeRemainingHeight(ViewState* state) const;
218
219  // Advances next_column_ past any padding columns.
220  void SkipPaddingColumns();
221
222  // Returns the column set of the last non-padding row.
223  ColumnSet* GetLastValidColumnSet();
224
225  // The view we were created with. We don't own this.
226  View* const host_;
227
228  // Whether or not we've calculated the master/linked columns.
229  mutable bool calculated_master_columns_;
230
231  // Used to verify a view isn't added with a row span that expands into
232  // another column structure.
233  int remaining_row_span_;
234
235  // Current row.
236  int current_row_;
237
238  // Current column.
239  int next_column_;
240
241  // Column set for the current row. This is null for padding rows.
242  ColumnSet* current_row_col_set_;
243
244  // Insets.
245  gfx::Insets insets_;
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  mutable std::vector<ViewState*> view_states_;
252
253  // ColumnSets.
254  mutable std::vector<ColumnSet*> column_sets_;
255
256  // Rows.
257  mutable std::vector<Row*> rows_;
258
259  // Minimum preferred size.
260  gfx::Size minimum_size_;
261
262  DISALLOW_COPY_AND_ASSIGN(GridLayout);
263};
264
265// ColumnSet is used to define a set of columns. GridLayout may have any
266// number of ColumnSets. You don't create a ColumnSet directly, instead
267// use the AddColumnSet method of GridLayout.
268class VIEWS_EXPORT ColumnSet {
269 public:
270  ~ColumnSet();
271
272  // Adds a column for padding. When adding views, padding columns are
273  // automatically skipped. For example, if you create a column set with
274  // two columns separated by a padding column, the second AddView automatically
275  // skips past the padding column. That is, to add two views, do:
276  // layout->AddView(v1); layout->AddView(v2);, not:
277  // layout->AddView(v1); layout->SkipColumns(1); layout->AddView(v2);
278  // See class description for details on |resize_percent|.
279  void AddPaddingColumn(float resize_percent, int width);
280
281  // Adds a column. The alignment gives the default alignment for views added
282  // with no explicit alignment. fixed_width gives a specific width for the
283  // column, and is only used if size_type == FIXED. min_width gives the
284  // minimum width for the column.
285  //
286  // If none of the columns in a columnset are resizable, the views are only
287  // made as wide as the widest views in each column, even if extra space is
288  // provided. In other words, GridLayout does not automatically resize views
289  // unless the column is marked as resizable.
290  // See class description for details on |resize_percent|.
291  void AddColumn(GridLayout::Alignment h_align,
292                 GridLayout::Alignment v_align,
293                 float resize_percent,
294                 GridLayout::SizeType size_type,
295                 int fixed_width,
296                 int min_width);
297
298  // Forces the specified columns to have the same size. The size of
299  // linked columns is that of the max of the specified columns. This
300  // must end with -1. For example, the following forces the first and
301  // second column to have the same size:
302  // LinkColumnSizes(0, 1, -1);
303  void LinkColumnSizes(int first, ...);
304
305  // ID of this ColumnSet.
306  int id() const { return id_; }
307
308  int num_columns() const { return static_cast<int>(columns_.size()); }
309
310 private:
311  friend class GridLayout;
312
313  explicit ColumnSet(int id);
314
315  void AddColumn(GridLayout::Alignment h_align,
316                 GridLayout::Alignment v_align,
317                 float resize_percent,
318                 GridLayout::SizeType size_type,
319                 int fixed_width,
320                 int min_width,
321                 bool is_padding);
322
323  void AddViewState(ViewState* view_state);
324
325  // Set description of these.
326  void CalculateMasterColumns();
327  void AccumulateMasterColumns();
328
329  // Sets the size of each linked column to be the same.
330  void UnifySameSizedColumnSizes();
331
332  // Updates the remaining width field of the ViewState from that of the
333  // columns the view spans.
334  void UpdateRemainingWidth(ViewState* view_state);
335
336  // Makes sure the columns touched by view state are big enough for the
337  // view.
338  void DistributeRemainingWidth(ViewState* view_state);
339
340  // Returns the total size needed for this ColumnSet.
341  int LayoutWidth();
342
343  // Returns the width of the specified columns.
344  int GetColumnWidth(int start_col, int col_span);
345
346  // Updates the x coordinate of each column from the previous ones.
347  // NOTE: this doesn't include the insets.
348  void ResetColumnXCoordinates();
349
350  // Calculate the preferred width of each view in this column set, as well
351  // as updating the remaining_width.
352  void CalculateSize();
353
354  // Distributes delta amoung the resizable columns.
355  void Resize(int delta);
356
357  // ID for this columnset.
358  const int id_;
359
360  // The columns.
361  std::vector<Column*> columns_;
362
363  // The ViewStates. This is sorted based on column_span in ascending
364  // order.
365  std::vector<ViewState*> view_states_;
366
367  // The master column of those columns that are linked. See Column
368  // for a description of what the master column is.
369  std::vector<Column*> master_columns_;
370
371  DISALLOW_COPY_AND_ASSIGN(ColumnSet);
372};
373
374}  // namespace views
375
376#endif  // UI_VIEWS_LAYOUT_GRID_LAYOUT_H_
377