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;
224 gtk_application_window_update_menubar (GtkApplicationWindow *window)
226 gboolean should_have_menubar;
227 gboolean have_menubar;
229 have_menubar = window->priv->menubar != NULL;
231 should_have_menubar = window->priv->show_menubar &&
232 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
233 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
235 if (have_menubar && !should_have_menubar)
237 gtk_widget_unparent (window->priv->menubar);
238 window->priv->menubar = NULL;
240 gtk_widget_queue_resize (GTK_WIDGET (window));
243 if (!have_menubar && should_have_menubar)
247 combined = g_menu_new ();
248 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
249 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
251 window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->muxer, window->priv->accels);
252 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
253 gtk_widget_show_all (window->priv->menubar);
254 g_object_unref (combined);
256 gtk_widget_queue_resize (GTK_WIDGET (window));
261 gtk_application_window_get_app_desktop_name (void)
263 gchar *retval = NULL;
266 GDesktopAppInfo *app_info;
267 const gchar *app_name = NULL;
270 desktop_file = g_strconcat (g_get_prgname (), ".desktop", NULL);
271 app_info = g_desktop_app_info_new (desktop_file);
272 g_free (desktop_file);
274 if (app_info != NULL)
275 app_name = g_app_info_get_name (G_APP_INFO (app_info));
277 if (app_name != NULL)
278 retval = g_strdup (app_name);
280 g_clear_object (&app_info);
281 #endif /* HAVE_GIO_UNIX */
287 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
288 GtkSettings *settings)
290 gboolean shown_by_shell;
292 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
296 /* the shell shows it, so don't show it locally */
297 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
298 g_menu_remove (window->priv->app_menu_section, 0);
302 /* the shell does not show it, so make sure we show it */
303 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
305 GMenuModel *app_menu;
307 app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
309 if (app_menu != NULL)
311 const gchar *app_name;
314 app_name = g_get_application_name ();
315 if (app_name != g_get_prgname ())
317 /* the app has set its application name, use it */
318 name = g_strdup (app_name);
322 /* get the name from .desktop file */
323 name = gtk_application_window_get_app_desktop_name ();
325 name = g_strdup (_("Application"));
328 g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
336 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
337 GtkSettings *settings)
339 gboolean shown_by_shell;
341 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
345 /* the shell shows it, so don't show it locally */
346 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
347 g_menu_remove (window->priv->menubar_section, 0);
351 /* the shell does not show it, so make sure we show it */
352 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
356 menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
359 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
371 accel_activate (GClosure *closure,
372 GValue *return_value,
373 guint n_param_values,
374 const GValue *param_values,
375 gpointer invocation_hint,
376 gpointer marshal_data)
378 AccelClosure *aclosure = (AccelClosure*)closure;
379 GActionGroup *actions;
381 actions = G_ACTION_GROUP (closure->data);
382 if (g_action_group_get_action_enabled (actions, aclosure->action_name))
384 g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
386 /* we handled the accelerator */
387 g_value_set_boolean (return_value, TRUE);
392 free_accel_closures (GtkApplicationWindow *window)
396 for (l = window->priv->accel_closures; l; l = l->next)
398 AccelClosure *closure = l->data;
400 gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
402 g_object_unref (closure->closure.data);
403 if (closure->parameter)
404 g_variant_unref (closure->parameter);
405 g_free (closure->action_name);
406 g_closure_invalidate (&closure->closure);
407 g_closure_unref (&closure->closure);
409 g_slist_free (window->priv->accel_closures);
410 window->priv->accel_closures = NULL;
414 GtkApplicationWindow *window;
415 GActionGroup *actions;
418 /* Hack. We iterate over the accel map instead of the actions,
419 * in order to pull the parameters out of accel map entries
422 add_accel_closure (gpointer data,
423 const gchar *accel_path,
425 GdkModifierType accel_mods,
429 GtkApplicationWindow *window = d->window;
430 GActionGroup *actions = d->actions;
435 AccelClosure *closure;
440 if (!g_str_has_prefix (accel_path, "<GAction>/"))
443 path = accel_path + strlen ("<GAction>/");
444 p = strchr (path, '/');
447 action_name = g_strndup (path, p - path);
448 parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
450 g_warning ("Failed to parse parameter from '%s'\n", accel_path);
454 action_name = g_strdup (path);
458 if (g_action_group_has_action (actions, action_name))
460 closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
461 g_closure_set_marshal (&closure->closure, accel_activate);
463 closure->action_name = g_strdup (action_name);
464 closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
466 window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
467 g_closure_sink (&closure->closure);
469 gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
472 g_free (action_name);
476 gtk_application_window_update_accels (GtkApplicationWindow *window)
480 free_accel_closures (window);
482 data.window = window;
483 data.actions = G_ACTION_GROUP (window->priv->muxer);
485 gtk_accel_map_foreach (&data, add_accel_closure);
489 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
493 GtkApplicationWindow *window = user_data;
495 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
496 gtk_application_window_update_menubar (window);
500 gtk_application_window_shell_shows_menubar_changed (GObject *object,
504 GtkApplicationWindow *window = user_data;
506 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
507 gtk_application_window_update_menubar (window);
511 gtk_application_window_list_actions (GActionGroup *group)
513 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
515 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
519 gtk_application_window_query_action (GActionGroup *group,
520 const gchar *action_name,
522 const GVariantType **parameter_type,
523 const GVariantType **state_type,
524 GVariant **state_hint,
527 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
529 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
530 action_name, enabled, parameter_type, state_type, state_hint, state);
534 gtk_application_window_activate_action (GActionGroup *group,
535 const gchar *action_name,
538 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
540 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
544 gtk_application_window_change_action_state (GActionGroup *group,
545 const gchar *action_name,
548 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
550 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
554 gtk_application_window_lookup_action (GActionMap *action_map,
555 const gchar *action_name)
557 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
559 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
563 gtk_application_window_add_action (GActionMap *action_map,
566 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
568 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
572 gtk_application_window_remove_action (GActionMap *action_map,
573 const gchar *action_name)
575 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
577 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
581 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
583 iface->list_actions = gtk_application_window_list_actions;
584 iface->query_action = gtk_application_window_query_action;
585 iface->activate_action = gtk_application_window_activate_action;
586 iface->change_action_state = gtk_application_window_change_action_state;
590 gtk_application_window_map_iface_init (GActionMapInterface *iface)
592 iface->lookup_action = gtk_application_window_lookup_action;
593 iface->add_action = gtk_application_window_add_action;
594 iface->remove_action = gtk_application_window_remove_action;
597 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
598 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
599 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
606 static GParamSpec *gtk_application_window_properties[N_PROPS];
609 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
610 gint *minimum_height,
611 gint *natural_height)
613 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
615 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
616 ->get_preferred_height (widget, minimum_height, natural_height);
618 if (window->priv->menubar != NULL)
620 gint menubar_min_height, menubar_nat_height;
622 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
623 *minimum_height += menubar_min_height;
624 *natural_height += menubar_nat_height;
629 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
631 gint *minimum_height,
632 gint *natural_height)
634 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
636 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
637 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
639 if (window->priv->menubar != NULL)
641 gint menubar_min_height, menubar_nat_height;
643 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
644 *minimum_height += menubar_min_height;
645 *natural_height += menubar_nat_height;
650 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
654 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
656 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
657 ->get_preferred_width (widget, minimum_width, natural_width);
659 if (window->priv->menubar != NULL)
661 gint menubar_min_width, menubar_nat_width;
663 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
664 *minimum_width = MAX (*minimum_width, menubar_min_width);
665 *natural_width = MAX (*natural_width, menubar_nat_width);
670 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
675 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
678 if (window->priv->menubar != NULL)
679 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
683 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
684 ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
686 if (window->priv->menubar != NULL)
688 gint menubar_min_width, menubar_nat_width;
690 gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
691 *minimum_width = MAX (*minimum_width, menubar_min_width);
692 *natural_width = MAX (*natural_width, menubar_nat_width);
697 gtk_application_window_real_size_allocate (GtkWidget *widget,
698 GtkAllocation *allocation)
700 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
702 if (window->priv->menubar != NULL)
704 GtkAllocation menubar_allocation = *allocation;
708 _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
710 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
712 menubar_allocation.height = menubar_height;
713 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
715 child = gtk_bin_get_child (GTK_BIN (window));
716 if (child != NULL && gtk_widget_get_visible (child))
718 GtkAllocation child_allocation = *allocation;
721 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
722 child_allocation.x += border_width;
723 child_allocation.y += border_width + menubar_height;
724 child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
725 child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
727 gtk_widget_size_allocate (child, &child_allocation);
731 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
732 ->size_allocate (widget, allocation);
736 gtk_application_window_real_realize (GtkWidget *widget)
738 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
739 GtkApplication *application;
740 GtkSettings *settings;
742 application = gtk_window_get_application (GTK_WINDOW (window));
743 settings = gtk_widget_get_settings (widget);
745 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
746 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
747 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
748 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
750 if (!window->priv->muxer_initialised)
752 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "app", G_ACTION_GROUP (application));
753 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "win", G_ACTION_GROUP (window));
754 window->priv->muxer_initialised = TRUE;
757 gtk_application_window_update_shell_shows_app_menu (window, settings);
758 gtk_application_window_update_shell_shows_menubar (window, settings);
759 gtk_application_window_update_menubar (window);
760 gtk_application_window_update_accels (window);
762 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
765 #ifdef GDK_WINDOWING_X11
767 GdkWindow *gdkwindow;
769 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
771 if (GDK_IS_X11_WINDOW (gdkwindow) && window->priv->session)
773 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
774 g_application_get_application_id (G_APPLICATION (application)));
776 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
777 g_dbus_connection_get_unique_name (window->priv->session));
779 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
780 g_application_get_dbus_object_path (G_APPLICATION (application)));
782 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
783 window->priv->object_path);
785 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
786 gtk_application_get_app_menu_object_path (application));
788 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
789 gtk_application_get_menubar_object_path (application));
796 gtk_application_window_real_unrealize (GtkWidget *widget)
798 GtkSettings *settings;
800 settings = gtk_widget_get_settings (widget);
802 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
803 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
805 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
806 ->unrealize (widget);
810 gtk_application_window_publish (GtkApplicationWindow *window,
811 GDBusConnection *session,
812 const gchar *object_path)
814 g_assert (window->priv->session == NULL);
815 g_assert (window->priv->export_id == 0);
816 g_assert (window->priv->object_path == NULL);
818 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
819 G_ACTION_GROUP (window->priv->actions),
822 if (window->priv->export_id == 0)
825 window->priv->session = session;
826 window->priv->object_path = g_strdup (object_path);
832 gtk_application_window_unpublish (GtkApplicationWindow *window)
834 g_assert (window->priv->session != NULL);
835 g_assert (window->priv->export_id != 0);
836 g_assert (window->priv->object_path != NULL);
838 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
839 window->priv->session = NULL;
840 window->priv->export_id = 0;
842 g_free (window->priv->object_path);
843 window->priv->object_path = NULL;
847 gtk_application_window_real_map (GtkWidget *widget)
849 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
851 /* XXX could eliminate this by tweaking gtk_window_map */
852 if (window->priv->menubar)
853 gtk_widget_map (window->priv->menubar);
855 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
860 gtk_application_window_real_forall_internal (GtkContainer *container,
861 gboolean include_internal,
862 GtkCallback callback,
865 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
867 if (window->priv->menubar)
868 callback (window->priv->menubar, user_data);
870 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
871 ->forall (container, include_internal, callback, user_data);
876 gtk_application_window_get_property (GObject *object,
881 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
885 case PROP_SHOW_MENUBAR:
886 g_value_set_boolean (value, window->priv->show_menubar);
890 g_assert_not_reached ();
895 gtk_application_window_set_property (GObject *object,
900 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
904 case PROP_SHOW_MENUBAR:
905 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
909 g_assert_not_reached ();
914 gtk_application_window_dispose (GObject *object)
916 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
918 if (window->priv->menubar)
920 gtk_widget_unparent (window->priv->menubar);
921 window->priv->menubar = NULL;
924 free_accel_closures (window);
926 g_clear_object (&window->priv->app_menu_section);
927 g_clear_object (&window->priv->menubar_section);
928 g_clear_object (&window->priv->actions);
929 g_clear_object (&window->priv->accels);
930 g_clear_object (&window->priv->muxer);
932 G_OBJECT_CLASS (gtk_application_window_parent_class)
937 gtk_application_window_init (GtkApplicationWindow *window)
939 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
941 window->priv->actions = gtk_application_window_actions_new (window);
942 window->priv->app_menu_section = g_menu_new ();
943 window->priv->menubar_section = g_menu_new ();
944 window->priv->accels = gtk_accel_group_new ();
945 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
947 /* window->priv->actions is the one and only ref on the group, so when
948 * we dispose, the action group will die, disconnecting all signals.
950 g_signal_connect_swapped (window->priv->actions, "action-added",
951 G_CALLBACK (g_action_group_action_added), window);
952 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
953 G_CALLBACK (g_action_group_action_enabled_changed), window);
954 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
955 G_CALLBACK (g_action_group_action_state_changed), window);
956 g_signal_connect_swapped (window->priv->actions, "action-removed",
957 G_CALLBACK (g_action_group_action_removed), window);
959 window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
963 gtk_application_window_class_init (GtkApplicationWindowClass *class)
965 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
966 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
967 GObjectClass *object_class = G_OBJECT_CLASS (class);
969 container_class->forall = gtk_application_window_real_forall_internal;
970 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
971 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
972 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
973 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
974 widget_class->size_allocate = gtk_application_window_real_size_allocate;
975 widget_class->realize = gtk_application_window_real_realize;
976 widget_class->unrealize = gtk_application_window_real_unrealize;
977 widget_class->map = gtk_application_window_real_map;
978 object_class->get_property = gtk_application_window_get_property;
979 object_class->set_property = gtk_application_window_set_property;
980 object_class->dispose = gtk_application_window_dispose;
983 * GtkApplicationWindow:show-menubar:
985 * If this property is %TRUE, the window will display a menubar
986 * that includes the app menu and menubar, unless these are
987 * shown by the desktop shell. See gtk_application_set_app_menu()
988 * and gtk_application_set_menubar().
990 * If %FALSE, the window will not display a menubar, regardless
991 * of whether the desktop shell is showing the menus or not.
993 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
994 g_param_spec_boolean ("show-menubar",
995 P_("Show a menubar"),
996 P_("TRUE if the window should show a "
997 "menubar at the top of the window"),
998 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
999 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
1000 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
1004 * gtk_application_window_new:
1005 * @application: a #GtkApplication
1007 * Creates a new #GtkApplicationWindow.
1009 * Returns: a newly created #GtkApplicationWindow
1014 gtk_application_window_new (GtkApplication *application)
1016 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1018 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
1019 "application", application,
1024 * gtk_application_window_get_show_menubar:
1025 * @window: a #GtkApplicationWindow
1027 * Returns whether the window will display a menubar for the app menu
1028 * and menubar as needed.
1030 * Returns: %TRUE if @window will display a menubar when needed
1035 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
1037 return window->priv->show_menubar;
1041 * gtk_application_window_set_show_menubar:
1042 * @window: a #GtkApplicationWindow
1043 * @show_menubar: whether to show a menubar when needed
1045 * Sets whether the window will display a menubar for the app menu
1046 * and menubar as needed.
1051 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
1052 gboolean show_menubar)
1054 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
1056 show_menubar = !!show_menubar;
1058 if (window->priv->show_menubar != show_menubar)
1060 window->priv->show_menubar = show_menubar;
1062 gtk_application_window_update_menubar (window);
1064 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1068 GSimpleActionObserver *
1069 gtk_application_window_create_observer (GtkApplicationWindow *window,
1070 const gchar *action_name,
1073 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
1075 return g_simple_action_observer_new (window->priv->muxer, action_name, target);
1079 gtk_application_window_get_observable (GtkApplicationWindow *window)
1081 return G_ACTION_OBSERVABLE (window->priv->muxer);
1085 gtk_application_window_get_accel_group (GtkApplicationWindow *window)
1087 return window->priv->accels;