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. 4 5 #ifndef CHROME_BROWSER_UI_COCOA_AUTOFILL_SIMPLE_GRID_LAYOUT_H_ 6 #define CHROME_BROWSER_UI_COCOA_AUTOFILL_SIMPLE_GRID_LAYOUT_H_ 7 8 #import <Cocoa/Cocoa.h> 9 10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_vector.h" 12 13 class Column; 14 class ColumnSet; 15 class Row; 16 class ViewState; 17 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 { 26 public: 27 SimpleGridLayout(NSView* host); 28 ~SimpleGridLayout(); 29 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 33 // it is deleted. 34 ColumnSet* AddColumnSet(int id); 35 36 // Returns the column set for the specified id, or NULL if one doesn't exist. 37 ColumnSet* GetColumnSet(int id); 38 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); 43 44 // Starts a new row with the specified column set. 45 void StartRow(float vertical_resize, int column_set_id); 46 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. 50 ColumnSet* AddRow(); 51 52 // Advances past columns. Use this when the current column should not 53 // contain any views. 54 void SkipColumns(int col_count); 55 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); 60 61 // Layout all contained views according to constraints. 62 void Layout(NSView* superView); 63 64 void SizeRowsAndColumns(float width); 65 66 // Advances next_column_ past any padding columns. 67 void SkipPaddingColumns(); 68 69 // Returns the column set of the last non-padding row. 70 ColumnSet* GetLastValidColumnSet(); 71 72 // Get the height of a given row. 73 float GetRowHeight(int row); 74 75 // Y-position for a given row. 76 float GetRowLocation(int row_index) const; 77 78 // Get the preferred height for the given width. 79 float GetPreferredHeightForWidth(float with); 80 81 int num_rows() const { return static_cast<int>(rows_.size()); } 82 83 // These functions are mostly for testing & deviate from Views Layout class. 84 int next_column() { return next_column_; } 85 void AdvanceColumn() { next_column_++; } 86 87 private: 88 // Adds a new row, updating associated counters and positions. 89 void AddRow(Row* row); 90 91 // Next column in the current ColumnSet. 92 int next_column_; 93 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_; 98 99 NSView* host_; 100 }; 101 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. 105 class ColumnSet { 106 public: 107 explicit ColumnSet(int id); 108 ~ColumnSet(); 109 110 void AddPaddingColumn(int fixed_width); 111 void AddColumn(float resize_percent); 112 113 void CalculateSize(float width); 114 void ResetColumnXCoordinates(); 115 116 // ID of this ColumnSet. 117 int id() const { return id_; } 118 119 int num_columns() const { return static_cast<int>(columns_.size()); } 120 121 // Returns the width of the specified columns. 122 float GetColumnWidth(int column); 123 124 Column* GetColumn(int column_index) { 125 DCHECK(column_index >=0 && column_index < num_columns()); 126 return columns_[column_index]; 127 } 128 129 // These functions are mostly for testing & deviate from Views Layout class. 130 float ColumnLocation(int column_index); 131 132 private: 133 float CalculateRemainingWidth(float width); 134 void DistributeRemainingWidth(float width); 135 136 ScopedVector<Column> columns_; 137 int id_; 138 }; 139 140 #endif // CHROME_BROWSER_UI_COCOA_AUTOFILL_SIMPLE_GRID_LAYOUT_H_ 141