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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "gtkapplicationwindow.h"
26 #include "gtkapplicationprivate.h"
27 #include "gtkwindowprivate.h"
28 #include "gtkmodelmenu.h"
29 #include "gactionmuxer.h"
30 #include "gtkaccelgroup.h"
31 #include "gtkaccelmap.h"
35 #ifdef GDK_WINDOWING_X11
36 #include <gdk/x11/gdkx.h>
40 * SECTION:gtkapplicationwindow
41 * @title: GtkApplicationWindow
42 * @short_description: GtkWindow subclass with GtkApplication support
44 * GtkApplicationWindow is a #GtkWindow subclass that offers some
45 * extra functionality for better integration with #GtkApplication
46 * features. Notably, it can handle both the application menu as well
47 * as the menubar. See gtk_application_set_app_menu() and
48 * gtk_application_set_menubar().
50 * This class implements the #GActionGroup and #GActionMap interfaces,
51 * to let you add window-specific actions that will be exported by the
52 * associated #GtkApplication, together with its application-wide
53 * actions. Window-specific actions are prefixed with the "win."
54 * prefix and application-wide actions are prefixed with the "app."
55 * prefix. Actions must be addressed with the prefixed name when
56 * referring to them from a #GMenuModel.
58 * Note that widgets that are placed inside a GtkApplicationWindow
59 * can also activate these actions, if they implement the
60 * GtkActionable interface.
62 * As with #GtkApplication, the GDK lock will be acquired when
63 * processing actions arriving from other processes and should therefore
64 * be held when activating actions locally (if GDK threads are enabled).
66 * The settings #GtkSettings:gtk-shell-shows-app-menu and
67 * #GtkSettings:gtk-shell-shows-menubar tell GTK+ whether the
68 * desktop environment is showing the application menu and menubar
69 * models outside the application as part of the desktop shell.
70 * For instance, on OS X, both menus will be displayed remotely;
71 * on Windows neither will be. gnome-shell (starting with version 3.4)
72 * will display the application menu, but not the menubar.
74 * If the desktop environment does not display the menubar, then
75 * #GApplicationWindow will automatically show a #GtkMenubar for it.
76 * (see the #GtkApplication docs for some screenshots of how this
77 * looks on different platforms).
78 * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
79 * property. If the desktop environment does not display the application
80 * menu, then it will automatically be included in the menubar.
82 * <example><title>A GtkApplicationWindow with a menubar</title>
83 * <programlisting><![CDATA[
84 * app = gtk_application_new ();
86 * builder = gtk_builder_new ();
87 * gtk_builder_add_from_string (builder,
89 * " <menu id='menubar'>"
90 * " <submenu label='_Edit'>"
91 * " <item label='_Copy' action='win.copy'/>"
92 * " <item label='_Paste' action='win.paste'/>"
96 * gtk_application_set_menubar (G_APPLICATION (app),
97 * G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
98 * g_object_unref (builder);
102 * window = gtk_application_window_new (app);
107 * The XML format understood by #GtkBuilder for #GMenuModel consists
108 * of a toplevel <tag class="starttag">menu</tag> element, which contains
109 * one or more <tag class="starttag">item</tag> elements. Each
110 * <tag class="starttag">item</tag> element contains
111 * <tag class="starttag">attribute</tag> and <tag class="starttag">link</tag>
112 * elements with a mandatory name attribute.
113 * <tag class="starttag">link</tag> elements have the same content
114 * model as <tag class="starttag">menu</tag>.
116 * Attribute values can be translated using gettext, like other #GtkBuilder
117 * content. <tag class="starttag">attribute</tag> elements can be marked for
118 * translation with a <literal>translatable="yes"</literal> attribute.
119 * It is also possible to specify message context and translator comments,
120 * using the context and comments attributes. To make use of this, the
121 * #GtkBuilder must have been given the gettext domain to use.
124 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
127 GSimpleActionGroup parent_instance;
129 } GtkApplicationWindowActions;
131 static GType gtk_application_window_actions_get_type (void);
132 static void gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
133 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
134 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
137 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
138 const gchar *action_name,
140 GVariant *platform_data)
142 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
143 GApplication *application;
144 GApplicationClass *class;
146 application = G_APPLICATION (gtk_window_get_application (actions->window));
147 class = G_APPLICATION_GET_CLASS (application);
149 class->before_emit (application, platform_data);
150 g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
151 class->after_emit (application, platform_data);
155 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
156 const gchar *action_name,
158 GVariant *platform_data)
160 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
161 GApplication *application;
162 GApplicationClass *class;
164 application = G_APPLICATION (gtk_window_get_application (actions->window));
165 class = G_APPLICATION_GET_CLASS (application);
167 class->before_emit (application, platform_data);
168 g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
169 class->after_emit (application, platform_data);
173 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
178 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
180 iface->activate_action_full = gtk_application_window_actions_activate_action_full;
181 iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
185 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
189 static GSimpleActionGroup *
190 gtk_application_window_actions_new (GtkApplicationWindow *window)
192 GtkApplicationWindowActions *actions;
194 actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
195 actions->window = GTK_WINDOW (window);
197 return G_SIMPLE_ACTION_GROUP (actions);
200 /* Now onto GtkApplicationWindow... */
202 struct _GtkApplicationWindowPrivate
204 GSimpleActionGroup *actions;
205 GActionObservable *muxer;
206 gboolean muxer_initialised;
208 GtkAccelGroup *accels;
209 GSList *accel_closures;
211 GMenu *app_menu_section;
212 GMenu *menubar_section;
213 gboolean show_menubar;
215 GDBusConnection *session;
221 gtk_application_window_update_menubar (GtkApplicationWindow *window)
223 gboolean should_have_menubar;
224 gboolean have_menubar;
226 have_menubar = window->priv->menubar != NULL;
228 should_have_menubar = window->priv->show_menubar &&
229 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
230 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
232 if (have_menubar && !should_have_menubar)
234 gtk_widget_unparent (window->priv->menubar);
235 window->priv->menubar = NULL;
237 gtk_widget_queue_resize (GTK_WIDGET (window));
240 if (!have_menubar && should_have_menubar)
244 combined = g_menu_new ();
245 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
246 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
248 window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->muxer, window->priv->accels);
249 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
250 gtk_widget_show_all (window->priv->menubar);
251 g_object_unref (combined);
253 gtk_widget_queue_resize (GTK_WIDGET (window));
258 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
259 GtkSettings *settings)
261 gboolean shown_by_shell;
263 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
267 /* the shell shows it, so don't show it locally */
268 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
269 g_menu_remove (window->priv->app_menu_section, 0);
273 /* the shell does not show it, so make sure we show it */
274 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
276 GMenuModel *app_menu;
278 app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
280 if (app_menu != NULL)
284 name = g_get_application_name ();
285 if (name == g_get_prgname ())
286 name = _("Application");
287 g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
294 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
295 GtkSettings *settings)
297 gboolean shown_by_shell;
299 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
303 /* the shell shows it, so don't show it locally */
304 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
305 g_menu_remove (window->priv->menubar_section, 0);
309 /* the shell does not show it, so make sure we show it */
310 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
314 menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
317 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
329 accel_activate (GClosure *closure,
330 GValue *return_value,
331 guint n_param_values,
332 const GValue *param_values,
333 gpointer invocation_hint,
334 gpointer marshal_data)
336 AccelClosure *aclosure = (AccelClosure*)closure;
337 GActionGroup *actions;
339 actions = G_ACTION_GROUP (closure->data);
340 if (g_action_group_get_action_enabled (actions, aclosure->action_name))
342 g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
344 /* we handled the accelerator */
345 g_value_set_boolean (return_value, TRUE);
350 free_accel_closures (GtkApplicationWindow *window)
354 for (l = window->priv->accel_closures; l; l = l->next)
356 AccelClosure *closure = l->data;
358 gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
360 g_object_unref (closure->closure.data);
361 if (closure->parameter)
362 g_variant_unref (closure->parameter);
363 g_free (closure->action_name);
364 g_closure_invalidate (&closure->closure);
365 g_closure_unref (&closure->closure);
367 g_slist_free (window->priv->accel_closures);
368 window->priv->accel_closures = NULL;
372 GtkApplicationWindow *window;
373 GActionGroup *actions;
376 /* Hack. We iterate over the accel map instead of the actions,
377 * in order to pull the parameters out of accel map entries
380 add_accel_closure (gpointer data,
381 const gchar *accel_path,
383 GdkModifierType accel_mods,
387 GtkApplicationWindow *window = d->window;
388 GActionGroup *actions = d->actions;
393 AccelClosure *closure;
398 if (!g_str_has_prefix (accel_path, "<GAction>/"))
401 path = accel_path + strlen ("<GAction>/");
402 p = strchr (path, '/');
405 action_name = g_strndup (path, p - path);
406 parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
408 g_warning ("Failed to parse parameter from '%s'\n", accel_path);
412 action_name = g_strdup (path);
416 if (g_action_group_has_action (actions, action_name))
418 closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
419 g_closure_set_marshal (&closure->closure, accel_activate);
421 closure->action_name = g_strdup (action_name);
422 closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
424 window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
425 g_closure_sink (&closure->closure);
427 gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
430 g_free (action_name);
434 gtk_application_window_update_accels (GtkApplicationWindow *window)
438 free_accel_closures (window);
440 data.window = window;
441 data.actions = G_ACTION_GROUP (window->priv->muxer);
443 gtk_accel_map_foreach (&data, add_accel_closure);
447 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
451 GtkApplicationWindow *window = user_data;
453 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
454 gtk_application_window_update_menubar (window);
458 gtk_application_window_shell_shows_menubar_changed (GObject *object,
462 GtkApplicationWindow *window = user_data;
464 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
465 gtk_application_window_update_menubar (window);
469 gtk_application_window_list_actions (GActionGroup *group)
471 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
473 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
477 gtk_application_window_query_action (GActionGroup *group,
478 const gchar *action_name,
480 const GVariantType **parameter_type,
481 const GVariantType **state_type,
482 GVariant **state_hint,
485 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
487 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
488 action_name, enabled, parameter_type, state_type, state_hint, state);
492 gtk_application_window_activate_action (GActionGroup *group,
493 const gchar *action_name,
496 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
498 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
502 gtk_application_window_change_action_state (GActionGroup *group,
503 const gchar *action_name,
506 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
508 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
512 gtk_application_window_lookup_action (GActionMap *action_map,
513 const gchar *action_name)
515 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
517 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
521 gtk_application_window_add_action (GActionMap *action_map,
524 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
526 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
530 gtk_application_window_remove_action (GActionMap *action_map,
531 const gchar *action_name)
533 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
535 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
539 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
541 iface->list_actions = gtk_application_window_list_actions;
542 iface->query_action = gtk_application_window_query_action;
543 iface->activate_action = gtk_application_window_activate_action;
544 iface->change_action_state = gtk_application_window_change_action_state;
548 gtk_application_window_map_iface_init (GActionMapInterface *iface)
550 iface->lookup_action = gtk_application_window_lookup_action;
551 iface->add_action = gtk_application_window_add_action;
552 iface->remove_action = gtk_application_window_remove_action;
555 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
556 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
557 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
564 static GParamSpec *gtk_application_window_properties[N_PROPS];
567 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
568 gint *minimum_height,
569 gint *natural_height)
571 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
573 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
574 ->get_preferred_height (widget, minimum_height, natural_height);
576 if (window->priv->menubar != NULL)
578 gint menubar_min_height, menubar_nat_height;
580 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
581 *minimum_height += menubar_min_height;
582 *natural_height += menubar_nat_height;
587 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
589 gint *minimum_height,
590 gint *natural_height)
592 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
594 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
595 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
597 if (window->priv->menubar != NULL)
599 gint menubar_min_height, menubar_nat_height;
601 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
602 *minimum_height += menubar_min_height;
603 *natural_height += menubar_nat_height;
608 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
612 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
614 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
615 ->get_preferred_width (widget, minimum_width, natural_width);
617 if (window->priv->menubar != NULL)
619 gint menubar_min_width, menubar_nat_width;
621 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
622 *minimum_width = MAX (*minimum_width, menubar_min_width);
623 *natural_width = MAX (*natural_width, menubar_nat_width);
628 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
633 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
636 if (window->priv->menubar != NULL)
637 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
641 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
642 ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
644 if (window->priv->menubar != NULL)
646 gint menubar_min_width, menubar_nat_width;
648 gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
649 *minimum_width = MAX (*minimum_width, menubar_min_width);
650 *natural_width = MAX (*natural_width, menubar_nat_width);
655 gtk_application_window_real_size_allocate (GtkWidget *widget,
656 GtkAllocation *allocation)
658 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
660 if (window->priv->menubar != NULL)
662 GtkAllocation menubar_allocation = *allocation;
666 _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
668 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
670 menubar_allocation.height = menubar_height;
671 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
673 child = gtk_bin_get_child (GTK_BIN (window));
674 if (child != NULL && gtk_widget_get_visible (child))
676 GtkAllocation child_allocation = *allocation;
679 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
680 child_allocation.x += border_width;
681 child_allocation.y += border_width + menubar_height;
682 child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
683 child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
685 gtk_widget_size_allocate (child, &child_allocation);
689 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
690 ->size_allocate (widget, allocation);
694 gtk_application_window_real_realize (GtkWidget *widget)
696 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
697 GtkApplication *application;
698 GtkSettings *settings;
700 application = gtk_window_get_application (GTK_WINDOW (window));
701 settings = gtk_widget_get_settings (widget);
703 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
704 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
705 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
706 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
708 if (!window->priv->muxer_initialised)
710 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "app", G_ACTION_GROUP (application));
711 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "win", G_ACTION_GROUP (window));
712 window->priv->muxer_initialised = TRUE;
715 gtk_application_window_update_shell_shows_app_menu (window, settings);
716 gtk_application_window_update_shell_shows_menubar (window, settings);
717 gtk_application_window_update_menubar (window);
718 gtk_application_window_update_accels (window);
720 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
723 #ifdef GDK_WINDOWING_X11
725 GdkWindow *gdkwindow;
727 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
729 if (GDK_IS_X11_WINDOW (gdkwindow))
731 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
732 g_application_get_application_id (G_APPLICATION (application)));
734 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
735 g_dbus_connection_get_unique_name (window->priv->session));
737 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
738 gtk_application_get_dbus_object_path (application));
740 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
741 window->priv->object_path);
743 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
744 gtk_application_get_app_menu_object_path (application));
746 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
747 gtk_application_get_menubar_object_path (application));
754 gtk_application_window_real_unrealize (GtkWidget *widget)
756 GtkSettings *settings;
758 settings = gtk_widget_get_settings (widget);
760 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
761 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
763 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
764 ->unrealize (widget);
768 gtk_application_window_publish (GtkApplicationWindow *window,
769 GDBusConnection *session,
770 const gchar *object_path)
772 g_assert (window->priv->session == NULL);
773 g_assert (window->priv->export_id == 0);
774 g_assert (window->priv->object_path == NULL);
776 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
777 G_ACTION_GROUP (window->priv->actions),
780 if (window->priv->export_id == 0)
783 window->priv->session = session;
784 window->priv->object_path = g_strdup (object_path);
790 gtk_application_window_unpublish (GtkApplicationWindow *window)
792 g_assert (window->priv->session != NULL);
793 g_assert (window->priv->export_id != 0);
794 g_assert (window->priv->object_path != NULL);
796 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
797 window->priv->session = NULL;
798 window->priv->export_id = 0;
800 g_free (window->priv->object_path);
801 window->priv->object_path = NULL;
805 gtk_application_window_real_map (GtkWidget *widget)
807 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
809 /* XXX could eliminate this by tweaking gtk_window_map */
810 if (window->priv->menubar)
811 gtk_widget_map (window->priv->menubar);
813 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
818 gtk_application_window_real_forall_internal (GtkContainer *container,
819 gboolean include_internal,
820 GtkCallback callback,
823 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
825 if (window->priv->menubar)
826 callback (window->priv->menubar, user_data);
828 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
829 ->forall (container, include_internal, callback, user_data);
834 gtk_application_window_get_property (GObject *object,
839 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
843 case PROP_SHOW_MENUBAR:
844 g_value_set_boolean (value, window->priv->show_menubar);
848 g_assert_not_reached ();
853 gtk_application_window_set_property (GObject *object,
858 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
862 case PROP_SHOW_MENUBAR:
863 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
867 g_assert_not_reached ();
872 gtk_application_window_dispose (GObject *object)
874 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
876 if (window->priv->menubar)
878 gtk_widget_unparent (window->priv->menubar);
879 window->priv->menubar = NULL;
882 free_accel_closures (window);
884 g_clear_object (&window->priv->app_menu_section);
885 g_clear_object (&window->priv->menubar_section);
886 g_clear_object (&window->priv->actions);
887 g_clear_object (&window->priv->accels);
888 g_clear_object (&window->priv->muxer);
890 G_OBJECT_CLASS (gtk_application_window_parent_class)
895 gtk_application_window_init (GtkApplicationWindow *window)
897 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
899 window->priv->actions = gtk_application_window_actions_new (window);
900 window->priv->app_menu_section = g_menu_new ();
901 window->priv->menubar_section = g_menu_new ();
902 window->priv->accels = gtk_accel_group_new ();
903 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
905 /* window->priv->actions is the one and only ref on the group, so when
906 * we dispose, the action group will die, disconnecting all signals.
908 g_signal_connect_swapped (window->priv->actions, "action-added",
909 G_CALLBACK (g_action_group_action_added), window);
910 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
911 G_CALLBACK (g_action_group_action_enabled_changed), window);
912 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
913 G_CALLBACK (g_action_group_action_state_changed), window);
914 g_signal_connect_swapped (window->priv->actions, "action-removed",
915 G_CALLBACK (g_action_group_action_removed), window);
917 window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
921 gtk_application_window_class_init (GtkApplicationWindowClass *class)
923 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
924 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
925 GObjectClass *object_class = G_OBJECT_CLASS (class);
927 container_class->forall = gtk_application_window_real_forall_internal;
928 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
929 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
930 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
931 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
932 widget_class->size_allocate = gtk_application_window_real_size_allocate;
933 widget_class->realize = gtk_application_window_real_realize;
934 widget_class->unrealize = gtk_application_window_real_unrealize;
935 widget_class->map = gtk_application_window_real_map;
936 object_class->get_property = gtk_application_window_get_property;
937 object_class->set_property = gtk_application_window_set_property;
938 object_class->dispose = gtk_application_window_dispose;
941 * GtkApplicationWindow:show-menubar:
943 * If this property is %TRUE, the window will display a menubar
944 * that includes the app menu and menubar, unless these are
945 * shown by the desktop shell. See gtk_application_set_app_menu()
946 * and gtk_application_set_menubar().
948 * If %FALSE, the window will not display a menubar, regardless
949 * of whether the desktop shell is showing the menus or not.
951 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
952 g_param_spec_boolean ("show-menubar",
953 P_("Show a menubar"),
954 P_("TRUE if the window should show a "
955 "menubar at the top of the window"),
956 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
957 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
958 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
962 * gtk_application_window_new:
963 * @application: a #GtkApplication
965 * Creates a new #GtkApplicationWindow.
967 * Returns: a newly created #GtkApplicationWindow
972 gtk_application_window_new (GtkApplication *application)
974 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
976 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
977 "application", application,
982 * gtk_application_window_get_show_menubar:
983 * @window: a #GtkApplicationWindow
985 * Returns whether the window will display a menubar for the app menu
986 * and menubar as needed.
988 * Returns: %TRUE if @window will display a menubar when needed
993 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
995 return window->priv->show_menubar;
999 * gtk_application_window_set_show_menubar:
1000 * @window: a #GtkApplicationWindow
1001 * @show_menubar: whether to show a menubar when needed
1003 * Sets whether the window will display a menubar for the app menu
1004 * and menubar as needed.
1009 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
1010 gboolean show_menubar)
1012 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
1014 show_menubar = !!show_menubar;
1016 if (window->priv->show_menubar != show_menubar)
1018 window->priv->show_menubar = show_menubar;
1020 gtk_application_window_update_menubar (window);
1022 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1026 GSimpleActionObserver *
1027 gtk_application_window_create_observer (GtkApplicationWindow *window,
1028 const gchar *action_name,
1031 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
1033 return g_simple_action_observer_new (window->priv->muxer, action_name, target);
1037 gtk_application_window_get_observable (GtkApplicationWindow *window)
1039 return G_ACTION_OBSERVABLE (window->priv->muxer);
1043 gtk_application_window_get_accel_group (GtkApplicationWindow *window)
1045 return window->priv->accels;