summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/generic_info_view.cc
blob: 1cb44260a925be74229af5c504b9bad10703389a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/generic_info_view.h"

#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/color_utils.h"
#include "views/controls/label.h"
#include "views/controls/textfield/textfield.h"
#include "views/layout/grid_layout.h"
#include "views/layout/layout_constants.h"

GenericInfoView::GenericInfoView(int number_of_rows)
    : number_of_rows_(number_of_rows), name_string_ids_(NULL) {
  DCHECK(number_of_rows_ > 0);
}

GenericInfoView::GenericInfoView(
    int number_of_rows, const int name_string_ids[])
    : number_of_rows_(number_of_rows), name_string_ids_(name_string_ids) {
  DCHECK(number_of_rows_ > 0);
}

void GenericInfoView::SetNameByStringId(int row, int name_string_id) {
  SetName(row, UTF16ToWide(l10n_util::GetStringUTF16(name_string_id)));
}

void GenericInfoView::SetName(int row, const string16& name) {
  DCHECK(name_views_.get());  // Can only be called after Init time.
  DCHECK(row >= 0 && row < number_of_rows_);
  name_views_[row]->SetText(name);
}

void GenericInfoView::SetValue(int row, const string16& name) {
  DCHECK(value_views_.get());  // Can only be called after Init time.
  DCHECK(row >= 0 && row < number_of_rows_);
  value_views_[row]->SetText(name);
}

void GenericInfoView::ViewHierarchyChanged(bool is_add,
                                           views::View* parent,
                                           views::View* child) {
  if (is_add && child == this) {
    InitGenericInfoView();
    if (name_string_ids_) {
      for (int i = 0; i < number_of_rows_; ++i)
        SetNameByStringId(i, name_string_ids_[i]);
    }
  }
}

void GenericInfoView::InitGenericInfoView() {
  const int kInfoViewBorderSize = 1;
  const int kInfoViewInsetSize = 3;
  const int kLayoutId = 0;

  SkColor border_color = color_utils::GetSysSkColor(COLOR_3DSHADOW);
  views::Border* border = views::Border::CreateSolidBorder(
      kInfoViewBorderSize, border_color);
  set_border(border);

  using views::GridLayout;

  GridLayout* layout = new GridLayout(this);
  layout->SetInsets(kInfoViewInsetSize, kInfoViewInsetSize,
                    kInfoViewInsetSize, kInfoViewInsetSize);
  SetLayoutManager(layout);

  views::ColumnSet* column_set = layout->AddColumnSet(kLayoutId);
  column_set->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0,
                        GridLayout::USE_PREF, 0, 0);
  column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
  column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
                        GridLayout::USE_PREF, 0, 0);

  name_views_.reset(new views::Label* [number_of_rows_]);
  value_views_.reset(new views::Textfield* [number_of_rows_]);

  for (int i = 0; i < number_of_rows_; ++i) {
    if (i)
      layout->AddPaddingRow(0, kRelatedControlSmallVerticalSpacing);
    name_views_[i] = new views::Label;
    value_views_[i] = new views::Textfield;
    AddRow(kLayoutId, layout, name_views_[i], value_views_[i]);
  }
}

void GenericInfoView::AddRow(
    int layout_id, views::GridLayout* layout, views::Label* name,
    views::Textfield* value) {
  // Add to the view hierarchy.
  layout->StartRow(0, layout_id);
  layout->AddView(name);
  layout->AddView(value);

  // Color these borderless text areas the same as the containing dialog.
  SkColor text_area_background = color_utils::GetSysSkColor(COLOR_3DFACE);

  // Init them now that they're in the view hierarchy.
  value->SetReadOnly(true);
  value->RemoveBorder();
  value->SetBackgroundColor(text_area_background);
}