1 // Copyright (c) 2011 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 #include "chrome/browser/ui/views/generic_info_view.h" 6 7 #include "base/logging.h" 8 #include "base/utf_string_conversions.h" 9 #include "ui/base/l10n/l10n_util.h" 10 #include "ui/gfx/color_utils.h" 11 #include "views/controls/label.h" 12 #include "views/controls/textfield/textfield.h" 13 #include "views/layout/grid_layout.h" 14 #include "views/layout/layout_constants.h" 15 16 GenericInfoView::GenericInfoView(int number_of_rows) 17 : number_of_rows_(number_of_rows), name_string_ids_(NULL) { 18 DCHECK(number_of_rows_ > 0); 19 } 20 21 GenericInfoView::GenericInfoView( 22 int number_of_rows, const int name_string_ids[]) 23 : number_of_rows_(number_of_rows), name_string_ids_(name_string_ids) { 24 DCHECK(number_of_rows_ > 0); 25 } 26 27 void GenericInfoView::SetNameByStringId(int row, int name_string_id) { 28 SetName(row, UTF16ToWide(l10n_util::GetStringUTF16(name_string_id))); 29 } 30 31 void GenericInfoView::SetName(int row, const string16& name) { 32 DCHECK(name_views_.get()); // Can only be called after Init time. 33 DCHECK(row >= 0 && row < number_of_rows_); 34 name_views_[row]->SetText(name); 35 } 36 37 void GenericInfoView::SetValue(int row, const string16& name) { 38 DCHECK(value_views_.get()); // Can only be called after Init time. 39 DCHECK(row >= 0 && row < number_of_rows_); 40 value_views_[row]->SetText(name); 41 } 42 43 void GenericInfoView::ViewHierarchyChanged(bool is_add, 44 views::View* parent, 45 views::View* child) { 46 if (is_add && child == this) { 47 InitGenericInfoView(); 48 if (name_string_ids_) { 49 for (int i = 0; i < number_of_rows_; ++i) 50 SetNameByStringId(i, name_string_ids_[i]); 51 } 52 } 53 } 54 55 void GenericInfoView::InitGenericInfoView() { 56 const int kInfoViewBorderSize = 1; 57 const int kInfoViewInsetSize = 3; 58 const int kLayoutId = 0; 59 60 SkColor border_color = color_utils::GetSysSkColor(COLOR_3DSHADOW); 61 views::Border* border = views::Border::CreateSolidBorder( 62 kInfoViewBorderSize, border_color); 63 set_border(border); 64 65 using views::GridLayout; 66 67 GridLayout* layout = new GridLayout(this); 68 layout->SetInsets(kInfoViewInsetSize, kInfoViewInsetSize, 69 kInfoViewInsetSize, kInfoViewInsetSize); 70 SetLayoutManager(layout); 71 72 views::ColumnSet* column_set = layout->AddColumnSet(kLayoutId); 73 column_set->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0, 74 GridLayout::USE_PREF, 0, 0); 75 column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); 76 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, 77 GridLayout::USE_PREF, 0, 0); 78 79 name_views_.reset(new views::Label* [number_of_rows_]); 80 value_views_.reset(new views::Textfield* [number_of_rows_]); 81 82 for (int i = 0; i < number_of_rows_; ++i) { 83 if (i) 84 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing); 85 name_views_[i] = new views::Label; 86 value_views_[i] = new views::Textfield; 87 AddRow(kLayoutId, layout, name_views_[i], value_views_[i]); 88 } 89 } 90 91 void GenericInfoView::AddRow( 92 int layout_id, views::GridLayout* layout, views::Label* name, 93 views::Textfield* value) { 94 // Add to the view hierarchy. 95 layout->StartRow(0, layout_id); 96 layout->AddView(name); 97 layout->AddView(value); 98 99 // Color these borderless text areas the same as the containing dialog. 100 SkColor text_area_background = color_utils::GetSysSkColor(COLOR_3DFACE); 101 102 // Init them now that they're in the view hierarchy. 103 value->SetReadOnly(true); 104 value->RemoveBorder(); 105 value->SetBackgroundColor(text_area_background); 106 } 107