Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: chrome/browser/gtk/create_application_shortcuts_dialog_gtk.cc

Issue 5273004: Add "create Application Shortcut" to the app menu on NTP. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add support for GTK. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/l10n_util.h" 9 #include "app/l10n_util.h"
10 #include "base/environment.h" 10 #include "base/environment.h"
11 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/browser_thread.h" 12 #include "chrome/browser/browser_thread.h"
13 #include "chrome/browser/gtk/gtk_util.h" 13 #include "chrome/browser/gtk/gtk_util.h"
14 #include "chrome/browser/shell_integration.h" 14 #include "chrome/browser/shell_integration.h"
15 #include "chrome/browser/tab_contents/tab_contents.h" 15 #include "chrome/browser/tab_contents/tab_contents.h"
16 #include "chrome/browser/tab_contents/tab_contents_delegate.h" 16 #include "chrome/browser/tab_contents/tab_contents_delegate.h"
17 #include "chrome/browser/web_applications/web_app.h" 17 #include "chrome/browser/web_applications/web_app.h"
18 #include "chrome/common/extensions/extension.h"
19 #include "chrome/common/extensions/extension_resource.h"
18 #include "gfx/gtk_util.h" 20 #include "gfx/gtk_util.h"
19 #include "grit/chromium_strings.h" 21 #include "grit/chromium_strings.h"
20 #include "grit/generated_resources.h" 22 #include "grit/generated_resources.h"
21 #include "grit/locale_settings.h" 23 #include "grit/locale_settings.h"
22 24
23 namespace { 25 namespace {
24 26
25 // Size (in pixels) of the icon preview. 27 // Size (in pixels) of the icon preview.
26 const int kIconPreviewSizePixels = 32; 28 const int kIconPreviewSizePixels = 32;
27 29
28 // Height (in lines) of the shortcut description label. 30 // Height (in lines) of the shortcut description label.
29 const int kDescriptionLabelHeightLines = 3; 31 const int kDescriptionLabelHeightLines = 3;
30 32
31 } // namespace 33 } // namespace
32 34
33 // static 35 // static
34 void CreateApplicationShortcutsDialogGtk::Show(GtkWindow* parent, 36 void CreateWebApplicationShortcutsDialogGtk::Show(GtkWindow* parent,
35 TabContents* tab_contents) { 37 TabContents* tab_contents) {
36 new CreateApplicationShortcutsDialogGtk(parent, tab_contents); 38 new CreateWebApplicationShortcutsDialogGtk(parent, tab_contents);
37 } 39 }
38 40
41 void CreateChromeApplicationShortcutsDialogGtk::Show(GtkWindow* parent,
42 const Extension* app) {
43 new CreateChromeApplicationShortcutsDialogGtk(parent, app);
44 }
45
46
39 CreateApplicationShortcutsDialogGtk::CreateApplicationShortcutsDialogGtk( 47 CreateApplicationShortcutsDialogGtk::CreateApplicationShortcutsDialogGtk(
40 GtkWindow* parent, 48 GtkWindow* parent)
41 TabContents* tab_contents) 49 : parent_(parent),
42 : tab_contents_(tab_contents), 50 error_dialog_(NULL) {
43 error_dialog_(NULL) {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
45 52
46 // Will be balanced by Release later. 53 // Will be balanced by Release later.
47 AddRef(); 54 AddRef();
55 }
48 56
49 // Get shortcut information now, it's needed for our UI. 57 void CreateApplicationShortcutsDialogGtk::CreateIconPixBuf(
50 web_app::GetShortcutInfoForTab(tab_contents_, &shortcut_info_); 58 const SkBitmap& bitmap) {
51
52 // Prepare the icon. Try to scale it if it's too small, otherwise it would 59 // Prepare the icon. Try to scale it if it's too small, otherwise it would
53 // look weird. 60 // look weird.
54 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&shortcut_info_.favicon); 61 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&shortcut_info_.favicon);
55 int pixbuf_width = gdk_pixbuf_get_width(pixbuf); 62 int pixbuf_width = gdk_pixbuf_get_width(pixbuf);
56 int pixbuf_height = gdk_pixbuf_get_height(pixbuf); 63 int pixbuf_height = gdk_pixbuf_get_height(pixbuf);
57 if (pixbuf_width == pixbuf_height && pixbuf_width < kIconPreviewSizePixels) { 64 if (pixbuf_width == pixbuf_height && pixbuf_width < kIconPreviewSizePixels) {
58 // Only scale the pixbuf if it's a square (for simplicity). 65 // Only scale the pixbuf if it's a square (for simplicity).
59 // Generally it should be square, if it's a favicon. 66 // Generally it should be square, if it's a favicon or app icon.
60 // Use the highest quality interpolation. The scaling is 67 // Use the highest quality interpolation. The scaling is
61 // going to have low quality anyway, because the initial image 68 // going to have low quality anyway, because the initial image
62 // is likely small. 69 // is likely small.
63 favicon_pixbuf_ = gdk_pixbuf_scale_simple(pixbuf, 70 favicon_pixbuf_ = gdk_pixbuf_scale_simple(pixbuf,
64 kIconPreviewSizePixels, 71 kIconPreviewSizePixels,
65 kIconPreviewSizePixels, 72 kIconPreviewSizePixels,
66 GDK_INTERP_HYPER); 73 GDK_INTERP_HYPER);
67 g_object_unref(pixbuf); 74 g_object_unref(pixbuf);
68 } else { 75 } else {
69 favicon_pixbuf_ = pixbuf; 76 favicon_pixbuf_ = pixbuf;
70 } 77 }
78 }
71 79
80 void CreateApplicationShortcutsDialogGtk::CreateDialogBox(GtkWindow* parent) {
72 // Build the dialog. 81 // Build the dialog.
73 create_dialog_ = gtk_dialog_new_with_buttons( 82 create_dialog_ = gtk_dialog_new_with_buttons(
74 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_TITLE).c_str(), 83 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_TITLE).c_str(),
75 parent, 84 parent,
76 (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR), 85 (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR),
77 GTK_STOCK_CANCEL, 86 GTK_STOCK_CANCEL,
78 GTK_RESPONSE_REJECT, 87 GTK_RESPONSE_REJECT,
79 NULL); 88 NULL);
80 gtk_widget_realize(create_dialog_); 89 gtk_widget_realize(create_dialog_);
81 gtk_util::SetWindowSizeFromResources(GTK_WINDOW(create_dialog_), 90 gtk_util::SetWindowSizeFromResources(GTK_WINDOW(create_dialog_),
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 G_CALLBACK(OnToggleCheckboxThunk), this); 151 G_CALLBACK(OnToggleCheckboxThunk), this);
143 152
144 g_signal_connect(create_dialog_, "response", 153 g_signal_connect(create_dialog_, "response",
145 G_CALLBACK(OnCreateDialogResponseThunk), this); 154 G_CALLBACK(OnCreateDialogResponseThunk), this);
146 gtk_widget_show_all(create_dialog_); 155 gtk_widget_show_all(create_dialog_);
147 } 156 }
148 157
149 CreateApplicationShortcutsDialogGtk::~CreateApplicationShortcutsDialogGtk() { 158 CreateApplicationShortcutsDialogGtk::~CreateApplicationShortcutsDialogGtk() {
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
151 160
161 LOG(ERROR) << "~Create__ApplicationShortcutsDialogGtk()";
162
152 gtk_widget_destroy(create_dialog_); 163 gtk_widget_destroy(create_dialog_);
153 164
154 if (error_dialog_) 165 if (error_dialog_)
155 gtk_widget_destroy(error_dialog_); 166 gtk_widget_destroy(error_dialog_);
156 167
157 g_object_unref(favicon_pixbuf_); 168 g_object_unref(favicon_pixbuf_);
158 } 169 }
159 170
171
160 void CreateApplicationShortcutsDialogGtk::OnCreateDialogResponse( 172 void CreateApplicationShortcutsDialogGtk::OnCreateDialogResponse(
161 GtkWidget* widget, int response) { 173 GtkWidget* widget, int response) {
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
163 175
164 if (response == GTK_RESPONSE_ACCEPT) { 176 if (response == GTK_RESPONSE_ACCEPT) {
165 shortcut_info_.create_on_desktop = 177 shortcut_info_.create_on_desktop =
166 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop_checkbox_)); 178 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop_checkbox_));
167 shortcut_info_.create_in_applications_menu = 179 shortcut_info_.create_in_applications_menu =
168 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(menu_checkbox_)); 180 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(menu_checkbox_));
169 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 181 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
170 NewRunnableMethod(this, 182 NewRunnableMethod(this,
171 &CreateApplicationShortcutsDialogGtk::CreateDesktopShortcut, 183 &CreateApplicationShortcutsDialogGtk::CreateDesktopShortcut,
172 shortcut_info_)); 184 shortcut_info_));
173 185
174 if (tab_contents_->delegate()) 186 OnCreatedShortcut();
175 tab_contents_->delegate()->ConvertContentsToApplication(tab_contents_);
176 } else { 187 } else {
177 Release(); 188 Release();
178 } 189 }
179 } 190 }
180 191
181 void CreateApplicationShortcutsDialogGtk::OnErrorDialogResponse( 192 void CreateApplicationShortcutsDialogGtk::OnErrorDialogResponse(
182 GtkWidget* widget, int response) { 193 GtkWidget* widget, int response) {
183 Release(); 194 Release();
184 } 195 }
185 196
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 257
247 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop_checkbox_)) || 258 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop_checkbox_)) ||
248 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(menu_checkbox_))) { 259 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(menu_checkbox_))) {
249 can_accept = TRUE; 260 can_accept = TRUE;
250 } 261 }
251 262
252 gtk_dialog_set_response_sensitive(GTK_DIALOG(create_dialog_), 263 gtk_dialog_set_response_sensitive(GTK_DIALOG(create_dialog_),
253 GTK_RESPONSE_ACCEPT, 264 GTK_RESPONSE_ACCEPT,
254 can_accept); 265 can_accept);
255 } 266 }
267
268 CreateWebApplicationShortcutsDialogGtk::CreateWebApplicationShortcutsDialogGtk(
269 GtkWindow* parent,
270 TabContents* tab_contents)
271 : CreateApplicationShortcutsDialogGtk(parent),
272 tab_contents_(tab_contents) {
273
274 // Get shortcut information now, it's needed for our UI.
275 web_app::GetShortcutInfoForTab(tab_contents_, &shortcut_info_);
276 CreateIconPixBuf(shortcut_info_.favicon);
277
278 CreateDialogBox(parent);
279 }
280
281 void CreateWebApplicationShortcutsDialogGtk::OnCreatedShortcut() {
282 if (tab_contents_->delegate())
283 tab_contents_->delegate()->ConvertContentsToApplication(tab_contents_);
284 }
285
286 CreateChromeApplicationShortcutsDialogGtk::
287 CreateChromeApplicationShortcutsDialogGtk(
288 GtkWindow* parent,
289 const Extension* app)
290 : CreateApplicationShortcutsDialogGtk(parent),
291 app_(app),
292 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) {
293
294 // Get shortcut information now, it's needed for our UI.
295 shortcut_info_.extension_id = UTF8ToUTF16(app_->id());
296 shortcut_info_.url = GURL(app_->launch_web_url());
297 shortcut_info_.title = UTF8ToUTF16(app_->name());
298 shortcut_info_.description = UTF8ToUTF16(app_->description());
299
300 // Get the icon.
301 const gfx::Size max_size(kIconPreviewSizePixels, kIconPreviewSizePixels);
302 ExtensionResource icon_resource = app_->GetIconResource(
303 kIconPreviewSizePixels, ExtensionIconSet::MATCH_BIGGER);
304
305 // If no icon exists that is the desired size or larger, get the
306 // largest icon available:
307 if (icon_resource.empty())
308 icon_resource = app_->GetIconResource(
309 kIconPreviewSizePixels, ExtensionIconSet::MATCH_SMALLER);
310
311 tracker_.LoadImage(app_,
312 icon_resource,
313 max_size,
314 ImageLoadingTracker::DONT_CACHE);
315 }
316
317 // Called by tracker_ when the app's icon is loaded.
318 void CreateChromeApplicationShortcutsDialogGtk::OnImageLoaded(
319 SkBitmap* image, ExtensionResource resource, int index) {
320 if (image->isNull()) {
321 NOTREACHED() << "Corrupt image in profile?";
322 return;
323 }
324 shortcut_info_.favicon = *image;
325
326 CreateIconPixBuf(*image);
327 CreateDialogBox(parent_);
328 }
329
330 void CreateChromeApplicationShortcutsDialogGtk::OnCreatedShortcut() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698