2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "gtkapplication.h"
32 #include "gtkapplicationprivate.h"
33 #include "gtkmarshalers.h"
35 #include "gtkaccelmapprivate.h"
36 #include "gactionmuxer.h"
39 #ifdef GDK_WINDOWING_QUARTZ
40 #include "gtkquartz-menu.h"
41 #import <Cocoa/Cocoa.h>
42 #include <Carbon/Carbon.h>
43 #include "gtkmessagedialog.h"
47 #ifdef GDK_WINDOWING_X11
48 #include <gdk/x11/gdkx.h>
52 * SECTION:gtkapplication
53 * @title: GtkApplication
54 * @short_description: Application class
56 * #GtkApplication is a class that handles many important aspects
57 * of a GTK+ application in a convenient fashion, without enforcing
58 * a one-size-fits-all application model.
60 * Currently, GtkApplication handles GTK+ initialization, application
61 * uniqueness, session management, provides some basic scriptability and
62 * desktop shell integration by exporting actions and menus and manages a
63 * list of toplevel windows whose life-cycle is automatically tied to the
64 * life-cycle of your application.
66 * While GtkApplication works fine with plain #GtkWindows, it is recommended
67 * to use it together with #GtkApplicationWindow.
69 * When GDK threads are enabled, GtkApplication will acquire the GDK
70 * lock when invoking actions that arrive from other processes. The GDK
71 * lock is not touched for local action invocations. In order to have
72 * actions invoked in a predictable context it is therefore recommended
73 * that the GDK lock be held while invoking actions locally with
74 * g_action_group_activate_action(). The same applies to actions
75 * associated with #GtkApplicationWindow and to the 'activate' and
76 * 'open' #GApplication methods.
78 * To set an application menu on a GtkApplication, use
79 * gtk_application_set_app_menu(). The #GMenuModel that this function
80 * expects is usually constructed using #GtkBuilder, as seen in the
81 * following example. To set a menubar that will be automatically picked
82 * up by #GApplicationWindows, use gtk_application_set_menubar(). GTK+
83 * makes these menus appear as expected, depending on the platform
84 * the application is running on.
86 * <figure label="Menu integration in OS X">
87 * <graphic fileref="bloatpad-osx.png" format="PNG"/>
90 * <figure label="Menu integration in GNOME">
91 * <graphic fileref="bloatpad-gnome.png" format="PNG"/>
94 * <figure label="Menu integration in Xfce">
95 * <graphic fileref="bloatpad-xfce.png" format="PNG"/>
98 * <example id="gtkapplication"><title>A simple application</title>
100 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/bloatpad.c">
101 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
106 * GtkApplication optionally registers with a session manager
107 * of the users session (if you set the #GtkApplication::register-session
108 * property) and offers various functionality related to the session
111 * An application can be informed when the session is about to end
112 * by connecting to the #GtkApplication::quit signal.
114 * An application can request the session to be ended by calling
115 * gtk_application_end_session().
117 * An application can block various ways to end the session with
118 * the gtk_application_inhibit() function. Typical use cases for
119 * this kind of inhibiting are long-running, uninterruptible operations,
120 * such as burning a CD or performing a disk backup. The session
121 * manager may not honor the inhibitor, but it can be expected to
122 * inform the user about the negative consequences of ending the
123 * session while inhibitors are present.
133 static guint gtk_application_signals[LAST_SIGNAL];
137 PROP_REGISTER_SESSION,
142 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
144 struct _GtkApplicationPrivate
148 gboolean register_session;
150 #ifdef GDK_WINDOWING_X11
151 GDBusConnection *session_bus;
152 const gchar *application_id;
155 GMenuModel *app_menu;
156 gchar *app_menu_path;
165 GDBusProxy *sm_proxy;
166 GDBusProxy *client_proxy;
171 #ifdef GDK_WINDOWING_QUARTZ
181 #ifdef GDK_WINDOWING_X11
183 gtk_application_x11_publish_menu (GtkApplication *application,
191 /* unexport any existing menu */
194 g_dbus_connection_unexport_menu_model (application->priv->session_bus, *id);
200 /* export the new menu, if there is one */
203 /* try getting the preferred name */
204 *path = g_strconcat (application->priv->object_path, "/menus/", type, NULL);
205 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
207 /* keep trying until we get a working name... */
208 for (i = 0; *id == 0; i++)
211 *path = g_strdup_printf ("%s/menus/%s%d", application->priv->object_path, type, i);
212 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
218 gtk_application_set_app_menu_x11 (GtkApplication *application,
219 GMenuModel *app_menu)
221 gtk_application_x11_publish_menu (application, "appmenu", app_menu,
222 &application->priv->app_menu_id,
223 &application->priv->app_menu_path);
227 gtk_application_set_menubar_x11 (GtkApplication *application,
230 gtk_application_x11_publish_menu (application, "menubar", menubar,
231 &application->priv->menubar_id,
232 &application->priv->menubar_path);
236 gtk_application_window_added_x11 (GtkApplication *application,
239 if (application->priv->session_bus == NULL)
242 if (GTK_IS_APPLICATION_WINDOW (window))
244 GtkApplicationWindow *app_window = GTK_APPLICATION_WINDOW (window);
247 /* GtkApplicationWindow associates with us when it is first created,
248 * so surely it's not realized yet...
250 g_assert (!gtk_widget_get_realized (GTK_WIDGET (window)));
257 window_id = application->priv->next_id++;
258 window_path = g_strdup_printf ("%s/window/%d", application->priv->object_path, window_id);
259 success = gtk_application_window_publish (app_window, application->priv->session_bus, window_path);
260 g_free (window_path);
267 gtk_application_window_removed_x11 (GtkApplication *application,
270 if (application->priv->session_bus == NULL)
273 if (GTK_IS_APPLICATION_WINDOW (window))
274 gtk_application_window_unpublish (GTK_APPLICATION_WINDOW (window));
278 object_path_from_appid (const gchar *appid)
280 gchar *appid_path, *iter;
282 appid_path = g_strconcat ("/", appid, NULL);
283 for (iter = appid_path; *iter; iter++)
295 static void gtk_application_startup_session_dbus (GtkApplication *app);
298 gtk_application_startup_x11 (GtkApplication *application)
300 const gchar *application_id;
302 application_id = g_application_get_application_id (G_APPLICATION (application));
303 application->priv->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
304 application->priv->object_path = object_path_from_appid (application_id);
306 gtk_application_startup_session_dbus (GTK_APPLICATION (application));
310 gtk_application_shutdown_x11 (GtkApplication *application)
312 g_free (application->priv->object_path);
313 application->priv->object_path = NULL;
314 g_clear_object (&application->priv->session_bus);
316 g_clear_object (&application->priv->sm_proxy);
317 g_clear_object (&application->priv->client_proxy);
318 g_free (application->priv->app_id);
319 g_free (application->priv->client_path);
323 gtk_application_get_dbus_object_path (GtkApplication *application)
325 return application->priv->object_path;
329 gtk_application_get_app_menu_object_path (GtkApplication *application)
331 return application->priv->app_menu_path;
335 gtk_application_get_menubar_object_path (GtkApplication *application)
337 return application->priv->menubar_path;
342 #ifdef GDK_WINDOWING_QUARTZ
346 GtkApplicationInhibitFlags flags;
349 } GtkApplicationQuartzInhibitor;
352 gtk_application_quartz_inhibitor_free (GtkApplicationQuartzInhibitor *inhibitor)
354 g_free (inhibitor->reason);
355 g_clear_object (&inhibitor->window);
356 g_slice_free (GtkApplicationQuartzInhibitor, inhibitor);
360 gtk_application_menu_changed_quartz (GObject *object,
364 GtkApplication *application = GTK_APPLICATION (object);
367 combined = g_menu_new ();
368 g_menu_append_submenu (combined, "Application", gtk_application_get_app_menu (application));
369 g_menu_append_section (combined, NULL, gtk_application_get_menubar (application));
371 gtk_quartz_set_main_menu (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (application->priv->muxer));
374 static void gtk_application_startup_session_quartz (GtkApplication *app);
377 gtk_application_startup_quartz (GtkApplication *application)
379 [NSApp finishLaunching];
381 application->priv->muxer = g_action_muxer_new ();
382 g_action_muxer_insert (application->priv->muxer, "app", G_ACTION_GROUP (application));
384 g_signal_connect (application, "notify::app-menu", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
385 g_signal_connect (application, "notify::menubar", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
386 gtk_application_menu_changed_quartz (G_OBJECT (application), NULL, NULL);
388 gtk_application_startup_session_quartz (application);
392 gtk_application_shutdown_quartz (GtkApplication *application)
394 g_signal_handlers_disconnect_by_func (application, gtk_application_menu_changed_quartz, NULL);
396 g_object_unref (application->priv->muxer);
397 application->priv->muxer = NULL;
399 g_slist_free_full (application->priv->inhibitors,
400 (GDestroyNotify) gtk_application_quartz_inhibitor_free);
401 application->priv->inhibitors = NULL;
405 gtk_application_focus_changed (GtkApplication *application,
408 if (G_IS_ACTION_GROUP (window))
409 g_action_muxer_insert (application->priv->muxer, "win", G_ACTION_GROUP (window));
411 g_action_muxer_remove (application->priv->muxer, "win");
416 gtk_application_focus_in_event_cb (GtkWindow *window,
417 GdkEventFocus *event,
418 GtkApplication *application)
420 GtkApplicationPrivate *priv = application->priv;
423 /* Keep the window list sorted by most-recently-focused. */
424 link = g_list_find (priv->windows, window);
425 if (link != NULL && link != priv->windows)
427 priv->windows = g_list_remove_link (priv->windows, link);
428 priv->windows = g_list_concat (link, priv->windows);
431 #ifdef GDK_WINDOWING_QUARTZ
432 gtk_application_focus_changed (application, window);
439 gtk_application_startup (GApplication *application)
441 G_APPLICATION_CLASS (gtk_application_parent_class)
442 ->startup (application);
446 #ifdef GDK_WINDOWING_X11
447 gtk_application_startup_x11 (GTK_APPLICATION (application));
450 #ifdef GDK_WINDOWING_QUARTZ
451 gtk_application_startup_quartz (GTK_APPLICATION (application));
456 gtk_application_shutdown (GApplication *application)
458 #ifdef GDK_WINDOWING_X11
459 gtk_application_shutdown_x11 (GTK_APPLICATION (application));
462 #ifdef GDK_WINDOWING_QUARTZ
463 gtk_application_shutdown_quartz (GTK_APPLICATION (application));
466 G_APPLICATION_CLASS (gtk_application_parent_class)
467 ->shutdown (application);
471 gtk_application_add_platform_data (GApplication *application,
472 GVariantBuilder *builder)
474 const gchar *startup_id;
476 startup_id = getenv ("DESKTOP_STARTUP_ID");
478 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
479 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
480 g_variant_new_string (startup_id));
484 gtk_application_before_emit (GApplication *application,
485 GVariant *platform_data)
491 gdk_threads_enter ();
493 g_variant_iter_init (&iter, platform_data);
494 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
496 #ifdef GDK_WINDOWING_X11
497 if (strcmp (key, "desktop-startup-id") == 0)
502 display = gdk_display_get_default ();
503 id = g_variant_get_string (value, NULL);
504 if (GDK_IS_X11_DISPLAY (display))
505 gdk_x11_display_set_startup_notification_id (display, id);
512 gtk_application_after_emit (GApplication *application,
513 GVariant *platform_data)
515 gdk_notify_startup_complete ();
517 gdk_threads_leave ();
521 gtk_application_init (GtkApplication *application)
523 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
524 GTK_TYPE_APPLICATION,
525 GtkApplicationPrivate);
529 gtk_application_window_added (GtkApplication *application,
532 GtkApplicationPrivate *priv = application->priv;
534 priv->windows = g_list_prepend (priv->windows, window);
535 gtk_window_set_application (window, application);
536 g_application_hold (G_APPLICATION (application));
538 g_signal_connect (window, "focus-in-event",
539 G_CALLBACK (gtk_application_focus_in_event_cb),
542 #ifdef GDK_WINDOWING_X11
543 gtk_application_window_added_x11 (application, window);
548 gtk_application_window_removed (GtkApplication *application,
551 GtkApplicationPrivate *priv = application->priv;
553 #ifdef GDK_WINDOWING_X11
554 gtk_application_window_removed_x11 (application, window);
557 g_signal_handlers_disconnect_by_func (window,
558 gtk_application_focus_in_event_cb,
561 g_application_release (G_APPLICATION (application));
562 priv->windows = g_list_remove (priv->windows, window);
563 gtk_window_set_application (window, NULL);
567 extract_accel_from_menu_item (GMenuModel *model,
571 GMenuAttributeIter *iter;
574 const gchar *accel = NULL;
575 const gchar *action = NULL;
576 GVariant *target = NULL;
578 iter = g_menu_model_iterate_item_attributes (model, item);
579 while (g_menu_attribute_iter_get_next (iter, &key, &value))
581 if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
582 action = g_variant_get_string (value, NULL);
583 else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
584 accel = g_variant_get_string (value, NULL);
585 else if (g_str_equal (key, "target"))
586 target = g_variant_ref (value);
587 g_variant_unref (value);
589 g_object_unref (iter);
592 gtk_application_add_accelerator (app, accel, action, target);
595 g_variant_unref (target);
599 extract_accels_from_menu (GMenuModel *model,
607 for (i = 0; i < g_menu_model_get_n_items (model); i++)
609 extract_accel_from_menu_item (model, i, app);
611 iter = g_menu_model_iterate_item_links (model, i);
612 while (g_menu_link_iter_get_next (iter, &key, &m))
614 extract_accels_from_menu (m, app);
617 g_object_unref (iter);
622 gtk_application_get_property (GObject *object,
627 GtkApplication *application = GTK_APPLICATION (object);
631 case PROP_REGISTER_SESSION:
632 g_value_set_boolean (value, application->priv->register_session);
636 g_value_set_object (value, gtk_application_get_app_menu (application));
640 g_value_set_object (value, gtk_application_get_menubar (application));
644 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
650 gtk_application_set_property (GObject *object,
655 GtkApplication *application = GTK_APPLICATION (object);
659 case PROP_REGISTER_SESSION:
660 application->priv->register_session = g_value_get_boolean (value);
664 gtk_application_set_app_menu (application, g_value_get_object (value));
668 gtk_application_set_menubar (application, g_value_get_object (value));
672 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
678 gtk_application_quit (GtkApplication *app)
680 /* we are asked to quit, so don't linger */
681 g_application_set_inactivity_timeout (G_APPLICATION (app), 0);
685 gtk_application_finalize (GObject *object)
687 GtkApplication *application = GTK_APPLICATION (object);
689 g_clear_object (&application->priv->app_menu);
690 g_clear_object (&application->priv->menubar);
692 G_OBJECT_CLASS (gtk_application_parent_class)
697 gtk_application_class_init (GtkApplicationClass *class)
699 GObjectClass *object_class = G_OBJECT_CLASS (class);
700 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
702 object_class->get_property = gtk_application_get_property;
703 object_class->set_property = gtk_application_set_property;
704 object_class->finalize = gtk_application_finalize;
706 application_class->add_platform_data = gtk_application_add_platform_data;
707 application_class->before_emit = gtk_application_before_emit;
708 application_class->after_emit = gtk_application_after_emit;
709 application_class->startup = gtk_application_startup;
710 application_class->shutdown = gtk_application_shutdown;
712 class->window_added = gtk_application_window_added;
713 class->window_removed = gtk_application_window_removed;
714 class->quit = gtk_application_quit;
716 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
719 * GtkApplication::window-added:
720 * @application: the #GtkApplication which emitted the signal
721 * @window: the newly-added #GtkWindow
723 * Emitted when a #GtkWindow is added to @application through
724 * gtk_application_add_window().
728 gtk_application_signals[WINDOW_ADDED] =
729 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
730 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
732 g_cclosure_marshal_VOID__OBJECT,
733 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
736 * GtkApplication::window-removed:
737 * @application: the #GtkApplication which emitted the signal
738 * @window: the #GtkWindow that is being removed
740 * Emitted when a #GtkWindow is removed from @application,
741 * either as a side-effect of being destroyed or explicitly
742 * through gtk_application_remove_window().
746 gtk_application_signals[WINDOW_REMOVED] =
747 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
748 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
750 g_cclosure_marshal_VOID__OBJECT,
751 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
754 * GtkApplication::quit:
755 * @application: the #GtkApplication
757 * Emitted when the session manager wants the application to quit
758 * (generally because the user is logging out). The application
759 * should exit as soon as possible after receiving this signal; if
760 * it does not, the session manager may choose to forcibly kill it.
762 * Normally, an application would only be sent a ::quit if there
763 * are no inhibitors (see gtk_application_inhibit()).
764 * However, this is not guaranteed; in some situations the
765 * session manager may decide to end the session without giving
766 * applications a chance to object.
768 * To receive this signal, you need to set the
769 * #GtkApplication::register-session property
770 * when creating the application object.
774 gtk_application_signals[QUIT] =
775 g_signal_new ("quit", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
776 G_STRUCT_OFFSET (GtkApplicationClass, quit),
777 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
780 * GtkApplication::register-session:
782 * Set this property to %TRUE to register with the session manager
783 * and receive the #GtkApplication::quit signal when the session
788 g_object_class_install_property (object_class, PROP_REGISTER_SESSION,
789 g_param_spec_boolean ("register-session",
790 P_("Register session"),
791 P_("Register with the session manager"),
792 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
794 g_object_class_install_property (object_class, PROP_APP_MENU,
795 g_param_spec_object ("app-menu",
796 P_("Application menu"),
797 P_("The GMenuModel for the application menu"),
799 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
801 g_object_class_install_property (object_class, PROP_MENUBAR,
802 g_param_spec_object ("menubar",
804 P_("The GMenuModel for the menubar"),
806 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
810 * gtk_application_new:
811 * @application_id: the application id
812 * @flags: the application flags
814 * Creates a new #GtkApplication instance.
816 * This function calls g_type_init() for you. gtk_init() is called
817 * as soon as the application gets registered as the primary instance.
819 * Note that commandline arguments are not passed to gtk_init().
820 * All GTK+ functionality that is available via commandline arguments
821 * can also be achieved by setting suitable environment variables
822 * such as <envvar>G_DEBUG</envvar>, so this should not be a big
823 * problem. If you absolutely must support GTK+ commandline arguments,
824 * you can explicitly call gtk_init() before creating the application
827 * The application id must be valid. See g_application_id_is_valid().
829 * Returns: a new #GtkApplication instance
834 gtk_application_new (const gchar *application_id,
835 GApplicationFlags flags)
837 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
841 return g_object_new (GTK_TYPE_APPLICATION,
842 "application-id", application_id,
848 * gtk_application_add_window:
849 * @application: a #GtkApplication
850 * @window: a #GtkWindow
852 * Adds a window from @application.
854 * This call is equivalent to setting the #GtkWindow:application
855 * property of @window to @application.
857 * Normally, the connection between the application and the window
858 * will remain until the window is destroyed, but you can explicitly
859 * remove it with gtk_application_remove_window().
861 * GTK+ will keep the application running as long as it has
867 gtk_application_add_window (GtkApplication *application,
870 g_return_if_fail (GTK_IS_APPLICATION (application));
872 if (!g_list_find (application->priv->windows, window))
873 g_signal_emit (application,
874 gtk_application_signals[WINDOW_ADDED], 0, window);
878 * gtk_application_remove_window:
879 * @application: a #GtkApplication
880 * @window: a #GtkWindow
882 * Remove a window from @application.
884 * If @window belongs to @application then this call is equivalent to
885 * setting the #GtkWindow:application property of @window to
888 * The application may stop running as a result of a call to this
894 gtk_application_remove_window (GtkApplication *application,
897 g_return_if_fail (GTK_IS_APPLICATION (application));
899 if (g_list_find (application->priv->windows, window))
900 g_signal_emit (application,
901 gtk_application_signals[WINDOW_REMOVED], 0, window);
905 * gtk_application_get_windows:
906 * @application: a #GtkApplication
908 * Gets a list of the #GtkWindows associated with @application.
910 * The list is sorted by most recently focused window, such that the first
911 * element is the currently focused window. (Useful for choosing a parent
912 * for a transient window.)
914 * The list that is returned should not be modified in any way. It will
915 * only remain valid until the next focus change or window creation or
918 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
923 gtk_application_get_windows (GtkApplication *application)
925 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
927 return application->priv->windows;
931 * gtk_application_add_accelerator:
932 * @application: a #GtkApplication
933 * @accelerator: accelerator string
934 * @action_name: the name of the action to activate
935 * @parameter: (allow-none): parameter to pass when activating the action,
936 * or %NULL if the action does not accept an activation parameter
938 * Installs an accelerator that will cause the named action
939 * to be activated when the key combination specificed by @accelerator
942 * @accelerator must be a string that can be parsed by
943 * gtk_accelerator_parse(), e.g. "<Primary>q" or "<Control><Alt>p".
945 * @action_name must be the name of an action as it would be used
946 * in the app menu, i.e. actions that have been added to the application
947 * are referred to with an "app." prefix, and window-specific actions
948 * with a "win." prefix.
950 * GtkApplication also extracts accelerators out of 'accel' attributes
951 * in the #GMenuModels passed to gtk_application_set_app_menu() and
952 * gtk_application_set_menubar(), which is usually more convenient
953 * than calling this function for each accelerator.
958 gtk_application_add_accelerator (GtkApplication *application,
959 const gchar *accelerator,
960 const gchar *action_name,
965 GdkModifierType accel_mods;
967 g_return_if_fail (GTK_IS_APPLICATION (application));
969 /* Call this here, since gtk_init() is only getting called in startup() */
970 _gtk_accel_map_init ();
972 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
976 g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
980 accel_path = _gtk_accel_path_for_action (action_name, parameter);
982 if (gtk_accel_map_lookup_entry (accel_path, NULL))
983 gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
985 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
991 * gtk_application_remove_accelerator:
992 * @application: a #GtkApplication
993 * @action_name: the name of the action to activate
994 * @parameter: (allow-none): parameter to pass when activating the action,
995 * or %NULL if the action does not accept an activation parameter
997 * Removes an accelerator that has been previously added
998 * with gtk_application_add_accelerator().
1003 gtk_application_remove_accelerator (GtkApplication *application,
1004 const gchar *action_name,
1005 GVariant *parameter)
1009 g_return_if_fail (GTK_IS_APPLICATION (application));
1011 accel_path = _gtk_accel_path_for_action (action_name, parameter);
1013 if (!gtk_accel_map_lookup_entry (accel_path, NULL))
1015 g_warning ("No accelerator found for '%s'\n", accel_path);
1016 g_free (accel_path);
1020 gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
1021 g_free (accel_path);
1025 * gtk_application_set_app_menu:
1026 * @application: a #GtkApplication
1027 * @app_menu: (allow-none): a #GMenuModel, or %NULL
1029 * Sets or unsets the application menu for @application.
1031 * The application menu is a single menu containing items that typically
1032 * impact the application as a whole, rather than acting on a specific
1033 * window or document. For example, you would expect to see
1034 * "Preferences" or "Quit" in an application menu, but not "Save" or
1037 * If supported, the application menu will be rendered by the desktop
1043 gtk_application_set_app_menu (GtkApplication *application,
1044 GMenuModel *app_menu)
1046 g_return_if_fail (GTK_IS_APPLICATION (application));
1048 if (app_menu != application->priv->app_menu)
1050 if (application->priv->app_menu != NULL)
1051 g_object_unref (application->priv->app_menu);
1053 application->priv->app_menu = app_menu;
1055 if (application->priv->app_menu != NULL)
1056 g_object_ref (application->priv->app_menu);
1058 extract_accels_from_menu (app_menu, application);
1060 #ifdef GDK_WINDOWING_X11
1061 gtk_application_set_app_menu_x11 (application, app_menu);
1064 g_object_notify (G_OBJECT (application), "app-menu");
1069 * gtk_application_get_app_menu:
1070 * @application: a #GtkApplication
1072 * Returns the menu model that has been set with
1073 * gtk_application_set_app_menu().
1075 * Returns: (transfer none): the application menu of @application
1080 gtk_application_get_app_menu (GtkApplication *application)
1082 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1084 return application->priv->app_menu;
1088 * gtk_application_set_menubar:
1089 * @application: a #GtkApplication
1090 * @menubar: (allow-none): a #GMenuModel, or %NULL
1092 * Sets or unsets the menubar for windows of @application.
1094 * This is a menubar in the traditional sense.
1096 * Depending on the desktop environment, this may appear at the top of
1097 * each window, or at the top of the screen. In some environments, if
1098 * both the application menu and the menubar are set, the application
1099 * menu will be presented as if it were the first item of the menubar.
1100 * Other environments treat the two as completely separate -- for
1101 * example, the application menu may be rendered by the desktop shell
1102 * while the menubar (if set) remains in each individual window.
1107 gtk_application_set_menubar (GtkApplication *application,
1108 GMenuModel *menubar)
1110 g_return_if_fail (GTK_IS_APPLICATION (application));
1112 if (menubar != application->priv->menubar)
1114 if (application->priv->menubar != NULL)
1115 g_object_unref (application->priv->menubar);
1117 application->priv->menubar = menubar;
1119 if (application->priv->menubar != NULL)
1120 g_object_ref (application->priv->menubar);
1122 extract_accels_from_menu (menubar, application);
1124 #ifdef GDK_WINDOWING_X11
1125 gtk_application_set_menubar_x11 (application, menubar);
1128 g_object_notify (G_OBJECT (application), "menubar");
1133 * gtk_application_get_menubar:
1134 * @application: a #GtkApplication
1136 * Returns the menu model that has been set with
1137 * gtk_application_set_menubar().
1139 * Returns: (transfer none): the menubar for windows of @application
1144 gtk_application_get_menubar (GtkApplication *application)
1146 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1148 return application->priv->menubar;
1151 #if defined(GDK_WINDOWING_X11)
1153 /* D-Bus Session Management
1155 * The protocol and the D-Bus API are described here:
1156 * http://live.gnome.org/SessionManagement/GnomeSession
1157 * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1161 unregister_client (GtkApplication *app)
1163 GError *error = NULL;
1165 g_debug ("Unregistering client");
1167 g_dbus_proxy_call_sync (app->priv->sm_proxy,
1169 g_variant_new ("(o)", app->priv->client_path),
1170 G_DBUS_CALL_FLAGS_NONE,
1177 g_warning ("Failed to unregister client: %s", error->message);
1178 g_error_free (error);
1181 g_clear_object (&app->priv->client_proxy);
1183 g_free (app->priv->client_path);
1184 app->priv->client_path = NULL;
1188 gtk_application_quit_response (GtkApplication *application,
1190 const gchar *reason)
1192 g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1194 g_dbus_proxy_call (application->priv->client_proxy,
1195 "EndSessionResponse",
1196 g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1197 G_DBUS_CALL_FLAGS_NONE,
1202 client_proxy_signal (GDBusProxy *proxy,
1203 const gchar *sender_name,
1204 const gchar *signal_name,
1205 GVariant *parameters,
1206 GtkApplication *app)
1208 if (strcmp (signal_name, "QueryEndSession") == 0)
1210 g_debug ("Received QueryEndSession");
1211 gtk_application_quit_response (app, TRUE, NULL);
1213 else if (strcmp (signal_name, "CancelEndSession") == 0)
1215 g_debug ("Received CancelEndSession");
1217 else if (strcmp (signal_name, "EndSession") == 0)
1219 g_debug ("Received EndSession");
1220 gtk_application_quit_response (app, TRUE, NULL);
1221 unregister_client (app);
1222 g_signal_emit (app, gtk_application_signals[QUIT], 0);
1224 else if (strcmp (signal_name, "Stop") == 0)
1226 g_debug ("Received Stop");
1227 unregister_client (app);
1228 g_signal_emit (app, gtk_application_signals[QUIT], 0);
1233 gtk_application_startup_session_dbus (GtkApplication *app)
1235 static gchar *client_id;
1236 GError *error = NULL;
1239 if (app->priv->session_bus == NULL)
1242 if (client_id == NULL)
1244 const gchar *desktop_autostart_id;
1246 desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1247 /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1248 * use the same client id.
1250 g_unsetenv ("DESKTOP_AUTOSTART_ID");
1251 client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1254 g_debug ("Connecting to session manager");
1256 app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1257 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1258 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1260 "org.gnome.SessionManager",
1261 "/org/gnome/SessionManager",
1262 "org.gnome.SessionManager",
1267 g_warning ("Failed to get a session proxy: %s", error->message);
1268 g_error_free (error);
1272 /* FIXME: should we reuse the D-Bus application id here ? */
1273 app->priv->app_id = g_strdup (g_get_prgname ());
1275 if (!app->priv->register_session)
1278 g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1280 res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1282 g_variant_new ("(ss)", app->priv->app_id, client_id),
1283 G_DBUS_CALL_FLAGS_NONE,
1290 g_warning ("Failed to register client: %s", error->message);
1291 g_error_free (error);
1292 g_clear_object (&app->priv->sm_proxy);
1296 g_variant_get (res, "(o)", &app->priv->client_path);
1297 g_variant_unref (res);
1299 g_debug ("Registered client at '%s'", app->priv->client_path);
1301 app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1303 "org.gnome.SessionManager",
1304 app->priv->client_path,
1305 "org.gnome.SessionManager.ClientPrivate",
1310 g_warning ("Failed to get client proxy: %s", error->message);
1311 g_error_free (error);
1312 g_clear_object (&app->priv->sm_proxy);
1313 g_free (app->priv->client_path);
1314 app->priv->client_path = NULL;
1318 g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1324 * GtkApplicationInhibitFlags:
1325 * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit ending the user session
1326 * by logging out or by shutting down the computer
1327 * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1328 * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1329 * session or computer
1330 * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1331 * marked as idle (and possibly locked)
1333 * Types of user actions that may be blocked by gtk_application_inhibit().
1339 * gtk_application_inhibit:
1340 * @application: the #GApplication
1341 * @window: (allow-none): a #GtkWindow, or %NULL
1342 * @flags: what types of actions should be inhibited
1343 * @reason: (allow-none): a short, human-readable string that explains
1344 * why these operations are inhibited
1346 * Inform the session manager that certain types of actions should be
1347 * inhibited. This is not guaranteed to work on all platforms and for
1348 * all types of actions.
1350 * Applications should invoke this method when they begin an operation
1351 * that should not be interrupted, such as creating a CD or DVD. The
1352 * types of actions that may be blocked are specified by the @flags
1353 * parameter. When the application completes the operation it should
1354 * call g_application_uninhibit() to remove the inhibitor. Note that
1355 * an application can have multiple inhibitors, and all of the must
1356 * be individually removed. Inhibitors are also cleared when the
1357 * application exits.
1359 * Applications should not expect that they will always be able to block
1360 * the action. In most cases, users will be given the option to force
1361 * the action to take place.
1363 * Reasons should be short and to the point.
1365 * If @window is given, the session manager may point the user to
1366 * this window to find out more about why the action is inhibited.
1368 * Returns: A non-zero cookie that is used to uniquely identify this
1369 * request. It should be used as an argument to g_application_uninhibit()
1370 * in order to remove the request. If the platform does not support
1371 * inhibiting or the request failed for some reason, 0 is returned.
1376 gtk_application_inhibit (GtkApplication *application,
1378 GtkApplicationInhibitFlags flags,
1379 const gchar *reason)
1382 GError *error = NULL;
1386 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1387 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1388 g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1391 xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1395 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1397 g_variant_new ("(susu)",
1398 application->priv->app_id,
1402 G_DBUS_CALL_FLAGS_NONE,
1408 g_warning ("Calling Inhibit failed: %s", error->message);
1409 g_error_free (error);
1413 g_variant_get (res, "(u)", &cookie);
1414 g_variant_unref (res);
1420 * gtk_application_uninhibit:
1421 * @application: the #GApplication
1422 * @cookie: a cookie that was returned by g_application_inhibit()
1424 * Removes an inhibitor that has been established with g_application_inhibit().
1425 * Inhibitors are also cleared when the application exits.
1430 gtk_application_uninhibit (GtkApplication *application,
1433 g_return_if_fail (GTK_IS_APPLICATION (application));
1434 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1435 g_return_if_fail (application->priv->sm_proxy != NULL);
1437 g_dbus_proxy_call (application->priv->sm_proxy,
1439 g_variant_new ("(u)", cookie),
1440 G_DBUS_CALL_FLAGS_NONE,
1446 * gtk_application_is_inhibited:
1447 * @application: the #GApplication
1448 * @flags: what types of actions should be queried
1450 * Determines if any of the actions specified in @flags are
1451 * currently inhibited (possibly by another application).
1453 * Returns: %TRUE if any of the actions specified in @flags are inhibited
1458 gtk_application_is_inhibited (GtkApplication *application,
1459 GtkApplicationInhibitFlags flags)
1462 GError *error = NULL;
1465 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1466 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1467 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1469 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1471 g_variant_new ("(u)", flags),
1472 G_DBUS_CALL_FLAGS_NONE,
1478 g_warning ("Calling IsInhibited failed: %s", error->message);
1479 g_error_free (error);
1483 g_variant_get (res, "(b)", &inhibited);
1484 g_variant_unref (res);
1490 * GtkApplicationEndSessionStyle:
1491 * @GTK_APPLICATION_LOGOUT: End the session by logging out
1492 * @GTK_APPLICATION_REBOOT: Restart the computer
1493 * @GTK_APPLICATION_SHUTDOWN: Shut the computer down
1495 * Different ways to end a user session, for use with
1496 * gtk_application_end_session().
1500 * gtk_application_end_session:
1501 * @application: the #GtkApplication
1502 * @style: the desired kind of session end
1503 * @request_confirmation: whether or not the user should get a chance
1504 * to confirm the action
1506 * Requests that the session manager end the current session.
1507 * @style indicates how the session should be ended, and
1508 * @request_confirmation indicates whether or not the user should be
1509 * given a chance to confirm the action. Both of these parameters are
1510 * merely hints though; the session manager may choose to ignore them.
1512 * Return value: %TRUE if the request was sent; %FALSE if it could not
1513 * be sent (eg, because it could not connect to the session manager)
1518 gtk_application_end_session (GtkApplication *application,
1519 GtkApplicationEndSessionStyle style,
1520 gboolean request_confirmation)
1522 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1523 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1524 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1528 case GTK_APPLICATION_LOGOUT:
1529 g_dbus_proxy_call (application->priv->sm_proxy,
1531 g_variant_new ("(u)", request_confirmation ? 0 : 1),
1532 G_DBUS_CALL_FLAGS_NONE,
1536 case GTK_APPLICATION_REBOOT:
1537 case GTK_APPLICATION_SHUTDOWN:
1538 g_dbus_proxy_call (application->priv->sm_proxy,
1541 G_DBUS_CALL_FLAGS_NONE,
1550 #elif defined(GDK_WINDOWING_QUARTZ)
1552 /* OS X implementation copied from EggSMClient, but simplified since
1553 * it doesn't need to interact with the user.
1557 idle_will_quit (gpointer data)
1559 GtkApplication *app = data;
1561 if (app->priv->quit_inhibit == 0)
1562 g_signal_emit (app, gtk_application_signals[QUIT], 0);
1565 GtkApplicationQuartzInhibitor *inhibitor;
1569 for (iter = app->priv->inhibitors; iter; iter = iter->next)
1571 inhibitor = iter->data;
1572 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1575 g_assert (inhibitor != NULL);
1577 dialog = gtk_message_dialog_new (inhibitor->window,
1581 _("%s cannot quit at this time:\n\n%s"),
1582 g_get_application_name (),
1584 gtk_dialog_run (GTK_DIALOG (dialog));
1585 gtk_widget_destroy (dialog);
1592 quit_requested (const AppleEvent *aevt,
1596 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1598 /* Don't emit the "quit" signal immediately, since we're
1599 * called from a weird point in the guts of gdkeventloop-quartz.c
1601 g_idle_add_full (G_PRIORITY_DEFAULT, idle_will_quit, app, NULL);
1603 return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
1607 gtk_application_startup_session_quartz (GtkApplication *app)
1609 if (app->priv->register_session)
1610 AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1611 NewAEEventHandlerUPP (quit_requested),
1612 (long)GPOINTER_TO_SIZE (app), false);
1616 gtk_application_inhibit (GtkApplication *application,
1618 GtkApplicationInhibitFlags flags,
1619 const gchar *reason)
1621 GtkApplicationQuartzInhibitor *inhibitor;
1623 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1624 g_return_val_if_fail (flags != 0, 0);
1626 inhibitor = g_slice_new (GtkApplicationQuartzInhibitor);
1627 inhibitor->cookie = ++application->priv->next_cookie;
1628 inhibitor->flags = flags;
1629 inhibitor->reason = g_strdup (reason);
1630 inhibitor->window = window ? g_object_ref (window) : NULL;
1632 application->priv->inhibitors = g_slist_prepend (application->priv->inhibitors, inhibitor);
1634 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1635 application->priv->quit_inhibit++;
1637 return inhibitor->cookie;
1641 gtk_application_uninhibit (GtkApplication *application,
1646 for (iter = application->priv->inhibitors; iter; iter = iter->next)
1648 GtkApplicationQuartzInhibitor *inhibitor = iter->data;
1650 if (inhibitor->cookie == cookie)
1652 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1653 application->priv->quit_inhibit--;
1654 gtk_application_quartz_inhibitor_free (inhibitor);
1655 application->priv->inhibitors = g_slist_delete_link (application->priv->inhibitors, iter);
1660 g_warning ("Invalid inhibitor cookie");
1664 gtk_application_is_inhibited (GtkApplication *application,
1665 GtkApplicationInhibitFlags flags)
1667 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1668 return application->priv->quit_inhibit > 0;
1674 gtk_application_end_session (GtkApplication *application,
1675 GtkApplicationEndSessionStyle style,
1676 gboolean request_confirmation)
1678 static const ProcessSerialNumber loginwindow_psn = { 0, kSystemProcess };
1679 AppleEvent event = { typeNull, NULL };
1680 AppleEvent reply = { typeNull, NULL };
1681 AEAddressDesc target;
1687 case GTK_APPLICATION_LOGOUT:
1688 id = request_confirmation ? kAELogOut : kAEReallyLogOut;
1690 case GTK_APPLICATION_REBOOT:
1691 id = request_confirmation ? kAEShowRestartDialog : kAERestart;
1693 case GTK_APPLICATION_SHUTDOWN:
1694 id = request_confirmation ? kAEShowShutdownDialog : kAEShutDown;
1698 err = AECreateDesc (typeProcessSerialNumber, &loginwindow_psn,
1699 sizeof (loginwindow_psn), &target);
1702 g_warning ("Could not create descriptor for loginwindow: %d", err);
1706 err = AECreateAppleEvent (kCoreEventClass, id, &target,
1707 kAutoGenerateReturnID, kAnyTransactionID,
1709 AEDisposeDesc (&target);
1712 g_warning ("Could not create logout AppleEvent: %d", err);
1716 err = AESend (&event, &reply, kAENoReply, kAENormalPriority,
1717 kAEDefaultTimeout, NULL, NULL);
1718 AEDisposeDesc (&event);
1720 AEDisposeDesc (&reply);
1722 return err == noErr;
1727 /* Trivial implementation.
1729 * For the inhibit API on Windows, see
1730 * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1734 gtk_application_inhibit (GtkApplication *application,
1736 GtkApplicationInhibitFlags flags,
1737 const gchar *reason)
1743 gtk_application_uninhibit (GtkApplication *application,
1749 gtk_application_is_inhibited (GtkApplication *application,
1750 GtkApplicationInhibitFlags flags)
1756 gtk_application_end_session (GtkApplication *application,
1757 GtkApplicationEndSessionStyle style,
1758 gboolean request_confirmation)