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 "gtkmodelmenu.h"
28 #include "gactionmuxer.h"
29 #include "gtkaccelgroup.h"
30 #include "gtkaccelmap.h"
34 #ifdef GDK_WINDOWING_X11
35 #include <gdk/x11/gdkx.h>
39 * SECTION:gtkapplicationwindow
40 * @title: GtkApplicationWindow
41 * @short_description: GtkWindow subclass with GtkApplication support
43 * GtkApplicationWindow is a #GtkWindow subclass that offers some
44 * extra functionality for better integration with #GtkApplication
45 * features. Notably, it can handle both the application menu as well
46 * as the menubar. See gtk_application_set_app_menu() and
47 * gtk_application_set_menubar().
49 * This class implements the #GActionGroup and #GActionMap interfaces,
50 * to let you add window-specific actions that will be exported by the
51 * associated #GtkApplication, together with its application-wide
52 * actions. Window-specific actions are prefixed with the "win."
53 * prefix and application-wide actions are prefixed with the "app."
54 * prefix. Actions must be addressed with the prefixed name when
55 * referring to them from a #GMenuModel.
57 * As with #GtkApplication, the GDK lock will be acquired when
58 * processing actions arriving from other processes and should therefore
59 * be held when activating actions locally (if GDK threads are enabled).
61 * The settings #GtkSettings:gtk-shell-shows-app-menu and
62 * #GtkSettings:gtk-shell-shows-menubar tell GTK+ whether the
63 * desktop environment is showing the application menu and menubar
64 * models outside the application as part of the desktop shell.
65 * For instance, on OS X, both menus will be displayed remotely;
66 * on Windows neither will be. gnome-shell (starting with version 3.4)
67 * will display the application menu, but not the menubar.
69 * If the desktop environment does not display the menubar, then
70 * #GApplicationWindow will automatically show a #GtkMenubar for it.
71 * (see the #GtkApplication docs for some screenshots of how this
72 * looks on different platforms).
73 * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
74 * property. If the desktop environment does not display the application
75 * menu, then it will automatically be included in the menubar.
77 * <example><title>A GtkApplicationWindow with a menubar</title>
78 * <programlisting><![CDATA[
79 * app = gtk_application_new ();
81 * builder = gtk_builder_new ();
82 * gtk_builder_add_from_string (builder,
84 * " <menu id='menubar'>"
85 * " <submenu label='_Edit'>"
86 * " <item label='_Copy' action='win.copy'/>"
87 * " <item label='_Paste' action='win.paste'/>"
91 * gtk_application_set_menubar (G_APPLICATION (app),
92 * G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
93 * g_object_unref (builder);
97 * window = gtk_application_window_new (app);
103 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
106 GSimpleActionGroup parent_instance;
108 } GtkApplicationWindowActions;
110 static GType gtk_application_window_actions_get_type (void);
111 static void gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
112 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
113 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
116 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
117 const gchar *action_name,
119 GVariant *platform_data)
121 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
122 GApplication *application;
123 GApplicationClass *class;
125 application = G_APPLICATION (gtk_window_get_application (actions->window));
126 class = G_APPLICATION_GET_CLASS (application);
128 class->before_emit (application, platform_data);
129 g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
130 class->after_emit (application, platform_data);
134 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
135 const gchar *action_name,
137 GVariant *platform_data)
139 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
140 GApplication *application;
141 GApplicationClass *class;
143 application = G_APPLICATION (gtk_window_get_application (actions->window));
144 class = G_APPLICATION_GET_CLASS (application);
146 class->before_emit (application, platform_data);
147 g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
148 class->after_emit (application, platform_data);
152 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
157 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
159 iface->activate_action_full = gtk_application_window_actions_activate_action_full;
160 iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
164 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
168 static GSimpleActionGroup *
169 gtk_application_window_actions_new (GtkApplicationWindow *window)
171 GtkApplicationWindowActions *actions;
173 actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
174 actions->window = GTK_WINDOW (window);
176 return G_SIMPLE_ACTION_GROUP (actions);
179 /* Now onto GtkApplicationWindow... */
181 struct _GtkApplicationWindowPrivate
183 GSimpleActionGroup *actions;
184 GActionObservable *muxer;
185 gboolean muxer_initialised;
187 GtkAccelGroup *accels;
188 GSList *accel_closures;
190 GMenu *app_menu_section;
191 GMenu *menubar_section;
192 gboolean show_menubar;
194 GDBusConnection *session;
200 gtk_application_window_update_menubar (GtkApplicationWindow *window)
202 gboolean should_have_menubar;
203 gboolean have_menubar;
205 have_menubar = window->priv->menubar != NULL;
207 should_have_menubar = window->priv->show_menubar &&
208 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
209 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
211 if (have_menubar && !should_have_menubar)
213 gtk_widget_unparent (window->priv->menubar);
214 window->priv->menubar = NULL;
216 gtk_widget_queue_resize (GTK_WIDGET (window));
219 if (!have_menubar && should_have_menubar)
223 combined = g_menu_new ();
224 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
225 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
227 window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->muxer, window->priv->accels);
228 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
229 gtk_widget_show_all (window->priv->menubar);
230 g_object_unref (combined);
232 gtk_widget_queue_resize (GTK_WIDGET (window));
237 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
238 GtkSettings *settings)
240 gboolean shown_by_shell;
242 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
246 /* the shell shows it, so don't show it locally */
247 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
248 g_menu_remove (window->priv->app_menu_section, 0);
252 /* the shell does not show it, so make sure we show it */
253 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
255 GMenuModel *app_menu;
257 app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
259 if (app_menu != NULL)
260 g_menu_append_submenu (window->priv->app_menu_section, _("Application"), app_menu);
266 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
267 GtkSettings *settings)
269 gboolean shown_by_shell;
271 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
275 /* the shell shows it, so don't show it locally */
276 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
277 g_menu_remove (window->priv->menubar_section, 0);
281 /* the shell does not show it, so make sure we show it */
282 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
286 menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
289 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
301 accel_activate (GClosure *closure,
302 GValue *return_value,
303 guint n_param_values,
304 const GValue *param_values,
305 gpointer invocation_hint,
306 gpointer marshal_data)
308 AccelClosure *aclosure = (AccelClosure*)closure;
309 GActionGroup *actions;
311 actions = G_ACTION_GROUP (closure->data);
312 if (g_action_group_get_action_enabled (actions, aclosure->action_name))
314 g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
316 /* we handled the accelerator */
317 g_value_set_boolean (return_value, TRUE);
322 free_accel_closures (GtkApplicationWindow *window)
326 for (l = window->priv->accel_closures; l; l = l->next)
328 AccelClosure *closure = l->data;
330 gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
332 g_object_unref (closure->closure.data);
333 if (closure->parameter)
334 g_variant_unref (closure->parameter);
335 g_free (closure->action_name);
336 g_closure_invalidate (&closure->closure);
337 g_closure_unref (&closure->closure);
339 g_slist_free (window->priv->accel_closures);
340 window->priv->accel_closures = NULL;
344 GtkApplicationWindow *window;
345 GActionGroup *actions;
348 /* Hack. We iterate over the accel map instead of the actions,
349 * in order to pull the parameters out of accel map entries
352 add_accel_closure (gpointer data,
353 const gchar *accel_path,
355 GdkModifierType accel_mods,
359 GtkApplicationWindow *window = d->window;
360 GActionGroup *actions = d->actions;
365 AccelClosure *closure;
370 if (!g_str_has_prefix (accel_path, "<Actions>/"))
373 path = accel_path + strlen ("<Actions>/");
374 p = strchr (path, '/');
377 action_name = g_strndup (path, p - path);
378 parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
380 g_warning ("Failed to parse parameter from '%s'\n", accel_path);
384 action_name = g_strdup (path);
388 if (g_action_group_has_action (actions, action_name))
390 closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
391 g_closure_set_marshal (&closure->closure, accel_activate);
393 closure->action_name = g_strdup (action_name);
394 closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
396 window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
397 g_closure_sink (&closure->closure);
399 gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
402 g_free (action_name);
406 gtk_application_window_update_accels (GtkApplicationWindow *window)
410 free_accel_closures (window);
412 data.window = window;
413 data.actions = G_ACTION_GROUP (window->priv->muxer);
415 gtk_accel_map_foreach (&data, add_accel_closure);
419 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
423 GtkApplicationWindow *window = user_data;
425 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
426 gtk_application_window_update_menubar (window);
430 gtk_application_window_shell_shows_menubar_changed (GObject *object,
434 GtkApplicationWindow *window = user_data;
436 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
437 gtk_application_window_update_menubar (window);
441 gtk_application_window_list_actions (GActionGroup *group)
443 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
445 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
449 gtk_application_window_query_action (GActionGroup *group,
450 const gchar *action_name,
452 const GVariantType **parameter_type,
453 const GVariantType **state_type,
454 GVariant **state_hint,
457 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
459 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
460 action_name, enabled, parameter_type, state_type, state_hint, state);
464 gtk_application_window_activate_action (GActionGroup *group,
465 const gchar *action_name,
468 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
470 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
474 gtk_application_window_change_action_state (GActionGroup *group,
475 const gchar *action_name,
478 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
480 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
484 gtk_application_window_lookup_action (GActionMap *action_map,
485 const gchar *action_name)
487 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
489 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
493 gtk_application_window_add_action (GActionMap *action_map,
496 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
498 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
502 gtk_application_window_remove_action (GActionMap *action_map,
503 const gchar *action_name)
505 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
507 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
511 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
513 iface->list_actions = gtk_application_window_list_actions;
514 iface->query_action = gtk_application_window_query_action;
515 iface->activate_action = gtk_application_window_activate_action;
516 iface->change_action_state = gtk_application_window_change_action_state;
520 gtk_application_window_map_iface_init (GActionMapInterface *iface)
522 iface->lookup_action = gtk_application_window_lookup_action;
523 iface->add_action = gtk_application_window_add_action;
524 iface->remove_action = gtk_application_window_remove_action;
527 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
528 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
529 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
536 static GParamSpec *gtk_application_window_properties[N_PROPS];
539 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
540 gint *minimum_height,
541 gint *natural_height)
543 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
545 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
546 ->get_preferred_height (widget, minimum_height, natural_height);
548 if (window->priv->menubar != NULL)
550 gint menubar_min_height, menubar_nat_height;
552 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
553 *minimum_height += menubar_min_height;
554 *natural_height += menubar_nat_height;
559 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
561 gint *minimum_height,
562 gint *natural_height)
564 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
566 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
567 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
569 if (window->priv->menubar != NULL)
571 gint menubar_min_height, menubar_nat_height;
573 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
574 *minimum_height += menubar_min_height;
575 *natural_height += menubar_nat_height;
580 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
584 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
586 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
587 ->get_preferred_width (widget, minimum_width, natural_width);
589 if (window->priv->menubar != NULL)
591 gint menubar_min_width, menubar_nat_width;
593 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
594 *minimum_width = MAX (*minimum_width, menubar_min_width);
595 *natural_width = MAX (*natural_width, menubar_nat_width);
600 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
605 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
607 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
608 ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
610 if (window->priv->menubar != NULL)
612 gint menubar_min_width, menubar_nat_width;
614 gtk_widget_get_preferred_width_for_height (window->priv->menubar, height, &menubar_min_width, &menubar_nat_width);
615 *minimum_width = MAX (*minimum_width, menubar_min_width);
616 *natural_width = MAX (*natural_width, menubar_nat_width);
621 gtk_application_window_real_size_allocate (GtkWidget *widget,
622 GtkAllocation *allocation)
624 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
626 if (window->priv->menubar != NULL)
628 GtkAllocation menubar_allocation = *allocation;
632 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
634 menubar_allocation.height = menubar_height;
635 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
637 child = gtk_bin_get_child (GTK_BIN (window));
638 if (child != NULL && gtk_widget_get_visible (child))
640 GtkAllocation child_allocation = *allocation;
643 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
644 child_allocation.x += border_width;
645 child_allocation.y += border_width + menubar_height;
646 child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
647 child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
649 gtk_widget_size_allocate (child, &child_allocation);
652 gtk_widget_set_allocation (widget, allocation);
655 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
656 ->size_allocate (widget, allocation);
660 gtk_application_window_real_realize (GtkWidget *widget)
662 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
663 GtkApplication *application;
664 GtkSettings *settings;
666 application = gtk_window_get_application (GTK_WINDOW (window));
667 settings = gtk_widget_get_settings (widget);
669 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
670 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
671 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
672 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
674 if (!window->priv->muxer_initialised)
676 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "app", G_ACTION_GROUP (application));
677 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "win", G_ACTION_GROUP (window));
678 window->priv->muxer_initialised = TRUE;
681 gtk_application_window_update_shell_shows_app_menu (window, settings);
682 gtk_application_window_update_shell_shows_menubar (window, settings);
683 gtk_application_window_update_menubar (window);
684 gtk_application_window_update_accels (window);
686 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
689 #ifdef GDK_WINDOWING_X11
691 GdkWindow *gdkwindow;
693 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
695 if (GDK_IS_X11_WINDOW (gdkwindow))
697 const gchar *unique_id;
700 gdk_x11_window_set_utf8_property (gdkwindow, "_DBUS_OBJECT_PATH", window->priv->object_path);
702 unique_id = g_dbus_connection_get_unique_name (window->priv->session);
703 gdk_x11_window_set_utf8_property (gdkwindow, "_DBUS_UNIQUE_NAME", unique_id);
705 app_id = g_application_get_application_id (G_APPLICATION (application));
706 gdk_x11_window_set_utf8_property (gdkwindow, "_DBUS_APPLICATION_ID", app_id);
713 gtk_application_window_real_unrealize (GtkWidget *widget)
715 GtkSettings *settings;
717 settings = gtk_widget_get_settings (widget);
719 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
720 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
722 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
723 ->unrealize (widget);
727 gtk_application_window_publish (GtkApplicationWindow *window,
728 GDBusConnection *session,
729 const gchar *object_path)
731 g_assert (window->priv->session == NULL);
732 g_assert (window->priv->export_id == 0);
733 g_assert (window->priv->object_path == NULL);
735 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
736 G_ACTION_GROUP (window->priv->actions),
739 if (window->priv->export_id == 0)
742 window->priv->session = session;
743 window->priv->object_path = g_strdup (object_path);
749 gtk_application_window_unpublish (GtkApplicationWindow *window)
751 g_assert (window->priv->session != NULL);
752 g_assert (window->priv->export_id != 0);
753 g_assert (window->priv->object_path != NULL);
755 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
756 window->priv->session = NULL;
757 window->priv->export_id = 0;
759 g_free (window->priv->object_path);
760 window->priv->object_path = NULL;
764 gtk_application_window_real_map (GtkWidget *widget)
766 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
768 /* XXX could eliminate this by tweaking gtk_window_map */
769 if (window->priv->menubar)
770 gtk_widget_map (window->priv->menubar);
772 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
777 gtk_application_window_real_forall_internal (GtkContainer *container,
778 gboolean include_internal,
779 GtkCallback callback,
782 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
784 if (window->priv->menubar)
785 callback (window->priv->menubar, user_data);
787 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
788 ->forall (container, include_internal, callback, user_data);
793 gtk_application_window_get_property (GObject *object,
798 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
802 case PROP_SHOW_MENUBAR:
803 g_value_set_boolean (value, window->priv->show_menubar);
807 g_assert_not_reached ();
812 gtk_application_window_set_property (GObject *object,
817 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
821 case PROP_SHOW_MENUBAR:
822 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
826 g_assert_not_reached ();
831 gtk_application_window_dispose (GObject *object)
833 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
835 if (window->priv->menubar)
837 gtk_widget_unparent (window->priv->menubar);
838 window->priv->menubar = NULL;
841 free_accel_closures (window);
843 g_clear_object (&window->priv->app_menu_section);
844 g_clear_object (&window->priv->menubar_section);
845 g_clear_object (&window->priv->actions);
846 g_clear_object (&window->priv->accels);
847 g_clear_object (&window->priv->muxer);
849 G_OBJECT_CLASS (gtk_application_window_parent_class)
854 gtk_application_window_init (GtkApplicationWindow *window)
856 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
858 window->priv->actions = gtk_application_window_actions_new (window);
859 window->priv->app_menu_section = g_menu_new ();
860 window->priv->menubar_section = g_menu_new ();
861 window->priv->accels = gtk_accel_group_new ();
862 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
864 /* window->priv->actions is the one and only ref on the group, so when
865 * we dispose, the action group will die, disconnecting all signals.
867 g_signal_connect_swapped (window->priv->actions, "action-added",
868 G_CALLBACK (g_action_group_action_added), window);
869 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
870 G_CALLBACK (g_action_group_action_enabled_changed), window);
871 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
872 G_CALLBACK (g_action_group_action_state_changed), window);
873 g_signal_connect_swapped (window->priv->actions, "action-removed",
874 G_CALLBACK (g_action_group_action_removed), window);
876 window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
880 gtk_application_window_class_init (GtkApplicationWindowClass *class)
882 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
883 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
884 GObjectClass *object_class = G_OBJECT_CLASS (class);
886 container_class->forall = gtk_application_window_real_forall_internal;
887 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
888 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
889 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
890 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
891 widget_class->size_allocate = gtk_application_window_real_size_allocate;
892 widget_class->realize = gtk_application_window_real_realize;
893 widget_class->unrealize = gtk_application_window_real_unrealize;
894 widget_class->map = gtk_application_window_real_map;
895 object_class->get_property = gtk_application_window_get_property;
896 object_class->set_property = gtk_application_window_set_property;
897 object_class->dispose = gtk_application_window_dispose;
900 * GtkApplicationWindow:show-menubar:
902 * If this property is %TRUE, the window will display a menubar
903 * that includes the app menu and menubar, unless these are
904 * shown by the desktop shell. See gtk_application_set_app_menu()
905 * and gtk_application_set_menubar().
907 * If %FALSE, the window will not display a menubar, regardless
908 * of whether the desktop shell is showing the menus or not.
910 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
911 g_param_spec_boolean ("show-menubar",
912 P_("Show a menubar"),
913 P_("TRUE if the window should show a "
914 "menubar at the top of the window"),
915 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
916 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
917 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
921 * gtk_application_window_new:
922 * @application: a #GtkApplication
924 * Creates a new #GtkApplicationWindow.
926 * Returns: a newly created #GtkApplicationWindow
931 gtk_application_window_new (GtkApplication *application)
933 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
935 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
936 "application", application,
941 * gtk_application_window_get_show_menubar:
942 * @window: a #GtkApplicationWindow
944 * Returns whether the window will display a menubar for the app menu
945 * and menubar as needed.
947 * Returns: %TRUE if @window will display a menubar when needed
952 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
954 return window->priv->show_menubar;
958 * gtk_application_window_set_show_menubar:
959 * @window: a #GtkApplicationWindow
960 * @show_menubar: whether to show a menubar when needed
962 * Sets whether the window will display a menubar for the app menu
963 * and menubar as needed.
968 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
969 gboolean show_menubar)
971 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
973 show_menubar = !!show_menubar;
975 if (window->priv->show_menubar != show_menubar)
977 window->priv->show_menubar = show_menubar;
979 gtk_application_window_update_menubar (window);
981 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
985 GSimpleActionObserver *
986 gtk_application_window_create_observer (GtkApplicationWindow *window,
987 const gchar *action_name,
990 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
992 return g_simple_action_observer_new (window->priv->muxer, action_name, target);