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/generated_resources.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"
16 #include "ui/views/controls/menu/menu_item_view.h"
18 BeforeTranslateInfoBar::BeforeTranslateInfoBar(
19 scoped_ptr
<TranslateInfoBarDelegate
> delegate
)
20 : TranslateInfoBarBase(delegate
.Pass()),
23 language_menu_button_(NULL
),
26 never_translate_button_(NULL
),
27 always_translate_button_(NULL
),
28 options_menu_button_(NULL
) {
31 BeforeTranslateInfoBar::~BeforeTranslateInfoBar() {
34 void BeforeTranslateInfoBar::Layout() {
35 TranslateInfoBarBase::Layout();
39 labels
.push_back(label_1_
);
40 labels
.push_back(label_2_
);
41 AssignWidths(&labels
, std::max(0, EndX() - x
- NonLabelWidth()));
43 label_1_
->SetPosition(gfx::Point(x
, OffsetY(label_1_
)));
44 if (!label_1_
->text().empty())
45 x
= label_1_
->bounds().right() + kButtonInLabelSpacing
;
47 language_menu_button_
->SetPosition(
48 gfx::Point(x
, OffsetY(language_menu_button_
)));
49 x
= language_menu_button_
->bounds().right();
51 label_2_
->SetPosition(
52 gfx::Point(x
+ kButtonInLabelSpacing
, OffsetY(label_2_
)));
53 x
= (label_2_
->text().empty() ? x
: label_2_
->bounds().right()) +
56 accept_button_
->SetPosition(gfx::Point(x
, OffsetY(accept_button_
)));
57 x
= accept_button_
->bounds().right() + kButtonButtonSpacing
;
59 deny_button_
->SetPosition(gfx::Point(x
, OffsetY(deny_button_
)));
60 x
= deny_button_
->bounds().right() + kButtonButtonSpacing
;
62 if (never_translate_button_
) {
63 never_translate_button_
->SetPosition(
64 gfx::Point(x
, OffsetY(never_translate_button_
)));
65 x
= never_translate_button_
->bounds().right() + kButtonButtonSpacing
;
68 if (always_translate_button_
) {
69 always_translate_button_
->SetPosition(
70 gfx::Point(x
, OffsetY(always_translate_button_
)));
73 options_menu_button_
->SetPosition(gfx::Point(
74 EndX() - options_menu_button_
->width(), OffsetY(options_menu_button_
)));
77 void BeforeTranslateInfoBar::ViewHierarchyChanged(
78 const ViewHierarchyChangedDetails
& details
) {
79 if (!details
.is_add
|| (details
.child
!= this) || (label_1_
!= NULL
)) {
80 TranslateInfoBarBase::ViewHierarchyChanged(details
);
86 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE
,
87 base::string16(), &offset
));
89 label_1_
= CreateLabel(text
.substr(0, offset
));
90 AddChildView(label_1_
);
92 language_menu_button_
= CreateMenuButton(base::string16(), this);
93 TranslateInfoBarDelegate
* delegate
= GetDelegate();
94 language_menu_model_
.reset(new TranslateLanguageMenuModel(
95 TranslateLanguageMenuModel::ORIGINAL
, delegate
, this,
96 language_menu_button_
, false));
97 AddChildView(language_menu_button_
);
99 label_2_
= CreateLabel(text
.substr(offset
));
100 AddChildView(label_2_
);
102 accept_button_
= CreateLabelButton(this,
103 l10n_util::GetStringUTF16(IDS_TRANSLATE_INFOBAR_ACCEPT
), false);
104 AddChildView(accept_button_
);
106 deny_button_
= CreateLabelButton(this,
107 l10n_util::GetStringUTF16(IDS_TRANSLATE_INFOBAR_DENY
), false);
108 AddChildView(deny_button_
);
110 const base::string16
& language(
111 delegate
->language_name_at(delegate
->original_language_index()));
112 if (delegate
->ShouldShowNeverTranslateShortcut()) {
113 DCHECK(!delegate
->ShouldShowAlwaysTranslateShortcut());
114 never_translate_button_
= CreateLabelButton(this,
115 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_NEVER_TRANSLATE
,
117 AddChildView(never_translate_button_
);
118 } else if (delegate
->ShouldShowAlwaysTranslateShortcut()) {
119 always_translate_button_
= CreateLabelButton(this,
120 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_ALWAYS_TRANSLATE
,
122 AddChildView(always_translate_button_
);
125 options_menu_button_
= CreateMenuButton(
126 l10n_util::GetStringUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS
), this);
127 options_menu_model_
.reset(new OptionsMenuModel(delegate
));
128 AddChildView(options_menu_button_
);
130 // This must happen after adding all other children so InfoBarView can ensure
131 // the close button is the last child.
132 TranslateInfoBarBase::ViewHierarchyChanged(details
);
134 // This must happen after adding all children because it triggers layout,
135 // which assumes that particular children (e.g. the close button) have already
137 UpdateLanguageButtonText(language_menu_button_
,
138 delegate
->language_name_at(delegate
->original_language_index()));
141 int BeforeTranslateInfoBar::ContentMinimumWidth() {
142 return label_1_
->GetMinimumSize().width() +
143 label_2_
->GetMinimumSize().width() + NonLabelWidth();
146 void BeforeTranslateInfoBar::ButtonPressed(views::Button
* sender
,
147 const ui::Event
& event
) {
149 return; // We're closing; don't call anything, it might access the owner.
150 TranslateInfoBarDelegate
* delegate
= GetDelegate();
151 if (sender
== accept_button_
) {
152 delegate
->Translate();
153 } else if (sender
== deny_button_
) {
154 delegate
->TranslationDeclined();
156 } else if (sender
== never_translate_button_
) {
157 delegate
->NeverTranslatePageLanguage();
158 } else if (sender
== always_translate_button_
) {
159 delegate
->AlwaysTranslatePageLanguage();
161 TranslateInfoBarBase::ButtonPressed(sender
, event
);
165 void BeforeTranslateInfoBar::OnMenuButtonClicked(views::View
* source
,
166 const gfx::Point
& point
) {
168 return; // We're closing; don't call anything, it might access the owner.
169 if (source
== language_menu_button_
) {
170 RunMenuAt(language_menu_model_
.get(), language_menu_button_
,
171 views::MenuItemView::TOPLEFT
);
173 DCHECK_EQ(options_menu_button_
, source
);
174 RunMenuAt(options_menu_model_
.get(), options_menu_button_
,
175 views::MenuItemView::TOPRIGHT
);
179 int BeforeTranslateInfoBar::NonLabelWidth() const {
180 return (label_1_
->text().empty() ? 0 : kButtonInLabelSpacing
) +
181 language_menu_button_
->width() +
182 (label_2_
->text().empty() ? 0 : kButtonInLabelSpacing
) +
183 kEndOfLabelSpacing
+ accept_button_
->width() + kButtonButtonSpacing
+
184 deny_button_
->width() +
185 (never_translate_button_
?
186 (kButtonButtonSpacing
+ never_translate_button_
->width()) : 0) +
187 (always_translate_button_
?
188 (kButtonButtonSpacing
+ always_translate_button_
->width()) : 0) +
189 kEndOfLabelSpacing
+ options_menu_button_
->width();