[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / views / infobars / before_translate_infobar.cc
blob6704716cfd369a0b3ff444b613aebfc109b14428
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/infobars/before_translate_infobar.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/translate/options_menu_model.h"
9 #include "chrome/browser/translate/translate_infobar_delegate.h"
10 #include "chrome/browser/ui/views/infobars/translate_language_menu_model.h"
11 #include "grit/component_strings.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/views/controls/button/label_button.h"
14 #include "ui/views/controls/button/menu_button.h"
15 #include "ui/views/controls/label.h"
17 BeforeTranslateInfoBar::BeforeTranslateInfoBar(
18 scoped_ptr<TranslateInfoBarDelegate> delegate)
19 : TranslateInfoBarBase(delegate.Pass()),
20 label_1_(NULL),
21 label_2_(NULL),
22 language_menu_button_(NULL),
23 accept_button_(NULL),
24 deny_button_(NULL),
25 never_translate_button_(NULL),
26 always_translate_button_(NULL),
27 options_menu_button_(NULL) {
30 BeforeTranslateInfoBar::~BeforeTranslateInfoBar() {
33 void BeforeTranslateInfoBar::Layout() {
34 TranslateInfoBarBase::Layout();
36 int x = StartX();
37 Labels labels;
38 labels.push_back(label_1_);
39 labels.push_back(label_2_);
40 AssignWidths(&labels, std::max(0, EndX() - x - NonLabelWidth()));
42 label_1_->SetPosition(gfx::Point(x, OffsetY(label_1_)));
43 if (!label_1_->text().empty())
44 x = label_1_->bounds().right() + kButtonInLabelSpacing;
46 language_menu_button_->SetPosition(
47 gfx::Point(x, OffsetY(language_menu_button_)));
48 x = language_menu_button_->bounds().right();
50 label_2_->SetPosition(
51 gfx::Point(x + kButtonInLabelSpacing, OffsetY(label_2_)));
52 x = (label_2_->text().empty() ? x : label_2_->bounds().right()) +
53 kEndOfLabelSpacing;
55 accept_button_->SetPosition(gfx::Point(x, OffsetY(accept_button_)));
56 x = accept_button_->bounds().right() + kButtonButtonSpacing;
58 deny_button_->SetPosition(gfx::Point(x, OffsetY(deny_button_)));
59 x = deny_button_->bounds().right() + kButtonButtonSpacing;
61 if (never_translate_button_) {
62 never_translate_button_->SetPosition(
63 gfx::Point(x, OffsetY(never_translate_button_)));
64 x = never_translate_button_->bounds().right() + kButtonButtonSpacing;
67 if (always_translate_button_) {
68 always_translate_button_->SetPosition(
69 gfx::Point(x, OffsetY(always_translate_button_)));
72 options_menu_button_->SetPosition(gfx::Point(
73 EndX() - options_menu_button_->width(), OffsetY(options_menu_button_)));
76 void BeforeTranslateInfoBar::ViewHierarchyChanged(
77 const ViewHierarchyChangedDetails& details) {
78 if (!details.is_add || (details.child != this) || (label_1_ != NULL)) {
79 TranslateInfoBarBase::ViewHierarchyChanged(details);
80 return;
83 size_t offset = 0;
84 base::string16 text(
85 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE,
86 base::string16(), &offset));
88 label_1_ = CreateLabel(text.substr(0, offset));
89 AddChildView(label_1_);
91 language_menu_button_ = CreateMenuButton(base::string16(), this);
92 TranslateInfoBarDelegate* delegate = GetDelegate();
93 language_menu_model_.reset(new TranslateLanguageMenuModel(
94 TranslateLanguageMenuModel::ORIGINAL, delegate, this,
95 language_menu_button_, false));
96 AddChildView(language_menu_button_);
98 label_2_ = CreateLabel(text.substr(offset));
99 AddChildView(label_2_);
101 accept_button_ = CreateLabelButton(
102 this, l10n_util::GetStringUTF16(IDS_TRANSLATE_INFOBAR_ACCEPT));
103 AddChildView(accept_button_);
105 deny_button_ = CreateLabelButton(
106 this, l10n_util::GetStringUTF16(IDS_TRANSLATE_INFOBAR_DENY));
107 AddChildView(deny_button_);
109 const base::string16& language(
110 delegate->language_name_at(delegate->original_language_index()));
111 if (delegate->ShouldShowNeverTranslateShortcut()) {
112 DCHECK(!delegate->ShouldShowAlwaysTranslateShortcut());
113 never_translate_button_ = CreateLabelButton(
114 this,
115 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_NEVER_TRANSLATE,
116 language));
117 AddChildView(never_translate_button_);
118 } else if (delegate->ShouldShowAlwaysTranslateShortcut()) {
119 always_translate_button_ = CreateLabelButton(
120 this,
121 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_ALWAYS_TRANSLATE,
122 language));
123 AddChildView(always_translate_button_);
126 options_menu_button_ = CreateMenuButton(
127 l10n_util::GetStringUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS), this);
128 options_menu_model_.reset(new OptionsMenuModel(delegate));
129 AddChildView(options_menu_button_);
131 // This must happen after adding all other children so InfoBarView can ensure
132 // the close button is the last child.
133 TranslateInfoBarBase::ViewHierarchyChanged(details);
135 // This must happen after adding all children because it triggers layout,
136 // which assumes that particular children (e.g. the close button) have already
137 // been added.
138 UpdateLanguageButtonText(language_menu_button_,
139 delegate->language_name_at(delegate->original_language_index()));
142 int BeforeTranslateInfoBar::ContentMinimumWidth() const {
143 return label_1_->GetMinimumSize().width() +
144 label_2_->GetMinimumSize().width() + NonLabelWidth();
147 void BeforeTranslateInfoBar::ButtonPressed(views::Button* sender,
148 const ui::Event& event) {
149 if (!owner())
150 return; // We're closing; don't call anything, it might access the owner.
151 TranslateInfoBarDelegate* delegate = GetDelegate();
152 if (sender == accept_button_) {
153 delegate->Translate();
154 } else if (sender == deny_button_) {
155 delegate->TranslationDeclined();
156 RemoveSelf();
157 } else if (sender == never_translate_button_) {
158 delegate->NeverTranslatePageLanguage();
159 } else if (sender == always_translate_button_) {
160 delegate->AlwaysTranslatePageLanguage();
161 } else {
162 TranslateInfoBarBase::ButtonPressed(sender, event);
166 void BeforeTranslateInfoBar::OnMenuButtonClicked(views::View* source,
167 const gfx::Point& point) {
168 if (!owner())
169 return; // We're closing; don't call anything, it might access the owner.
170 if (source == language_menu_button_) {
171 RunMenuAt(language_menu_model_.get(),
172 language_menu_button_,
173 views::MENU_ANCHOR_TOPLEFT);
174 } else {
175 DCHECK_EQ(options_menu_button_, source);
176 RunMenuAt(options_menu_model_.get(),
177 options_menu_button_,
178 views::MENU_ANCHOR_TOPRIGHT);
182 int BeforeTranslateInfoBar::NonLabelWidth() const {
183 return (label_1_->text().empty() ? 0 : kButtonInLabelSpacing) +
184 language_menu_button_->width() +
185 (label_2_->text().empty() ? 0 : kButtonInLabelSpacing) +
186 kEndOfLabelSpacing + accept_button_->width() + kButtonButtonSpacing +
187 deny_button_->width() +
188 (never_translate_button_ ?
189 (kButtonButtonSpacing + never_translate_button_->width()) : 0) +
190 (always_translate_button_ ?
191 (kButtonButtonSpacing + always_translate_button_->width()) : 0) +
192 kEndOfLabelSpacing + options_menu_button_->width();