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