| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/gtk/infobars/translate_infobar_base_gtk.h" | 5 #include "chrome/browser/ui/gtk/infobars/translate_infobar_base_gtk.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/translate/options_menu_model.h" | 8 #include "chrome/browser/translate/options_menu_model.h" |
| 9 #include "chrome/browser/translate/translate_infobar_delegate.h" | 9 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 10 #include "chrome/browser/ui/gtk/gtk_util.h" | 10 #include "chrome/browser/ui/gtk/gtk_util.h" |
| 11 #include "chrome/browser/ui/gtk/infobars/after_translate_infobar_gtk.h" | 11 #include "chrome/browser/ui/gtk/infobars/after_translate_infobar_gtk.h" |
| 12 #include "chrome/browser/ui/gtk/infobars/before_translate_infobar_gtk.h" | 12 #include "chrome/browser/ui/gtk/infobars/before_translate_infobar_gtk.h" |
| 13 #include "chrome/browser/ui/gtk/infobars/translate_message_infobar_gtk.h" | 13 #include "chrome/browser/ui/gtk/infobars/translate_message_infobar_gtk.h" |
| 14 #include "chrome/browser/ui/gtk/menu_gtk.h" | 14 #include "chrome/browser/ui/gtk/menu_gtk.h" |
| 15 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
| 16 #include "ui/base/gtk/gtk_signal_registrar.h" | 16 #include "ui/base/gtk/gtk_signal_registrar.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/gfx/animation/slide_animation.h" | 18 #include "ui/gfx/animation/slide_animation.h" |
| 19 #include "ui/gfx/canvas.h" | 19 #include "ui/gfx/canvas.h" |
| 20 | 20 |
| 21 | 21 |
| 22 // TranslateInfoBarDelegate --------------------------------------------------- | 22 // TranslateInfoBarDelegate --------------------------------------------------- |
| 23 | 23 |
| 24 InfoBar* TranslateInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { | 24 // static |
| 25 if (infobar_type_ == BEFORE_TRANSLATE) | 25 scoped_ptr<InfoBar> TranslateInfoBarDelegate::CreateInfoBar( |
| 26 return new BeforeTranslateInfoBar(owner, this); | 26 scoped_ptr<TranslateInfoBarDelegate> delegate) { |
| 27 if (infobar_type_ == AFTER_TRANSLATE) | 27 if (delegate->infobar_type() == BEFORE_TRANSLATE) |
| 28 return new AfterTranslateInfoBar(owner, this); | 28 return scoped_ptr<InfoBar>(new BeforeTranslateInfoBar(delegate.Pass())); |
| 29 return new TranslateMessageInfoBar(owner, this); | 29 if (delegate->infobar_type() == AFTER_TRANSLATE) |
| 30 return scoped_ptr<InfoBar>(new AfterTranslateInfoBar(delegate.Pass())); |
| 31 return scoped_ptr<InfoBar>(new TranslateMessageInfoBar(delegate.Pass())); |
| 30 } | 32 } |
| 31 | 33 |
| 32 | 34 |
| 33 // TranslateInfoBarBase ------------------------------------------------------- | 35 // TranslateInfoBarBase ------------------------------------------------------- |
| 34 | 36 |
| 35 TranslateInfoBarBase::TranslateInfoBarBase(InfoBarService* owner, | 37 TranslateInfoBarBase::TranslateInfoBarBase( |
| 36 TranslateInfoBarDelegate* delegate) | 38 scoped_ptr<TranslateInfoBarDelegate> delegate) |
| 37 : InfoBarGtk(owner, delegate), | 39 : InfoBarGtk(delegate.PassAs<InfoBarDelegate>()), |
| 38 background_error_percent_(0) { | 40 background_error_percent_(0) { |
| 39 DCHECK(delegate); | 41 TranslateInfoBarDelegate* translate_delegate = GetDelegate(); |
| 42 DCHECK(translate_delegate); |
| 40 TranslateInfoBarDelegate::BackgroundAnimationType animation = | 43 TranslateInfoBarDelegate::BackgroundAnimationType animation = |
| 41 delegate->background_animation_type(); | 44 translate_delegate->background_animation_type(); |
| 42 if (animation != TranslateInfoBarDelegate::NONE) { | 45 if (animation != TranslateInfoBarDelegate::NONE) { |
| 43 background_color_animation_.reset(new gfx::SlideAnimation(this)); | 46 background_color_animation_.reset(new gfx::SlideAnimation(this)); |
| 44 background_color_animation_->SetTweenType(gfx::Tween::LINEAR); | 47 background_color_animation_->SetTweenType(gfx::Tween::LINEAR); |
| 45 background_color_animation_->SetSlideDuration(500); | 48 background_color_animation_->SetSlideDuration(500); |
| 46 if (animation == TranslateInfoBarDelegate::NORMAL_TO_ERROR) { | 49 if (animation == TranslateInfoBarDelegate::NORMAL_TO_ERROR) { |
| 47 background_color_animation_->Show(); | 50 background_color_animation_->Show(); |
| 48 } else { | 51 } else { |
| 49 DCHECK_EQ(TranslateInfoBarDelegate::ERROR_TO_NORMAL, animation); | 52 DCHECK_EQ(TranslateInfoBarDelegate::ERROR_TO_NORMAL, animation); |
| 50 // Hide() runs the animation in reverse. | 53 // Hide() runs the animation in reverse. |
| 51 background_color_animation_->Reset(1.0); | 54 background_color_animation_->Reset(1.0); |
| 52 background_color_animation_->Hide(); | 55 background_color_animation_->Hide(); |
| 53 } | 56 } |
| 54 } else { | 57 } else { |
| 55 background_error_percent_ = delegate->is_error() ? 1 : 0; | 58 background_error_percent_ = translate_delegate->is_error() ? 1 : 0; |
| 56 } | 59 } |
| 57 } | 60 } |
| 58 | 61 |
| 59 TranslateInfoBarBase::~TranslateInfoBarBase() { | 62 TranslateInfoBarBase::~TranslateInfoBarBase() { |
| 60 } | 63 } |
| 61 | 64 |
| 62 void TranslateInfoBarBase::AnimationProgressed( | 65 void TranslateInfoBarBase::AnimationProgressed( |
| 63 const gfx::Animation* animation) { | 66 const gfx::Animation* animation) { |
| 64 DCHECK(widget()); | 67 DCHECK(widget()); |
| 65 if (animation == background_color_animation_.get()) { | 68 if (animation == background_color_animation_.get()) { |
| 66 background_error_percent_ = animation->GetCurrentValue(); | 69 background_error_percent_ = animation->GetCurrentValue(); |
| 67 // Queue the info bar widget for redisplay so it repaints its background. | 70 // Queue the info bar widget for redisplay so it repaints its background. |
| 68 gtk_widget_queue_draw(widget()); | 71 gtk_widget_queue_draw(widget()); |
| 69 } else { | 72 } else { |
| 70 InfoBar::AnimationProgressed(animation); | 73 InfoBar::AnimationProgressed(animation); |
| 71 } | 74 } |
| 72 } | 75 } |
| 73 | 76 |
| 77 void TranslateInfoBarBase::PlatformSpecificSetOwner() { |
| 78 InfoBarGtk::PlatformSpecificSetOwner(); |
| 79 |
| 80 if (!ShowOptionsMenuButton()) |
| 81 return; |
| 82 |
| 83 // The options button sits outside the translate_box so that it can be end |
| 84 // packed in hbox(). |
| 85 GtkWidget* options_menu_button = CreateMenuButton( |
| 86 l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_OPTIONS)); |
| 87 signals()->Connect(options_menu_button, "clicked", |
| 88 G_CALLBACK(&OnOptionsClickedThunk), this); |
| 89 gtk_widget_show_all(options_menu_button); |
| 90 gtk_util::CenterWidgetInHBox(hbox(), options_menu_button, true, 0); |
| 91 } |
| 92 |
| 74 void TranslateInfoBarBase::GetTopColor(InfoBarDelegate::Type type, | 93 void TranslateInfoBarBase::GetTopColor(InfoBarDelegate::Type type, |
| 75 double* r, double* g, double* b) { | 94 double* r, double* g, double* b) { |
| 76 if (background_error_percent_ <= 0) { | 95 if (background_error_percent_ <= 0) { |
| 77 InfoBarGtk::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, r, g, b); | 96 InfoBarGtk::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, r, g, b); |
| 78 } else if (background_error_percent_ >= 1) { | 97 } else if (background_error_percent_ >= 1) { |
| 79 InfoBarGtk::GetTopColor(InfoBarDelegate::WARNING_TYPE, r, g, b); | 98 InfoBarGtk::GetTopColor(InfoBarDelegate::WARNING_TYPE, r, g, b); |
| 80 } else { | 99 } else { |
| 81 double normal_r, normal_g, normal_b; | 100 double normal_r, normal_g, normal_b; |
| 82 InfoBarGtk::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, | 101 InfoBarGtk::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, |
| 83 &normal_r, &normal_g, &normal_b); | 102 &normal_r, &normal_g, &normal_b); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 114 double offset_r = error_r - normal_r; | 133 double offset_r = error_r - normal_r; |
| 115 double offset_g = error_g - normal_g; | 134 double offset_g = error_g - normal_g; |
| 116 double offset_b = error_b - normal_b; | 135 double offset_b = error_b - normal_b; |
| 117 | 136 |
| 118 *r = normal_r + (background_error_percent_ * offset_r); | 137 *r = normal_r + (background_error_percent_ * offset_r); |
| 119 *g = normal_g + (background_error_percent_ * offset_g); | 138 *g = normal_g + (background_error_percent_ * offset_g); |
| 120 *b = normal_b + (background_error_percent_ * offset_b); | 139 *b = normal_b + (background_error_percent_ * offset_b); |
| 121 } | 140 } |
| 122 } | 141 } |
| 123 | 142 |
| 124 void TranslateInfoBarBase::InitWidgets() { | |
| 125 InfoBarGtk::InitWidgets(); | |
| 126 | |
| 127 if (!ShowOptionsMenuButton()) | |
| 128 return; | |
| 129 | |
| 130 // The options button sits outside the translate_box so that it can be end | |
| 131 // packed in hbox(). | |
| 132 GtkWidget* options_menu_button = CreateMenuButton( | |
| 133 l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_OPTIONS)); | |
| 134 signals()->Connect(options_menu_button, "clicked", | |
| 135 G_CALLBACK(&OnOptionsClickedThunk), this); | |
| 136 gtk_widget_show_all(options_menu_button); | |
| 137 gtk_util::CenterWidgetInHBox(hbox(), options_menu_button, true, 0); | |
| 138 } | |
| 139 | |
| 140 bool TranslateInfoBarBase::ShowOptionsMenuButton() const { | 143 bool TranslateInfoBarBase::ShowOptionsMenuButton() const { |
| 141 return false; | 144 return false; |
| 142 } | 145 } |
| 143 | 146 |
| 144 GtkWidget* TranslateInfoBarBase::CreateLanguageCombobox( | 147 GtkWidget* TranslateInfoBarBase::CreateLanguageCombobox( |
| 145 size_t selected_language, | 148 size_t selected_language, |
| 146 size_t exclude_language) { | 149 size_t exclude_language) { |
| 147 DCHECK(selected_language != exclude_language); | 150 DCHECK(selected_language != exclude_language); |
| 148 | 151 |
| 149 GtkListStore* model = gtk_list_store_new(LANGUAGE_COMBO_COLUMN_COUNT, | 152 GtkListStore* model = gtk_list_store_new(LANGUAGE_COMBO_COLUMN_COUNT, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 } | 197 } |
| 195 | 198 |
| 196 TranslateInfoBarDelegate* TranslateInfoBarBase::GetDelegate() { | 199 TranslateInfoBarDelegate* TranslateInfoBarBase::GetDelegate() { |
| 197 return static_cast<TranslateInfoBarDelegate*>(delegate()); | 200 return static_cast<TranslateInfoBarDelegate*>(delegate()); |
| 198 } | 201 } |
| 199 | 202 |
| 200 void TranslateInfoBarBase::OnOptionsClicked(GtkWidget* sender) { | 203 void TranslateInfoBarBase::OnOptionsClicked(GtkWidget* sender) { |
| 201 menu_model_.reset(new OptionsMenuModel(GetDelegate())); | 204 menu_model_.reset(new OptionsMenuModel(GetDelegate())); |
| 202 ShowMenuWithModel(sender, NULL, menu_model_.get()); | 205 ShowMenuWithModel(sender, NULL, menu_model_.get()); |
| 203 } | 206 } |
| OLD | NEW |