OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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/gtk/translate/translate_infobar_base_gtk.h" | |
6 | |
7 #include "app/l10n_util.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "chrome/browser/translate/options_menu_model.h" | |
10 #include "chrome/browser/translate/translate_infobar_delegate.h" | |
11 #include "chrome/browser/gtk/translate/after_translate_infobar_gtk.h" | |
12 #include "chrome/browser/gtk/translate/before_translate_infobar_gtk.h" | |
13 #include "chrome/browser/gtk/translate/translate_message_infobar_gtk.h" | |
14 #include "chrome/browser/gtk/gtk_util.h" | |
15 #include "chrome/browser/gtk/menu_gtk.h" | |
16 #include "gfx/canvas.h" | |
17 #include "grit/generated_resources.h" | |
18 #include "ui/base/animation/slide_animation.h" | |
19 | |
20 namespace { | |
21 | |
22 // To be able to map from language id <-> entry in the combo box, we | |
23 // store the language id in the combo box data model in addition to the | |
24 // displayed name. | |
25 enum { | |
26 LANGUAGE_COMBO_COLUMN_ID, | |
27 LANGUAGE_COMBO_COLUMN_NAME, | |
28 LANGUAGE_COMBO_COLUMN_COUNT | |
29 }; | |
30 | |
31 } // namespace | |
32 | |
33 TranslateInfoBarBase::TranslateInfoBarBase(TranslateInfoBarDelegate* delegate) | |
34 : InfoBar(delegate) { | |
35 TranslateInfoBarDelegate::BackgroundAnimationType animation = | |
36 delegate->background_animation_type(); | |
37 if (animation != TranslateInfoBarDelegate::NONE) { | |
38 background_color_animation_.reset(new ui::SlideAnimation(this)); | |
39 background_color_animation_->SetTweenType(ui::Tween::LINEAR); | |
40 background_color_animation_->SetSlideDuration(500); | |
41 if (animation == TranslateInfoBarDelegate::NORMAL_TO_ERROR) { | |
42 background_color_animation_->Show(); | |
43 } else { | |
44 DCHECK_EQ(TranslateInfoBarDelegate::ERROR_TO_NORMAL, animation); | |
45 // Hide() runs the animation in reverse. | |
46 background_color_animation_->Reset(1.0); | |
47 background_color_animation_->Hide(); | |
48 } | |
49 } else { | |
50 background_error_percent_ = delegate->IsError() ? 1 : 0; | |
51 } | |
52 } | |
53 | |
54 TranslateInfoBarBase::~TranslateInfoBarBase() { | |
55 } | |
56 | |
57 void TranslateInfoBarBase::Init() { | |
58 if (!ShowOptionsMenuButton()) | |
59 return; | |
60 | |
61 // The options button sits outside the translate_box so that it can be end | |
62 // packed in hbox_. | |
63 GtkWidget* options_menu_button = BuildOptionsMenuButton(); | |
64 g_signal_connect(options_menu_button, "clicked", | |
65 G_CALLBACK(&OnOptionsClickedThunk), this); | |
66 gtk_widget_show_all(options_menu_button); | |
67 gtk_util::CenterWidgetInHBox(hbox_, options_menu_button, true, 0); | |
68 } | |
69 | |
70 void TranslateInfoBarBase::GetTopColor(InfoBarDelegate::Type type, | |
71 double* r, double* g, double *b) { | |
72 if (background_error_percent_ <= 0) { | |
73 InfoBar::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, r, g, b); | |
74 } else if (background_error_percent_ >= 1) { | |
75 InfoBar::GetTopColor(InfoBarDelegate::WARNING_TYPE, r, g, b); | |
76 } else { | |
77 double normal_r, normal_g, normal_b; | |
78 InfoBar::GetTopColor(InfoBarDelegate::PAGE_ACTION_TYPE, | |
79 &normal_r, &normal_g, &normal_b); | |
80 | |
81 double error_r, error_g, error_b; | |
82 InfoBar::GetTopColor(InfoBarDelegate::WARNING_TYPE, | |
83 &error_r, &error_g, &error_b); | |
84 | |
85 double offset_r = error_r - normal_r; | |
86 double offset_g = error_g - normal_g; | |
87 double offset_b = error_b - normal_b; | |
88 | |
89 *r = normal_r + (background_error_percent_ * offset_r); | |
90 *g = normal_g + (background_error_percent_ * offset_g); | |
91 *b = normal_b + (background_error_percent_ * offset_b); | |
92 } | |
93 } | |
94 | |
95 void TranslateInfoBarBase::GetBottomColor(InfoBarDelegate::Type type, | |
96 double* r, double* g, double *b) { | |
97 if (background_error_percent_ <= 0) { | |
98 InfoBar::GetBottomColor(InfoBarDelegate::PAGE_ACTION_TYPE, r, g, b); | |
99 } else if (background_error_percent_ >= 1) { | |
100 InfoBar::GetBottomColor(InfoBarDelegate::WARNING_TYPE, r, g, b); | |
101 } else { | |
102 double normal_r, normal_g, normal_b; | |
103 InfoBar::GetBottomColor(InfoBarDelegate::PAGE_ACTION_TYPE, | |
104 &normal_r, &normal_g, &normal_b); | |
105 | |
106 double error_r, error_g, error_b; | |
107 InfoBar::GetBottomColor(InfoBarDelegate::WARNING_TYPE, | |
108 &error_r, &error_g, &error_b); | |
109 | |
110 double offset_r = error_r - normal_r; | |
111 double offset_g = error_g - normal_g; | |
112 double offset_b = error_b - normal_b; | |
113 | |
114 *r = normal_r + (background_error_percent_ * offset_r); | |
115 *g = normal_g + (background_error_percent_ * offset_g); | |
116 *b = normal_b + (background_error_percent_ * offset_b); | |
117 } | |
118 } | |
119 | |
120 void TranslateInfoBarBase::AnimationProgressed(const ui::Animation* animation) { | |
121 DCHECK(animation == background_color_animation_.get()); | |
122 background_error_percent_ = animation->GetCurrentValue(); | |
123 // Queue the info bar widget for redisplay so it repaints its background. | |
124 gtk_widget_queue_draw(widget()); | |
125 } | |
126 | |
127 bool TranslateInfoBarBase::ShowOptionsMenuButton() const { | |
128 return false; | |
129 } | |
130 | |
131 GtkWidget* TranslateInfoBarBase::CreateLabel(const std::string& text) { | |
132 GtkWidget* label = gtk_label_new(text.c_str()); | |
133 gtk_widget_modify_fg(label, GTK_STATE_NORMAL, >k_util::kGdkBlack); | |
134 return label; | |
135 } | |
136 | |
137 GtkWidget* TranslateInfoBarBase::CreateLanguageCombobox(int selected_language, | |
138 int exclude_language) { | |
139 GtkListStore* model = gtk_list_store_new(LANGUAGE_COMBO_COLUMN_COUNT, | |
140 G_TYPE_INT, G_TYPE_STRING); | |
141 bool set_selection = false; | |
142 GtkTreeIter selected_iter; | |
143 TranslateInfoBarDelegate* delegate = GetDelegate(); | |
144 for (int i = 0; i < delegate->GetLanguageCount(); ++i) { | |
145 if (i == exclude_language) | |
146 continue; | |
147 GtkTreeIter tree_iter; | |
148 const string16& name = delegate->GetLanguageDisplayableNameAt(i); | |
149 | |
150 gtk_list_store_append(model, &tree_iter); | |
151 gtk_list_store_set(model, &tree_iter, | |
152 LANGUAGE_COMBO_COLUMN_ID, i, | |
153 LANGUAGE_COMBO_COLUMN_NAME, UTF16ToUTF8(name).c_str(), | |
154 -1); | |
155 if (i == selected_language) { | |
156 selected_iter = tree_iter; | |
157 set_selection = true; | |
158 } | |
159 } | |
160 | |
161 GtkWidget* combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model)); | |
162 if (set_selection) | |
163 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combobox), &selected_iter); | |
164 g_object_unref(model); | |
165 GtkCellRenderer* renderer = gtk_cell_renderer_text_new(); | |
166 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE); | |
167 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), renderer, | |
168 "text", LANGUAGE_COMBO_COLUMN_NAME, | |
169 NULL); | |
170 return combobox; | |
171 } | |
172 | |
173 // static | |
174 int TranslateInfoBarBase::GetLanguageComboboxActiveId(GtkComboBox* combo) { | |
175 GtkTreeIter iter; | |
176 if (!gtk_combo_box_get_active_iter(combo, &iter)) | |
177 return 0; | |
178 | |
179 gint id = 0; | |
180 gtk_tree_model_get(gtk_combo_box_get_model(combo), &iter, | |
181 LANGUAGE_COMBO_COLUMN_ID, &id, | |
182 -1); | |
183 return id; | |
184 } | |
185 | |
186 TranslateInfoBarDelegate* TranslateInfoBarBase::GetDelegate() const { | |
187 return static_cast<TranslateInfoBarDelegate*>(delegate()); | |
188 } | |
189 | |
190 // static | |
191 GtkWidget* TranslateInfoBarBase::BuildOptionsMenuButton() { | |
192 GtkWidget* button = gtk_button_new(); | |
193 GtkWidget* former_child = gtk_bin_get_child(GTK_BIN(button)); | |
194 if (former_child) | |
195 gtk_container_remove(GTK_CONTAINER(button), former_child); | |
196 | |
197 GtkWidget* hbox = gtk_hbox_new(FALSE, 0); | |
198 | |
199 GtkWidget* label = gtk_label_new( | |
200 l10n_util::GetStringUTF8(IDS_TRANSLATE_INFOBAR_OPTIONS).c_str()); | |
201 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); | |
202 | |
203 GtkWidget* arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE); | |
204 gtk_box_pack_start(GTK_BOX(hbox), arrow, FALSE, FALSE, 0); | |
205 | |
206 gtk_container_add(GTK_CONTAINER(button), hbox); | |
207 | |
208 return button; | |
209 } | |
210 | |
211 void TranslateInfoBarBase::OnOptionsClicked(GtkWidget* sender) { | |
212 if (!options_menu_model_.get()) { | |
213 options_menu_model_.reset(new OptionsMenuModel(GetDelegate())); | |
214 options_menu_menu_.reset(new MenuGtk(NULL, options_menu_model_.get())); | |
215 } | |
216 options_menu_menu_->Popup(sender, 1, gtk_get_current_event_time()); | |
217 } | |
218 | |
219 // TranslateInfoBarDelegate specific method: | |
220 InfoBar* TranslateInfoBarDelegate::CreateInfoBar() { | |
221 TranslateInfoBarBase* infobar = NULL; | |
222 switch (type_) { | |
223 case BEFORE_TRANSLATE: | |
224 infobar = new BeforeTranslateInfoBar(this); | |
225 break; | |
226 case AFTER_TRANSLATE: | |
227 infobar = new AfterTranslateInfoBar(this); | |
228 break; | |
229 case TRANSLATING: | |
230 case TRANSLATION_ERROR: | |
231 infobar = new TranslateMessageInfoBar(this); | |
232 break; | |
233 default: | |
234 NOTREACHED(); | |
235 } | |
236 infobar->Init(); | |
237 // Set |infobar_view_| so that the model can notify the infobar when it | |
238 // changes. | |
239 infobar_view_ = infobar; | |
240 return infobar; | |
241 } | |
OLD | NEW |