2 * Copyright © 2011 Canonical 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 "gtkapplicationwindow.h"
24 #include "gtkapplicationprivate.h"
25 #include "gtkwidgetprivate.h"
26 #include "gtkwindowprivate.h"
27 #include "gtkmodelmenu.h"
28 #include "gactionmuxer.h"
29 #include "gtkaccelgroup.h"
30 #include "gtkaccelmap.h"
32 #include "gtksettings.h"
35 #ifdef GDK_WINDOWING_X11
36 #include <gdk/x11/gdkx.h>
40 #include <gio/gdesktopappinfo.h>
44 * SECTION:gtkapplicationwindow
45 * @title: GtkApplicationWindow
46 * @short_description: GtkWindow subclass with GtkApplication support
48 * GtkApplicationWindow is a #GtkWindow subclass that offers some
49 * extra functionality for better integration with #GtkApplication
50 * features. Notably, it can handle both the application menu as well
51 * as the menubar. See gtk_application_set_app_menu() and
52 * gtk_application_set_menubar().
54 * This class implements the #GActionGroup and #GActionMap interfaces,
55 * to let you add window-specific actions that will be exported by the
56 * associated #GtkApplication, together with its application-wide
57 * actions. Window-specific actions are prefixed with the "win."
58 * prefix and application-wide actions are prefixed with the "app."
59 * prefix. Actions must be addressed with the prefixed name when
60 * referring to them from a #GMenuModel.
62 * Note that widgets that are placed inside a GtkApplicationWindow
63 * can also activate these actions, if they implement the
64 * GtkActionable interface.
66 * As with #GtkApplication, the GDK lock will be acquired when
67 * processing actions arriving from other processes and should therefore
68 * be held when activating actions locally (if GDK threads are enabled).
70 * The settings #GtkSettings:gtk-shell-shows-app-menu and
71 * #GtkSettings:gtk-shell-shows-menubar tell GTK+ whether the
72 * desktop environment is showing the application menu and menubar
73 * models outside the application as part of the desktop shell.
74 * For instance, on OS X, both menus will be displayed remotely;
75 * on Windows neither will be. gnome-shell (starting with version 3.4)
76 * will display the application menu, but not the menubar.
78 * If the desktop environment does not display the menubar, then
79 * #GtkApplicationWindow will automatically show a #GtkMenuBar for it.
80 * (see the #GtkApplication docs for some screenshots of how this
81 * looks on different platforms).
82 * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
83 * property. If the desktop environment does not display the application
84 * menu, then it will automatically be included in the menubar.
86 * <example><title>A GtkApplicationWindow with a menubar</title>
87 * <programlisting><![CDATA[
88 * app = gtk_application_new ();
90 * builder = gtk_builder_new ();
91 * gtk_builder_add_from_string (builder,
93 * " <menu id='menubar'>"
94 * " <submenu label='_Edit'>"
95 * " <item label='_Copy' action='win.copy'/>"
96 * " <item label='_Paste' action='win.paste'/>"
100 * gtk_application_set_menubar (G_APPLICATION (app),
101 * G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
102 * g_object_unref (builder);
106 * window = gtk_application_window_new (app);
111 * <example><title>Handling fallback yourself</title>
113 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/sunny.c">
114 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
119 * The XML format understood by #GtkBuilder for #GMenuModel consists
120 * of a toplevel <tag class="starttag">menu</tag> element, which contains
121 * one or more <tag class="starttag">item</tag> elements. Each
122 * <tag class="starttag">item</tag> element contains
123 * <tag class="starttag">attribute</tag> and <tag class="starttag">link</tag>
124 * elements with a mandatory name attribute.
125 * <tag class="starttag">link</tag> elements have the same content
126 * model as <tag class="starttag">menu</tag>.
128 * Attribute values can be translated using gettext, like other #GtkBuilder
129 * content. <tag class="starttag">attribute</tag> elements can be marked for
130 * translation with a <literal>translatable="yes"</literal> attribute.
131 * It is also possible to specify message context and translator comments,
132 * using the context and comments attributes. To make use of this, the
133 * #GtkBuilder must have been given the gettext domain to use.
136 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
139 GSimpleActionGroup parent_instance;
141 } GtkApplicationWindowActions;
143 static GType gtk_application_window_actions_get_type (void);
144 static void gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
145 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
146 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
149 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
150 const gchar *action_name,
152 GVariant *platform_data)
154 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
155 GApplication *application;
156 GApplicationClass *class;
158 application = G_APPLICATION (gtk_window_get_application (actions->window));
159 class = G_APPLICATION_GET_CLASS (application);
161 class->before_emit (application, platform_data);
162 g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
163 class->after_emit (application, platform_data);
167 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
168 const gchar *action_name,
170 GVariant *platform_data)
172 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
173 GApplication *application;
174 GApplicationClass *class;
176 application = G_APPLICATION (gtk_window_get_application (actions->window));
177 class = G_APPLICATION_GET_CLASS (application);
179 class->before_emit (application, platform_data);
180 g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
181 class->after_emit (application, platform_data);
185 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
190 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
192 iface->activate_action_full = gtk_application_window_actions_activate_action_full;
193 iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
197 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
201 static GSimpleActionGroup *
202 gtk_application_window_actions_new (GtkApplicationWindow *window)
204 GtkApplicationWindowActions *actions;
206 actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
207 actions->window = GTK_WINDOW (window);
209 return G_SIMPLE_ACTION_GROUP (actions);
212 /* Now onto GtkApplicationWindow... */
214 struct _GtkApplicationWindowPrivate
216 GSimpleActionGroup *actions;
217 GActionObservable *muxer;
218 gboolean muxer_initialised;
220 GtkAccelGroup *accels;
221 GSList *accel_closures;
223 GMenu *app_menu_section;
224 GMenu *menubar_section;
225 gboolean show_menubar;
227 GDBusConnection *session;
235 gtk_application_window_update_menubar (GtkApplicationWindow *window)
237 gboolean should_have_menubar;
238 gboolean have_menubar;
240 have_menubar = window->priv->menubar != NULL;
242 should_have_menubar = window->priv->show_menubar &&
243 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
244 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
246 if (have_menubar && !should_have_menubar)
248 gtk_widget_unparent (window->priv->menubar);
249 window->priv->menubar = NULL;
251 gtk_widget_queue_resize (GTK_WIDGET (window));
254 if (!have_menubar && should_have_menubar)
258 combined = g_menu_new ();
259 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
260 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
262 window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->accels);
263 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
264 gtk_widget_show_all (window->priv->menubar);
265 g_object_unref (combined);
267 gtk_widget_queue_resize (GTK_WIDGET (window));
272 gtk_application_window_get_app_desktop_name (void)
274 gchar *retval = NULL;
277 GDesktopAppInfo *app_info;
278 const gchar *app_name = NULL;
281 desktop_file = g_strconcat (g_get_prgname (), ".desktop", NULL);
282 app_info = g_desktop_app_info_new (desktop_file);
283 g_free (desktop_file);
285 if (app_info != NULL)
286 app_name = g_app_info_get_name (G_APP_INFO (app_info));
288 if (app_name != NULL)
289 retval = g_strdup (app_name);
291 g_clear_object (&app_info);
292 #endif /* HAVE_GIO_UNIX */
298 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
299 GtkSettings *settings)
301 gboolean shown_by_shell;
303 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
307 /* the shell shows it, so don't show it locally */
308 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
309 g_menu_remove (window->priv->app_menu_section, 0);
313 /* the shell does not show it, so make sure we show it */
314 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
316 GMenuModel *app_menu;
318 app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
320 if (app_menu != NULL)
322 const gchar *app_name;
325 app_name = g_get_application_name ();
326 if (app_name != g_get_prgname ())
328 /* the app has set its application name, use it */
329 name = g_strdup (app_name);
333 /* get the name from .desktop file */
334 name = gtk_application_window_get_app_desktop_name ();
336 name = g_strdup (_("Application"));
339 g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
347 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
348 GtkSettings *settings)
350 gboolean shown_by_shell;
352 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
356 /* the shell shows it, so don't show it locally */
357 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
358 g_menu_remove (window->priv->menubar_section, 0);
362 /* the shell does not show it, so make sure we show it */
363 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
367 menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
370 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
382 accel_activate (GClosure *closure,
383 GValue *return_value,
384 guint n_param_values,
385 const GValue *param_values,
386 gpointer invocation_hint,
387 gpointer marshal_data)
389 AccelClosure *aclosure = (AccelClosure*)closure;
390 GActionGroup *actions;
392 actions = G_ACTION_GROUP (closure->data);
393 if (g_action_group_get_action_enabled (actions, aclosure->action_name))
395 g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
397 /* we handled the accelerator */
398 g_value_set_boolean (return_value, TRUE);
403 free_accel_closures (GtkApplicationWindow *window)
407 for (l = window->priv->accel_closures; l; l = l->next)
409 AccelClosure *closure = l->data;
411 gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
413 g_object_unref (closure->closure.data);
414 if (closure->parameter)
415 g_variant_unref (closure->parameter);
416 g_free (closure->action_name);
417 g_closure_invalidate (&closure->closure);
418 g_closure_unref (&closure->closure);
420 g_slist_free (window->priv->accel_closures);
421 window->priv->accel_closures = NULL;
424 /* Hack. We iterate over the accel map instead of the actions,
425 * in order to pull the parameters out of accel map entries
428 add_accel_closure (gpointer data,
429 const gchar *accel_path,
431 GdkModifierType accel_mods,
434 GtkApplicationWindow *window = data;
435 GActionGroup *actions;
440 AccelClosure *closure;
445 if (!g_str_has_prefix (accel_path, "<GAction>/"))
448 path = accel_path + strlen ("<GAction>/");
449 p = strchr (path, '/');
452 action_name = g_strndup (path, p - path);
453 parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
455 g_warning ("Failed to parse parameter from '%s'\n", accel_path);
459 action_name = g_strdup (path);
463 actions = G_ACTION_GROUP (_gtk_widget_get_action_muxer (GTK_WIDGET (window)));
464 if (g_action_group_has_action (actions, action_name))
466 closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
467 g_closure_set_marshal (&closure->closure, accel_activate);
469 closure->action_name = g_strdup (action_name);
470 closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
472 window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
473 g_closure_sink (&closure->closure);
475 gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
478 g_free (action_name);
482 gtk_application_window_update_accels (GtkApplicationWindow *window)
484 free_accel_closures (window);
486 gtk_accel_map_foreach (window, add_accel_closure);
490 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
494 GtkApplicationWindow *window = user_data;
496 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
497 gtk_application_window_update_menubar (window);
501 gtk_application_window_shell_shows_menubar_changed (GObject *object,
505 GtkApplicationWindow *window = user_data;
507 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
508 gtk_application_window_update_menubar (window);
512 gtk_application_window_list_actions (GActionGroup *group)
514 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
516 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
520 gtk_application_window_query_action (GActionGroup *group,
521 const gchar *action_name,
523 const GVariantType **parameter_type,
524 const GVariantType **state_type,
525 GVariant **state_hint,
528 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
530 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
531 action_name, enabled, parameter_type, state_type, state_hint, state);
535 gtk_application_window_activate_action (GActionGroup *group,
536 const gchar *action_name,
539 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
541 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
545 gtk_application_window_change_action_state (GActionGroup *group,
546 const gchar *action_name,
549 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
551 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
555 gtk_application_window_lookup_action (GActionMap *action_map,
556 const gchar *action_name)
558 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
560 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
564 gtk_application_window_add_action (GActionMap *action_map,
567 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
569 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
573 gtk_application_window_remove_action (GActionMap *action_map,
574 const gchar *action_name)
576 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
578 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
582 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
584 iface->list_actions = gtk_application_window_list_actions;
585 iface->query_action = gtk_application_window_query_action;
586 iface->activate_action = gtk_application_window_activate_action;
587 iface->change_action_state = gtk_application_window_change_action_state;
591 gtk_application_window_map_iface_init (GActionMapInterface *iface)
593 iface->lookup_action = gtk_application_window_lookup_action;
594 iface->add_action = gtk_application_window_add_action;
595 iface->remove_action = gtk_application_window_remove_action;
598 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
599 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
600 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
607 static GParamSpec *gtk_application_window_properties[N_PROPS];
610 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
611 gint *minimum_height,
612 gint *natural_height)
614 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
616 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
617 ->get_preferred_height (widget, minimum_height, natural_height);
619 if (window->priv->menubar != NULL)
621 gint menubar_min_height, menubar_nat_height;
623 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
624 *minimum_height += menubar_min_height;
625 *natural_height += menubar_nat_height;
630 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
632 gint *minimum_height,
633 gint *natural_height)
635 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
637 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
638 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
640 if (window->priv->menubar != NULL)
642 gint menubar_min_height, menubar_nat_height;
644 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
645 *minimum_height += menubar_min_height;
646 *natural_height += menubar_nat_height;
651 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
655 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
657 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
658 ->get_preferred_width (widget, minimum_width, natural_width);
660 if (window->priv->menubar != NULL)
662 gint menubar_min_width, menubar_nat_width;
664 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
665 *minimum_width = MAX (*minimum_width, menubar_min_width);
666 *natural_width = MAX (*natural_width, menubar_nat_width);
671 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
676 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
679 if (window->priv->menubar != NULL)
680 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
684 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
685 ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
687 if (window->priv->menubar != NULL)
689 gint menubar_min_width, menubar_nat_width;
691 gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
692 *minimum_width = MAX (*minimum_width, menubar_min_width);
693 *natural_width = MAX (*natural_width, menubar_nat_width);
698 gtk_application_window_real_size_allocate (GtkWidget *widget,
699 GtkAllocation *allocation)
701 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
703 if (window->priv->menubar != NULL)
705 GtkAllocation menubar_allocation = *allocation;
709 _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
711 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
713 menubar_allocation.height = menubar_height;
714 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
716 child = gtk_bin_get_child (GTK_BIN (window));
717 if (child != NULL && gtk_widget_get_visible (child))
719 GtkAllocation child_allocation = *allocation;
722 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
723 child_allocation.x += border_width;
724 child_allocation.y += border_width + menubar_height;
725 child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
726 child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
728 gtk_widget_size_allocate (child, &child_allocation);
732 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
733 ->size_allocate (widget, allocation);
737 gtk_application_window_real_realize (GtkWidget *widget)
739 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
740 GtkApplication *application;
741 GtkSettings *settings;
743 application = gtk_window_get_application (GTK_WINDOW (window));
744 settings = gtk_widget_get_settings (widget);
746 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
747 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
748 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
749 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
751 if (!window->priv->muxer_initialised)
753 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "app", G_ACTION_GROUP (application));
754 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "win", G_ACTION_GROUP (window));
755 window->priv->muxer_initialised = TRUE;
758 gtk_application_window_update_shell_shows_app_menu (window, settings);
759 gtk_application_window_update_shell_shows_menubar (window, settings);
760 gtk_application_window_update_menubar (window);
761 gtk_application_window_update_accels (window);
763 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
766 #ifdef GDK_WINDOWING_X11
768 GdkWindow *gdkwindow;
770 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
772 if (GDK_IS_X11_WINDOW (gdkwindow) && window->priv->session)
774 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
775 g_application_get_application_id (G_APPLICATION (application)));
777 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
778 g_dbus_connection_get_unique_name (window->priv->session));
780 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
781 g_application_get_dbus_object_path (G_APPLICATION (application)));
783 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
784 window->priv->object_path);
786 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
787 gtk_application_get_app_menu_object_path (application));
789 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
790 gtk_application_get_menubar_object_path (application));
797 gtk_application_window_real_unrealize (GtkWidget *widget)
799 GtkSettings *settings;
801 settings = gtk_widget_get_settings (widget);
803 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
804 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
806 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
807 ->unrealize (widget);
811 gtk_application_window_publish (GtkApplicationWindow *window,
812 GDBusConnection *session,
813 const gchar *object_path,
816 g_assert (window->priv->session == NULL);
817 g_assert (window->priv->export_id == 0);
818 g_assert (window->priv->object_path == NULL);
819 g_assert (window->priv->id == 0);
821 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
822 G_ACTION_GROUP (window->priv->actions),
825 if (window->priv->export_id == 0)
828 window->priv->session = session;
829 window->priv->object_path = g_strdup (object_path);
830 window->priv->id = object_id;
836 gtk_application_window_unpublish (GtkApplicationWindow *window)
838 g_assert (window->priv->session != NULL);
839 g_assert (window->priv->export_id != 0);
840 g_assert (window->priv->object_path != NULL);
841 g_assert (window->priv->id != 0);
843 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
844 window->priv->session = NULL;
845 window->priv->export_id = 0;
846 window->priv->id = 0;
848 g_free (window->priv->object_path);
849 window->priv->object_path = NULL;
853 gtk_application_window_real_map (GtkWidget *widget)
855 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
857 /* XXX could eliminate this by tweaking gtk_window_map */
858 if (window->priv->menubar)
859 gtk_widget_map (window->priv->menubar);
861 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
866 gtk_application_window_real_forall_internal (GtkContainer *container,
867 gboolean include_internal,
868 GtkCallback callback,
871 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
873 if (window->priv->menubar)
874 callback (window->priv->menubar, user_data);
876 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
877 ->forall (container, include_internal, callback, user_data);
882 gtk_application_window_get_property (GObject *object,
887 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
891 case PROP_SHOW_MENUBAR:
892 g_value_set_boolean (value, window->priv->show_menubar);
896 g_assert_not_reached ();
901 gtk_application_window_set_property (GObject *object,
906 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
910 case PROP_SHOW_MENUBAR:
911 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
915 g_assert_not_reached ();
920 gtk_application_window_dispose (GObject *object)
922 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
924 if (window->priv->menubar)
926 gtk_widget_unparent (window->priv->menubar);
927 window->priv->menubar = NULL;
930 free_accel_closures (window);
932 g_clear_object (&window->priv->app_menu_section);
933 g_clear_object (&window->priv->menubar_section);
934 g_clear_object (&window->priv->actions);
935 g_clear_object (&window->priv->accels);
936 g_clear_object (&window->priv->muxer);
938 G_OBJECT_CLASS (gtk_application_window_parent_class)
943 gtk_application_window_init (GtkApplicationWindow *window)
945 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
947 window->priv->actions = gtk_application_window_actions_new (window);
948 window->priv->app_menu_section = g_menu_new ();
949 window->priv->menubar_section = g_menu_new ();
950 window->priv->accels = gtk_accel_group_new ();
951 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
953 gtk_widget_insert_action_group (GTK_WIDGET (window), "win", G_ACTION_GROUP (window->priv->actions));
955 /* window->priv->actions is the one and only ref on the group, so when
956 * we dispose, the action group will die, disconnecting all signals.
958 g_signal_connect_swapped (window->priv->actions, "action-added",
959 G_CALLBACK (g_action_group_action_added), window);
960 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
961 G_CALLBACK (g_action_group_action_enabled_changed), window);
962 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
963 G_CALLBACK (g_action_group_action_state_changed), window);
964 g_signal_connect_swapped (window->priv->actions, "action-removed",
965 G_CALLBACK (g_action_group_action_removed), window);
967 window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
971 gtk_application_window_class_init (GtkApplicationWindowClass *class)
973 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
974 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
975 GObjectClass *object_class = G_OBJECT_CLASS (class);
977 container_class->forall = gtk_application_window_real_forall_internal;
978 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
979 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
980 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
981 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
982 widget_class->size_allocate = gtk_application_window_real_size_allocate;
983 widget_class->realize = gtk_application_window_real_realize;
984 widget_class->unrealize = gtk_application_window_real_unrealize;
985 widget_class->map = gtk_application_window_real_map;
986 object_class->get_property = gtk_application_window_get_property;
987 object_class->set_property = gtk_application_window_set_property;
988 object_class->dispose = gtk_application_window_dispose;
991 * GtkApplicationWindow:show-menubar:
993 * If this property is %TRUE, the window will display a menubar
994 * that includes the app menu and menubar, unless these are
995 * shown by the desktop shell. See gtk_application_set_app_menu()
996 * and gtk_application_set_menubar().
998 * If %FALSE, the window will not display a menubar, regardless
999 * of whether the desktop shell is showing the menus or not.
1001 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
1002 g_param_spec_boolean ("show-menubar",
1003 P_("Show a menubar"),
1004 P_("TRUE if the window should show a "
1005 "menubar at the top of the window"),
1006 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
1007 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
1008 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
1012 * gtk_application_window_new:
1013 * @application: a #GtkApplication
1015 * Creates a new #GtkApplicationWindow.
1017 * Returns: a newly created #GtkApplicationWindow
1022 gtk_application_window_new (GtkApplication *application)
1024 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1026 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
1027 "application", application,
1032 * gtk_application_window_get_show_menubar:
1033 * @window: a #GtkApplicationWindow
1035 * Returns whether the window will display a menubar for the app menu
1036 * and menubar as needed.
1038 * Returns: %TRUE if @window will display a menubar when needed
1043 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
1045 return window->priv->show_menubar;
1049 * gtk_application_window_set_show_menubar:
1050 * @window: a #GtkApplicationWindow
1051 * @show_menubar: whether to show a menubar when needed
1053 * Sets whether the window will display a menubar for the app menu
1054 * and menubar as needed.
1059 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
1060 gboolean show_menubar)
1062 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
1064 show_menubar = !!show_menubar;
1066 if (window->priv->show_menubar != show_menubar)
1068 window->priv->show_menubar = show_menubar;
1070 gtk_application_window_update_menubar (window);
1072 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1076 GSimpleActionObserver *
1077 gtk_application_window_create_observer (GtkApplicationWindow *window,
1078 const gchar *action_name,
1081 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
1083 return g_simple_action_observer_new (window->priv->muxer, action_name, target);
1087 gtk_application_window_get_observable (GtkApplicationWindow *window)
1089 return G_ACTION_OBSERVABLE (window->priv->muxer);
1093 gtk_application_window_get_accel_group (GtkApplicationWindow *window)
1095 return window->priv->accels;
1099 * gtk_application_window_get_id:
1100 * @window: a #GtkApplicationWindow
1102 * Returns the unique ID of the window. If the window has not yet been added to
1103 * a #GtkApplication, returns <literal>0</literal>.
1105 * Returns: the unique ID for @window, or <literal>0</literal> if the window
1106 * has not yet been added to a #GtkApplication
1111 gtk_application_window_get_id (GtkApplicationWindow *window)
1113 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), 0);
1115 return window->priv->id;