OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/layout/grid_layout.h" | 5 #include "ui/views/layout/grid_layout.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 810 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
821 if (view_state->v_align == BASELINE && view_state->baseline != -1) { | 821 if (view_state->v_align == BASELINE && view_state->baseline != -1) { |
822 y += rows_[view_state->start_row]->max_ascent() - view_state->baseline; | 822 y += rows_[view_state->start_row]->max_ascent() - view_state->baseline; |
823 height = view_state->pref_height; | 823 height = view_state->pref_height; |
824 } else { | 824 } else { |
825 CalculateSize(view_state->pref_height, view_state->v_align, &y, &height); | 825 CalculateSize(view_state->pref_height, view_state->v_align, &y, &height); |
826 } | 826 } |
827 view->SetBounds(x, y, width, height); | 827 view->SetBounds(x, y, width, height); |
828 } | 828 } |
829 } | 829 } |
830 | 830 |
831 gfx::Size GridLayout::GetPreferredSize(View* host) { | 831 gfx::Size GridLayout::GetPreferredSize(const View* host) const { |
832 DCHECK(host_ == host); | 832 DCHECK(host_ == host); |
833 gfx::Size out; | 833 gfx::Size out; |
834 SizeRowsAndColumns(false, 0, 0, &out); | 834 SizeRowsAndColumns(false, 0, 0, &out); |
835 out.SetSize(std::max(out.width(), minimum_size_.width()), | 835 out.SetSize(std::max(out.width(), minimum_size_.width()), |
836 std::max(out.height(), minimum_size_.height())); | 836 std::max(out.height(), minimum_size_.height())); |
837 return out; | 837 return out; |
838 } | 838 } |
839 | 839 |
840 int GridLayout::GetPreferredHeightForWidth(View* host, int width) { | 840 int GridLayout::GetPreferredHeightForWidth(const View* host, int width) const { |
841 DCHECK(host_ == host); | 841 DCHECK(host_ == host); |
842 gfx::Size pref; | 842 gfx::Size pref; |
843 SizeRowsAndColumns(false, width, 0, &pref); | 843 SizeRowsAndColumns(false, width, 0, &pref); |
844 return pref.height(); | 844 return pref.height(); |
845 } | 845 } |
846 | 846 |
847 void GridLayout::SizeRowsAndColumns(bool layout, int width, int height, | 847 void GridLayout::SizeRowsAndColumns(bool layout, int width, int height, |
848 gfx::Size* pref) { | 848 gfx::Size* pref) const { |
849 // Make sure the master columns have been calculated. | 849 // Make sure the master columns have been calculated. |
850 CalculateMasterColumnsIfNecessary(); | 850 CalculateMasterColumnsIfNecessary(); |
851 pref->SetSize(0, 0); | 851 pref->SetSize(0, 0); |
852 if (rows_.empty()) | 852 if (rows_.empty()) |
853 return; | 853 return; |
854 | 854 |
855 // Calculate the preferred width of each of the columns. Some views' | 855 // Calculate the preferred width of each of the columns. Some views' |
856 // preferred heights are derived from their width, as such we need to | 856 // preferred heights are derived from their width, as such we need to |
857 // calculate the size of the columns first. | 857 // calculate the size of the columns first. |
858 for (std::vector<ColumnSet*>::iterator i = column_sets_.begin(); | 858 for (std::vector<ColumnSet*>::iterator i = column_sets_.begin(); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
943 if (layout && height != pref->height()) { | 943 if (layout && height != pref->height()) { |
944 // We're doing a layout, and the height differs from the preferred height, | 944 // We're doing a layout, and the height differs from the preferred height, |
945 // divy up the extra space. | 945 // divy up the extra space. |
946 LayoutElement::DistributeDelta(height - pref->height(), &rows_); | 946 LayoutElement::DistributeDelta(height - pref->height(), &rows_); |
947 | 947 |
948 // Reset y locations. | 948 // Reset y locations. |
949 LayoutElement::CalculateLocationsFromSize(&rows_); | 949 LayoutElement::CalculateLocationsFromSize(&rows_); |
950 } | 950 } |
951 } | 951 } |
952 | 952 |
953 void GridLayout::CalculateMasterColumnsIfNecessary() { | 953 void GridLayout::CalculateMasterColumnsIfNecessary() const { |
954 if (!calculated_master_columns_) { | 954 if (!calculated_master_columns_) { |
955 calculated_master_columns_ = true; | 955 calculated_master_columns_ = true; |
956 for (std::vector<ColumnSet*>::iterator i = column_sets_.begin(); | 956 for (std::vector<ColumnSet*>::iterator i = column_sets_.begin(); |
957 i != column_sets_.end(); ++i) { | 957 i != column_sets_.end(); ++i) { |
958 (*i)->CalculateMasterColumns(); | 958 (*i)->CalculateMasterColumns(); |
959 } | 959 } |
960 } | 960 } |
961 } | 961 } |
962 | 962 |
963 void GridLayout::AddViewState(ViewState* view_state) { | 963 void GridLayout::AddViewState(ViewState* view_state) { |
(...skipping 24 matching lines...) Expand all Loading... |
988 // that. | 988 // that. |
989 DCHECK(remaining_row_span_ <= 0 || | 989 DCHECK(remaining_row_span_ <= 0 || |
990 row->column_set() == NULL || | 990 row->column_set() == NULL || |
991 row->column_set() == GetLastValidColumnSet()); | 991 row->column_set() == GetLastValidColumnSet()); |
992 next_column_ = 0; | 992 next_column_ = 0; |
993 rows_.push_back(row); | 993 rows_.push_back(row); |
994 current_row_col_set_ = row->column_set(); | 994 current_row_col_set_ = row->column_set(); |
995 SkipPaddingColumns(); | 995 SkipPaddingColumns(); |
996 } | 996 } |
997 | 997 |
998 void GridLayout::UpdateRemainingHeightFromRows(ViewState* view_state) { | 998 void GridLayout::UpdateRemainingHeightFromRows(ViewState* view_state) const { |
999 for (int i = 0, start_row = view_state->start_row; | 999 for (int i = 0, start_row = view_state->start_row; |
1000 i < view_state->row_span; ++i) { | 1000 i < view_state->row_span; ++i) { |
1001 view_state->remaining_height -= rows_[i + start_row]->Size(); | 1001 view_state->remaining_height -= rows_[i + start_row]->Size(); |
1002 } | 1002 } |
1003 } | 1003 } |
1004 | 1004 |
1005 void GridLayout::DistributeRemainingHeight(ViewState* view_state) { | 1005 void GridLayout::DistributeRemainingHeight(ViewState* view_state) const { |
1006 int height = view_state->remaining_height; | 1006 int height = view_state->remaining_height; |
1007 if (height <= 0) | 1007 if (height <= 0) |
1008 return; | 1008 return; |
1009 | 1009 |
1010 // Determine the number of resizable rows the view touches. | 1010 // Determine the number of resizable rows the view touches. |
1011 int resizable_rows = 0; | 1011 int resizable_rows = 0; |
1012 int start_row = view_state->start_row; | 1012 int start_row = view_state->start_row; |
1013 int max_row = view_state->start_row + view_state->row_span; | 1013 int max_row = view_state->start_row + view_state->row_span; |
1014 for (int i = start_row; i < max_row; ++i) { | 1014 for (int i = start_row; i < max_row; ++i) { |
1015 if (rows_[i]->IsResizable()) { | 1015 if (rows_[i]->IsResizable()) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1055 | 1055 |
1056 ColumnSet* GridLayout::GetLastValidColumnSet() { | 1056 ColumnSet* GridLayout::GetLastValidColumnSet() { |
1057 for (int i = current_row_ - 1; i >= 0; --i) { | 1057 for (int i = current_row_ - 1; i >= 0; --i) { |
1058 if (rows_[i]->column_set()) | 1058 if (rows_[i]->column_set()) |
1059 return rows_[i]->column_set(); | 1059 return rows_[i]->column_set(); |
1060 } | 1060 } |
1061 return NULL; | 1061 return NULL; |
1062 } | 1062 } |
1063 | 1063 |
1064 } // namespace views | 1064 } // namespace views |
OLD | NEW |