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 "gtkclipboard.h"
34 #include "gtkmarshalers.h"
36 #include "gtkrecentmanager.h"
37 #include "gtkaccelmapprivate.h"
38 #include "gactionmuxer.h"
41 #ifdef GDK_WINDOWING_QUARTZ
42 #include "gtkquartz-menu.h"
43 #import <Cocoa/Cocoa.h>
44 #include <Carbon/Carbon.h>
45 #include "gtkmessagedialog.h"
49 #ifdef GDK_WINDOWING_X11
50 #include <gdk/x11/gdkx.h>
54 * SECTION:gtkapplication
55 * @title: GtkApplication
56 * @short_description: Application class
58 * #GtkApplication is a class that handles many important aspects
59 * of a GTK+ application in a convenient fashion, without enforcing
60 * a one-size-fits-all application model.
62 * Currently, GtkApplication handles GTK+ initialization, application
63 * uniqueness, session management, provides some basic scriptability and
64 * desktop shell integration by exporting actions and menus and manages a
65 * list of toplevel windows whose life-cycle is automatically tied to the
66 * life-cycle of your application.
68 * While GtkApplication works fine with plain #GtkWindows, it is recommended
69 * to use it together with #GtkApplicationWindow.
71 * When GDK threads are enabled, GtkApplication will acquire the GDK
72 * lock when invoking actions that arrive from other processes. The GDK
73 * lock is not touched for local action invocations. In order to have
74 * actions invoked in a predictable context it is therefore recommended
75 * that the GDK lock be held while invoking actions locally with
76 * g_action_group_activate_action(). The same applies to actions
77 * associated with #GtkApplicationWindow and to the 'activate' and
78 * 'open' #GApplication methods.
80 * To set an application menu on a GtkApplication, use
81 * gtk_application_set_app_menu(). The #GMenuModel that this function
82 * expects is usually constructed using #GtkBuilder, as seen in the
83 * following example. To set a menubar that will be automatically picked
84 * up by #GApplicationWindows, use gtk_application_set_menubar(). GTK+
85 * makes these menus appear as expected, depending on the platform
86 * the application is running on.
88 * <figure label="Menu integration in OS X">
89 * <graphic fileref="bloatpad-osx.png" format="PNG"/>
92 * <figure label="Menu integration in GNOME">
93 * <graphic fileref="bloatpad-gnome.png" format="PNG"/>
96 * <figure label="Menu integration in Xfce">
97 * <graphic fileref="bloatpad-xfce.png" format="PNG"/>
100 * <example id="gtkapplication"><title>A simple application</title>
102 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/bloatpad.c">
103 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
108 * GtkApplication optionally registers with a session manager
109 * of the users session (if you set the #GtkApplication:register-session
110 * property) and offers various functionality related to the session
113 * An application can be informed when the session is about to end
114 * by connecting to the #GtkApplication::quit signal.
116 * An application can request the session to be ended by calling
117 * gtk_application_end_session().
119 * An application can block various ways to end the session with
120 * the gtk_application_inhibit() function. Typical use cases for
121 * this kind of inhibiting are long-running, uninterruptible operations,
122 * such as burning a CD or performing a disk backup. The session
123 * manager may not honor the inhibitor, but it can be expected to
124 * inform the user about the negative consequences of ending the
125 * session while inhibitors are present.
135 static guint gtk_application_signals[LAST_SIGNAL];
139 PROP_REGISTER_SESSION,
144 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
146 struct _GtkApplicationPrivate
150 gboolean register_session;
152 GMenuModel *app_menu;
155 #ifdef GDK_WINDOWING_X11
156 GDBusConnection *session_bus;
157 const gchar *application_id;
160 gchar *app_menu_path;
168 GDBusProxy *sm_proxy;
169 GDBusProxy *client_proxy;
174 #ifdef GDK_WINDOWING_QUARTZ
184 #ifdef GDK_WINDOWING_X11
186 gtk_application_x11_publish_menu (GtkApplication *application,
194 /* unexport any existing menu */
197 g_dbus_connection_unexport_menu_model (application->priv->session_bus, *id);
203 /* export the new menu, if there is one */
206 /* try getting the preferred name */
207 *path = g_strconcat (application->priv->object_path, "/menus/", type, NULL);
208 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
210 /* keep trying until we get a working name... */
211 for (i = 0; *id == 0; i++)
214 *path = g_strdup_printf ("%s/menus/%s%d", application->priv->object_path, type, i);
215 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
221 gtk_application_set_app_menu_x11 (GtkApplication *application,
222 GMenuModel *app_menu)
224 gtk_application_x11_publish_menu (application, "appmenu", app_menu,
225 &application->priv->app_menu_id,
226 &application->priv->app_menu_path);
230 gtk_application_set_menubar_x11 (GtkApplication *application,
233 gtk_application_x11_publish_menu (application, "menubar", menubar,
234 &application->priv->menubar_id,
235 &application->priv->menubar_path);
239 gtk_application_window_added_x11 (GtkApplication *application,
242 if (application->priv->session_bus == NULL)
245 if (GTK_IS_APPLICATION_WINDOW (window))
247 GtkApplicationWindow *app_window = GTK_APPLICATION_WINDOW (window);
250 /* GtkApplicationWindow associates with us when it is first created,
251 * so surely it's not realized yet...
253 g_assert (!gtk_widget_get_realized (GTK_WIDGET (window)));
260 window_id = application->priv->next_id++;
261 window_path = g_strdup_printf ("%s/window/%d", application->priv->object_path, window_id);
262 success = gtk_application_window_publish (app_window, application->priv->session_bus, window_path);
263 g_free (window_path);
270 gtk_application_window_removed_x11 (GtkApplication *application,
273 if (application->priv->session_bus == NULL)
276 if (GTK_IS_APPLICATION_WINDOW (window))
277 gtk_application_window_unpublish (GTK_APPLICATION_WINDOW (window));
281 object_path_from_appid (const gchar *appid)
283 gchar *appid_path, *iter;
285 appid_path = g_strconcat ("/", appid, NULL);
286 for (iter = appid_path; *iter; iter++)
298 static void gtk_application_startup_session_dbus (GtkApplication *app);
301 gtk_application_startup_x11 (GtkApplication *application)
303 const gchar *application_id;
305 application_id = g_application_get_application_id (G_APPLICATION (application));
306 application->priv->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
307 application->priv->object_path = object_path_from_appid (application_id);
309 gtk_application_startup_session_dbus (GTK_APPLICATION (application));
313 gtk_application_shutdown_x11 (GtkApplication *application)
315 g_free (application->priv->object_path);
316 application->priv->object_path = NULL;
317 g_clear_object (&application->priv->session_bus);
319 g_clear_object (&application->priv->sm_proxy);
320 g_clear_object (&application->priv->client_proxy);
321 g_free (application->priv->app_id);
322 g_free (application->priv->client_path);
326 gtk_application_get_dbus_object_path (GtkApplication *application)
328 return application->priv->object_path;
332 gtk_application_get_app_menu_object_path (GtkApplication *application)
334 return application->priv->app_menu_path;
338 gtk_application_get_menubar_object_path (GtkApplication *application)
340 return application->priv->menubar_path;
345 #ifdef GDK_WINDOWING_QUARTZ
349 GtkApplicationInhibitFlags flags;
352 } GtkApplicationQuartzInhibitor;
355 gtk_application_quartz_inhibitor_free (GtkApplicationQuartzInhibitor *inhibitor)
357 g_free (inhibitor->reason);
358 g_clear_object (&inhibitor->window);
359 g_slice_free (GtkApplicationQuartzInhibitor, inhibitor);
363 gtk_application_menu_changed_quartz (GObject *object,
367 GtkApplication *application = GTK_APPLICATION (object);
370 combined = g_menu_new ();
371 g_menu_append_submenu (combined, "Application", gtk_application_get_app_menu (application));
372 g_menu_append_section (combined, NULL, gtk_application_get_menubar (application));
374 gtk_quartz_set_main_menu (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (application->priv->muxer));
377 static void gtk_application_startup_session_quartz (GtkApplication *app);
380 gtk_application_startup_quartz (GtkApplication *application)
382 [NSApp finishLaunching];
384 application->priv->muxer = g_action_muxer_new ();
385 g_action_muxer_insert (application->priv->muxer, "app", G_ACTION_GROUP (application));
387 g_signal_connect (application, "notify::app-menu", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
388 g_signal_connect (application, "notify::menubar", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
389 gtk_application_menu_changed_quartz (G_OBJECT (application), NULL, NULL);
391 gtk_application_startup_session_quartz (application);
395 gtk_application_shutdown_quartz (GtkApplication *application)
397 g_signal_handlers_disconnect_by_func (application, gtk_application_menu_changed_quartz, NULL);
399 g_object_unref (application->priv->muxer);
400 application->priv->muxer = NULL;
402 g_slist_free_full (application->priv->inhibitors,
403 (GDestroyNotify) gtk_application_quartz_inhibitor_free);
404 application->priv->inhibitors = NULL;
408 gtk_application_focus_changed (GtkApplication *application,
411 if (G_IS_ACTION_GROUP (window))
412 g_action_muxer_insert (application->priv->muxer, "win", G_ACTION_GROUP (window));
414 g_action_muxer_remove (application->priv->muxer, "win");
419 gtk_application_focus_in_event_cb (GtkWindow *window,
420 GdkEventFocus *event,
421 GtkApplication *application)
423 GtkApplicationPrivate *priv = application->priv;
426 /* Keep the window list sorted by most-recently-focused. */
427 link = g_list_find (priv->windows, window);
428 if (link != NULL && link != priv->windows)
430 priv->windows = g_list_remove_link (priv->windows, link);
431 priv->windows = g_list_concat (link, priv->windows);
434 #ifdef GDK_WINDOWING_QUARTZ
435 gtk_application_focus_changed (application, window);
442 gtk_application_startup (GApplication *application)
444 G_APPLICATION_CLASS (gtk_application_parent_class)
445 ->startup (application);
449 #ifdef GDK_WINDOWING_X11
450 gtk_application_startup_x11 (GTK_APPLICATION (application));
453 #ifdef GDK_WINDOWING_QUARTZ
454 gtk_application_startup_quartz (GTK_APPLICATION (application));
459 gtk_application_shutdown (GApplication *application)
461 #ifdef GDK_WINDOWING_X11
462 gtk_application_shutdown_x11 (GTK_APPLICATION (application));
465 #ifdef GDK_WINDOWING_QUARTZ
466 gtk_application_shutdown_quartz (GTK_APPLICATION (application));
469 /* Try storing all clipboard data we have */
470 _gtk_clipboard_store_all ();
472 /* Synchronize the recent manager singleton */
473 _gtk_recent_manager_sync ();
475 G_APPLICATION_CLASS (gtk_application_parent_class)
476 ->shutdown (application);
480 gtk_application_add_platform_data (GApplication *application,
481 GVariantBuilder *builder)
483 const gchar *startup_id;
485 startup_id = getenv ("DESKTOP_STARTUP_ID");
487 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
488 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
489 g_variant_new_string (startup_id));
493 gtk_application_before_emit (GApplication *application,
494 GVariant *platform_data)
500 gdk_threads_enter ();
502 g_variant_iter_init (&iter, platform_data);
503 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
505 #ifdef GDK_WINDOWING_X11
506 if (strcmp (key, "desktop-startup-id") == 0)
511 display = gdk_display_get_default ();
512 id = g_variant_get_string (value, NULL);
513 if (GDK_IS_X11_DISPLAY (display))
514 gdk_x11_display_set_startup_notification_id (display, id);
521 gtk_application_after_emit (GApplication *application,
522 GVariant *platform_data)
524 gdk_notify_startup_complete ();
526 gdk_threads_leave ();
530 gtk_application_init (GtkApplication *application)
532 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
533 GTK_TYPE_APPLICATION,
534 GtkApplicationPrivate);
538 gtk_application_window_added (GtkApplication *application,
541 GtkApplicationPrivate *priv = application->priv;
543 priv->windows = g_list_prepend (priv->windows, window);
544 gtk_window_set_application (window, application);
545 g_application_hold (G_APPLICATION (application));
547 g_signal_connect (window, "focus-in-event",
548 G_CALLBACK (gtk_application_focus_in_event_cb),
551 #ifdef GDK_WINDOWING_X11
552 gtk_application_window_added_x11 (application, window);
557 gtk_application_window_removed (GtkApplication *application,
560 GtkApplicationPrivate *priv = application->priv;
562 #ifdef GDK_WINDOWING_X11
563 gtk_application_window_removed_x11 (application, window);
566 g_signal_handlers_disconnect_by_func (window,
567 gtk_application_focus_in_event_cb,
570 g_application_release (G_APPLICATION (application));
571 priv->windows = g_list_remove (priv->windows, window);
572 gtk_window_set_application (window, NULL);
576 extract_accel_from_menu_item (GMenuModel *model,
580 GMenuAttributeIter *iter;
583 const gchar *accel = NULL;
584 const gchar *action = NULL;
585 GVariant *target = NULL;
587 iter = g_menu_model_iterate_item_attributes (model, item);
588 while (g_menu_attribute_iter_get_next (iter, &key, &value))
590 if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
591 action = g_variant_get_string (value, NULL);
592 else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
593 accel = g_variant_get_string (value, NULL);
594 else if (g_str_equal (key, "target"))
595 target = g_variant_ref (value);
596 g_variant_unref (value);
598 g_object_unref (iter);
601 gtk_application_add_accelerator (app, accel, action, target);
604 g_variant_unref (target);
608 extract_accels_from_menu (GMenuModel *model,
616 for (i = 0; i < g_menu_model_get_n_items (model); i++)
618 extract_accel_from_menu_item (model, i, app);
620 iter = g_menu_model_iterate_item_links (model, i);
621 while (g_menu_link_iter_get_next (iter, &key, &m))
623 extract_accels_from_menu (m, app);
626 g_object_unref (iter);
631 gtk_application_get_property (GObject *object,
636 GtkApplication *application = GTK_APPLICATION (object);
640 case PROP_REGISTER_SESSION:
641 g_value_set_boolean (value, application->priv->register_session);
645 g_value_set_object (value, gtk_application_get_app_menu (application));
649 g_value_set_object (value, gtk_application_get_menubar (application));
653 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
659 gtk_application_set_property (GObject *object,
664 GtkApplication *application = GTK_APPLICATION (object);
668 case PROP_REGISTER_SESSION:
669 application->priv->register_session = g_value_get_boolean (value);
673 gtk_application_set_app_menu (application, g_value_get_object (value));
677 gtk_application_set_menubar (application, g_value_get_object (value));
681 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
687 gtk_application_quit (GtkApplication *app)
689 /* we are asked to quit, so don't linger */
690 g_application_set_inactivity_timeout (G_APPLICATION (app), 0);
694 gtk_application_finalize (GObject *object)
696 GtkApplication *application = GTK_APPLICATION (object);
698 g_clear_object (&application->priv->app_menu);
699 g_clear_object (&application->priv->menubar);
701 G_OBJECT_CLASS (gtk_application_parent_class)
706 gtk_application_class_init (GtkApplicationClass *class)
708 GObjectClass *object_class = G_OBJECT_CLASS (class);
709 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
711 object_class->get_property = gtk_application_get_property;
712 object_class->set_property = gtk_application_set_property;
713 object_class->finalize = gtk_application_finalize;
715 application_class->add_platform_data = gtk_application_add_platform_data;
716 application_class->before_emit = gtk_application_before_emit;
717 application_class->after_emit = gtk_application_after_emit;
718 application_class->startup = gtk_application_startup;
719 application_class->shutdown = gtk_application_shutdown;
721 class->window_added = gtk_application_window_added;
722 class->window_removed = gtk_application_window_removed;
723 class->quit = gtk_application_quit;
725 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
728 * GtkApplication::window-added:
729 * @application: the #GtkApplication which emitted the signal
730 * @window: the newly-added #GtkWindow
732 * Emitted when a #GtkWindow is added to @application through
733 * gtk_application_add_window().
737 gtk_application_signals[WINDOW_ADDED] =
738 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
739 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
741 g_cclosure_marshal_VOID__OBJECT,
742 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
745 * GtkApplication::window-removed:
746 * @application: the #GtkApplication which emitted the signal
747 * @window: the #GtkWindow that is being removed
749 * Emitted when a #GtkWindow is removed from @application,
750 * either as a side-effect of being destroyed or explicitly
751 * through gtk_application_remove_window().
755 gtk_application_signals[WINDOW_REMOVED] =
756 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
757 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
759 g_cclosure_marshal_VOID__OBJECT,
760 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
763 * GtkApplication::quit:
764 * @application: the #GtkApplication
766 * Emitted when the session manager wants the application to quit
767 * (generally because the user is logging out). The application
768 * should exit as soon as possible after receiving this signal; if
769 * it does not, the session manager may choose to forcibly kill it.
771 * Normally, an application would only be sent a ::quit if there
772 * are no inhibitors (see gtk_application_inhibit()).
773 * However, this is not guaranteed; in some situations the
774 * session manager may decide to end the session without giving
775 * applications a chance to object.
777 * To receive this signal, you need to set the
778 * #GtkApplication:register-session property
779 * when creating the application object.
783 gtk_application_signals[QUIT] =
784 g_signal_new ("quit", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
785 G_STRUCT_OFFSET (GtkApplicationClass, quit),
786 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
789 * GtkApplication:register-session:
791 * Set this property to %TRUE to register with the session manager
792 * and receive the #GtkApplication::quit signal when the session
797 g_object_class_install_property (object_class, PROP_REGISTER_SESSION,
798 g_param_spec_boolean ("register-session",
799 P_("Register session"),
800 P_("Register with the session manager"),
801 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
803 g_object_class_install_property (object_class, PROP_APP_MENU,
804 g_param_spec_object ("app-menu",
805 P_("Application menu"),
806 P_("The GMenuModel for the application menu"),
808 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
810 g_object_class_install_property (object_class, PROP_MENUBAR,
811 g_param_spec_object ("menubar",
813 P_("The GMenuModel for the menubar"),
815 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
819 * gtk_application_new:
820 * @application_id: the application id
821 * @flags: the application flags
823 * Creates a new #GtkApplication instance.
825 * This function calls g_type_init() for you. gtk_init() is called
826 * as soon as the application gets registered as the primary instance.
828 * Concretely, gtk_init() is called in the default handler for the
829 * startup() signal. Therefore, #GtkApplication subclasses should
830 * chain up in their startup() handler before using any GTK+ API.
832 * Note that commandline arguments are not passed to gtk_init().
833 * All GTK+ functionality that is available via commandline arguments
834 * can also be achieved by setting suitable environment variables
835 * such as <envar>G_DEBUG</envar>, so this should not be a big
836 * problem. If you absolutely must support GTK+ commandline arguments,
837 * you can explicitly call gtk_init() before creating the application
840 * The application id must be valid. See g_application_id_is_valid().
842 * Returns: a new #GtkApplication instance
847 gtk_application_new (const gchar *application_id,
848 GApplicationFlags flags)
850 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
854 return g_object_new (GTK_TYPE_APPLICATION,
855 "application-id", application_id,
861 * gtk_application_add_window:
862 * @application: a #GtkApplication
863 * @window: a #GtkWindow
865 * Adds a window from @application.
867 * This call is equivalent to setting the #GtkWindow:application
868 * property of @window to @application.
870 * Normally, the connection between the application and the window
871 * will remain until the window is destroyed, but you can explicitly
872 * remove it with gtk_application_remove_window().
874 * GTK+ will keep the application running as long as it has
880 gtk_application_add_window (GtkApplication *application,
883 g_return_if_fail (GTK_IS_APPLICATION (application));
885 if (!g_list_find (application->priv->windows, window))
886 g_signal_emit (application,
887 gtk_application_signals[WINDOW_ADDED], 0, window);
891 * gtk_application_remove_window:
892 * @application: a #GtkApplication
893 * @window: a #GtkWindow
895 * Remove a window from @application.
897 * If @window belongs to @application then this call is equivalent to
898 * setting the #GtkWindow:application property of @window to
901 * The application may stop running as a result of a call to this
907 gtk_application_remove_window (GtkApplication *application,
910 g_return_if_fail (GTK_IS_APPLICATION (application));
912 if (g_list_find (application->priv->windows, window))
913 g_signal_emit (application,
914 gtk_application_signals[WINDOW_REMOVED], 0, window);
918 * gtk_application_get_windows:
919 * @application: a #GtkApplication
921 * Gets a list of the #GtkWindows associated with @application.
923 * The list is sorted by most recently focused window, such that the first
924 * element is the currently focused window. (Useful for choosing a parent
925 * for a transient window.)
927 * The list that is returned should not be modified in any way. It will
928 * only remain valid until the next focus change or window creation or
931 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
936 gtk_application_get_windows (GtkApplication *application)
938 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
940 return application->priv->windows;
944 * gtk_application_add_accelerator:
945 * @application: a #GtkApplication
946 * @accelerator: accelerator string
947 * @action_name: the name of the action to activate
948 * @parameter: (allow-none): parameter to pass when activating the action,
949 * or %NULL if the action does not accept an activation parameter
951 * Installs an accelerator that will cause the named action
952 * to be activated when the key combination specificed by @accelerator
955 * @accelerator must be a string that can be parsed by
956 * gtk_accelerator_parse(), e.g. "<Primary>q" or "<Control><Alt>p".
958 * @action_name must be the name of an action as it would be used
959 * in the app menu, i.e. actions that have been added to the application
960 * are referred to with an "app." prefix, and window-specific actions
961 * with a "win." prefix.
963 * GtkApplication also extracts accelerators out of 'accel' attributes
964 * in the #GMenuModels passed to gtk_application_set_app_menu() and
965 * gtk_application_set_menubar(), which is usually more convenient
966 * than calling this function for each accelerator.
971 gtk_application_add_accelerator (GtkApplication *application,
972 const gchar *accelerator,
973 const gchar *action_name,
978 GdkModifierType accel_mods;
980 g_return_if_fail (GTK_IS_APPLICATION (application));
982 /* Call this here, since gtk_init() is only getting called in startup() */
983 _gtk_accel_map_init ();
985 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
989 g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
993 accel_path = _gtk_accel_path_for_action (action_name, parameter);
995 if (gtk_accel_map_lookup_entry (accel_path, NULL))
996 gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
998 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
1000 g_free (accel_path);
1004 * gtk_application_remove_accelerator:
1005 * @application: a #GtkApplication
1006 * @action_name: the name of the action to activate
1007 * @parameter: (allow-none): parameter to pass when activating the action,
1008 * or %NULL if the action does not accept an activation parameter
1010 * Removes an accelerator that has been previously added
1011 * with gtk_application_add_accelerator().
1016 gtk_application_remove_accelerator (GtkApplication *application,
1017 const gchar *action_name,
1018 GVariant *parameter)
1022 g_return_if_fail (GTK_IS_APPLICATION (application));
1024 accel_path = _gtk_accel_path_for_action (action_name, parameter);
1026 if (!gtk_accel_map_lookup_entry (accel_path, NULL))
1028 g_warning ("No accelerator found for '%s'\n", accel_path);
1029 g_free (accel_path);
1033 gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
1034 g_free (accel_path);
1038 * gtk_application_set_app_menu:
1039 * @application: a #GtkApplication
1040 * @app_menu: (allow-none): a #GMenuModel, or %NULL
1042 * Sets or unsets the application menu for @application.
1044 * The application menu is a single menu containing items that typically
1045 * impact the application as a whole, rather than acting on a specific
1046 * window or document. For example, you would expect to see
1047 * "Preferences" or "Quit" in an application menu, but not "Save" or
1050 * If supported, the application menu will be rendered by the desktop
1056 gtk_application_set_app_menu (GtkApplication *application,
1057 GMenuModel *app_menu)
1059 g_return_if_fail (GTK_IS_APPLICATION (application));
1061 if (app_menu != application->priv->app_menu)
1063 if (application->priv->app_menu != NULL)
1064 g_object_unref (application->priv->app_menu);
1066 application->priv->app_menu = app_menu;
1068 if (application->priv->app_menu != NULL)
1069 g_object_ref (application->priv->app_menu);
1071 extract_accels_from_menu (app_menu, application);
1073 #ifdef GDK_WINDOWING_X11
1074 gtk_application_set_app_menu_x11 (application, app_menu);
1077 g_object_notify (G_OBJECT (application), "app-menu");
1082 * gtk_application_get_app_menu:
1083 * @application: a #GtkApplication
1085 * Returns the menu model that has been set with
1086 * gtk_application_set_app_menu().
1088 * Returns: (transfer none): the application menu of @application
1093 gtk_application_get_app_menu (GtkApplication *application)
1095 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1097 return application->priv->app_menu;
1101 * gtk_application_set_menubar:
1102 * @application: a #GtkApplication
1103 * @menubar: (allow-none): a #GMenuModel, or %NULL
1105 * Sets or unsets the menubar for windows of @application.
1107 * This is a menubar in the traditional sense.
1109 * Depending on the desktop environment, this may appear at the top of
1110 * each window, or at the top of the screen. In some environments, if
1111 * both the application menu and the menubar are set, the application
1112 * menu will be presented as if it were the first item of the menubar.
1113 * Other environments treat the two as completely separate -- for
1114 * example, the application menu may be rendered by the desktop shell
1115 * while the menubar (if set) remains in each individual window.
1120 gtk_application_set_menubar (GtkApplication *application,
1121 GMenuModel *menubar)
1123 g_return_if_fail (GTK_IS_APPLICATION (application));
1125 if (menubar != application->priv->menubar)
1127 if (application->priv->menubar != NULL)
1128 g_object_unref (application->priv->menubar);
1130 application->priv->menubar = menubar;
1132 if (application->priv->menubar != NULL)
1133 g_object_ref (application->priv->menubar);
1135 extract_accels_from_menu (menubar, application);
1137 #ifdef GDK_WINDOWING_X11
1138 gtk_application_set_menubar_x11 (application, menubar);
1141 g_object_notify (G_OBJECT (application), "menubar");
1146 * gtk_application_get_menubar:
1147 * @application: a #GtkApplication
1149 * Returns the menu model that has been set with
1150 * gtk_application_set_menubar().
1152 * Returns: (transfer none): the menubar for windows of @application
1157 gtk_application_get_menubar (GtkApplication *application)
1159 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1161 return application->priv->menubar;
1164 #if defined(GDK_WINDOWING_X11)
1166 /* D-Bus Session Management
1168 * The protocol and the D-Bus API are described here:
1169 * http://live.gnome.org/SessionManagement/GnomeSession
1170 * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1174 unregister_client (GtkApplication *app)
1176 GError *error = NULL;
1178 g_debug ("Unregistering client");
1180 g_dbus_proxy_call_sync (app->priv->sm_proxy,
1182 g_variant_new ("(o)", app->priv->client_path),
1183 G_DBUS_CALL_FLAGS_NONE,
1190 g_warning ("Failed to unregister client: %s", error->message);
1191 g_error_free (error);
1194 g_clear_object (&app->priv->client_proxy);
1196 g_free (app->priv->client_path);
1197 app->priv->client_path = NULL;
1201 gtk_application_quit_response (GtkApplication *application,
1203 const gchar *reason)
1205 g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1207 g_dbus_proxy_call (application->priv->client_proxy,
1208 "EndSessionResponse",
1209 g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1210 G_DBUS_CALL_FLAGS_NONE,
1215 client_proxy_signal (GDBusProxy *proxy,
1216 const gchar *sender_name,
1217 const gchar *signal_name,
1218 GVariant *parameters,
1219 GtkApplication *app)
1221 if (strcmp (signal_name, "QueryEndSession") == 0)
1223 g_debug ("Received QueryEndSession");
1224 gtk_application_quit_response (app, TRUE, NULL);
1226 else if (strcmp (signal_name, "CancelEndSession") == 0)
1228 g_debug ("Received CancelEndSession");
1230 else if (strcmp (signal_name, "EndSession") == 0)
1232 g_debug ("Received EndSession");
1233 gtk_application_quit_response (app, TRUE, NULL);
1234 unregister_client (app);
1235 g_signal_emit (app, gtk_application_signals[QUIT], 0);
1237 else if (strcmp (signal_name, "Stop") == 0)
1239 g_debug ("Received Stop");
1240 unregister_client (app);
1241 g_signal_emit (app, gtk_application_signals[QUIT], 0);
1246 gtk_application_startup_session_dbus (GtkApplication *app)
1248 static gchar *client_id;
1249 GError *error = NULL;
1252 if (app->priv->session_bus == NULL)
1255 if (client_id == NULL)
1257 const gchar *desktop_autostart_id;
1259 desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1260 /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1261 * use the same client id.
1263 g_unsetenv ("DESKTOP_AUTOSTART_ID");
1264 client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1267 g_debug ("Connecting to session manager");
1269 app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1270 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1271 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1273 "org.gnome.SessionManager",
1274 "/org/gnome/SessionManager",
1275 "org.gnome.SessionManager",
1280 g_warning ("Failed to get a session proxy: %s", error->message);
1281 g_error_free (error);
1285 /* FIXME: should we reuse the D-Bus application id here ? */
1286 app->priv->app_id = g_strdup (g_get_prgname ());
1288 if (!app->priv->register_session)
1291 g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1293 res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1295 g_variant_new ("(ss)", app->priv->app_id, client_id),
1296 G_DBUS_CALL_FLAGS_NONE,
1303 g_warning ("Failed to register client: %s", error->message);
1304 g_error_free (error);
1305 g_clear_object (&app->priv->sm_proxy);
1309 g_variant_get (res, "(o)", &app->priv->client_path);
1310 g_variant_unref (res);
1312 g_debug ("Registered client at '%s'", app->priv->client_path);
1314 app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1316 "org.gnome.SessionManager",
1317 app->priv->client_path,
1318 "org.gnome.SessionManager.ClientPrivate",
1323 g_warning ("Failed to get client proxy: %s", error->message);
1324 g_error_free (error);
1325 g_clear_object (&app->priv->sm_proxy);
1326 g_free (app->priv->client_path);
1327 app->priv->client_path = NULL;
1331 g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1337 * GtkApplicationInhibitFlags:
1338 * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit ending the user session
1339 * by logging out or by shutting down the computer
1340 * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1341 * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1342 * session or computer
1343 * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1344 * marked as idle (and possibly locked)
1346 * Types of user actions that may be blocked by gtk_application_inhibit().
1352 * gtk_application_inhibit:
1353 * @application: the #GApplication
1354 * @window: (allow-none): a #GtkWindow, or %NULL
1355 * @flags: what types of actions should be inhibited
1356 * @reason: (allow-none): a short, human-readable string that explains
1357 * why these operations are inhibited
1359 * Inform the session manager that certain types of actions should be
1360 * inhibited. This is not guaranteed to work on all platforms and for
1361 * all types of actions.
1363 * Applications should invoke this method when they begin an operation
1364 * that should not be interrupted, such as creating a CD or DVD. The
1365 * types of actions that may be blocked are specified by the @flags
1366 * parameter. When the application completes the operation it should
1367 * call g_application_uninhibit() to remove the inhibitor. Note that
1368 * an application can have multiple inhibitors, and all of the must
1369 * be individually removed. Inhibitors are also cleared when the
1370 * application exits.
1372 * Applications should not expect that they will always be able to block
1373 * the action. In most cases, users will be given the option to force
1374 * the action to take place.
1376 * Reasons should be short and to the point.
1378 * If @window is given, the session manager may point the user to
1379 * this window to find out more about why the action is inhibited.
1381 * Returns: A non-zero cookie that is used to uniquely identify this
1382 * request. It should be used as an argument to g_application_uninhibit()
1383 * in order to remove the request. If the platform does not support
1384 * inhibiting or the request failed for some reason, 0 is returned.
1389 gtk_application_inhibit (GtkApplication *application,
1391 GtkApplicationInhibitFlags flags,
1392 const gchar *reason)
1395 GError *error = NULL;
1399 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1400 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1401 g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1404 xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1408 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1410 g_variant_new ("(susu)",
1411 application->priv->app_id,
1415 G_DBUS_CALL_FLAGS_NONE,
1421 g_warning ("Calling Inhibit failed: %s", error->message);
1422 g_error_free (error);
1426 g_variant_get (res, "(u)", &cookie);
1427 g_variant_unref (res);
1433 * gtk_application_uninhibit:
1434 * @application: the #GApplication
1435 * @cookie: a cookie that was returned by g_application_inhibit()
1437 * Removes an inhibitor that has been established with g_application_inhibit().
1438 * Inhibitors are also cleared when the application exits.
1443 gtk_application_uninhibit (GtkApplication *application,
1446 g_return_if_fail (GTK_IS_APPLICATION (application));
1447 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1448 g_return_if_fail (application->priv->sm_proxy != NULL);
1450 g_dbus_proxy_call (application->priv->sm_proxy,
1452 g_variant_new ("(u)", cookie),
1453 G_DBUS_CALL_FLAGS_NONE,
1459 * gtk_application_is_inhibited:
1460 * @application: the #GApplication
1461 * @flags: what types of actions should be queried
1463 * Determines if any of the actions specified in @flags are
1464 * currently inhibited (possibly by another application).
1466 * Returns: %TRUE if any of the actions specified in @flags are inhibited
1471 gtk_application_is_inhibited (GtkApplication *application,
1472 GtkApplicationInhibitFlags flags)
1475 GError *error = NULL;
1478 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1479 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1480 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1482 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1484 g_variant_new ("(u)", flags),
1485 G_DBUS_CALL_FLAGS_NONE,
1491 g_warning ("Calling IsInhibited failed: %s", error->message);
1492 g_error_free (error);
1496 g_variant_get (res, "(b)", &inhibited);
1497 g_variant_unref (res);
1503 * GtkApplicationEndSessionStyle:
1504 * @GTK_APPLICATION_LOGOUT: End the session by logging out
1505 * @GTK_APPLICATION_REBOOT: Restart the computer
1506 * @GTK_APPLICATION_SHUTDOWN: Shut the computer down
1508 * Different ways to end a user session, for use with
1509 * gtk_application_end_session().
1513 * gtk_application_end_session:
1514 * @application: the #GtkApplication
1515 * @style: the desired kind of session end
1516 * @request_confirmation: whether or not the user should get a chance
1517 * to confirm the action
1519 * Requests that the session manager end the current session.
1520 * @style indicates how the session should be ended, and
1521 * @request_confirmation indicates whether or not the user should be
1522 * given a chance to confirm the action. Both of these parameters are
1523 * merely hints though; the session manager may choose to ignore them.
1525 * Return value: %TRUE if the request was sent; %FALSE if it could not
1526 * be sent (eg, because it could not connect to the session manager)
1531 gtk_application_end_session (GtkApplication *application,
1532 GtkApplicationEndSessionStyle style,
1533 gboolean request_confirmation)
1535 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1536 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1537 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1541 case GTK_APPLICATION_LOGOUT:
1542 g_dbus_proxy_call (application->priv->sm_proxy,
1544 g_variant_new ("(u)", request_confirmation ? 0 : 1),
1545 G_DBUS_CALL_FLAGS_NONE,
1549 case GTK_APPLICATION_REBOOT:
1550 case GTK_APPLICATION_SHUTDOWN:
1551 g_dbus_proxy_call (application->priv->sm_proxy,
1554 G_DBUS_CALL_FLAGS_NONE,
1563 #elif defined(GDK_WINDOWING_QUARTZ)
1565 /* OS X implementation copied from EggSMClient, but simplified since
1566 * it doesn't need to interact with the user.
1570 idle_will_quit (gpointer data)
1572 GtkApplication *app = data;
1574 if (app->priv->quit_inhibit == 0)
1575 g_signal_emit (app, gtk_application_signals[QUIT], 0);
1578 GtkApplicationQuartzInhibitor *inhibitor;
1582 for (iter = app->priv->inhibitors; iter; iter = iter->next)
1584 inhibitor = iter->data;
1585 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1588 g_assert (inhibitor != NULL);
1590 dialog = gtk_message_dialog_new (inhibitor->window,
1594 _("%s cannot quit at this time:\n\n%s"),
1595 g_get_application_name (),
1597 gtk_dialog_run (GTK_DIALOG (dialog));
1598 gtk_widget_destroy (dialog);
1605 quit_requested (const AppleEvent *aevt,
1609 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1611 /* Don't emit the "quit" signal immediately, since we're
1612 * called from a weird point in the guts of gdkeventloop-quartz.c
1614 g_idle_add_full (G_PRIORITY_DEFAULT, idle_will_quit, app, NULL);
1616 return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
1620 gtk_application_startup_session_quartz (GtkApplication *app)
1622 if (app->priv->register_session)
1623 AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1624 NewAEEventHandlerUPP (quit_requested),
1625 (long)GPOINTER_TO_SIZE (app), false);
1629 gtk_application_inhibit (GtkApplication *application,
1631 GtkApplicationInhibitFlags flags,
1632 const gchar *reason)
1634 GtkApplicationQuartzInhibitor *inhibitor;
1636 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1637 g_return_val_if_fail (flags != 0, 0);
1639 inhibitor = g_slice_new (GtkApplicationQuartzInhibitor);
1640 inhibitor->cookie = ++application->priv->next_cookie;
1641 inhibitor->flags = flags;
1642 inhibitor->reason = g_strdup (reason);
1643 inhibitor->window = window ? g_object_ref (window) : NULL;
1645 application->priv->inhibitors = g_slist_prepend (application->priv->inhibitors, inhibitor);
1647 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1648 application->priv->quit_inhibit++;
1650 return inhibitor->cookie;
1654 gtk_application_uninhibit (GtkApplication *application,
1659 for (iter = application->priv->inhibitors; iter; iter = iter->next)
1661 GtkApplicationQuartzInhibitor *inhibitor = iter->data;
1663 if (inhibitor->cookie == cookie)
1665 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1666 application->priv->quit_inhibit--;
1667 gtk_application_quartz_inhibitor_free (inhibitor);
1668 application->priv->inhibitors = g_slist_delete_link (application->priv->inhibitors, iter);
1673 g_warning ("Invalid inhibitor cookie");
1677 gtk_application_is_inhibited (GtkApplication *application,
1678 GtkApplicationInhibitFlags flags)
1680 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1681 return application->priv->quit_inhibit > 0;
1687 gtk_application_end_session (GtkApplication *application,
1688 GtkApplicationEndSessionStyle style,
1689 gboolean request_confirmation)
1691 static const ProcessSerialNumber loginwindow_psn = { 0, kSystemProcess };
1692 AppleEvent event = { typeNull, NULL };
1693 AppleEvent reply = { typeNull, NULL };
1694 AEAddressDesc target;
1700 case GTK_APPLICATION_LOGOUT:
1701 id = request_confirmation ? kAELogOut : kAEReallyLogOut;
1703 case GTK_APPLICATION_REBOOT:
1704 id = request_confirmation ? kAEShowRestartDialog : kAERestart;
1706 case GTK_APPLICATION_SHUTDOWN:
1707 id = request_confirmation ? kAEShowShutdownDialog : kAEShutDown;
1711 err = AECreateDesc (typeProcessSerialNumber, &loginwindow_psn,
1712 sizeof (loginwindow_psn), &target);
1715 g_warning ("Could not create descriptor for loginwindow: %d", err);
1719 err = AECreateAppleEvent (kCoreEventClass, id, &target,
1720 kAutoGenerateReturnID, kAnyTransactionID,
1722 AEDisposeDesc (&target);
1725 g_warning ("Could not create logout AppleEvent: %d", err);
1729 err = AESend (&event, &reply, kAENoReply, kAENormalPriority,
1730 kAEDefaultTimeout, NULL, NULL);
1731 AEDisposeDesc (&event);
1733 AEDisposeDesc (&reply);
1735 return err == noErr;
1740 /* Trivial implementation.
1742 * For the inhibit API on Windows, see
1743 * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1747 gtk_application_inhibit (GtkApplication *application,
1749 GtkApplicationInhibitFlags flags,
1750 const gchar *reason)
1756 gtk_application_uninhibit (GtkApplication *application,
1762 gtk_application_is_inhibited (GtkApplication *application,
1763 GtkApplicationInhibitFlags flags)
1769 gtk_application_end_session (GtkApplication *application,
1770 GtkApplicationEndSessionStyle style,
1771 gboolean request_confirmation)