Debian Codegraph @

gtk/gtkapplication-quartz.c

raw symbols SCIP
1 /*
2 * Copyright © 2010 Codethink Limited
3 * Copyright © 2013 Canonical Limited
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the licence, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Ryan Lortie <[email protected]>
19 */
20
21 #include "config.h"
22
23 #include "gtkapplicationprivate.h"
24 #include "gtkbuilder.h"
25 #include "gtknative.h"
26 #import <gdk/macos/GdkMacosView.h>
27 #import <gdk/macos/GdkMacosWindow.h>
28 #import "gtkapplication-quartz-private.h"
29
30 typedef struct
31 {
32 guint cookie;
33 GtkApplicationInhibitFlags flags;
34 char *reason;
35 GtkWindow *window;
36 } GtkApplicationQuartzInhibitor;
37
38 static void
39 gtk_application_quartz_inhibitor_free (GtkApplicationQuartzInhibitor *inhibitor)
40 {
41 g_free (inhibitor->reason);
42 g_clear_object (&inhibitor->window);
43 g_free (inhibitor);
44 }
45
46 typedef GtkApplicationImplClass GtkApplicationImplQuartzClass;
47
48 typedef struct
49 {
50 GtkApplicationImpl impl;
51
52 GtkActionMuxer *muxer;
53 GMenu *combined;
54 GMenuModel *standard_app_menu;
55
56 GSList *inhibitors;
57 int quit_inhibit;
58 guint next_cookie;
59 NSObject *delegate;
60 } GtkApplicationImplQuartz;
61
62 G_DEFINE_TYPE (GtkApplicationImplQuartz, gtk_application_impl_quartz, GTK_TYPE_APPLICATION_IMPL)
63
64 @interface GtkApplicationQuartzDelegate : NSObject<NSApplicationDelegate>
65 {
66 GtkApplicationImplQuartz *quartz;
67 }
68
69 - (id)initWithImpl:(GtkApplicationImplQuartz*)impl;
70 @end
71
72 @implementation GtkApplicationQuartzDelegate
73 -(id)initWithImpl:(GtkApplicationImplQuartz*)impl
74 {
75 [super init];
76 quartz = impl;
77 return self;
78 }
79
80 -(NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *)sender
81 {
82 const gchar *quit_action_name = "quit";
83 GActionGroup *action_group = G_ACTION_GROUP (quartz->impl.application);
84
85 if (quartz->quit_inhibit != 0)
86 return NSTerminateCancel;
87
88 if (g_action_group_has_action (action_group, quit_action_name))
89 {
90 g_action_group_activate_action (action_group, quit_action_name, NULL);
91 return NSTerminateCancel;
92 }
93
94 return NSTerminateNow;
95 }
96
97 -(void)application:(NSApplication *)theApplication openFiles:(NSArray *)filenames
98 {
99 GFile **files;
100 int i;
101 GApplicationFlags flags;
102
103 flags = g_application_get_flags (G_APPLICATION (quartz->impl.application));
104
105 if (~flags & G_APPLICATION_HANDLES_OPEN)
106 {
107 [theApplication replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
108 return;
109 }
110
111 files = g_new (GFile *, [filenames count]);
112
113 for (i = 0; i < [filenames count]; i++)
114 files[i] = g_file_new_for_path ([(NSString *)[filenames objectAtIndex:i] UTF8String]);
115
116 g_application_open (G_APPLICATION (quartz->impl.application), files, [filenames count], "");
117
118 for (i = 0; i < [filenames count]; i++)
119 g_object_unref (files[i]);
120
121 g_free (files);
122
123 [theApplication replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
124 }
125
126 - (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app
127 {
128 return YES;
129 }
130 @end
131
132 @interface GtkMacosContentView : GdkMacosView<NSMenuItemValidation>
133
134 /* In some cases GTK pops up a native window, such as when opening or
135 * saving a file. We map common actions such as undo, copy, paste, etc.
136 * to selectors, so these actions can be activated in a native window.
137 * As a concequence, we also need to implement them on our own view,
138 * and activate the action to the focused widget.
139 */
140
141 - (void) undo:(id)sender;
142 - (void) redo:(id)sender;
143
144 - (void) cut:(id)sender;
145 - (void) copy:(id)sender;
146 - (void) paste:(id)sender;
147
148 - (void) selectAll:(id)sender;
149 @end
150
151 @implementation GtkMacosContentView
152
153 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
154 {
155 if ([menuItem isKindOfClass:[GNSMenuItem class]])
156 return [((GNSMenuItem *) menuItem) validateMenuItem:menuItem];
157 return NO;
158 }
159
160 - (void)undo:(id)sender
161 {
162 [self maybeActivateAction:"text.undo" sender:sender];
163 }
164
165 - (void)redo:(id)sender
166 {
167 [self maybeActivateAction:"text.redo" sender:sender];
168 }
169
170 - (void)cut:(id)sender
171 {
172 [self maybeActivateAction:"clipboard.cut" sender:sender];
173 }
174
175 - (void)copy:(id)sender
176 {
177 [self maybeActivateAction:"clipboard.copy" sender:sender];
178 }
179
180 - (void)paste:(id)sender
181 {
182 [self maybeActivateAction:"clipboard.paste" sender:sender];
183 }
184
185 - (void)selectAll:(id)sender
186 {
187 [super selectAll:sender];
188 [self maybeActivateAction:"selection.select-all" sender:sender];
189 }
190
191 -(void)maybeActivateAction:(const char*)actionName sender:(id)sender
192 {
193 if ([sender isKindOfClass:[GNSMenuItem class]])
194 [((GNSMenuItem *) sender) didSelectItem:sender];
195 else
196 g_warning ("%s: sender %s is not a GNSMenuItem", actionName, [[sender description] UTF8String]);
197 }
198
199 @end
200
201 static void
202 gtk_application_impl_quartz_set_default_accels (GtkApplicationImpl *impl)
203 {
204 const char *pref_accel[] = {"<Meta>comma", NULL};
205 const char *hide_others_accel[] = {"<Meta><Alt>h", NULL};
206 const char *hide_accel[] = {"<Meta>h", NULL};
207 const char *quit_accel[] = {"<Meta>q", NULL};
208 const char *undo_accel[] = {"<Meta>z", NULL};
209 const char *redo_accel[] = {"<Meta><Shift>z", NULL};
210 const char *cut_accel[] = {"<Meta>x", NULL};
211 const char *copy_accel[] = {"<Meta>c", NULL};
212 const char *paste_accel[] = {"<Meta>v", NULL};
213 const char *select_all_accel[] = {"<Meta>a", NULL};
214
215 gtk_application_set_accels_for_action (impl->application, "app.preferences", pref_accel);
216 gtk_application_set_accels_for_action (impl->application, "gtkinternal.hide-others", hide_others_accel);
217 gtk_application_set_accels_for_action (impl->application, "gtkinternal.hide", hide_accel);
218 gtk_application_set_accels_for_action (impl->application, "app.quit", quit_accel);
219 gtk_application_set_accels_for_action (impl->application, "text.undo", undo_accel);
220 gtk_application_set_accels_for_action (impl->application, "text.redo", redo_accel);
221 gtk_application_set_accels_for_action (impl->application, "clipboard.cut", cut_accel);
222 gtk_application_set_accels_for_action (impl->application, "clipboard.copy", copy_accel);
223 gtk_application_set_accels_for_action (impl->application, "clipboard.paste", paste_accel);
224 gtk_application_set_accels_for_action (impl->application, "selection.select-all", select_all_accel);
225 }
226
227 static void
228 gtk_application_impl_quartz_hide (GSimpleAction *action,
229 GVariant *parameter,
230 gpointer user_data)
231 {
232 [NSApp hide:NSApp];
233 }
234
235 static void
236 gtk_application_impl_quartz_hide_others (GSimpleAction *action,
237 GVariant *parameter,
238 gpointer user_data)
239 {
240 [NSApp hideOtherApplications:NSApp];
241 }
242
243 static void
244 gtk_application_impl_quartz_show_all (GSimpleAction *action,
245 GVariant *parameter,
246 gpointer user_data)
247 {
248 [NSApp unhideAllApplications:NSApp];
249 }
250
251 static GActionEntry gtk_application_impl_quartz_actions[] = {
252 { "hide", gtk_application_impl_quartz_hide },
253 { "hide-others", gtk_application_impl_quartz_hide_others },
254 { "show-all", gtk_application_impl_quartz_show_all }
255 };
256
257 static void
258 gtk_application_impl_quartz_set_app_menu (GtkApplicationImpl *impl,
259 GMenuModel *app_menu)
260 {
261 GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
262
263 /* If there are any items at all, then the first one is the app menu */
264 if (g_menu_model_get_n_items (G_MENU_MODEL (quartz->combined)))
265 g_menu_remove (quartz->combined, 0);
266
267 if (app_menu)
268 g_menu_prepend_submenu (quartz->combined, "Application", app_menu);
269 else
270 {
271 GMenu *empty;
272
273 /* We must preserve the rule that index 0 is the app menu */
274 empty = g_menu_new ();
275 g_menu_prepend_submenu (quartz->combined, "Application", G_MENU_MODEL (empty));
276 g_object_unref (empty);
277 }
278 }
279
280 static void
281 gtk_application_impl_quartz_startup (GtkApplicationImpl *impl,
282 gboolean support_save)
283 {
284 GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
285 GSimpleActionGroup *gtkinternal;
286 GMenuModel *menubar;
287
288 quartz->delegate = [[GtkApplicationQuartzDelegate alloc] initWithImpl:quartz];
289 [NSApp setDelegate: (id<NSApplicationDelegate>)quartz->delegate];
290
291 quartz->muxer = gtk_action_muxer_new (NULL);
292 gtk_action_muxer_set_parent (quartz->muxer, gtk_application_get_action_muxer (impl->application));
293
294 gtk_application_impl_quartz_set_default_accels (impl);
295
296 /* and put code behind the 'special' accels */
297 gtkinternal = g_simple_action_group_new ();
298 g_action_map_add_action_entries (G_ACTION_MAP (gtkinternal), gtk_application_impl_quartz_actions,
299 G_N_ELEMENTS (gtk_application_impl_quartz_actions), quartz);
300 gtk_application_insert_action_group (impl->application, "gtkinternal", G_ACTION_GROUP (gtkinternal));
301 g_object_unref (gtkinternal);
302
303 /* now setup the menu */
304 if (quartz->standard_app_menu == NULL)
305 {
306 GtkBuilder *builder;
307
308 /* If the user didn't fill in their own menu yet, add ours.
309 *
310 * The fact that we do this here ensures that we will always have the
311 * app menu at index 0 in 'combined'.
312 */
313 builder = gtk_builder_new_from_resource ("/org/gtk/libgtk/ui/gtkapplication-quartz.ui");
314 quartz->standard_app_menu = G_MENU_MODEL (g_object_ref (gtk_builder_get_object (builder, "app-menu")));
315 g_object_unref (builder);
316 }
317
318 gtk_application_impl_quartz_set_app_menu (impl, quartz->standard_app_menu);
319
320 /* This may or may not add an item to 'combined' */
321 menubar = gtk_application_get_menubar (impl->application);
322 if (menubar == NULL)
323 {
324 GtkBuilder *builder;
325
326 /* Provide a fallback menu, so keyboard shortcuts work in native windows too.
327 */
328 builder = gtk_builder_new_from_resource ("/org/gtk/libgtk/ui/gtkapplication-quartz.ui");
329 menubar = G_MENU_MODEL (g_object_ref (gtk_builder_get_object (builder, "default-menu")));
330 g_object_unref (builder);
331 }
332
333 gtk_application_impl_set_menubar (impl, menubar);
334
335 /* OK. Now put it in the menu. */
336 gtk_application_impl_quartz_setup_menu (G_MENU_MODEL (quartz->combined), quartz->muxer);
337
338 [NSApp finishLaunching];
339 }
340
341 static void
342 gtk_application_impl_quartz_shutdown (GtkApplicationImpl *impl)
343 {
344 GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
345
346 /* destroy our custom menubar */
347 [NSApp setMainMenu:[[[NSMenu alloc] init] autorelease]];
348
349 if (quartz->delegate)
350 {
351 [quartz->delegate release];
352 quartz->delegate = NULL;
353 }
354
355 g_slist_free_full (quartz->inhibitors, (GDestroyNotify) gtk_application_quartz_inhibitor_free);
356 quartz->inhibitors = NULL;
357 }
358
359 static void
360 on_window_unmap_cb (GtkApplicationImpl *impl,
361 GtkWindow *window)
362 {
363 GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
364
365 if ((GActionGroup *)window == gtk_action_muxer_get_group (quartz->muxer, "win"))
366 gtk_action_muxer_remove (quartz->muxer, "win");
367 }
368
369 static void
370 gtk_application_impl_quartz_active_window_changed (GtkApplicationImpl *impl,
371 GtkWindow *window)
372 {
373 GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
374
375 /* Track unmapping of the window so we can clear the "win" field.
376 * Without this, we might hold on to a reference of the window
377 * preventing it from getting disposed.
378 */
379 if (window != NULL && !g_object_get_data (G_OBJECT (window), "quartz-muxer-unmap"))
380 {
381 gulong handler_id = g_signal_connect_object (window,
382 "unmap",
383 G_CALLBACK (on_window_unmap_cb),
384 impl,
385 G_CONNECT_SWAPPED);
386 g_object_set_data (G_OBJECT (window),
387 "quartz-muxer-unmap",
388 GSIZE_TO_POINTER (handler_id));
389 }
390
391 gtk_action_muxer_remove (quartz->muxer, "win");
392
393 if (G_IS_ACTION_GROUP (window))
394 gtk_action_muxer_insert (quartz->muxer, "win", G_ACTION_GROUP (window));
395 }
396
397 static void
398 gtk_application_impl_quartz_set_menubar (GtkApplicationImpl *impl,
399 GMenuModel *menubar)
400 {
401 GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
402
403 /* If we have the menubar, it is a section at index '1' */
404 if (g_menu_model_get_n_items (G_MENU_MODEL (quartz->combined)) > 1)
405 g_menu_remove (quartz->combined, 1);
406
407 if (menubar)
408 g_menu_append_section (quartz->combined, NULL, menubar);
409 }
410
411 static guint
412 gtk_application_impl_quartz_inhibit (GtkApplicationImpl *impl,
413 GtkWindow *window,
414 GtkApplicationInhibitFlags flags,
415 const char *reason)
416 {
417 GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
418 GtkApplicationQuartzInhibitor *inhibitor;
419
420 inhibitor = g_new (GtkApplicationQuartzInhibitor, 1);
421 inhibitor->cookie = ++quartz->next_cookie;
422 inhibitor->flags = flags;
423 inhibitor->reason = g_strdup (reason);
424 inhibitor->window = window ? g_object_ref (window) : NULL;
425
426 quartz->inhibitors = g_slist_prepend (quartz->inhibitors, inhibitor);
427
428 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
429 quartz->quit_inhibit++;
430
431 return inhibitor->cookie;
432 }
433
434 static void
435 gtk_application_impl_quartz_uninhibit (GtkApplicationImpl *impl,
436 guint cookie)
437 {
438 GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
439 GSList *iter;
440
441 for (iter = quartz->inhibitors; iter; iter = iter->next)
442 {
443 GtkApplicationQuartzInhibitor *inhibitor = iter->data;
444
445 if (inhibitor->cookie == cookie)
446 {
447 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
448 quartz->quit_inhibit--;
449 gtk_application_quartz_inhibitor_free (inhibitor);
450 quartz->inhibitors = g_slist_delete_link (quartz->inhibitors, iter);
451 return;
452 }
453 }
454
455 g_warning ("Invalid inhibitor cookie");
456 }
457
458 static void
459 gtk_application_impl_quartz_init (GtkApplicationImplQuartz *quartz)
460 {
461 /* This is required so that Cocoa is not going to parse the
462 command line arguments by itself and generate OpenFile events.
463 We already parse the command line ourselves, so this is needed
464 to prevent opening files twice, etc. */
465 [[NSUserDefaults standardUserDefaults] setObject:@"NO"
466 forKey:@"NSTreatUnknownArgumentsAsOpen"];
467
468 quartz->combined = g_menu_new ();
469 }
470
471 static void
472 gtk_application_impl_quartz_finalize (GObject *object)
473 {
474 GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) object;
475
476 g_clear_object (&quartz->combined);
477 g_clear_object (&quartz->standard_app_menu);
478
479 G_OBJECT_CLASS (gtk_application_impl_quartz_parent_class)->finalize (object);
480 }
481
482 static void
483 gtk_application_impl_quartz_class_init (GtkApplicationImplClass *class)
484 {
485 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
486
487 class->startup = gtk_application_impl_quartz_startup;
488 class->shutdown = gtk_application_impl_quartz_shutdown;
489 class->active_window_changed = gtk_application_impl_quartz_active_window_changed;
490 class->set_app_menu = gtk_application_impl_quartz_set_app_menu;
491 class->set_menubar = gtk_application_impl_quartz_set_menubar;
492 class->inhibit = gtk_application_impl_quartz_inhibit;
493 class->uninhibit = gtk_application_impl_quartz_uninhibit;
494
495 gobject_class->finalize = gtk_application_impl_quartz_finalize;
496
497 [GdkMacosWindow setContentViewClass:[GtkMacosContentView class]];
498 }