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 <gtk/gtk.h> | |
6 | |
7 #include "app/l10n_util.h" | |
8 #include "base/string_util.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "chrome/browser/browser_list.h" | |
11 #include "chrome/browser/browser_window.h" | |
12 #include "chrome/browser/extensions/extension_install_ui.h" | |
13 #include "chrome/browser/gtk/browser_window_gtk.h" | |
14 #include "chrome/browser/gtk/gtk_util.h" | |
15 #include "chrome/common/extensions/extension.h" | |
16 #include "gfx/gtk_util.h" | |
17 #include "grit/generated_resources.h" | |
18 #include "skia/ext/image_operations.h" | |
19 | |
20 class Profile; | |
21 | |
22 namespace { | |
23 | |
24 const int kRightColumnMinWidth = 290; | |
25 | |
26 const int kImageSize = 69; | |
27 | |
28 // Padding on all sides of each permission in the permissions list. | |
29 const int kPermissionsPadding = 8; | |
30 | |
31 // Make a GtkLabel with |str| as its text, using the formatting in |format|. | |
32 GtkWidget* MakeMarkupLabel(const char* format, const std::string& str) { | |
33 GtkWidget* label = gtk_label_new(NULL); | |
34 char* markup = g_markup_printf_escaped(format, str.c_str()); | |
35 gtk_label_set_markup(GTK_LABEL(label), markup); | |
36 g_free(markup); | |
37 | |
38 // Left align it. | |
39 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); | |
40 | |
41 return label; | |
42 } | |
43 | |
44 void OnDialogResponse(GtkDialog* dialog, int response_id, | |
45 ExtensionInstallUI::Delegate* delegate) { | |
46 if (response_id == GTK_RESPONSE_ACCEPT) { | |
47 delegate->InstallUIProceed(); | |
48 } else { | |
49 delegate->InstallUIAbort(); | |
50 } | |
51 | |
52 gtk_widget_destroy(GTK_WIDGET(dialog)); | |
53 } | |
54 | |
55 void ShowInstallPromptDialog2(GtkWindow* parent, SkBitmap* skia_icon, | |
56 const Extension* extension, | |
57 ExtensionInstallUI::Delegate *delegate, | |
58 const std::vector<string16>& permissions) { | |
59 // Build the dialog. | |
60 GtkWidget* dialog = gtk_dialog_new_with_buttons( | |
61 l10n_util::GetStringUTF8(IDS_EXTENSION_INSTALL_PROMPT_TITLE).c_str(), | |
62 parent, | |
63 GTK_DIALOG_MODAL, | |
64 NULL); | |
65 GtkWidget* close_button = gtk_dialog_add_button(GTK_DIALOG(dialog), | |
66 GTK_STOCK_CANCEL, GTK_RESPONSE_CLOSE); | |
67 gtk_dialog_add_button(GTK_DIALOG(dialog), | |
68 l10n_util::GetStringUTF8(IDS_EXTENSION_PROMPT_INSTALL_BUTTON).c_str(), | |
69 GTK_RESPONSE_ACCEPT); | |
70 gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE); | |
71 | |
72 // Create a two column layout. | |
73 GtkWidget* content_area = GTK_DIALOG(dialog)->vbox; | |
74 gtk_box_set_spacing(GTK_BOX(content_area), gtk_util::kContentAreaSpacing); | |
75 | |
76 GtkWidget* icon_hbox = gtk_hbox_new(FALSE, gtk_util::kContentAreaSpacing); | |
77 gtk_box_pack_start(GTK_BOX(content_area), icon_hbox, TRUE, TRUE, 0); | |
78 | |
79 // Resize the icon if necessary. | |
80 SkBitmap scaled_icon = *skia_icon; | |
81 if (scaled_icon.width() > kImageSize || scaled_icon.height() > kImageSize) { | |
82 scaled_icon = skia::ImageOperations::Resize(scaled_icon, | |
83 skia::ImageOperations::RESIZE_LANCZOS3, | |
84 kImageSize, kImageSize); | |
85 } | |
86 | |
87 // Put Icon in the left column. | |
88 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&scaled_icon); | |
89 GtkWidget* icon = gtk_image_new_from_pixbuf(pixbuf); | |
90 g_object_unref(pixbuf); | |
91 gtk_box_pack_start(GTK_BOX(icon_hbox), icon, FALSE, FALSE, 0); | |
92 // Top justify the image. | |
93 gtk_misc_set_alignment(GTK_MISC(icon), 0.5, 0.0); | |
94 | |
95 // Create a new vbox for the right column. | |
96 GtkWidget* right_column_area = gtk_vbox_new(FALSE, gtk_util::kControlSpacing); | |
97 gtk_box_pack_start(GTK_BOX(icon_hbox), right_column_area, TRUE, TRUE, 0); | |
98 | |
99 std::string heading_text = l10n_util::GetStringFUTF8( | |
100 IDS_EXTENSION_INSTALL_PROMPT_HEADING, UTF8ToUTF16(extension->name())); | |
101 GtkWidget* heading_label = MakeMarkupLabel("<span weight=\"bold\">%s</span>", | |
102 heading_text); | |
103 gtk_misc_set_alignment(GTK_MISC(heading_label), 0.0, 0.5); | |
104 bool show_permissions = !permissions.empty(); | |
105 // If we are not going to show the permissions, vertically center the title. | |
106 gtk_box_pack_start(GTK_BOX(right_column_area), heading_label, | |
107 !show_permissions, !show_permissions, 0); | |
108 | |
109 if (show_permissions) { | |
110 GtkWidget* warning_label = gtk_label_new(l10n_util::GetStringUTF8( | |
111 IDS_EXTENSION_PROMPT_WILL_HAVE_ACCESS_TO).c_str()); | |
112 gtk_misc_set_alignment(GTK_MISC(warning_label), 0.0, 0.5); | |
113 gtk_util::SetLabelWidth(warning_label, kRightColumnMinWidth); | |
114 | |
115 gtk_box_pack_start(GTK_BOX(right_column_area), warning_label, | |
116 FALSE, FALSE, 0); | |
117 | |
118 GtkWidget* frame = gtk_frame_new(NULL); | |
119 gtk_box_pack_start(GTK_BOX(right_column_area), frame, FALSE, FALSE, 0); | |
120 | |
121 GtkWidget* text_view = gtk_text_view_new(); | |
122 gtk_container_add(GTK_CONTAINER(frame), text_view); | |
123 gtk_text_view_set_editable(GTK_TEXT_VIEW(text_view), FALSE); | |
124 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text_view), | |
125 kPermissionsPadding); | |
126 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text_view), | |
127 kPermissionsPadding); | |
128 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_view), GTK_WRAP_WORD); | |
129 GtkTextBuffer* buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view)); | |
130 GtkTextTagTable* tag_table = gtk_text_buffer_get_tag_table(buffer); | |
131 | |
132 GtkTextTag* padding_below_tag = gtk_text_tag_new(NULL); | |
133 g_object_set(G_OBJECT(padding_below_tag), "pixels-below-lines", | |
134 kPermissionsPadding, NULL); | |
135 g_object_set(G_OBJECT(padding_below_tag), "pixels-below-lines-set", | |
136 TRUE, NULL); | |
137 gtk_text_tag_table_add(tag_table, padding_below_tag); | |
138 g_object_unref(padding_below_tag); | |
139 GtkTextTag* padding_above_tag = gtk_text_tag_new(NULL); | |
140 g_object_set(G_OBJECT(padding_above_tag), "pixels-above-lines", | |
141 kPermissionsPadding, NULL); | |
142 g_object_set(G_OBJECT(padding_above_tag), "pixels-above-lines-set", | |
143 TRUE, NULL); | |
144 gtk_text_tag_table_add(tag_table, padding_above_tag); | |
145 g_object_unref(padding_above_tag); | |
146 | |
147 GtkTextIter end_iter; | |
148 gtk_text_buffer_get_end_iter(buffer, &end_iter); | |
149 for (std::vector<string16>::const_iterator iter = permissions.begin(); | |
150 iter != permissions.end(); ++iter) { | |
151 if (iter != permissions.begin()) | |
152 gtk_text_buffer_insert(buffer, &end_iter, "\n", -1); | |
153 gtk_text_buffer_insert_with_tags( | |
154 buffer, &end_iter, UTF16ToUTF8(*iter).c_str(), -1, | |
155 padding_below_tag, | |
156 iter == permissions.begin() ? padding_above_tag : NULL, | |
157 NULL); | |
158 } | |
159 } | |
160 | |
161 g_signal_connect(dialog, "response", G_CALLBACK(OnDialogResponse), delegate); | |
162 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE); | |
163 | |
164 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE); | |
165 gtk_widget_show_all(dialog); | |
166 gtk_widget_grab_focus(close_button); | |
167 } | |
168 | |
169 } // namespace | |
170 | |
171 void ExtensionInstallUI::ShowExtensionInstallUIPrompt2Impl( | |
172 Profile* profile, | |
173 Delegate* delegate, | |
174 const Extension* extension, | |
175 SkBitmap* icon, | |
176 const std::vector<string16>& permissions) { | |
177 Browser* browser = BrowserList::GetLastActiveWithProfile(profile); | |
178 if (!browser) { | |
179 delegate->InstallUIAbort(); | |
180 return; | |
181 } | |
182 | |
183 BrowserWindowGtk* browser_window = static_cast<BrowserWindowGtk*>( | |
184 browser->window()); | |
185 if (!browser_window) { | |
186 delegate->InstallUIAbort(); | |
187 return; | |
188 } | |
189 | |
190 ShowInstallPromptDialog2(browser_window->window(), icon, extension, | |
191 delegate, permissions); | |
192 } | |
OLD | NEW |