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;
152 const gchar *object_path;
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 if (application->priv->session_bus == NULL)
191 /* unexport any existing menu */
194 g_dbus_connection_unexport_menu_model (application->priv->session_bus, *id);
200 /* export the new menu, if there is one */
203 /* try getting the preferred name */
204 *path = g_strconcat (application->priv->object_path, "/menus/", type, NULL);
205 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
207 /* keep trying until we get a working name... */
208 for (i = 0; *id == 0; i++)
211 *path = g_strdup_printf ("%s/menus/%s%d", application->priv->object_path, type, i);
212 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
218 gtk_application_set_app_menu_x11 (GtkApplication *application,
219 GMenuModel *app_menu)
221 gtk_application_x11_publish_menu (application, "appmenu", app_menu,
222 &application->priv->app_menu_id,
223 &application->priv->app_menu_path);
227 gtk_application_set_menubar_x11 (GtkApplication *application,
230 gtk_application_x11_publish_menu (application, "menubar", menubar,
231 &application->priv->menubar_id,
232 &application->priv->menubar_path);
236 gtk_application_window_added_x11 (GtkApplication *application,
239 if (application->priv->session_bus == NULL)
242 if (GTK_IS_APPLICATION_WINDOW (window))
244 GtkApplicationWindow *app_window = GTK_APPLICATION_WINDOW (window);
247 /* GtkApplicationWindow associates with us when it is first created,
248 * so surely it's not realized yet...
250 g_assert (!gtk_widget_get_realized (GTK_WIDGET (window)));
257 window_id = application->priv->next_id++;
258 window_path = g_strdup_printf ("%s/window/%d", application->priv->object_path, window_id);
259 success = gtk_application_window_publish (app_window, application->priv->session_bus, window_path);
260 g_free (window_path);
267 gtk_application_window_removed_x11 (GtkApplication *application,
270 if (application->priv->session_bus == NULL)
273 if (GTK_IS_APPLICATION_WINDOW (window))
274 gtk_application_window_unpublish (GTK_APPLICATION_WINDOW (window));
277 static void gtk_application_startup_session_dbus (GtkApplication *app);
280 gtk_application_startup_x11 (GtkApplication *application)
282 application->priv->session_bus = g_application_get_dbus_connection (G_APPLICATION (application));
283 application->priv->object_path = g_application_get_dbus_object_path (G_APPLICATION (application));
285 gtk_application_startup_session_dbus (GTK_APPLICATION (application));
289 gtk_application_shutdown_x11 (GtkApplication *application)
291 application->priv->session_bus = NULL;
292 application->priv->object_path = NULL;
294 g_clear_object (&application->priv->sm_proxy);
295 g_clear_object (&application->priv->client_proxy);
296 g_free (application->priv->app_id);
297 g_free (application->priv->client_path);
301 gtk_application_get_app_menu_object_path (GtkApplication *application)
303 return application->priv->app_menu_path;
307 gtk_application_get_menubar_object_path (GtkApplication *application)
309 return application->priv->menubar_path;
314 #ifdef GDK_WINDOWING_QUARTZ
318 GtkApplicationInhibitFlags flags;
321 } GtkApplicationQuartzInhibitor;
324 gtk_application_quartz_inhibitor_free (GtkApplicationQuartzInhibitor *inhibitor)
326 g_free (inhibitor->reason);
327 g_clear_object (&inhibitor->window);
328 g_slice_free (GtkApplicationQuartzInhibitor, inhibitor);
332 gtk_application_menu_changed_quartz (GObject *object,
336 GtkApplication *application = GTK_APPLICATION (object);
339 combined = g_menu_new ();
340 g_menu_append_submenu (combined, "Application", gtk_application_get_app_menu (application));
341 g_menu_append_section (combined, NULL, gtk_application_get_menubar (application));
343 gtk_quartz_set_main_menu (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (application->priv->muxer));
346 static void gtk_application_startup_session_quartz (GtkApplication *app);
349 gtk_application_startup_quartz (GtkApplication *application)
351 [NSApp finishLaunching];
353 application->priv->muxer = g_action_muxer_new ();
354 g_action_muxer_insert (application->priv->muxer, "app", G_ACTION_GROUP (application));
356 g_signal_connect (application, "notify::app-menu", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
357 g_signal_connect (application, "notify::menubar", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
358 gtk_application_menu_changed_quartz (G_OBJECT (application), NULL, NULL);
360 gtk_application_startup_session_quartz (application);
364 gtk_application_shutdown_quartz (GtkApplication *application)
366 g_signal_handlers_disconnect_by_func (application, gtk_application_menu_changed_quartz, NULL);
368 g_object_unref (application->priv->muxer);
369 application->priv->muxer = NULL;
371 g_slist_free_full (application->priv->inhibitors,
372 (GDestroyNotify) gtk_application_quartz_inhibitor_free);
373 application->priv->inhibitors = NULL;
377 gtk_application_focus_changed (GtkApplication *application,
380 if (G_IS_ACTION_GROUP (window))
381 g_action_muxer_insert (application->priv->muxer, "win", G_ACTION_GROUP (window));
383 g_action_muxer_remove (application->priv->muxer, "win");
388 gtk_application_focus_in_event_cb (GtkWindow *window,
389 GdkEventFocus *event,
390 GtkApplication *application)
392 GtkApplicationPrivate *priv = application->priv;
395 /* Keep the window list sorted by most-recently-focused. */
396 link = g_list_find (priv->windows, window);
397 if (link != NULL && link != priv->windows)
399 priv->windows = g_list_remove_link (priv->windows, link);
400 priv->windows = g_list_concat (link, priv->windows);
403 #ifdef GDK_WINDOWING_QUARTZ
404 gtk_application_focus_changed (application, window);
411 gtk_application_startup (GApplication *application)
413 G_APPLICATION_CLASS (gtk_application_parent_class)
414 ->startup (application);
418 #ifdef GDK_WINDOWING_X11
419 gtk_application_startup_x11 (GTK_APPLICATION (application));
422 #ifdef GDK_WINDOWING_QUARTZ
423 gtk_application_startup_quartz (GTK_APPLICATION (application));
428 gtk_application_shutdown (GApplication *application)
430 #ifdef GDK_WINDOWING_X11
431 gtk_application_shutdown_x11 (GTK_APPLICATION (application));
434 #ifdef GDK_WINDOWING_QUARTZ
435 gtk_application_shutdown_quartz (GTK_APPLICATION (application));
438 /* Try storing all clipboard data we have */
439 _gtk_clipboard_store_all ();
441 /* Synchronize the recent manager singleton */
442 _gtk_recent_manager_sync ();
444 G_APPLICATION_CLASS (gtk_application_parent_class)
445 ->shutdown (application);
449 gtk_application_add_platform_data (GApplication *application,
450 GVariantBuilder *builder)
452 const gchar *startup_id;
454 startup_id = getenv ("DESKTOP_STARTUP_ID");
456 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
457 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
458 g_variant_new_string (startup_id));
462 gtk_application_before_emit (GApplication *application,
463 GVariant *platform_data)
469 gdk_threads_enter ();
471 g_variant_iter_init (&iter, platform_data);
472 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
474 #ifdef GDK_WINDOWING_X11
475 if (strcmp (key, "desktop-startup-id") == 0)
480 display = gdk_display_get_default ();
481 id = g_variant_get_string (value, NULL);
482 if (GDK_IS_X11_DISPLAY (display))
483 gdk_x11_display_set_startup_notification_id (display, id);
490 gtk_application_after_emit (GApplication *application,
491 GVariant *platform_data)
493 gdk_notify_startup_complete ();
495 gdk_threads_leave ();
499 gtk_application_init (GtkApplication *application)
501 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
502 GTK_TYPE_APPLICATION,
503 GtkApplicationPrivate);
507 gtk_application_window_added (GtkApplication *application,
510 GtkApplicationPrivate *priv = application->priv;
512 priv->windows = g_list_prepend (priv->windows, window);
513 gtk_window_set_application (window, application);
514 g_application_hold (G_APPLICATION (application));
516 g_signal_connect (window, "focus-in-event",
517 G_CALLBACK (gtk_application_focus_in_event_cb),
520 #ifdef GDK_WINDOWING_X11
521 gtk_application_window_added_x11 (application, window);
526 gtk_application_window_removed (GtkApplication *application,
529 GtkApplicationPrivate *priv = application->priv;
531 #ifdef GDK_WINDOWING_X11
532 gtk_application_window_removed_x11 (application, window);
535 g_signal_handlers_disconnect_by_func (window,
536 gtk_application_focus_in_event_cb,
539 g_application_release (G_APPLICATION (application));
540 priv->windows = g_list_remove (priv->windows, window);
541 gtk_window_set_application (window, NULL);
545 extract_accel_from_menu_item (GMenuModel *model,
549 GMenuAttributeIter *iter;
552 const gchar *accel = NULL;
553 const gchar *action = NULL;
554 GVariant *target = NULL;
556 iter = g_menu_model_iterate_item_attributes (model, item);
557 while (g_menu_attribute_iter_get_next (iter, &key, &value))
559 if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
560 action = g_variant_get_string (value, NULL);
561 else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
562 accel = g_variant_get_string (value, NULL);
563 else if (g_str_equal (key, "target"))
564 target = g_variant_ref (value);
565 g_variant_unref (value);
567 g_object_unref (iter);
570 gtk_application_add_accelerator (app, accel, action, target);
573 g_variant_unref (target);
577 extract_accels_from_menu (GMenuModel *model,
585 for (i = 0; i < g_menu_model_get_n_items (model); i++)
587 extract_accel_from_menu_item (model, i, app);
589 iter = g_menu_model_iterate_item_links (model, i);
590 while (g_menu_link_iter_get_next (iter, &key, &m))
592 extract_accels_from_menu (m, app);
595 g_object_unref (iter);
600 gtk_application_get_property (GObject *object,
605 GtkApplication *application = GTK_APPLICATION (object);
609 case PROP_REGISTER_SESSION:
610 g_value_set_boolean (value, application->priv->register_session);
614 g_value_set_object (value, gtk_application_get_app_menu (application));
618 g_value_set_object (value, gtk_application_get_menubar (application));
622 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
628 gtk_application_set_property (GObject *object,
633 GtkApplication *application = GTK_APPLICATION (object);
637 case PROP_REGISTER_SESSION:
638 application->priv->register_session = g_value_get_boolean (value);
642 gtk_application_set_app_menu (application, g_value_get_object (value));
646 gtk_application_set_menubar (application, g_value_get_object (value));
650 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
656 gtk_application_finalize (GObject *object)
658 GtkApplication *application = GTK_APPLICATION (object);
660 g_clear_object (&application->priv->app_menu);
661 g_clear_object (&application->priv->menubar);
663 G_OBJECT_CLASS (gtk_application_parent_class)
668 gtk_application_class_init (GtkApplicationClass *class)
670 GObjectClass *object_class = G_OBJECT_CLASS (class);
671 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
673 object_class->get_property = gtk_application_get_property;
674 object_class->set_property = gtk_application_set_property;
675 object_class->finalize = gtk_application_finalize;
677 application_class->add_platform_data = gtk_application_add_platform_data;
678 application_class->before_emit = gtk_application_before_emit;
679 application_class->after_emit = gtk_application_after_emit;
680 application_class->startup = gtk_application_startup;
681 application_class->shutdown = gtk_application_shutdown;
683 class->window_added = gtk_application_window_added;
684 class->window_removed = gtk_application_window_removed;
686 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
689 * GtkApplication::window-added:
690 * @application: the #GtkApplication which emitted the signal
691 * @window: the newly-added #GtkWindow
693 * Emitted when a #GtkWindow is added to @application through
694 * gtk_application_add_window().
698 gtk_application_signals[WINDOW_ADDED] =
699 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
700 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
702 g_cclosure_marshal_VOID__OBJECT,
703 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
706 * GtkApplication::window-removed:
707 * @application: the #GtkApplication which emitted the signal
708 * @window: the #GtkWindow that is being removed
710 * Emitted when a #GtkWindow is removed from @application,
711 * either as a side-effect of being destroyed or explicitly
712 * through gtk_application_remove_window().
716 gtk_application_signals[WINDOW_REMOVED] =
717 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
718 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
720 g_cclosure_marshal_VOID__OBJECT,
721 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
724 * GtkApplication:register-session:
726 * Set this property to %TRUE to register with the session manager.
730 g_object_class_install_property (object_class, PROP_REGISTER_SESSION,
731 g_param_spec_boolean ("register-session",
732 P_("Register session"),
733 P_("Register with the session manager"),
734 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
736 g_object_class_install_property (object_class, PROP_APP_MENU,
737 g_param_spec_object ("app-menu",
738 P_("Application menu"),
739 P_("The GMenuModel for the application menu"),
741 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
743 g_object_class_install_property (object_class, PROP_MENUBAR,
744 g_param_spec_object ("menubar",
746 P_("The GMenuModel for the menubar"),
748 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
752 * gtk_application_new:
753 * @application_id: the application id
754 * @flags: the application flags
756 * Creates a new #GtkApplication instance.
758 * This function calls g_type_init() for you. gtk_init() is called
759 * as soon as the application gets registered as the primary instance.
761 * Concretely, gtk_init() is called in the default handler for the
762 * #GApplication:startup signal. Therefore, #GtkApplication subclasses should
763 * chain up in their #GApplication:startup handler before using any GTK+ API.
765 * Note that commandline arguments are not passed to gtk_init().
766 * All GTK+ functionality that is available via commandline arguments
767 * can also be achieved by setting suitable environment variables
768 * such as <envar>G_DEBUG</envar>, so this should not be a big
769 * problem. If you absolutely must support GTK+ commandline arguments,
770 * you can explicitly call gtk_init() before creating the application
773 * The application id must be valid. See g_application_id_is_valid().
775 * Returns: a new #GtkApplication instance
780 gtk_application_new (const gchar *application_id,
781 GApplicationFlags flags)
783 g_return_val_if_fail (g_application_id_is_valid (application_id), NULL);
787 return g_object_new (GTK_TYPE_APPLICATION,
788 "application-id", application_id,
794 * gtk_application_add_window:
795 * @application: a #GtkApplication
796 * @window: a #GtkWindow
798 * Adds a window to @application.
800 * This call is equivalent to setting the #GtkWindow:application
801 * property of @window to @application.
803 * Normally, the connection between the application and the window
804 * will remain until the window is destroyed, but you can explicitly
805 * remove it with gtk_application_remove_window().
807 * GTK+ will keep the application running as long as it has
813 gtk_application_add_window (GtkApplication *application,
816 g_return_if_fail (GTK_IS_APPLICATION (application));
818 if (!g_list_find (application->priv->windows, window))
819 g_signal_emit (application,
820 gtk_application_signals[WINDOW_ADDED], 0, window);
824 * gtk_application_remove_window:
825 * @application: a #GtkApplication
826 * @window: a #GtkWindow
828 * Remove a window from @application.
830 * If @window belongs to @application then this call is equivalent to
831 * setting the #GtkWindow:application property of @window to
834 * The application may stop running as a result of a call to this
840 gtk_application_remove_window (GtkApplication *application,
843 g_return_if_fail (GTK_IS_APPLICATION (application));
845 if (g_list_find (application->priv->windows, window))
846 g_signal_emit (application,
847 gtk_application_signals[WINDOW_REMOVED], 0, window);
851 * gtk_application_get_windows:
852 * @application: a #GtkApplication
854 * Gets a list of the #GtkWindows associated with @application.
856 * The list is sorted by most recently focused window, such that the first
857 * element is the currently focused window. (Useful for choosing a parent
858 * for a transient window.)
860 * The list that is returned should not be modified in any way. It will
861 * only remain valid until the next focus change or window creation or
864 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
869 gtk_application_get_windows (GtkApplication *application)
871 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
873 return application->priv->windows;
877 * gtk_application_add_accelerator:
878 * @application: a #GtkApplication
879 * @accelerator: accelerator string
880 * @action_name: the name of the action to activate
881 * @parameter: (allow-none): parameter to pass when activating the action,
882 * or %NULL if the action does not accept an activation parameter
884 * Installs an accelerator that will cause the named action
885 * to be activated when the key combination specificed by @accelerator
888 * @accelerator must be a string that can be parsed by
889 * gtk_accelerator_parse(), e.g. "<Primary>q" or "<Control><Alt>p".
891 * @action_name must be the name of an action as it would be used
892 * in the app menu, i.e. actions that have been added to the application
893 * are referred to with an "app." prefix, and window-specific actions
894 * with a "win." prefix.
896 * GtkApplication also extracts accelerators out of 'accel' attributes
897 * in the #GMenuModels passed to gtk_application_set_app_menu() and
898 * gtk_application_set_menubar(), which is usually more convenient
899 * than calling this function for each accelerator.
904 gtk_application_add_accelerator (GtkApplication *application,
905 const gchar *accelerator,
906 const gchar *action_name,
911 GdkModifierType accel_mods;
913 g_return_if_fail (GTK_IS_APPLICATION (application));
915 /* Call this here, since gtk_init() is only getting called in startup() */
916 _gtk_accel_map_init ();
918 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
922 g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
926 accel_path = _gtk_accel_path_for_action (action_name, parameter);
928 if (gtk_accel_map_lookup_entry (accel_path, NULL))
929 gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
931 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
937 * gtk_application_remove_accelerator:
938 * @application: a #GtkApplication
939 * @action_name: the name of the action to activate
940 * @parameter: (allow-none): parameter to pass when activating the action,
941 * or %NULL if the action does not accept an activation parameter
943 * Removes an accelerator that has been previously added
944 * with gtk_application_add_accelerator().
949 gtk_application_remove_accelerator (GtkApplication *application,
950 const gchar *action_name,
955 g_return_if_fail (GTK_IS_APPLICATION (application));
957 accel_path = _gtk_accel_path_for_action (action_name, parameter);
959 if (!gtk_accel_map_lookup_entry (accel_path, NULL))
961 g_warning ("No accelerator found for '%s'\n", accel_path);
966 gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
971 * gtk_application_set_app_menu:
972 * @application: a #GtkApplication
973 * @app_menu: (allow-none): a #GMenuModel, or %NULL
975 * Sets or unsets the application menu for @application.
977 * This can only be done in the primary instance of the application,
978 * after it has been registered. #GApplication:startup is a good place
981 * The application menu is a single menu containing items that typically
982 * impact the application as a whole, rather than acting on a specific
983 * window or document. For example, you would expect to see
984 * "Preferences" or "Quit" in an application menu, but not "Save" or
987 * If supported, the application menu will be rendered by the desktop
990 * Use the base #GActionMap interface to add actions, to respond to the user
991 * selecting these menu items.
996 gtk_application_set_app_menu (GtkApplication *application,
997 GMenuModel *app_menu)
999 g_return_if_fail (GTK_IS_APPLICATION (application));
1000 g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1001 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1003 if (app_menu != application->priv->app_menu)
1005 if (application->priv->app_menu != NULL)
1006 g_object_unref (application->priv->app_menu);
1008 application->priv->app_menu = app_menu;
1010 if (application->priv->app_menu != NULL)
1011 g_object_ref (application->priv->app_menu);
1013 extract_accels_from_menu (app_menu, application);
1015 #ifdef GDK_WINDOWING_X11
1016 gtk_application_set_app_menu_x11 (application, app_menu);
1019 g_object_notify (G_OBJECT (application), "app-menu");
1024 * gtk_application_get_app_menu:
1025 * @application: a #GtkApplication
1027 * Returns the menu model that has been set with
1028 * gtk_application_set_app_menu().
1030 * Returns: (transfer none): the application menu of @application
1035 gtk_application_get_app_menu (GtkApplication *application)
1037 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1039 return application->priv->app_menu;
1043 * gtk_application_set_menubar:
1044 * @application: a #GtkApplication
1045 * @menubar: (allow-none): a #GMenuModel, or %NULL
1047 * Sets or unsets the menubar for windows of @application.
1049 * This is a menubar in the traditional sense.
1051 * This can only be done in the primary instance of the application,
1052 * after it has been registered. #GApplication:startup is a good place
1055 * Depending on the desktop environment, this may appear at the top of
1056 * each window, or at the top of the screen. In some environments, if
1057 * both the application menu and the menubar are set, the application
1058 * menu will be presented as if it were the first item of the menubar.
1059 * Other environments treat the two as completely separate -- for
1060 * example, the application menu may be rendered by the desktop shell
1061 * while the menubar (if set) remains in each individual window.
1063 * Use the base #GActionMap interface to add actions, to respond to the user
1064 * selecting these menu items.
1069 gtk_application_set_menubar (GtkApplication *application,
1070 GMenuModel *menubar)
1072 g_return_if_fail (GTK_IS_APPLICATION (application));
1073 g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1074 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1076 if (menubar != application->priv->menubar)
1078 if (application->priv->menubar != NULL)
1079 g_object_unref (application->priv->menubar);
1081 application->priv->menubar = menubar;
1083 if (application->priv->menubar != NULL)
1084 g_object_ref (application->priv->menubar);
1086 extract_accels_from_menu (menubar, application);
1088 #ifdef GDK_WINDOWING_X11
1089 gtk_application_set_menubar_x11 (application, menubar);
1092 g_object_notify (G_OBJECT (application), "menubar");
1097 * gtk_application_get_menubar:
1098 * @application: a #GtkApplication
1100 * Returns the menu model that has been set with
1101 * gtk_application_set_menubar().
1103 * Returns: (transfer none): the menubar for windows of @application
1108 gtk_application_get_menubar (GtkApplication *application)
1110 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1112 return application->priv->menubar;
1115 #if defined(GDK_WINDOWING_X11)
1117 /* D-Bus Session Management
1119 * The protocol and the D-Bus API are described here:
1120 * http://live.gnome.org/SessionManagement/GnomeSession
1121 * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1125 unregister_client (GtkApplication *app)
1127 GError *error = NULL;
1129 g_debug ("Unregistering client");
1131 g_dbus_proxy_call_sync (app->priv->sm_proxy,
1133 g_variant_new ("(o)", app->priv->client_path),
1134 G_DBUS_CALL_FLAGS_NONE,
1141 g_warning ("Failed to unregister client: %s", error->message);
1142 g_error_free (error);
1145 g_clear_object (&app->priv->client_proxy);
1147 g_free (app->priv->client_path);
1148 app->priv->client_path = NULL;
1152 gtk_application_quit_response (GtkApplication *application,
1154 const gchar *reason)
1156 g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1158 g_dbus_proxy_call (application->priv->client_proxy,
1159 "EndSessionResponse",
1160 g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1161 G_DBUS_CALL_FLAGS_NONE,
1166 client_proxy_signal (GDBusProxy *proxy,
1167 const gchar *sender_name,
1168 const gchar *signal_name,
1169 GVariant *parameters,
1170 GtkApplication *app)
1172 if (strcmp (signal_name, "QueryEndSession") == 0)
1174 g_debug ("Received QueryEndSession");
1175 gtk_application_quit_response (app, TRUE, NULL);
1177 else if (strcmp (signal_name, "CancelEndSession") == 0)
1179 g_debug ("Received CancelEndSession");
1181 else if (strcmp (signal_name, "EndSession") == 0)
1183 g_debug ("Received EndSession");
1184 gtk_application_quit_response (app, TRUE, NULL);
1185 unregister_client (app);
1186 g_application_quit (G_APPLICATION (app));
1188 else if (strcmp (signal_name, "Stop") == 0)
1190 g_debug ("Received Stop");
1191 unregister_client (app);
1192 g_application_quit (G_APPLICATION (app));
1197 gtk_application_startup_session_dbus (GtkApplication *app)
1199 static gchar *client_id;
1200 GError *error = NULL;
1203 if (app->priv->session_bus == NULL)
1206 if (client_id == NULL)
1208 const gchar *desktop_autostart_id;
1210 desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1211 /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1212 * use the same client id.
1214 g_unsetenv ("DESKTOP_AUTOSTART_ID");
1215 client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1218 g_debug ("Connecting to session manager");
1220 app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1221 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1222 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1224 "org.gnome.SessionManager",
1225 "/org/gnome/SessionManager",
1226 "org.gnome.SessionManager",
1231 g_warning ("Failed to get a session proxy: %s", error->message);
1232 g_error_free (error);
1236 /* FIXME: should we reuse the D-Bus application id here ? */
1237 app->priv->app_id = g_strdup (g_get_prgname ());
1239 if (!app->priv->register_session)
1242 g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1244 res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1246 g_variant_new ("(ss)", app->priv->app_id, client_id),
1247 G_DBUS_CALL_FLAGS_NONE,
1254 g_warning ("Failed to register client: %s", error->message);
1255 g_error_free (error);
1256 g_clear_object (&app->priv->sm_proxy);
1260 g_variant_get (res, "(o)", &app->priv->client_path);
1261 g_variant_unref (res);
1263 g_debug ("Registered client at '%s'", app->priv->client_path);
1265 app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1267 "org.gnome.SessionManager",
1268 app->priv->client_path,
1269 "org.gnome.SessionManager.ClientPrivate",
1274 g_warning ("Failed to get client proxy: %s", error->message);
1275 g_error_free (error);
1276 g_clear_object (&app->priv->sm_proxy);
1277 g_free (app->priv->client_path);
1278 app->priv->client_path = NULL;
1282 g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1288 * GtkApplicationInhibitFlags:
1289 * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit ending the user session
1290 * by logging out or by shutting down the computer
1291 * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1292 * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1293 * session or computer
1294 * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1295 * marked as idle (and possibly locked)
1297 * Types of user actions that may be blocked by gtk_application_inhibit().
1303 * gtk_application_inhibit:
1304 * @application: the #GApplication
1305 * @window: (allow-none): a #GtkWindow, or %NULL
1306 * @flags: what types of actions should be inhibited
1307 * @reason: (allow-none): a short, human-readable string that explains
1308 * why these operations are inhibited
1310 * Inform the session manager that certain types of actions should be
1311 * inhibited. This is not guaranteed to work on all platforms and for
1312 * all types of actions.
1314 * Applications should invoke this method when they begin an operation
1315 * that should not be interrupted, such as creating a CD or DVD. The
1316 * types of actions that may be blocked are specified by the @flags
1317 * parameter. When the application completes the operation it should
1318 * call g_application_uninhibit() to remove the inhibitor. Note that
1319 * an application can have multiple inhibitors, and all of the must
1320 * be individually removed. Inhibitors are also cleared when the
1321 * application exits.
1323 * Applications should not expect that they will always be able to block
1324 * the action. In most cases, users will be given the option to force
1325 * the action to take place.
1327 * Reasons should be short and to the point.
1329 * If @window is given, the session manager may point the user to
1330 * this window to find out more about why the action is inhibited.
1332 * Returns: A non-zero cookie that is used to uniquely identify this
1333 * request. It should be used as an argument to g_application_uninhibit()
1334 * in order to remove the request. If the platform does not support
1335 * inhibiting or the request failed for some reason, 0 is returned.
1340 gtk_application_inhibit (GtkApplication *application,
1342 GtkApplicationInhibitFlags flags,
1343 const gchar *reason)
1346 GError *error = NULL;
1350 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1351 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1352 g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1355 xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1359 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1361 g_variant_new ("(susu)",
1362 application->priv->app_id,
1366 G_DBUS_CALL_FLAGS_NONE,
1372 g_warning ("Calling Inhibit failed: %s", error->message);
1373 g_error_free (error);
1377 g_variant_get (res, "(u)", &cookie);
1378 g_variant_unref (res);
1384 * gtk_application_uninhibit:
1385 * @application: the #GApplication
1386 * @cookie: a cookie that was returned by g_application_inhibit()
1388 * Removes an inhibitor that has been established with g_application_inhibit().
1389 * Inhibitors are also cleared when the application exits.
1394 gtk_application_uninhibit (GtkApplication *application,
1397 g_return_if_fail (GTK_IS_APPLICATION (application));
1398 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1399 g_return_if_fail (application->priv->sm_proxy != NULL);
1401 g_dbus_proxy_call (application->priv->sm_proxy,
1403 g_variant_new ("(u)", cookie),
1404 G_DBUS_CALL_FLAGS_NONE,
1410 * gtk_application_is_inhibited:
1411 * @application: the #GApplication
1412 * @flags: what types of actions should be queried
1414 * Determines if any of the actions specified in @flags are
1415 * currently inhibited (possibly by another application).
1417 * Returns: %TRUE if any of the actions specified in @flags are inhibited
1422 gtk_application_is_inhibited (GtkApplication *application,
1423 GtkApplicationInhibitFlags flags)
1426 GError *error = NULL;
1429 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1430 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1431 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1433 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1435 g_variant_new ("(u)", flags),
1436 G_DBUS_CALL_FLAGS_NONE,
1442 g_warning ("Calling IsInhibited failed: %s", error->message);
1443 g_error_free (error);
1447 g_variant_get (res, "(b)", &inhibited);
1448 g_variant_unref (res);
1453 #elif defined(GDK_WINDOWING_QUARTZ)
1455 /* OS X implementation copied from EggSMClient, but simplified since
1456 * it doesn't need to interact with the user.
1460 idle_will_quit (gpointer data)
1462 GtkApplication *app = data;
1464 if (app->priv->quit_inhibit == 0)
1465 g_application_quit (G_APPLICATION (app));
1468 GtkApplicationQuartzInhibitor *inhibitor;
1472 for (iter = app->priv->inhibitors; iter; iter = iter->next)
1474 inhibitor = iter->data;
1475 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1478 g_assert (inhibitor != NULL);
1480 dialog = gtk_message_dialog_new (inhibitor->window,
1484 _("%s cannot quit at this time:\n\n%s"),
1485 g_get_application_name (),
1487 g_signal_connect_swapped (dialog,
1489 G_CALLBACK (gtk_widget_destroy),
1491 gtk_widget_show_all (dialog);
1494 return G_SOURCE_REMOVE;
1498 quit_requested (const AppleEvent *aevt,
1502 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1504 /* Don't emit the "quit" signal immediately, since we're
1505 * called from a weird point in the guts of gdkeventloop-quartz.c
1507 g_idle_add_full (G_PRIORITY_DEFAULT, idle_will_quit, app, NULL);
1509 return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
1513 gtk_application_startup_session_quartz (GtkApplication *app)
1515 if (app->priv->register_session)
1516 AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1517 NewAEEventHandlerUPP (quit_requested),
1518 (long)GPOINTER_TO_SIZE (app), false);
1522 gtk_application_inhibit (GtkApplication *application,
1524 GtkApplicationInhibitFlags flags,
1525 const gchar *reason)
1527 GtkApplicationQuartzInhibitor *inhibitor;
1529 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1530 g_return_val_if_fail (flags != 0, 0);
1532 inhibitor = g_slice_new (GtkApplicationQuartzInhibitor);
1533 inhibitor->cookie = ++application->priv->next_cookie;
1534 inhibitor->flags = flags;
1535 inhibitor->reason = g_strdup (reason);
1536 inhibitor->window = window ? g_object_ref (window) : NULL;
1538 application->priv->inhibitors = g_slist_prepend (application->priv->inhibitors, inhibitor);
1540 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1541 application->priv->quit_inhibit++;
1543 return inhibitor->cookie;
1547 gtk_application_uninhibit (GtkApplication *application,
1552 for (iter = application->priv->inhibitors; iter; iter = iter->next)
1554 GtkApplicationQuartzInhibitor *inhibitor = iter->data;
1556 if (inhibitor->cookie == cookie)
1558 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1559 application->priv->quit_inhibit--;
1560 gtk_application_quartz_inhibitor_free (inhibitor);
1561 application->priv->inhibitors = g_slist_delete_link (application->priv->inhibitors, iter);
1566 g_warning ("Invalid inhibitor cookie");
1570 gtk_application_is_inhibited (GtkApplication *application,
1571 GtkApplicationInhibitFlags flags)
1573 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1574 return application->priv->quit_inhibit > 0;
1581 /* Trivial implementation.
1583 * For the inhibit API on Windows, see
1584 * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1588 gtk_application_inhibit (GtkApplication *application,
1590 GtkApplicationInhibitFlags flags,
1591 const gchar *reason)
1597 gtk_application_uninhibit (GtkApplication *application,
1603 gtk_application_is_inhibited (GtkApplication *application,
1604 GtkApplicationInhibitFlags flags)