1 // Copyright (c) 2013 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 CHROME_BROWSER_UI_COCOA_AUTOFILL_SIMPLE_GRID_LAYOUT_H_
6 #define CHROME_BROWSER_UI_COCOA_AUTOFILL_SIMPLE_GRID_LAYOUT_H_
8 #import <Cocoa/Cocoa.h>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
18 // SimpleGridLayout is a layout manager that positions child views in a grid.
19 // Each row has exactly one ColumnSet, ColumnSets can be shared between rows.
20 // See ui/views/layout/grid_layout.h for more details - this is a very
21 // simplified version of the views class.
22 // This is based on GridLayout in ui/views/layout/grid_layout.h.
23 // TODO(groby): Unify both classes, create cross-platform abstraction.
24 // http://crbug.com/240461
25 class SimpleGridLayout
{
27 SimpleGridLayout(NSView
* host
);
30 // Creates a new column set with the specified id and returns it.
31 // The id is later used when starting a new row.
32 // Layout takes ownership of the ColumnSet and will delete it when
34 ColumnSet
* AddColumnSet(int id
);
36 // Returns the column set for the specified id, or NULL if one doesn't exist.
37 ColumnSet
* GetColumnSet(int id
);
39 // Adds a padding row. Padding rows typically don't have any views, but are
40 // used to provide vertical white space between views.
41 // |size| specifies the height of the row.
42 void AddPaddingRow(int size
);
44 // Starts a new row with the specified column set.
45 void StartRow(float vertical_resize
, int column_set_id
);
47 // This is a convenience function that starts a new row,
48 // and returns a new ColumnSet associated with it. All rows created by this
49 // will have a height of 0 and resize_percent set to 1.0.
52 // Advances past columns. Use this when the current column should not
54 void SkipColumns(int col_count
);
56 // TODO(groby): This currently *must* be called after a StartRow for the row
57 // the view is in. At some point, I'd like an AddView that just populates
58 // the next available slot, if possible.
59 void AddView(NSView
* view
);
61 // Layout all contained views according to constraints.
62 void Layout(NSView
* superView
);
64 void SizeRowsAndColumns(float width
);
66 // Advances next_column_ past any padding columns.
67 void SkipPaddingColumns();
69 // Returns the column set of the last non-padding row.
70 ColumnSet
* GetLastValidColumnSet();
72 // Get the height of a given row.
73 float GetRowHeight(int row
);
75 // Y-position for a given row.
76 float GetRowLocation(int row_index
) const;
78 // Get the preferred height for the given width.
79 float GetPreferredHeightForWidth(float with
);
81 int num_rows() const { return static_cast<int>(rows_
.size()); }
83 // These functions are mostly for testing & deviate from Views Layout class.
84 int next_column() { return next_column_
; }
85 void AdvanceColumn() { next_column_
++; }
88 // Adds a new row, updating associated counters and positions.
89 void AddRow(Row
* row
);
91 // Next column in the current ColumnSet.
94 int current_auto_id_
; // Starting value for autogenerated columnset ids.
95 ScopedVector
<ViewState
> view_states_
;
96 ScopedVector
<ColumnSet
> column_sets_
;
97 ScopedVector
<Row
> rows_
;
102 // ColumnSet is used to define a set of columns.
103 // You don't create a ColumnSet directly, instead use the AddRow method
104 // of SimpleGridLayout.
107 explicit ColumnSet(int id
);
110 void AddPaddingColumn(int fixed_width
);
111 void AddColumn(float resize_percent
);
113 void CalculateSize(float width
);
114 void ResetColumnXCoordinates();
116 // ID of this ColumnSet.
117 int id() const { return id_
; }
119 int num_columns() const { return static_cast<int>(columns_
.size()); }
121 // Returns the width of the specified columns.
122 float GetColumnWidth(int column
);
124 Column
* GetColumn(int column_index
) {
125 DCHECK(column_index
>=0 && column_index
< num_columns());
126 return columns_
[column_index
];
129 // These functions are mostly for testing & deviate from Views Layout class.
130 float ColumnLocation(int column_index
);
133 float CalculateRemainingWidth(float width
);
134 void DistributeRemainingWidth(float width
);
136 ScopedVector
<Column
> columns_
;
140 #endif // CHROME_BROWSER_UI_COCOA_AUTOFILL_SIMPLE_GRID_LAYOUT_H_