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>
51 * SECTION:gtkapplication
52 * @title: GtkApplication
53 * @short_description: Application class
55 * #GtkApplication is a class that handles many important aspects
56 * of a GTK+ application in a convenient fashion, without enforcing
57 * a one-size-fits-all application model.
59 * Currently, GtkApplication handles GTK+ initialization, application
60 * uniqueness, session management, provides some basic scriptability and
61 * desktop shell integration by exporting actions and menus and manages a
62 * list of toplevel windows whose life-cycle is automatically tied to the
63 * life-cycle of your application.
65 * While GtkApplication works fine with plain #GtkWindows, it is recommended
66 * to use it together with #GtkApplicationWindow.
68 * When GDK threads are enabled, GtkApplication will acquire the GDK
69 * lock when invoking actions that arrive from other processes. The GDK
70 * lock is not touched for local action invocations. In order to have
71 * actions invoked in a predictable context it is therefore recommended
72 * that the GDK lock be held while invoking actions locally with
73 * g_action_group_activate_action(). The same applies to actions
74 * associated with #GtkApplicationWindow and to the 'activate' and
75 * 'open' #GApplication methods.
77 * To set an application menu for a GtkApplication, use
78 * gtk_application_set_app_menu(). The #GMenuModel that this function
79 * expects is usually constructed using #GtkBuilder, as seen in the
80 * following example. To specify a menubar that will be shown by
81 * #GtkApplicationWindows, use gtk_application_set_menubar(). Use the base
82 * #GActionMap interface to add actions, to respond to the user
83 * selecting these menu items.
85 * GTK+ displays these menus 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 block various ways to end the session with
114 * the gtk_application_inhibit() function. Typical use cases for
115 * this kind of inhibiting are long-running, uninterruptible operations,
116 * such as burning a CD or performing a disk backup. The session
117 * manager may not honor the inhibitor, but it can be expected to
118 * inform the user about the negative consequences of ending the
119 * session while inhibitors are present.
128 static guint gtk_application_signals[LAST_SIGNAL];
132 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
177 #ifdef GDK_WINDOWING_X11
179 gtk_application_x11_publish_menu (GtkApplication *application,
187 if (application->priv->session_bus == NULL)
190 /* unexport any existing menu */
193 g_dbus_connection_unexport_menu_model (application->priv->session_bus, *id);
199 /* export the new menu, if there is one */
202 /* try getting the preferred name */
203 *path = g_strconcat (application->priv->object_path, "/menus/", type, NULL);
204 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
206 /* keep trying until we get a working name... */
207 for (i = 0; *id == 0; i++)
210 *path = g_strdup_printf ("%s/menus/%s%d", application->priv->object_path, type, i);
211 *id = g_dbus_connection_export_menu_model (application->priv->session_bus, *path, model, NULL);
217 gtk_application_set_app_menu_x11 (GtkApplication *application,
218 GMenuModel *app_menu)
220 g_free (application->priv->app_menu_path);
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 g_free (application->priv->menubar_path);
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 g_clear_object (&application->priv->sm_proxy);
296 g_clear_object (&application->priv->client_proxy);
297 g_free (application->priv->app_id);
298 g_free (application->priv->client_path);
300 g_free (application->priv->app_menu_path);
301 g_free (application->priv->menubar_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 /* Try storing all clipboard data we have */
429 _gtk_clipboard_store_all ();
431 /* Synchronize the recent manager singleton */
432 _gtk_recent_manager_sync ();
434 G_APPLICATION_CLASS (gtk_application_parent_class)
435 ->shutdown (application);
439 gtk_application_add_platform_data (GApplication *application,
440 GVariantBuilder *builder)
442 const gchar *startup_id;
444 startup_id = getenv ("DESKTOP_STARTUP_ID");
446 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
447 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
448 g_variant_new_string (startup_id));
452 gtk_application_before_emit (GApplication *application,
453 GVariant *platform_data)
459 gdk_threads_enter ();
461 g_variant_iter_init (&iter, platform_data);
462 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
464 #ifdef GDK_WINDOWING_X11
465 if (strcmp (key, "desktop-startup-id") == 0)
470 display = gdk_display_get_default ();
471 id = g_variant_get_string (value, NULL);
472 if (GDK_IS_X11_DISPLAY (display))
473 gdk_x11_display_set_startup_notification_id (display, id);
480 gtk_application_after_emit (GApplication *application,
481 GVariant *platform_data)
483 gdk_notify_startup_complete ();
485 gdk_threads_leave ();
489 gtk_application_init (GtkApplication *application)
491 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
492 GTK_TYPE_APPLICATION,
493 GtkApplicationPrivate);
495 #ifdef GDK_WINDOWING_X11
496 application->priv->next_id = 1;
501 gtk_application_window_added (GtkApplication *application,
504 GtkApplicationPrivate *priv = application->priv;
506 priv->windows = g_list_prepend (priv->windows, window);
507 gtk_window_set_application (window, application);
508 g_application_hold (G_APPLICATION (application));
510 g_signal_connect (window, "focus-in-event",
511 G_CALLBACK (gtk_application_focus_in_event_cb),
514 #ifdef GDK_WINDOWING_X11
515 gtk_application_window_added_x11 (application, window);
518 g_object_notify (G_OBJECT (application), "active-window");
522 gtk_application_window_removed (GtkApplication *application,
525 GtkApplicationPrivate *priv = application->priv;
528 old_active = priv->windows;
530 #ifdef GDK_WINDOWING_X11
531 gtk_application_window_removed_x11 (application, window);
534 g_signal_handlers_disconnect_by_func (window,
535 gtk_application_focus_in_event_cb,
538 g_application_release (G_APPLICATION (application));
539 priv->windows = g_list_remove (priv->windows, window);
540 gtk_window_set_application (window, NULL);
542 if (priv->windows != old_active)
543 g_object_notify (G_OBJECT (application), "active-window");
547 extract_accel_from_menu_item (GMenuModel *model,
551 GMenuAttributeIter *iter;
554 const gchar *accel = NULL;
555 const gchar *action = NULL;
556 GVariant *target = NULL;
558 iter = g_menu_model_iterate_item_attributes (model, item);
559 while (g_menu_attribute_iter_get_next (iter, &key, &value))
561 if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
562 action = g_variant_get_string (value, NULL);
563 else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
564 accel = g_variant_get_string (value, NULL);
565 else if (g_str_equal (key, "target"))
566 target = g_variant_ref (value);
567 g_variant_unref (value);
569 g_object_unref (iter);
572 gtk_application_add_accelerator (app, accel, action, target);
575 g_variant_unref (target);
579 extract_accels_from_menu (GMenuModel *model,
587 for (i = 0; i < g_menu_model_get_n_items (model); i++)
589 extract_accel_from_menu_item (model, i, app);
591 iter = g_menu_model_iterate_item_links (model, i);
592 while (g_menu_link_iter_get_next (iter, &key, &m))
594 extract_accels_from_menu (m, app);
597 g_object_unref (iter);
602 gtk_application_get_property (GObject *object,
607 GtkApplication *application = GTK_APPLICATION (object);
611 case PROP_REGISTER_SESSION:
612 g_value_set_boolean (value, application->priv->register_session);
616 g_value_set_object (value, gtk_application_get_app_menu (application));
620 g_value_set_object (value, gtk_application_get_menubar (application));
624 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
630 gtk_application_set_property (GObject *object,
635 GtkApplication *application = GTK_APPLICATION (object);
639 case PROP_REGISTER_SESSION:
640 application->priv->register_session = g_value_get_boolean (value);
644 gtk_application_set_app_menu (application, g_value_get_object (value));
648 gtk_application_set_menubar (application, g_value_get_object (value));
652 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
658 gtk_application_finalize (GObject *object)
660 GtkApplication *application = GTK_APPLICATION (object);
662 g_clear_object (&application->priv->app_menu);
663 g_clear_object (&application->priv->menubar);
665 G_OBJECT_CLASS (gtk_application_parent_class)
670 gtk_application_class_init (GtkApplicationClass *class)
672 GObjectClass *object_class = G_OBJECT_CLASS (class);
673 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
675 object_class->get_property = gtk_application_get_property;
676 object_class->set_property = gtk_application_set_property;
677 object_class->finalize = gtk_application_finalize;
679 application_class->add_platform_data = gtk_application_add_platform_data;
680 application_class->before_emit = gtk_application_before_emit;
681 application_class->after_emit = gtk_application_after_emit;
682 application_class->startup = gtk_application_startup;
683 application_class->shutdown = gtk_application_shutdown;
685 class->window_added = gtk_application_window_added;
686 class->window_removed = gtk_application_window_removed;
688 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
691 * GtkApplication::window-added:
692 * @application: the #GtkApplication which emitted the signal
693 * @window: the newly-added #GtkWindow
695 * Emitted when a #GtkWindow is added to @application through
696 * gtk_application_add_window().
700 gtk_application_signals[WINDOW_ADDED] =
701 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
702 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
704 g_cclosure_marshal_VOID__OBJECT,
705 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
708 * GtkApplication::window-removed:
709 * @application: the #GtkApplication which emitted the signal
710 * @window: the #GtkWindow that is being removed
712 * Emitted when a #GtkWindow is removed from @application,
713 * either as a side-effect of being destroyed or explicitly
714 * through gtk_application_remove_window().
718 gtk_application_signals[WINDOW_REMOVED] =
719 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
720 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
722 g_cclosure_marshal_VOID__OBJECT,
723 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
726 * GtkApplication:register-session:
728 * Set this property to %TRUE to register with the session manager.
732 g_object_class_install_property (object_class, PROP_REGISTER_SESSION,
733 g_param_spec_boolean ("register-session",
734 P_("Register session"),
735 P_("Register with the session manager"),
736 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
738 g_object_class_install_property (object_class, PROP_APP_MENU,
739 g_param_spec_object ("app-menu",
740 P_("Application menu"),
741 P_("The GMenuModel for the application menu"),
743 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
745 g_object_class_install_property (object_class, PROP_MENUBAR,
746 g_param_spec_object ("menubar",
748 P_("The GMenuModel for the menubar"),
750 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
752 g_object_class_install_property (object_class, PROP_ACTIVE_WINDOW,
753 g_param_spec_object ("active-window",
755 P_("The window which most recently had focus"),
757 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
761 * gtk_application_new:
762 * @application_id: (allow-none): The application ID.
763 * @flags: the application flags
765 * Creates a new #GtkApplication instance.
767 * This function calls g_type_init() for you. gtk_init() is called
768 * as soon as the application gets registered as the primary instance.
770 * Concretely, gtk_init() is called in the default handler for the
771 * #GApplication::startup signal. Therefore, #GtkApplication subclasses should
772 * chain up in their #GApplication:startup handler before using any GTK+ API.
774 * Note that commandline arguments are not passed to gtk_init().
775 * All GTK+ functionality that is available via commandline arguments
776 * can also be achieved by setting suitable environment variables
777 * such as <envar>G_DEBUG</envar>, so this should not be a big
778 * problem. If you absolutely must support GTK+ commandline arguments,
779 * you can explicitly call gtk_init() before creating the application
782 * If non-%NULL, the application ID must be valid. See
783 * g_application_id_is_valid().
785 * If no application ID is given then some features (most notably application
786 * uniqueness) will be disabled. A null application ID is only allowed with
789 * Returns: a new #GtkApplication instance
794 gtk_application_new (const gchar *application_id,
795 GApplicationFlags flags)
797 g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
801 return g_object_new (GTK_TYPE_APPLICATION,
802 "application-id", application_id,
808 * gtk_application_add_window:
809 * @application: a #GtkApplication
810 * @window: a #GtkWindow
812 * Adds a window to @application.
814 * This call is equivalent to setting the #GtkWindow:application
815 * property of @window to @application.
817 * Normally, the connection between the application and the window
818 * will remain until the window is destroyed, but you can explicitly
819 * remove it with gtk_application_remove_window().
821 * GTK+ will keep the application running as long as it has
827 gtk_application_add_window (GtkApplication *application,
830 g_return_if_fail (GTK_IS_APPLICATION (application));
832 if (!g_list_find (application->priv->windows, window))
833 g_signal_emit (application,
834 gtk_application_signals[WINDOW_ADDED], 0, window);
838 * gtk_application_remove_window:
839 * @application: a #GtkApplication
840 * @window: a #GtkWindow
842 * Remove a window from @application.
844 * If @window belongs to @application then this call is equivalent to
845 * setting the #GtkWindow:application property of @window to
848 * The application may stop running as a result of a call to this
854 gtk_application_remove_window (GtkApplication *application,
857 g_return_if_fail (GTK_IS_APPLICATION (application));
859 if (g_list_find (application->priv->windows, window))
860 g_signal_emit (application,
861 gtk_application_signals[WINDOW_REMOVED], 0, window);
865 * gtk_application_get_windows:
866 * @application: a #GtkApplication
868 * Gets a list of the #GtkWindows associated with @application.
870 * The list is sorted by most recently focused window, such that the first
871 * element is the currently focused window. (Useful for choosing a parent
872 * for a transient window.)
874 * The list that is returned should not be modified in any way. It will
875 * only remain valid until the next focus change or window creation or
878 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
883 gtk_application_get_windows (GtkApplication *application)
885 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
887 return application->priv->windows;
891 * gtk_application_get_window_by_id:
892 * @application: a #GtkApplication
893 * @id: an identifier number
895 * Returns: (transfer none): the #GtkApplicationWindow with ID @id, or
896 * %NULL if there is no window with this ID
901 gtk_application_get_window_by_id (GtkApplication *application,
906 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
908 for (l = application->priv->windows; l != NULL; l = l->next)
910 if (GTK_IS_APPLICATION_WINDOW (l->data) &&
911 gtk_application_window_get_id (GTK_APPLICATION_WINDOW (l->data)) == id)
919 * gtk_application_get_active_window:
920 * @application: a #GtkApplication
922 * Gets the "active" window for the application.
924 * The active window is the one that was most recently focused (within
925 * the application). This window may not have the focus at the moment
926 * if another application has it -- this is just the most
927 * recently-focused window within this application.
929 * Returns: (transfer none): the active window
934 gtk_application_get_active_window (GtkApplication *application)
936 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
938 return application->priv->windows ? application->priv->windows->data : NULL;
942 * gtk_application_add_accelerator:
943 * @application: a #GtkApplication
944 * @accelerator: accelerator string
945 * @action_name: the name of the action to activate
946 * @parameter: (allow-none): parameter to pass when activating the action,
947 * or %NULL if the action does not accept an activation parameter
949 * Installs an accelerator that will cause the named action
950 * to be activated when the key combination specificed by @accelerator
953 * @accelerator must be a string that can be parsed by
954 * gtk_accelerator_parse(), e.g. "<Primary>q" or
955 * "<Control><Alt>p".
957 * @action_name must be the name of an action as it would be used
958 * in the app menu, i.e. actions that have been added to the application
959 * are referred to with an "app." prefix, and window-specific actions
960 * with a "win." prefix.
962 * GtkApplication also extracts accelerators out of 'accel' attributes
963 * in the #GMenuModels passed to gtk_application_set_app_menu() and
964 * gtk_application_set_menubar(), which is usually more convenient
965 * than calling this function for each accelerator.
970 gtk_application_add_accelerator (GtkApplication *application,
971 const gchar *accelerator,
972 const gchar *action_name,
977 GdkModifierType accel_mods;
979 g_return_if_fail (GTK_IS_APPLICATION (application));
981 /* Call this here, since gtk_init() is only getting called in startup() */
982 _gtk_accel_map_init ();
984 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
988 g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
992 accel_path = _gtk_accel_path_for_action (action_name, parameter);
994 if (gtk_accel_map_lookup_entry (accel_path, NULL))
995 gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
997 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
1003 * gtk_application_remove_accelerator:
1004 * @application: a #GtkApplication
1005 * @action_name: the name of the action to activate
1006 * @parameter: (allow-none): parameter to pass when activating the action,
1007 * or %NULL if the action does not accept an activation parameter
1009 * Removes an accelerator that has been previously added
1010 * with gtk_application_add_accelerator().
1015 gtk_application_remove_accelerator (GtkApplication *application,
1016 const gchar *action_name,
1017 GVariant *parameter)
1021 g_return_if_fail (GTK_IS_APPLICATION (application));
1023 accel_path = _gtk_accel_path_for_action (action_name, parameter);
1025 if (!gtk_accel_map_lookup_entry (accel_path, NULL))
1027 g_warning ("No accelerator found for '%s'\n", accel_path);
1028 g_free (accel_path);
1032 gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
1033 g_free (accel_path);
1037 * gtk_application_set_app_menu:
1038 * @application: a #GtkApplication
1039 * @app_menu: (allow-none): a #GMenuModel, or %NULL
1041 * Sets or unsets the application menu for @application.
1043 * This can only be done in the primary instance of the application,
1044 * after it has been registered. #GApplication:startup is a good place
1047 * The application menu is a single menu containing items that typically
1048 * impact the application as a whole, rather than acting on a specific
1049 * window or document. For example, you would expect to see
1050 * "Preferences" or "Quit" in an application menu, but not "Save" or
1053 * If supported, the application menu will be rendered by the desktop
1056 * Use the base #GActionMap interface to add actions, to respond to the user
1057 * selecting these menu items.
1062 gtk_application_set_app_menu (GtkApplication *application,
1063 GMenuModel *app_menu)
1065 g_return_if_fail (GTK_IS_APPLICATION (application));
1066 g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1067 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1069 if (app_menu != application->priv->app_menu)
1071 if (application->priv->app_menu != NULL)
1072 g_object_unref (application->priv->app_menu);
1074 application->priv->app_menu = app_menu;
1076 if (application->priv->app_menu != NULL)
1077 g_object_ref (application->priv->app_menu);
1080 extract_accels_from_menu (app_menu, application);
1082 #ifdef GDK_WINDOWING_X11
1083 gtk_application_set_app_menu_x11 (application, app_menu);
1086 g_object_notify (G_OBJECT (application), "app-menu");
1091 * gtk_application_get_app_menu:
1092 * @application: a #GtkApplication
1094 * Returns the menu model that has been set with
1095 * gtk_application_set_app_menu().
1097 * Returns: (transfer none): the application menu of @application
1102 gtk_application_get_app_menu (GtkApplication *application)
1104 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1106 return application->priv->app_menu;
1110 * gtk_application_set_menubar:
1111 * @application: a #GtkApplication
1112 * @menubar: (allow-none): a #GMenuModel, or %NULL
1114 * Sets or unsets the menubar for windows of @application.
1116 * This is a menubar in the traditional sense.
1118 * This can only be done in the primary instance of the application,
1119 * after it has been registered. #GApplication:startup is a good place
1122 * Depending on the desktop environment, this may appear at the top of
1123 * each window, or at the top of the screen. In some environments, if
1124 * both the application menu and the menubar are set, the application
1125 * menu will be presented as if it were the first item of the menubar.
1126 * Other environments treat the two as completely separate -- for
1127 * example, the application menu may be rendered by the desktop shell
1128 * while the menubar (if set) remains in each individual window.
1130 * Use the base #GActionMap interface to add actions, to respond to the user
1131 * selecting these menu items.
1136 gtk_application_set_menubar (GtkApplication *application,
1137 GMenuModel *menubar)
1139 g_return_if_fail (GTK_IS_APPLICATION (application));
1140 g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1141 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1143 if (menubar != application->priv->menubar)
1145 if (application->priv->menubar != NULL)
1146 g_object_unref (application->priv->menubar);
1148 application->priv->menubar = menubar;
1150 if (application->priv->menubar != NULL)
1151 g_object_ref (application->priv->menubar);
1154 extract_accels_from_menu (menubar, application);
1156 #ifdef GDK_WINDOWING_X11
1157 gtk_application_set_menubar_x11 (application, menubar);
1160 g_object_notify (G_OBJECT (application), "menubar");
1165 * gtk_application_get_menubar:
1166 * @application: a #GtkApplication
1168 * Returns the menu model that has been set with
1169 * gtk_application_set_menubar().
1171 * Returns: (transfer none): the menubar for windows of @application
1176 gtk_application_get_menubar (GtkApplication *application)
1178 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1180 return application->priv->menubar;
1183 #if defined(GDK_WINDOWING_X11)
1185 /* D-Bus Session Management
1187 * The protocol and the D-Bus API are described here:
1188 * http://live.gnome.org/SessionManagement/GnomeSession
1189 * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1193 unregister_client (GtkApplication *app)
1195 GError *error = NULL;
1197 g_debug ("Unregistering client");
1199 g_dbus_proxy_call_sync (app->priv->sm_proxy,
1201 g_variant_new ("(o)", app->priv->client_path),
1202 G_DBUS_CALL_FLAGS_NONE,
1209 g_warning ("Failed to unregister client: %s", error->message);
1210 g_error_free (error);
1213 g_clear_object (&app->priv->client_proxy);
1215 g_free (app->priv->client_path);
1216 app->priv->client_path = NULL;
1220 gtk_application_quit_response (GtkApplication *application,
1222 const gchar *reason)
1224 g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1226 g_dbus_proxy_call (application->priv->client_proxy,
1227 "EndSessionResponse",
1228 g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1229 G_DBUS_CALL_FLAGS_NONE,
1234 client_proxy_signal (GDBusProxy *proxy,
1235 const gchar *sender_name,
1236 const gchar *signal_name,
1237 GVariant *parameters,
1238 GtkApplication *app)
1240 if (strcmp (signal_name, "QueryEndSession") == 0)
1242 g_debug ("Received QueryEndSession");
1243 gtk_application_quit_response (app, TRUE, NULL);
1245 else if (strcmp (signal_name, "CancelEndSession") == 0)
1247 g_debug ("Received CancelEndSession");
1249 else if (strcmp (signal_name, "EndSession") == 0)
1251 g_debug ("Received EndSession");
1252 gtk_application_quit_response (app, TRUE, NULL);
1253 unregister_client (app);
1254 g_application_quit (G_APPLICATION (app));
1256 else if (strcmp (signal_name, "Stop") == 0)
1258 g_debug ("Received Stop");
1259 unregister_client (app);
1260 g_application_quit (G_APPLICATION (app));
1265 gtk_application_startup_session_dbus (GtkApplication *app)
1267 static gchar *client_id;
1268 GError *error = NULL;
1271 if (app->priv->session_bus == NULL)
1274 if (client_id == NULL)
1276 const gchar *desktop_autostart_id;
1278 desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1279 /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1280 * use the same client id.
1282 g_unsetenv ("DESKTOP_AUTOSTART_ID");
1283 client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1286 g_debug ("Connecting to session manager");
1288 app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1289 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1290 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1292 "org.gnome.SessionManager",
1293 "/org/gnome/SessionManager",
1294 "org.gnome.SessionManager",
1299 g_warning ("Failed to get a session proxy: %s", error->message);
1300 g_error_free (error);
1304 /* FIXME: should we reuse the D-Bus application id here ? */
1305 app->priv->app_id = g_strdup (g_get_prgname ());
1307 if (!app->priv->register_session)
1310 g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1312 res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1314 g_variant_new ("(ss)", app->priv->app_id, client_id),
1315 G_DBUS_CALL_FLAGS_NONE,
1322 g_warning ("Failed to register client: %s", error->message);
1323 g_error_free (error);
1324 g_clear_object (&app->priv->sm_proxy);
1328 g_variant_get (res, "(o)", &app->priv->client_path);
1329 g_variant_unref (res);
1331 g_debug ("Registered client at '%s'", app->priv->client_path);
1333 app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1335 "org.gnome.SessionManager",
1336 app->priv->client_path,
1337 "org.gnome.SessionManager.ClientPrivate",
1342 g_warning ("Failed to get client proxy: %s", error->message);
1343 g_error_free (error);
1344 g_clear_object (&app->priv->sm_proxy);
1345 g_free (app->priv->client_path);
1346 app->priv->client_path = NULL;
1350 g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1356 * GtkApplicationInhibitFlags:
1357 * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit ending the user session
1358 * by logging out or by shutting down the computer
1359 * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1360 * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1361 * session or computer
1362 * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1363 * marked as idle (and possibly locked)
1365 * Types of user actions that may be blocked by gtk_application_inhibit().
1371 * gtk_application_inhibit:
1372 * @application: the #GApplication
1373 * @window: (allow-none): a #GtkWindow, or %NULL
1374 * @flags: what types of actions should be inhibited
1375 * @reason: (allow-none): a short, human-readable string that explains
1376 * why these operations are inhibited
1378 * Inform the session manager that certain types of actions should be
1379 * inhibited. This is not guaranteed to work on all platforms and for
1380 * all types of actions.
1382 * Applications should invoke this method when they begin an operation
1383 * that should not be interrupted, such as creating a CD or DVD. The
1384 * types of actions that may be blocked are specified by the @flags
1385 * parameter. When the application completes the operation it should
1386 * call gtk_application_uninhibit() to remove the inhibitor. Note that
1387 * an application can have multiple inhibitors, and all of the must
1388 * be individually removed. Inhibitors are also cleared when the
1389 * application exits.
1391 * Applications should not expect that they will always be able to block
1392 * the action. In most cases, users will be given the option to force
1393 * the action to take place.
1395 * Reasons should be short and to the point.
1397 * If @window is given, the session manager may point the user to
1398 * this window to find out more about why the action is inhibited.
1400 * Returns: A non-zero cookie that is used to uniquely identify this
1401 * request. It should be used as an argument to gtk_application_uninhibit()
1402 * in order to remove the request. If the platform does not support
1403 * inhibiting or the request failed for some reason, 0 is returned.
1408 gtk_application_inhibit (GtkApplication *application,
1410 GtkApplicationInhibitFlags flags,
1411 const gchar *reason)
1414 GError *error = NULL;
1418 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1419 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1420 g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1423 xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1427 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1429 g_variant_new ("(susu)",
1430 application->priv->app_id,
1434 G_DBUS_CALL_FLAGS_NONE,
1440 g_warning ("Calling Inhibit failed: %s", error->message);
1441 g_error_free (error);
1445 g_variant_get (res, "(u)", &cookie);
1446 g_variant_unref (res);
1452 * gtk_application_uninhibit:
1453 * @application: the #GApplication
1454 * @cookie: a cookie that was returned by gtk_application_inhibit()
1456 * Removes an inhibitor that has been established with gtk_application_inhibit().
1457 * Inhibitors are also cleared when the application exits.
1462 gtk_application_uninhibit (GtkApplication *application,
1465 g_return_if_fail (GTK_IS_APPLICATION (application));
1466 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1467 g_return_if_fail (application->priv->sm_proxy != NULL);
1469 g_dbus_proxy_call (application->priv->sm_proxy,
1471 g_variant_new ("(u)", cookie),
1472 G_DBUS_CALL_FLAGS_NONE,
1478 * gtk_application_is_inhibited:
1479 * @application: the #GApplication
1480 * @flags: what types of actions should be queried
1482 * Determines if any of the actions specified in @flags are
1483 * currently inhibited (possibly by another application).
1485 * Returns: %TRUE if any of the actions specified in @flags are inhibited
1490 gtk_application_is_inhibited (GtkApplication *application,
1491 GtkApplicationInhibitFlags flags)
1494 GError *error = NULL;
1497 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1498 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1499 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1501 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1503 g_variant_new ("(u)", flags),
1504 G_DBUS_CALL_FLAGS_NONE,
1510 g_warning ("Calling IsInhibited failed: %s", error->message);
1511 g_error_free (error);
1515 g_variant_get (res, "(b)", &inhibited);
1516 g_variant_unref (res);
1521 #elif defined(GDK_WINDOWING_QUARTZ)
1523 /* OS X implementation copied from EggSMClient, but simplified since
1524 * it doesn't need to interact with the user.
1528 idle_will_quit (gpointer data)
1530 GtkApplication *app = data;
1532 if (app->priv->quit_inhibit == 0)
1533 g_application_quit (G_APPLICATION (app));
1536 GtkApplicationQuartzInhibitor *inhibitor;
1540 for (iter = app->priv->inhibitors; iter; iter = iter->next)
1542 inhibitor = iter->data;
1543 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1546 g_assert (inhibitor != NULL);
1548 dialog = gtk_message_dialog_new (inhibitor->window,
1552 _("%s cannot quit at this time:\n\n%s"),
1553 g_get_application_name (),
1555 g_signal_connect_swapped (dialog,
1557 G_CALLBACK (gtk_widget_destroy),
1559 gtk_widget_show_all (dialog);
1562 return G_SOURCE_REMOVE;
1566 quit_requested (const AppleEvent *aevt,
1570 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1572 /* Don't emit the "quit" signal immediately, since we're
1573 * called from a weird point in the guts of gdkeventloop-quartz.c
1575 g_idle_add_full (G_PRIORITY_DEFAULT, idle_will_quit, app, NULL);
1577 return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
1581 gtk_application_startup_session_quartz (GtkApplication *app)
1583 if (app->priv->register_session)
1584 AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1585 NewAEEventHandlerUPP (quit_requested),
1586 (long)GPOINTER_TO_SIZE (app), false);
1590 gtk_application_inhibit (GtkApplication *application,
1592 GtkApplicationInhibitFlags flags,
1593 const gchar *reason)
1595 GtkApplicationQuartzInhibitor *inhibitor;
1597 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1598 g_return_val_if_fail (flags != 0, 0);
1600 inhibitor = g_slice_new (GtkApplicationQuartzInhibitor);
1601 inhibitor->cookie = ++application->priv->next_cookie;
1602 inhibitor->flags = flags;
1603 inhibitor->reason = g_strdup (reason);
1604 inhibitor->window = window ? g_object_ref (window) : NULL;
1606 application->priv->inhibitors = g_slist_prepend (application->priv->inhibitors, inhibitor);
1608 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1609 application->priv->quit_inhibit++;
1611 return inhibitor->cookie;
1615 gtk_application_uninhibit (GtkApplication *application,
1620 for (iter = application->priv->inhibitors; iter; iter = iter->next)
1622 GtkApplicationQuartzInhibitor *inhibitor = iter->data;
1624 if (inhibitor->cookie == cookie)
1626 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1627 application->priv->quit_inhibit--;
1628 gtk_application_quartz_inhibitor_free (inhibitor);
1629 application->priv->inhibitors = g_slist_delete_link (application->priv->inhibitors, iter);
1634 g_warning ("Invalid inhibitor cookie");
1638 gtk_application_is_inhibited (GtkApplication *application,
1639 GtkApplicationInhibitFlags flags)
1641 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1642 return application->priv->quit_inhibit > 0;
1649 /* Trivial implementation.
1651 * For the inhibit API on Windows, see
1652 * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1656 gtk_application_inhibit (GtkApplication *application,
1658 GtkApplicationInhibitFlags flags,
1659 const gchar *reason)
1665 gtk_application_uninhibit (GtkApplication *application,
1671 gtk_application_is_inhibited (GtkApplication *application,
1672 GtkApplicationInhibitFlags flags)