summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authorjcivelli@chromium.org <jcivelli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-19 00:52:05 +0000
committerjcivelli@chromium.org <jcivelli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-19 00:52:05 +0000
commitd905e7a7d5efb36cfa2ac0e474ffe8fed5008017 (patch)
tree76dda9173e629f7367f2511d19653fbdfd43c0de /chrome/browser
parent6dc033c2daee6c71bec401c3324c4440975561bc (diff)
downloadchromium_src-d905e7a7d5efb36cfa2ac0e474ffe8fed5008017.zip
chromium_src-d905e7a7d5efb36cfa2ac0e474ffe8fed5008017.tar.gz
chromium_src-d905e7a7d5efb36cfa2ac0e474ffe8fed5008017.tar.bz2
Porting the infobars on Linux to the new TranslateInfobarDelegate2 (relanding)
Each translate infobar is now its own class and most of the logic is in the delegate. Original review: http://codereview.chromium.org/2836006/show BUG=40828 TEST=Thoroughly test the translate feature on Linux. Review URL: http://codereview.chromium.org/2817015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50308 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/browser_main.cc8
-rw-r--r--chrome/browser/gtk/translate/after_translate_infobar_gtk.cc101
-rw-r--r--chrome/browser/gtk/translate/after_translate_infobar_gtk.h39
-rw-r--r--chrome/browser/gtk/translate/before_translate_infobar_gtk.cc71
-rw-r--r--chrome/browser/gtk/translate/before_translate_infobar_gtk.h31
-rw-r--r--chrome/browser/gtk/translate/translate_infobar_base_gtk.cc237
-rw-r--r--chrome/browser/gtk/translate/translate_infobar_base_gtk.h87
-rw-r--r--chrome/browser/gtk/translate/translate_message_infobar_gtk.cc40
-rw-r--r--chrome/browser/gtk/translate/translate_message_infobar_gtk.h26
-rw-r--r--chrome/browser/gtk/translate_infobars.cc586
-rw-r--r--chrome/browser/gtk/translate_infobars.h138
-rw-r--r--chrome/browser/tab_contents/render_view_context_menu.cc20
-rw-r--r--chrome/browser/translate/translate_infobar_delegate2.cc5
-rw-r--r--chrome/browser/translate/translate_manager2_unittest.cc185
14 files changed, 704 insertions, 870 deletions
diff --git a/chrome/browser/browser_main.cc b/chrome/browser/browser_main.cc
index 712f7e1..414b3de 100644
--- a/chrome/browser/browser_main.cc
+++ b/chrome/browser/browser_main.cc
@@ -56,10 +56,10 @@
#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/browser/search_engines/template_url_prepopulate_data.h"
#include "chrome/browser/shell_integration.h"
-#if !defined(OS_WIN)
-#include "chrome/browser/translate/translate_manager.h"
-#else
+#if defined(OS_WIN) || defined(OS_LINUX)
#include "chrome/browser/translate/translate_manager2.h"
+#else
+#include "chrome/browser/translate/translate_manager.h"
#endif
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
@@ -1175,7 +1175,7 @@ int BrowserMain(const MainFunctionParams& parameters) {
return ResultCodes::MACHINE_LEVEL_INSTALL_EXISTS;
// Create the TranslateManager singleton.
-#if defined(OS_WIN)
+#if defined(OS_WIN) || defined(OS_LINUX)
Singleton<TranslateManager2>::get();
#else
Singleton<TranslateManager>::get();
diff --git a/chrome/browser/gtk/translate/after_translate_infobar_gtk.cc b/chrome/browser/gtk/translate/after_translate_infobar_gtk.cc
new file mode 100644
index 0000000..1fab560
--- /dev/null
+++ b/chrome/browser/gtk/translate/after_translate_infobar_gtk.cc
@@ -0,0 +1,101 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/gtk/translate/after_translate_infobar_gtk.h"
+
+#include "app/l10n_util.h"
+#include "base/message_loop.h"
+#include "base/utf_string_conversions.h"
+#include "chrome/browser/gtk/gtk_util.h"
+#include "chrome/browser/translate/translate_infobar_delegate2.h"
+#include "grit/generated_resources.h"
+
+AfterTranslateInfoBar::AfterTranslateInfoBar(
+ TranslateInfoBarDelegate2* delegate)
+ : TranslateInfoBarBase(delegate),
+ ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
+}
+
+AfterTranslateInfoBar::~AfterTranslateInfoBar() {
+}
+
+void AfterTranslateInfoBar::Init() {
+ TranslateInfoBarBase::Init();
+
+ bool swapped_language_combos = false;
+ std::vector<string16> strings;
+ TranslateInfoBarDelegate2::GetAfterTranslateStrings(
+ &strings, &swapped_language_combos);
+ DCHECK(strings.size() == 3U);
+
+ GtkWidget* hbox = gtk_hbox_new(FALSE, gtk_util::kControlSpacing);
+ gtk_util::CenterWidgetInHBox(hbox_, hbox, false, 0);
+
+ GtkWidget* original_lang_combo =
+ CreateLanguageCombobox(GetDelegate()->original_language_index(),
+ GetDelegate()->target_language_index());
+ g_signal_connect(original_lang_combo, "changed",
+ G_CALLBACK(&OnOriginalLanguageModifiedThunk), this);
+ GtkWidget* target_lang_combo =
+ CreateLanguageCombobox(GetDelegate()->target_language_index(),
+ GetDelegate()->original_language_index());
+ g_signal_connect(target_lang_combo, "changed",
+ G_CALLBACK(&OnTargetLanguageModifiedThunk), this);
+
+ gtk_box_pack_start(GTK_BOX(hbox), CreateLabel(UTF16ToUTF8(strings[0])),
+ FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(hbox),
+ swapped_language_combos ? target_lang_combo :
+ original_lang_combo,
+ FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(hbox), CreateLabel(UTF16ToUTF8(strings[1])),
+ FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(hbox),
+ swapped_language_combos ? original_lang_combo :
+ target_lang_combo,
+ FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(hbox), CreateLabel(UTF16ToUTF8(strings[2])),
+ FALSE, FALSE, 0);
+
+ GtkWidget* button = gtk_button_new_with_label(
+ l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_REVERT).c_str());
+ g_signal_connect(button, "clicked",G_CALLBACK(&OnRevertPressedThunk), this);
+ gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
+
+ gtk_widget_show_all(border_bin_.get());
+}
+
+void AfterTranslateInfoBar::OnOriginalLanguageModified(GtkWidget* sender) {
+ int index = GetLanguageComboboxActiveId(GTK_COMBO_BOX(sender));
+ if (index == GetDelegate()->original_language_index())
+ return;
+
+ // Setting the language will lead to a new translation that is going to close
+ // the infobar. This is not OK to do this from the signal handler, so we'll
+ // defer it.
+ MessageLoop::current()->PostTask(FROM_HERE, method_factory_.NewRunnableMethod(
+ &AfterTranslateInfoBar::SetOriginalLanguage, index));
+}
+
+void AfterTranslateInfoBar::OnTargetLanguageModified(GtkWidget* sender) {
+ int index = GetLanguageComboboxActiveId(GTK_COMBO_BOX(sender));
+ if (index == GetDelegate()->target_language_index())
+ return;
+
+ // See comment in OnOriginalLanguageModified on why we use a task.
+ MessageLoop::current()->PostTask(FROM_HERE, method_factory_.NewRunnableMethod(
+ &AfterTranslateInfoBar::SetTargetLanguage, index));
+}
+
+void AfterTranslateInfoBar::OnRevertPressed(GtkWidget* sender) {
+ GetDelegate()->RevertTranslation();
+}
+
+void AfterTranslateInfoBar::SetOriginalLanguage(int language_index) {
+ GetDelegate()->SetOriginalLanguage(language_index);
+}
+
+void AfterTranslateInfoBar::SetTargetLanguage(int language_index) {
+ GetDelegate()->SetTargetLanguage(language_index);
+}
diff --git a/chrome/browser/gtk/translate/after_translate_infobar_gtk.h b/chrome/browser/gtk/translate/after_translate_infobar_gtk.h
new file mode 100644
index 0000000..576b023
--- /dev/null
+++ b/chrome/browser/gtk/translate/after_translate_infobar_gtk.h
@@ -0,0 +1,39 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_GTK_TRANSLATE_AFTER_TRANSLATE_INFOBAR_GTK_H_
+#define CHROME_BROWSER_GTK_TRANSLATE_AFTER_TRANSLATE_INFOBAR_GTK_H_
+
+#include "base/task.h"
+#include "chrome/browser/gtk/translate/translate_infobar_base_gtk.h"
+
+class TranslateInfoBarDelegate2;
+
+class AfterTranslateInfoBar : public TranslateInfoBarBase {
+ public:
+ explicit AfterTranslateInfoBar(TranslateInfoBarDelegate2* delegate);
+ virtual ~AfterTranslateInfoBar();
+
+ // Overridden from TranslateInfoBarBase:
+ virtual void Init();
+
+ protected:
+ virtual bool ShowOptionsMenuButton() const { return true; }
+
+ private:
+ CHROMEGTK_CALLBACK_0(AfterTranslateInfoBar, void, OnOriginalLanguageModified);
+ CHROMEGTK_CALLBACK_0(AfterTranslateInfoBar, void, OnTargetLanguageModified);
+ CHROMEGTK_CALLBACK_0(AfterTranslateInfoBar, void, OnRevertPressed);
+
+ // These methods set the original/target language on the
+ // TranslateInfobarDelegate.
+ void SetOriginalLanguage(int language_index);
+ void SetTargetLanguage(int language_index);
+
+ ScopedRunnableMethodFactory<AfterTranslateInfoBar> method_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(AfterTranslateInfoBar);
+};
+
+#endif // CHROME_BROWSER_GTK_TRANSLATE_AFTER_TRANSLATE_INFOBAR_GTK_H_
diff --git a/chrome/browser/gtk/translate/before_translate_infobar_gtk.cc b/chrome/browser/gtk/translate/before_translate_infobar_gtk.cc
new file mode 100644
index 0000000..53b9773
--- /dev/null
+++ b/chrome/browser/gtk/translate/before_translate_infobar_gtk.cc
@@ -0,0 +1,71 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/gtk/translate/before_translate_infobar_gtk.h"
+
+#include "app/l10n_util.h"
+#include "base/utf_string_conversions.h"
+#include "chrome/browser/gtk/gtk_util.h"
+#include "chrome/browser/translate/translate_infobar_delegate2.h"
+#include "grit/generated_resources.h"
+
+BeforeTranslateInfoBar::BeforeTranslateInfoBar(
+ TranslateInfoBarDelegate2* delegate)
+ : TranslateInfoBarBase(delegate) {
+}
+
+BeforeTranslateInfoBar::~BeforeTranslateInfoBar() {
+}
+
+void BeforeTranslateInfoBar::Init() {
+ TranslateInfoBarBase::Init();
+
+ GtkWidget* hbox = gtk_hbox_new(FALSE, gtk_util::kControlSpacing);
+ gtk_util::CenterWidgetInHBox(hbox_, hbox, false, 0);
+ size_t offset = 0;
+ string16 text =
+ l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_BEFORE_MESSAGE,
+ string16(), &offset);
+
+ gtk_box_pack_start(GTK_BOX(hbox),
+ CreateLabel(UTF16ToUTF8(text.substr(0, offset))),
+ FALSE, FALSE, 0);
+ GtkWidget* combobox =
+ CreateLanguageCombobox(GetDelegate()->original_language_index(),
+ GetDelegate()->target_language_index());
+ g_signal_connect(combobox, "changed",
+ G_CALLBACK(&OnLanguageModifiedThunk), this);
+ gtk_box_pack_start(GTK_BOX(hbox), combobox, FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(hbox),
+ CreateLabel(UTF16ToUTF8(text.substr(offset))),
+ FALSE, FALSE, 0);
+
+ GtkWidget* button = gtk_button_new_with_label(
+ l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_ACCEPT).c_str());
+ g_signal_connect(button, "clicked",G_CALLBACK(&OnAcceptPressedThunk), this);
+ gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
+
+ button = gtk_button_new_with_label(
+ l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_DENY).c_str());
+ g_signal_connect(button, "clicked",G_CALLBACK(&OnDenyPressedThunk), this);
+ gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
+
+ gtk_widget_show_all(border_bin_.get());
+}
+
+void BeforeTranslateInfoBar::OnLanguageModified(GtkWidget* sender) {
+ int index = GetLanguageComboboxActiveId(GTK_COMBO_BOX(sender));
+ if (index == GetDelegate()->original_language_index())
+ return;
+
+ GetDelegate()->SetOriginalLanguage(index);
+}
+
+void BeforeTranslateInfoBar::OnAcceptPressed(GtkWidget* sender) {
+ GetDelegate()->Translate();
+}
+
+void BeforeTranslateInfoBar::OnDenyPressed(GtkWidget* sender) {
+ RemoveInfoBar();
+}
diff --git a/chrome/browser/gtk/translate/before_translate_infobar_gtk.h b/chrome/browser/gtk/translate/before_translate_infobar_gtk.h
new file mode 100644
index 0000000..f31c529
--- /dev/null
+++ b/chrome/browser/gtk/translate/before_translate_infobar_gtk.h
@@ -0,0 +1,31 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_GTK_TRANSLATE_BEFORE_TRANSLATE_INFOBAR_GTK_H_
+#define CHROME_BROWSER_GTK_TRANSLATE_BEFORE_TRANSLATE_INFOBAR_GTK_H_
+
+#include "chrome/browser/gtk/translate/translate_infobar_base_gtk.h"
+
+class TranslateInfoBarDelegate2;
+
+class BeforeTranslateInfoBar : public TranslateInfoBarBase {
+ public:
+ explicit BeforeTranslateInfoBar(TranslateInfoBarDelegate2* delegate);
+ virtual ~BeforeTranslateInfoBar();
+
+ // Overridden from TranslateInfoBarBase:
+ virtual void Init();
+
+ protected:
+ virtual bool ShowOptionsMenuButton() const { return true; }
+
+ private:
+ CHROMEGTK_CALLBACK_0(BeforeTranslateInfoBar, void, OnLanguageModified);
+ CHROMEGTK_CALLBACK_0(BeforeTranslateInfoBar, void, OnAcceptPressed);
+ CHROMEGTK_CALLBACK_0(BeforeTranslateInfoBar, void, OnDenyPressed);
+
+ DISALLOW_COPY_AND_ASSIGN(BeforeTranslateInfoBar);
+};
+
+#endif // CHROME_BROWSER_GTK_TRANSLATE_BEFORE_TRANSLATE_INFOBAR_GTK_H_
diff --git a/chrome/browser/gtk/translate/translate_infobar_base_gtk.cc b/chrome/browser/gtk/translate/translate_infobar_base_gtk.cc
new file mode 100644
index 0000000..a2d46e0
--- /dev/null
+++ b/chrome/browser/gtk/translate/translate_infobar_base_gtk.cc
@@ -0,0 +1,237 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/gtk/translate/translate_infobar_base_gtk.h"
+
+#include "app/l10n_util.h"
+#include "app/resource_bundle.h"
+#include "app/slide_animation.h"
+#include "base/utf_string_conversions.h"
+#include "chrome/browser/translate/options_menu_model2.h"
+#include "chrome/browser/translate/translate_infobar_delegate2.h"
+#include "chrome/browser/gtk/translate/after_translate_infobar_gtk.h"
+#include "chrome/browser/gtk/translate/before_translate_infobar_gtk.h"
+#include "chrome/browser/gtk/translate/translate_message_infobar_gtk.h"
+#include "chrome/browser/gtk/gtk_util.h"
+#include "chrome/browser/gtk/menu_gtk.h"
+#include "gfx/canvas.h"
+#include "gfx/gtk_util.h"
+#include "grit/generated_resources.h"
+
+namespace {
+
+// To be able to map from language id <-> entry in the combo box, we
+// store the language id in the combo box data model in addition to the
+// displayed name.
+enum {
+ LANGUAGE_COMBO_COLUMN_ID,
+ LANGUAGE_COMBO_COLUMN_NAME,
+ LANGUAGE_COMBO_COLUMN_COUNT
+};
+
+} // namespace
+
+TranslateInfoBarBase::TranslateInfoBarBase(TranslateInfoBarDelegate2* delegate)
+ : InfoBar(delegate) {
+ TranslateInfoBarDelegate2::BackgroundAnimationType animation =
+ delegate->background_animation_type();
+ if (animation != TranslateInfoBarDelegate2::NONE) {
+ background_color_animation_.reset(new SlideAnimation(this));
+ background_color_animation_->SetTweenType(Tween::LINEAR);
+ background_color_animation_->SetSlideDuration(500);
+ if (animation == TranslateInfoBarDelegate2::NORMAL_TO_ERROR) {
+ background_color_animation_->Show();
+ } else {
+ DCHECK_EQ(TranslateInfoBarDelegate2::ERROR_TO_NORMAL, animation);
+ // Hide() runs the animation in reverse.
+ background_color_animation_->Reset(1.0);
+ background_color_animation_->Hide();
+ }
+ }
+}
+
+TranslateInfoBarBase::~TranslateInfoBarBase() {
+}
+
+void TranslateInfoBarBase::Init() {
+ if (!ShowOptionsMenuButton())
+ return;
+
+ // The options button sits outside the translate_box so that it can be end
+ // packed in hbox_.
+ GtkWidget* options_menu_button = BuildOptionsMenuButton();
+ g_signal_connect(options_menu_button, "clicked",
+ G_CALLBACK(&OnOptionsClickedThunk), this);
+ gtk_widget_show_all(options_menu_button);
+ gtk_util::CenterWidgetInHBox(hbox_, options_menu_button, true, 0);
+}
+
+void TranslateInfoBarBase::GetTopColor(InfoBarDelegate::Type type,
+ double* r, double* g, double *b) {
+ if (background_error_percent_ <= 0) {
+ InfoBar::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, r, g, b);
+ } else if (background_error_percent_ >= 1) {
+ InfoBar::GetTopColor(InfoBarDelegate::ERROR_TYPE, r, g, b);
+ } else {
+ double normal_r, normal_g, normal_b;
+ InfoBar::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE,
+ &normal_r, &normal_g, &normal_b);
+
+ double error_r, error_g, error_b;
+ InfoBar::GetTopColor(InfoBarDelegate::ERROR_TYPE,
+ &error_r, &error_g, &error_b);
+
+ double offset_r = error_r - normal_r;
+ double offset_g = error_g - normal_g;
+ double offset_b = error_b - normal_b;
+
+ *r = normal_r + (background_error_percent_ * offset_r);
+ *g = normal_g + (background_error_percent_ * offset_g);
+ *b = normal_b + (background_error_percent_ * offset_b);
+ }
+}
+
+void TranslateInfoBarBase::GetBottomColor(InfoBarDelegate::Type type,
+ double* r, double* g, double *b) {
+ if (background_error_percent_ <= 0) {
+ InfoBar::GetBottomColor(InfoBarDelegate::PAGE_ACTION_TYPE, r, g, b);
+ } else if (background_error_percent_ >= 1) {
+ InfoBar::GetBottomColor(InfoBarDelegate::ERROR_TYPE, r, g, b);
+ } else {
+ double normal_r, normal_g, normal_b;
+ InfoBar::GetBottomColor(InfoBarDelegate::PAGE_ACTION_TYPE,
+ &normal_r, &normal_g, &normal_b);
+
+ double error_r, error_g, error_b;
+ InfoBar::GetBottomColor(InfoBarDelegate::ERROR_TYPE,
+ &error_r, &error_g, &error_b);
+
+ double offset_r = error_r - normal_r;
+ double offset_g = error_g - normal_g;
+ double offset_b = error_b - normal_b;
+
+ *r = normal_r + (background_error_percent_ * offset_r);
+ *g = normal_g + (background_error_percent_ * offset_g);
+ *b = normal_b + (background_error_percent_ * offset_b);
+ }
+}
+
+void TranslateInfoBarBase::AnimationProgressed(const Animation* animation) {
+ DCHECK(animation == background_color_animation_.get());
+ background_error_percent_ = animation->GetCurrentValue();
+ // Queue the info bar widget for redisplay so it repaints its background.
+ gtk_widget_queue_draw(widget());
+}
+
+GtkWidget* TranslateInfoBarBase::CreateLabel(const std::string& text) {
+ GtkWidget* label = gtk_label_new(text.c_str());
+ gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &gfx::kGdkBlack);
+ return label;
+}
+
+GtkWidget* TranslateInfoBarBase::CreateLanguageCombobox(int selected_language,
+ int exclude_language) {
+ GtkListStore* model = gtk_list_store_new(LANGUAGE_COMBO_COLUMN_COUNT,
+ G_TYPE_INT, G_TYPE_STRING);
+ bool set_selection = false;
+ GtkTreeIter selected_iter;
+ TranslateInfoBarDelegate2* delegate = GetDelegate();
+ for (int i = 0; i < delegate->GetLanguageCount(); ++i) {
+ if (i == exclude_language)
+ continue;
+ GtkTreeIter tree_iter;
+ const string16& name = delegate->GetLanguageDisplayableNameAt(i);
+
+ gtk_list_store_append(model, &tree_iter);
+ gtk_list_store_set(model, &tree_iter,
+ LANGUAGE_COMBO_COLUMN_ID, i,
+ LANGUAGE_COMBO_COLUMN_NAME, UTF16ToUTF8(name).c_str(),
+ -1);
+ if (i == selected_language) {
+ selected_iter = tree_iter;
+ set_selection = true;
+ }
+ }
+
+ GtkWidget* combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
+ if (set_selection)
+ gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combobox), &selected_iter);
+ g_object_unref(model);
+ GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
+ gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE);
+ gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), renderer,
+ "text", LANGUAGE_COMBO_COLUMN_NAME,
+ NULL);
+ return combobox;
+}
+
+// static
+int TranslateInfoBarBase::GetLanguageComboboxActiveId(GtkComboBox* combo) {
+ GtkTreeIter iter;
+ if (!gtk_combo_box_get_active_iter(combo, &iter))
+ return 0;
+
+ gint id = 0;
+ gtk_tree_model_get(gtk_combo_box_get_model(combo), &iter,
+ LANGUAGE_COMBO_COLUMN_ID, &id,
+ -1);
+ return id;
+}
+
+TranslateInfoBarDelegate2* TranslateInfoBarBase::GetDelegate() const {
+ return static_cast<TranslateInfoBarDelegate2*>(delegate());
+}
+
+// static
+GtkWidget* TranslateInfoBarBase::BuildOptionsMenuButton() {
+ GtkWidget* button = gtk_button_new();
+ GtkWidget* former_child = gtk_bin_get_child(GTK_BIN(button));
+ if (former_child)
+ gtk_container_remove(GTK_CONTAINER(button), former_child);
+
+ GtkWidget* hbox = gtk_hbox_new(FALSE, 0);
+
+ GtkWidget* label = gtk_label_new(
+ l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_OPTIONS).c_str());
+ gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
+
+ GtkWidget* arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
+ gtk_box_pack_start(GTK_BOX(hbox), arrow, FALSE, FALSE, 0);
+
+ gtk_container_add(GTK_CONTAINER(button), hbox);
+
+ return button;
+}
+
+void TranslateInfoBarBase::OnOptionsClicked(GtkWidget* sender) {
+ if (!options_menu_model_.get()) {
+ options_menu_model_.reset(new OptionsMenuModel2(GetDelegate()));
+ options_menu_menu_.reset(new MenuGtk(NULL, options_menu_model_.get()));
+ }
+ options_menu_menu_->Popup(sender, 1, gtk_get_current_event_time());
+}
+
+// TranslateInfoBarDelegate specific method:
+InfoBar* TranslateInfoBarDelegate2::CreateInfoBar() {
+ TranslateInfoBarBase* infobar = NULL;
+ switch (type_) {
+ case BEFORE_TRANSLATE:
+ infobar = new BeforeTranslateInfoBar(this);
+ break;
+ case AFTER_TRANSLATE:
+ infobar = new AfterTranslateInfoBar(this);
+ break;
+ case TRANSLATING:
+ case TRANSLATION_ERROR:
+ infobar = new TranslateMessageInfoBar(this);
+ break;
+ default:
+ NOTREACHED();
+ }
+ infobar->Init();
+ // Set |infobar_view_| so that the model can notify the infobar when it
+ // changes.
+ infobar_view_ = infobar;
+ return infobar;
+}
diff --git a/chrome/browser/gtk/translate/translate_infobar_base_gtk.h b/chrome/browser/gtk/translate/translate_infobar_base_gtk.h
new file mode 100644
index 0000000..f2911f6
--- /dev/null
+++ b/chrome/browser/gtk/translate/translate_infobar_base_gtk.h
@@ -0,0 +1,87 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_GTK_TRANSLATE_TRANSLATE_INFOBAR_BASE_GTK_H_
+#define CHROME_BROWSER_GTK_TRANSLATE_TRANSLATE_INFOBAR_BASE_GTK_H_
+
+#include "chrome/browser/translate/translate_infobar_view.h"
+#include "chrome/browser/gtk/infobar_gtk.h"
+
+class MenuGtk;
+class OptionsMenuModel2;
+class TranslateInfoBarDelegate2;
+
+// This class contains some of the base functionality that translate infobars
+// use.
+class TranslateInfoBarBase : public TranslateInfoBarView,
+ public InfoBar,
+ public AnimationDelegate {
+ public:
+ explicit TranslateInfoBarBase(TranslateInfoBarDelegate2* delegate);
+ virtual ~TranslateInfoBarBase();
+
+ // Initializes the infobar widgets. Should be called after the object has been
+ // created.
+ virtual void Init();
+
+ // Overridden from InfoBar:
+ virtual void GetTopColor(InfoBarDelegate::Type type,
+ double* r, double* g, double *b);
+ virtual void GetBottomColor(InfoBarDelegate::Type type,
+ double* r, double* g, double *b);
+
+ // Overridden from TranslateInfoBarView:
+ virtual void OriginalLanguageChanged() {}
+ virtual void TargetLanguageChanged() {}
+
+ // Overridden from AnimationDelegate:
+ virtual void AnimationProgressed(const Animation* animation);
+
+ protected:
+ // Sub-classes that want to have the options menu button showing sould
+ // override and return true.
+ virtual bool ShowOptionsMenuButton() const { return false; }
+
+ // Creates a label with the appropriate font and color for the translate
+ // infobars.
+ GtkWidget* CreateLabel(const std::string& text);
+
+ // Creates a combobox that displays the languages currently available.
+ // |selected_language| is the language index (as used in the
+ // TranslateInfoDelegate) that should be selected initially.
+ // |exclude_language| is the language index of the language that should not be
+ // included in the list (-1 means no language excluded).
+ GtkWidget* CreateLanguageCombobox(int selected_language,
+ int exclude_language);
+
+ // Given an above-constructed combobox, returns the currently selected
+ // language id.
+ static int GetLanguageComboboxActiveId(GtkComboBox* combo);
+
+ // Convenience to retrieve the TranslateInfoBarDelegate2 for this infobar.
+ TranslateInfoBarDelegate2* GetDelegate() const;
+
+ private:
+ // Builds a button with an arrow in it to emulate the menu-button style from
+ // the windows version.
+ static GtkWidget* BuildOptionsMenuButton();
+
+ // The menu displayed when the Options button is pressed.
+ scoped_ptr<OptionsMenuModel2> options_menu_model_;
+ scoped_ptr<MenuGtk> options_menu_menu_;
+
+ CHROMEGTK_CALLBACK_0(TranslateInfoBarBase, void, OnOptionsClicked);
+
+ // A percentage to average the normal page action background with the error
+ // background. When 0, the infobar background should be pure PAGE_ACTION_TYPE.
+ // When 1, the infobar background should be pure ERROR_TYPE.
+ double background_error_percent_;
+
+ // Changes the color of the background from normal to error color and back.
+ scoped_ptr<SlideAnimation> background_color_animation_;
+
+ DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarBase);
+};
+
+#endif // CHROME_BROWSER_GTK_TRANSLATE_TRANSLATE_INFOBAR_BASE_GTK_H_
diff --git a/chrome/browser/gtk/translate/translate_message_infobar_gtk.cc b/chrome/browser/gtk/translate/translate_message_infobar_gtk.cc
new file mode 100644
index 0000000..3a5b828
--- /dev/null
+++ b/chrome/browser/gtk/translate/translate_message_infobar_gtk.cc
@@ -0,0 +1,40 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/gtk/translate/translate_message_infobar_gtk.h"
+
+#include "base/utf_string_conversions.h"
+#include "chrome/browser/gtk/gtk_util.h"
+#include "chrome/browser/translate/translate_infobar_delegate2.h"
+
+TranslateMessageInfoBar::TranslateMessageInfoBar(
+ TranslateInfoBarDelegate2* delegate)
+ : TranslateInfoBarBase(delegate) {
+}
+
+TranslateMessageInfoBar::~TranslateMessageInfoBar() {
+}
+
+void TranslateMessageInfoBar::Init() {
+ TranslateInfoBarBase::Init();
+
+ GtkWidget* hbox = gtk_hbox_new(FALSE, gtk_util::kControlSpacing);
+ gtk_util::CenterWidgetInHBox(hbox_, hbox, false, 0);
+
+ std::string text = UTF16ToUTF8(GetDelegate()->GetMessageInfoBarText());
+ gtk_box_pack_start(GTK_BOX(hbox), CreateLabel(text.c_str()), FALSE, FALSE, 0);
+ string16 button_text = GetDelegate()->GetMessageInfoBarButtonText();
+ if (!button_text.empty()) {
+ GtkWidget* button =
+ gtk_button_new_with_label(UTF16ToUTF8(button_text).c_str());
+ g_signal_connect(button, "clicked",G_CALLBACK(&OnButtonPressedThunk), this);
+ gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
+ }
+
+ gtk_widget_show_all(border_bin_.get());
+}
+
+void TranslateMessageInfoBar::OnButtonPressed(GtkWidget* sender) {
+ GetDelegate()->MessageInfoBarButtonPressed();
+}
diff --git a/chrome/browser/gtk/translate/translate_message_infobar_gtk.h b/chrome/browser/gtk/translate/translate_message_infobar_gtk.h
new file mode 100644
index 0000000..f0e70a3
--- /dev/null
+++ b/chrome/browser/gtk/translate/translate_message_infobar_gtk.h
@@ -0,0 +1,26 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_GTK_TRANSLATE_TRANSLATE_MESSAGE_INFOBAR_GTK_H_
+#define CHROME_BROWSER_GTK_TRANSLATE_TRANSLATE_MESSAGE_INFOBAR_GTK_H_
+
+#include "chrome/browser/gtk/translate/translate_infobar_base_gtk.h"
+
+class TranslateInfoBarDelegate2;
+
+class TranslateMessageInfoBar : public TranslateInfoBarBase {
+ public:
+ explicit TranslateMessageInfoBar(TranslateInfoBarDelegate2* delegate);
+ virtual ~TranslateMessageInfoBar();
+
+ // Overridden from TranslateInfoBarBase:
+ virtual void Init();
+
+ private:
+ CHROMEGTK_CALLBACK_0(TranslateMessageInfoBar, void, OnButtonPressed);
+
+ DISALLOW_COPY_AND_ASSIGN(TranslateMessageInfoBar);
+};
+
+#endif // CHROME_BROWSER_GTK_TRANSLATE_TRANSLATE_MESSAGE_INFOBAR_GTK_H_
diff --git a/chrome/browser/gtk/translate_infobars.cc b/chrome/browser/gtk/translate_infobars.cc
deleted file mode 100644
index d85a7f5..0000000
--- a/chrome/browser/gtk/translate_infobars.cc
+++ /dev/null
@@ -1,586 +0,0 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/gtk/translate_infobars.h"
-
-#include <string>
-#include <vector>
-
-#include "app/l10n_util.h"
-#include "app/slide_animation.h"
-#include "base/utf_string_conversions.h"
-#include "chrome/app/chrome_dll_resource.h"
-#include "chrome/browser/browser_process.h"
-#include "chrome/browser/gtk/gtk_util.h"
-#include "chrome/browser/gtk/menu_gtk.h"
-#include "chrome/browser/tab_contents/tab_contents.h"
-#include "chrome/browser/translate/translate_infobars_delegates.h"
-#include "chrome/browser/translate/options_menu_model.h"
-#include "chrome/browser/translate/page_translated_details.h"
-#include "chrome/common/notification_service.h"
-#include "gfx/gtk_util.h"
-#include "grit/app_resources.h"
-#include "grit/generated_resources.h"
-#include "grit/locale_settings.h"
-#include "grit/theme_resources.h"
-
-namespace {
-
-// Reorders the widgets in the NULL terminated array |widgets| that are all
-// children of |box|.
-void ReorderWidgetsTo(GtkWidget* box, GtkWidget** widgets) {
- for (int count = 0; *widgets != NULL; widgets++) {
- gtk_box_reorder_child(GTK_BOX(box), *widgets, count++);
- gtk_widget_show_all(*widgets);
- }
-}
-
-// To be able to map from language id <-> entry in the combo box, we
-// store the language id in the combo box data model in addition to the
-// displayed name.
-enum {
- LANGUAGE_COMBO_COLUMN_ID,
- LANGUAGE_COMBO_COLUMN_NAME,
- LANGUAGE_COMBO_COLUMN_COUNT
-};
-
-// Creates a combobox set up to display text from a list of language codes
-// (translating the codes into the display string).
-GtkWidget* BuildLanguageComboboxFrom(
- TranslateInfoBarDelegate* delegate,
- const std::vector<std::string>& languages) {
- std::map<string16, int> language_id_map;
- std::vector<string16> display_languages;
- for (size_t i = 0; i < languages.size(); ++i) {
- string16 name = delegate->GetDisplayNameForLocale(languages[i]);
- display_languages.push_back(name);
- language_id_map[name] = i;
- }
-
- l10n_util::SortStrings16(g_browser_process->GetApplicationLocale(),
- &display_languages);
-
- // Note: be sure this model matches the LANGUAGE_COMBO enum above.
- GtkListStore* model = gtk_list_store_new(LANGUAGE_COMBO_COLUMN_COUNT,
- G_TYPE_INT, G_TYPE_STRING);
- for (size_t i = 0; i < display_languages.size(); ++i) {
- GtkTreeIter tree_iter;
- const string16& name = display_languages[i];
- gtk_list_store_append(model, &tree_iter);
- gtk_list_store_set(model, &tree_iter,
- LANGUAGE_COMBO_COLUMN_ID, language_id_map[name],
- LANGUAGE_COMBO_COLUMN_NAME, UTF16ToUTF8(name).c_str(),
- -1);
- }
-
- GtkWidget* combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
- g_object_unref(model);
- GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
- gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE);
- gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), renderer,
- "text", LANGUAGE_COMBO_COLUMN_NAME,
- NULL);
-
- return combobox;
-}
-
-// Given an above-constructed combo box, grab the currently-selected
-// language id.
-int GetLanguageComboboxActiveId(GtkComboBox* combo) {
- GtkTreeIter iter;
- if (!gtk_combo_box_get_active_iter(combo, &iter))
- return 0;
-
- gint id = 0;
- gtk_tree_model_get(gtk_combo_box_get_model(combo), &iter,
- LANGUAGE_COMBO_COLUMN_ID, &id,
- -1);
- return id;
-}
-
-// Given an above-constructed combo box, select the row corresponding
-// to a given language id.
-void SetLanguageComboboxActiveId(GtkComboBox* combo, int target) {
- GtkTreeModel* model = gtk_combo_box_get_model(combo);
- GtkTreeIter iter;
- gtk_tree_model_get_iter_first(model, &iter);
- do {
- gint id;
- gtk_tree_model_get(model, &iter,
- LANGUAGE_COMBO_COLUMN_ID, &id,
- -1);
- if (id == target) {
- gtk_combo_box_set_active_iter(combo, &iter);
- return;
- }
- } while (gtk_tree_model_iter_next(model, &iter));
-}
-
-// Builds a button with an arrow in it to emulate the menu-button style from
-// the windows version.
-GtkWidget* BuildOptionsMenuButton() {
- GtkWidget* button = gtk_button_new();
- GtkWidget* former_child = gtk_bin_get_child(GTK_BIN(button));
- if (former_child)
- gtk_container_remove(GTK_CONTAINER(button), former_child);
-
- GtkWidget* hbox = gtk_hbox_new(FALSE, 0);
-
- GtkWidget* label = gtk_label_new(
- l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_OPTIONS).c_str());
- gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
-
- GtkWidget* arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
- gtk_box_pack_start(GTK_BOX(hbox), arrow, FALSE, FALSE, 0);
-
- gtk_container_add(GTK_CONTAINER(button), hbox);
-
- return button;
-}
-
-} // namespace
-
-TranslateInfoBar::TranslateInfoBar(TranslateInfoBarDelegate* delegate)
- : InfoBar(delegate),
- background_error_percent_(0),
- state_(TranslateInfoBarDelegate::kTranslateNone),
- translation_pending_(false),
- swapped_language_placeholders_(false) {
- // Initialize slide animation for transitioning to and from error state.
- error_animation_.reset(new SlideAnimation(this));
- error_animation_->SetTweenType(Tween::LINEAR);
- error_animation_->SetSlideDuration(500);
-
- BuildWidgets();
-
- // Register for PAGE_TRANSLATED notification.
- notification_registrar_.Add(this, NotificationType::PAGE_TRANSLATED,
- Source<TabContents>(GetDelegate()->tab_contents()));
-}
-
-TranslateInfoBar::~TranslateInfoBar() {
-}
-
-void TranslateInfoBar::GetTopColor(InfoBarDelegate::Type type,
- double* r, double* g, double *b) {
- if (background_error_percent_ <= 0) {
- InfoBar::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, r, g, b);
- } else if (background_error_percent_ >= 1) {
- InfoBar::GetTopColor(InfoBarDelegate::ERROR_TYPE, r, g, b);
- } else {
- double normal_r, normal_g, normal_b;
- InfoBar::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE,
- &normal_r, &normal_g, &normal_b);
-
- double error_r, error_g, error_b;
- InfoBar::GetTopColor(InfoBarDelegate::ERROR_TYPE,
- &error_r, &error_g, &error_b);
-
- double offset_r = error_r - normal_r;
- double offset_g = error_g - normal_g;
- double offset_b = error_b - normal_b;
-
- *r = normal_r + (background_error_percent_ * offset_r);
- *g = normal_g + (background_error_percent_ * offset_g);
- *b = normal_b + (background_error_percent_ * offset_b);
- }
-}
-
-void TranslateInfoBar::GetBottomColor(InfoBarDelegate::Type type,
- double* r, double* g, double *b) {
- if (background_error_percent_ <= 0) {
- InfoBar::GetBottomColor(InfoBarDelegate::PAGE_ACTION_TYPE, r, g, b);
- } else if (background_error_percent_ >= 1) {
- InfoBar::GetBottomColor(InfoBarDelegate::ERROR_TYPE, r, g, b);
- } else {
- double normal_r, normal_g, normal_b;
- InfoBar::GetBottomColor(InfoBarDelegate::PAGE_ACTION_TYPE,
- &normal_r, &normal_g, &normal_b);
-
- double error_r, error_g, error_b;
- InfoBar::GetBottomColor(InfoBarDelegate::ERROR_TYPE,
- &error_r, &error_g, &error_b);
-
- double offset_r = error_r - normal_r;
- double offset_g = error_g - normal_g;
- double offset_b = error_b - normal_b;
-
- *r = normal_r + (background_error_percent_ * offset_r);
- *g = normal_g + (background_error_percent_ * offset_g);
- *b = normal_b + (background_error_percent_ * offset_b);
- }
-}
-
-void TranslateInfoBar::Observe(NotificationType type,
- const NotificationSource& source, const NotificationDetails& details) {
- if (type.value == NotificationType::PAGE_TRANSLATED) {
- TabContents* tab = Source<TabContents>(source).ptr();
- if (tab != GetDelegate()->tab_contents())
- return;
-
- PageTranslatedDetails* page_translated_details =
- Details<PageTranslatedDetails>(details).ptr();
- UpdateState((page_translated_details->error_type == TranslateErrors::NONE ?
- TranslateInfoBarDelegate::kAfterTranslate :
- TranslateInfoBarDelegate::kTranslateError), false,
- page_translated_details->error_type);
- } else {
- InfoBar::Observe(type, source, details);
- }
-}
-
-void TranslateInfoBar::AnimationProgressed(const Animation* animation) {
- background_error_percent_ = animation->GetCurrentValue();
- // Queue the info bar widget for redisplay.
- gtk_widget_queue_draw(widget());
-}
-
-bool TranslateInfoBar::IsCommandIdChecked(int command_id) const {
- TranslateInfoBarDelegate* translate_delegate = GetDelegate();
- switch (command_id) {
- case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG :
- return translate_delegate->IsLanguageBlacklisted();
-
- case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE :
- return translate_delegate->IsSiteBlacklisted();
-
- case IDC_TRANSLATE_OPTIONS_ALWAYS :
- return translate_delegate->ShouldAlwaysTranslate();
-
- default:
- NOTREACHED() << "Invalid command_id from menu";
- break;
- }
- return false;
-}
-
-bool TranslateInfoBar::IsCommandIdEnabled(int command_id) const {
- TranslateInfoBarDelegate* translate_delegate = GetDelegate();
- switch (command_id) {
- case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG :
- case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE :
- return !translate_delegate->ShouldAlwaysTranslate();
-
- case IDC_TRANSLATE_OPTIONS_ALWAYS :
- return (!translate_delegate->IsLanguageBlacklisted() &&
- !translate_delegate->IsSiteBlacklisted());
-
- default:
- break;
- }
- return true;
-}
-
-bool TranslateInfoBar::GetAcceleratorForCommandId(int command_id,
- menus::Accelerator* accelerator) {
- return false;
-}
-
-void TranslateInfoBar::ExecuteCommand(int command_id) {
- switch (command_id) {
- case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG:
- GetDelegate()->ToggleLanguageBlacklist();
- RemoveInfoBar();
- break;
-
- case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE:
- GetDelegate()->ToggleSiteBlacklist();
- RemoveInfoBar();
- break;
-
- case IDC_TRANSLATE_OPTIONS_ALWAYS:
- GetDelegate()->ToggleAlwaysTranslate();
- break;
-
- case IDC_TRANSLATE_OPTIONS_ABOUT: {
- TabContents* tab_contents = GetDelegate()->tab_contents();
- if (tab_contents) {
- string16 url = l10n_util::GetStringUTF16(
- IDS_ABOUT_GOOGLE_TRANSLATE_URL);
- tab_contents->OpenURL(GURL(url), GURL(), NEW_FOREGROUND_TAB,
- PageTransition::LINK);
- }
- break;
- }
-
- default:
- NOTREACHED() << "Invalid command id from menu.";
- break;
- }
-}
-
-void TranslateInfoBar::BuildWidgets() {
- translate_box_ = gtk_hbox_new(FALSE, gtk_util::kControlSpacing);
- gtk_widget_set_no_show_all(translate_box_, TRUE);
-
- // Add all labels to the translate_box_. Unlike views or cocoa, there's no
- // concept of manual layout in GTK, so we'll manually reorder these widgets
- // inside |translate_box_| and selectively show and hide them.
- label_1_ = gtk_label_new(NULL);
- gtk_box_pack_start(GTK_BOX(translate_box_), label_1_, FALSE, FALSE, 0);
- gtk_widget_modify_fg(label_1_, GTK_STATE_NORMAL, &gfx::kGdkBlack);
-
- label_2_ = gtk_label_new(NULL);
- gtk_box_pack_start(GTK_BOX(translate_box_), label_2_, FALSE, FALSE, 0);
- gtk_widget_modify_fg(label_2_, GTK_STATE_NORMAL, &gfx::kGdkBlack);
-
- label_3_ = gtk_label_new(NULL);
- gtk_box_pack_start(GTK_BOX(translate_box_), label_3_, FALSE, FALSE, 0);
- gtk_widget_modify_fg(label_3_, GTK_STATE_NORMAL, &gfx::kGdkBlack);
-
- translating_label_ = gtk_label_new(
- l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_TRANSLATING).c_str());
- gtk_box_pack_start(GTK_BOX(translate_box_), translating_label_,
- FALSE, FALSE, 0);
- gtk_widget_modify_fg(translating_label_, GTK_STATE_NORMAL, &gfx::kGdkBlack);
-
- error_label_ = gtk_label_new(NULL);
- gtk_box_pack_start(GTK_BOX(translate_box_), error_label_,
- FALSE, FALSE, 0);
- gtk_widget_modify_fg(error_label_, GTK_STATE_NORMAL, &gfx::kGdkBlack);
-
- accept_button_ = gtk_button_new_with_label(
- l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_ACCEPT).c_str());
- g_signal_connect(accept_button_, "clicked",
- G_CALLBACK(&OnAcceptPressedThunk), this);
- accept_button_vbox_ = gtk_util::CenterWidgetInHBox(
- translate_box_, accept_button_, false, 0);
-
- deny_button_ = gtk_button_new_with_label(
- l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_DENY).c_str());
- g_signal_connect(deny_button_, "clicked",
- G_CALLBACK(&OnDenyPressedThunk), this);
- deny_button_vbox_ = gtk_util::CenterWidgetInHBox(
- translate_box_, deny_button_, false, 0);
-
- retry_button_ = gtk_button_new_with_label(
- l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_RETRY).c_str());
- g_signal_connect(retry_button_, "clicked",
- G_CALLBACK(&OnAcceptPressedThunk), this);
- retry_button_vbox_ = gtk_util::CenterWidgetInHBox(
- translate_box_, retry_button_, false, 0);
-
- revert_button_ = gtk_button_new_with_label(
- l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_REVERT).c_str());
- g_signal_connect(revert_button_, "clicked",
- G_CALLBACK(&OnRevertPressedThunk), this);
- revert_button_vbox_ = gtk_util::CenterWidgetInHBox(
- translate_box_, revert_button_, false, 0);
-
- std::vector<std::string> orig_languages;
- GetDelegate()->GetAvailableOriginalLanguages(&orig_languages);
- original_language_combobox_ = BuildLanguageComboboxFrom(GetDelegate(),
- orig_languages);
- g_signal_connect(original_language_combobox_, "changed",
- G_CALLBACK(&OnOriginalModifiedThunk), this);
- original_language_combobox_vbox_ = gtk_util::CenterWidgetInHBox(
- translate_box_, original_language_combobox_, false, 0);
-
- std::vector<std::string> target_languages;
- GetDelegate()->GetAvailableTargetLanguages(&target_languages);
- target_language_combobox_ = BuildLanguageComboboxFrom(GetDelegate(),
- target_languages);
- g_signal_connect(target_language_combobox_, "changed",
- G_CALLBACK(&OnTargetModifiedThunk), this);
- target_language_combobox_vbox_ = gtk_util::CenterWidgetInHBox(
- translate_box_, target_language_combobox_, false, 0);
-
- gtk_box_pack_start(GTK_BOX(hbox_), translate_box_, FALSE, FALSE, 0);
-
- // The options button sits outside the translate_box so that it can be end
- // packed in hbox_.
- options_menu_button_ = BuildOptionsMenuButton();
- g_signal_connect(options_menu_button_, "clicked",
- G_CALLBACK(&OnOptionsClickedThunk), this);
- gtk_widget_show_all(options_menu_button_);
- gtk_util::CenterWidgetInHBox(
- hbox_, options_menu_button_, true, 0);
-
- UpdateState(GetDelegate()->state(), GetDelegate()->translation_pending(),
- GetDelegate()->error_type());
-
- gtk_widget_show_all(border_bin_.get());
-}
-
-void TranslateInfoBar::UpdateState(
- TranslateInfoBarDelegate::TranslateState new_state,
- bool new_translation_pending, TranslateErrors::Type error_type) {
- if (state_ == new_state && translation_pending_ == new_translation_pending)
- return;
-
- TranslateInfoBarDelegate::TranslateState old_state = state_;
- state_ = new_state;
- translation_pending_ = new_translation_pending;
-
- // Show the box...
- gtk_widget_show(translate_box_);
- // ...but hide all children of the translate box. They will be selectively
- // unhidden and reordered based on the current translate state.
- gtk_container_foreach(
- GTK_CONTAINER(translate_box_),
- reinterpret_cast<void (*)(GtkWidget*, void*)>(gtk_widget_hide_all), NULL);
-
- switch (state_) {
- case TranslateInfoBarDelegate::kBeforeTranslate: {
- SetLabels();
-
- GtkWidget* before_state[] = {
- label_1_,
- original_language_combobox_vbox_,
- label_2_,
- translation_pending_ ? translating_label_ : accept_button_vbox_,
- translation_pending_ ? NULL : deny_button_vbox_,
- NULL
- };
- ReorderWidgetsTo(translate_box_, before_state);
-
- SetLanguageComboboxActiveId(GTK_COMBO_BOX(original_language_combobox_),
- GetDelegate()->original_lang_index());
- break;
- }
- case TranslateInfoBarDelegate::kAfterTranslate: {
- SetLabels();
- GtkWidget* first_button_box =
- swapped_language_placeholders_ ?
- target_language_combobox_vbox_ : original_language_combobox_vbox_;
- GtkWidget* second_button_box =
- swapped_language_placeholders_ ?
- original_language_combobox_vbox_ : target_language_combobox_vbox_;
-
- GtkWidget* after_state[] = {
- label_1_,
- first_button_box,
- label_2_,
- second_button_box,
- label_3_,
- translation_pending_ ? translating_label_ : revert_button_vbox_,
- NULL
- };
- ReorderWidgetsTo(translate_box_, after_state);
-
- SetLanguageComboboxActiveId(GTK_COMBO_BOX(original_language_combobox_),
- GetDelegate()->original_lang_index());
- SetLanguageComboboxActiveId(GTK_COMBO_BOX(target_language_combobox_),
- GetDelegate()->target_lang_index());
- break;
- }
- case TranslateInfoBarDelegate::kTranslateError: {
- string16 error_message_utf16 = GetDelegate()->GetErrorMessage(error_type);
- gtk_label_set_text(GTK_LABEL(error_label_),
- UTF16ToUTF8(error_message_utf16).c_str());
-
- GtkWidget* error_state[] = {
- error_label_,
- retry_button_vbox_,
- NULL
- };
- ReorderWidgetsTo(translate_box_, error_state);
- break;
- }
- default: {
- NOTIMPLEMENTED() << "Received state " << new_state;
- }
- }
-
- // The options button is visible any time that we aren't in an error state.
- if (state_ == TranslateInfoBarDelegate::kTranslateError)
- gtk_widget_hide(options_menu_button_);
- else
- gtk_widget_show(options_menu_button_);
-
- // If background should change per state, trigger animation of transition
- // accordingly.
- if (old_state != TranslateInfoBarDelegate::kTranslateError &&
- state_ == TranslateInfoBarDelegate::kTranslateError) {
- error_animation_->Show(); // Transition to error state.
- } else if (old_state == TranslateInfoBarDelegate::kTranslateError &&
- state_ != TranslateInfoBarDelegate::kTranslateError) {
- error_animation_->Hide(); // Transition from error state.
- } else {
- error_animation_->Stop(); // No transition.
- }
-}
-
-void TranslateInfoBar::SetLabels() {
- std::vector<size_t> offsets;
- string16 message_text_utf16;
- GetDelegate()->GetMessageText(GetDelegate()->state(), &message_text_utf16,
- &offsets, &swapped_language_placeholders_);
-
- string16 text = message_text_utf16.substr(0, offsets[0]);
- gtk_label_set_text(GTK_LABEL(label_1_), UTF16ToUTF8(text).c_str());
-
- text = message_text_utf16.substr(offsets[0], offsets[1] - offsets[0]);
- gtk_label_set_text(GTK_LABEL(label_2_), UTF16ToUTF8(text).c_str());
-
- if (offsets.size() == 3) {
- text = message_text_utf16.substr(offsets[1], offsets[2] - offsets[1]);
- gtk_label_set_text(GTK_LABEL(label_3_), UTF16ToUTF8(text).c_str());
- }
-}
-
-TranslateInfoBarDelegate* TranslateInfoBar::GetDelegate() const {
- return static_cast<TranslateInfoBarDelegate*>(delegate());
-}
-
-void TranslateInfoBar::LanguageModified() {
- options_menu_model_.reset();
-
- // Selecting an item from the "from language" menu in the before translate
- // phase shouldn't trigger translation - http://crbug.com/36666
- if (GetDelegate()->state() == TranslateInfoBarDelegate::kAfterTranslate) {
- GetDelegate()->Translate();
- UpdateState(GetDelegate()->state(), GetDelegate()->translation_pending(),
- GetDelegate()->error_type());
- }
-}
-
-void TranslateInfoBar::OnOriginalModified(GtkWidget* sender) {
- int index = GetLanguageComboboxActiveId(GTK_COMBO_BOX(sender));
- if (index == GetDelegate()->original_lang_index())
- return;
-
- GetDelegate()->ModifyOriginalLanguage(index);
-
- LanguageModified();
-}
-
-void TranslateInfoBar::OnTargetModified(GtkWidget* sender) {
- int index = GetLanguageComboboxActiveId(GTK_COMBO_BOX(sender));
- if (index == GetDelegate()->target_lang_index())
- return;
-
- GetDelegate()->ModifyTargetLanguage(index);
-
- LanguageModified();
-}
-
-void TranslateInfoBar::OnAcceptPressed(GtkWidget* sender) {
- GetDelegate()->Translate();
- UpdateState(GetDelegate()->state(), GetDelegate()->translation_pending(),
- GetDelegate()->error_type());
-}
-
-void TranslateInfoBar::OnDenyPressed(GtkWidget* sender) {
- GetDelegate()->TranslationDeclined();
- RemoveInfoBar();
-}
-
-void TranslateInfoBar::OnRevertPressed(GtkWidget* sender) {
- GetDelegate()->RevertTranslation();
-}
-
-void TranslateInfoBar::OnOptionsClicked(GtkWidget* sender) {
- if (!options_menu_model_.get()) {
- options_menu_model_.reset(new OptionsMenuModel(this, GetDelegate()));
- options_menu_menu_.reset(new MenuGtk(this, options_menu_model_.get()));
- }
- options_menu_menu_->Popup(sender, 1, gtk_get_current_event_time());
-}
-
-// TranslateInfoBarDelegate, InfoBarDelegate overrides: ------------------
-
-InfoBar* TranslateInfoBarDelegate::CreateInfoBar() {
- return new TranslateInfoBar(this);
-}
diff --git a/chrome/browser/gtk/translate_infobars.h b/chrome/browser/gtk/translate_infobars.h
deleted file mode 100644
index 87d2029..0000000
--- a/chrome/browser/gtk/translate_infobars.h
+++ /dev/null
@@ -1,138 +0,0 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_GTK_TRANSLATE_INFOBARS_H_
-#define CHROME_BROWSER_GTK_TRANSLATE_INFOBARS_H_
-
-#include <gtk/gtk.h>
-
-#include "app/gtk_signal.h"
-#include "app/menus/simple_menu_model.h"
-#include "chrome/browser/gtk/infobar_gtk.h"
-#include "chrome/browser/gtk/menu_gtk.h"
-#include "chrome/browser/translate/translate_infobars_delegates.h"
-#include "chrome/common/notification_registrar.h"
-
-class SlideAnimation;
-class OptionsMenuModel;
-
-// InfoBar that asks user if they want to translate a page that isn't in the
-// user's language to their language. Bar changes during and after translation.
-class TranslateInfoBar : public InfoBar,
- public AnimationDelegate,
- public menus::SimpleMenuModel::Delegate,
- public MenuGtk::Delegate {
- public:
- explicit TranslateInfoBar(TranslateInfoBarDelegate* delegate);
- virtual ~TranslateInfoBar();
-
- // Overridden from InfoBar:
- virtual void GetTopColor(InfoBarDelegate::Type type,
- double* r, double* g, double *b);
- virtual void GetBottomColor(InfoBarDelegate::Type type,
- double* r, double* g, double *b);
-
- // Overridden from NotificationObserver (through InfoBar):
- virtual void Observe(NotificationType type,
- const NotificationSource& source, const NotificationDetails& details);
-
- // Overridden from AnimationDelegate:
- virtual void AnimationProgressed(const Animation* animation);
-
- // Overridden for both menus::SimpleMenuModel::Delegate and MenuGtk::Delegate:
- virtual bool IsCommandIdChecked(int command_id) const;
- virtual bool IsCommandIdEnabled(int command_id) const;
-
- // Overridden from menus::SimpleMenuModel::Delegate:
- virtual bool GetAcceleratorForCommandId(int command_id,
- menus::Accelerator* accelerator);
- virtual void ExecuteCommand(int command_id);
-
- // Overridden from MenuGtk::Delegate:
- virtual void ExecuteCommandById(int command_id) {
- ExecuteCommand(command_id);
- }
-
- private:
- // Builds all the widgets and sets the initial view of the dialog.
- void BuildWidgets();
-
- // Changes the layout of the info bar, displaying the correct widgets in the
- // correct order for |new_state|.
- void UpdateState(TranslateInfoBarDelegate::TranslateState new_state,
- bool new_translation_pending, TranslateErrors::Type error_type);
-
- // Sets the text in the three labels for the current state.
- void SetLabels();
-
- // Casts delegate() to the only possible subclass.
- TranslateInfoBarDelegate* GetDelegate() const;
-
- // Called after OnOriginalModified or OnTargetModified.
- void LanguageModified();
-
- CHROMEGTK_CALLBACK_0(TranslateInfoBar, void, OnOriginalModified);
- CHROMEGTK_CALLBACK_0(TranslateInfoBar, void, OnTargetModified);
- CHROMEGTK_CALLBACK_0(TranslateInfoBar, void, OnAcceptPressed);
- CHROMEGTK_CALLBACK_0(TranslateInfoBar, void, OnDenyPressed);
- CHROMEGTK_CALLBACK_0(TranslateInfoBar, void, OnRevertPressed);
- CHROMEGTK_CALLBACK_0(TranslateInfoBar, void, OnOptionsClicked);
-
- // A percentage to average the normal page action background with the error
- // background. When 0, the infobar background should be pure
- // PAGE_ACTION_TYPE. When 1, the infobar background should be pure
- // ERROR_TYPE.
- double background_error_percent_;
-
- // Infobar keeps track of the state it is displaying, which should match that
- // in the TranslateInfoBarDelegate. UI needs to keep track separately because
- // infobar may receive PAGE_TRANSLATED notifications before delegate does, in
- // which case, delegate's state is not updated and hence can't be used to
- // update display. After the notification is sent out to all observers, both
- // infobar and delegate would end up with the same state.
- TranslateInfoBarDelegate::TranslateState state_;
- bool translation_pending_;
- TranslateErrors::Type error_type_;
-
- GtkWidget* translate_box_;
- GtkWidget* label_1_;
- GtkWidget* label_2_;
- GtkWidget* label_3_;
- GtkWidget* translating_label_;
- GtkWidget* error_label_;
-
- GtkWidget* accept_button_;
- GtkWidget* accept_button_vbox_;
-
- GtkWidget* deny_button_;
- GtkWidget* deny_button_vbox_;
-
- GtkWidget* retry_button_;
- GtkWidget* retry_button_vbox_;
-
- GtkWidget* revert_button_;
- GtkWidget* revert_button_vbox_;
-
- GtkWidget* original_language_combobox_;
- GtkWidget* original_language_combobox_vbox_;
-
- GtkWidget* target_language_combobox_;
- GtkWidget* target_language_combobox_vbox_;
-
- GtkWidget* options_menu_button_;
- scoped_ptr<MenuGtk> options_menu_menu_;
- scoped_ptr<OptionsMenuModel> options_menu_model_;
-
- // This is true if language placeholders in label have been swapped.
- bool swapped_language_placeholders_;
-
- NotificationRegistrar notification_registrar_;
-
- // Changes the color of the background from blue to red and back.
- scoped_ptr<SlideAnimation> error_animation_;
-
- DISALLOW_COPY_AND_ASSIGN(TranslateInfoBar);
-};
-
-#endif // CHROME_BROWSER_GTK_TRANSLATE_INFOBARS_H_
diff --git a/chrome/browser/tab_contents/render_view_context_menu.cc b/chrome/browser/tab_contents/render_view_context_menu.cc
index ef4e6d7..30e004a 100644
--- a/chrome/browser/tab_contents/render_view_context_menu.cc
+++ b/chrome/browser/tab_contents/render_view_context_menu.cc
@@ -35,7 +35,7 @@
#include "chrome/browser/tab_contents/navigation_entry.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/translate/translate_manager.h"
-#if defined(OS_WIN)
+#if defined(OS_WIN) || defined(OS_LINUX)
#include "chrome/browser/translate/translate_manager2.h"
#endif
#include "chrome/common/chrome_switches.h"
@@ -449,7 +449,11 @@ void RenderViewContextMenu::AppendPageItems() {
menu_model_.AddItemWithStringId(IDC_PRINT, IDS_CONTENT_CONTEXT_PRINT);
std::string locale = g_browser_process->GetApplicationLocale();
+#if defined(OS_WIN) || defined(OS_LINUX)
+ locale = TranslateManager2::GetLanguageCode(locale);
+#else
locale = TranslateManager::GetLanguageCode(locale);
+#endif
string16 language = l10n_util::GetDisplayNameForLocale(locale, locale, true);
menu_model_.AddItem(
IDC_CONTENT_CONTEXT_TRANSLATE,
@@ -723,11 +727,19 @@ bool RenderViewContextMenu::IsCommandIdEnabled(int id) const {
std::string original_lang =
source_tab_contents_->language_state().original_language();
std::string target_lang = g_browser_process->GetApplicationLocale();
+#if defined(OS_WIN) || defined(OS_LINUX)
+ target_lang = TranslateManager2::GetLanguageCode(target_lang);
+#else
target_lang = TranslateManager::GetLanguageCode(target_lang);
+#endif
return original_lang != target_lang &&
!source_tab_contents_->language_state().IsPageTranslated() &&
!source_tab_contents_->interstitial_page() &&
+#if defined(OS_WIN) || defined(OS_LINUX)
+ TranslateManager2::IsTranslatableURL(params_.page_url);
+#else
TranslateManager::IsTranslatableURL(params_.page_url);
+#endif
}
case IDC_CONTENT_CONTEXT_OPENLINKNEWTAB:
@@ -1132,13 +1144,17 @@ void RenderViewContextMenu::ExecuteCommand(int id) {
std::string original_lang =
source_tab_contents_->language_state().original_language();
std::string target_lang = g_browser_process->GetApplicationLocale();
+#if defined(OS_WIN) || defined(OS_LINUX)
+ target_lang = TranslateManager2::GetLanguageCode(target_lang);
+#else
target_lang = TranslateManager::GetLanguageCode(target_lang);
+#endif
// Since the user decided to translate for that language and site, clears
// any preferences for not translating them.
TranslatePrefs prefs(profile_->GetPrefs());
prefs.RemoveLanguageFromBlacklist(original_lang);
prefs.RemoveSiteFromBlacklist(params_.page_url.HostNoBrackets());
-#if defined(OS_WIN)
+#if defined(OS_WIN) || defined(OS_LINUX)
Singleton<TranslateManager2>::get()->TranslatePage(
source_tab_contents_, original_lang, target_lang);
#else
diff --git a/chrome/browser/translate/translate_infobar_delegate2.cc b/chrome/browser/translate/translate_infobar_delegate2.cc
index bc11024..d773542 100644
--- a/chrome/browser/translate/translate_infobar_delegate2.cc
+++ b/chrome/browser/translate/translate_infobar_delegate2.cc
@@ -300,9 +300,8 @@ void TranslateInfoBarDelegate2::GetAfterTranslateStrings(
strings->push_back(text.substr(offsets[1]));
}
-#if !defined(OS_WIN) && !defined(OS_CHROMEOS)
-// Necessary so we link OK on Mac and Linux while the new translate infobars
-// are being ported to these platforms.
+#if !defined(OS_WIN) && !defined(OS_LINUX)
+// Necessary so we link OK on Mac until Mac uses the new delegate as well.
InfoBar* TranslateInfoBarDelegate2::CreateInfoBar() {
return NULL;
}
diff --git a/chrome/browser/translate/translate_manager2_unittest.cc b/chrome/browser/translate/translate_manager2_unittest.cc
index 6ad168d..5f171e2 100644
--- a/chrome/browser/translate/translate_manager2_unittest.cc
+++ b/chrome/browser/translate/translate_manager2_unittest.cc
@@ -4,6 +4,7 @@
#include "chrome/browser/renderer_host/test/test_render_view_host.h"
+#include "base/utf_string_conversions.h"
#include "chrome/app/chrome_dll_resource.h"
#include "chrome/browser/renderer_host/mock_render_process_host.h"
#include "chrome/browser/tab_contents/render_view_context_menu.h"
@@ -41,17 +42,18 @@ class TranslateManager2Test : public RenderViewHostTestHarness,
// Simluates navigating to a page and getting the page contents and language
// for that navigation.
void SimulateNavigation(const GURL& url, int page_id,
- const std::wstring& contents,
+ const std::string& contents,
const std::string& lang) {
NavigateAndCommit(url);
SimulateOnPageContents(url, page_id, contents, lang);
}
void SimulateOnPageContents(const GURL& url, int page_id,
- const std::wstring& contents,
+ const std::string& contents,
const std::string& lang) {
rvh()->TestOnMessageReceived(ViewHostMsg_PageContents(0, url, page_id,
- contents, lang));
+ UTF8ToUTF16(contents),
+ lang));
}
bool GetTranslateMessage(int* page_id,
@@ -264,7 +266,7 @@ class TestRenderViewContextMenu : public RenderViewContextMenu {
TEST_F(TranslateManager2Test, NormalTranslate) {
// Simulate navigating to a page.
- SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr");
// We should have an infobar.
TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar();
@@ -274,7 +276,7 @@ TEST_F(TranslateManager2Test, NormalTranslate) {
// Simulate clicking translate.
process()->sink().ClearMessages();
infobar->Translate();
-
+
// Simulate the translate script being retrieved (it only needs to be done
// once in the test as it is cached).
SimulateURLFetch(true);
@@ -318,7 +320,7 @@ TEST_F(TranslateManager2Test, NormalTranslate) {
TEST_F(TranslateManager2Test, TranslateScriptNotAvailable) {
// Simulate navigating to a page.
- SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr");
// We should have an infobar.
TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar();
@@ -398,7 +400,7 @@ TEST_F(TranslateManager2Test, TestAllLanguages) {
// Simulate navigating to a page.
NavigateAndCommit(url);
- SimulateOnPageContents(url, i, L"", lang);
+ SimulateOnPageContents(url, i, "", lang);
// Verify we have/don't have an info-bar as expected.
infobar = GetTranslateInfoBar();
@@ -413,7 +415,7 @@ TEST_F(TranslateManager2Test, TestAllLanguages) {
// Tests auto-translate on page.
TEST_F(TranslateManager2Test, AutoTranslateOnNavigate) {
// Simulate navigating to a page and getting its language.
- SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr");
// Simulate the user translating.
TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar();
@@ -426,7 +428,7 @@ TEST_F(TranslateManager2Test, AutoTranslateOnNavigate) {
// Now navigate to a new page in the same language.
process()->sink().ClearMessages();
- SimulateNavigation(GURL("http://news.google.fr"), 1, L"Les news", "fr");
+ SimulateNavigation(GURL("http://news.google.fr"), 1, "Les news", "fr");
// This should have automatically triggered a translation.
int page_id = 0;
@@ -438,7 +440,7 @@ TEST_F(TranslateManager2Test, AutoTranslateOnNavigate) {
// Now navigate to a page in a different language.
process()->sink().ClearMessages();
- SimulateNavigation(GURL("http://news.google.es"), 1, L"Las news", "es");
+ SimulateNavigation(GURL("http://news.google.es"), 1, "Las news", "es");
// This should not have triggered a translate.
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
@@ -447,28 +449,28 @@ TEST_F(TranslateManager2Test, AutoTranslateOnNavigate) {
// Tests that multiple OnPageContents do not cause multiple infobars.
TEST_F(TranslateManager2Test, MultipleOnPageContents) {
// Simulate navigating to a page and getting its language.
- SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr");
// Simulate clicking 'Nope' (don't translate).
EXPECT_TRUE(DenyTranslation());
EXPECT_EQ(0, contents()->infobar_delegate_count());
// Send a new PageContents, we should not show an infobar.
- SimulateOnPageContents(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
+ SimulateOnPageContents(GURL("http://www.google.fr"), 0, "Le Google", "fr");
EXPECT_EQ(0, contents()->infobar_delegate_count());
// Do the same steps but simulate closing the infobar this time.
- SimulateNavigation(GURL("http://www.youtube.fr"), 1, L"Le YouTube", "fr");
+ SimulateNavigation(GURL("http://www.youtube.fr"), 1, "Le YouTube", "fr");
EXPECT_TRUE(CloseTranslateInfoBar());
EXPECT_EQ(0, contents()->infobar_delegate_count());
- SimulateOnPageContents(GURL("http://www.youtube.fr"), 1, L"Le YouTube", "fr");
+ SimulateOnPageContents(GURL("http://www.youtube.fr"), 1, "Le YouTube", "fr");
EXPECT_EQ(0, contents()->infobar_delegate_count());
}
// Test that reloading the page brings back the infobar.
TEST_F(TranslateManager2Test, Reload) {
// Simulate navigating to a page and getting its language.
- SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr");
// Close the infobar.
EXPECT_TRUE(CloseTranslateInfoBar());
@@ -495,7 +497,7 @@ TEST_F(TranslateManager2Test, ReloadFromLocationBar) {
GURL url("http://www.google.fr");
// Simulate navigating to a page and getting its language.
- SimulateNavigation(url, 0, L"Le Google", "fr");
+ SimulateNavigation(url, 0, "Le Google", "fr");
// Close the infobar.
EXPECT_TRUE(CloseTranslateInfoBar());
@@ -523,34 +525,34 @@ TEST_F(TranslateManager2Test, ReloadFromLocationBar) {
// in-page.
TEST_F(TranslateManager2Test, CloseInfoBarInPageNavigation) {
// Simulate navigating to a page and getting its language.
- SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr");
// Close the infobar.
EXPECT_TRUE(CloseTranslateInfoBar());
// Navigate in page, no infobar should be shown.
- SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, "Le Google", "fr");
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
// Navigate out of page, a new infobar should show.
- SimulateNavigation(GURL("http://www.google.fr/foot"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr/foot"), 0, "Le Google", "fr");
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
// Tests that denying translation is sticky when navigating in page.
TEST_F(TranslateManager2Test, DenyTranslateInPageNavigation) {
// Simulate navigating to a page and getting its language.
- SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr");
// Simulate clicking 'Nope' (don't translate).
EXPECT_TRUE(DenyTranslation());
// Navigate in page, no infobar should be shown.
- SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, "Le Google", "fr");
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
// Navigate out of page, a new infobar should show.
- SimulateNavigation(GURL("http://www.google.fr/foot"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr/foot"), 0, "Le Google", "fr");
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
@@ -558,7 +560,7 @@ TEST_F(TranslateManager2Test, DenyTranslateInPageNavigation) {
// return when navigating in page.
TEST_F(TranslateManager2Test, TranslateCloseInfoBarInPageNavigation) {
// Simulate navigating to a page and getting its language.
- SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr");
// Simulate the user translating.
TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar();
@@ -572,14 +574,14 @@ TEST_F(TranslateManager2Test, TranslateCloseInfoBarInPageNavigation) {
EXPECT_TRUE(CloseTranslateInfoBar());
// Navigate in page, no infobar should be shown.
- SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, "Le Google", "fr");
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
// Navigate out of page, a new infobar should show.
// Note that we navigate to a page in a different language so we don't trigger
// the auto-translate feature (it would translate the page automatically and
// the before translate inforbar would not be shown).
- SimulateNavigation(GURL("http://www.google.de"), 0, L"Das Google", "de");
+ SimulateNavigation(GURL("http://www.google.de"), 0, "Das Google", "de");
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
}
@@ -587,7 +589,7 @@ TEST_F(TranslateManager2Test, TranslateCloseInfoBarInPageNavigation) {
// in-page.
TEST_F(TranslateManager2Test, TranslateInPageNavigation) {
// Simulate navigating to a page and getting its language.
- SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr");
// Simulate the user translating.
TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar();
@@ -602,14 +604,14 @@ TEST_F(TranslateManager2Test, TranslateInPageNavigation) {
// Navigate in page, the same infobar should still be shown.
ClearRemovedInfoBars();
- SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr/#ref1"), 0, "Le Google", "fr");
EXPECT_FALSE(InfoBarRemoved());
EXPECT_EQ(infobar, GetTranslateInfoBar());
// Navigate out of page, a new infobar should show.
// See note in TranslateCloseInfoBarInPageNavigation test on why it is
// important to navigate to a page in a different language for this test.
- SimulateNavigation(GURL("http://www.google.de"), 0, L"Das Google", "de");
+ SimulateNavigation(GURL("http://www.google.de"), 0, "Das Google", "de");
// The old infobar is gone.
EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar));
// And there is a new one.
@@ -620,7 +622,7 @@ TEST_F(TranslateManager2Test, TranslateInPageNavigation) {
// unsupported language.
TEST_F(TranslateManager2Test, UnsupportedPageLanguage) {
// Simulate navigating to a page and getting an unsupported language.
- SimulateNavigation(GURL("http://www.google.com"), 0, L"Google", "qbz");
+ SimulateNavigation(GURL("http://www.google.com"), 0, "Google", "qbz");
// No info-bar should be shown.
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
@@ -636,7 +638,7 @@ TEST_F(TranslateManager2Test, UnsupportedUILanguage) {
// Simulate navigating to a page in a language supported by the translate
// server.
- SimulateNavigation(GURL("http://www.google.com"), 0, L"Google", "en");
+ SimulateNavigation(GURL("http://www.google.com"), 0, "Google", "en");
// No info-bar should be shown.
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
@@ -651,7 +653,7 @@ TEST_F(TranslateManager2Test, TranslateEnabledPref) {
prefs->SetBoolean(prefs::kEnableTranslate, true);
// Simulate navigating to a page and getting its language.
- SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr");
// An infobar should be shown.
TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar();
@@ -668,7 +670,7 @@ TEST_F(TranslateManager2Test, TranslateEnabledPref) {
// Simulate getting the page contents and language, that should not trigger
// a translate infobar.
- SimulateOnPageContents(url, 1, L"Le YouTube", "fr");
+ SimulateOnPageContents(url, 1, "Le YouTube", "fr");
infobar = GetTranslateInfoBar();
EXPECT_TRUE(infobar == NULL);
}
@@ -677,7 +679,7 @@ TEST_F(TranslateManager2Test, TranslateEnabledPref) {
TEST_F(TranslateManager2Test, NeverTranslateLanguagePref) {
// Simulate navigating to a page and getting its language.
GURL url("http://www.google.fr");
- SimulateNavigation(url, 0, L"Le Google", "fr");
+ SimulateNavigation(url, 0, "Le Google", "fr");
// An infobar should be shown.
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
@@ -698,7 +700,7 @@ TEST_F(TranslateManager2Test, NeverTranslateLanguagePref) {
EXPECT_TRUE(CloseTranslateInfoBar());
// Navigate to a new page also in French.
- SimulateNavigation(GURL("http://wwww.youtube.fr"), 1, L"Le YouTube", "fr");
+ SimulateNavigation(GURL("http://wwww.youtube.fr"), 1, "Le YouTube", "fr");
// There should not be a translate infobar.
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
@@ -710,7 +712,7 @@ TEST_F(TranslateManager2Test, NeverTranslateLanguagePref) {
EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url));
// Navigate to a page in French.
- SimulateNavigation(url, 2, L"Le Google", "fr");
+ SimulateNavigation(url, 2, "Le Google", "fr");
// There should be a translate infobar.
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
@@ -723,7 +725,7 @@ TEST_F(TranslateManager2Test, NeverTranslateSitePref) {
// Simulate navigating to a page and getting its language.
GURL url("http://www.google.fr");
std::string host(url.host());
- SimulateNavigation(url, 0, L"Le Google", "fr");
+ SimulateNavigation(url, 0, "Le Google", "fr");
// An infobar should be shown.
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
@@ -744,7 +746,7 @@ TEST_F(TranslateManager2Test, NeverTranslateSitePref) {
EXPECT_TRUE(CloseTranslateInfoBar());
// Navigate to a new page also on the same site.
- SimulateNavigation(GURL("http://www.google.fr/hello"), 1, L"Bonjour", "fr");
+ SimulateNavigation(GURL("http://www.google.fr/hello"), 1, "Bonjour", "fr");
// There should not be a translate infobar.
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
@@ -756,7 +758,7 @@ TEST_F(TranslateManager2Test, NeverTranslateSitePref) {
EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url));
// Navigate to a page in French.
- SimulateNavigation(url, 0, L"Le Google", "fr");
+ SimulateNavigation(url, 0, "Le Google", "fr");
// There should be a translate infobar.
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
@@ -775,7 +777,7 @@ TEST_F(TranslateManager2Test, AlwaysTranslateLanguagePref) {
translate_prefs.WhitelistLanguagePair("fr", "en");
// Load a page in French.
- SimulateNavigation(GURL("http://www.google.fr"), 0, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr"), 0, "Le Google", "fr");
// It should have triggered an automatic translation to English.
SimulateURLFetch(true); // Simulate the translate script being retrieved.
@@ -791,7 +793,7 @@ TEST_F(TranslateManager2Test, AlwaysTranslateLanguagePref) {
EXPECT_TRUE(GetTranslateInfoBar() == NULL);
// Try another language, it should not be autotranslated.
- SimulateNavigation(GURL("http://www.google.es"), 1, L"El Google", "es");
+ SimulateNavigation(GURL("http://www.google.es"), 1, "El Google", "es");
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
EXPECT_TRUE(CloseTranslateInfoBar());
@@ -801,7 +803,7 @@ TEST_F(TranslateManager2Test, AlwaysTranslateLanguagePref) {
TestingProfile* test_profile =
static_cast<TestingProfile*>(contents()->profile());
test_profile->set_off_the_record(true);
- SimulateNavigation(GURL("http://www.youtube.fr"), 2, L"Le YouTube", "fr");
+ SimulateNavigation(GURL("http://www.youtube.fr"), 2, "Le YouTube", "fr");
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
EXPECT_TRUE(CloseTranslateInfoBar());
@@ -811,103 +813,12 @@ TEST_F(TranslateManager2Test, AlwaysTranslateLanguagePref) {
// behavior, which is show an infobar.
SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists);
translate_prefs.RemoveLanguagePairFromWhitelist("fr", "en");
- SimulateNavigation(GURL("http://www.google.fr"), 3, L"Le Google", "fr");
+ SimulateNavigation(GURL("http://www.google.fr"), 3, "Le Google", "fr");
EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
EXPECT_TRUE(GetTranslateInfoBar() != NULL);
prefs->RemovePrefObserver(TranslatePrefs::kPrefTranslateWhitelists,
&pref_observer_);
}
-/*
-
-// Context menu.
-TEST_F(TranslateManager2Test, ContextMenu) {
- // Blacklist www.google.fr and French for translation.
- GURL url("http://www.google.fr");
- TranslatePrefs translate_prefs(contents()->profile()->GetPrefs());
- translate_prefs.BlacklistLanguage("fr");
- translate_prefs.BlacklistSite(url.host());
- EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr"));
- EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(url.host()));
-
- // Simulate navigating to a page in French. The translate menu should show.
- SimulateNavigation(url, 0, L"Le Google", "fr");
- scoped_ptr<TestRenderViewContextMenu> menu(
- TestRenderViewContextMenu::CreateContextMenu(contents()));
- menu->Init();
- EXPECT_TRUE(menu->IsItemPresent(IDS_CONTENT_CONTEXT_TRANSLATE));
- EXPECT_TRUE(menu->TestIsCommandIdEnabled(IDS_CONTENT_CONTEXT_TRANSLATE));
-
- // Use the menu to translate the page.
- menu->TestExecuteItemCommand(IDS_CONTENT_CONTEXT_TRANSLATE);
-
- // That should have triggered a translation.
- SimulateURLFetch(true); // Simulate the translate script being retrieved.
- int page_id = 0;
- std::string original_lang, target_lang;
- EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
- EXPECT_EQ(0, page_id);
- EXPECT_EQ("fr", original_lang);
- EXPECT_EQ("en", target_lang);
- process()->sink().ClearMessages();
-
- // This should also have reverted the blacklisting of this site and language.
- EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr"));
- EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host()));
-
- // Let's simulate the page being translated.
- rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "fr", "en",
- TranslateErrors::NONE));
-
- // The translate menu should now be disabled.
- menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
- menu->Init();
- EXPECT_TRUE(menu->IsItemPresent(IDS_CONTENT_CONTEXT_TRANSLATE));
- EXPECT_FALSE(menu->TestIsCommandIdEnabled(IDS_CONTENT_CONTEXT_TRANSLATE));
-
- // Test that selecting translate in the context menu WHILE the page is being
- // translated does nothing (this could happen if autotranslate kicks-in and
- // the user selects the menu while the translation is being performed).
- SimulateNavigation(GURL("http://www.google.es"), 1, L"El Google", "es");
- TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar();
- ASSERT_TRUE(infobar != NULL);
- infobar->Translate();
- EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
- EXPECT_EQ(1, page_id);
- process()->sink().ClearMessages();
- menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
- menu->Init();
- EXPECT_TRUE(menu->TestIsCommandIdEnabled(IDS_CONTENT_CONTEXT_TRANSLATE));
- menu->TestExecuteItemCommand(IDS_CONTENT_CONTEXT_TRANSLATE);
- // No message expected since the translation should have been ignored.
- EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
-
- // Now test that selecting translate in the context menu AFTER the page has
- // been translated does nothing.
- SimulateNavigation(GURL("http://www.google.de"), 2, L"Das Google", "de");
- infobar = GetTranslateInfoBar();
- ASSERT_TRUE(infobar != NULL);
- infobar->Translate();
- EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
- EXPECT_EQ(2, page_id);
- process()->sink().ClearMessages();
- menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
- menu->Init();
- EXPECT_TRUE(menu->TestIsCommandIdEnabled(IDS_CONTENT_CONTEXT_TRANSLATE));
- rvh()->TestOnMessageReceived(ViewHostMsg_PageTranslated(0, 0, "de", "en",
- TranslateErrors::NONE));
- menu->TestExecuteItemCommand(IDS_CONTENT_CONTEXT_TRANSLATE);
- // No message expected since the translation should have been ignored.
- EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
-
- // Test that the translate context menu is disabled when the page is in the
- // same language as the UI.
- SimulateNavigation(url, 0, L"Google", "en");
- menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
- menu->Init();
- EXPECT_TRUE(menu->IsItemPresent(IDS_CONTENT_CONTEXT_TRANSLATE));
- EXPECT_FALSE(menu->TestIsCommandIdEnabled(IDS_CONTENT_CONTEXT_TRANSLATE));
-}
-*/
// Context menu.
TEST_F(TranslateManager2Test, ContextMenu) {
@@ -920,7 +831,7 @@ TEST_F(TranslateManager2Test, ContextMenu) {
EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(url.host()));
// Simulate navigating to a page in French. The translate menu should show.
- SimulateNavigation(url, 0, L"Le Google", "fr");
+ SimulateNavigation(url, 0, "Le Google", "fr");
scoped_ptr<TestRenderViewContextMenu> menu(
TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
@@ -957,7 +868,7 @@ TEST_F(TranslateManager2Test, ContextMenu) {
// Test that selecting translate in the context menu WHILE the page is being
// translated does nothing (this could happen if autotranslate kicks-in and
// the user selects the menu while the translation is being performed).
- SimulateNavigation(GURL("http://www.google.es"), 1, L"El Google", "es");
+ SimulateNavigation(GURL("http://www.google.es"), 1, "El Google", "es");
TranslateInfoBarDelegate2* infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
@@ -973,7 +884,7 @@ TEST_F(TranslateManager2Test, ContextMenu) {
// Now test that selecting translate in the context menu AFTER the page has
// been translated does nothing.
- SimulateNavigation(GURL("http://www.google.de"), 2, L"Das Google", "de");
+ SimulateNavigation(GURL("http://www.google.de"), 2, "Das Google", "de");
infobar = GetTranslateInfoBar();
ASSERT_TRUE(infobar != NULL);
infobar->Translate();
@@ -991,7 +902,7 @@ TEST_F(TranslateManager2Test, ContextMenu) {
// Test that the translate context menu is disabled when the page is in the
// same language as the UI.
- SimulateNavigation(url, 0, L"Google", "en");
+ SimulateNavigation(url, 0, "Google", "en");
menu.reset(TestRenderViewContextMenu::CreateContextMenu(contents()));
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));