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 * This function calls g_type_init() for you. gtk_init() is called
772 * as soon as the application gets registered as the primary instance.
774 * Concretely, gtk_init() is called in the default handler for the
775 * #GApplication::startup signal. Therefore, #GtkApplication subclasses should
776 * chain up in their #GApplication:startup handler before using any GTK+ API.
778 * Note that commandline arguments are not passed to gtk_init().
779 * All GTK+ functionality that is available via commandline arguments
780 * can also be achieved by setting suitable environment variables
781 * such as <envar>G_DEBUG</envar>, so this should not be a big
782 * problem. If you absolutely must support GTK+ commandline arguments,
783 * you can explicitly call gtk_init() before creating the application
786 * If non-%NULL, the application ID must be valid. See
787 * g_application_id_is_valid().
789 * If no application ID is given then some features (most notably application
790 * uniqueness) will be disabled. A null application ID is only allowed with
793 * Returns: a new #GtkApplication instance
798 gtk_application_new (const gchar *application_id,
799 GApplicationFlags flags)
801 g_return_val_if_fail (application_id == NULL || g_application_id_is_valid (application_id), NULL);
805 return g_object_new (GTK_TYPE_APPLICATION,
806 "application-id", application_id,
812 * gtk_application_add_window:
813 * @application: a #GtkApplication
814 * @window: a #GtkWindow
816 * Adds a window to @application.
818 * This call is equivalent to setting the #GtkWindow:application
819 * property of @window to @application.
821 * Normally, the connection between the application and the window
822 * will remain until the window is destroyed, but you can explicitly
823 * remove it with gtk_application_remove_window().
825 * GTK+ will keep the application running as long as it has
831 gtk_application_add_window (GtkApplication *application,
834 g_return_if_fail (GTK_IS_APPLICATION (application));
836 if (!g_list_find (application->priv->windows, window))
837 g_signal_emit (application,
838 gtk_application_signals[WINDOW_ADDED], 0, window);
842 * gtk_application_remove_window:
843 * @application: a #GtkApplication
844 * @window: a #GtkWindow
846 * Remove a window from @application.
848 * If @window belongs to @application then this call is equivalent to
849 * setting the #GtkWindow:application property of @window to
852 * The application may stop running as a result of a call to this
858 gtk_application_remove_window (GtkApplication *application,
861 g_return_if_fail (GTK_IS_APPLICATION (application));
863 if (g_list_find (application->priv->windows, window))
864 g_signal_emit (application,
865 gtk_application_signals[WINDOW_REMOVED], 0, window);
869 * gtk_application_get_windows:
870 * @application: a #GtkApplication
872 * Gets a list of the #GtkWindows associated with @application.
874 * The list is sorted by most recently focused window, such that the first
875 * element is the currently focused window. (Useful for choosing a parent
876 * for a transient window.)
878 * The list that is returned should not be modified in any way. It will
879 * only remain valid until the next focus change or window creation or
882 * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow
887 gtk_application_get_windows (GtkApplication *application)
889 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
891 return application->priv->windows;
895 * gtk_application_get_window_by_id:
896 * @application: a #GtkApplication
897 * @id: an identifier number
899 * Returns: (transfer none): the #GtkApplicationWindow with ID @id, or
900 * %NULL if there is no window with this ID
905 gtk_application_get_window_by_id (GtkApplication *application,
910 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
912 for (l = application->priv->windows; l != NULL; l = l->next)
914 if (GTK_IS_APPLICATION_WINDOW (l->data) &&
915 gtk_application_window_get_id (GTK_APPLICATION_WINDOW (l->data)) == id)
923 * gtk_application_get_active_window:
924 * @application: a #GtkApplication
926 * Gets the "active" window for the application.
928 * The active window is the one that was most recently focused (within
929 * the application). This window may not have the focus at the moment
930 * if another application has it -- this is just the most
931 * recently-focused window within this application.
933 * Returns: (transfer none): the active window
938 gtk_application_get_active_window (GtkApplication *application)
940 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
942 return application->priv->windows ? application->priv->windows->data : NULL;
946 * gtk_application_add_accelerator:
947 * @application: a #GtkApplication
948 * @accelerator: accelerator string
949 * @action_name: the name of the action to activate
950 * @parameter: (allow-none): parameter to pass when activating the action,
951 * or %NULL if the action does not accept an activation parameter
953 * Installs an accelerator that will cause the named action
954 * to be activated when the key combination specificed by @accelerator
957 * @accelerator must be a string that can be parsed by
958 * gtk_accelerator_parse(), e.g. "<Primary>q" or
959 * "<Control><Alt>p".
961 * @action_name must be the name of an action as it would be used
962 * in the app menu, i.e. actions that have been added to the application
963 * are referred to with an "app." prefix, and window-specific actions
964 * with a "win." prefix.
966 * GtkApplication also extracts accelerators out of 'accel' attributes
967 * in the #GMenuModels passed to gtk_application_set_app_menu() and
968 * gtk_application_set_menubar(), which is usually more convenient
969 * than calling this function for each accelerator.
974 gtk_application_add_accelerator (GtkApplication *application,
975 const gchar *accelerator,
976 const gchar *action_name,
981 GdkModifierType accel_mods;
983 g_return_if_fail (GTK_IS_APPLICATION (application));
985 /* Call this here, since gtk_init() is only getting called in startup() */
986 _gtk_accel_map_init ();
988 gtk_accelerator_parse (accelerator, &accel_key, &accel_mods);
992 g_warning ("Failed to parse accelerator: '%s'\n", accelerator);
996 accel_path = _gtk_accel_path_for_action (action_name, parameter);
998 if (gtk_accel_map_lookup_entry (accel_path, NULL))
999 gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
1001 gtk_accel_map_add_entry (accel_path, accel_key, accel_mods);
1003 g_free (accel_path);
1007 * gtk_application_remove_accelerator:
1008 * @application: a #GtkApplication
1009 * @action_name: the name of the action to activate
1010 * @parameter: (allow-none): parameter to pass when activating the action,
1011 * or %NULL if the action does not accept an activation parameter
1013 * Removes an accelerator that has been previously added
1014 * with gtk_application_add_accelerator().
1019 gtk_application_remove_accelerator (GtkApplication *application,
1020 const gchar *action_name,
1021 GVariant *parameter)
1025 g_return_if_fail (GTK_IS_APPLICATION (application));
1027 accel_path = _gtk_accel_path_for_action (action_name, parameter);
1029 if (!gtk_accel_map_lookup_entry (accel_path, NULL))
1031 g_warning ("No accelerator found for '%s'\n", accel_path);
1032 g_free (accel_path);
1036 gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
1037 g_free (accel_path);
1041 * gtk_application_set_app_menu:
1042 * @application: a #GtkApplication
1043 * @app_menu: (allow-none): a #GMenuModel, or %NULL
1045 * Sets or unsets the application menu for @application.
1047 * This can only be done in the primary instance of the application,
1048 * after it has been registered. #GApplication:startup is a good place
1051 * The application menu is a single menu containing items that typically
1052 * impact the application as a whole, rather than acting on a specific
1053 * window or document. For example, you would expect to see
1054 * "Preferences" or "Quit" in an application menu, but not "Save" or
1057 * If supported, the application menu will be rendered by the desktop
1060 * Use the base #GActionMap interface to add actions, to respond to the user
1061 * selecting these menu items.
1066 gtk_application_set_app_menu (GtkApplication *application,
1067 GMenuModel *app_menu)
1069 g_return_if_fail (GTK_IS_APPLICATION (application));
1070 g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1071 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1073 if (app_menu != application->priv->app_menu)
1075 if (application->priv->app_menu != NULL)
1076 g_object_unref (application->priv->app_menu);
1078 application->priv->app_menu = app_menu;
1080 if (application->priv->app_menu != NULL)
1081 g_object_ref (application->priv->app_menu);
1084 extract_accels_from_menu (app_menu, application);
1086 #ifdef GDK_WINDOWING_X11
1087 gtk_application_set_app_menu_x11 (application, app_menu);
1090 g_object_notify (G_OBJECT (application), "app-menu");
1095 * gtk_application_get_app_menu:
1096 * @application: a #GtkApplication
1098 * Returns the menu model that has been set with
1099 * gtk_application_set_app_menu().
1101 * Returns: (transfer none): the application menu of @application
1106 gtk_application_get_app_menu (GtkApplication *application)
1108 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1110 return application->priv->app_menu;
1114 * gtk_application_set_menubar:
1115 * @application: a #GtkApplication
1116 * @menubar: (allow-none): a #GMenuModel, or %NULL
1118 * Sets or unsets the menubar for windows of @application.
1120 * This is a menubar in the traditional sense.
1122 * This can only be done in the primary instance of the application,
1123 * after it has been registered. #GApplication:startup is a good place
1126 * Depending on the desktop environment, this may appear at the top of
1127 * each window, or at the top of the screen. In some environments, if
1128 * both the application menu and the menubar are set, the application
1129 * menu will be presented as if it were the first item of the menubar.
1130 * Other environments treat the two as completely separate -- for
1131 * example, the application menu may be rendered by the desktop shell
1132 * while the menubar (if set) remains in each individual window.
1134 * Use the base #GActionMap interface to add actions, to respond to the user
1135 * selecting these menu items.
1140 gtk_application_set_menubar (GtkApplication *application,
1141 GMenuModel *menubar)
1143 g_return_if_fail (GTK_IS_APPLICATION (application));
1144 g_return_if_fail (g_application_get_is_registered (G_APPLICATION (application)));
1145 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1147 if (menubar != application->priv->menubar)
1149 if (application->priv->menubar != NULL)
1150 g_object_unref (application->priv->menubar);
1152 application->priv->menubar = menubar;
1154 if (application->priv->menubar != NULL)
1155 g_object_ref (application->priv->menubar);
1158 extract_accels_from_menu (menubar, application);
1160 #ifdef GDK_WINDOWING_X11
1161 gtk_application_set_menubar_x11 (application, menubar);
1164 g_object_notify (G_OBJECT (application), "menubar");
1169 * gtk_application_get_menubar:
1170 * @application: a #GtkApplication
1172 * Returns the menu model that has been set with
1173 * gtk_application_set_menubar().
1175 * Returns: (transfer none): the menubar for windows of @application
1180 gtk_application_get_menubar (GtkApplication *application)
1182 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1184 return application->priv->menubar;
1187 #if defined(GDK_WINDOWING_X11)
1189 /* D-Bus Session Management
1191 * The protocol and the D-Bus API are described here:
1192 * http://live.gnome.org/SessionManagement/GnomeSession
1193 * http://people.gnome.org/~mccann/gnome-session/docs/gnome-session.html
1197 unregister_client (GtkApplication *app)
1199 GError *error = NULL;
1201 g_debug ("Unregistering client");
1203 g_dbus_proxy_call_sync (app->priv->sm_proxy,
1205 g_variant_new ("(o)", app->priv->client_path),
1206 G_DBUS_CALL_FLAGS_NONE,
1213 g_warning ("Failed to unregister client: %s", error->message);
1214 g_error_free (error);
1217 g_clear_object (&app->priv->client_proxy);
1219 g_free (app->priv->client_path);
1220 app->priv->client_path = NULL;
1224 gtk_application_quit_response (GtkApplication *application,
1226 const gchar *reason)
1228 g_debug ("Calling EndSessionResponse %d '%s'", will_quit, reason);
1230 g_dbus_proxy_call (application->priv->client_proxy,
1231 "EndSessionResponse",
1232 g_variant_new ("(bs)", will_quit, reason ? reason : ""),
1233 G_DBUS_CALL_FLAGS_NONE,
1238 client_proxy_signal (GDBusProxy *proxy,
1239 const gchar *sender_name,
1240 const gchar *signal_name,
1241 GVariant *parameters,
1242 GtkApplication *app)
1244 if (strcmp (signal_name, "QueryEndSession") == 0)
1246 g_debug ("Received QueryEndSession");
1247 gtk_application_quit_response (app, TRUE, NULL);
1249 else if (strcmp (signal_name, "CancelEndSession") == 0)
1251 g_debug ("Received CancelEndSession");
1253 else if (strcmp (signal_name, "EndSession") == 0)
1255 g_debug ("Received EndSession");
1256 gtk_application_quit_response (app, TRUE, NULL);
1257 unregister_client (app);
1258 g_application_quit (G_APPLICATION (app));
1260 else if (strcmp (signal_name, "Stop") == 0)
1262 g_debug ("Received Stop");
1263 unregister_client (app);
1264 g_application_quit (G_APPLICATION (app));
1269 gtk_application_startup_session_dbus (GtkApplication *app)
1271 static gchar *client_id;
1272 GError *error = NULL;
1275 if (app->priv->session_bus == NULL)
1278 if (client_id == NULL)
1280 const gchar *desktop_autostart_id;
1282 desktop_autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
1283 /* Unset DESKTOP_AUTOSTART_ID in order to avoid child processes to
1284 * use the same client id.
1286 g_unsetenv ("DESKTOP_AUTOSTART_ID");
1287 client_id = g_strdup (desktop_autostart_id ? desktop_autostart_id : "");
1290 g_debug ("Connecting to session manager");
1292 app->priv->sm_proxy = g_dbus_proxy_new_sync (app->priv->session_bus,
1293 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1294 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1296 "org.gnome.SessionManager",
1297 "/org/gnome/SessionManager",
1298 "org.gnome.SessionManager",
1303 g_warning ("Failed to get a session proxy: %s", error->message);
1304 g_error_free (error);
1308 /* FIXME: should we reuse the D-Bus application id here ? */
1309 app->priv->app_id = g_strdup (g_get_prgname ());
1311 if (!app->priv->register_session)
1314 g_debug ("Registering client '%s' '%s'", app->priv->app_id, client_id);
1316 res = g_dbus_proxy_call_sync (app->priv->sm_proxy,
1318 g_variant_new ("(ss)", app->priv->app_id, client_id),
1319 G_DBUS_CALL_FLAGS_NONE,
1326 g_warning ("Failed to register client: %s", error->message);
1327 g_error_free (error);
1328 g_clear_object (&app->priv->sm_proxy);
1332 g_variant_get (res, "(o)", &app->priv->client_path);
1333 g_variant_unref (res);
1335 g_debug ("Registered client at '%s'", app->priv->client_path);
1337 app->priv->client_proxy = g_dbus_proxy_new_sync (app->priv->session_bus, 0,
1339 "org.gnome.SessionManager",
1340 app->priv->client_path,
1341 "org.gnome.SessionManager.ClientPrivate",
1346 g_warning ("Failed to get client proxy: %s", error->message);
1347 g_error_free (error);
1348 g_clear_object (&app->priv->sm_proxy);
1349 g_free (app->priv->client_path);
1350 app->priv->client_path = NULL;
1354 g_signal_connect (app->priv->client_proxy, "g-signal", G_CALLBACK (client_proxy_signal), app);
1360 * GtkApplicationInhibitFlags:
1361 * @GTK_APPLICATION_INHIBIT_LOGOUT: Inhibit ending the user session
1362 * by logging out or by shutting down the computer
1363 * @GTK_APPLICATION_INHIBIT_SWITCH: Inhibit user switching
1364 * @GTK_APPLICATION_INHIBIT_SUSPEND: Inhibit suspending the
1365 * session or computer
1366 * @GTK_APPLICATION_INHIBIT_IDLE: Inhibit the session being
1367 * marked as idle (and possibly locked)
1369 * Types of user actions that may be blocked by gtk_application_inhibit().
1375 * gtk_application_inhibit:
1376 * @application: the #GApplication
1377 * @window: (allow-none): a #GtkWindow, or %NULL
1378 * @flags: what types of actions should be inhibited
1379 * @reason: (allow-none): a short, human-readable string that explains
1380 * why these operations are inhibited
1382 * Inform the session manager that certain types of actions should be
1383 * inhibited. This is not guaranteed to work on all platforms and for
1384 * all types of actions.
1386 * Applications should invoke this method when they begin an operation
1387 * that should not be interrupted, such as creating a CD or DVD. The
1388 * types of actions that may be blocked are specified by the @flags
1389 * parameter. When the application completes the operation it should
1390 * call gtk_application_uninhibit() to remove the inhibitor. Note that
1391 * an application can have multiple inhibitors, and all of the must
1392 * be individually removed. Inhibitors are also cleared when the
1393 * application exits.
1395 * Applications should not expect that they will always be able to block
1396 * the action. In most cases, users will be given the option to force
1397 * the action to take place.
1399 * Reasons should be short and to the point.
1401 * If @window is given, the session manager may point the user to
1402 * this window to find out more about why the action is inhibited.
1404 * Returns: A non-zero cookie that is used to uniquely identify this
1405 * request. It should be used as an argument to gtk_application_uninhibit()
1406 * in order to remove the request. If the platform does not support
1407 * inhibiting or the request failed for some reason, 0 is returned.
1412 gtk_application_inhibit (GtkApplication *application,
1414 GtkApplicationInhibitFlags flags,
1415 const gchar *reason)
1418 GError *error = NULL;
1422 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1423 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), 0);
1424 g_return_val_if_fail (application->priv->sm_proxy != NULL, 0);
1427 xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (window)));
1431 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1433 g_variant_new ("(susu)",
1434 application->priv->app_id,
1438 G_DBUS_CALL_FLAGS_NONE,
1444 g_warning ("Calling Inhibit failed: %s", error->message);
1445 g_error_free (error);
1449 g_variant_get (res, "(u)", &cookie);
1450 g_variant_unref (res);
1456 * gtk_application_uninhibit:
1457 * @application: the #GApplication
1458 * @cookie: a cookie that was returned by gtk_application_inhibit()
1460 * Removes an inhibitor that has been established with gtk_application_inhibit().
1461 * Inhibitors are also cleared when the application exits.
1466 gtk_application_uninhibit (GtkApplication *application,
1469 g_return_if_fail (GTK_IS_APPLICATION (application));
1470 g_return_if_fail (!g_application_get_is_remote (G_APPLICATION (application)));
1471 g_return_if_fail (application->priv->sm_proxy != NULL);
1473 g_dbus_proxy_call (application->priv->sm_proxy,
1475 g_variant_new ("(u)", cookie),
1476 G_DBUS_CALL_FLAGS_NONE,
1482 * gtk_application_is_inhibited:
1483 * @application: the #GApplication
1484 * @flags: what types of actions should be queried
1486 * Determines if any of the actions specified in @flags are
1487 * currently inhibited (possibly by another application).
1489 * Returns: %TRUE if any of the actions specified in @flags are inhibited
1494 gtk_application_is_inhibited (GtkApplication *application,
1495 GtkApplicationInhibitFlags flags)
1498 GError *error = NULL;
1501 g_return_val_if_fail (GTK_IS_APPLICATION (application), FALSE);
1502 g_return_val_if_fail (!g_application_get_is_remote (G_APPLICATION (application)), FALSE);
1503 g_return_val_if_fail (application->priv->sm_proxy != NULL, FALSE);
1505 res = g_dbus_proxy_call_sync (application->priv->sm_proxy,
1507 g_variant_new ("(u)", flags),
1508 G_DBUS_CALL_FLAGS_NONE,
1514 g_warning ("Calling IsInhibited failed: %s", error->message);
1515 g_error_free (error);
1519 g_variant_get (res, "(b)", &inhibited);
1520 g_variant_unref (res);
1525 #elif defined(GDK_WINDOWING_QUARTZ)
1527 /* OS X implementation copied from EggSMClient, but simplified since
1528 * it doesn't need to interact with the user.
1532 idle_will_quit (gpointer data)
1534 GtkApplication *app = data;
1536 if (app->priv->quit_inhibit == 0)
1537 g_application_quit (G_APPLICATION (app));
1540 GtkApplicationQuartzInhibitor *inhibitor;
1544 for (iter = app->priv->inhibitors; iter; iter = iter->next)
1546 inhibitor = iter->data;
1547 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1550 g_assert (inhibitor != NULL);
1552 dialog = gtk_message_dialog_new (inhibitor->window,
1556 _("%s cannot quit at this time:\n\n%s"),
1557 g_get_application_name (),
1559 g_signal_connect_swapped (dialog,
1561 G_CALLBACK (gtk_widget_destroy),
1563 gtk_widget_show_all (dialog);
1566 return G_SOURCE_REMOVE;
1570 quit_requested (const AppleEvent *aevt,
1574 GtkApplication *app = GSIZE_TO_POINTER ((gsize)refcon);
1576 /* Don't emit the "quit" signal immediately, since we're
1577 * called from a weird point in the guts of gdkeventloop-quartz.c
1579 g_idle_add_full (G_PRIORITY_DEFAULT, idle_will_quit, app, NULL);
1581 return app->priv->quit_inhibit == 0 ? noErr : userCanceledErr;
1585 gtk_application_startup_session_quartz (GtkApplication *app)
1587 if (app->priv->register_session)
1588 AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
1589 NewAEEventHandlerUPP (quit_requested),
1590 (long)GPOINTER_TO_SIZE (app), false);
1594 gtk_application_inhibit (GtkApplication *application,
1596 GtkApplicationInhibitFlags flags,
1597 const gchar *reason)
1599 GtkApplicationQuartzInhibitor *inhibitor;
1601 g_return_val_if_fail (GTK_IS_APPLICATION (application), 0);
1602 g_return_val_if_fail (flags != 0, 0);
1604 inhibitor = g_slice_new (GtkApplicationQuartzInhibitor);
1605 inhibitor->cookie = ++application->priv->next_cookie;
1606 inhibitor->flags = flags;
1607 inhibitor->reason = g_strdup (reason);
1608 inhibitor->window = window ? g_object_ref (window) : NULL;
1610 application->priv->inhibitors = g_slist_prepend (application->priv->inhibitors, inhibitor);
1612 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1613 application->priv->quit_inhibit++;
1615 return inhibitor->cookie;
1619 gtk_application_uninhibit (GtkApplication *application,
1624 for (iter = application->priv->inhibitors; iter; iter = iter->next)
1626 GtkApplicationQuartzInhibitor *inhibitor = iter->data;
1628 if (inhibitor->cookie == cookie)
1630 if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1631 application->priv->quit_inhibit--;
1632 gtk_application_quartz_inhibitor_free (inhibitor);
1633 application->priv->inhibitors = g_slist_delete_link (application->priv->inhibitors, iter);
1638 g_warning ("Invalid inhibitor cookie");
1642 gtk_application_is_inhibited (GtkApplication *application,
1643 GtkApplicationInhibitFlags flags)
1645 if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
1646 return application->priv->quit_inhibit > 0;
1653 /* Trivial implementation.
1655 * For the inhibit API on Windows, see
1656 * http://msdn.microsoft.com/en-us/library/ms700677%28VS.85%29.aspx
1660 gtk_application_inhibit (GtkApplication *application,
1662 GtkApplicationInhibitFlags flags,
1663 const gchar *reason)
1669 gtk_application_uninhibit (GtkApplication *application,
1675 gtk_application_is_inhibited (GtkApplication *application,
1676 GtkApplicationInhibitFlags flags)