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 for 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 specify a menubar that will be shown by
82 * #GApplicationWindows, use gtk_application_set_menubar(). Use the base
83 * #GActionMap interface to add actions, to respond to the user
84 * selecting these menu items.
86 * GTK+ displays these menus as expected, depending on the platform
87 * the application is running on.
89 * <figure label="Menu integration in OS X">
90 * <graphic fileref="bloatpad-osx.png" format="PNG"/>
93 * <figure label="Menu integration in GNOME">
94 * <graphic fileref="bloatpad-gnome.png" format="PNG"/>
97 * <figure label="Menu integration in Xfce">
98 * <graphic fileref="bloatpad-xfce.png" format="PNG"/>
101 * <example id="gtkapplication"><title>A simple application</title>
103 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/bloatpad.c">
104 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
109 * GtkApplication optionally registers with a session manager
110 * of the users session (if you set the #GtkApplication:register-session
111 * property) and offers various functionality related to the session
114 * An application can block various ways to end the session with
115 * the gtk_application_inhibit() function. Typical use cases for
116 * this kind of inhibiting are long-running, uninterruptible operations,
117 * such as burning a CD or performing a disk backup. The session
118 * manager may not honor the inhibitor, but it can be expected to
119 * inform the user about the negative consequences of ending the
120 * session while inhibitors are present.
129 static guint gtk_application_signals[LAST_SIGNAL];
133 PROP_REGISTER_SESSION,
138 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
140 struct _GtkApplicationPrivate
144 gboolean register_session;
146 GMenuModel *app_menu;
149 #ifdef GDK_WINDOWING_X11
150 GDBusConnection *session_bus;
151 const gchar *application_id;
154 gchar *app_menu_path;
162 GDBusProxy *sm_proxy;
163 GDBusProxy *client_proxy;
168 #ifdef GDK_WINDOWING_QUARTZ
178 #ifdef GDK_WINDOWING_X11
180 gtk_application_x11_publish_menu (GtkApplication *application,
188 /* unexport any existing menu */
191 g_dbus_connection_unexport_menu_model (application->priv->session_bus, *id);
197 /* export the new menu, if there is one */
200 /* try getting the preferred name */
201 *path = g_strconcat (application->priv->object_path, "/menus/", type, NULL);
202 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
204 /* keep trying until we get a working name... */
205 for (i = 0; *id == 0; i++)
208 *path = g_strdup_printf ("%s/menus/%s%d", application->priv->object_path, type, i);
209 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
215 gtk_application_set_app_menu_x11 (GtkApplication *application,
216 GMenuModel *app_menu)
218 gtk_application_x11_publish_menu (application, "appmenu", app_menu,
219 &application->priv->app_menu_id,
220 &application->priv->app_menu_path);
224 gtk_application_set_menubar_x11 (GtkApplication *application,
227 gtk_application_x11_publish_menu (application, "menubar", menubar,
228 &application->priv->menubar_id,
229 &application->priv->menubar_path);
233 gtk_application_window_added_x11 (GtkApplication *application,
236 if (application->priv->session_bus == NULL)
239 if (GTK_IS_APPLICATION_WINDOW (window))
241 GtkApplicationWindow *app_window = GTK_APPLICATION_WINDOW (window);
244 /* GtkApplicationWindow associates with us when it is first created,
245 * so surely it's not realized yet...
247 g_assert (!gtk_widget_get_realized (GTK_WIDGET (window)));
254 window_id = application->priv->next_id++;
255 window_path = g_strdup_printf ("%s/window/%d", application->priv->object_path, window_id);
256 success = gtk_application_window_publish (app_window, application->priv->session_bus, window_path);
257 g_free (window_path);
264 gtk_application_window_removed_x11 (GtkApplication *application,
267 if (application->priv->session_bus == NULL)
270 if (GTK_IS_APPLICATION_WINDOW (window))
271 gtk_application_window_unpublish (GTK_APPLICATION_WINDOW (window));
275 object_path_from_appid (const gchar *appid)
277 gchar *appid_path, *iter;
279 appid_path = g_strconcat ("/", appid, NULL);
280 for (iter = appid_path; *iter; iter++)
292 static void gtk_application_startup_session_dbus (GtkApplication *app);
295 gtk_application_startup_x11 (GtkApplication *application)
297 const gchar *application_id;
299 application_id = g_application_get_application_id (G_APPLICATION (application));
300 application->priv->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
301 application->priv->object_path = object_path_from_appid (application_id);
303 gtk_application_startup_session_dbus (GTK_APPLICATION (application));
307 gtk_application_shutdown_x11 (GtkApplication *application)
309 g_free (application->priv->object_path);
310 application->priv->object_path = NULL;
311 g_clear_object (&application->priv->session_bus);
313 g_clear_object (&application->priv->sm_proxy);
314 g_clear_object (&application->priv->client_proxy);
315 g_free (application->priv->app_id);
316 g_free (application->priv->client_path);
320 gtk_application_get_dbus_object_path (GtkApplication *application)
322 return application->priv->object_path;
326 gtk_application_get_app_menu_object_path (GtkApplication *application)
328 return application->priv->app_menu_path;
332 gtk_application_get_menubar_object_path (GtkApplication *application)
334 return application->priv->menubar_path;
339 #ifdef GDK_WINDOWING_QUARTZ
343 GtkApplicationInhibitFlags flags;
346 } GtkApplicationQuartzInhibitor;
349 gtk_application_quartz_inhibitor_free (GtkApplicationQuartzInhibitor *inhibitor)
351 g_free (inhibitor->reason);
352 g_clear_object (&inhibitor->window);
353 g_slice_free (GtkApplicationQuartzInhibitor, inhibitor);
357 gtk_application_menu_changed_quartz (GObject *object,
361 GtkApplication *application = GTK_APPLICATION (object);
364 combined = g_menu_new ();
365 g_menu_append_submenu (combined, "Application", gtk_application_get_app_menu (application));
366 g_menu_append_section (combined, NULL, gtk_application_get_menubar (application));
368 gtk_quartz_set_main_menu (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (application->priv->muxer));
371 static void gtk_application_startup_session_quartz (GtkApplication *app);
374 gtk_application_startup_quartz (GtkApplication *application)
376 [NSApp finishLaunching];
378 application->priv->muxer = g_action_muxer_new ();
379 g_action_muxer_insert (application->priv->muxer, "app", G_ACTION_GROUP (application));
381 g_signal_connect (application, "notify::app-menu", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
382 g_signal_connect (application, "notify::menubar", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
383 gtk_application_menu_changed_quartz (G_OBJECT (application), NULL, NULL);
385 gtk_application_startup_session_quartz (application);
389 gtk_application_shutdown_quartz (GtkApplication *application)
391 g_signal_handlers_disconnect_by_func (application, gtk_application_menu_changed_quartz, NULL);
393 g_object_unref (application->priv->muxer);
394 application->priv->muxer = NULL;
396 g_slist_free_full (application->priv->inhibitors,
397 (GDestroyNotify) gtk_application_quartz_inhibitor_free);
398 application->priv->inhibitors = NULL;
402 gtk_application_focus_changed (GtkApplication *application,
405 if (G_IS_ACTION_GROUP (window))
406 g_action_muxer_insert (application->priv->muxer, "win", G_ACTION_GROUP (window));
408 g_action_muxer_remove (application->priv->muxer, "win");
413 gtk_application_focus_in_event_cb (GtkWindow *window,
414 GdkEventFocus *event,
415 GtkApplication *application)
417 GtkApplicationPrivate *priv = application->priv;
420 /* Keep the window list sorted by most-recently-focused. */
421 link = g_list_find (priv->windows, window);
422 if (link != NULL && link != priv->windows)
424 priv->windows = g_list_remove_link (priv->windows, link);
425 priv->windows = g_list_concat (link, priv->windows);
428 #ifdef GDK_WINDOWING_QUARTZ
429 gtk_application_focus_changed (application, window);
436 gtk_application_startup (GApplication *application)
438 G_APPLICATION_CLASS (gtk_application_parent_class)
439 ->startup (application);
443 #ifdef GDK_WINDOWING_X11
444 gtk_application_startup_x11 (GTK_APPLICATION (application));
447 #ifdef GDK_WINDOWING_QUARTZ
448 gtk_application_startup_quartz (GTK_APPLICATION (application));
453 gtk_application_shutdown (GApplication *application)
455 #ifdef GDK_WINDOWING_X11
456 gtk_application_shutdown_x11 (GTK_APPLICATION (application));
459 #ifdef GDK_WINDOWING_QUARTZ
460 gtk_application_shutdown_quartz (GTK_APPLICATION (application));
463 /* Try storing all clipboard data we have */
464 _gtk_clipboard_store_all ();
466 /* Synchronize the recent manager singleton */
467 _gtk_recent_manager_sync ();
469 G_APPLICATION_CLASS (gtk_application_parent_class)
470 ->shutdown (application);
474 gtk_application_add_platform_data (GApplication *application,
475 GVariantBuilder *builder)
477 const gchar *startup_id;
479 startup_id = getenv ("DESKTOP_STARTUP_ID");
481 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
482 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
483 g_variant_new_string (startup_id));
487 gtk_application_before_emit (GApplication *application,
488 GVariant *platform_data)
494 gdk_threads_enter ();
496 g_variant_iter_init (&iter, platform_data);
497 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
499 #ifdef GDK_WINDOWING_X11
500 if (strcmp (key, "desktop-startup-id") == 0)
505 display = gdk_display_get_default ();
506 id = g_variant_get_string (value, NULL);
507 if (GDK_IS_X11_DISPLAY (display))
508 gdk_x11_display_set_startup_notification_id (display, id);
515 gtk_application_after_emit (GApplication *application,
516 GVariant *platform_data)
518 gdk_notify_startup_complete ();
520 gdk_threads_leave ();
524 gtk_application_init (GtkApplication *application)
526 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
527 GTK_TYPE_APPLICATION,
528 GtkApplicationPrivate);
532 gtk_application_window_added (GtkApplication *application,
535 GtkApplicationPrivate *priv = application->priv;
537 priv->windows = g_list_prepend (priv->windows, window);
538 gtk_window_set_application (window, application);
539 g_application_hold (G_APPLICATION (application));
541 g_signal_connect (window, "focus-in-event",
542 G_CALLBACK (gtk_application_focus_in_event_cb),
545 #ifdef GDK_WINDOWING_X11
546 gtk_application_window_added_x11 (application, window);
551 gtk_application_window_removed (GtkApplication *application,
554 GtkApplicationPrivate *priv = application->priv;
556 #ifdef GDK_WINDOWING_X11
557 gtk_application_window_removed_x11 (application, window);
560 g_signal_handlers_disconnect_by_func (window,
561 gtk_application_focus_in_event_cb,
564 g_application_release (G_APPLICATION (application));
565 priv->windows = g_list_remove (priv->windows, window);
566 gtk_window_set_application (window, NULL);
570 extract_accel_from_menu_item (GMenuModel *model,
574 GMenuAttributeIter *iter;
577 const gchar *accel = NULL;
578 const gchar *action = NULL;
579 GVariant *target = NULL;
581 iter = g_menu_model_iterate_item_attributes (model, item);
582 while (g_menu_attribute_iter_get_next (iter, &key, &value))
584 if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
585 action = g_variant_get_string (value, NULL);
586 else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
587 accel = g_variant_get_string (value, NULL);
588 else if (g_str_equal (key, "target"))
589 target = g_variant_ref (value);
590 g_variant_unref (value);
592 g_object_unref (iter);
595 gtk_application_add_accelerator (app, accel, action, target);
598 g_variant_unref (target);
602 extract_accels_from_menu (GMenuModel *model,
610 for (i = 0; i < g_menu_model_get_n_items (model); i++)
612 extract_accel_from_menu_item (model, i, app);
614 iter = g_menu_model_iterate_item_links (model, i);
615 while (g_menu_link_iter_get_next (iter, &key, &m))
617 extract_accels_from_menu (m, app);
620 g_object_unref (iter);
625 gtk_application_get_property (GObject *object,
630 GtkApplication *application = GTK_APPLICATION (object);
634 case PROP_REGISTER_SESSION:
635 g_value_set_boolean (value, application->priv->register_session);
639 g_value_set_object (value, gtk_application_get_app_menu (application));
643 g_value_set_object (value, gtk_application_get_menubar (application));
647 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
653 gtk_application_set_property (GObject *object,
658 GtkApplication *application = GTK_APPLICATION (object);
662 case PROP_REGISTER_SESSION:
663 application->priv->register_session = g_value_get_boolean (value);
667 gtk_application_set_app_menu (application, g_value_get_object (value));
671 gtk_application_set_menubar (application, g_value_get_object (value));
675 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
681 gtk_application_finalize (GObject *object)
683 GtkApplication *application = GTK_APPLICATION (object);
685 g_clear_object (&application->priv->app_menu);
686 g_clear_object (&application->priv->menubar);
688 G_OBJECT_CLASS (gtk_application_parent_class)
693 gtk_application_class_init (GtkApplicationClass *class)
695 GObjectClass *object_class = G_OBJECT_CLASS (class);
696 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
698 object_class->get_property = gtk_application_get_property;
699 object_class->set_property = gtk_application_set_property;
700 object_class->finalize = gtk_application_finalize;
702 application_class->add_platform_data = gtk_application_add_platform_data;
703 application_class->before_emit = gtk_application_before_emit;
704 application_class->after_emit = gtk_application_after_emit;
705 application_class->startup = gtk_application_startup;
706 application_class->shutdown = gtk_application_shutdown;
708 class->window_added = gtk_application_window_added;
709 class->window_removed = gtk_application_window_removed;
711 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
714 * GtkApplication::window-added:
715 * @application: the #GtkApplication which emitted the signal
716 * @window: the newly-added #GtkWindow
718 * Emitted when a #GtkWindow is added to @application through
719 * gtk_application_add_window().
723 gtk_application_signals[WINDOW_ADDED] =
724 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
725 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
727 g_cclosure_marshal_VOID__OBJECT,
728 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
731 * GtkApplication::window-removed:
732 * @application: the #GtkApplication which emitted the signal
733 * @window: the #GtkWindow that is being removed
735 * Emitted when a #GtkWindow is removed from @application,
736 * either as a side-effect of being destroyed or explicitly
737 * through gtk_application_remove_window().
741 gtk_application_signals[WINDOW_REMOVED] =
742 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
743 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
745 g_cclosure_marshal_VOID__OBJECT,
746 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
749 * GtkApplication:register-session:
751 * Set this property to %TRUE to register with the session manager.
755 g_object_class_install_property (object_class, PROP_REGISTER_SESSION,
756 g_param_spec_boolean ("register-session",
757 P_("Register session"),
758 P_("Register with the session manager"),
759 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
761 g_object_class_install_property (object_class, PROP_APP_MENU,
762 g_param_spec_object ("app-menu",
763 P_("Application menu"),
764 P_("The GMenuModel for the application menu"),
766 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
768 g_object_class_install_property (object_class, PROP_MENUBAR,
769 g_param_spec_object ("menubar",
771 P_("The GMenuModel for the menubar"),
773 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
777 * gtk_application_new:
778 * @application_id: the application id
779 * @flags: the application flags
781 * Creates a new #GtkApplication instance.
783 * This function calls g_type_init() for you. gtk_init() is called
784 * as soon as the application gets registered as the primary instance.
786 * Concretely, gtk_init() is called in the default handler for the
787 * startup() signal. Therefore, #GtkApplication subclasses should
788 * chain up in their startup() handler before using any GTK+ API.
790 * Note that commandline arguments are not passed to gtk_init().
791 * All GTK+ functionality that is available via commandline arguments
792 * can also be achieved by setting suitable environment variables
793 * such as <envar>G_DEBUG</envar>, so this should not be a big
794 * problem. If you absolutely must support GTK+ commandline arguments,
795 * you can explicitly call gtk_init() before creating the application
798 * The application id must be valid. See g_application_id_is_valid().
800 * Returns: a new #GtkApplication instance
805 gtk_application_new (const gchar *application_id,
806 GApplicationFlags flags)
808 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
812 return g_object_new (GTK_TYPE_APPLICATION,
813 "application-id", application_id,
819 * gtk_application_add_window:
820 * @application: a #GtkApplication
821 * @window: a #GtkWindow
823 * Adds a window from @application.
825 * This call is equivalent to setting the #GtkWindow:application
826 * property of @window to @application.
828 * Normally, the connection between the application and the window
829 * will remain until the window is destroyed, but you can explicitly
830 * remove it with gtk_application_remove_window().
832 * GTK+ will keep the application running as long as it has
838 gtk_application_add_window (GtkApplication *application,
841 g_return_if_fail (GTK_IS_APPLICATION (application));
843 if (!g_list_find (application->priv->windows, window))
844 g_signal_emit (application,
845 gtk_application_signals[WINDOW_ADDED], 0, window);
849 * gtk_application_remove_window:
850 * @application: a #GtkApplication
851 * @window: a #GtkWindow
853 * Remove a window from @application.
855 * If @window belongs to @application then this call is equivalent to
856 * setting the #GtkWindow:application property of @window to
859 * The application may stop running as a result of a call to this
865 gtk_application_remove_window (GtkApplication *application,
868 g_return_if_fail (GTK_IS_APPLICATION (application));
870 if (g_list_find (application->priv->windows, window))
871 g_signal_emit (application,
872 gtk_application_signals[WINDOW_REMOVED], 0, window);
876 * gtk_application_get_windows:
877 * @application: a #GtkApplication
879 * Gets a list of the #GtkWindows associated with @application.
881 * The list is sorted by most recently focused window, such that the first
882 * element is the currently focused window. (Useful for choosing a parent
883 * for a transient window.)
885 * The list that is returned should not be modified in any way. It will
886 * only remain valid until the next focus change or window creation or
889 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
894 gtk_application_get_windows (GtkApplication *application)
896 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
898 return application->priv->windows;
902 * gtk_application_add_accelerator:
903 * @application: a #GtkApplication
904 * @accelerator: accelerator string
905 * @action_name: the name of the action to activate
906 * @parameter: (allow-none): parameter to pass when activating the action,
907 * or %NULL if the action does not accept an activation parameter
909 * Installs an accelerator that will cause the named action
910 * to be activated when the key combination specificed by @accelerator
913 * @accelerator must be a string that can be parsed by
914 * gtk_accelerator_parse(), e.g. "<Primary>q" or "<Control><Alt>p".
916 * @action_name must be the name of an action as it would be used
917 * in the app menu, i.e. actions that have been added to the application
918 * are referred to with an "app." prefix, and window-specific actions
919 * with a "win." prefix.
921 * GtkApplication also extracts accelerators out of 'accel' attributes
922 * in the #GMenuModels passed to gtk_application_set_app_menu() and
923 * gtk_application_set_menubar(), which is usually more convenient
924 * than calling this function for each accelerator.
929 gtk_application_add_accelerator (GtkApplication *application,
930 const gchar *accelerator,
931 const gchar *action_name,
936 GdkModifierType accel_mods;
938 g_return_if_fail (GTK_IS_APPLICATION (application));
940 /* Call this here, since gtk_init() is only getting called in startup() */
941 _gtk_accel_map_init ();
943 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
947 g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
951 accel_path = _gtk_accel_path_for_action (action_name, parameter);
953 if (gtk_accel_map_lookup_entry (accel_path, NULL))
954 gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
956 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
962 * gtk_application_remove_accelerator:
963 * @application: a #GtkApplication
964 * @action_name: the name of the action to activate
965 * @parameter: (allow-none): parameter to pass when activating the action,
966 * or %NULL if the action does not accept an activation parameter
968 * Removes an accelerator that has been previously added
969 * with gtk_application_add_accelerator().
974 gtk_application_remove_accelerator (GtkApplication *application,
975 const gchar *action_name,
980 g_return_if_fail (GTK_IS_APPLICATION (application));
982 accel_path = _gtk_accel_path_for_action (action_name, parameter);
984 if (!gtk_accel_map_lookup_entry (accel_path, NULL))
986 g_warning ("No accelerator found for '%s'\n", accel_path);
991 gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
996 * gtk_application_set_app_menu:
997 * @application: a #GtkApplication
998 * @app_menu: (allow-none): a #GMenuModel, or %NULL
1000 * Sets or unsets the application menu for @application.
1002 * The application menu is a single menu containing items that typically
1003 * impact the application as a whole, rather than acting on a specific
1004 * window or document. For example, you would expect to see
1005 * "Preferences" or "Quit" in an application menu, but not "Save" or
1008 * If supported, the application menu will be rendered by the desktop
1011 * Use the base #GActionMap interface to add actions, to respond to the user
1012 * selecting these menu items.
1017 gtk_application_set_app_menu (GtkApplication *application,
1018 GMenuModel *app_menu)
1020 g_return_if_fail (GTK_IS_APPLICATION (application));
1022 if (app_menu != application->priv->app_menu)
1024 if (application->priv->app_menu != NULL)
1025 g_object_unref (application->priv->app_menu);
1027 application->priv->app_menu = app_menu;
1029 if (application->priv->app_menu != NULL)
1030 g_object_ref (application->priv->app_menu);
1032 extract_accels_from_menu (app_menu, application);
1034 #ifdef GDK_WINDOWING_X11
1035 gtk_application_set_app_menu_x11 (application, app_menu);
1038 g_object_notify (G_OBJECT (application), "app-menu");
1043 * gtk_application_get_app_menu:
1044 * @application: a #GtkApplication
1046 * Returns the menu model that has been set with
1047 * gtk_application_set_app_menu().
1049 * Returns: (transfer none): the application menu of @application
1054 gtk_application_get_app_menu (GtkApplication *application)
1056 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1058 return application->priv->app_menu;
1062 * gtk_application_set_menubar:
1063 * @application: a #GtkApplication
1064 * @menubar: (allow-none): a #GMenuModel, or %NULL
1066 * Sets or unsets the menubar for windows of @application.
1068 * This is a menubar in the traditional sense.
1070 * Depending on the desktop environment, this may appear at the top of
1071 * each window, or at the top of the screen. In some environments, if
1072 * both the application menu and the menubar are set, the application
1073 * menu will be presented as if it were the first item of the menubar.
1074 * Other environments treat the two as completely separate -- for
1075 * example, the application menu may be rendered by the desktop shell
1076 * while the menubar (if set) remains in each individual window.
1078 * Use the base #GActionMap interface to add actions, to respond to the user
1079 * selecting these menu items.
1084 gtk_application_set_menubar (GtkApplication *application,
1085 GMenuModel *menubar)
1087 g_return_if_fail (GTK_IS_APPLICATION (application));
1089 if (menubar != application->priv->menubar)
1091 if (application->priv->menubar != NULL)
1092 g_object_unref (application->priv->menubar);
1094 application->priv->menubar = menubar;
1096 if (application->priv->menubar != NULL)
1097 g_object_ref (application->priv->menubar);
1099 extract_accels_from_menu (menubar, application);
1101 #ifdef GDK_WINDOWING_X11
1102 gtk_application_set_menubar_x11 (application, menubar);
1105 g_object_notify (G_OBJECT (application), "menubar");
1110 * gtk_application_get_menubar:
1111 * @application: a #GtkApplication
1113 * Returns the menu model that has been set with
1114 * gtk_application_set_menubar().
1116 * Returns: (transfer none): the menubar for windows of @application
1121 gtk_application_get_menubar (GtkApplication *application)
1123 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1125 return application->priv->menubar;
1128 #if defined(GDK_WINDOWING_X11)
1130 /* D-Bus Session Management
1132 * The protocol and the D-Bus API are described here:
1133 * http://live.gnome.org/SessionManagement/GnomeSession
1134 * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1138 unregister_client (GtkApplication *app)
1140 GError *error = NULL;
1142 g_debug ("Unregistering client");
1144 g_dbus_proxy_call_sync (app->priv->sm_proxy,
1146 g_variant_new ("(o)", app->priv->client_path),
1147 G_DBUS_CALL_FLAGS_NONE,
1154 g_warning ("Failed to unregister client: %s", error->message);
1155 g_error_free (error);
1158 g_clear_object (&app->priv->client_proxy);
1160 g_free (app->priv->client_path);
1161 app->priv->client_path = NULL;
1165 gtk_application_quit_response (GtkApplication *application,
1167 const gchar *reason)
1169 g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1171 g_dbus_proxy_call (application->priv->client_proxy,
1172 "EndSessionResponse",
1173 g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1174 G_DBUS_CALL_FLAGS_NONE,
1179 client_proxy_signal (GDBusProxy *proxy,
1180 const gchar *sender_name,
1181 const gchar *signal_name,
1182 GVariant *parameters,
1183 GtkApplication *app)
1185 if (strcmp (signal_name, "QueryEndSession") == 0)
1187 g_debug ("Received QueryEndSession");
1188 gtk_application_quit_response (app, TRUE, NULL);
1190 else if (strcmp (signal_name, "CancelEndSession") == 0)
1192 g_debug ("Received CancelEndSession");
1194 else if (strcmp (signal_name, "EndSession") == 0)
1196 g_debug ("Received EndSession");
1197 gtk_application_quit_response (app, TRUE, NULL);
1198 unregister_client (app);
1199 g_application_quit (G_APPLICATION (app));
1201 else if (strcmp (signal_name, "Stop") == 0)
1203 g_debug ("Received Stop");
1204 unregister_client (app);
1205 g_application_quit (G_APPLICATION (app));
1210 gtk_application_startup_session_dbus (GtkApplication *app)
1212 static gchar *client_id;
1213 GError *error = NULL;
1216 if (app->priv->session_bus == NULL)
1219 if (client_id == NULL)
1221 const gchar *desktop_autostart_id;
1223 desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1224 /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1225 * use the same client id.
1227 g_unsetenv ("DESKTOP_AUTOSTART_ID");
1228 client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1231 g_debug ("Connecting to session manager");
1233 app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1234 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1235 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1237 "org.gnome.SessionManager",
1238 "/org/gnome/SessionManager",
1239 "org.gnome.SessionManager",
1244 g_warning ("Failed to get a session proxy: %s", error->message);
1245 g_error_free (error);
1249 /* FIXME: should we reuse the D-Bus application id here ? */
1250 app->priv->app_id = g_strdup (g_get_prgname ());
1252 if (!app->priv->register_session)
1255 g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1257 res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1259 g_variant_new ("(ss)", app->priv->app_id, client_id),
1260 G_DBUS_CALL_FLAGS_NONE,
1267 g_warning ("Failed to register client: %s", error->message);
1268 g_error_free (error);
1269 g_clear_object (&app->priv->sm_proxy);
1273 g_variant_get (res, "(o)", &app->priv->client_path);
1274 g_variant_unref (res);
1276 g_debug ("Registered client at '%s'", app->priv->client_path);
1278 app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1280 "org.gnome.SessionManager",
1281 app->priv->client_path,
1282 "org.gnome.SessionManager.ClientPrivate",
1287 g_warning ("Failed to get client proxy: %s", error->message);
1288 g_error_free (error);
1289 g_clear_object (&app->priv->sm_proxy);
1290 g_free (app->priv->client_path);
1291 app->priv->client_path = NULL;
1295 g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1301 * GtkApplicationInhibitFlags:
1302 * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit ending the user session
1303 * by logging out or by shutting down the computer
1304 * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1305 * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1306 * session or computer
1307 * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1308 * marked as idle (and possibly locked)
1310 * Types of user actions that may be blocked by gtk_application_inhibit().
1316 * gtk_application_inhibit:
1317 * @application: the #GApplication
1318 * @window: (allow-none): a #GtkWindow, or %NULL
1319 * @flags: what types of actions should be inhibited
1320 * @reason: (allow-none): a short, human-readable string that explains
1321 * why these operations are inhibited
1323 * Inform the session manager that certain types of actions should be
1324 * inhibited. This is not guaranteed to work on all platforms and for
1325 * all types of actions.
1327 * Applications should invoke this method when they begin an operation
1328 * that should not be interrupted, such as creating a CD or DVD. The
1329 * types of actions that may be blocked are specified by the @flags
1330 * parameter. When the application completes the operation it should
1331 * call g_application_uninhibit() to remove the inhibitor. Note that
1332 * an application can have multiple inhibitors, and all of the must
1333 * be individually removed. Inhibitors are also cleared when the
1334 * application exits.
1336 * Applications should not expect that they will always be able to block
1337 * the action. In most cases, users will be given the option to force
1338 * the action to take place.
1340 * Reasons should be short and to the point.
1342 * If @window is given, the session manager may point the user to
1343 * this window to find out more about why the action is inhibited.
1345 * Returns: A non-zero cookie that is used to uniquely identify this
1346 * request. It should be used as an argument to g_application_uninhibit()
1347 * in order to remove the request. If the platform does not support
1348 * inhibiting or the request failed for some reason, 0 is returned.
1353 gtk_application_inhibit (GtkApplication *application,
1355 GtkApplicationInhibitFlags flags,
1356 const gchar *reason)
1359 GError *error = NULL;
1363 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1364 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1365 g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1368 xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1372 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1374 g_variant_new ("(susu)",
1375 application->priv->app_id,
1379 G_DBUS_CALL_FLAGS_NONE,
1385 g_warning ("Calling Inhibit failed: %s", error->message);
1386 g_error_free (error);
1390 g_variant_get (res, "(u)", &cookie);
1391 g_variant_unref (res);
1397 * gtk_application_uninhibit:
1398 * @application: the #GApplication
1399 * @cookie: a cookie that was returned by g_application_inhibit()
1401 * Removes an inhibitor that has been established with g_application_inhibit().
1402 * Inhibitors are also cleared when the application exits.
1407 gtk_application_uninhibit (GtkApplication *application,
1410 g_return_if_fail (GTK_IS_APPLICATION (application));
1411 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1412 g_return_if_fail (application->priv->sm_proxy != NULL);
1414 g_dbus_proxy_call (application->priv->sm_proxy,
1416 g_variant_new ("(u)", cookie),
1417 G_DBUS_CALL_FLAGS_NONE,
1423 * gtk_application_is_inhibited:
1424 * @application: the #GApplication
1425 * @flags: what types of actions should be queried
1427 * Determines if any of the actions specified in @flags are
1428 * currently inhibited (possibly by another application).
1430 * Returns: %TRUE if any of the actions specified in @flags are inhibited
1435 gtk_application_is_inhibited (GtkApplication *application,
1436 GtkApplicationInhibitFlags flags)
1439 GError *error = NULL;
1442 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1443 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1444 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1446 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1448 g_variant_new ("(u)", flags),
1449 G_DBUS_CALL_FLAGS_NONE,
1455 g_warning ("Calling IsInhibited failed: %s", error->message);
1456 g_error_free (error);
1460 g_variant_get (res, "(b)", &inhibited);
1461 g_variant_unref (res);
1466 #elif defined(GDK_WINDOWING_QUARTZ)
1468 /* OS X implementation copied from EggSMClient, but simplified since
1469 * it doesn't need to interact with the user.
1473 idle_will_quit (gpointer data)
1475 GtkApplication *app = data;
1477 if (app->priv->quit_inhibit == 0)
1478 g_application_quit (G_APPLICATION (app));
1481 GtkApplicationQuartzInhibitor *inhibitor;
1485 for (iter = app->priv->inhibitors; iter; iter = iter->next)
1487 inhibitor = iter->data;
1488 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1491 g_assert (inhibitor != NULL);
1493 dialog = gtk_message_dialog_new (inhibitor->window,
1497 _("%s cannot quit at this time:\n\n%s"),
1498 g_get_application_name (),
1500 g_signal_connect_swapped (dialog,
1502 G_CALLBACK (gtk_widget_destroy),
1504 gtk_widget_show_all (dialog);
1507 return G_SOURCE_REMOVE;
1511 quit_requested (const AppleEvent *aevt,
1515 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1517 /* Don't emit the "quit" signal immediately, since we're
1518 * called from a weird point in the guts of gdkeventloop-quartz.c
1520 g_idle_add_full (G_PRIORITY_DEFAULT, idle_will_quit, app, NULL);
1522 return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
1526 gtk_application_startup_session_quartz (GtkApplication *app)
1528 if (app->priv->register_session)
1529 AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1530 NewAEEventHandlerUPP (quit_requested),
1531 (long)GPOINTER_TO_SIZE (app), false);
1535 gtk_application_inhibit (GtkApplication *application,
1537 GtkApplicationInhibitFlags flags,
1538 const gchar *reason)
1540 GtkApplicationQuartzInhibitor *inhibitor;
1542 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1543 g_return_val_if_fail (flags != 0, 0);
1545 inhibitor = g_slice_new (GtkApplicationQuartzInhibitor);
1546 inhibitor->cookie = ++application->priv->next_cookie;
1547 inhibitor->flags = flags;
1548 inhibitor->reason = g_strdup (reason);
1549 inhibitor->window = window ? g_object_ref (window) : NULL;
1551 application->priv->inhibitors = g_slist_prepend (application->priv->inhibitors, inhibitor);
1553 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1554 application->priv->quit_inhibit++;
1556 return inhibitor->cookie;
1560 gtk_application_uninhibit (GtkApplication *application,
1565 for (iter = application->priv->inhibitors; iter; iter = iter->next)
1567 GtkApplicationQuartzInhibitor *inhibitor = iter->data;
1569 if (inhibitor->cookie == cookie)
1571 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1572 application->priv->quit_inhibit--;
1573 gtk_application_quartz_inhibitor_free (inhibitor);
1574 application->priv->inhibitors = g_slist_delete_link (application->priv->inhibitors, iter);
1579 g_warning ("Invalid inhibitor cookie");
1583 gtk_application_is_inhibited (GtkApplication *application,
1584 GtkApplicationInhibitFlags flags)
1586 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1587 return application->priv->quit_inhibit > 0;
1594 /* Trivial implementation.
1596 * For the inhibit API on Windows, see
1597 * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1601 gtk_application_inhibit (GtkApplication *application,
1603 GtkApplicationInhibitFlags flags,
1604 const gchar *reason)
1610 gtk_application_uninhibit (GtkApplication *application,
1616 gtk_application_is_inhibited (GtkApplication *application,
1617 GtkApplicationInhibitFlags flags)