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 3 // found in the LICENSE file. 4 // 5 // Currently this file is only used for the uninstall prompt. The install prompt 6 // code is in extension_install_prompt2_gtk.cc. 7 8 #include "chrome/browser/extensions/extension_uninstall_dialog.h" 9 10 #include <gtk/gtk.h> 11 12 #include "base/strings/string_util.h" 13 #include "base/strings/utf_string_conversions.h" 14 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser_window.h" 16 #include "chrome/browser/ui/gtk/browser_window_gtk.h" 17 #include "chrome/common/extensions/extension.h" 18 #include "grit/generated_resources.h" 19 #include "ui/base/gtk/gtk_hig_constants.h" 20 #include "ui/base/l10n/l10n_util.h" 21 #include "ui/gfx/gtk_util.h" 22 23 namespace { 24 25 // Left or right margin. 26 const int kPanelHorizMargin = 13; 27 28 // GTK implementation of the uninstall dialog. 29 class ExtensionUninstallDialogGtk : public ExtensionUninstallDialog { 30 public: 31 ExtensionUninstallDialogGtk(Profile* profile, 32 Browser* browser, 33 Delegate* delegate); 34 virtual ~ExtensionUninstallDialogGtk() OVERRIDE; 35 36 private: 37 virtual void Show() OVERRIDE; 38 39 CHROMEGTK_CALLBACK_1(ExtensionUninstallDialogGtk, void, OnResponse, int); 40 41 GtkWidget* dialog_; 42 }; 43 44 ExtensionUninstallDialogGtk::ExtensionUninstallDialogGtk( 45 Profile* profile, 46 Browser* browser, 47 ExtensionUninstallDialog::Delegate* delegate) 48 : ExtensionUninstallDialog(profile, browser, delegate), 49 dialog_(NULL) {} 50 51 void ExtensionUninstallDialogGtk::Show() { 52 if (!browser_) { 53 delegate_->ExtensionUninstallCanceled(); 54 return; 55 } 56 BrowserWindow* browser_window = browser_->window(); 57 if (!browser_window) { 58 delegate_->ExtensionUninstallCanceled(); 59 return; 60 } 61 62 // Build the dialog. 63 dialog_ = gtk_dialog_new_with_buttons( 64 l10n_util::GetStringUTF8(IDS_EXTENSION_UNINSTALL_PROMPT_TITLE).c_str(), 65 browser_window->GetNativeWindow(), 66 GTK_DIALOG_MODAL, 67 GTK_STOCK_CANCEL, 68 GTK_RESPONSE_CLOSE, 69 l10n_util::GetStringUTF8(IDS_EXTENSION_PROMPT_UNINSTALL_BUTTON).c_str(), 70 GTK_RESPONSE_ACCEPT, 71 NULL); 72 #if !GTK_CHECK_VERSION(2, 22, 0) 73 gtk_dialog_set_has_separator(GTK_DIALOG(dialog_), FALSE); 74 #endif 75 76 // Create a two column layout. 77 GtkWidget* content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog_)); 78 gtk_box_set_spacing(GTK_BOX(content_area), ui::kContentAreaSpacing); 79 80 GtkWidget* icon_hbox = gtk_hbox_new(FALSE, ui::kContentAreaSpacing); 81 gtk_box_pack_start(GTK_BOX(content_area), icon_hbox, TRUE, TRUE, 0); 82 83 // Put Icon in the left column. 84 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(*icon_.bitmap()); 85 GtkWidget* icon = gtk_image_new_from_pixbuf(pixbuf); 86 g_object_unref(pixbuf); 87 gtk_box_pack_start(GTK_BOX(icon_hbox), icon, TRUE, TRUE, 0); 88 89 // Create a new vbox for the right column. 90 GtkWidget* right_column_area = gtk_vbox_new(FALSE, 0); 91 gtk_box_pack_start(GTK_BOX(icon_hbox), right_column_area, TRUE, TRUE, 0); 92 93 std::string heading_text = l10n_util::GetStringFUTF8( 94 IDS_EXTENSION_UNINSTALL_PROMPT_HEADING, UTF8ToUTF16(extension_->name())); 95 GtkWidget* heading_label = gtk_label_new(heading_text.c_str()); 96 gtk_misc_set_alignment(GTK_MISC(heading_label), 0.0, 0.5); 97 gtk_box_pack_start(GTK_BOX(right_column_area), heading_label, TRUE, TRUE, 0); 98 99 g_signal_connect(dialog_, "response", G_CALLBACK(OnResponseThunk), this); 100 gtk_window_set_resizable(GTK_WINDOW(dialog_), FALSE); 101 gtk_widget_show_all(dialog_); 102 } 103 104 ExtensionUninstallDialogGtk::~ExtensionUninstallDialogGtk() { 105 delegate_ = NULL; 106 if (dialog_) { 107 gtk_widget_destroy(dialog_); 108 dialog_ = NULL; 109 } 110 } 111 112 void ExtensionUninstallDialogGtk::OnResponse( 113 GtkWidget* dialog, int response_id) { 114 CHECK_EQ(dialog_, dialog); 115 116 gtk_widget_destroy(dialog_); 117 dialog_ = NULL; 118 119 if (delegate_) { 120 if (response_id == GTK_RESPONSE_ACCEPT) 121 delegate_->ExtensionUninstallAccepted(); 122 else 123 delegate_->ExtensionUninstallCanceled(); 124 } 125 } 126 127 } // namespace 128 129 // static 130 // Platform specific implementation of the uninstall dialog show method. 131 ExtensionUninstallDialog* ExtensionUninstallDialog::Create( 132 Profile* profile, Browser* browser, Delegate* delegate) { 133 return new ExtensionUninstallDialogGtk(profile, browser, delegate); 134 } 135