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/after_translate_infobar_gtk.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/translate/translate_infobar_delegate.h"
11 #include "chrome/browser/ui/gtk/gtk_util.h"
12 #include "grit/generated_resources.h"
13 #include "ui/base/gtk/gtk_hig_constants.h"
14 #include "ui/base/gtk/gtk_signal_registrar.h"
15 #include "ui/base/l10n/l10n_util.h"
17 AfterTranslateInfoBar::AfterTranslateInfoBar(
18 scoped_ptr
<TranslateInfoBarDelegate
> delegate
)
19 : TranslateInfoBarBase(delegate
.Pass()),
23 AfterTranslateInfoBar::~AfterTranslateInfoBar() {
26 void AfterTranslateInfoBar::PlatformSpecificSetOwner() {
27 TranslateInfoBarBase::PlatformSpecificSetOwner();
29 bool swapped_language_combos
= false;
30 bool autodetermined_source_language
=
31 GetDelegate()->original_language_index() ==
32 TranslateInfoBarDelegate::kNoIndex
;
34 std::vector
<base::string16
> strings
;
35 TranslateInfoBarDelegate::GetAfterTranslateStrings(
36 &strings
, &swapped_language_combos
, autodetermined_source_language
);
37 DCHECK_EQ(autodetermined_source_language
? 2U : 3U, strings
.size());
39 GtkWidget
* new_hbox
= gtk_hbox_new(FALSE
, ui::kControlSpacing
);
40 gtk_util::CenterWidgetInHBox(hbox(), new_hbox
, false, 0);
42 size_t original_language_index
= GetDelegate()->original_language_index();
43 size_t target_language_index
= GetDelegate()->target_language_index();
44 bool exclude_the_other
= original_language_index
!= target_language_index
;
46 GtkWidget
* original_lang_combo
= NULL
;
47 if (!autodetermined_source_language
) {
48 original_lang_combo
= CreateLanguageCombobox(
49 original_language_index
,
51 target_language_index
: TranslateInfoBarDelegate::kNoIndex
);
52 signals()->Connect(original_lang_combo
, "changed",
53 G_CALLBACK(&OnOriginalLanguageModifiedThunk
), this);
55 GtkWidget
* target_lang_combo
= CreateLanguageCombobox(
56 target_language_index
,
57 exclude_the_other
? original_language_index
:
58 TranslateInfoBarDelegate::kNoIndex
);
59 signals()->Connect(target_lang_combo
, "changed",
60 G_CALLBACK(&OnTargetLanguageModifiedThunk
), this);
62 gtk_box_pack_start(GTK_BOX(new_hbox
),
63 CreateLabel(base::UTF16ToUTF8(strings
[0])),
67 (swapped_language_combos
|| autodetermined_source_language
) ?
68 target_lang_combo
: original_lang_combo
,
70 gtk_box_pack_start(GTK_BOX(new_hbox
),
71 CreateLabel(base::UTF16ToUTF8(strings
[1])),
73 if (!autodetermined_source_language
) {
74 gtk_box_pack_start(GTK_BOX(new_hbox
),
75 swapped_language_combos
?
76 original_lang_combo
: target_lang_combo
,
78 gtk_box_pack_start(GTK_BOX(new_hbox
),
79 CreateLabel(base::UTF16ToUTF8(strings
[2])),
83 GtkWidget
* button
= gtk_button_new_with_label(
84 l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_REVERT
).c_str());
85 signals()->Connect(button
, "clicked",
86 G_CALLBACK(&OnRevertPressedThunk
), this);
87 gtk_box_pack_start(GTK_BOX(new_hbox
), button
, FALSE
, FALSE
, 0);
90 bool AfterTranslateInfoBar::ShowOptionsMenuButton() const {
94 void AfterTranslateInfoBar::SetOriginalLanguage(size_t language_index
) {
95 GetDelegate()->UpdateOriginalLanguageIndex(language_index
);
96 GetDelegate()->Translate();
99 void AfterTranslateInfoBar::SetTargetLanguage(size_t language_index
) {
100 GetDelegate()->UpdateTargetLanguageIndex(language_index
);
101 GetDelegate()->Translate();
104 void AfterTranslateInfoBar::OnOriginalLanguageModified(GtkWidget
* sender
) {
105 size_t index
= GetLanguageComboboxActiveId(GTK_COMBO_BOX(sender
));
106 if (index
== GetDelegate()->original_language_index())
109 // Setting the language will lead to a new translation that is going to close
110 // the infobar. This is not OK to do this from the signal handler, so we'll
112 base::MessageLoop::current()->PostTask(
114 base::Bind(&AfterTranslateInfoBar::SetOriginalLanguage
,
115 weak_factory_
.GetWeakPtr(),
119 void AfterTranslateInfoBar::OnTargetLanguageModified(GtkWidget
* sender
) {
120 size_t index
= GetLanguageComboboxActiveId(GTK_COMBO_BOX(sender
));
121 if (index
== GetDelegate()->target_language_index())
124 // See comment in OnOriginalLanguageModified on why we use a task.
125 base::MessageLoop::current()->PostTask(
127 base::Bind(&AfterTranslateInfoBar::SetTargetLanguage
,
128 weak_factory_
.GetWeakPtr(),
132 void AfterTranslateInfoBar::OnRevertPressed(GtkWidget
* sender
) {
133 GetDelegate()->RevertTranslation();