| 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 // static | 24 InfoBar* TranslateInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { |
| 25 scoped_ptr<InfoBar> TranslateInfoBarDelegate::CreateInfoBar( | 25 if (infobar_type_ == BEFORE_TRANSLATE) |
| 26 scoped_ptr<TranslateInfoBarDelegate> delegate) { | 26 return new BeforeTranslateInfoBar(owner, this); |
| 27 if (delegate->infobar_type() == BEFORE_TRANSLATE) | 27 if (infobar_type_ == AFTER_TRANSLATE) |
| 28 return scoped_ptr<InfoBar>(new BeforeTranslateInfoBar(delegate.Pass())); | 28 return new AfterTranslateInfoBar(owner, this); |
| 29 if (delegate->infobar_type() == AFTER_TRANSLATE) | 29 return new TranslateMessageInfoBar(owner, this); |
| 30 return scoped_ptr<InfoBar>(new AfterTranslateInfoBar(delegate.Pass())); | |
| 31 return scoped_ptr<InfoBar>(new TranslateMessageInfoBar(delegate.Pass())); | |
| 32 } | 30 } |
| 33 | 31 |
| 34 | 32 |
| 35 // TranslateInfoBarBase ------------------------------------------------------- | 33 // TranslateInfoBarBase ------------------------------------------------------- |
| 36 | 34 |
| 37 TranslateInfoBarBase::TranslateInfoBarBase( | 35 TranslateInfoBarBase::TranslateInfoBarBase(InfoBarService* owner, |
| 38 scoped_ptr<TranslateInfoBarDelegate> delegate) | 36 TranslateInfoBarDelegate* delegate) |
| 39 : InfoBarGtk(delegate.PassAs<InfoBarDelegate>()), | 37 : InfoBarGtk(owner, delegate), |
| 40 background_error_percent_(0) { | 38 background_error_percent_(0) { |
| 41 TranslateInfoBarDelegate* translate_delegate = GetDelegate(); | 39 DCHECK(delegate); |
| 42 DCHECK(translate_delegate); | |
| 43 TranslateInfoBarDelegate::BackgroundAnimationType animation = | 40 TranslateInfoBarDelegate::BackgroundAnimationType animation = |
| 44 translate_delegate->background_animation_type(); | 41 delegate->background_animation_type(); |
| 45 if (animation != TranslateInfoBarDelegate::NONE) { | 42 if (animation != TranslateInfoBarDelegate::NONE) { |
| 46 background_color_animation_.reset(new gfx::SlideAnimation(this)); | 43 background_color_animation_.reset(new gfx::SlideAnimation(this)); |
| 47 background_color_animation_->SetTweenType(gfx::Tween::LINEAR); | 44 background_color_animation_->SetTweenType(gfx::Tween::LINEAR); |
| 48 background_color_animation_->SetSlideDuration(500); | 45 background_color_animation_->SetSlideDuration(500); |
| 49 if (animation == TranslateInfoBarDelegate::NORMAL_TO_ERROR) { | 46 if (animation == TranslateInfoBarDelegate::NORMAL_TO_ERROR) { |
| 50 background_color_animation_->Show(); | 47 background_color_animation_->Show(); |
| 51 } else { | 48 } else { |
| 52 DCHECK_EQ(TranslateInfoBarDelegate::ERROR_TO_NORMAL, animation); | 49 DCHECK_EQ(TranslateInfoBarDelegate::ERROR_TO_NORMAL, animation); |
| 53 // Hide() runs the animation in reverse. | 50 // Hide() runs the animation in reverse. |
| 54 background_color_animation_->Reset(1.0); | 51 background_color_animation_->Reset(1.0); |
| 55 background_color_animation_->Hide(); | 52 background_color_animation_->Hide(); |
| 56 } | 53 } |
| 57 } else { | 54 } else { |
| 58 background_error_percent_ = translate_delegate->is_error() ? 1 : 0; | 55 background_error_percent_ = delegate->is_error() ? 1 : 0; |
| 59 } | 56 } |
| 60 } | 57 } |
| 61 | 58 |
| 62 TranslateInfoBarBase::~TranslateInfoBarBase() { | 59 TranslateInfoBarBase::~TranslateInfoBarBase() { |
| 63 } | 60 } |
| 64 | 61 |
| 65 void TranslateInfoBarBase::AnimationProgressed( | 62 void TranslateInfoBarBase::AnimationProgressed( |
| 66 const gfx::Animation* animation) { | 63 const gfx::Animation* animation) { |
| 67 DCHECK(widget()); | 64 DCHECK(widget()); |
| 68 if (animation == background_color_animation_.get()) { | 65 if (animation == background_color_animation_.get()) { |
| 69 background_error_percent_ = animation->GetCurrentValue(); | 66 background_error_percent_ = animation->GetCurrentValue(); |
| 70 // Queue the info bar widget for redisplay so it repaints its background. | 67 // Queue the info bar widget for redisplay so it repaints its background. |
| 71 gtk_widget_queue_draw(widget()); | 68 gtk_widget_queue_draw(widget()); |
| 72 } else { | 69 } else { |
| 73 InfoBar::AnimationProgressed(animation); | 70 InfoBar::AnimationProgressed(animation); |
| 74 } | 71 } |
| 75 } | 72 } |
| 76 | 73 |
| 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 | |
| 93 void TranslateInfoBarBase::GetTopColor(InfoBarDelegate::Type type, | 74 void TranslateInfoBarBase::GetTopColor(InfoBarDelegate::Type type, |
| 94 double* r, double* g, double* b) { | 75 double* r, double* g, double* b) { |
| 95 if (background_error_percent_ <= 0) { | 76 if (background_error_percent_ <= 0) { |
| 96 InfoBarGtk::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, r, g, b); | 77 InfoBarGtk::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, r, g, b); |
| 97 } else if (background_error_percent_ >= 1) { | 78 } else if (background_error_percent_ >= 1) { |
| 98 InfoBarGtk::GetTopColor(InfoBarDelegate::WARNING_TYPE, r, g, b); | 79 InfoBarGtk::GetTopColor(InfoBarDelegate::WARNING_TYPE, r, g, b); |
| 99 } else { | 80 } else { |
| 100 double normal_r, normal_g, normal_b; | 81 double normal_r, normal_g, normal_b; |
| 101 InfoBarGtk::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, | 82 InfoBarGtk::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, |
| 102 &normal_r, &normal_g, &normal_b); | 83 &normal_r, &normal_g, &normal_b); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 133 double offset_r = error_r - normal_r; | 114 double offset_r = error_r - normal_r; |
| 134 double offset_g = error_g - normal_g; | 115 double offset_g = error_g - normal_g; |
| 135 double offset_b = error_b - normal_b; | 116 double offset_b = error_b - normal_b; |
| 136 | 117 |
| 137 *r = normal_r + (background_error_percent_ * offset_r); | 118 *r = normal_r + (background_error_percent_ * offset_r); |
| 138 *g = normal_g + (background_error_percent_ * offset_g); | 119 *g = normal_g + (background_error_percent_ * offset_g); |
| 139 *b = normal_b + (background_error_percent_ * offset_b); | 120 *b = normal_b + (background_error_percent_ * offset_b); |
| 140 } | 121 } |
| 141 } | 122 } |
| 142 | 123 |
| 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 |
| 143 bool TranslateInfoBarBase::ShowOptionsMenuButton() const { | 140 bool TranslateInfoBarBase::ShowOptionsMenuButton() const { |
| 144 return false; | 141 return false; |
| 145 } | 142 } |
| 146 | 143 |
| 147 GtkWidget* TranslateInfoBarBase::CreateLanguageCombobox( | 144 GtkWidget* TranslateInfoBarBase::CreateLanguageCombobox( |
| 148 size_t selected_language, | 145 size_t selected_language, |
| 149 size_t exclude_language) { | 146 size_t exclude_language) { |
| 150 DCHECK(selected_language != exclude_language); | 147 DCHECK(selected_language != exclude_language); |
| 151 | 148 |
| 152 GtkListStore* model = gtk_list_store_new(LANGUAGE_COMBO_COLUMN_COUNT, | 149 GtkListStore* model = gtk_list_store_new(LANGUAGE_COMBO_COLUMN_COUNT, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 } | 194 } |
| 198 | 195 |
| 199 TranslateInfoBarDelegate* TranslateInfoBarBase::GetDelegate() { | 196 TranslateInfoBarDelegate* TranslateInfoBarBase::GetDelegate() { |
| 200 return static_cast<TranslateInfoBarDelegate*>(delegate()); | 197 return static_cast<TranslateInfoBarDelegate*>(delegate()); |
| 201 } | 198 } |
| 202 | 199 |
| 203 void TranslateInfoBarBase::OnOptionsClicked(GtkWidget* sender) { | 200 void TranslateInfoBarBase::OnOptionsClicked(GtkWidget* sender) { |
| 204 menu_model_.reset(new OptionsMenuModel(GetDelegate())); | 201 menu_model_.reset(new OptionsMenuModel(GetDelegate())); |
| 205 ShowMenuWithModel(sender, NULL, menu_model_.get()); | 202 ShowMenuWithModel(sender, NULL, menu_model_.get()); |
| 206 } | 203 } |
| OLD | NEW |