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 "gtkmodelmenu-quartz.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 * #GtkApplicationWindow<!-- -->s, 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 g_free (application->priv->app_menu_path);
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 g_free (application->priv->menubar_path);
232 gtk_application_x11_publish_menu (application, "menubar", menubar,
233 &application->priv->menubar_id,
234 &application->priv->menubar_path);
238 gtk_application_window_added_x11 (GtkApplication *application,
241 if (application->priv->session_bus == NULL)
244 if (GTK_IS_APPLICATION_WINDOW (window))
246 GtkApplicationWindow *app_window = GTK_APPLICATION_WINDOW (window);
249 /* GtkApplicationWindow associates with us when it is first created,
250 * so surely it's not realized yet...
252 g_assert (!gtk_widget_get_realized (GTK_WIDGET (window)));
259 window_id = application->priv->next_id++;
260 window_path = g_strdup_printf ("%s/window/%u", application->priv->object_path, window_id);
261 success = gtk_application_window_publish (app_window, application->priv->session_bus, window_path, window_id);
262 g_free (window_path);
269 gtk_application_window_removed_x11 (GtkApplication *application,
272 if (application->priv->session_bus == NULL)
275 if (GTK_IS_APPLICATION_WINDOW (window))
276 gtk_application_window_unpublish (GTK_APPLICATION_WINDOW (window));
279 static void gtk_application_startup_session_dbus (GtkApplication *app);
282 gtk_application_startup_x11 (GtkApplication *application)
284 application->priv->session_bus = g_application_get_dbus_connection (G_APPLICATION (application));
285 application->priv->object_path = g_application_get_dbus_object_path (G_APPLICATION (application));
287 gtk_application_startup_session_dbus (GTK_APPLICATION (application));
291 gtk_application_shutdown_x11 (GtkApplication *application)
293 application->priv->session_bus = NULL;
294 application->priv->object_path = NULL;
296 g_clear_object (&application->priv->sm_proxy);
297 g_clear_object (&application->priv->client_proxy);
298 g_free (application->priv->app_id);
299 g_free (application->priv->client_path);
301 g_free (application->priv->app_menu_path);
302 g_free (application->priv->menubar_path);
306 gtk_application_get_app_menu_object_path (GtkApplication *application)
308 return application->priv->app_menu_path;
312 gtk_application_get_menubar_object_path (GtkApplication *application)
314 return application->priv->menubar_path;
319 #ifdef GDK_WINDOWING_QUARTZ
323 GtkApplicationInhibitFlags flags;
326 } GtkApplicationQuartzInhibitor;
329 gtk_application_quartz_inhibitor_free (GtkApplicationQuartzInhibitor *inhibitor)
331 g_free (inhibitor->reason);
332 g_clear_object (&inhibitor->window);
333 g_slice_free (GtkApplicationQuartzInhibitor, inhibitor);
337 gtk_application_menu_changed_quartz (GObject *object,
341 GtkApplication *application = GTK_APPLICATION (object);
344 combined = g_menu_new ();
345 g_menu_append_submenu (combined, "Application", gtk_application_get_app_menu (application));
346 g_menu_append_section (combined, NULL, gtk_application_get_menubar (application));
348 gtk_quartz_set_main_menu (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (application->priv->muxer));
351 static void gtk_application_startup_session_quartz (GtkApplication *app);
354 gtk_application_startup_quartz (GtkApplication *application)
356 [NSApp finishLaunching];
358 application->priv->muxer = g_action_muxer_new ();
359 g_action_muxer_insert (application->priv->muxer, "app", G_ACTION_GROUP (application));
361 g_signal_connect (application, "notify::app-menu", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
362 g_signal_connect (application, "notify::menubar", G_CALLBACK (gtk_application_menu_changed_quartz), NULL);
363 gtk_application_menu_changed_quartz (G_OBJECT (application), NULL, NULL);
365 gtk_application_startup_session_quartz (application);
369 gtk_application_shutdown_quartz (GtkApplication *application)
371 g_signal_handlers_disconnect_by_func (application, gtk_application_menu_changed_quartz, NULL);
373 g_object_unref (application->priv->muxer);
374 application->priv->muxer = NULL;
376 g_slist_free_full (application->priv->inhibitors,
377 (GDestroyNotify) gtk_application_quartz_inhibitor_free);
378 application->priv->inhibitors = NULL;
382 gtk_application_focus_changed (GtkApplication *application,
385 if (G_IS_ACTION_GROUP (window))
386 g_action_muxer_insert (application->priv->muxer, "win", G_ACTION_GROUP (window));
388 g_action_muxer_remove (application->priv->muxer, "win");
393 gtk_application_focus_in_event_cb (GtkWindow *window,
394 GdkEventFocus *event,
395 GtkApplication *application)
397 GtkApplicationPrivate *priv = application->priv;
400 /* Keep the window list sorted by most-recently-focused. */
401 link = g_list_find (priv->windows, window);
402 if (link != NULL && link != priv->windows)
404 priv->windows = g_list_remove_link (priv->windows, link);
405 priv->windows = g_list_concat (link, priv->windows);
408 #ifdef GDK_WINDOWING_QUARTZ
409 gtk_application_focus_changed (application, window);
416 gtk_application_startup (GApplication *application)
418 G_APPLICATION_CLASS (gtk_application_parent_class)
419 ->startup (application);
423 #ifdef GDK_WINDOWING_X11
424 gtk_application_startup_x11 (GTK_APPLICATION (application));
427 #ifdef GDK_WINDOWING_QUARTZ
428 gtk_application_startup_quartz (GTK_APPLICATION (application));
433 gtk_application_shutdown (GApplication *application)
435 #ifdef GDK_WINDOWING_X11
436 gtk_application_shutdown_x11 (GTK_APPLICATION (application));
439 #ifdef GDK_WINDOWING_QUARTZ
440 gtk_application_shutdown_quartz (GTK_APPLICATION (application));
443 /* Try storing all clipboard data we have */
444 _gtk_clipboard_store_all ();
446 /* Synchronize the recent manager singleton */
447 _gtk_recent_manager_sync ();
449 G_APPLICATION_CLASS (gtk_application_parent_class)
450 ->shutdown (application);
454 gtk_application_add_platform_data (GApplication *application,
455 GVariantBuilder *builder)
457 const gchar *startup_id;
459 startup_id = getenv ("DESKTOP_STARTUP_ID");
461 if (startup_id && g_utf8_validate (startup_id, -1, NULL))
462 g_variant_builder_add (builder, "{sv}", "desktop-startup-id",
463 g_variant_new_string (startup_id));
467 gtk_application_before_emit (GApplication *application,
468 GVariant *platform_data)
474 gdk_threads_enter ();
476 g_variant_iter_init (&iter, platform_data);
477 while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
479 #ifdef GDK_WINDOWING_X11
480 if (strcmp (key, "desktop-startup-id") == 0)
485 display = gdk_display_get_default ();
486 id = g_variant_get_string (value, NULL);
487 if (GDK_IS_X11_DISPLAY (display))
488 gdk_x11_display_set_startup_notification_id (display, id);
495 gtk_application_after_emit (GApplication *application,
496 GVariant *platform_data)
498 gdk_notify_startup_complete ();
500 gdk_threads_leave ();
504 gtk_application_init (GtkApplication *application)
506 application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application,
507 GTK_TYPE_APPLICATION,
508 GtkApplicationPrivate);
510 #ifdef GDK_WINDOWING_X11
511 application->priv->next_id = 1;
516 gtk_application_window_added (GtkApplication *application,
519 GtkApplicationPrivate *priv = application->priv;
521 priv->windows = g_list_prepend (priv->windows, window);
522 gtk_window_set_application (window, application);
523 g_application_hold (G_APPLICATION (application));
525 g_signal_connect (window, "focus-in-event",
526 G_CALLBACK (gtk_application_focus_in_event_cb),
529 #ifdef GDK_WINDOWING_X11
530 gtk_application_window_added_x11 (application, window);
535 gtk_application_window_removed (GtkApplication *application,
538 GtkApplicationPrivate *priv = application->priv;
540 #ifdef GDK_WINDOWING_X11
541 gtk_application_window_removed_x11 (application, window);
544 g_signal_handlers_disconnect_by_func (window,
545 gtk_application_focus_in_event_cb,
548 g_application_release (G_APPLICATION (application));
549 priv->windows = g_list_remove (priv->windows, window);
550 gtk_window_set_application (window, NULL);
554 extract_accel_from_menu_item (GMenuModel *model,
558 GMenuAttributeIter *iter;
561 const gchar *accel = NULL;
562 const gchar *action = NULL;
563 GVariant *target = NULL;
565 iter = g_menu_model_iterate_item_attributes (model, item);
566 while (g_menu_attribute_iter_get_next (iter, &key, &value))
568 if (g_str_equal (key, "action") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
569 action = g_variant_get_string (value, NULL);
570 else if (g_str_equal (key, "accel") && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
571 accel = g_variant_get_string (value, NULL);
572 else if (g_str_equal (key, "target"))
573 target = g_variant_ref (value);
574 g_variant_unref (value);
576 g_object_unref (iter);
579 gtk_application_add_accelerator (app, accel, action, target);
582 g_variant_unref (target);
586 extract_accels_from_menu (GMenuModel *model,
594 for (i = 0; i < g_menu_model_get_n_items (model); i++)
596 extract_accel_from_menu_item (model, i, app);
598 iter = g_menu_model_iterate_item_links (model, i);
599 while (g_menu_link_iter_get_next (iter, &key, &m))
601 extract_accels_from_menu (m, app);
604 g_object_unref (iter);
609 gtk_application_get_property (GObject *object,
614 GtkApplication *application = GTK_APPLICATION (object);
618 case PROP_REGISTER_SESSION:
619 g_value_set_boolean (value, application->priv->register_session);
623 g_value_set_object (value, gtk_application_get_app_menu (application));
627 g_value_set_object (value, gtk_application_get_menubar (application));
631 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
637 gtk_application_set_property (GObject *object,
642 GtkApplication *application = GTK_APPLICATION (object);
646 case PROP_REGISTER_SESSION:
647 application->priv->register_session = g_value_get_boolean (value);
651 gtk_application_set_app_menu (application, g_value_get_object (value));
655 gtk_application_set_menubar (application, g_value_get_object (value));
659 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
665 gtk_application_finalize (GObject *object)
667 GtkApplication *application = GTK_APPLICATION (object);
669 g_clear_object (&application->priv->app_menu);
670 g_clear_object (&application->priv->menubar);
672 G_OBJECT_CLASS (gtk_application_parent_class)
677 gtk_application_class_init (GtkApplicationClass *class)
679 GObjectClass *object_class = G_OBJECT_CLASS (class);
680 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
682 object_class->get_property = gtk_application_get_property;
683 object_class->set_property = gtk_application_set_property;
684 object_class->finalize = gtk_application_finalize;
686 application_class->add_platform_data = gtk_application_add_platform_data;
687 application_class->before_emit = gtk_application_before_emit;
688 application_class->after_emit = gtk_application_after_emit;
689 application_class->startup = gtk_application_startup;
690 application_class->shutdown = gtk_application_shutdown;
692 class->window_added = gtk_application_window_added;
693 class->window_removed = gtk_application_window_removed;
695 g_type_class_add_private (class, sizeof (GtkApplicationPrivate));
698 * GtkApplication::window-added:
699 * @application: the #GtkApplication which emitted the signal
700 * @window: the newly-added #GtkWindow
702 * Emitted when a #GtkWindow is added to @application through
703 * gtk_application_add_window().
707 gtk_application_signals[WINDOW_ADDED] =
708 g_signal_new ("window-added", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
709 G_STRUCT_OFFSET (GtkApplicationClass, window_added),
711 g_cclosure_marshal_VOID__OBJECT,
712 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
715 * GtkApplication::window-removed:
716 * @application: the #GtkApplication which emitted the signal
717 * @window: the #GtkWindow that is being removed
719 * Emitted when a #GtkWindow is removed from @application,
720 * either as a side-effect of being destroyed or explicitly
721 * through gtk_application_remove_window().
725 gtk_application_signals[WINDOW_REMOVED] =
726 g_signal_new ("window-removed", GTK_TYPE_APPLICATION, G_SIGNAL_RUN_FIRST,
727 G_STRUCT_OFFSET (GtkApplicationClass, window_removed),
729 g_cclosure_marshal_VOID__OBJECT,
730 G_TYPE_NONE, 1, GTK_TYPE_WINDOW);
733 * GtkApplication:register-session:
735 * Set this property to %TRUE to register with the session manager.
739 g_object_class_install_property (object_class, PROP_REGISTER_SESSION,
740 g_param_spec_boolean ("register-session",
741 P_("Register session"),
742 P_("Register with the session manager"),
743 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
745 g_object_class_install_property (object_class, PROP_APP_MENU,
746 g_param_spec_object ("app-menu",
747 P_("Application menu"),
748 P_("The GMenuModel for the application menu"),
750 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
752 g_object_class_install_property (object_class, PROP_MENUBAR,
753 g_param_spec_object ("menubar",
755 P_("The GMenuModel for the menubar"),
757 G_PARAM_READWRITE | 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_add_accelerator:
920 * @application: a #GtkApplication
921 * @accelerator: accelerator string
922 * @action_name: the name of the action to activate
923 * @parameter: (allow-none): parameter to pass when activating the action,
924 * or %NULL if the action does not accept an activation parameter
926 * Installs an accelerator that will cause the named action
927 * to be activated when the key combination specificed by @accelerator
930 * @accelerator must be a string that can be parsed by
931 * gtk_accelerator_parse(), e.g. "<Primary>q" or
932 * "<Control><Alt>p".
934 * @action_name must be the name of an action as it would be used
935 * in the app menu, i.e. actions that have been added to the application
936 * are referred to with an "app." prefix, and window-specific actions
937 * with a "win." prefix.
939 * GtkApplication also extracts accelerators out of 'accel' attributes
940 * in the #GMenuModels passed to gtk_application_set_app_menu() and
941 * gtk_application_set_menubar(), which is usually more convenient
942 * than calling this function for each accelerator.
947 gtk_application_add_accelerator (GtkApplication *application,
948 const gchar *accelerator,
949 const gchar *action_name,
954 GdkModifierType accel_mods;
956 g_return_if_fail (GTK_IS_APPLICATION (application));
958 /* Call this here, since gtk_init() is only getting called in startup() */
959 _gtk_accel_map_init ();
961 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
965 g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
969 accel_path = _gtk_accel_path_for_action (action_name, parameter);
971 if (gtk_accel_map_lookup_entry (accel_path, NULL))
972 gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
974 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
980 * gtk_application_remove_accelerator:
981 * @application: a #GtkApplication
982 * @action_name: the name of the action to activate
983 * @parameter: (allow-none): parameter to pass when activating the action,
984 * or %NULL if the action does not accept an activation parameter
986 * Removes an accelerator that has been previously added
987 * with gtk_application_add_accelerator().
992 gtk_application_remove_accelerator (GtkApplication *application,
993 const gchar *action_name,
998 g_return_if_fail (GTK_IS_APPLICATION (application));
1000 accel_path = _gtk_accel_path_for_action (action_name, parameter);
1002 if (!gtk_accel_map_lookup_entry (accel_path, NULL))
1004 g_warning ("No accelerator found for '%s'\n", accel_path);
1005 g_free (accel_path);
1009 gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
1010 g_free (accel_path);
1014 * gtk_application_set_app_menu:
1015 * @application: a #GtkApplication
1016 * @app_menu: (allow-none): a #GMenuModel, or %NULL
1018 * Sets or unsets the application menu for @application.
1020 * This can only be done in the primary instance of the application,
1021 * after it has been registered. #GApplication:startup is a good place
1024 * The application menu is a single menu containing items that typically
1025 * impact the application as a whole, rather than acting on a specific
1026 * window or document. For example, you would expect to see
1027 * "Preferences" or "Quit" in an application menu, but not "Save" or
1030 * If supported, the application menu will be rendered by the desktop
1033 * Use the base #GActionMap interface to add actions, to respond to the user
1034 * selecting these menu items.
1039 gtk_application_set_app_menu (GtkApplication *application,
1040 GMenuModel *app_menu)
1042 g_return_if_fail (GTK_IS_APPLICATION (application));
1043 g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1044 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1046 if (app_menu != application->priv->app_menu)
1048 if (application->priv->app_menu != NULL)
1049 g_object_unref (application->priv->app_menu);
1051 application->priv->app_menu = app_menu;
1053 if (application->priv->app_menu != NULL)
1054 g_object_ref (application->priv->app_menu);
1056 extract_accels_from_menu (app_menu, application);
1058 #ifdef GDK_WINDOWING_X11
1059 gtk_application_set_app_menu_x11 (application, app_menu);
1062 g_object_notify (G_OBJECT (application), "app-menu");
1067 * gtk_application_get_app_menu:
1068 * @application: a #GtkApplication
1070 * Returns the menu model that has been set with
1071 * gtk_application_set_app_menu().
1073 * Returns: (transfer none): the application menu of @application
1078 gtk_application_get_app_menu (GtkApplication *application)
1080 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1082 return application->priv->app_menu;
1086 * gtk_application_set_menubar:
1087 * @application: a #GtkApplication
1088 * @menubar: (allow-none): a #GMenuModel, or %NULL
1090 * Sets or unsets the menubar for windows of @application.
1092 * This is a menubar in the traditional sense.
1094 * This can only be done in the primary instance of the application,
1095 * after it has been registered. #GApplication:startup is a good place
1098 * Depending on the desktop environment, this may appear at the top of
1099 * each window, or at the top of the screen. In some environments, if
1100 * both the application menu and the menubar are set, the application
1101 * menu will be presented as if it were the first item of the menubar.
1102 * Other environments treat the two as completely separate -- for
1103 * example, the application menu may be rendered by the desktop shell
1104 * while the menubar (if set) remains in each individual window.
1106 * Use the base #GActionMap interface to add actions, to respond to the user
1107 * selecting these menu items.
1112 gtk_application_set_menubar (GtkApplication *application,
1113 GMenuModel *menubar)
1115 g_return_if_fail (GTK_IS_APPLICATION (application));
1116 g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1117 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1119 if (menubar != application->priv->menubar)
1121 if (application->priv->menubar != NULL)
1122 g_object_unref (application->priv->menubar);
1124 application->priv->menubar = menubar;
1126 if (application->priv->menubar != NULL)
1127 g_object_ref (application->priv->menubar);
1129 extract_accels_from_menu (menubar, application);
1131 #ifdef GDK_WINDOWING_X11
1132 gtk_application_set_menubar_x11 (application, menubar);
1135 g_object_notify (G_OBJECT (application), "menubar");
1140 * gtk_application_get_menubar:
1141 * @application: a #GtkApplication
1143 * Returns the menu model that has been set with
1144 * gtk_application_set_menubar().
1146 * Returns: (transfer none): the menubar for windows of @application
1151 gtk_application_get_menubar (GtkApplication *application)
1153 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1155 return application->priv->menubar;
1158 #if defined(GDK_WINDOWING_X11)
1160 /* D-Bus Session Management
1162 * The protocol and the D-Bus API are described here:
1163 * http://live.gnome.org/SessionManagement/GnomeSession
1164 * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1168 unregister_client (GtkApplication *app)
1170 GError *error = NULL;
1172 g_debug ("Unregistering client");
1174 g_dbus_proxy_call_sync (app->priv->sm_proxy,
1176 g_variant_new ("(o)", app->priv->client_path),
1177 G_DBUS_CALL_FLAGS_NONE,
1184 g_warning ("Failed to unregister client: %s", error->message);
1185 g_error_free (error);
1188 g_clear_object (&app->priv->client_proxy);
1190 g_free (app->priv->client_path);
1191 app->priv->client_path = NULL;
1195 gtk_application_quit_response (GtkApplication *application,
1197 const gchar *reason)
1199 g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1201 g_dbus_proxy_call (application->priv->client_proxy,
1202 "EndSessionResponse",
1203 g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1204 G_DBUS_CALL_FLAGS_NONE,
1209 client_proxy_signal (GDBusProxy *proxy,
1210 const gchar *sender_name,
1211 const gchar *signal_name,
1212 GVariant *parameters,
1213 GtkApplication *app)
1215 if (strcmp (signal_name, "QueryEndSession") == 0)
1217 g_debug ("Received QueryEndSession");
1218 gtk_application_quit_response (app, TRUE, NULL);
1220 else if (strcmp (signal_name, "CancelEndSession") == 0)
1222 g_debug ("Received CancelEndSession");
1224 else if (strcmp (signal_name, "EndSession") == 0)
1226 g_debug ("Received EndSession");
1227 gtk_application_quit_response (app, TRUE, NULL);
1228 unregister_client (app);
1229 g_application_quit (G_APPLICATION (app));
1231 else if (strcmp (signal_name, "Stop") == 0)
1233 g_debug ("Received Stop");
1234 unregister_client (app);
1235 g_application_quit (G_APPLICATION (app));
1240 gtk_application_startup_session_dbus (GtkApplication *app)
1242 static gchar *client_id;
1243 GError *error = NULL;
1246 if (app->priv->session_bus == NULL)
1249 if (client_id == NULL)
1251 const gchar *desktop_autostart_id;
1253 desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1254 /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1255 * use the same client id.
1257 g_unsetenv ("DESKTOP_AUTOSTART_ID");
1258 client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1261 g_debug ("Connecting to session manager");
1263 app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1264 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1265 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1267 "org.gnome.SessionManager",
1268 "/org/gnome/SessionManager",
1269 "org.gnome.SessionManager",
1274 g_warning ("Failed to get a session proxy: %s", error->message);
1275 g_error_free (error);
1279 /* FIXME: should we reuse the D-Bus application id here ? */
1280 app->priv->app_id = g_strdup (g_get_prgname ());
1282 if (!app->priv->register_session)
1285 g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1287 res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1289 g_variant_new ("(ss)", app->priv->app_id, client_id),
1290 G_DBUS_CALL_FLAGS_NONE,
1297 g_warning ("Failed to register client: %s", error->message);
1298 g_error_free (error);
1299 g_clear_object (&app->priv->sm_proxy);
1303 g_variant_get (res, "(o)", &app->priv->client_path);
1304 g_variant_unref (res);
1306 g_debug ("Registered client at '%s'", app->priv->client_path);
1308 app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1310 "org.gnome.SessionManager",
1311 app->priv->client_path,
1312 "org.gnome.SessionManager.ClientPrivate",
1317 g_warning ("Failed to get client proxy: %s", error->message);
1318 g_error_free (error);
1319 g_clear_object (&app->priv->sm_proxy);
1320 g_free (app->priv->client_path);
1321 app->priv->client_path = NULL;
1325 g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1331 * GtkApplicationInhibitFlags:
1332 * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit ending the user session
1333 * by logging out or by shutting down the computer
1334 * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1335 * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1336 * session or computer
1337 * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1338 * marked as idle (and possibly locked)
1340 * Types of user actions that may be blocked by gtk_application_inhibit().
1346 * gtk_application_inhibit:
1347 * @application: the #GApplication
1348 * @window: (allow-none): a #GtkWindow, or %NULL
1349 * @flags: what types of actions should be inhibited
1350 * @reason: (allow-none): a short, human-readable string that explains
1351 * why these operations are inhibited
1353 * Inform the session manager that certain types of actions should be
1354 * inhibited. This is not guaranteed to work on all platforms and for
1355 * all types of actions.
1357 * Applications should invoke this method when they begin an operation
1358 * that should not be interrupted, such as creating a CD or DVD. The
1359 * types of actions that may be blocked are specified by the @flags
1360 * parameter. When the application completes the operation it should
1361 * call gtk_application_uninhibit() to remove the inhibitor. Note that
1362 * an application can have multiple inhibitors, and all of the must
1363 * be individually removed. Inhibitors are also cleared when the
1364 * application exits.
1366 * Applications should not expect that they will always be able to block
1367 * the action. In most cases, users will be given the option to force
1368 * the action to take place.
1370 * Reasons should be short and to the point.
1372 * If @window is given, the session manager may point the user to
1373 * this window to find out more about why the action is inhibited.
1375 * Returns: A non-zero cookie that is used to uniquely identify this
1376 * request. It should be used as an argument to gtk_application_uninhibit()
1377 * in order to remove the request. If the platform does not support
1378 * inhibiting or the request failed for some reason, 0 is returned.
1383 gtk_application_inhibit (GtkApplication *application,
1385 GtkApplicationInhibitFlags flags,
1386 const gchar *reason)
1389 GError *error = NULL;
1393 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1394 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1395 g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1398 xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1402 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1404 g_variant_new ("(susu)",
1405 application->priv->app_id,
1409 G_DBUS_CALL_FLAGS_NONE,
1415 g_warning ("Calling Inhibit failed: %s", error->message);
1416 g_error_free (error);
1420 g_variant_get (res, "(u)", &cookie);
1421 g_variant_unref (res);
1427 * gtk_application_uninhibit:
1428 * @application: the #GApplication
1429 * @cookie: a cookie that was returned by gtk_application_inhibit()
1431 * Removes an inhibitor that has been established with gtk_application_inhibit().
1432 * Inhibitors are also cleared when the application exits.
1437 gtk_application_uninhibit (GtkApplication *application,
1440 g_return_if_fail (GTK_IS_APPLICATION (application));
1441 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1442 g_return_if_fail (application->priv->sm_proxy != NULL);
1444 g_dbus_proxy_call (application->priv->sm_proxy,
1446 g_variant_new ("(u)", cookie),
1447 G_DBUS_CALL_FLAGS_NONE,
1453 * gtk_application_is_inhibited:
1454 * @application: the #GApplication
1455 * @flags: what types of actions should be queried
1457 * Determines if any of the actions specified in @flags are
1458 * currently inhibited (possibly by another application).
1460 * Returns: %TRUE if any of the actions specified in @flags are inhibited
1465 gtk_application_is_inhibited (GtkApplication *application,
1466 GtkApplicationInhibitFlags flags)
1469 GError *error = NULL;
1472 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1473 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1474 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1476 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1478 g_variant_new ("(u)", flags),
1479 G_DBUS_CALL_FLAGS_NONE,
1485 g_warning ("Calling IsInhibited failed: %s", error->message);
1486 g_error_free (error);
1490 g_variant_get (res, "(b)", &inhibited);
1491 g_variant_unref (res);
1496 #elif defined(GDK_WINDOWING_QUARTZ)
1498 /* OS X implementation copied from EggSMClient, but simplified since
1499 * it doesn't need to interact with the user.
1503 idle_will_quit (gpointer data)
1505 GtkApplication *app = data;
1507 if (app->priv->quit_inhibit == 0)
1508 g_application_quit (G_APPLICATION (app));
1511 GtkApplicationQuartzInhibitor *inhibitor;
1515 for (iter = app->priv->inhibitors; iter; iter = iter->next)
1517 inhibitor = iter->data;
1518 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1521 g_assert (inhibitor != NULL);
1523 dialog = gtk_message_dialog_new (inhibitor->window,
1527 _("%s cannot quit at this time:\n\n%s"),
1528 g_get_application_name (),
1530 g_signal_connect_swapped (dialog,
1532 G_CALLBACK (gtk_widget_destroy),
1534 gtk_widget_show_all (dialog);
1537 return G_SOURCE_REMOVE;
1541 quit_requested (const AppleEvent *aevt,
1545 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1547 /* Don't emit the "quit" signal immediately, since we're
1548 * called from a weird point in the guts of gdkeventloop-quartz.c
1550 g_idle_add_full (G_PRIORITY_DEFAULT, idle_will_quit, app, NULL);
1552 return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
1556 gtk_application_startup_session_quartz (GtkApplication *app)
1558 if (app->priv->register_session)
1559 AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1560 NewAEEventHandlerUPP (quit_requested),
1561 (long)GPOINTER_TO_SIZE (app), false);
1565 gtk_application_inhibit (GtkApplication *application,
1567 GtkApplicationInhibitFlags flags,
1568 const gchar *reason)
1570 GtkApplicationQuartzInhibitor *inhibitor;
1572 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1573 g_return_val_if_fail (flags != 0, 0);
1575 inhibitor = g_slice_new (GtkApplicationQuartzInhibitor);
1576 inhibitor->cookie = ++application->priv->next_cookie;
1577 inhibitor->flags = flags;
1578 inhibitor->reason = g_strdup (reason);
1579 inhibitor->window = window ? g_object_ref (window) : NULL;
1581 application->priv->inhibitors = g_slist_prepend (application->priv->inhibitors, inhibitor);
1583 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1584 application->priv->quit_inhibit++;
1586 return inhibitor->cookie;
1590 gtk_application_uninhibit (GtkApplication *application,
1595 for (iter = application->priv->inhibitors; iter; iter = iter->next)
1597 GtkApplicationQuartzInhibitor *inhibitor = iter->data;
1599 if (inhibitor->cookie == cookie)
1601 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1602 application->priv->quit_inhibit--;
1603 gtk_application_quartz_inhibitor_free (inhibitor);
1604 application->priv->inhibitors = g_slist_delete_link (application->priv->inhibitors, iter);
1609 g_warning ("Invalid inhibitor cookie");
1613 gtk_application_is_inhibited (GtkApplication *application,
1614 GtkApplicationInhibitFlags flags)
1616 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1617 return application->priv->quit_inhibit > 0;
1624 /* Trivial implementation.
1626 * For the inhibit API on Windows, see
1627 * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1631 gtk_application_inhibit (GtkApplication *application,
1633 GtkApplicationInhibitFlags flags,
1634 const gchar *reason)
1640 gtk_application_uninhibit (GtkApplication *application,
1646 gtk_application_is_inhibited (GtkApplication *application,
1647 GtkApplicationInhibitFlags flags)