1 // Copyright (c) 2011 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/gtk/infobars/before_translate_infobar_gtk.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/translate/translate_infobar_delegate.h"
9 #include "chrome/browser/ui/gtk/gtk_util.h"
10 #include "grit/generated_resources.h"
11 #include "ui/base/gtk/gtk_hig_constants.h"
12 #include "ui/base/gtk/gtk_signal_registrar.h"
13 #include "ui/base/l10n/l10n_util.h"
15 BeforeTranslateInfoBar::BeforeTranslateInfoBar(
16 scoped_ptr
<TranslateInfoBarDelegate
> delegate
)
17 : TranslateInfoBarBase(delegate
.Pass()) {
20 BeforeTranslateInfoBar::~BeforeTranslateInfoBar() {
23 void BeforeTranslateInfoBar::PlatformSpecificSetOwner() {
24 TranslateInfoBarBase::PlatformSpecificSetOwner();
26 GtkWidget
* new_hbox
= gtk_hbox_new(FALSE
, ui::kControlSpacing
);
27 gtk_util::CenterWidgetInHBox(hbox(), new_hbox
, false, 0);
30 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE
,
31 base::string16(), &offset
);
33 gtk_box_pack_start(GTK_BOX(new_hbox
),
34 CreateLabel(base::UTF16ToUTF8(text
.substr(0, offset
))),
36 size_t original_language_index
= GetDelegate()->original_language_index();
37 size_t target_language_index
= GetDelegate()->target_language_index();
38 bool exclude_the_other
= original_language_index
!= target_language_index
;
39 GtkWidget
* combobox
= CreateLanguageCombobox(
40 original_language_index
,
41 exclude_the_other
? target_language_index
:
42 TranslateInfoBarDelegate::kNoIndex
);
43 signals()->Connect(combobox
, "changed",
44 G_CALLBACK(&OnLanguageModifiedThunk
), this);
45 gtk_box_pack_start(GTK_BOX(new_hbox
), combobox
, FALSE
, FALSE
, 0);
46 gtk_box_pack_start(GTK_BOX(new_hbox
),
47 CreateLabel(base::UTF16ToUTF8(text
.substr(offset
))),
50 GtkWidget
* button
= gtk_button_new_with_label(
51 l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_ACCEPT
).c_str());
52 signals()->Connect(button
, "clicked",
53 G_CALLBACK(&OnAcceptPressedThunk
), this);
54 gtk_box_pack_start(GTK_BOX(new_hbox
), button
, FALSE
, FALSE
, 0);
56 button
= gtk_button_new_with_label(
57 l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_DENY
).c_str());
58 signals()->Connect(button
, "clicked",
59 G_CALLBACK(&OnDenyPressedThunk
), this);
60 gtk_box_pack_start(GTK_BOX(new_hbox
), button
, FALSE
, FALSE
, 0);
62 TranslateInfoBarDelegate
* delegate
= GetDelegate();
63 if (delegate
->ShouldShowNeverTranslateShortcut()) {
64 std::string label
= l10n_util::GetStringFUTF8(
65 IDS_TRANSLATE_INFOBAR_NEVER_TRANSLATE
,
66 delegate
->language_name_at(delegate
->original_language_index()));
67 button
= gtk_button_new_with_label(label
.c_str());
68 signals()->Connect(button
, "clicked",
69 G_CALLBACK(&OnNeverTranslatePressedThunk
), this);
70 gtk_box_pack_start(GTK_BOX(new_hbox
), button
, FALSE
, FALSE
, 0);
73 if (delegate
->ShouldShowAlwaysTranslateShortcut()) {
74 std::string label
= l10n_util::GetStringFUTF8(
75 IDS_TRANSLATE_INFOBAR_ALWAYS_TRANSLATE
,
76 delegate
->language_name_at(delegate
->original_language_index()));
77 button
= gtk_button_new_with_label(label
.c_str());
78 signals()->Connect(button
, "clicked",
79 G_CALLBACK(&OnAlwaysTranslatePressedThunk
), this);
80 gtk_box_pack_start(GTK_BOX(new_hbox
), button
, FALSE
, FALSE
, 0);
84 bool BeforeTranslateInfoBar::ShowOptionsMenuButton() const {
88 void BeforeTranslateInfoBar::OnLanguageModified(GtkWidget
* sender
) {
89 GetDelegate()->UpdateOriginalLanguageIndex(
90 GetLanguageComboboxActiveId(GTK_COMBO_BOX(sender
)));
93 void BeforeTranslateInfoBar::OnAcceptPressed(GtkWidget
* sender
) {
94 GetDelegate()->Translate();
97 void BeforeTranslateInfoBar::OnDenyPressed(GtkWidget
* sender
) {
98 GetDelegate()->TranslationDeclined();
102 void BeforeTranslateInfoBar::OnNeverTranslatePressed(GtkWidget
* sender
) {
103 GetDelegate()->NeverTranslatePageLanguage();
106 void BeforeTranslateInfoBar::OnAlwaysTranslatePressed(GtkWidget
* sender
) {
107 GetDelegate()->AlwaysTranslatePageLanguage();