Home | History | Annotate | Download | only in infobars
      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.
      4 
      5 #include "chrome/browser/ui/gtk/infobars/after_translate_infobar_gtk.h"
      6 
      7 #include "base/message_loop.h"
      8 #include "base/utf_string_conversions.h"
      9 #include "chrome/browser/translate/translate_infobar_delegate.h"
     10 #include "chrome/browser/ui/gtk/gtk_util.h"
     11 #include "grit/generated_resources.h"
     12 #include "ui/base/l10n/l10n_util.h"
     13 
     14 AfterTranslateInfoBar::AfterTranslateInfoBar(
     15     TranslateInfoBarDelegate* delegate)
     16     : TranslateInfoBarBase(delegate),
     17       ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
     18 }
     19 
     20 AfterTranslateInfoBar::~AfterTranslateInfoBar() {
     21 }
     22 
     23 void AfterTranslateInfoBar::Init() {
     24   TranslateInfoBarBase::Init();
     25 
     26   bool swapped_language_combos = false;
     27   std::vector<string16> strings;
     28   TranslateInfoBarDelegate::GetAfterTranslateStrings(
     29       &strings, &swapped_language_combos);
     30   DCHECK(strings.size() == 3U);
     31 
     32   GtkWidget* hbox = gtk_hbox_new(FALSE, gtk_util::kControlSpacing);
     33   gtk_util::CenterWidgetInHBox(hbox_, hbox, false, 0);
     34 
     35   GtkWidget* original_lang_combo =
     36       CreateLanguageCombobox(GetDelegate()->original_language_index(),
     37                              GetDelegate()->target_language_index());
     38   g_signal_connect(original_lang_combo, "changed",
     39                    G_CALLBACK(&OnOriginalLanguageModifiedThunk), this);
     40   GtkWidget* target_lang_combo =
     41       CreateLanguageCombobox(GetDelegate()->target_language_index(),
     42                              GetDelegate()->original_language_index());
     43   g_signal_connect(target_lang_combo, "changed",
     44                    G_CALLBACK(&OnTargetLanguageModifiedThunk), this);
     45 
     46   gtk_box_pack_start(GTK_BOX(hbox), CreateLabel(UTF16ToUTF8(strings[0])),
     47                      FALSE, FALSE, 0);
     48   gtk_box_pack_start(GTK_BOX(hbox),
     49                      swapped_language_combos ? target_lang_combo :
     50                                                original_lang_combo,
     51                      FALSE, FALSE, 0);
     52   gtk_box_pack_start(GTK_BOX(hbox), CreateLabel(UTF16ToUTF8(strings[1])),
     53                      FALSE, FALSE, 0);
     54   gtk_box_pack_start(GTK_BOX(hbox),
     55                      swapped_language_combos ? original_lang_combo :
     56                                                target_lang_combo,
     57                      FALSE, FALSE, 0);
     58   gtk_box_pack_start(GTK_BOX(hbox), CreateLabel(UTF16ToUTF8(strings[2])),
     59                      FALSE, FALSE, 0);
     60 
     61   GtkWidget* button = gtk_button_new_with_label(
     62       l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_REVERT).c_str());
     63   g_signal_connect(button, "clicked",G_CALLBACK(&OnRevertPressedThunk), this);
     64   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
     65 }
     66 
     67 bool AfterTranslateInfoBar::ShowOptionsMenuButton() const {
     68   return true;
     69 }
     70 
     71 void AfterTranslateInfoBar::OnOriginalLanguageModified(GtkWidget* sender) {
     72   size_t index = GetLanguageComboboxActiveId(GTK_COMBO_BOX(sender));
     73   if (index == GetDelegate()->original_language_index())
     74     return;
     75 
     76   // Setting the language will lead to a new translation that is going to close
     77   // the infobar.  This is not OK to do this from the signal handler, so we'll
     78   // defer it.
     79   MessageLoop::current()->PostTask(FROM_HERE, method_factory_.NewRunnableMethod(
     80       &AfterTranslateInfoBar::SetOriginalLanguage, index));
     81 }
     82 
     83 void AfterTranslateInfoBar::OnTargetLanguageModified(GtkWidget* sender) {
     84   size_t index = GetLanguageComboboxActiveId(GTK_COMBO_BOX(sender));
     85   if (index == GetDelegate()->target_language_index())
     86     return;
     87 
     88   // See comment in OnOriginalLanguageModified on why we use a task.
     89   MessageLoop::current()->PostTask(FROM_HERE, method_factory_.NewRunnableMethod(
     90       &AfterTranslateInfoBar::SetTargetLanguage, index));
     91 }
     92 
     93 void AfterTranslateInfoBar::OnRevertPressed(GtkWidget* sender) {
     94   GetDelegate()->RevertTranslation();
     95 }
     96 
     97 void AfterTranslateInfoBar::SetOriginalLanguage(size_t language_index) {
     98   GetDelegate()->SetOriginalLanguage(language_index);
     99 }
    100 
    101 void AfterTranslateInfoBar::SetTargetLanguage(size_t language_index) {
    102   GetDelegate()->SetTargetLanguage(language_index);
    103 }
    104