[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 1 | // Copyright (c) 2009 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 <gtk/gtk.h> |
| 6 | |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 7 | #include "app/l10n_util.h" |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 8 | #include "base/rand_util.h" |
| 9 | #include "base/string_util.h" |
| 10 | #include "chrome/browser/browser_list.h" |
| 11 | #include "chrome/browser/browser_window.h" |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_install_ui.h" |
[email protected] | 16d51df | 2010-03-02 09:16:44 | [diff] [blame] | 13 | #include "chrome/browser/gtk/browser_window_gtk.h" |
| 14 | #include "chrome/browser/gtk/gtk_util.h" |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 15 | #include "chrome/common/extensions/extension.h" |
[email protected] | 5c7293a | 2010-03-17 06:40:57 | [diff] [blame] | 16 | #include "gfx/gtk_util.h" |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 17 | #include "grit/generated_resources.h" |
| 18 | |
| 19 | class Profile; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | const int kRightColumnWidth = 290; |
| 24 | |
| 25 | // Left or right margin. |
| 26 | const int kPanelHorizMargin = 13; |
| 27 | |
| 28 | GtkWidget* MakeMarkupLabel(const char* format, const std::string& str) { |
| 29 | GtkWidget* label = gtk_label_new(NULL); |
| 30 | char* markup = g_markup_printf_escaped(format, str.c_str()); |
| 31 | gtk_label_set_markup(GTK_LABEL(label), markup); |
| 32 | g_free(markup); |
| 33 | |
| 34 | // Left align it. |
| 35 | gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); |
| 36 | |
| 37 | return label; |
| 38 | } |
| 39 | |
| 40 | void OnDialogResponse(GtkDialog* dialog, int response_id, |
| 41 | ExtensionInstallUI::Delegate* delegate) { |
| 42 | if (response_id == GTK_RESPONSE_ACCEPT) { |
[email protected] | 21217ca | 2010-01-19 07:18:45 | [diff] [blame] | 43 | delegate->InstallUIProceed(false); |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 44 | } else { |
[email protected] | a487176 | 2009-11-10 03:27:39 | [diff] [blame] | 45 | delegate->InstallUIAbort(); |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | gtk_widget_destroy(GTK_WIDGET(dialog)); |
| 49 | } |
| 50 | |
| 51 | void ShowInstallPromptDialog(GtkWindow* parent, SkBitmap* skia_icon, |
| 52 | Extension *extension, |
[email protected] | 2687c38 | 2009-09-14 22:53:50 | [diff] [blame] | 53 | ExtensionInstallUI::Delegate *delegate, |
[email protected] | 1716438 | 2009-12-09 23:53:18 | [diff] [blame] | 54 | const string16& warning_text, |
[email protected] | 3a91eff | 2010-03-17 00:16:02 | [diff] [blame] | 55 | ExtensionInstallUI::PromptType type) { |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 56 | // Build the dialog. |
[email protected] | 3a91eff | 2010-03-17 00:16:02 | [diff] [blame] | 57 | int title_id = ExtensionInstallUI::kTitleIds[type]; |
| 58 | int button_id = ExtensionInstallUI::kButtonIds[type]; |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 59 | GtkWidget* dialog = gtk_dialog_new_with_buttons( |
[email protected] | a487176 | 2009-11-10 03:27:39 | [diff] [blame] | 60 | l10n_util::GetStringUTF8(title_id).c_str(), |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 61 | parent, |
| 62 | GTK_DIALOG_MODAL, |
| 63 | GTK_STOCK_CANCEL, |
| 64 | GTK_RESPONSE_CLOSE, |
[email protected] | b5ab7234 | 2009-11-16 22:25:28 | [diff] [blame] | 65 | l10n_util::GetStringUTF8(button_id).c_str(), |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 66 | GTK_RESPONSE_ACCEPT, |
| 67 | NULL); |
| 68 | gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE); |
| 69 | |
[email protected] | 2687c38 | 2009-09-14 22:53:50 | [diff] [blame] | 70 | // Create a two column layout. |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 71 | GtkWidget* content_area = GTK_DIALOG(dialog)->vbox; |
| 72 | gtk_box_set_spacing(GTK_BOX(content_area), gtk_util::kContentAreaSpacing); |
| 73 | |
[email protected] | 2687c38 | 2009-09-14 22:53:50 | [diff] [blame] | 74 | GtkWidget* icon_hbox = gtk_hbox_new(FALSE, gtk_util::kContentAreaSpacing); |
| 75 | gtk_box_pack_start(GTK_BOX(content_area), icon_hbox, TRUE, TRUE, 0); |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 76 | |
[email protected] | 2687c38 | 2009-09-14 22:53:50 | [diff] [blame] | 77 | // Put Icon in the left column. |
| 78 | GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(skia_icon); |
| 79 | GtkWidget* icon = gtk_image_new_from_pixbuf(pixbuf); |
[email protected] | 576d789 | 2010-03-23 02:31:58 | [diff] [blame] | 80 | g_object_unref(pixbuf); |
[email protected] | 2687c38 | 2009-09-14 22:53:50 | [diff] [blame] | 81 | gtk_box_pack_start(GTK_BOX(icon_hbox), icon, TRUE, TRUE, 0); |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 82 | |
[email protected] | 2687c38 | 2009-09-14 22:53:50 | [diff] [blame] | 83 | // Create a new vbox for the right column. |
| 84 | GtkWidget* right_column_area = gtk_vbox_new(FALSE, 0); |
| 85 | gtk_box_pack_start(GTK_BOX(icon_hbox), right_column_area, TRUE, TRUE, 0); |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 86 | |
[email protected] | 3a91eff | 2010-03-17 00:16:02 | [diff] [blame] | 87 | int heading_id = ExtensionInstallUI::kHeadingIds[type]; |
[email protected] | 2687c38 | 2009-09-14 22:53:50 | [diff] [blame] | 88 | std::string heading_text = WideToUTF8(l10n_util::GetStringF( |
[email protected] | a487176 | 2009-11-10 03:27:39 | [diff] [blame] | 89 | heading_id, UTF8ToWide(extension->name()))); |
[email protected] | 2687c38 | 2009-09-14 22:53:50 | [diff] [blame] | 90 | GtkWidget* heading_label = MakeMarkupLabel("<span weight=\"bold\">%s</span>", |
| 91 | heading_text); |
| 92 | gtk_misc_set_alignment(GTK_MISC(heading_label), 0.0, 0.5); |
| 93 | gtk_label_set_selectable(GTK_LABEL(heading_label), TRUE); |
| 94 | gtk_box_pack_start(GTK_BOX(right_column_area), heading_label, TRUE, TRUE, 0); |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 95 | |
[email protected] | 1716438 | 2009-12-09 23:53:18 | [diff] [blame] | 96 | GtkWidget* warning_label = gtk_label_new(UTF16ToUTF8(warning_text).c_str()); |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 97 | gtk_label_set_line_wrap(GTK_LABEL(warning_label), TRUE); |
| 98 | gtk_widget_set_size_request(warning_label, kRightColumnWidth, -1); |
| 99 | gtk_misc_set_alignment(GTK_MISC(warning_label), 0.0, 0.5); |
| 100 | gtk_label_set_selectable(GTK_LABEL(warning_label), TRUE); |
| 101 | gtk_box_pack_start(GTK_BOX(right_column_area), warning_label, TRUE, TRUE, 0); |
| 102 | |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 103 | g_signal_connect(dialog, "response", G_CALLBACK(OnDialogResponse), delegate); |
| 104 | gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE); |
| 105 | gtk_widget_show_all(dialog); |
| 106 | } |
| 107 | |
| 108 | } // namespace |
| 109 | |
[email protected] | a487176 | 2009-11-10 03:27:39 | [diff] [blame] | 110 | void ExtensionInstallUI::ShowExtensionInstallUIPromptImpl( |
[email protected] | b24d831 | 2009-08-27 06:47:46 | [diff] [blame] | 111 | Profile* profile, Delegate* delegate, Extension* extension, SkBitmap* icon, |
[email protected] | 3a91eff | 2010-03-17 00:16:02 | [diff] [blame] | 112 | const string16& warning_text, ExtensionInstallUI::PromptType type) { |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 113 | Browser* browser = BrowserList::GetLastActiveWithProfile(profile); |
| 114 | if (!browser) { |
[email protected] | b45c207 | 2010-01-19 00:50:49 | [diff] [blame] | 115 | delegate->InstallUIAbort(); |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
| 119 | BrowserWindowGtk* browser_window = static_cast<BrowserWindowGtk*>( |
| 120 | browser->window()); |
| 121 | if (!browser_window) { |
[email protected] | a487176 | 2009-11-10 03:27:39 | [diff] [blame] | 122 | delegate->InstallUIAbort(); |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
[email protected] | a487176 | 2009-11-10 03:27:39 | [diff] [blame] | 126 | ShowInstallPromptDialog(browser_window->window(), icon, extension, |
[email protected] | 3a91eff | 2010-03-17 00:16:02 | [diff] [blame] | 127 | delegate, warning_text, type); |
[email protected] | f7c157d | 2009-08-26 21:58:22 | [diff] [blame] | 128 | } |