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"
38 #ifdef GDK_WINDOWING_QUARTZ
39 #include "gtkmodelmenu-quartz.h"
40 #import <Cocoa/Cocoa.h>
41 #include <Carbon/Carbon.h>
42 #include "gtkmessagedialog.h"
46 #ifdef GDK_WINDOWING_X11
47 #include <gdk/x11/gdkx.h>
50 extern void _gtk_accessibility_shutdown (void);
53 * SECTION:gtkapplication
54 * @title: GtkApplication
55 * @short_description: Application class
57 * #GtkApplication is a class that handles many important aspects
58 * of a GTK+ application in a convenient fashion, without enforcing
59 * a one-size-fits-all application model.
61 * Currently, GtkApplication handles GTK+ initialization, application
62 * uniqueness, session management, provides some basic scriptability and
63 * desktop shell integration by exporting actions and menus and manages a
64 * list of toplevel windows whose life-cycle is automatically tied to the
65 * life-cycle of your application.
67 * While GtkApplication works fine with plain #GtkWindows, it is recommended
68 * to use it together with #GtkApplicationWindow.
70 * When GDK threads are enabled, GtkApplication will acquire the GDK
71 * lock when invoking actions that arrive from other processes. The GDK
72 * lock is not touched for local action invocations. In order to have
73 * actions invoked in a predictable context it is therefore recommended
74 * that the GDK lock be held while invoking actions locally with
75 * g_action_group_activate_action(). The same applies to actions
76 * associated with #GtkApplicationWindow and to the 'activate' and
77 * 'open' #GApplication methods.
79 * To set an application menu for a GtkApplication, use
80 * gtk_application_set_app_menu(). The #GMenuModel that this function
81 * expects is usually constructed using #GtkBuilder, as seen in the
82 * following example. To specify a menubar that will be shown by
83 * #GtkApplicationWindows, use gtk_application_set_menubar(). Use the base
84 * #GActionMap interface to add actions, to respond to the user
85 * selecting these menu items.
87 * GTK+ displays these menus as expected, depending on the platform
88 * the application is running on.
90 * <figure label="Menu integration in OS X">
91 * <graphic fileref="bloatpad-osx.png" format="PNG"/>
94 * <figure label="Menu integration in GNOME">
95 * <graphic fileref="bloatpad-gnome.png" format="PNG"/>
98 * <figure label="Menu integration in Xfce">
99 * <graphic fileref="bloatpad-xfce.png" format="PNG"/>
102 * <example id="gtkapplication"><title>A simple application</title>
104 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/bloatpad.c">
105 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
110 * GtkApplication optionally registers with a session manager
111 * of the users session (if you set the #GtkApplication:register-session
112 * property) and offers various functionality related to the session
115 * An application can block various ways to end the session with
116 * the gtk_application_inhibit() function. Typical use cases for
117 * this kind of inhibiting are long-running, uninterruptible operations,
118 * such as burning a CD or performing a disk backup. The session
119 * manager may not honor the inhibitor, but it can be expected to
120 * inform the user about the negative consequences of ending the
121 * session while inhibitors are present.
130 static guint gtk_application_signals[LAST_SIGNAL];
134 PROP_REGISTER_SESSION,
140 G_DEFINE_TYPE (GtkApplication, gtk_application, G_TYPE_APPLICATION)
142 struct _GtkApplicationPrivate
146 gboolean register_session;
148 GMenuModel *app_menu;
151 #ifdef GDK_WINDOWING_X11
152 GDBusConnection *session_bus;
153 const gchar *application_id;
154 const gchar *object_path;
156 gchar *app_menu_path;
164 GDBusProxy *sm_proxy;
165 GDBusProxy *client_proxy;
170 #ifdef GDK_WINDOWING_QUARTZ
179 #ifdef GDK_WINDOWING_X11
181 gtk_application_x11_publish_menu (GtkApplication *application,
189 if (application->priv->session_bus == NULL)
192 /* unexport any existing menu */
195 g_dbus_connection_unexport_menu_model (application->priv->session_bus, *id);
201 /* export the new menu, if there is one */
204 /* try getting the preferred name */
205 *path = g_strconcat (application->priv->object_path, "/menus/", type, NULL);
206 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
208 /* keep trying until we get a working name... */
209 for (i = 0; *id == 0; i++)
212 *path = g_strdup_printf ("%s/menus/%s%d", application->priv->object_path, type, i);
213 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
219 gtk_application_set_app_menu_x11 (GtkApplication *application,
220 GMenuModel *app_menu)
222 gtk_application_x11_publish_menu (application, "appmenu", app_menu,
223 &application->priv->app_menu_id,
224 &application->priv->app_menu_path);
228 gtk_application_set_menubar_x11 (GtkApplication *application,
231 gtk_application_x11_publish_menu (application, "menubar", menubar,
232 &application->priv->menubar_id,
233 &application->priv->menubar_path);
237 gtk_application_window_added_x11 (GtkApplication *application,
240 if (application->priv->session_bus == NULL)
243 if (GTK_IS_APPLICATION_WINDOW (window))
245 GtkApplicationWindow *app_window = GTK_APPLICATION_WINDOW (window);
248 /* GtkApplicationWindow associates with us when it is first created,
249 * so surely it's not realized yet...
251 g_assert (!gtk_widget_get_realized (GTK_WIDGET (window)));
258 window_id = application->priv->next_id++;
259 window_path = g_strdup_printf ("%s/window/%u", application->priv->object_path, window_id);
260 success = gtk_application_window_publish (app_window, application->priv->session_bus, window_path, window_id);
261 g_free (window_path);
268 gtk_application_window_removed_x11 (GtkApplication *application,
271 if (application->priv->session_bus == NULL)
274 if (GTK_IS_APPLICATION_WINDOW (window))
275 gtk_application_window_unpublish (GTK_APPLICATION_WINDOW (window));
278 static void gtk_application_startup_session_dbus (GtkApplication *app);
281 gtk_application_startup_x11 (GtkApplication *application)
283 application->priv->session_bus = g_application_get_dbus_connection (G_APPLICATION (application));
284 application->priv->object_path = g_application_get_dbus_object_path (G_APPLICATION (application));
286 gtk_application_startup_session_dbus (GTK_APPLICATION (application));
290 gtk_application_shutdown_x11 (GtkApplication *application)
292 application->priv->session_bus = NULL;
293 application->priv->object_path = NULL;
295 gtk_application_set_app_menu_x11 (application, NULL);
296 gtk_application_set_menubar_x11 (application, NULL);
298 g_clear_object (&application->priv->sm_proxy);
299 g_clear_object (&application->priv->client_proxy);
300 g_free (application->priv->app_id);
301 g_free (application->priv->client_path);
305 gtk_application_get_app_menu_object_path (GtkApplication *application)
307 return application->priv->app_menu_path;
311 gtk_application_get_menubar_object_path (GtkApplication *application)
313 return application->priv->menubar_path;
318 #ifdef GDK_WINDOWING_QUARTZ
322 GtkApplicationInhibitFlags flags;
325 } GtkApplicationQuartzInhibitor;
328 gtk_application_quartz_inhibitor_free (GtkApplicationQuartzInhibitor *inhibitor)
330 g_free (inhibitor->reason);
331 g_clear_object (&inhibitor->window);
332 g_slice_free (GtkApplicationQuartzInhibitor, inhibitor);
336 gtk_application_menu_changed_quartz (GObject *object,
340 GtkApplication *application = GTK_APPLICATION (object);
343 combined = g_menu_new ();
344 g_menu_append_submenu (combined, "Application", gtk_application_get_app_menu (application));
345 g_menu_append_section (combined, NULL, gtk_application_get_menubar (application));
347 gtk_quartz_set_main_menu (G_MENU_MODEL (combined), application);
349 g_object_unref (combined);
352 static void gtk_application_startup_session_quartz (GtkApplication *app);
355 gtk_application_startup_quartz (GtkApplication *application)
357 [NSApp finishLaunching];
359 g_signal_connect (application, "notify::app-menu", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
360 g_signal_connect (application, "notify::menubar", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
361 gtk_application_menu_changed_quartz (G_OBJECT (application), NULL, NULL);
363 gtk_application_startup_session_quartz (application);
367 gtk_application_shutdown_quartz (GtkApplication *application)
369 gtk_quartz_clear_main_menu ();
371 g_signal_handlers_disconnect_by_func (application, gtk_application_menu_changed_quartz, NULL);
373 g_slist_free_full (application->priv->inhibitors,
374 (GDestroyNotify) gtk_application_quartz_inhibitor_free);
375 application->priv->inhibitors = NULL;
380 gtk_application_focus_in_event_cb (GtkWindow *window,
381 GdkEventFocus *event,
382 GtkApplication *application)
384 GtkApplicationPrivate *priv = application->priv;
387 /* Keep the window list sorted by most-recently-focused. */
388 link = g_list_find (priv->windows, window);
389 if (link != NULL && link != priv->windows)
391 priv->windows = g_list_remove_link (priv->windows, link);
392 priv->windows = g_list_concat (link, priv->windows);
395 g_object_notify (G_OBJECT (application), "active-window");
401 gtk_application_startup (GApplication *application)
403 G_APPLICATION_CLASS (gtk_application_parent_class)
404 ->startup (application);
408 #ifdef GDK_WINDOWING_X11
409 gtk_application_startup_x11 (GTK_APPLICATION (application));
412 #ifdef GDK_WINDOWING_QUARTZ
413 gtk_application_startup_quartz (GTK_APPLICATION (application));
418 gtk_application_shutdown (GApplication *application)
420 #ifdef GDK_WINDOWING_X11
421 gtk_application_shutdown_x11 (GTK_APPLICATION (application));
424 #ifdef GDK_WINDOWING_QUARTZ
425 gtk_application_shutdown_quartz (GTK_APPLICATION (application));
428 /* Keep this section in sync with gtk_main() */
430 /* Try storing all clipboard data we have */
431 _gtk_clipboard_store_all ();
433 /* Synchronize the recent manager singleton */
434 _gtk_recent_manager_sync ();
436 _gtk_accessibility_shutdown ();
438 G_APPLICATION_CLASS (gtk_application_parent_class)
439 ->shutdown (application);
443 gtk_application_add_platform_data (GApplication *application,
444 GVariantBuilder *builder)
446 const gchar *startup_id;
448 startup_id = getenv ("DESKTOP_STARTUP_ID");
450 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
451 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
452 g_variant_new_string (startup_id));
456 gtk_application_before_emit (GApplication *application,
457 GVariant *platform_data)
463 gdk_threads_enter ();
465 g_variant_iter_init (&iter, platform_data);
466 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
468 #ifdef GDK_WINDOWING_X11
469 if (strcmp (key, "desktop-startup-id") == 0)
474 display = gdk_display_get_default ();
475 id = g_variant_get_string (value, NULL);
476 if (GDK_IS_X11_DISPLAY (display))
477 gdk_x11_display_set_startup_notification_id (display, id);
484 gtk_application_after_emit (GApplication *application,
485 GVariant *platform_data)
487 gdk_notify_startup_complete ();
489 gdk_threads_leave ();
493 gtk_application_init (GtkApplication *application)
495 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
496 GTK_TYPE_APPLICATION,
497 GtkApplicationPrivate);
499 #ifdef GDK_WINDOWING_X11
500 application->priv->next_id = 1;
505 gtk_application_window_added (GtkApplication *application,
508 GtkApplicationPrivate *priv = application->priv;
510 priv->windows = g_list_prepend (priv->windows, window);
511 gtk_window_set_application (window, application);
512 g_application_hold (G_APPLICATION (application));
514 g_signal_connect (window, "focus-in-event",
515 G_CALLBACK (gtk_application_focus_in_event_cb),
518 #ifdef GDK_WINDOWING_X11
519 gtk_application_window_added_x11 (application, window);
522 g_object_notify (G_OBJECT (application), "active-window");
526 gtk_application_window_removed (GtkApplication *application,
529 GtkApplicationPrivate *priv = application->priv;
532 old_active = priv->windows;
534 #ifdef GDK_WINDOWING_X11
535 gtk_application_window_removed_x11 (application, window);
538 g_signal_handlers_disconnect_by_func (window,
539 gtk_application_focus_in_event_cb,
542 g_application_release (G_APPLICATION (application));
543 priv->windows = g_list_remove (priv->windows, window);
544 gtk_window_set_application (window, NULL);
546 if (priv->windows != old_active)
547 g_object_notify (G_OBJECT (application), "active-window");
551 extract_accel_from_menu_item (GMenuModel *model,
555 GMenuAttributeIter *iter;
558 const gchar *accel = NULL;
559 const gchar *action = NULL;
560 GVariant *target = NULL;
562 iter = g_menu_model_iterate_item_attributes (model, item);
563 while (g_menu_attribute_iter_get_next (iter, &key, &value))
565 if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
566 action = g_variant_get_string (value, NULL);
567 else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
568 accel = g_variant_get_string (value, NULL);
569 else if (g_str_equal (key, "target"))
570 target = g_variant_ref (value);
571 g_variant_unref (value);
573 g_object_unref (iter);
576 gtk_application_add_accelerator (app, accel, action, target);
579 g_variant_unref (target);
583 extract_accels_from_menu (GMenuModel *model,
591 for (i = 0; i < g_menu_model_get_n_items (model); i++)
593 extract_accel_from_menu_item (model, i, app);
595 iter = g_menu_model_iterate_item_links (model, i);
596 while (g_menu_link_iter_get_next (iter, &key, &m))
598 extract_accels_from_menu (m, app);
601 g_object_unref (iter);
606 gtk_application_get_property (GObject *object,
611 GtkApplication *application = GTK_APPLICATION (object);
615 case PROP_REGISTER_SESSION:
616 g_value_set_boolean (value, application->priv->register_session);
620 g_value_set_object (value, gtk_application_get_app_menu (application));
624 g_value_set_object (value, gtk_application_get_menubar (application));
628 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
634 gtk_application_set_property (GObject *object,
639 GtkApplication *application = GTK_APPLICATION (object);
643 case PROP_REGISTER_SESSION:
644 application->priv->register_session = g_value_get_boolean (value);
648 gtk_application_set_app_menu (application, g_value_get_object (value));
652 gtk_application_set_menubar (application, g_value_get_object (value));
656 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
662 gtk_application_finalize (GObject *object)
664 GtkApplication *application = GTK_APPLICATION (object);
666 g_clear_object (&application->priv->app_menu);
667 g_clear_object (&application->priv->menubar);
669 G_OBJECT_CLASS (gtk_application_parent_class)
674 gtk_application_class_init (GtkApplicationClass *class)
676 GObjectClass *object_class = G_OBJECT_CLASS (class);
677 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
679 object_class->get_property = gtk_application_get_property;
680 object_class->set_property = gtk_application_set_property;
681 object_class->finalize = gtk_application_finalize;
683 application_class->add_platform_data = gtk_application_add_platform_data;
684 application_class->before_emit = gtk_application_before_emit;
685 application_class->after_emit = gtk_application_after_emit;
686 application_class->startup = gtk_application_startup;
687 application_class->shutdown = gtk_application_shutdown;
689 class->window_added = gtk_application_window_added;
690 class->window_removed = gtk_application_window_removed;
692 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
695 * GtkApplication::window-added:
696 * @application: the #GtkApplication which emitted the signal
697 * @window: the newly-added #GtkWindow
699 * Emitted when a #GtkWindow is added to @application through
700 * gtk_application_add_window().
704 gtk_application_signals[WINDOW_ADDED] =
705 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
706 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
708 g_cclosure_marshal_VOID__OBJECT,
709 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
712 * GtkApplication::window-removed:
713 * @application: the #GtkApplication which emitted the signal
714 * @window: the #GtkWindow that is being removed
716 * Emitted when a #GtkWindow is removed from @application,
717 * either as a side-effect of being destroyed or explicitly
718 * through gtk_application_remove_window().
722 gtk_application_signals[WINDOW_REMOVED] =
723 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
724 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
726 g_cclosure_marshal_VOID__OBJECT,
727 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
730 * GtkApplication:register-session:
732 * Set this property to %TRUE to register with the session manager.
736 g_object_class_install_property (object_class, PROP_REGISTER_SESSION,
737 g_param_spec_boolean ("register-session",
738 P_("Register session"),
739 P_("Register with the session manager"),
740 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
742 g_object_class_install_property (object_class, PROP_APP_MENU,
743 g_param_spec_object ("app-menu",
744 P_("Application menu"),
745 P_("The GMenuModel for the application menu"),
747 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
749 g_object_class_install_property (object_class, PROP_MENUBAR,
750 g_param_spec_object ("menubar",
752 P_("The GMenuModel for the menubar"),
754 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
756 g_object_class_install_property (object_class, PROP_ACTIVE_WINDOW,
757 g_param_spec_object ("active-window",
759 P_("The window which most recently had focus"),
761 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
765 * gtk_application_new:
766 * @application_id: (allow-none): The application ID.
767 * @flags: the application flags
769 * Creates a new #GtkApplication instance.
771 * When using #GtkApplication, it is not necessary to call gtk_init()
772 * manually. It is called as soon as the application gets registered as
773 * the primary instance.
775 * Concretely, gtk_init() is called in the default handler for the
776 * #GApplication::startup signal. Therefore, #GtkApplication subclasses should
777 * chain up in their #GApplication:startup handler before using any GTK+ API.
779 * Note that commandline arguments are not passed to gtk_init().
780 * All GTK+ functionality that is available via commandline arguments
781 * can also be achieved by setting suitable environment variables
782 * such as <envar>G_DEBUG</envar>, so this should not be a big
783 * problem. If you absolutely must support GTK+ commandline arguments,
784 * you can explicitly call gtk_init() before creating the application
787 * If non-%NULL, the application ID must be valid. See
788 * g_application_id_is_valid().
790 * If no application ID is given then some features (most notably application
791 * uniqueness) will be disabled. A null application ID is only allowed with
794 * Returns: a new #GtkApplication instance
799 gtk_application_new (const gchar *application_id,
800 GApplicationFlags flags)
802 g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
804 return g_object_new (GTK_TYPE_APPLICATION,
805 "application-id", application_id,
811 * gtk_application_add_window:
812 * @application: a #GtkApplication
813 * @window: a #GtkWindow
815 * Adds a window to @application.
817 * This call is equivalent to setting the #GtkWindow:application
818 * property of @window to @application.
820 * Normally, the connection between the application and the window
821 * will remain until the window is destroyed, but you can explicitly
822 * remove it with gtk_application_remove_window().
824 * GTK+ will keep the application running as long as it has
830 gtk_application_add_window (GtkApplication *application,
833 g_return_if_fail (GTK_IS_APPLICATION (application));
835 if (!g_list_find (application->priv->windows, window))
836 g_signal_emit (application,
837 gtk_application_signals[WINDOW_ADDED], 0, window);
841 * gtk_application_remove_window:
842 * @application: a #GtkApplication
843 * @window: a #GtkWindow
845 * Remove a window from @application.
847 * If @window belongs to @application then this call is equivalent to
848 * setting the #GtkWindow:application property of @window to
851 * The application may stop running as a result of a call to this
857 gtk_application_remove_window (GtkApplication *application,
860 g_return_if_fail (GTK_IS_APPLICATION (application));
862 if (g_list_find (application->priv->windows, window))
863 g_signal_emit (application,
864 gtk_application_signals[WINDOW_REMOVED], 0, window);
868 * gtk_application_get_windows:
869 * @application: a #GtkApplication
871 * Gets a list of the #GtkWindows associated with @application.
873 * The list is sorted by most recently focused window, such that the first
874 * element is the currently focused window. (Useful for choosing a parent
875 * for a transient window.)
877 * The list that is returned should not be modified in any way. It will
878 * only remain valid until the next focus change or window creation or
881 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
886 gtk_application_get_windows (GtkApplication *application)
888 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
890 return application->priv->windows;
894 * gtk_application_get_window_by_id:
895 * @application: a #GtkApplication
896 * @id: an identifier number
898 * Returns the #GtkApplicationWindow with the given ID.
900 * Returns: (transfer none): the window with ID @id, or
901 * %NULL if there is no window with this ID
906 gtk_application_get_window_by_id (GtkApplication *application,
911 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
913 for (l = application->priv->windows; l != NULL; l = l->next)
915 if (GTK_IS_APPLICATION_WINDOW (l->data) &&
916 gtk_application_window_get_id (GTK_APPLICATION_WINDOW (l->data)) == id)
924 * gtk_application_get_active_window:
925 * @application: a #GtkApplication
927 * Gets the "active" window for the application.
929 * The active window is the one that was most recently focused (within
930 * the application). This window may not have the focus at the moment
931 * if another application has it -- this is just the most
932 * recently-focused window within this application.
934 * Returns: (transfer none): the active window
939 gtk_application_get_active_window (GtkApplication *application)
941 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
943 return application->priv->windows ? application->priv->windows->data : NULL;
947 * gtk_application_add_accelerator:
948 * @application: a #GtkApplication
949 * @accelerator: accelerator string
950 * @action_name: the name of the action to activate
951 * @parameter: (allow-none): parameter to pass when activating the action,
952 * or %NULL if the action does not accept an activation parameter
954 * Installs an accelerator that will cause the named action
955 * to be activated when the key combination specificed by @accelerator
958 * @accelerator must be a string that can be parsed by
959 * gtk_accelerator_parse(), e.g. "<Primary>q" or
960 * "<Control><Alt>p".
962 * @action_name must be the name of an action as it would be used
963 * in the app menu, i.e. actions that have been added to the application
964 * are referred to with an "app." prefix, and window-specific actions
965 * with a "win." prefix.
967 * GtkApplication also extracts accelerators out of 'accel' attributes
968 * in the #GMenuModels passed to gtk_application_set_app_menu() and
969 * gtk_application_set_menubar(), which is usually more convenient
970 * than calling this function for each accelerator.
975 gtk_application_add_accelerator (GtkApplication *application,
976 const gchar *accelerator,
977 const gchar *action_name,
982 GdkModifierType accel_mods;
984 g_return_if_fail (GTK_IS_APPLICATION (application));
986 /* Call this here, since gtk_init() is only getting called in startup() */
987 _gtk_accel_map_init ();
989 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
993 g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
997 accel_path = _gtk_accel_path_for_action (action_name, parameter);
999 if (gtk_accel_map_lookup_entry (accel_path, NULL))
1000 gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
1002 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
1004 g_free (accel_path);
1008 * gtk_application_remove_accelerator:
1009 * @application: a #GtkApplication
1010 * @action_name: the name of the action to activate
1011 * @parameter: (allow-none): parameter to pass when activating the action,
1012 * or %NULL if the action does not accept an activation parameter
1014 * Removes an accelerator that has been previously added
1015 * with gtk_application_add_accelerator().
1020 gtk_application_remove_accelerator (GtkApplication *application,
1021 const gchar *action_name,
1022 GVariant *parameter)
1026 g_return_if_fail (GTK_IS_APPLICATION (application));
1028 accel_path = _gtk_accel_path_for_action (action_name, parameter);
1030 if (!gtk_accel_map_lookup_entry (accel_path, NULL))
1032 g_warning ("No accelerator found for '%s'\n", accel_path);
1033 g_free (accel_path);
1037 gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
1038 g_free (accel_path);
1042 * gtk_application_set_app_menu:
1043 * @application: a #GtkApplication
1044 * @app_menu: (allow-none): a #GMenuModel, or %NULL
1046 * Sets or unsets the application menu for @application.
1048 * This can only be done in the primary instance of the application,
1049 * after it has been registered. #GApplication:startup is a good place
1052 * The application menu is a single menu containing items that typically
1053 * impact the application as a whole, rather than acting on a specific
1054 * window or document. For example, you would expect to see
1055 * "Preferences" or "Quit" in an application menu, but not "Save" or
1058 * If supported, the application menu will be rendered by the desktop
1061 * Use the base #GActionMap interface to add actions, to respond to the user
1062 * selecting these menu items.
1067 gtk_application_set_app_menu (GtkApplication *application,
1068 GMenuModel *app_menu)
1070 g_return_if_fail (GTK_IS_APPLICATION (application));
1071 g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1072 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1074 if (app_menu != application->priv->app_menu)
1076 if (application->priv->app_menu != NULL)
1077 g_object_unref (application->priv->app_menu);
1079 application->priv->app_menu = app_menu;
1081 if (application->priv->app_menu != NULL)
1082 g_object_ref (application->priv->app_menu);
1085 extract_accels_from_menu (app_menu, application);
1087 #ifdef GDK_WINDOWING_X11
1088 gtk_application_set_app_menu_x11 (application, app_menu);
1091 g_object_notify (G_OBJECT (application), "app-menu");
1096 * gtk_application_get_app_menu:
1097 * @application: a #GtkApplication
1099 * Returns the menu model that has been set with
1100 * gtk_application_set_app_menu().
1102 * Returns: (transfer none): the application menu of @application
1107 gtk_application_get_app_menu (GtkApplication *application)
1109 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1111 return application->priv->app_menu;
1115 * gtk_application_set_menubar:
1116 * @application: a #GtkApplication
1117 * @menubar: (allow-none): a #GMenuModel, or %NULL
1119 * Sets or unsets the menubar for windows of @application.
1121 * This is a menubar in the traditional sense.
1123 * This can only be done in the primary instance of the application,
1124 * after it has been registered. #GApplication:startup is a good place
1127 * Depending on the desktop environment, this may appear at the top of
1128 * each window, or at the top of the screen. In some environments, if
1129 * both the application menu and the menubar are set, the application
1130 * menu will be presented as if it were the first item of the menubar.
1131 * Other environments treat the two as completely separate -- for
1132 * example, the application menu may be rendered by the desktop shell
1133 * while the menubar (if set) remains in each individual window.
1135 * Use the base #GActionMap interface to add actions, to respond to the user
1136 * selecting these menu items.
1141 gtk_application_set_menubar (GtkApplication *application,
1142 GMenuModel *menubar)
1144 g_return_if_fail (GTK_IS_APPLICATION (application));
1145 g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1146 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1148 if (menubar != application->priv->menubar)
1150 if (application->priv->menubar != NULL)
1151 g_object_unref (application->priv->menubar);
1153 application->priv->menubar = menubar;
1155 if (application->priv->menubar != NULL)
1156 g_object_ref (application->priv->menubar);
1159 extract_accels_from_menu (menubar, application);
1161 #ifdef GDK_WINDOWING_X11
1162 gtk_application_set_menubar_x11 (application, menubar);
1165 g_object_notify (G_OBJECT (application), "menubar");
1170 * gtk_application_get_menubar:
1171 * @application: a #GtkApplication
1173 * Returns the menu model that has been set with
1174 * gtk_application_set_menubar().
1176 * Returns: (transfer none): the menubar for windows of @application
1181 gtk_application_get_menubar (GtkApplication *application)
1183 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1185 return application->priv->menubar;
1188 #if defined(GDK_WINDOWING_X11)
1190 /* D-Bus Session Management
1192 * The protocol and the D-Bus API are described here:
1193 * http://live.gnome.org/SessionManagement/GnomeSession
1194 * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1198 unregister_client (GtkApplication *app)
1200 GError *error = NULL;
1202 g_debug ("Unregistering client");
1204 g_dbus_proxy_call_sync (app->priv->sm_proxy,
1206 g_variant_new ("(o)", app->priv->client_path),
1207 G_DBUS_CALL_FLAGS_NONE,
1214 g_warning ("Failed to unregister client: %s", error->message);
1215 g_error_free (error);
1218 g_clear_object (&app->priv->client_proxy);
1220 g_free (app->priv->client_path);
1221 app->priv->client_path = NULL;
1225 gtk_application_quit_response (GtkApplication *application,
1227 const gchar *reason)
1229 g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1231 g_dbus_proxy_call (application->priv->client_proxy,
1232 "EndSessionResponse",
1233 g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1234 G_DBUS_CALL_FLAGS_NONE,
1239 client_proxy_signal (GDBusProxy *proxy,
1240 const gchar *sender_name,
1241 const gchar *signal_name,
1242 GVariant *parameters,
1243 GtkApplication *app)
1245 if (strcmp (signal_name, "QueryEndSession") == 0)
1247 g_debug ("Received QueryEndSession");
1248 gtk_application_quit_response (app, TRUE, NULL);
1250 else if (strcmp (signal_name, "CancelEndSession") == 0)
1252 g_debug ("Received CancelEndSession");
1254 else if (strcmp (signal_name, "EndSession") == 0)
1256 g_debug ("Received EndSession");
1257 gtk_application_quit_response (app, TRUE, NULL);
1258 unregister_client (app);
1259 g_application_quit (G_APPLICATION (app));
1261 else if (strcmp (signal_name, "Stop") == 0)
1263 g_debug ("Received Stop");
1264 unregister_client (app);
1265 g_application_quit (G_APPLICATION (app));
1270 gtk_application_startup_session_dbus (GtkApplication *app)
1272 static gchar *client_id;
1273 GError *error = NULL;
1276 if (app->priv->session_bus == NULL)
1279 if (client_id == NULL)
1281 const gchar *desktop_autostart_id;
1283 desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1284 /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1285 * use the same client id.
1287 g_unsetenv ("DESKTOP_AUTOSTART_ID");
1288 client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1291 g_debug ("Connecting to session manager");
1293 app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1294 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1295 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1297 "org.gnome.SessionManager",
1298 "/org/gnome/SessionManager",
1299 "org.gnome.SessionManager",
1304 g_warning ("Failed to get a session proxy: %s", error->message);
1305 g_error_free (error);
1309 /* FIXME: should we reuse the D-Bus application id here ? */
1310 app->priv->app_id = g_strdup (g_get_prgname ());
1312 if (!app->priv->register_session)
1315 g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1317 res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1319 g_variant_new ("(ss)", app->priv->app_id, client_id),
1320 G_DBUS_CALL_FLAGS_NONE,
1327 g_warning ("Failed to register client: %s", error->message);
1328 g_error_free (error);
1329 g_clear_object (&app->priv->sm_proxy);
1333 g_variant_get (res, "(o)", &app->priv->client_path);
1334 g_variant_unref (res);
1336 g_debug ("Registered client at '%s'", app->priv->client_path);
1338 app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1340 "org.gnome.SessionManager",
1341 app->priv->client_path,
1342 "org.gnome.SessionManager.ClientPrivate",
1347 g_warning ("Failed to get client proxy: %s", error->message);
1348 g_error_free (error);
1349 g_clear_object (&app->priv->sm_proxy);
1350 g_free (app->priv->client_path);
1351 app->priv->client_path = NULL;
1355 g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1361 * GtkApplicationInhibitFlags:
1362 * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit ending the user session
1363 * by logging out or by shutting down the computer
1364 * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1365 * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1366 * session or computer
1367 * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1368 * marked as idle (and possibly locked)
1370 * Types of user actions that may be blocked by gtk_application_inhibit().
1376 * gtk_application_inhibit:
1377 * @application: the #GApplication
1378 * @window: (allow-none): a #GtkWindow, or %NULL
1379 * @flags: what types of actions should be inhibited
1380 * @reason: (allow-none): a short, human-readable string that explains
1381 * why these operations are inhibited
1383 * Inform the session manager that certain types of actions should be
1384 * inhibited. This is not guaranteed to work on all platforms and for
1385 * all types of actions.
1387 * Applications should invoke this method when they begin an operation
1388 * that should not be interrupted, such as creating a CD or DVD. The
1389 * types of actions that may be blocked are specified by the @flags
1390 * parameter. When the application completes the operation it should
1391 * call gtk_application_uninhibit() to remove the inhibitor. Note that
1392 * an application can have multiple inhibitors, and all of the must
1393 * be individually removed. Inhibitors are also cleared when the
1394 * application exits.
1396 * Applications should not expect that they will always be able to block
1397 * the action. In most cases, users will be given the option to force
1398 * the action to take place.
1400 * Reasons should be short and to the point.
1402 * If @window is given, the session manager may point the user to
1403 * this window to find out more about why the action is inhibited.
1405 * Returns: A non-zero cookie that is used to uniquely identify this
1406 * request. It should be used as an argument to gtk_application_uninhibit()
1407 * in order to remove the request. If the platform does not support
1408 * inhibiting or the request failed for some reason, 0 is returned.
1413 gtk_application_inhibit (GtkApplication *application,
1415 GtkApplicationInhibitFlags flags,
1416 const gchar *reason)
1419 GError *error = NULL;
1423 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1424 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1425 g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1428 xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1432 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1434 g_variant_new ("(susu)",
1435 application->priv->app_id,
1439 G_DBUS_CALL_FLAGS_NONE,
1445 g_warning ("Calling Inhibit failed: %s", error->message);
1446 g_error_free (error);
1450 g_variant_get (res, "(u)", &cookie);
1451 g_variant_unref (res);
1457 * gtk_application_uninhibit:
1458 * @application: the #GApplication
1459 * @cookie: a cookie that was returned by gtk_application_inhibit()
1461 * Removes an inhibitor that has been established with gtk_application_inhibit().
1462 * Inhibitors are also cleared when the application exits.
1467 gtk_application_uninhibit (GtkApplication *application,
1470 g_return_if_fail (GTK_IS_APPLICATION (application));
1471 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1472 g_return_if_fail (application->priv->sm_proxy != NULL);
1474 g_dbus_proxy_call (application->priv->sm_proxy,
1476 g_variant_new ("(u)", cookie),
1477 G_DBUS_CALL_FLAGS_NONE,
1483 * gtk_application_is_inhibited:
1484 * @application: the #GApplication
1485 * @flags: what types of actions should be queried
1487 * Determines if any of the actions specified in @flags are
1488 * currently inhibited (possibly by another application).
1490 * Returns: %TRUE if any of the actions specified in @flags are inhibited
1495 gtk_application_is_inhibited (GtkApplication *application,
1496 GtkApplicationInhibitFlags flags)
1499 GError *error = NULL;
1502 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1503 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1504 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1506 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1508 g_variant_new ("(u)", flags),
1509 G_DBUS_CALL_FLAGS_NONE,
1515 g_warning ("Calling IsInhibited failed: %s", error->message);
1516 g_error_free (error);
1520 g_variant_get (res, "(b)", &inhibited);
1521 g_variant_unref (res);
1526 #elif defined(GDK_WINDOWING_QUARTZ)
1528 /* OS X implementation copied from EggSMClient, but simplified since
1529 * it doesn't need to interact with the user.
1533 idle_will_quit (gpointer data)
1535 GtkApplication *app = data;
1537 if (app->priv->quit_inhibit == 0)
1538 g_application_quit (G_APPLICATION (app));
1541 GtkApplicationQuartzInhibitor *inhibitor;
1545 for (iter = app->priv->inhibitors; iter; iter = iter->next)
1547 inhibitor = iter->data;
1548 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1551 g_assert (inhibitor != NULL);
1553 dialog = gtk_message_dialog_new (inhibitor->window,
1557 _("%s cannot quit at this time:\n\n%s"),
1558 g_get_application_name (),
1560 g_signal_connect_swapped (dialog,
1562 G_CALLBACK (gtk_widget_destroy),
1564 gtk_widget_show_all (dialog);
1567 return G_SOURCE_REMOVE;
1571 quit_requested (const AppleEvent *aevt,
1575 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1577 /* Don't emit the "quit" signal immediately, since we're
1578 * called from a weird point in the guts of gdkeventloop-quartz.c
1580 g_idle_add_full (G_PRIORITY_DEFAULT, idle_will_quit, app, NULL);
1582 return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
1586 gtk_application_startup_session_quartz (GtkApplication *app)
1588 if (app->priv->register_session)
1589 AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1590 NewAEEventHandlerUPP (quit_requested),
1591 (long)GPOINTER_TO_SIZE (app), false);
1595 gtk_application_inhibit (GtkApplication *application,
1597 GtkApplicationInhibitFlags flags,
1598 const gchar *reason)
1600 GtkApplicationQuartzInhibitor *inhibitor;
1602 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1603 g_return_val_if_fail (flags != 0, 0);
1605 inhibitor = g_slice_new (GtkApplicationQuartzInhibitor);
1606 inhibitor->cookie = ++application->priv->next_cookie;
1607 inhibitor->flags = flags;
1608 inhibitor->reason = g_strdup (reason);
1609 inhibitor->window = window ? g_object_ref (window) : NULL;
1611 application->priv->inhibitors = g_slist_prepend (application->priv->inhibitors, inhibitor);
1613 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1614 application->priv->quit_inhibit++;
1616 return inhibitor->cookie;
1620 gtk_application_uninhibit (GtkApplication *application,
1625 for (iter = application->priv->inhibitors; iter; iter = iter->next)
1627 GtkApplicationQuartzInhibitor *inhibitor = iter->data;
1629 if (inhibitor->cookie == cookie)
1631 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1632 application->priv->quit_inhibit--;
1633 gtk_application_quartz_inhibitor_free (inhibitor);
1634 application->priv->inhibitors = g_slist_delete_link (application->priv->inhibitors, iter);
1639 g_warning ("Invalid inhibitor cookie");
1643 gtk_application_is_inhibited (GtkApplication *application,
1644 GtkApplicationInhibitFlags flags)
1646 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1647 return application->priv->quit_inhibit > 0;
1654 /* Trivial implementation.
1656 * For the inhibit API on Windows, see
1657 * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1661 gtk_application_inhibit (GtkApplication *application,
1663 GtkApplicationInhibitFlags flags,
1664 const gchar *reason)
1670 gtk_application_uninhibit (GtkApplication *application,
1676 gtk_application_is_inhibited (GtkApplication *application,
1677 GtkApplicationInhibitFlags flags)