summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/options/options_group_view.cc
blob: 80eb0bb584734861c3a5659d643679d8bc2a2ce4 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <vsstyle.h>
#include <vssym32.h>

#include "chrome/browser/views/options/options_group_view.h"

#include "base/gfx/native_theme.h"
#include "base/gfx/skia_utils.h"
#include "chrome/app/locales/locale_settings.h"
#include "chrome/browser/standard_layout.h"
#include "chrome/common/gfx/chrome_font.h"
#include "chrome/common/gfx/chrome_canvas.h"
#include "chrome/common/l10n_util.h"
#include "chrome/common/resource_bundle.h"
#include "chrome/views/grid_layout.h"
#include "chrome/views/label.h"
#include "chrome/views/separator.h"
#include "generated_resources.h"

static const int kLeftColumnWidthChars = 20;
static const int kOptionsGroupViewColumnSpacing = 30;

///////////////////////////////////////////////////////////////////////////////
// OptionsGroupView, public:

OptionsGroupView::OptionsGroupView(ChromeViews::View* contents,
                                   const std::wstring& title,
                                   const std::wstring& description,
                                   bool show_separator)
    : contents_(contents),
      title_label_(new ChromeViews::Label(title)),
      description_label_(new ChromeViews::Label(description)),
      separator_(NULL),
      show_separator_(show_separator),
      highlighted_(false) {
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  ChromeFont title_font =
      rb.GetFont(ResourceBundle::BaseFont).DeriveFont(0, ChromeFont::BOLD);
  title_label_->SetFont(title_font);
  SkColor title_color = gfx::NativeTheme::instance()->GetThemeColorWithDefault(
      gfx::NativeTheme::BUTTON, BP_GROUPBOX, GBS_NORMAL, TMT_TEXTCOLOR, COLOR_WINDOWTEXT);
  title_label_->SetColor(title_color);
  title_label_->SetMultiLine(true);
  title_label_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);

  description_label_->SetMultiLine(true);
  description_label_->SetHorizontalAlignment(ChromeViews::Label::ALIGN_LEFT);
}

void OptionsGroupView::SetHighlighted(bool highlighted) {
  highlighted_ = highlighted;
  SchedulePaint();
}

int OptionsGroupView::GetContentsWidth() const {
  return contents_->GetWidth();
}

///////////////////////////////////////////////////////////////////////////////
// OptionsGroupView, ChromeViews::View overrides:

void OptionsGroupView::Paint(ChromeCanvas* canvas) {
  if (highlighted_) {
    COLORREF infocolor = GetSysColor(COLOR_INFOBK);
    SkColor background_color = SkColorSetRGB(GetRValue(infocolor),
                                             GetGValue(infocolor),
                                             GetBValue(infocolor));
    int y_offset = kUnrelatedControlVerticalSpacing / 2;
    canvas->FillRectInt(background_color, 0, 0, GetWidth(),
                        GetHeight() - y_offset);
  }
}

void OptionsGroupView::ViewHierarchyChanged(bool is_add,
                                            ChromeViews::View* parent,
                                            ChromeViews::View* child) {
  if (is_add && child == this)
    Init();
}

///////////////////////////////////////////////////////////////////////////////
// OptionsGroupView, private:

void OptionsGroupView::Init() {
  using ChromeViews::GridLayout;
  using ChromeViews::ColumnSet;

  GridLayout* layout = new GridLayout(this);
  SetLayoutManager(layout);

  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  ChromeFont font = rb.GetFont(ResourceBundle::BaseFont);
  std::wstring left_column_chars =
      l10n_util::GetString(IDS_OPTIONS_DIALOG_LEFT_COLUMN_WIDTH_CHARS);
  int left_column_width =
      font.ave_char_width() * _wtoi(left_column_chars.c_str());

  const int two_column_layout_id = 0;
  ColumnSet* column_set = layout->AddColumnSet(two_column_layout_id);
  column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
  column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 0,
                        GridLayout::FIXED, left_column_width, 0);
  column_set->AddPaddingColumn(0, kOptionsGroupViewColumnSpacing);
  column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 1,
                        GridLayout::USE_PREF, 0, 0);
  column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);

  layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
  layout->StartRow(0, two_column_layout_id);
  // We need to do this so that the label can calculate an appropriate
  // preferred size (width * height) based on this width constraint. Otherwise
  // Label::GetPreferredSize will return 0,0 as the preferred size.
  title_label_->SetBounds(0, 0, left_column_width, 0);
  layout->AddView(title_label_);
  layout->AddView(contents_, 1, 3, GridLayout::FILL, GridLayout::FILL);
  layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
  layout->StartRow(1, two_column_layout_id);
  // See comment above for title_label_.
  description_label_->SetBounds(0, 0, left_column_width, 0);
  layout->AddView(description_label_);
  layout->AddPaddingRow(0, kUnrelatedControlVerticalSpacing);

  if (show_separator_) {
    const int single_column_layout_id = 1;
    column_set = layout->AddColumnSet(single_column_layout_id);
    column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
                          GridLayout::USE_PREF, 0, 0);

    separator_ = new ChromeViews::Separator;
    layout->StartRow(0, single_column_layout_id);
    layout->AddView(separator_);
  }
}