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 * As with #GtkApplication, the GDK lock will be acquired when
59 * processing actions arriving from other processes and should therefore
60 * be held when activating actions locally (if GDK threads are enabled).
62 * The settings #GtkSettings:gtk-shell-shows-app-menu and
63 * #GtkSettings:gtk-shell-shows-menubar tell GTK+ whether the
64 * desktop environment is showing the application menu and menubar
65 * models outside the application as part of the desktop shell.
66 * For instance, on OS X, both menus will be displayed remotely;
67 * on Windows neither will be. gnome-shell (starting with version 3.4)
68 * will display the application menu, but not the menubar.
70 * If the desktop environment does not display the menubar, then
71 * #GApplicationWindow will automatically show a #GtkMenubar for it.
72 * (see the #GtkApplication docs for some screenshots of how this
73 * looks on different platforms).
74 * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
75 * property. If the desktop environment does not display the application
76 * menu, then it will automatically be included in the menubar.
78 * <example><title>A GtkApplicationWindow with a menubar</title>
79 * <programlisting><![CDATA[
80 * app = gtk_application_new ();
82 * builder = gtk_builder_new ();
83 * gtk_builder_add_from_string (builder,
85 * " <menu id='menubar'>"
86 * " <submenu label='_Edit'>"
87 * " <item label='_Copy' action='win.copy'/>"
88 * " <item label='_Paste' action='win.paste'/>"
92 * gtk_application_set_menubar (G_APPLICATION (app),
93 * G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
94 * g_object_unref (builder);
98 * window = gtk_application_window_new (app);
104 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
107 GSimpleActionGroup parent_instance;
109 } GtkApplicationWindowActions;
111 static GType gtk_application_window_actions_get_type (void);
112 static void gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
113 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
114 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
117 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
118 const gchar *action_name,
120 GVariant *platform_data)
122 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
123 GApplication *application;
124 GApplicationClass *class;
126 application = G_APPLICATION (gtk_window_get_application (actions->window));
127 class = G_APPLICATION_GET_CLASS (application);
129 class->before_emit (application, platform_data);
130 g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
131 class->after_emit (application, platform_data);
135 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
136 const gchar *action_name,
138 GVariant *platform_data)
140 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
141 GApplication *application;
142 GApplicationClass *class;
144 application = G_APPLICATION (gtk_window_get_application (actions->window));
145 class = G_APPLICATION_GET_CLASS (application);
147 class->before_emit (application, platform_data);
148 g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
149 class->after_emit (application, platform_data);
153 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
158 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
160 iface->activate_action_full = gtk_application_window_actions_activate_action_full;
161 iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
165 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
169 static GSimpleActionGroup *
170 gtk_application_window_actions_new (GtkApplicationWindow *window)
172 GtkApplicationWindowActions *actions;
174 actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
175 actions->window = GTK_WINDOW (window);
177 return G_SIMPLE_ACTION_GROUP (actions);
180 /* Now onto GtkApplicationWindow... */
182 struct _GtkApplicationWindowPrivate
184 GSimpleActionGroup *actions;
185 GActionObservable *muxer;
186 gboolean muxer_initialised;
188 GtkAccelGroup *accels;
189 GSList *accel_closures;
191 GMenu *app_menu_section;
192 GMenu *menubar_section;
193 gboolean show_menubar;
195 GDBusConnection *session;
201 gtk_application_window_update_menubar (GtkApplicationWindow *window)
203 gboolean should_have_menubar;
204 gboolean have_menubar;
206 have_menubar = window->priv->menubar != NULL;
208 should_have_menubar = window->priv->show_menubar &&
209 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
210 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
212 if (have_menubar && !should_have_menubar)
214 gtk_widget_unparent (window->priv->menubar);
215 window->priv->menubar = NULL;
217 gtk_widget_queue_resize (GTK_WIDGET (window));
220 if (!have_menubar && should_have_menubar)
224 combined = g_menu_new ();
225 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
226 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
228 window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->muxer, window->priv->accels);
229 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
230 gtk_widget_show_all (window->priv->menubar);
231 g_object_unref (combined);
233 gtk_widget_queue_resize (GTK_WIDGET (window));
238 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
239 GtkSettings *settings)
241 gboolean shown_by_shell;
243 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
247 /* the shell shows it, so don't show it locally */
248 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
249 g_menu_remove (window->priv->app_menu_section, 0);
253 /* the shell does not show it, so make sure we show it */
254 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
256 GMenuModel *app_menu;
258 app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
260 if (app_menu != NULL)
264 name = g_get_application_name ();
265 if (name == g_get_prgname ())
266 name = _("Application");
267 g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
274 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
275 GtkSettings *settings)
277 gboolean shown_by_shell;
279 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
283 /* the shell shows it, so don't show it locally */
284 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
285 g_menu_remove (window->priv->menubar_section, 0);
289 /* the shell does not show it, so make sure we show it */
290 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
294 menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
297 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
309 accel_activate (GClosure *closure,
310 GValue *return_value,
311 guint n_param_values,
312 const GValue *param_values,
313 gpointer invocation_hint,
314 gpointer marshal_data)
316 AccelClosure *aclosure = (AccelClosure*)closure;
317 GActionGroup *actions;
319 actions = G_ACTION_GROUP (closure->data);
320 if (g_action_group_get_action_enabled (actions, aclosure->action_name))
322 g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
324 /* we handled the accelerator */
325 g_value_set_boolean (return_value, TRUE);
330 free_accel_closures (GtkApplicationWindow *window)
334 for (l = window->priv->accel_closures; l; l = l->next)
336 AccelClosure *closure = l->data;
338 gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
340 g_object_unref (closure->closure.data);
341 if (closure->parameter)
342 g_variant_unref (closure->parameter);
343 g_free (closure->action_name);
344 g_closure_invalidate (&closure->closure);
345 g_closure_unref (&closure->closure);
347 g_slist_free (window->priv->accel_closures);
348 window->priv->accel_closures = NULL;
352 GtkApplicationWindow *window;
353 GActionGroup *actions;
356 /* Hack. We iterate over the accel map instead of the actions,
357 * in order to pull the parameters out of accel map entries
360 add_accel_closure (gpointer data,
361 const gchar *accel_path,
363 GdkModifierType accel_mods,
367 GtkApplicationWindow *window = d->window;
368 GActionGroup *actions = d->actions;
373 AccelClosure *closure;
378 if (!g_str_has_prefix (accel_path, "<Actions>/"))
381 path = accel_path + strlen ("<Actions>/");
382 p = strchr (path, '/');
385 action_name = g_strndup (path, p - path);
386 parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
388 g_warning ("Failed to parse parameter from '%s'\n", accel_path);
392 action_name = g_strdup (path);
396 if (g_action_group_has_action (actions, action_name))
398 closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
399 g_closure_set_marshal (&closure->closure, accel_activate);
401 closure->action_name = g_strdup (action_name);
402 closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
404 window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
405 g_closure_sink (&closure->closure);
407 gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
410 g_free (action_name);
414 gtk_application_window_update_accels (GtkApplicationWindow *window)
418 free_accel_closures (window);
420 data.window = window;
421 data.actions = G_ACTION_GROUP (window->priv->muxer);
423 gtk_accel_map_foreach (&data, add_accel_closure);
427 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
431 GtkApplicationWindow *window = user_data;
433 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
434 gtk_application_window_update_menubar (window);
438 gtk_application_window_shell_shows_menubar_changed (GObject *object,
442 GtkApplicationWindow *window = user_data;
444 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
445 gtk_application_window_update_menubar (window);
449 gtk_application_window_list_actions (GActionGroup *group)
451 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
453 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
457 gtk_application_window_query_action (GActionGroup *group,
458 const gchar *action_name,
460 const GVariantType **parameter_type,
461 const GVariantType **state_type,
462 GVariant **state_hint,
465 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
467 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
468 action_name, enabled, parameter_type, state_type, state_hint, state);
472 gtk_application_window_activate_action (GActionGroup *group,
473 const gchar *action_name,
476 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
478 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
482 gtk_application_window_change_action_state (GActionGroup *group,
483 const gchar *action_name,
486 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
488 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
492 gtk_application_window_lookup_action (GActionMap *action_map,
493 const gchar *action_name)
495 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
497 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
501 gtk_application_window_add_action (GActionMap *action_map,
504 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
506 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
510 gtk_application_window_remove_action (GActionMap *action_map,
511 const gchar *action_name)
513 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
515 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
519 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
521 iface->list_actions = gtk_application_window_list_actions;
522 iface->query_action = gtk_application_window_query_action;
523 iface->activate_action = gtk_application_window_activate_action;
524 iface->change_action_state = gtk_application_window_change_action_state;
528 gtk_application_window_map_iface_init (GActionMapInterface *iface)
530 iface->lookup_action = gtk_application_window_lookup_action;
531 iface->add_action = gtk_application_window_add_action;
532 iface->remove_action = gtk_application_window_remove_action;
535 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
536 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
537 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
544 static GParamSpec *gtk_application_window_properties[N_PROPS];
547 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
548 gint *minimum_height,
549 gint *natural_height)
551 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
553 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
554 ->get_preferred_height (widget, minimum_height, natural_height);
556 if (window->priv->menubar != NULL)
558 gint menubar_min_height, menubar_nat_height;
560 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
561 *minimum_height += menubar_min_height;
562 *natural_height += menubar_nat_height;
567 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
569 gint *minimum_height,
570 gint *natural_height)
572 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
574 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
575 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
577 if (window->priv->menubar != NULL)
579 gint menubar_min_height, menubar_nat_height;
581 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
582 *minimum_height += menubar_min_height;
583 *natural_height += menubar_nat_height;
588 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
592 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
594 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
595 ->get_preferred_width (widget, minimum_width, natural_width);
597 if (window->priv->menubar != NULL)
599 gint menubar_min_width, menubar_nat_width;
601 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
602 *minimum_width = MAX (*minimum_width, menubar_min_width);
603 *natural_width = MAX (*natural_width, menubar_nat_width);
608 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
613 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
616 if (window->priv->menubar != NULL)
617 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
621 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
622 ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
624 if (window->priv->menubar != NULL)
626 gint menubar_min_width, menubar_nat_width;
628 gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
629 *minimum_width = MAX (*minimum_width, menubar_min_width);
630 *natural_width = MAX (*natural_width, menubar_nat_width);
635 gtk_application_window_real_size_allocate (GtkWidget *widget,
636 GtkAllocation *allocation)
638 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
640 if (window->priv->menubar != NULL)
642 GtkAllocation menubar_allocation = *allocation;
646 _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
648 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
650 menubar_allocation.height = menubar_height;
651 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
653 child = gtk_bin_get_child (GTK_BIN (window));
654 if (child != NULL && gtk_widget_get_visible (child))
656 GtkAllocation child_allocation = *allocation;
659 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
660 child_allocation.x += border_width;
661 child_allocation.y += border_width + menubar_height;
662 child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
663 child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
665 gtk_widget_size_allocate (child, &child_allocation);
669 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
670 ->size_allocate (widget, allocation);
674 gtk_application_window_real_realize (GtkWidget *widget)
676 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
677 GtkApplication *application;
678 GtkSettings *settings;
680 application = gtk_window_get_application (GTK_WINDOW (window));
681 settings = gtk_widget_get_settings (widget);
683 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
684 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
685 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
686 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
688 if (!window->priv->muxer_initialised)
690 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "app", G_ACTION_GROUP (application));
691 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "win", G_ACTION_GROUP (window));
692 window->priv->muxer_initialised = TRUE;
695 gtk_application_window_update_shell_shows_app_menu (window, settings);
696 gtk_application_window_update_shell_shows_menubar (window, settings);
697 gtk_application_window_update_menubar (window);
698 gtk_application_window_update_accels (window);
700 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
703 #ifdef GDK_WINDOWING_X11
705 GdkWindow *gdkwindow;
707 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
709 if (GDK_IS_X11_WINDOW (gdkwindow))
711 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
712 g_application_get_application_id (G_APPLICATION (application)));
714 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
715 g_dbus_connection_get_unique_name (window->priv->session));
717 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
718 gtk_application_get_dbus_object_path (application));
720 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
721 window->priv->object_path);
723 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
724 gtk_application_get_app_menu_object_path (application));
726 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
727 gtk_application_get_menubar_object_path (application));
734 gtk_application_window_real_unrealize (GtkWidget *widget)
736 GtkSettings *settings;
738 settings = gtk_widget_get_settings (widget);
740 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
741 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
743 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
744 ->unrealize (widget);
748 gtk_application_window_publish (GtkApplicationWindow *window,
749 GDBusConnection *session,
750 const gchar *object_path)
752 g_assert (window->priv->session == NULL);
753 g_assert (window->priv->export_id == 0);
754 g_assert (window->priv->object_path == NULL);
756 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
757 G_ACTION_GROUP (window->priv->actions),
760 if (window->priv->export_id == 0)
763 window->priv->session = session;
764 window->priv->object_path = g_strdup (object_path);
770 gtk_application_window_unpublish (GtkApplicationWindow *window)
772 g_assert (window->priv->session != NULL);
773 g_assert (window->priv->export_id != 0);
774 g_assert (window->priv->object_path != NULL);
776 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
777 window->priv->session = NULL;
778 window->priv->export_id = 0;
780 g_free (window->priv->object_path);
781 window->priv->object_path = NULL;
785 gtk_application_window_real_map (GtkWidget *widget)
787 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
789 /* XXX could eliminate this by tweaking gtk_window_map */
790 if (window->priv->menubar)
791 gtk_widget_map (window->priv->menubar);
793 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
798 gtk_application_window_real_forall_internal (GtkContainer *container,
799 gboolean include_internal,
800 GtkCallback callback,
803 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
805 if (window->priv->menubar)
806 callback (window->priv->menubar, user_data);
808 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
809 ->forall (container, include_internal, callback, user_data);
814 gtk_application_window_get_property (GObject *object,
819 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
823 case PROP_SHOW_MENUBAR:
824 g_value_set_boolean (value, window->priv->show_menubar);
828 g_assert_not_reached ();
833 gtk_application_window_set_property (GObject *object,
838 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
842 case PROP_SHOW_MENUBAR:
843 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
847 g_assert_not_reached ();
852 gtk_application_window_dispose (GObject *object)
854 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
856 if (window->priv->menubar)
858 gtk_widget_unparent (window->priv->menubar);
859 window->priv->menubar = NULL;
862 free_accel_closures (window);
864 g_clear_object (&window->priv->app_menu_section);
865 g_clear_object (&window->priv->menubar_section);
866 g_clear_object (&window->priv->actions);
867 g_clear_object (&window->priv->accels);
868 g_clear_object (&window->priv->muxer);
870 G_OBJECT_CLASS (gtk_application_window_parent_class)
875 gtk_application_window_init (GtkApplicationWindow *window)
877 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
879 window->priv->actions = gtk_application_window_actions_new (window);
880 window->priv->app_menu_section = g_menu_new ();
881 window->priv->menubar_section = g_menu_new ();
882 window->priv->accels = gtk_accel_group_new ();
883 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
885 /* window->priv->actions is the one and only ref on the group, so when
886 * we dispose, the action group will die, disconnecting all signals.
888 g_signal_connect_swapped (window->priv->actions, "action-added",
889 G_CALLBACK (g_action_group_action_added), window);
890 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
891 G_CALLBACK (g_action_group_action_enabled_changed), window);
892 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
893 G_CALLBACK (g_action_group_action_state_changed), window);
894 g_signal_connect_swapped (window->priv->actions, "action-removed",
895 G_CALLBACK (g_action_group_action_removed), window);
897 window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
901 gtk_application_window_class_init (GtkApplicationWindowClass *class)
903 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
904 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
905 GObjectClass *object_class = G_OBJECT_CLASS (class);
907 container_class->forall = gtk_application_window_real_forall_internal;
908 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
909 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
910 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
911 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
912 widget_class->size_allocate = gtk_application_window_real_size_allocate;
913 widget_class->realize = gtk_application_window_real_realize;
914 widget_class->unrealize = gtk_application_window_real_unrealize;
915 widget_class->map = gtk_application_window_real_map;
916 object_class->get_property = gtk_application_window_get_property;
917 object_class->set_property = gtk_application_window_set_property;
918 object_class->dispose = gtk_application_window_dispose;
921 * GtkApplicationWindow:show-menubar:
923 * If this property is %TRUE, the window will display a menubar
924 * that includes the app menu and menubar, unless these are
925 * shown by the desktop shell. See gtk_application_set_app_menu()
926 * and gtk_application_set_menubar().
928 * If %FALSE, the window will not display a menubar, regardless
929 * of whether the desktop shell is showing the menus or not.
931 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
932 g_param_spec_boolean ("show-menubar",
933 P_("Show a menubar"),
934 P_("TRUE if the window should show a "
935 "menubar at the top of the window"),
936 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
937 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
938 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
942 * gtk_application_window_new:
943 * @application: a #GtkApplication
945 * Creates a new #GtkApplicationWindow.
947 * Returns: a newly created #GtkApplicationWindow
952 gtk_application_window_new (GtkApplication *application)
954 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
956 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
957 "application", application,
962 * gtk_application_window_get_show_menubar:
963 * @window: a #GtkApplicationWindow
965 * Returns whether the window will display a menubar for the app menu
966 * and menubar as needed.
968 * Returns: %TRUE if @window will display a menubar when needed
973 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
975 return window->priv->show_menubar;
979 * gtk_application_window_set_show_menubar:
980 * @window: a #GtkApplicationWindow
981 * @show_menubar: whether to show a menubar when needed
983 * Sets whether the window will display a menubar for the app menu
984 * and menubar as needed.
989 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
990 gboolean show_menubar)
992 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
994 show_menubar = !!show_menubar;
996 if (window->priv->show_menubar != show_menubar)
998 window->priv->show_menubar = show_menubar;
1000 gtk_application_window_update_menubar (window);
1002 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1006 GSimpleActionObserver *
1007 gtk_application_window_create_observer (GtkApplicationWindow *window,
1008 const gchar *action_name,
1011 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
1013 return g_simple_action_observer_new (window->priv->muxer, action_name, target);