OLD | NEW |
| (Empty) |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/gtk/infobars/translate_infobar_base_gtk.h" | |
6 | |
7 #include "base/strings/utf_string_conversions.h" | |
8 #include "chrome/browser/translate/options_menu_model.h" | |
9 #include "chrome/browser/translate/translate_infobar_delegate.h" | |
10 #include "chrome/browser/translate/translate_tab_helper.h" | |
11 #include "chrome/browser/ui/gtk/gtk_util.h" | |
12 #include "chrome/browser/ui/gtk/infobars/after_translate_infobar_gtk.h" | |
13 #include "chrome/browser/ui/gtk/infobars/before_translate_infobar_gtk.h" | |
14 #include "chrome/browser/ui/gtk/infobars/translate_message_infobar_gtk.h" | |
15 #include "chrome/browser/ui/gtk/menu_gtk.h" | |
16 #include "grit/generated_resources.h" | |
17 #include "ui/base/gtk/gtk_signal_registrar.h" | |
18 #include "ui/base/l10n/l10n_util.h" | |
19 #include "ui/gfx/animation/slide_animation.h" | |
20 #include "ui/gfx/canvas.h" | |
21 | |
22 | |
23 // TranslateInfoBarDelegate --------------------------------------------------- | |
24 | |
25 // static | |
26 scoped_ptr<InfoBar> TranslateInfoBarDelegate::CreateInfoBar( | |
27 scoped_ptr<TranslateInfoBarDelegate> delegate) { | |
28 if (delegate->translate_step() == translate::TRANSLATE_STEP_BEFORE_TRANSLATE) | |
29 return scoped_ptr<InfoBar>(new BeforeTranslateInfoBar(delegate.Pass())); | |
30 if (delegate->translate_step() == translate::TRANSLATE_STEP_AFTER_TRANSLATE) | |
31 return scoped_ptr<InfoBar>(new AfterTranslateInfoBar(delegate.Pass())); | |
32 return scoped_ptr<InfoBar>(new TranslateMessageInfoBar(delegate.Pass())); | |
33 } | |
34 | |
35 | |
36 // TranslateInfoBarBase ------------------------------------------------------- | |
37 | |
38 TranslateInfoBarBase::TranslateInfoBarBase( | |
39 scoped_ptr<TranslateInfoBarDelegate> delegate) | |
40 : InfoBarGtk(delegate.PassAs<InfoBarDelegate>()), | |
41 background_error_percent_(0) { | |
42 TranslateInfoBarDelegate* translate_delegate = GetDelegate(); | |
43 DCHECK(translate_delegate); | |
44 TranslateInfoBarDelegate::BackgroundAnimationType animation = | |
45 translate_delegate->background_animation_type(); | |
46 if (animation != TranslateInfoBarDelegate::NONE) { | |
47 background_color_animation_.reset(new gfx::SlideAnimation(this)); | |
48 background_color_animation_->SetTweenType(gfx::Tween::LINEAR); | |
49 background_color_animation_->SetSlideDuration(500); | |
50 if (animation == TranslateInfoBarDelegate::NORMAL_TO_ERROR) { | |
51 background_color_animation_->Show(); | |
52 } else { | |
53 DCHECK_EQ(TranslateInfoBarDelegate::ERROR_TO_NORMAL, animation); | |
54 // Hide() runs the animation in reverse. | |
55 background_color_animation_->Reset(1.0); | |
56 background_color_animation_->Hide(); | |
57 } | |
58 } else { | |
59 background_error_percent_ = translate_delegate->is_error() ? 1 : 0; | |
60 } | |
61 } | |
62 | |
63 TranslateInfoBarBase::~TranslateInfoBarBase() { | |
64 } | |
65 | |
66 void TranslateInfoBarBase::AnimationProgressed( | |
67 const gfx::Animation* animation) { | |
68 DCHECK(widget()); | |
69 if (animation == background_color_animation_.get()) { | |
70 background_error_percent_ = animation->GetCurrentValue(); | |
71 // Queue the info bar widget for redisplay so it repaints its background. | |
72 gtk_widget_queue_draw(widget()); | |
73 } else { | |
74 InfoBar::AnimationProgressed(animation); | |
75 } | |
76 } | |
77 | |
78 void TranslateInfoBarBase::PlatformSpecificSetOwner() { | |
79 InfoBarGtk::PlatformSpecificSetOwner(); | |
80 | |
81 if (!ShowOptionsMenuButton()) | |
82 return; | |
83 | |
84 // The options button sits outside the translate_box so that it can be end | |
85 // packed in hbox(). | |
86 GtkWidget* options_menu_button = CreateMenuButton( | |
87 l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_OPTIONS)); | |
88 signals()->Connect(options_menu_button, "clicked", | |
89 G_CALLBACK(&OnOptionsClickedThunk), this); | |
90 gtk_widget_show_all(options_menu_button); | |
91 gtk_util::CenterWidgetInHBox(hbox(), options_menu_button, true, 0); | |
92 } | |
93 | |
94 void TranslateInfoBarBase::GetTopColor(InfoBarDelegate::Type type, | |
95 double* r, double* g, double* b) { | |
96 if (background_error_percent_ <= 0) { | |
97 InfoBarGtk::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, r, g, b); | |
98 } else if (background_error_percent_ >= 1) { | |
99 InfoBarGtk::GetTopColor(InfoBarDelegate::WARNING_TYPE, r, g, b); | |
100 } else { | |
101 double normal_r, normal_g, normal_b; | |
102 InfoBarGtk::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, | |
103 &normal_r, &normal_g, &normal_b); | |
104 | |
105 double error_r, error_g, error_b; | |
106 InfoBarGtk::GetTopColor(InfoBarDelegate::WARNING_TYPE, | |
107 &error_r, &error_g, &error_b); | |
108 | |
109 double offset_r = error_r - normal_r; | |
110 double offset_g = error_g - normal_g; | |
111 double offset_b = error_b - normal_b; | |
112 | |
113 *r = normal_r + (background_error_percent_ * offset_r); | |
114 *g = normal_g + (background_error_percent_ * offset_g); | |
115 *b = normal_b + (background_error_percent_ * offset_b); | |
116 } | |
117 } | |
118 | |
119 void TranslateInfoBarBase::GetBottomColor(InfoBarDelegate::Type type, | |
120 double* r, double* g, double* b) { | |
121 if (background_error_percent_ <= 0) { | |
122 InfoBarGtk::GetBottomColor(InfoBarDelegate::PAGE_ACTION_TYPE, r, g, b); | |
123 } else if (background_error_percent_ >= 1) { | |
124 InfoBarGtk::GetBottomColor(InfoBarDelegate::WARNING_TYPE, r, g, b); | |
125 } else { | |
126 double normal_r, normal_g, normal_b; | |
127 InfoBarGtk::GetBottomColor(InfoBarDelegate::PAGE_ACTION_TYPE, | |
128 &normal_r, &normal_g, &normal_b); | |
129 | |
130 double error_r, error_g, error_b; | |
131 InfoBarGtk::GetBottomColor(InfoBarDelegate::WARNING_TYPE, | |
132 &error_r, &error_g, &error_b); | |
133 | |
134 double offset_r = error_r - normal_r; | |
135 double offset_g = error_g - normal_g; | |
136 double offset_b = error_b - normal_b; | |
137 | |
138 *r = normal_r + (background_error_percent_ * offset_r); | |
139 *g = normal_g + (background_error_percent_ * offset_g); | |
140 *b = normal_b + (background_error_percent_ * offset_b); | |
141 } | |
142 } | |
143 | |
144 bool TranslateInfoBarBase::ShowOptionsMenuButton() const { | |
145 return false; | |
146 } | |
147 | |
148 GtkWidget* TranslateInfoBarBase::CreateLanguageCombobox( | |
149 size_t selected_language, | |
150 size_t exclude_language) { | |
151 DCHECK(selected_language != exclude_language); | |
152 | |
153 GtkListStore* model = gtk_list_store_new(LANGUAGE_COMBO_COLUMN_COUNT, | |
154 G_TYPE_INT, G_TYPE_STRING); | |
155 bool set_selection = false; | |
156 GtkTreeIter selected_iter; | |
157 TranslateInfoBarDelegate* delegate = GetDelegate(); | |
158 for (size_t i = 0; i < delegate->num_languages(); ++i) { | |
159 if (i == exclude_language) | |
160 continue; | |
161 GtkTreeIter tree_iter; | |
162 const base::string16& name = delegate->language_name_at(i); | |
163 | |
164 gtk_list_store_append(model, &tree_iter); | |
165 gtk_list_store_set(model, &tree_iter, | |
166 LANGUAGE_COMBO_COLUMN_ID, i, | |
167 LANGUAGE_COMBO_COLUMN_NAME, | |
168 base::UTF16ToUTF8(name).c_str(), | |
169 -1); | |
170 if (i == selected_language) { | |
171 selected_iter = tree_iter; | |
172 set_selection = true; | |
173 } | |
174 } | |
175 | |
176 GtkWidget* combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model)); | |
177 if (set_selection) | |
178 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combobox), &selected_iter); | |
179 g_object_unref(model); | |
180 GtkCellRenderer* renderer = gtk_cell_renderer_text_new(); | |
181 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE); | |
182 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), renderer, | |
183 "text", LANGUAGE_COMBO_COLUMN_NAME, | |
184 NULL); | |
185 return combobox; | |
186 } | |
187 | |
188 // static | |
189 size_t TranslateInfoBarBase::GetLanguageComboboxActiveId(GtkComboBox* combo) { | |
190 GtkTreeIter iter; | |
191 if (!gtk_combo_box_get_active_iter(combo, &iter)) | |
192 return 0; | |
193 | |
194 gint id = 0; | |
195 gtk_tree_model_get(gtk_combo_box_get_model(combo), &iter, | |
196 LANGUAGE_COMBO_COLUMN_ID, &id, | |
197 -1); | |
198 return static_cast<size_t>(id); | |
199 } | |
200 | |
201 TranslateInfoBarDelegate* TranslateInfoBarBase::GetDelegate() { | |
202 return static_cast<TranslateInfoBarDelegate*>(delegate()); | |
203 } | |
204 | |
205 void TranslateInfoBarBase::OnOptionsClicked(GtkWidget* sender) { | |
206 menu_model_.reset(new OptionsMenuModel(GetDelegate())); | |
207 ShowMenuWithModel(sender, NULL, menu_model_.get()); | |
208 } | |
OLD | NEW |