| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/gtk/create_application_shortcuts_dialog_gtk.h" | 5 #include "chrome/browser/gtk/create_application_shortcuts_dialog_gtk.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "app/gtk_util.h" |
| 9 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| 10 #include "base/env_var.h" | 11 #include "base/env_var.h" |
| 11 #include "chrome/browser/chrome_thread.h" | 12 #include "chrome/browser/chrome_thread.h" |
| 12 #include "chrome/browser/gtk/gtk_util.h" | 13 #include "chrome/browser/gtk/gtk_util.h" |
| 13 #include "chrome/browser/shell_integration.h" | 14 #include "chrome/browser/shell_integration.h" |
| 14 #include "chrome/browser/tab_contents/tab_contents.h" | 15 #include "chrome/browser/tab_contents/tab_contents.h" |
| 15 #include "chrome/browser/tab_contents/tab_contents_delegate.h" | 16 #include "chrome/browser/tab_contents/tab_contents_delegate.h" |
| 16 #include "chrome/browser/web_applications/web_app.h" | 17 #include "chrome/browser/web_applications/web_app.h" |
| 18 #include "gfx/gtk_util.h" |
| 17 #include "grit/chromium_strings.h" | 19 #include "grit/chromium_strings.h" |
| 18 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
| 19 #include "grit/locale_settings.h" | 21 #include "grit/locale_settings.h" |
| 20 | 22 |
| 23 namespace { |
| 24 |
| 25 // Size (in pixels) of the icon preview. |
| 26 const int kIconPreviewSizePixels = 32; |
| 27 |
| 28 // Height (in lines) of the shortcut description label. |
| 29 const int kDescriptionLabelHeightLines = 3; |
| 30 |
| 31 } // namespace |
| 32 |
| 21 // static | 33 // static |
| 22 void CreateApplicationShortcutsDialogGtk::Show(GtkWindow* parent, | 34 void CreateApplicationShortcutsDialogGtk::Show(GtkWindow* parent, |
| 23 TabContents* tab_contents) { | 35 TabContents* tab_contents) { |
| 24 new CreateApplicationShortcutsDialogGtk(parent, tab_contents); | 36 new CreateApplicationShortcutsDialogGtk(parent, tab_contents); |
| 25 } | 37 } |
| 26 | 38 |
| 27 CreateApplicationShortcutsDialogGtk::CreateApplicationShortcutsDialogGtk( | 39 CreateApplicationShortcutsDialogGtk::CreateApplicationShortcutsDialogGtk( |
| 28 GtkWindow* parent, | 40 GtkWindow* parent, |
| 29 TabContents* tab_contents) | 41 TabContents* tab_contents) |
| 30 : tab_contents_(tab_contents), | 42 : tab_contents_(tab_contents), |
| 31 error_dialog_(NULL) { | 43 error_dialog_(NULL) { |
| 32 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 44 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 33 | 45 |
| 34 // Will be balanced by Release later. | 46 // Will be balanced by Release later. |
| 35 AddRef(); | 47 AddRef(); |
| 36 | 48 |
| 49 // Get shortcut information now, it's needed for our UI. |
| 50 web_app::GetShortcutInfoForTab(tab_contents_, &shortcut_info_); |
| 51 |
| 52 // Prepare the icon. Try to scale it if it's too small, otherwise it would |
| 53 // look weird. |
| 54 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&shortcut_info_.favicon); |
| 55 int pixbuf_width = gdk_pixbuf_get_width(pixbuf); |
| 56 int pixbuf_height = gdk_pixbuf_get_height(pixbuf); |
| 57 if (pixbuf_width == pixbuf_height && pixbuf_width < kIconPreviewSizePixels) { |
| 58 // Only scale the pixbuf if it's a square (for simplicity). |
| 59 // Generally it should be square, if it's a favicon. |
| 60 // Use the highest quality interpolation. The scaling is |
| 61 // going to have low quality anyway, because the initial image |
| 62 // is likely small. |
| 63 favicon_pixbuf_ = gdk_pixbuf_scale_simple(pixbuf, |
| 64 kIconPreviewSizePixels, |
| 65 kIconPreviewSizePixels, |
| 66 GDK_INTERP_HYPER); |
| 67 g_object_unref(pixbuf); |
| 68 } else { |
| 69 favicon_pixbuf_ = pixbuf; |
| 70 } |
| 71 |
| 37 // Build the dialog. | 72 // Build the dialog. |
| 38 create_dialog_ = gtk_dialog_new_with_buttons( | 73 create_dialog_ = gtk_dialog_new_with_buttons( |
| 39 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_TITLE).c_str(), | 74 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_TITLE).c_str(), |
| 40 parent, | 75 parent, |
| 41 (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR), | 76 (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR), |
| 42 GTK_STOCK_CANCEL, | 77 GTK_STOCK_CANCEL, |
| 43 GTK_RESPONSE_REJECT, | 78 GTK_RESPONSE_REJECT, |
| 44 NULL); | 79 NULL); |
| 45 gtk_widget_realize(create_dialog_); | 80 gtk_widget_realize(create_dialog_); |
| 46 gtk_util::SetWindowSizeFromResources(GTK_WINDOW(create_dialog_), | 81 gtk_util::SetWindowSizeFromResources(GTK_WINDOW(create_dialog_), |
| 47 IDS_CREATE_SHORTCUTS_DIALOG_WIDTH_CHARS, | 82 IDS_CREATE_SHORTCUTS_DIALOG_WIDTH_CHARS, |
| 48 -1, // height | 83 -1, // height |
| 49 false); // resizable | 84 false); // resizable |
| 50 gtk_util::AddButtonToDialog(create_dialog_, | 85 gtk_util::AddButtonToDialog(create_dialog_, |
| 51 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_COMMIT).c_str(), | 86 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_COMMIT).c_str(), |
| 52 GTK_STOCK_APPLY, GTK_RESPONSE_ACCEPT); | 87 GTK_STOCK_APPLY, GTK_RESPONSE_ACCEPT); |
| 53 | 88 |
| 54 GtkWidget* content_area = GTK_DIALOG(create_dialog_)->vbox; | 89 GtkWidget* content_area = GTK_DIALOG(create_dialog_)->vbox; |
| 55 gtk_box_set_spacing(GTK_BOX(content_area), gtk_util::kContentAreaSpacing); | 90 gtk_box_set_spacing(GTK_BOX(content_area), gtk_util::kContentAreaSpacing); |
| 56 | 91 |
| 57 GtkWidget* vbox = gtk_vbox_new(FALSE, gtk_util::kControlSpacing); | 92 GtkWidget* vbox = gtk_vbox_new(FALSE, gtk_util::kControlSpacing); |
| 58 gtk_container_add(GTK_CONTAINER(content_area), vbox); | 93 gtk_container_add(GTK_CONTAINER(content_area), vbox); |
| 59 | 94 |
| 95 // Create a box containing basic information about the new shortcut: an image |
| 96 // on the left, and a description on the right. |
| 97 GtkWidget* hbox = gtk_hbox_new(FALSE, gtk_util::kControlSpacing); |
| 98 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); |
| 99 gtk_container_set_border_width(GTK_CONTAINER(hbox), |
| 100 gtk_util::kControlSpacing); |
| 101 |
| 102 // Put the icon preview in place. |
| 103 GtkWidget* favicon_image = gtk_image_new_from_pixbuf(favicon_pixbuf_); |
| 104 gtk_box_pack_start(GTK_BOX(hbox), favicon_image, FALSE, FALSE, 0); |
| 105 |
| 106 // Create the label with application shortcut description. |
| 107 GtkWidget* description_label = gtk_label_new(NULL); |
| 108 gtk_box_pack_start(GTK_BOX(hbox), description_label, FALSE, FALSE, 0); |
| 109 gtk_label_set_line_wrap(GTK_LABEL(description_label), TRUE); |
| 110 gtk_widget_realize(description_label); |
| 111 int label_height; |
| 112 gtk_util::GetWidgetSizeFromCharacters(description_label, -1, |
| 113 kDescriptionLabelHeightLines, NULL, |
| 114 &label_height); |
| 115 gtk_widget_set_size_request(description_label, -1, label_height); |
| 116 gtk_misc_set_alignment(GTK_MISC(description_label), 0, 0.5); |
| 117 std::string description(UTF16ToUTF8(shortcut_info_.description)); |
| 118 std::string title(UTF16ToUTF8(shortcut_info_.title)); |
| 119 gtk_label_set_text(GTK_LABEL(description_label), |
| 120 (description.empty() ? title : description).c_str()); |
| 121 |
| 60 // Label on top of the checkboxes. | 122 // Label on top of the checkboxes. |
| 61 GtkWidget* description = gtk_label_new( | 123 GtkWidget* checkboxes_label = gtk_label_new( |
| 62 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_LABEL).c_str()); | 124 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_LABEL).c_str()); |
| 63 gtk_misc_set_alignment(GTK_MISC(description), 0, 0); | 125 gtk_misc_set_alignment(GTK_MISC(checkboxes_label), 0, 0); |
| 64 gtk_box_pack_start(GTK_BOX(vbox), description, FALSE, FALSE, 0); | 126 gtk_box_pack_start(GTK_BOX(vbox), checkboxes_label, FALSE, FALSE, 0); |
| 65 | 127 |
| 66 // Desktop checkbox. | 128 // Desktop checkbox. |
| 67 desktop_checkbox_ = gtk_check_button_new_with_label( | 129 desktop_checkbox_ = gtk_check_button_new_with_label( |
| 68 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX).c_str()); | 130 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_DESKTOP_CHKBOX).c_str()); |
| 69 gtk_box_pack_start(GTK_BOX(vbox), desktop_checkbox_, FALSE, FALSE, 0); | 131 gtk_box_pack_start(GTK_BOX(vbox), desktop_checkbox_, FALSE, FALSE, 0); |
| 70 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(desktop_checkbox_), true); | 132 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(desktop_checkbox_), true); |
| 71 g_signal_connect(desktop_checkbox_, "toggled", | 133 g_signal_connect(desktop_checkbox_, "toggled", |
| 72 G_CALLBACK(OnToggleCheckboxThunk), this); | 134 G_CALLBACK(OnToggleCheckboxThunk), this); |
| 73 | 135 |
| 74 // Menu checkbox. | 136 // Menu checkbox. |
| 75 menu_checkbox_ = gtk_check_button_new_with_label( | 137 menu_checkbox_ = gtk_check_button_new_with_label( |
| 76 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_MENU_CHKBOX).c_str()); | 138 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_MENU_CHKBOX).c_str()); |
| 77 gtk_box_pack_start(GTK_BOX(vbox), menu_checkbox_, FALSE, FALSE, 0); | 139 gtk_box_pack_start(GTK_BOX(vbox), menu_checkbox_, FALSE, FALSE, 0); |
| 78 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(menu_checkbox_), false); | 140 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(menu_checkbox_), false); |
| 79 g_signal_connect(menu_checkbox_, "toggled", | 141 g_signal_connect(menu_checkbox_, "toggled", |
| 80 G_CALLBACK(OnToggleCheckboxThunk), this); | 142 G_CALLBACK(OnToggleCheckboxThunk), this); |
| 81 | 143 |
| 82 g_signal_connect(create_dialog_, "response", | 144 g_signal_connect(create_dialog_, "response", |
| 83 G_CALLBACK(OnCreateDialogResponseThunk), this); | 145 G_CALLBACK(OnCreateDialogResponseThunk), this); |
| 84 gtk_widget_show_all(create_dialog_); | 146 gtk_widget_show_all(create_dialog_); |
| 85 } | 147 } |
| 86 | 148 |
| 87 CreateApplicationShortcutsDialogGtk::~CreateApplicationShortcutsDialogGtk() { | 149 CreateApplicationShortcutsDialogGtk::~CreateApplicationShortcutsDialogGtk() { |
| 88 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 150 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 89 | 151 |
| 90 gtk_widget_destroy(create_dialog_); | 152 gtk_widget_destroy(create_dialog_); |
| 91 | 153 |
| 92 if (error_dialog_) | 154 if (error_dialog_) |
| 93 gtk_widget_destroy(error_dialog_); | 155 gtk_widget_destroy(error_dialog_); |
| 156 |
| 157 g_object_unref(favicon_pixbuf_); |
| 94 } | 158 } |
| 95 | 159 |
| 96 void CreateApplicationShortcutsDialogGtk::OnCreateDialogResponse( | 160 void CreateApplicationShortcutsDialogGtk::OnCreateDialogResponse( |
| 97 GtkWidget* widget, int response) { | 161 GtkWidget* widget, int response) { |
| 98 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 162 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 99 | 163 |
| 100 if (response == GTK_RESPONSE_ACCEPT) { | 164 if (response == GTK_RESPONSE_ACCEPT) { |
| 101 ShellIntegration::ShortcutInfo shortcut_info; | 165 shortcut_info_.create_on_desktop = |
| 102 web_app::GetShortcutInfoForTab(tab_contents_, &shortcut_info); | |
| 103 shortcut_info.create_on_desktop = | |
| 104 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop_checkbox_)); | 166 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop_checkbox_)); |
| 105 shortcut_info.create_in_applications_menu = | 167 shortcut_info_.create_in_applications_menu = |
| 106 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(menu_checkbox_)); | 168 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(menu_checkbox_)); |
| 107 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, | 169 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, |
| 108 NewRunnableMethod(this, | 170 NewRunnableMethod(this, |
| 109 &CreateApplicationShortcutsDialogGtk::CreateDesktopShortcut, | 171 &CreateApplicationShortcutsDialogGtk::CreateDesktopShortcut, |
| 110 shortcut_info)); | 172 shortcut_info_)); |
| 111 | 173 |
| 112 if (tab_contents_->delegate()) | 174 if (tab_contents_->delegate()) |
| 113 tab_contents_->delegate()->ConvertContentsToApplication(tab_contents_); | 175 tab_contents_->delegate()->ConvertContentsToApplication(tab_contents_); |
| 114 } else { | 176 } else { |
| 115 Release(); | 177 Release(); |
| 116 } | 178 } |
| 117 } | 179 } |
| 118 | 180 |
| 119 void CreateApplicationShortcutsDialogGtk::OnErrorDialogResponse( | 181 void CreateApplicationShortcutsDialogGtk::OnErrorDialogResponse( |
| 120 GtkWidget* widget, int response) { | 182 GtkWidget* widget, int response) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 | 246 |
| 185 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop_checkbox_)) || | 247 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop_checkbox_)) || |
| 186 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(menu_checkbox_))) { | 248 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(menu_checkbox_))) { |
| 187 can_accept = TRUE; | 249 can_accept = TRUE; |
| 188 } | 250 } |
| 189 | 251 |
| 190 gtk_dialog_set_response_sensitive(GTK_DIALOG(create_dialog_), | 252 gtk_dialog_set_response_sensitive(GTK_DIALOG(create_dialog_), |
| 191 GTK_RESPONSE_ACCEPT, | 253 GTK_RESPONSE_ACCEPT, |
| 192 can_accept); | 254 can_accept); |
| 193 } | 255 } |
| OLD | NEW |