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 g_application_set_app_menu() and
47 * g_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 * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
72 * property. If the desktop environment does not display the application
73 * menu, then it will automatilly be included in the menubar.
75 * <example><title>A GtkApplicationWindow with a menubar</title>
76 * <programlisting><![CDATA[
77 * app = gtk_application_new ();
79 * builder = gtk_builder_new ();
80 * gtk_builder_add_from_string (builder,
82 * " <menu id='menubar'>"
83 * " <submenu label='_Edit'>"
84 * " <item label='_Copy' action='win.copy'/>"
85 * " <item label='_Paste' action='win.paste'/>"
89 * g_application_set_menubar (G_APPLICATION (app),
90 * G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
91 * g_object_unref (builder);
95 * window = gtk_application_window_new (app);
101 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
104 GSimpleActionGroup parent_instance;
106 } GtkApplicationWindowActions;
108 static GType gtk_application_window_actions_get_type (void);
109 static void gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
110 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
111 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
114 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
115 const gchar *action_name,
117 GVariant *platform_data)
119 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
120 GApplication *application;
121 GApplicationClass *class;
123 application = G_APPLICATION (gtk_window_get_application (actions->window));
124 class = G_APPLICATION_GET_CLASS (application);
126 class->before_emit (application, platform_data);
127 g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
128 class->after_emit (application, platform_data);
132 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
133 const gchar *action_name,
135 GVariant *platform_data)
137 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
138 GApplication *application;
139 GApplicationClass *class;
141 application = G_APPLICATION (gtk_window_get_application (actions->window));
142 class = G_APPLICATION_GET_CLASS (application);
144 class->before_emit (application, platform_data);
145 g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
146 class->after_emit (application, platform_data);
150 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
155 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
157 iface->activate_action_full = gtk_application_window_actions_activate_action_full;
158 iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
162 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
166 static GSimpleActionGroup *
167 gtk_application_window_actions_new (GtkApplicationWindow *window)
169 GtkApplicationWindowActions *actions;
171 actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
172 actions->window = GTK_WINDOW (window);
174 return G_SIMPLE_ACTION_GROUP (actions);
177 /* Now onto GtkApplicationWindow... */
179 struct _GtkApplicationWindowPrivate
181 GSimpleActionGroup *actions;
182 GActionObservable *muxer;
184 GtkAccelGroup *accels;
185 GSList *accel_closures;
187 GMenu *app_menu_section;
188 GMenu *menubar_section;
189 gboolean show_menubar;
191 GDBusConnection *session;
197 gtk_application_window_update_menubar (GtkApplicationWindow *window)
199 gboolean should_have_menubar;
200 gboolean have_menubar;
202 have_menubar = window->priv->menubar != NULL;
204 should_have_menubar = window->priv->show_menubar &&
205 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
206 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
208 if (have_menubar && !should_have_menubar)
210 gtk_widget_unparent (window->priv->menubar);
211 window->priv->menubar = NULL;
213 gtk_widget_queue_resize (GTK_WIDGET (window));
216 if (!have_menubar && should_have_menubar)
220 combined = g_menu_new ();
221 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
222 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
224 window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->muxer, window->priv->accels);
225 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
226 gtk_widget_show_all (window->priv->menubar);
227 g_object_unref (combined);
229 gtk_widget_queue_resize (GTK_WIDGET (window));
234 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
235 GtkSettings *settings)
237 gboolean shown_by_shell;
239 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
243 /* the shell shows it, so don't show it locally */
244 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
245 g_menu_remove (window->priv->app_menu_section, 0);
249 /* the shell does not show it, so make sure we show it */
250 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
252 GMenuModel *app_menu;
254 app_menu = g_application_get_app_menu (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
256 if (app_menu != NULL)
257 g_menu_append_submenu (window->priv->app_menu_section, _("Application"), app_menu);
263 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
264 GtkSettings *settings)
266 gboolean shown_by_shell;
268 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
272 /* the shell shows it, so don't show it locally */
273 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
274 g_menu_remove (window->priv->menubar_section, 0);
278 /* the shell does not show it, so make sure we show it */
279 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
283 menubar = g_application_get_menubar (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
286 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
298 accel_activate (GClosure *closure,
299 GValue *return_value,
300 guint n_param_values,
301 const GValue *param_values,
302 gpointer invocation_hint,
303 gpointer marshal_data)
305 AccelClosure *aclosure = (AccelClosure*)closure;
306 GActionGroup *actions;
308 actions = G_ACTION_GROUP (closure->data);
309 if (g_action_group_get_action_enabled (actions, aclosure->action_name))
311 g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
313 /* we handled the accelerator */
314 g_value_set_boolean (return_value, TRUE);
319 free_accel_closures (GtkApplicationWindow *window)
323 for (l = window->priv->accel_closures; l; l = l->next)
325 AccelClosure *closure = l->data;
327 gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
329 g_object_unref (closure->closure.data);
330 if (closure->parameter)
331 g_variant_unref (closure->parameter);
332 g_free (closure->action_name);
333 g_closure_invalidate (&closure->closure);
334 g_closure_unref (&closure->closure);
336 g_slist_free (window->priv->accel_closures);
337 window->priv->accel_closures = NULL;
341 GtkApplicationWindow *window;
342 GActionGroup *actions;
345 /* Hack. We iterate over the accel map instead of the actions,
346 * in order to pull the parameters out of accel map entries
349 add_accel_closure (gpointer data,
350 const gchar *accel_path,
352 GdkModifierType accel_mods,
356 GtkApplicationWindow *window = d->window;
357 GActionGroup *actions = d->actions;
362 AccelClosure *closure;
367 if (!g_str_has_prefix (accel_path, "<Actions>/"))
370 path = accel_path + strlen ("<Actions>/");
371 p = strchr (path, '/');
374 action_name = g_strndup (path, p - path);
375 parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
377 g_warning ("Failed to parse parameter from '%s'\n", accel_path);
381 action_name = g_strdup (path);
385 if (g_action_group_has_action (actions, action_name))
387 closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
388 g_closure_set_marshal (&closure->closure, accel_activate);
390 closure->action_name = g_strdup (action_name);
391 closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
393 window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
394 g_closure_sink (&closure->closure);
396 gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
399 g_free (action_name);
403 gtk_application_window_update_accels (GtkApplicationWindow *window)
407 free_accel_closures (window);
409 data.window = window;
410 data.actions = G_ACTION_GROUP (window->priv->muxer);
412 gtk_accel_map_foreach (&data, add_accel_closure);
416 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
420 GtkApplicationWindow *window = user_data;
422 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
423 gtk_application_window_update_menubar (window);
427 gtk_application_window_shell_shows_menubar_changed (GObject *object,
431 GtkApplicationWindow *window = user_data;
433 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
434 gtk_application_window_update_menubar (window);
438 gtk_application_window_list_actions (GActionGroup *group)
440 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
442 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
446 gtk_application_window_query_action (GActionGroup *group,
447 const gchar *action_name,
449 const GVariantType **parameter_type,
450 const GVariantType **state_type,
451 GVariant **state_hint,
454 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
456 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
457 action_name, enabled, parameter_type, state_type, state_hint, state);
461 gtk_application_window_activate_action (GActionGroup *group,
462 const gchar *action_name,
465 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
467 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
471 gtk_application_window_change_action_state (GActionGroup *group,
472 const gchar *action_name,
475 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
477 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
481 gtk_application_window_lookup_action (GActionMap *action_map,
482 const gchar *action_name)
484 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
486 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
490 gtk_application_window_add_action (GActionMap *action_map,
493 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
495 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
499 gtk_application_window_remove_action (GActionMap *action_map,
500 const gchar *action_name)
502 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
504 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
508 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
510 iface->list_actions = gtk_application_window_list_actions;
511 iface->query_action = gtk_application_window_query_action;
512 iface->activate_action = gtk_application_window_activate_action;
513 iface->change_action_state = gtk_application_window_change_action_state;
517 gtk_application_window_map_iface_init (GActionMapInterface *iface)
519 iface->lookup_action = gtk_application_window_lookup_action;
520 iface->add_action = gtk_application_window_add_action;
521 iface->remove_action = gtk_application_window_remove_action;
524 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
525 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
526 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
533 static GParamSpec *gtk_application_window_properties[N_PROPS];
536 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
537 gint *minimum_height,
538 gint *natural_height)
540 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
542 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
543 ->get_preferred_height (widget, minimum_height, natural_height);
545 if (window->priv->menubar != NULL)
547 gint menubar_min_height, menubar_nat_height;
549 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
550 *minimum_height += menubar_min_height;
551 *natural_height += menubar_nat_height;
556 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
558 gint *minimum_height,
559 gint *natural_height)
561 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
563 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
564 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
566 if (window->priv->menubar != NULL)
568 gint menubar_min_height, menubar_nat_height;
570 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
571 *minimum_height += menubar_min_height;
572 *natural_height += menubar_nat_height;
577 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
581 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
583 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
584 ->get_preferred_width (widget, minimum_width, natural_width);
586 if (window->priv->menubar != NULL)
588 gint menubar_min_width, menubar_nat_width;
590 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
591 *minimum_width = MAX (*minimum_width, menubar_min_width);
592 *natural_width = MAX (*natural_width, menubar_nat_width);
597 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
602 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
604 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
605 ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
607 if (window->priv->menubar != NULL)
609 gint menubar_min_width, menubar_nat_width;
611 gtk_widget_get_preferred_width_for_height (window->priv->menubar, height, &menubar_min_width, &menubar_nat_width);
612 *minimum_width = MAX (*minimum_width, menubar_min_width);
613 *natural_width = MAX (*natural_width, menubar_nat_width);
618 gtk_application_window_real_size_allocate (GtkWidget *widget,
619 GtkAllocation *allocation)
621 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
623 if (window->priv->menubar != NULL)
625 GtkAllocation menubar_allocation = *allocation;
626 gint menubar_min_height, menubar_nat_height;
629 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_min_height, &menubar_nat_height);
631 menubar_allocation.height = menubar_min_height;
632 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
634 child = gtk_bin_get_child (GTK_BIN (window));
635 if (child != NULL && gtk_widget_get_visible (child))
637 GtkAllocation child_allocation = *allocation;
640 child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
642 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
643 child_allocation.x += border_width;
644 child_allocation.y += border_width + menubar_min_height;
645 child_allocation.width -= border_width * 2;
646 child_allocation.height -= border_width * 2 - menubar_min_height;
647 gtk_widget_size_allocate (child, &child_allocation);
650 gtk_widget_set_allocation (widget, allocation);
653 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
654 ->size_allocate (widget, allocation);
658 gtk_application_window_real_realize (GtkWidget *widget)
660 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
661 GtkApplication *application;
662 GtkSettings *settings;
664 application = gtk_window_get_application (GTK_WINDOW (window));
665 settings = gtk_widget_get_settings (widget);
667 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
668 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
669 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
670 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
672 if (window->priv->muxer == NULL)
676 muxer = g_action_muxer_new ();
677 g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (application));
678 g_action_muxer_insert (muxer, "win", G_ACTION_GROUP (window));
679 window->priv->muxer = G_ACTION_OBSERVABLE (muxer);
682 gtk_application_window_update_shell_shows_app_menu (window, settings);
683 gtk_application_window_update_shell_shows_menubar (window, settings);
684 gtk_application_window_update_menubar (window);
685 gtk_application_window_update_accels (window);
687 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
690 #ifdef GDK_WINDOWING_X11
692 GdkWindow *gdkwindow;
694 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
696 if (GDK_IS_X11_WINDOW (gdkwindow))
698 const gchar *unique_id;
701 gdk_x11_window_set_utf8_property (gdkwindow, "_DBUS_OBJECT_PATH", window->priv->object_path);
703 unique_id = g_dbus_connection_get_unique_name (window->priv->session);
704 gdk_x11_window_set_utf8_property (gdkwindow, "_DBUS_UNIQUE_NAME", unique_id);
706 app_id = g_application_get_application_id (G_APPLICATION (application));
707 gdk_x11_window_set_utf8_property (gdkwindow, "_DBUS_APPLICATION_ID", app_id);
714 gtk_application_window_real_unrealize (GtkWidget *widget)
716 GtkSettings *settings;
718 settings = gtk_widget_get_settings (widget);
720 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
721 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
723 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
724 ->unrealize (widget);
728 gtk_application_window_publish (GtkApplicationWindow *window,
729 GDBusConnection *session,
730 const gchar *object_path)
732 g_assert (window->priv->session == NULL);
733 g_assert (window->priv->export_id == 0);
734 g_assert (window->priv->object_path == NULL);
736 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
737 G_ACTION_GROUP (window->priv->actions),
740 if (window->priv->export_id == 0)
743 window->priv->session = session;
744 window->priv->object_path = g_strdup (object_path);
750 gtk_application_window_unpublish (GtkApplicationWindow *window)
752 g_assert (window->priv->session != NULL);
753 g_assert (window->priv->export_id != 0);
754 g_assert (window->priv->object_path != NULL);
756 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
757 window->priv->session = NULL;
758 window->priv->export_id = 0;
760 g_free (window->priv->object_path);
761 window->priv->object_path = NULL;
765 gtk_application_window_real_map (GtkWidget *widget)
767 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
769 /* XXX could eliminate this by tweaking gtk_window_map */
770 if (window->priv->menubar)
771 gtk_widget_map (window->priv->menubar);
773 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
778 gtk_application_window_real_forall_internal (GtkContainer *container,
779 gboolean include_internal,
780 GtkCallback callback,
783 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
785 if (window->priv->menubar)
786 callback (window->priv->menubar, user_data);
788 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
789 ->forall (container, include_internal, callback, user_data);
794 gtk_application_window_get_property (GObject *object,
799 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
803 case PROP_SHOW_MENUBAR:
804 g_value_set_boolean (value, window->priv->show_menubar);
808 g_assert_not_reached ();
813 gtk_application_window_set_property (GObject *object,
818 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
822 case PROP_SHOW_MENUBAR:
823 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
827 g_assert_not_reached ();
832 gtk_application_window_dispose (GObject *object)
834 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
836 if (window->priv->menubar)
838 gtk_widget_unparent (window->priv->menubar);
839 window->priv->menubar = NULL;
842 free_accel_closures (window);
844 g_clear_object (&window->priv->app_menu_section);
845 g_clear_object (&window->priv->menubar_section);
846 g_clear_object (&window->priv->actions);
847 g_clear_object (&window->priv->accels);
848 g_clear_object (&window->priv->muxer);
850 G_OBJECT_CLASS (gtk_application_window_parent_class)
855 gtk_application_window_init (GtkApplicationWindow *window)
857 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
859 window->priv->actions = gtk_application_window_actions_new (window);
860 window->priv->app_menu_section = g_menu_new ();
861 window->priv->menubar_section = g_menu_new ();
862 window->priv->accels = gtk_accel_group_new ();
863 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
865 /* window->priv->actions is the one and only ref on the group, so when
866 * we dispose, the action group will die, disconnecting all signals.
868 g_signal_connect_swapped (window->priv->actions, "action-added",
869 G_CALLBACK (g_action_group_action_added), window);
870 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
871 G_CALLBACK (g_action_group_action_enabled_changed), window);
872 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
873 G_CALLBACK (g_action_group_action_state_changed), window);
874 g_signal_connect_swapped (window->priv->actions, "action-removed",
875 G_CALLBACK (g_action_group_action_removed), window);
879 gtk_application_window_class_init (GtkApplicationWindowClass *class)
881 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
882 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
883 GObjectClass *object_class = G_OBJECT_CLASS (class);
885 container_class->forall = gtk_application_window_real_forall_internal;
886 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
887 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
888 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
889 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
890 widget_class->size_allocate = gtk_application_window_real_size_allocate;
891 widget_class->realize = gtk_application_window_real_realize;
892 widget_class->unrealize = gtk_application_window_real_unrealize;
893 widget_class->map = gtk_application_window_real_map;
894 object_class->get_property = gtk_application_window_get_property;
895 object_class->set_property = gtk_application_window_set_property;
896 object_class->dispose = gtk_application_window_dispose;
899 * GtkApplicationWindow:show-menubar:
901 * If this property is %TRUE, the window will display a menubar
902 * that includes the app menu and menubar, unless these are
903 * shown by the desktop shell. See g_application_set_app_menu()
904 * and g_application_set_menubar().
906 * If %FALSE, the window will not display a menubar, regardless
907 * of whether the desktop shell is showing the menus or not.
909 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
910 g_param_spec_boolean ("show-menubar",
911 P_("Show a menubar"),
912 P_("TRUE if the window should show a "
913 "menubar at the top of the window"),
914 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
915 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
916 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
920 * gtk_application_window_new:
921 * @application: a #GtkApplication
923 * Creates a new #GtkApplicationWindow.
925 * Returns: a newly created #GtkApplicationWindow
930 gtk_application_window_new (GtkApplication *application)
932 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
934 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
935 "application", application,
940 * gtk_application_window_get_show_menubar:
941 * @window: a #GtkApplicationWindow
943 * Returns whether the window will display a menubar for the app menu
944 * and menubar as needed.
946 * Returns: %TRUE if @window will display a menubar when needed
951 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
953 return window->priv->show_menubar;
957 * gtk_application_window_set_show_menubar:
958 * @window: a #GtkApplicationWindow
959 * @show_menubar: whether to show a menubar when needed
961 * Sets whether the window will display a menubar for the app menu
962 * and menubar as needed.
967 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
968 gboolean show_menubar)
970 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
972 show_menubar = !!show_menubar;
974 if (window->priv->show_menubar != show_menubar)
976 window->priv->show_menubar = show_menubar;
978 gtk_application_window_update_menubar (window);
980 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);