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, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
22 #include "gtkapplication.h"
30 #include "gtkapplicationprivate.h"
31 #include "gtkclipboard.h"
32 #include "gtkmarshalers.h"
34 #include "gtkrecentmanager.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 block various ways to end the session with
112 * the gtk_application_inhibit() function. Typical use cases for
113 * this kind of inhibiting are long-running, uninterruptible operations,
114 * such as burning a CD or performing a disk backup. The session
115 * manager may not honor the inhibitor, but it can be expected to
116 * inform the user about the negative consequences of ending the
117 * session while inhibitors are present.
126 static guint gtk_application_signals[LAST_SIGNAL];
130 PROP_REGISTER_SESSION,
135 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
137 struct _GtkApplicationPrivate
141 gboolean register_session;
143 GMenuModel *app_menu;
146 #ifdef GDK_WINDOWING_X11
147 GDBusConnection *session_bus;
148 const gchar *application_id;
151 gchar *app_menu_path;
159 GDBusProxy *sm_proxy;
160 GDBusProxy *client_proxy;
165 #ifdef GDK_WINDOWING_QUARTZ
175 #ifdef GDK_WINDOWING_X11
177 gtk_application_x11_publish_menu (GtkApplication *application,
185 /* unexport any existing menu */
188 g_dbus_connection_unexport_menu_model (application->priv->session_bus, *id);
194 /* export the new menu, if there is one */
197 /* try getting the preferred name */
198 *path = g_strconcat (application->priv->object_path, "/menus/", type, NULL);
199 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
201 /* keep trying until we get a working name... */
202 for (i = 0; *id == 0; i++)
205 *path = g_strdup_printf ("%s/menus/%s%d", application->priv->object_path, type, i);
206 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
212 gtk_application_set_app_menu_x11 (GtkApplication *application,
213 GMenuModel *app_menu)
215 gtk_application_x11_publish_menu (application, "appmenu", app_menu,
216 &application->priv->app_menu_id,
217 &application->priv->app_menu_path);
221 gtk_application_set_menubar_x11 (GtkApplication *application,
224 gtk_application_x11_publish_menu (application, "menubar", menubar,
225 &application->priv->menubar_id,
226 &application->priv->menubar_path);
230 gtk_application_window_added_x11 (GtkApplication *application,
233 if (application->priv->session_bus == NULL)
236 if (GTK_IS_APPLICATION_WINDOW (window))
238 GtkApplicationWindow *app_window = GTK_APPLICATION_WINDOW (window);
241 /* GtkApplicationWindow associates with us when it is first created,
242 * so surely it's not realized yet...
244 g_assert (!gtk_widget_get_realized (GTK_WIDGET (window)));
251 window_id = application->priv->next_id++;
252 window_path = g_strdup_printf ("%s/window/%d", application->priv->object_path, window_id);
253 success = gtk_application_window_publish (app_window, application->priv->session_bus, window_path);
254 g_free (window_path);
261 gtk_application_window_removed_x11 (GtkApplication *application,
264 if (application->priv->session_bus == NULL)
267 if (GTK_IS_APPLICATION_WINDOW (window))
268 gtk_application_window_unpublish (GTK_APPLICATION_WINDOW (window));
272 object_path_from_appid (const gchar *appid)
274 gchar *appid_path, *iter;
276 appid_path = g_strconcat ("/", appid, NULL);
277 for (iter = appid_path; *iter; iter++)
289 static void gtk_application_startup_session_dbus (GtkApplication *app);
292 gtk_application_startup_x11 (GtkApplication *application)
294 const gchar *application_id;
296 application_id = g_application_get_application_id (G_APPLICATION (application));
297 application->priv->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
298 application->priv->object_path = object_path_from_appid (application_id);
300 gtk_application_startup_session_dbus (GTK_APPLICATION (application));
304 gtk_application_shutdown_x11 (GtkApplication *application)
306 g_free (application->priv->object_path);
307 application->priv->object_path = NULL;
308 g_clear_object (&application->priv->session_bus);
310 g_clear_object (&application->priv->sm_proxy);
311 g_clear_object (&application->priv->client_proxy);
312 g_free (application->priv->app_id);
313 g_free (application->priv->client_path);
317 gtk_application_get_dbus_object_path (GtkApplication *application)
319 return application->priv->object_path;
323 gtk_application_get_app_menu_object_path (GtkApplication *application)
325 return application->priv->app_menu_path;
329 gtk_application_get_menubar_object_path (GtkApplication *application)
331 return application->priv->menubar_path;
336 #ifdef GDK_WINDOWING_QUARTZ
340 GtkApplicationInhibitFlags flags;
343 } GtkApplicationQuartzInhibitor;
346 gtk_application_quartz_inhibitor_free (GtkApplicationQuartzInhibitor *inhibitor)
348 g_free (inhibitor->reason);
349 g_clear_object (&inhibitor->window);
350 g_slice_free (GtkApplicationQuartzInhibitor, inhibitor);
354 gtk_application_menu_changed_quartz (GObject *object,
358 GtkApplication *application = GTK_APPLICATION (object);
361 combined = g_menu_new ();
362 g_menu_append_submenu (combined, "Application", gtk_application_get_app_menu (application));
363 g_menu_append_section (combined, NULL, gtk_application_get_menubar (application));
365 gtk_quartz_set_main_menu (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (application->priv->muxer));
368 static void gtk_application_startup_session_quartz (GtkApplication *app);
371 gtk_application_startup_quartz (GtkApplication *application)
373 [NSApp finishLaunching];
375 application->priv->muxer = g_action_muxer_new ();
376 g_action_muxer_insert (application->priv->muxer, "app", G_ACTION_GROUP (application));
378 g_signal_connect (application, "notify::app-menu", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
379 g_signal_connect (application, "notify::menubar", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
380 gtk_application_menu_changed_quartz (G_OBJECT (application), NULL, NULL);
382 gtk_application_startup_session_quartz (application);
386 gtk_application_shutdown_quartz (GtkApplication *application)
388 g_signal_handlers_disconnect_by_func (application, gtk_application_menu_changed_quartz, NULL);
390 g_object_unref (application->priv->muxer);
391 application->priv->muxer = NULL;
393 g_slist_free_full (application->priv->inhibitors,
394 (GDestroyNotify) gtk_application_quartz_inhibitor_free);
395 application->priv->inhibitors = NULL;
399 gtk_application_focus_changed (GtkApplication *application,
402 if (G_IS_ACTION_GROUP (window))
403 g_action_muxer_insert (application->priv->muxer, "win", G_ACTION_GROUP (window));
405 g_action_muxer_remove (application->priv->muxer, "win");
410 gtk_application_focus_in_event_cb (GtkWindow *window,
411 GdkEventFocus *event,
412 GtkApplication *application)
414 GtkApplicationPrivate *priv = application->priv;
417 /* Keep the window list sorted by most-recently-focused. */
418 link = g_list_find (priv->windows, window);
419 if (link != NULL && link != priv->windows)
421 priv->windows = g_list_remove_link (priv->windows, link);
422 priv->windows = g_list_concat (link, priv->windows);
425 #ifdef GDK_WINDOWING_QUARTZ
426 gtk_application_focus_changed (application, window);
433 gtk_application_startup (GApplication *application)
435 G_APPLICATION_CLASS (gtk_application_parent_class)
436 ->startup (application);
440 #ifdef GDK_WINDOWING_X11
441 gtk_application_startup_x11 (GTK_APPLICATION (application));
444 #ifdef GDK_WINDOWING_QUARTZ
445 gtk_application_startup_quartz (GTK_APPLICATION (application));
450 gtk_application_shutdown (GApplication *application)
452 #ifdef GDK_WINDOWING_X11
453 gtk_application_shutdown_x11 (GTK_APPLICATION (application));
456 #ifdef GDK_WINDOWING_QUARTZ
457 gtk_application_shutdown_quartz (GTK_APPLICATION (application));
460 /* Try storing all clipboard data we have */
461 _gtk_clipboard_store_all ();
463 /* Synchronize the recent manager singleton */
464 _gtk_recent_manager_sync ();
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_finalize (GObject *object)
680 GtkApplication *application = GTK_APPLICATION (object);
682 g_clear_object (&application->priv->app_menu);
683 g_clear_object (&application->priv->menubar);
685 G_OBJECT_CLASS (gtk_application_parent_class)
690 gtk_application_class_init (GtkApplicationClass *class)
692 GObjectClass *object_class = G_OBJECT_CLASS (class);
693 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
695 object_class->get_property = gtk_application_get_property;
696 object_class->set_property = gtk_application_set_property;
697 object_class->finalize = gtk_application_finalize;
699 application_class->add_platform_data = gtk_application_add_platform_data;
700 application_class->before_emit = gtk_application_before_emit;
701 application_class->after_emit = gtk_application_after_emit;
702 application_class->startup = gtk_application_startup;
703 application_class->shutdown = gtk_application_shutdown;
705 class->window_added = gtk_application_window_added;
706 class->window_removed = gtk_application_window_removed;
708 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
711 * GtkApplication::window-added:
712 * @application: the #GtkApplication which emitted the signal
713 * @window: the newly-added #GtkWindow
715 * Emitted when a #GtkWindow is added to @application through
716 * gtk_application_add_window().
720 gtk_application_signals[WINDOW_ADDED] =
721 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
722 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
724 g_cclosure_marshal_VOID__OBJECT,
725 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
728 * GtkApplication::window-removed:
729 * @application: the #GtkApplication which emitted the signal
730 * @window: the #GtkWindow that is being removed
732 * Emitted when a #GtkWindow is removed from @application,
733 * either as a side-effect of being destroyed or explicitly
734 * through gtk_application_remove_window().
738 gtk_application_signals[WINDOW_REMOVED] =
739 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
740 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
742 g_cclosure_marshal_VOID__OBJECT,
743 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
746 * GtkApplication:register-session:
748 * Set this property to %TRUE to register with the session manager.
752 g_object_class_install_property (object_class, PROP_REGISTER_SESSION,
753 g_param_spec_boolean ("register-session",
754 P_("Register session"),
755 P_("Register with the session manager"),
756 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
758 g_object_class_install_property (object_class, PROP_APP_MENU,
759 g_param_spec_object ("app-menu",
760 P_("Application menu"),
761 P_("The GMenuModel for the application menu"),
763 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
765 g_object_class_install_property (object_class, PROP_MENUBAR,
766 g_param_spec_object ("menubar",
768 P_("The GMenuModel for the menubar"),
770 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
774 * gtk_application_new:
775 * @application_id: the application id
776 * @flags: the application flags
778 * Creates a new #GtkApplication instance.
780 * This function calls g_type_init() for you. gtk_init() is called
781 * as soon as the application gets registered as the primary instance.
783 * Concretely, gtk_init() is called in the default handler for the
784 * startup() signal. Therefore, #GtkApplication subclasses should
785 * chain up in their startup() handler before using any GTK+ API.
787 * Note that commandline arguments are not passed to gtk_init().
788 * All GTK+ functionality that is available via commandline arguments
789 * can also be achieved by setting suitable environment variables
790 * such as <envar>G_DEBUG</envar>, so this should not be a big
791 * problem. If you absolutely must support GTK+ commandline arguments,
792 * you can explicitly call gtk_init() before creating the application
795 * The application id must be valid. See g_application_id_is_valid().
797 * Returns: a new #GtkApplication instance
802 gtk_application_new (const gchar *application_id,
803 GApplicationFlags flags)
805 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
809 return g_object_new (GTK_TYPE_APPLICATION,
810 "application-id", application_id,
816 * gtk_application_add_window:
817 * @application: a #GtkApplication
818 * @window: a #GtkWindow
820 * Adds a window from @application.
822 * This call is equivalent to setting the #GtkWindow:application
823 * property of @window to @application.
825 * Normally, the connection between the application and the window
826 * will remain until the window is destroyed, but you can explicitly
827 * remove it with gtk_application_remove_window().
829 * GTK+ will keep the application running as long as it has
835 gtk_application_add_window (GtkApplication *application,
838 g_return_if_fail (GTK_IS_APPLICATION (application));
840 if (!g_list_find (application->priv->windows, window))
841 g_signal_emit (application,
842 gtk_application_signals[WINDOW_ADDED], 0, window);
846 * gtk_application_remove_window:
847 * @application: a #GtkApplication
848 * @window: a #GtkWindow
850 * Remove a window from @application.
852 * If @window belongs to @application then this call is equivalent to
853 * setting the #GtkWindow:application property of @window to
856 * The application may stop running as a result of a call to this
862 gtk_application_remove_window (GtkApplication *application,
865 g_return_if_fail (GTK_IS_APPLICATION (application));
867 if (g_list_find (application->priv->windows, window))
868 g_signal_emit (application,
869 gtk_application_signals[WINDOW_REMOVED], 0, window);
873 * gtk_application_get_windows:
874 * @application: a #GtkApplication
876 * Gets a list of the #GtkWindows associated with @application.
878 * The list is sorted by most recently focused window, such that the first
879 * element is the currently focused window. (Useful for choosing a parent
880 * for a transient window.)
882 * The list that is returned should not be modified in any way. It will
883 * only remain valid until the next focus change or window creation or
886 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
891 gtk_application_get_windows (GtkApplication *application)
893 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
895 return application->priv->windows;
899 * gtk_application_add_accelerator:
900 * @application: a #GtkApplication
901 * @accelerator: accelerator string
902 * @action_name: the name of the action to activate
903 * @parameter: (allow-none): parameter to pass when activating the action,
904 * or %NULL if the action does not accept an activation parameter
906 * Installs an accelerator that will cause the named action
907 * to be activated when the key combination specificed by @accelerator
910 * @accelerator must be a string that can be parsed by
911 * gtk_accelerator_parse(), e.g. "<Primary>q" or "<Control><Alt>p".
913 * @action_name must be the name of an action as it would be used
914 * in the app menu, i.e. actions that have been added to the application
915 * are referred to with an "app." prefix, and window-specific actions
916 * with a "win." prefix.
918 * GtkApplication also extracts accelerators out of 'accel' attributes
919 * in the #GMenuModels passed to gtk_application_set_app_menu() and
920 * gtk_application_set_menubar(), which is usually more convenient
921 * than calling this function for each accelerator.
926 gtk_application_add_accelerator (GtkApplication *application,
927 const gchar *accelerator,
928 const gchar *action_name,
933 GdkModifierType accel_mods;
935 g_return_if_fail (GTK_IS_APPLICATION (application));
937 /* Call this here, since gtk_init() is only getting called in startup() */
938 _gtk_accel_map_init ();
940 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
944 g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
948 accel_path = _gtk_accel_path_for_action (action_name, parameter);
950 if (gtk_accel_map_lookup_entry (accel_path, NULL))
951 gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
953 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
959 * gtk_application_remove_accelerator:
960 * @application: a #GtkApplication
961 * @action_name: the name of the action to activate
962 * @parameter: (allow-none): parameter to pass when activating the action,
963 * or %NULL if the action does not accept an activation parameter
965 * Removes an accelerator that has been previously added
966 * with gtk_application_add_accelerator().
971 gtk_application_remove_accelerator (GtkApplication *application,
972 const gchar *action_name,
977 g_return_if_fail (GTK_IS_APPLICATION (application));
979 accel_path = _gtk_accel_path_for_action (action_name, parameter);
981 if (!gtk_accel_map_lookup_entry (accel_path, NULL))
983 g_warning ("No accelerator found for '%s'\n", accel_path);
988 gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
993 * gtk_application_set_app_menu:
994 * @application: a #GtkApplication
995 * @app_menu: (allow-none): a #GMenuModel, or %NULL
997 * Sets or unsets the application menu for @application.
999 * The application menu is a single menu containing items that typically
1000 * impact the application as a whole, rather than acting on a specific
1001 * window or document. For example, you would expect to see
1002 * "Preferences" or "Quit" in an application menu, but not "Save" or
1005 * If supported, the application menu will be rendered by the desktop
1011 gtk_application_set_app_menu (GtkApplication *application,
1012 GMenuModel *app_menu)
1014 g_return_if_fail (GTK_IS_APPLICATION (application));
1016 if (app_menu != application->priv->app_menu)
1018 if (application->priv->app_menu != NULL)
1019 g_object_unref (application->priv->app_menu);
1021 application->priv->app_menu = app_menu;
1023 if (application->priv->app_menu != NULL)
1024 g_object_ref (application->priv->app_menu);
1026 extract_accels_from_menu (app_menu, application);
1028 #ifdef GDK_WINDOWING_X11
1029 gtk_application_set_app_menu_x11 (application, app_menu);
1032 g_object_notify (G_OBJECT (application), "app-menu");
1037 * gtk_application_get_app_menu:
1038 * @application: a #GtkApplication
1040 * Returns the menu model that has been set with
1041 * gtk_application_set_app_menu().
1043 * Returns: (transfer none): the application menu of @application
1048 gtk_application_get_app_menu (GtkApplication *application)
1050 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1052 return application->priv->app_menu;
1056 * gtk_application_set_menubar:
1057 * @application: a #GtkApplication
1058 * @menubar: (allow-none): a #GMenuModel, or %NULL
1060 * Sets or unsets the menubar for windows of @application.
1062 * This is a menubar in the traditional sense.
1064 * Depending on the desktop environment, this may appear at the top of
1065 * each window, or at the top of the screen. In some environments, if
1066 * both the application menu and the menubar are set, the application
1067 * menu will be presented as if it were the first item of the menubar.
1068 * Other environments treat the two as completely separate -- for
1069 * example, the application menu may be rendered by the desktop shell
1070 * while the menubar (if set) remains in each individual window.
1075 gtk_application_set_menubar (GtkApplication *application,
1076 GMenuModel *menubar)
1078 g_return_if_fail (GTK_IS_APPLICATION (application));
1080 if (menubar != application->priv->menubar)
1082 if (application->priv->menubar != NULL)
1083 g_object_unref (application->priv->menubar);
1085 application->priv->menubar = menubar;
1087 if (application->priv->menubar != NULL)
1088 g_object_ref (application->priv->menubar);
1090 extract_accels_from_menu (menubar, application);
1092 #ifdef GDK_WINDOWING_X11
1093 gtk_application_set_menubar_x11 (application, menubar);
1096 g_object_notify (G_OBJECT (application), "menubar");
1101 * gtk_application_get_menubar:
1102 * @application: a #GtkApplication
1104 * Returns the menu model that has been set with
1105 * gtk_application_set_menubar().
1107 * Returns: (transfer none): the menubar for windows of @application
1112 gtk_application_get_menubar (GtkApplication *application)
1114 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1116 return application->priv->menubar;
1119 #if defined(GDK_WINDOWING_X11)
1121 /* D-Bus Session Management
1123 * The protocol and the D-Bus API are described here:
1124 * http://live.gnome.org/SessionManagement/GnomeSession
1125 * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1129 unregister_client (GtkApplication *app)
1131 GError *error = NULL;
1133 g_debug ("Unregistering client");
1135 g_dbus_proxy_call_sync (app->priv->sm_proxy,
1137 g_variant_new ("(o)", app->priv->client_path),
1138 G_DBUS_CALL_FLAGS_NONE,
1145 g_warning ("Failed to unregister client: %s", error->message);
1146 g_error_free (error);
1149 g_clear_object (&app->priv->client_proxy);
1151 g_free (app->priv->client_path);
1152 app->priv->client_path = NULL;
1156 gtk_application_quit_response (GtkApplication *application,
1158 const gchar *reason)
1160 g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1162 g_dbus_proxy_call (application->priv->client_proxy,
1163 "EndSessionResponse",
1164 g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1165 G_DBUS_CALL_FLAGS_NONE,
1170 client_proxy_signal (GDBusProxy *proxy,
1171 const gchar *sender_name,
1172 const gchar *signal_name,
1173 GVariant *parameters,
1174 GtkApplication *app)
1176 if (strcmp (signal_name, "QueryEndSession") == 0)
1178 g_debug ("Received QueryEndSession");
1179 gtk_application_quit_response (app, TRUE, NULL);
1181 else if (strcmp (signal_name, "CancelEndSession") == 0)
1183 g_debug ("Received CancelEndSession");
1185 else if (strcmp (signal_name, "EndSession") == 0)
1187 g_debug ("Received EndSession");
1188 gtk_application_quit_response (app, TRUE, NULL);
1189 unregister_client (app);
1190 g_application_quit (G_APPLICATION (app));
1192 else if (strcmp (signal_name, "Stop") == 0)
1194 g_debug ("Received Stop");
1195 unregister_client (app);
1196 g_application_quit (G_APPLICATION (app));
1201 gtk_application_startup_session_dbus (GtkApplication *app)
1203 static gchar *client_id;
1204 GError *error = NULL;
1207 if (app->priv->session_bus == NULL)
1210 if (client_id == NULL)
1212 const gchar *desktop_autostart_id;
1214 desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1215 /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1216 * use the same client id.
1218 g_unsetenv ("DESKTOP_AUTOSTART_ID");
1219 client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1222 g_debug ("Connecting to session manager");
1224 app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1225 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1226 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1228 "org.gnome.SessionManager",
1229 "/org/gnome/SessionManager",
1230 "org.gnome.SessionManager",
1235 g_warning ("Failed to get a session proxy: %s", error->message);
1236 g_error_free (error);
1240 /* FIXME: should we reuse the D-Bus application id here ? */
1241 app->priv->app_id = g_strdup (g_get_prgname ());
1243 if (!app->priv->register_session)
1246 g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1248 res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1250 g_variant_new ("(ss)", app->priv->app_id, client_id),
1251 G_DBUS_CALL_FLAGS_NONE,
1258 g_warning ("Failed to register client: %s", error->message);
1259 g_error_free (error);
1260 g_clear_object (&app->priv->sm_proxy);
1264 g_variant_get (res, "(o)", &app->priv->client_path);
1265 g_variant_unref (res);
1267 g_debug ("Registered client at '%s'", app->priv->client_path);
1269 app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1271 "org.gnome.SessionManager",
1272 app->priv->client_path,
1273 "org.gnome.SessionManager.ClientPrivate",
1278 g_warning ("Failed to get client proxy: %s", error->message);
1279 g_error_free (error);
1280 g_clear_object (&app->priv->sm_proxy);
1281 g_free (app->priv->client_path);
1282 app->priv->client_path = NULL;
1286 g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1292 * GtkApplicationInhibitFlags:
1293 * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit ending the user session
1294 * by logging out or by shutting down the computer
1295 * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1296 * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1297 * session or computer
1298 * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1299 * marked as idle (and possibly locked)
1301 * Types of user actions that may be blocked by gtk_application_inhibit().
1307 * gtk_application_inhibit:
1308 * @application: the #GApplication
1309 * @window: (allow-none): a #GtkWindow, or %NULL
1310 * @flags: what types of actions should be inhibited
1311 * @reason: (allow-none): a short, human-readable string that explains
1312 * why these operations are inhibited
1314 * Inform the session manager that certain types of actions should be
1315 * inhibited. This is not guaranteed to work on all platforms and for
1316 * all types of actions.
1318 * Applications should invoke this method when they begin an operation
1319 * that should not be interrupted, such as creating a CD or DVD. The
1320 * types of actions that may be blocked are specified by the @flags
1321 * parameter. When the application completes the operation it should
1322 * call g_application_uninhibit() to remove the inhibitor. Note that
1323 * an application can have multiple inhibitors, and all of the must
1324 * be individually removed. Inhibitors are also cleared when the
1325 * application exits.
1327 * Applications should not expect that they will always be able to block
1328 * the action. In most cases, users will be given the option to force
1329 * the action to take place.
1331 * Reasons should be short and to the point.
1333 * If @window is given, the session manager may point the user to
1334 * this window to find out more about why the action is inhibited.
1336 * Returns: A non-zero cookie that is used to uniquely identify this
1337 * request. It should be used as an argument to g_application_uninhibit()
1338 * in order to remove the request. If the platform does not support
1339 * inhibiting or the request failed for some reason, 0 is returned.
1344 gtk_application_inhibit (GtkApplication *application,
1346 GtkApplicationInhibitFlags flags,
1347 const gchar *reason)
1350 GError *error = NULL;
1354 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1355 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1356 g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1359 xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1363 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1365 g_variant_new ("(susu)",
1366 application->priv->app_id,
1370 G_DBUS_CALL_FLAGS_NONE,
1376 g_warning ("Calling Inhibit failed: %s", error->message);
1377 g_error_free (error);
1381 g_variant_get (res, "(u)", &cookie);
1382 g_variant_unref (res);
1388 * gtk_application_uninhibit:
1389 * @application: the #GApplication
1390 * @cookie: a cookie that was returned by g_application_inhibit()
1392 * Removes an inhibitor that has been established with g_application_inhibit().
1393 * Inhibitors are also cleared when the application exits.
1398 gtk_application_uninhibit (GtkApplication *application,
1401 g_return_if_fail (GTK_IS_APPLICATION (application));
1402 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1403 g_return_if_fail (application->priv->sm_proxy != NULL);
1405 g_dbus_proxy_call (application->priv->sm_proxy,
1407 g_variant_new ("(u)", cookie),
1408 G_DBUS_CALL_FLAGS_NONE,
1414 * gtk_application_is_inhibited:
1415 * @application: the #GApplication
1416 * @flags: what types of actions should be queried
1418 * Determines if any of the actions specified in @flags are
1419 * currently inhibited (possibly by another application).
1421 * Returns: %TRUE if any of the actions specified in @flags are inhibited
1426 gtk_application_is_inhibited (GtkApplication *application,
1427 GtkApplicationInhibitFlags flags)
1430 GError *error = NULL;
1433 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1434 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1435 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1437 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1439 g_variant_new ("(u)", flags),
1440 G_DBUS_CALL_FLAGS_NONE,
1446 g_warning ("Calling IsInhibited failed: %s", error->message);
1447 g_error_free (error);
1451 g_variant_get (res, "(b)", &inhibited);
1452 g_variant_unref (res);
1457 #elif defined(GDK_WINDOWING_QUARTZ)
1459 /* OS X implementation copied from EggSMClient, but simplified since
1460 * it doesn't need to interact with the user.
1464 idle_will_quit (gpointer data)
1466 GtkApplication *app = data;
1468 if (app->priv->quit_inhibit == 0)
1469 g_application_quit (G_APPLICATION (app));
1472 GtkApplicationQuartzInhibitor *inhibitor;
1476 for (iter = app->priv->inhibitors; iter; iter = iter->next)
1478 inhibitor = iter->data;
1479 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1482 g_assert (inhibitor != NULL);
1484 dialog = gtk_message_dialog_new (inhibitor->window,
1488 _("%s cannot quit at this time:\n\n%s"),
1489 g_get_application_name (),
1491 g_signal_connect_swapped (dialog,
1493 G_CALLBACK (gtk_widget_destroy),
1495 gtk_widget_show_all (dialog);
1498 return G_SOURCE_REMOVE;
1502 quit_requested (const AppleEvent *aevt,
1506 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1508 /* Don't emit the "quit" signal immediately, since we're
1509 * called from a weird point in the guts of gdkeventloop-quartz.c
1511 g_idle_add_full (G_PRIORITY_DEFAULT, idle_will_quit, app, NULL);
1513 return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
1517 gtk_application_startup_session_quartz (GtkApplication *app)
1519 if (app->priv->register_session)
1520 AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1521 NewAEEventHandlerUPP (quit_requested),
1522 (long)GPOINTER_TO_SIZE (app), false);
1526 gtk_application_inhibit (GtkApplication *application,
1528 GtkApplicationInhibitFlags flags,
1529 const gchar *reason)
1531 GtkApplicationQuartzInhibitor *inhibitor;
1533 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1534 g_return_val_if_fail (flags != 0, 0);
1536 inhibitor = g_slice_new (GtkApplicationQuartzInhibitor);
1537 inhibitor->cookie = ++application->priv->next_cookie;
1538 inhibitor->flags = flags;
1539 inhibitor->reason = g_strdup (reason);
1540 inhibitor->window = window ? g_object_ref (window) : NULL;
1542 application->priv->inhibitors = g_slist_prepend (application->priv->inhibitors, inhibitor);
1544 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1545 application->priv->quit_inhibit++;
1547 return inhibitor->cookie;
1551 gtk_application_uninhibit (GtkApplication *application,
1556 for (iter = application->priv->inhibitors; iter; iter = iter->next)
1558 GtkApplicationQuartzInhibitor *inhibitor = iter->data;
1560 if (inhibitor->cookie == cookie)
1562 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1563 application->priv->quit_inhibit--;
1564 gtk_application_quartz_inhibitor_free (inhibitor);
1565 application->priv->inhibitors = g_slist_delete_link (application->priv->inhibitors, iter);
1570 g_warning ("Invalid inhibitor cookie");
1574 gtk_application_is_inhibited (GtkApplication *application,
1575 GtkApplicationInhibitFlags flags)
1577 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1578 return application->priv->quit_inhibit > 0;
1585 /* Trivial implementation.
1587 * For the inhibit API on Windows, see
1588 * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1592 gtk_application_inhibit (GtkApplication *application,
1594 GtkApplicationInhibitFlags flags,
1595 const gchar *reason)
1601 gtk_application_uninhibit (GtkApplication *application,
1607 gtk_application_is_inhibited (GtkApplication *application,
1608 GtkApplicationInhibitFlags flags)