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 "gtkwindowprivate.h"
26 #include "gtkmodelmenu.h"
27 #include "gactionmuxer.h"
28 #include "gtkaccelgroup.h"
29 #include "gtkaccelmap.h"
31 #include "gtksettings.h"
34 #ifdef GDK_WINDOWING_X11
35 #include <gdk/x11/gdkx.h>
39 #include <gio/gdesktopappinfo.h>
43 * SECTION:gtkapplicationwindow
44 * @title: GtkApplicationWindow
45 * @short_description: GtkWindow subclass with GtkApplication support
47 * GtkApplicationWindow is a #GtkWindow subclass that offers some
48 * extra functionality for better integration with #GtkApplication
49 * features. Notably, it can handle both the application menu as well
50 * as the menubar. See gtk_application_set_app_menu() and
51 * gtk_application_set_menubar().
53 * This class implements the #GActionGroup and #GActionMap interfaces,
54 * to let you add window-specific actions that will be exported by the
55 * associated #GtkApplication, together with its application-wide
56 * actions. Window-specific actions are prefixed with the "win."
57 * prefix and application-wide actions are prefixed with the "app."
58 * prefix. Actions must be addressed with the prefixed name when
59 * referring to them from a #GMenuModel.
61 * Note that widgets that are placed inside a GtkApplicationWindow
62 * can also activate these actions, if they implement the
63 * GtkActionable interface.
65 * As with #GtkApplication, the GDK lock will be acquired when
66 * processing actions arriving from other processes and should therefore
67 * be held when activating actions locally (if GDK threads are enabled).
69 * The settings #GtkSettings:gtk-shell-shows-app-menu and
70 * #GtkSettings:gtk-shell-shows-menubar tell GTK+ whether the
71 * desktop environment is showing the application menu and menubar
72 * models outside the application as part of the desktop shell.
73 * For instance, on OS X, both menus will be displayed remotely;
74 * on Windows neither will be. gnome-shell (starting with version 3.4)
75 * will display the application menu, but not the menubar.
77 * If the desktop environment does not display the menubar, then
78 * #GApplicationWindow will automatically show a #GtkMenubar for it.
79 * (see the #GtkApplication docs for some screenshots of how this
80 * looks on different platforms).
81 * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
82 * property. If the desktop environment does not display the application
83 * menu, then it will automatically be included in the menubar.
85 * <example><title>A GtkApplicationWindow with a menubar</title>
86 * <programlisting><![CDATA[
87 * app = gtk_application_new ();
89 * builder = gtk_builder_new ();
90 * gtk_builder_add_from_string (builder,
92 * " <menu id='menubar'>"
93 * " <submenu label='_Edit'>"
94 * " <item label='_Copy' action='win.copy'/>"
95 * " <item label='_Paste' action='win.paste'/>"
99 * gtk_application_set_menubar (G_APPLICATION (app),
100 * G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
101 * g_object_unref (builder);
105 * window = gtk_application_window_new (app);
110 * The XML format understood by #GtkBuilder for #GMenuModel consists
111 * of a toplevel <tag class="starttag">menu</tag> element, which contains
112 * one or more <tag class="starttag">item</tag> elements. Each
113 * <tag class="starttag">item</tag> element contains
114 * <tag class="starttag">attribute</tag> and <tag class="starttag">link</tag>
115 * elements with a mandatory name attribute.
116 * <tag class="starttag">link</tag> elements have the same content
117 * model as <tag class="starttag">menu</tag>.
119 * Attribute values can be translated using gettext, like other #GtkBuilder
120 * content. <tag class="starttag">attribute</tag> elements can be marked for
121 * translation with a <literal>translatable="yes"</literal> attribute.
122 * It is also possible to specify message context and translator comments,
123 * using the context and comments attributes. To make use of this, the
124 * #GtkBuilder must have been given the gettext domain to use.
127 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
130 GSimpleActionGroup parent_instance;
132 } GtkApplicationWindowActions;
134 static GType gtk_application_window_actions_get_type (void);
135 static void gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
136 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
137 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
140 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
141 const gchar *action_name,
143 GVariant *platform_data)
145 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
146 GApplication *application;
147 GApplicationClass *class;
149 application = G_APPLICATION (gtk_window_get_application (actions->window));
150 class = G_APPLICATION_GET_CLASS (application);
152 class->before_emit (application, platform_data);
153 g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
154 class->after_emit (application, platform_data);
158 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
159 const gchar *action_name,
161 GVariant *platform_data)
163 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
164 GApplication *application;
165 GApplicationClass *class;
167 application = G_APPLICATION (gtk_window_get_application (actions->window));
168 class = G_APPLICATION_GET_CLASS (application);
170 class->before_emit (application, platform_data);
171 g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
172 class->after_emit (application, platform_data);
176 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
181 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
183 iface->activate_action_full = gtk_application_window_actions_activate_action_full;
184 iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
188 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
192 static GSimpleActionGroup *
193 gtk_application_window_actions_new (GtkApplicationWindow *window)
195 GtkApplicationWindowActions *actions;
197 actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
198 actions->window = GTK_WINDOW (window);
200 return G_SIMPLE_ACTION_GROUP (actions);
203 /* Now onto GtkApplicationWindow... */
205 struct _GtkApplicationWindowPrivate
207 GSimpleActionGroup *actions;
208 GActionObservable *muxer;
209 gboolean muxer_initialised;
211 GtkAccelGroup *accels;
212 GSList *accel_closures;
214 GMenu *app_menu_section;
215 GMenu *menubar_section;
216 gboolean show_menubar;
218 GDBusConnection *session;
226 gtk_application_window_update_menubar (GtkApplicationWindow *window)
228 gboolean should_have_menubar;
229 gboolean have_menubar;
231 have_menubar = window->priv->menubar != NULL;
233 should_have_menubar = window->priv->show_menubar &&
234 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
235 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
237 if (have_menubar && !should_have_menubar)
239 gtk_widget_unparent (window->priv->menubar);
240 window->priv->menubar = NULL;
242 gtk_widget_queue_resize (GTK_WIDGET (window));
245 if (!have_menubar && should_have_menubar)
249 combined = g_menu_new ();
250 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
251 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
253 window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->muxer, window->priv->accels);
254 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
255 gtk_widget_show_all (window->priv->menubar);
256 g_object_unref (combined);
258 gtk_widget_queue_resize (GTK_WIDGET (window));
263 gtk_application_window_get_app_desktop_name (void)
265 gchar *retval = NULL;
268 GDesktopAppInfo *app_info;
269 const gchar *app_name = NULL;
272 desktop_file = g_strconcat (g_get_prgname (), ".desktop", NULL);
273 app_info = g_desktop_app_info_new (desktop_file);
274 g_free (desktop_file);
276 if (app_info != NULL)
277 app_name = g_app_info_get_name (G_APP_INFO (app_info));
279 if (app_name != NULL)
280 retval = g_strdup (app_name);
282 g_clear_object (&app_info);
283 #endif /* HAVE_GIO_UNIX */
289 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
290 GtkSettings *settings)
292 gboolean shown_by_shell;
294 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
298 /* the shell shows it, so don't show it locally */
299 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
300 g_menu_remove (window->priv->app_menu_section, 0);
304 /* the shell does not show it, so make sure we show it */
305 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
307 GMenuModel *app_menu;
309 app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
311 if (app_menu != NULL)
313 const gchar *app_name;
316 app_name = g_get_application_name ();
317 if (app_name != g_get_prgname ())
319 /* the app has set its application name, use it */
320 name = g_strdup (app_name);
324 /* get the name from .desktop file */
325 name = gtk_application_window_get_app_desktop_name ();
327 name = g_strdup (_("Application"));
330 g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
338 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
339 GtkSettings *settings)
341 gboolean shown_by_shell;
343 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
347 /* the shell shows it, so don't show it locally */
348 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
349 g_menu_remove (window->priv->menubar_section, 0);
353 /* the shell does not show it, so make sure we show it */
354 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
358 menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
361 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
373 accel_activate (GClosure *closure,
374 GValue *return_value,
375 guint n_param_values,
376 const GValue *param_values,
377 gpointer invocation_hint,
378 gpointer marshal_data)
380 AccelClosure *aclosure = (AccelClosure*)closure;
381 GActionGroup *actions;
383 actions = G_ACTION_GROUP (closure->data);
384 if (g_action_group_get_action_enabled (actions, aclosure->action_name))
386 g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
388 /* we handled the accelerator */
389 g_value_set_boolean (return_value, TRUE);
394 free_accel_closures (GtkApplicationWindow *window)
398 for (l = window->priv->accel_closures; l; l = l->next)
400 AccelClosure *closure = l->data;
402 gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
404 g_object_unref (closure->closure.data);
405 if (closure->parameter)
406 g_variant_unref (closure->parameter);
407 g_free (closure->action_name);
408 g_closure_invalidate (&closure->closure);
409 g_closure_unref (&closure->closure);
411 g_slist_free (window->priv->accel_closures);
412 window->priv->accel_closures = NULL;
416 GtkApplicationWindow *window;
417 GActionGroup *actions;
420 /* Hack. We iterate over the accel map instead of the actions,
421 * in order to pull the parameters out of accel map entries
424 add_accel_closure (gpointer data,
425 const gchar *accel_path,
427 GdkModifierType accel_mods,
431 GtkApplicationWindow *window = d->window;
432 GActionGroup *actions = d->actions;
437 AccelClosure *closure;
442 if (!g_str_has_prefix (accel_path, "<GAction>/"))
445 path = accel_path + strlen ("<GAction>/");
446 p = strchr (path, '/');
449 action_name = g_strndup (path, p - path);
450 parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
452 g_warning ("Failed to parse parameter from '%s'\n", accel_path);
456 action_name = g_strdup (path);
460 if (g_action_group_has_action (actions, action_name))
462 closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
463 g_closure_set_marshal (&closure->closure, accel_activate);
465 closure->action_name = g_strdup (action_name);
466 closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
468 window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
469 g_closure_sink (&closure->closure);
471 gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
474 g_free (action_name);
478 gtk_application_window_update_accels (GtkApplicationWindow *window)
482 free_accel_closures (window);
484 data.window = window;
485 data.actions = G_ACTION_GROUP (window->priv->muxer);
487 gtk_accel_map_foreach (&data, add_accel_closure);
491 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
495 GtkApplicationWindow *window = user_data;
497 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
498 gtk_application_window_update_menubar (window);
502 gtk_application_window_shell_shows_menubar_changed (GObject *object,
506 GtkApplicationWindow *window = user_data;
508 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
509 gtk_application_window_update_menubar (window);
513 gtk_application_window_list_actions (GActionGroup *group)
515 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
517 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
521 gtk_application_window_query_action (GActionGroup *group,
522 const gchar *action_name,
524 const GVariantType **parameter_type,
525 const GVariantType **state_type,
526 GVariant **state_hint,
529 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
531 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
532 action_name, enabled, parameter_type, state_type, state_hint, state);
536 gtk_application_window_activate_action (GActionGroup *group,
537 const gchar *action_name,
540 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
542 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
546 gtk_application_window_change_action_state (GActionGroup *group,
547 const gchar *action_name,
550 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
552 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
556 gtk_application_window_lookup_action (GActionMap *action_map,
557 const gchar *action_name)
559 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
561 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
565 gtk_application_window_add_action (GActionMap *action_map,
568 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
570 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
574 gtk_application_window_remove_action (GActionMap *action_map,
575 const gchar *action_name)
577 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
579 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
583 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
585 iface->list_actions = gtk_application_window_list_actions;
586 iface->query_action = gtk_application_window_query_action;
587 iface->activate_action = gtk_application_window_activate_action;
588 iface->change_action_state = gtk_application_window_change_action_state;
592 gtk_application_window_map_iface_init (GActionMapInterface *iface)
594 iface->lookup_action = gtk_application_window_lookup_action;
595 iface->add_action = gtk_application_window_add_action;
596 iface->remove_action = gtk_application_window_remove_action;
599 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
600 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
601 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
608 static GParamSpec *gtk_application_window_properties[N_PROPS];
611 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
612 gint *minimum_height,
613 gint *natural_height)
615 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
617 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
618 ->get_preferred_height (widget, minimum_height, natural_height);
620 if (window->priv->menubar != NULL)
622 gint menubar_min_height, menubar_nat_height;
624 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
625 *minimum_height += menubar_min_height;
626 *natural_height += menubar_nat_height;
631 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
633 gint *minimum_height,
634 gint *natural_height)
636 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
638 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
639 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
641 if (window->priv->menubar != NULL)
643 gint menubar_min_height, menubar_nat_height;
645 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
646 *minimum_height += menubar_min_height;
647 *natural_height += menubar_nat_height;
652 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
656 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
658 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
659 ->get_preferred_width (widget, minimum_width, natural_width);
661 if (window->priv->menubar != NULL)
663 gint menubar_min_width, menubar_nat_width;
665 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
666 *minimum_width = MAX (*minimum_width, menubar_min_width);
667 *natural_width = MAX (*natural_width, menubar_nat_width);
672 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
677 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
680 if (window->priv->menubar != NULL)
681 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
685 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
686 ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
688 if (window->priv->menubar != NULL)
690 gint menubar_min_width, menubar_nat_width;
692 gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
693 *minimum_width = MAX (*minimum_width, menubar_min_width);
694 *natural_width = MAX (*natural_width, menubar_nat_width);
699 gtk_application_window_real_size_allocate (GtkWidget *widget,
700 GtkAllocation *allocation)
702 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
704 if (window->priv->menubar != NULL)
706 GtkAllocation menubar_allocation = *allocation;
710 _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
712 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
714 menubar_allocation.height = menubar_height;
715 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
717 child = gtk_bin_get_child (GTK_BIN (window));
718 if (child != NULL && gtk_widget_get_visible (child))
720 GtkAllocation child_allocation = *allocation;
723 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
724 child_allocation.x += border_width;
725 child_allocation.y += border_width + menubar_height;
726 child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
727 child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
729 gtk_widget_size_allocate (child, &child_allocation);
733 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
734 ->size_allocate (widget, allocation);
738 gtk_application_window_real_realize (GtkWidget *widget)
740 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
741 GtkApplication *application;
742 GtkSettings *settings;
744 application = gtk_window_get_application (GTK_WINDOW (window));
745 settings = gtk_widget_get_settings (widget);
747 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
748 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
749 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
750 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
752 if (!window->priv->muxer_initialised)
754 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "app", G_ACTION_GROUP (application));
755 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "win", G_ACTION_GROUP (window));
756 window->priv->muxer_initialised = TRUE;
759 gtk_application_window_update_shell_shows_app_menu (window, settings);
760 gtk_application_window_update_shell_shows_menubar (window, settings);
761 gtk_application_window_update_menubar (window);
762 gtk_application_window_update_accels (window);
764 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
767 #ifdef GDK_WINDOWING_X11
769 GdkWindow *gdkwindow;
771 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
773 if (GDK_IS_X11_WINDOW (gdkwindow) && window->priv->session)
775 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
776 g_application_get_application_id (G_APPLICATION (application)));
778 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
779 g_dbus_connection_get_unique_name (window->priv->session));
781 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
782 g_application_get_dbus_object_path (G_APPLICATION (application)));
784 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
785 window->priv->object_path);
787 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
788 gtk_application_get_app_menu_object_path (application));
790 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
791 gtk_application_get_menubar_object_path (application));
798 gtk_application_window_real_unrealize (GtkWidget *widget)
800 GtkSettings *settings;
802 settings = gtk_widget_get_settings (widget);
804 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
805 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
807 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
808 ->unrealize (widget);
812 gtk_application_window_publish (GtkApplicationWindow *window,
813 GDBusConnection *session,
814 const gchar *object_path,
817 g_assert (window->priv->session == NULL);
818 g_assert (window->priv->export_id == 0);
819 g_assert (window->priv->object_path == NULL);
820 g_assert (window->priv->id == 0);
822 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
823 G_ACTION_GROUP (window->priv->actions),
826 if (window->priv->export_id == 0)
829 window->priv->session = session;
830 window->priv->object_path = g_strdup (object_path);
831 window->priv->id = object_id;
837 gtk_application_window_unpublish (GtkApplicationWindow *window)
839 g_assert (window->priv->session != NULL);
840 g_assert (window->priv->export_id != 0);
841 g_assert (window->priv->object_path != NULL);
842 g_assert (window->priv->id != 0);
844 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
845 window->priv->session = NULL;
846 window->priv->export_id = 0;
847 window->priv->id = 0;
849 g_free (window->priv->object_path);
850 window->priv->object_path = NULL;
854 gtk_application_window_real_map (GtkWidget *widget)
856 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
858 /* XXX could eliminate this by tweaking gtk_window_map */
859 if (window->priv->menubar)
860 gtk_widget_map (window->priv->menubar);
862 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
867 gtk_application_window_real_forall_internal (GtkContainer *container,
868 gboolean include_internal,
869 GtkCallback callback,
872 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
874 if (window->priv->menubar)
875 callback (window->priv->menubar, user_data);
877 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
878 ->forall (container, include_internal, callback, user_data);
883 gtk_application_window_get_property (GObject *object,
888 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
892 case PROP_SHOW_MENUBAR:
893 g_value_set_boolean (value, window->priv->show_menubar);
897 g_assert_not_reached ();
902 gtk_application_window_set_property (GObject *object,
907 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
911 case PROP_SHOW_MENUBAR:
912 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
916 g_assert_not_reached ();
921 gtk_application_window_dispose (GObject *object)
923 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
925 if (window->priv->menubar)
927 gtk_widget_unparent (window->priv->menubar);
928 window->priv->menubar = NULL;
931 free_accel_closures (window);
933 g_clear_object (&window->priv->app_menu_section);
934 g_clear_object (&window->priv->menubar_section);
935 g_clear_object (&window->priv->actions);
936 g_clear_object (&window->priv->accels);
937 g_clear_object (&window->priv->muxer);
939 G_OBJECT_CLASS (gtk_application_window_parent_class)
944 gtk_application_window_init (GtkApplicationWindow *window)
946 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
948 window->priv->actions = gtk_application_window_actions_new (window);
949 window->priv->app_menu_section = g_menu_new ();
950 window->priv->menubar_section = g_menu_new ();
951 window->priv->accels = gtk_accel_group_new ();
952 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
954 /* window->priv->actions is the one and only ref on the group, so when
955 * we dispose, the action group will die, disconnecting all signals.
957 g_signal_connect_swapped (window->priv->actions, "action-added",
958 G_CALLBACK (g_action_group_action_added), window);
959 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
960 G_CALLBACK (g_action_group_action_enabled_changed), window);
961 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
962 G_CALLBACK (g_action_group_action_state_changed), window);
963 g_signal_connect_swapped (window->priv->actions, "action-removed",
964 G_CALLBACK (g_action_group_action_removed), window);
966 window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
970 gtk_application_window_class_init (GtkApplicationWindowClass *class)
972 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
973 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
974 GObjectClass *object_class = G_OBJECT_CLASS (class);
976 container_class->forall = gtk_application_window_real_forall_internal;
977 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
978 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
979 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
980 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
981 widget_class->size_allocate = gtk_application_window_real_size_allocate;
982 widget_class->realize = gtk_application_window_real_realize;
983 widget_class->unrealize = gtk_application_window_real_unrealize;
984 widget_class->map = gtk_application_window_real_map;
985 object_class->get_property = gtk_application_window_get_property;
986 object_class->set_property = gtk_application_window_set_property;
987 object_class->dispose = gtk_application_window_dispose;
990 * GtkApplicationWindow:show-menubar:
992 * If this property is %TRUE, the window will display a menubar
993 * that includes the app menu and menubar, unless these are
994 * shown by the desktop shell. See gtk_application_set_app_menu()
995 * and gtk_application_set_menubar().
997 * If %FALSE, the window will not display a menubar, regardless
998 * of whether the desktop shell is showing the menus or not.
1000 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
1001 g_param_spec_boolean ("show-menubar",
1002 P_("Show a menubar"),
1003 P_("TRUE if the window should show a "
1004 "menubar at the top of the window"),
1005 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
1006 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
1007 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
1011 * gtk_application_window_new:
1012 * @application: a #GtkApplication
1014 * Creates a new #GtkApplicationWindow.
1016 * Returns: a newly created #GtkApplicationWindow
1021 gtk_application_window_new (GtkApplication *application)
1023 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1025 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
1026 "application", application,
1031 * gtk_application_window_get_show_menubar:
1032 * @window: a #GtkApplicationWindow
1034 * Returns whether the window will display a menubar for the app menu
1035 * and menubar as needed.
1037 * Returns: %TRUE if @window will display a menubar when needed
1042 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
1044 return window->priv->show_menubar;
1048 * gtk_application_window_set_show_menubar:
1049 * @window: a #GtkApplicationWindow
1050 * @show_menubar: whether to show a menubar when needed
1052 * Sets whether the window will display a menubar for the app menu
1053 * and menubar as needed.
1058 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
1059 gboolean show_menubar)
1061 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
1063 show_menubar = !!show_menubar;
1065 if (window->priv->show_menubar != show_menubar)
1067 window->priv->show_menubar = show_menubar;
1069 gtk_application_window_update_menubar (window);
1071 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1075 GSimpleActionObserver *
1076 gtk_application_window_create_observer (GtkApplicationWindow *window,
1077 const gchar *action_name,
1080 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
1082 return g_simple_action_observer_new (window->priv->muxer, action_name, target);
1086 gtk_application_window_get_observable (GtkApplicationWindow *window)
1088 return G_ACTION_OBSERVABLE (window->priv->muxer);
1092 gtk_application_window_get_accel_group (GtkApplicationWindow *window)
1094 return window->priv->accels;
1098 * gtk_application_window_get_id:
1099 * @window: a #GtkApplicationWindow
1101 * Returns the unique ID of the window. If the window has not yet been added to
1102 * a #GtkApplication, returns <literal>0</literal>.
1104 * Returns: the unique ID for @window, or <literal>0</literal> if the window
1105 * has not yet been added to a #GtkApplication
1110 gtk_application_window_get_id (GtkApplicationWindow *window)
1112 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), 0);
1114 return window->priv->id;