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 * The settings #GtkSettings:gtk-shell-shows-app-menu and
58 * #GtkSettings:gtk-shell-shows-menubar tell GTK+ whether the
59 * desktop environment is showing the application menu and menubar
60 * models outside the application as part of the desktop shell.
61 * For instance, on OS X, both menus will be displayed remotely;
62 * on Windows neither will be. gnome-shell (starting with version 3.4)
63 * will display the application menu, but not the menubar.
65 * If the desktop environment does not display the menubar, then
66 * #GApplicationWindow will automatically show a #GtkMenubar for it.
67 * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
68 * property. If the desktop environment does not display the application
69 * menu, then it will automatilly be included in the menubar.
71 * <example><title>A GtkApplicationWindow with a menubar</title>
72 * <programlisting><![CDATA[
73 * app = gtk_application_new ();
75 * builder = gtk_builder_new ();
76 * gtk_builder_add_from_string (builder,
78 * " <menu id='menubar'>"
79 * " <submenu label='_Edit'>"
80 * " <item label='_Copy' action='win.copy'/>"
81 * " <item label='_Paste' action='win.paste'/>"
85 * g_application_set_menubar (G_APPLICATION (app),
86 * G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
87 * g_object_unref (builder);
91 * window = gtk_application_window_new (app);
97 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
100 GSimpleActionGroup parent_instance;
102 } GtkApplicationWindowActions;
104 static GType gtk_application_window_actions_get_type (void);
105 static void gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
106 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
107 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
110 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
111 const gchar *action_name,
113 GVariant *platform_data)
115 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
116 GApplication *application;
117 GApplicationClass *class;
119 application = G_APPLICATION (gtk_window_get_application (actions->window));
120 class = G_APPLICATION_GET_CLASS (application);
122 class->before_emit (application, platform_data);
123 g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
124 class->after_emit (application, platform_data);
128 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
129 const gchar *action_name,
131 GVariant *platform_data)
133 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
134 GApplication *application;
135 GApplicationClass *class;
137 application = G_APPLICATION (gtk_window_get_application (actions->window));
138 class = G_APPLICATION_GET_CLASS (application);
140 class->before_emit (application, platform_data);
141 g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
142 class->after_emit (application, platform_data);
146 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
151 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
153 iface->activate_action_full = gtk_application_window_actions_activate_action_full;
154 iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
158 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
162 static GSimpleActionGroup *
163 gtk_application_window_actions_new (GtkApplicationWindow *window)
165 GtkApplicationWindowActions *actions;
167 actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
168 actions->window = GTK_WINDOW (window);
170 return G_SIMPLE_ACTION_GROUP (actions);
173 /* Now onto GtkApplicationWindow... */
175 struct _GtkApplicationWindowPrivate
177 GSimpleActionGroup *actions;
178 GActionObservable *muxer;
180 GtkAccelGroup *accels;
181 GSList *accel_closures;
183 GMenu *app_menu_section;
184 GMenu *menubar_section;
185 gboolean show_menubar;
187 GDBusConnection *session;
193 gtk_application_window_update_menubar (GtkApplicationWindow *window)
195 gboolean should_have_menubar;
196 gboolean have_menubar;
198 have_menubar = window->priv->menubar != NULL;
200 should_have_menubar = window->priv->show_menubar &&
201 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
202 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
204 if (have_menubar && !should_have_menubar)
206 gtk_widget_unparent (window->priv->menubar);
207 window->priv->menubar = NULL;
209 gtk_widget_queue_resize (GTK_WIDGET (window));
212 if (!have_menubar && should_have_menubar)
216 combined = g_menu_new ();
217 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
218 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
220 window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->muxer, window->priv->accels);
221 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
222 gtk_widget_show_all (window->priv->menubar);
223 g_object_unref (combined);
225 gtk_widget_queue_resize (GTK_WIDGET (window));
230 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
231 GtkSettings *settings)
233 gboolean shown_by_shell;
235 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
239 /* the shell shows it, so don't show it locally */
240 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
241 g_menu_remove (window->priv->app_menu_section, 0);
245 /* the shell does not show it, so make sure we show it */
246 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
248 GMenuModel *app_menu;
250 app_menu = g_application_get_app_menu (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
252 if (app_menu != NULL)
253 g_menu_append_submenu (window->priv->app_menu_section, _("Application"), app_menu);
259 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
260 GtkSettings *settings)
262 gboolean shown_by_shell;
264 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
268 /* the shell shows it, so don't show it locally */
269 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
270 g_menu_remove (window->priv->menubar_section, 0);
274 /* the shell does not show it, so make sure we show it */
275 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
279 menubar = g_application_get_menubar (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
282 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
294 accel_activate (GClosure *closure,
295 GValue *return_value,
296 guint n_param_values,
297 const GValue *param_values,
298 gpointer invocation_hint,
299 gpointer marshal_data)
301 AccelClosure *aclosure = (AccelClosure*)closure;
302 GActionGroup *actions;
304 actions = G_ACTION_GROUP (closure->data);
305 if (g_action_group_get_action_enabled (actions, aclosure->action_name))
307 g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
309 /* we handled the accelerator */
310 g_value_set_boolean (return_value, TRUE);
315 free_accel_closures (GtkApplicationWindow *window)
319 for (l = window->priv->accel_closures; l; l = l->next)
321 AccelClosure *closure = l->data;
323 gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
325 g_object_unref (closure->closure.data);
326 if (closure->parameter)
327 g_variant_unref (closure->parameter);
328 g_free (closure->action_name);
329 g_closure_invalidate (&closure->closure);
330 g_closure_unref (&closure->closure);
332 g_slist_free (window->priv->accel_closures);
333 window->priv->accel_closures = NULL;
337 GtkApplicationWindow *window;
338 GActionGroup *actions;
341 /* Hack. We iterate over the accel map instead of the actions,
342 * in order to pull the parameters out of accel map entries
345 add_accel_closure (gpointer data,
346 const gchar *accel_path,
348 GdkModifierType accel_mods,
352 GtkApplicationWindow *window = d->window;
353 GActionGroup *actions = d->actions;
358 AccelClosure *closure;
363 if (!g_str_has_prefix (accel_path, "<Actions>/"))
366 path = accel_path + strlen ("<Actions>/");
367 p = strchr (path, '/');
370 action_name = g_strndup (path, p - path);
371 parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
373 g_warning ("Failed to parse parameter from '%s'\n", accel_path);
377 action_name = g_strdup (path);
381 if (g_action_group_has_action (actions, action_name))
383 closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
384 g_closure_set_marshal (&closure->closure, accel_activate);
386 closure->action_name = g_strdup (action_name);
387 closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
389 window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
390 g_closure_sink (&closure->closure);
392 gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
395 g_free (action_name);
399 gtk_application_window_update_accels (GtkApplicationWindow *window)
403 free_accel_closures (window);
405 data.window = window;
406 data.actions = G_ACTION_GROUP (window->priv->muxer);
408 gtk_accel_map_foreach (&data, add_accel_closure);
412 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
416 GtkApplicationWindow *window = user_data;
418 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
419 gtk_application_window_update_menubar (window);
423 gtk_application_window_shell_shows_menubar_changed (GObject *object,
427 GtkApplicationWindow *window = user_data;
429 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
430 gtk_application_window_update_menubar (window);
434 gtk_application_window_list_actions (GActionGroup *group)
436 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
438 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
442 gtk_application_window_query_action (GActionGroup *group,
443 const gchar *action_name,
445 const GVariantType **parameter_type,
446 const GVariantType **state_type,
447 GVariant **state_hint,
450 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
452 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
453 action_name, enabled, parameter_type, state_type, state_hint, state);
457 gtk_application_window_activate_action (GActionGroup *group,
458 const gchar *action_name,
461 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
463 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
467 gtk_application_window_change_action_state (GActionGroup *group,
468 const gchar *action_name,
471 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
473 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
477 gtk_application_window_lookup_action (GActionMap *action_map,
478 const gchar *action_name)
480 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
482 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
486 gtk_application_window_add_action (GActionMap *action_map,
489 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
491 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
495 gtk_application_window_remove_action (GActionMap *action_map,
496 const gchar *action_name)
498 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
500 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
504 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
506 iface->list_actions = gtk_application_window_list_actions;
507 iface->query_action = gtk_application_window_query_action;
508 iface->activate_action = gtk_application_window_activate_action;
509 iface->change_action_state = gtk_application_window_change_action_state;
513 gtk_application_window_map_iface_init (GActionMapInterface *iface)
515 iface->lookup_action = gtk_application_window_lookup_action;
516 iface->add_action = gtk_application_window_add_action;
517 iface->remove_action = gtk_application_window_remove_action;
520 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
521 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
522 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
529 static GParamSpec *gtk_application_window_properties[N_PROPS];
532 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
533 gint *minimum_height,
534 gint *natural_height)
536 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
538 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
539 ->get_preferred_height (widget, minimum_height, natural_height);
541 if (window->priv->menubar != NULL)
543 gint menubar_min_height, menubar_nat_height;
545 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
546 *minimum_height += menubar_min_height;
547 *natural_height += menubar_nat_height;
552 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
554 gint *minimum_height,
555 gint *natural_height)
557 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
559 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
560 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
562 if (window->priv->menubar != NULL)
564 gint menubar_min_height, menubar_nat_height;
566 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
567 *minimum_height += menubar_min_height;
568 *natural_height += menubar_nat_height;
573 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
577 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
579 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
580 ->get_preferred_width (widget, minimum_width, natural_width);
582 if (window->priv->menubar != NULL)
584 gint menubar_min_width, menubar_nat_width;
586 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
587 *minimum_width = MAX (*minimum_width, menubar_min_width);
588 *natural_width = MAX (*natural_width, menubar_nat_width);
593 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
598 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
600 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
601 ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
603 if (window->priv->menubar != NULL)
605 gint menubar_min_width, menubar_nat_width;
607 gtk_widget_get_preferred_width_for_height (window->priv->menubar, height, &menubar_min_width, &menubar_nat_width);
608 *minimum_width = MAX (*minimum_width, menubar_min_width);
609 *natural_width = MAX (*natural_width, menubar_nat_width);
614 gtk_application_window_real_size_allocate (GtkWidget *widget,
615 GtkAllocation *allocation)
617 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
619 if (window->priv->menubar != NULL)
621 GtkAllocation menubar_allocation = *allocation;
622 gint menubar_min_height, menubar_nat_height;
625 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_min_height, &menubar_nat_height);
627 menubar_allocation.height = menubar_min_height;
628 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
630 child = gtk_bin_get_child (GTK_BIN (window));
631 if (child != NULL && gtk_widget_get_visible (child))
633 GtkAllocation child_allocation = *allocation;
636 child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
638 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
639 child_allocation.x += border_width;
640 child_allocation.y += border_width + menubar_min_height;
641 child_allocation.width -= border_width * 2;
642 child_allocation.height -= border_width * 2 - menubar_min_height;
643 gtk_widget_size_allocate (child, &child_allocation);
646 gtk_widget_set_allocation (widget, allocation);
649 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
650 ->size_allocate (widget, allocation);
654 gtk_application_window_real_realize (GtkWidget *widget)
656 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
657 GtkApplication *application;
658 GtkSettings *settings;
660 application = gtk_window_get_application (GTK_WINDOW (window));
661 settings = gtk_widget_get_settings (widget);
663 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
664 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
665 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
666 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
668 if (window->priv->muxer == NULL)
672 muxer = g_action_muxer_new ();
673 g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (application));
674 g_action_muxer_insert (muxer, "win", G_ACTION_GROUP (window));
675 window->priv->muxer = G_ACTION_OBSERVABLE (muxer);
678 gtk_application_window_update_shell_shows_app_menu (window, settings);
679 gtk_application_window_update_shell_shows_menubar (window, settings);
680 gtk_application_window_update_menubar (window);
681 gtk_application_window_update_accels (window);
683 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
686 #ifdef GDK_WINDOWING_X11
688 GdkWindow *gdkwindow;
690 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
692 if (GDK_IS_X11_WINDOW (gdkwindow))
694 const gchar *unique_id;
697 gdk_x11_window_set_utf8_property (gdkwindow, "_DBUS_OBJECT_PATH", window->priv->object_path);
699 unique_id = g_dbus_connection_get_unique_name (window->priv->session);
700 gdk_x11_window_set_utf8_property (gdkwindow, "_DBUS_UNIQUE_NAME", unique_id);
702 app_id = g_application_get_application_id (G_APPLICATION (application));
703 gdk_x11_window_set_utf8_property (gdkwindow, "_DBUS_APPLICATION_ID", app_id);
710 gtk_application_window_real_unrealize (GtkWidget *widget)
712 GtkSettings *settings;
714 settings = gtk_widget_get_settings (widget);
716 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
717 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
719 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
720 ->unrealize (widget);
724 gtk_application_window_publish (GtkApplicationWindow *window,
725 GDBusConnection *session,
726 const gchar *object_path)
728 g_assert (window->priv->session == NULL);
729 g_assert (window->priv->export_id == 0);
730 g_assert (window->priv->object_path == NULL);
732 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
733 G_ACTION_GROUP (window->priv->actions),
736 if (window->priv->export_id == 0)
739 window->priv->session = session;
740 window->priv->object_path = g_strdup (object_path);
746 gtk_application_window_unpublish (GtkApplicationWindow *window)
748 g_assert (window->priv->session != NULL);
749 g_assert (window->priv->export_id != 0);
750 g_assert (window->priv->object_path != NULL);
752 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
753 window->priv->session = NULL;
754 window->priv->export_id = 0;
756 g_free (window->priv->object_path);
757 window->priv->object_path = NULL;
761 gtk_application_window_real_map (GtkWidget *widget)
763 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
765 /* XXX could eliminate this by tweaking gtk_window_map */
766 if (window->priv->menubar)
767 gtk_widget_map (window->priv->menubar);
769 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
774 gtk_application_window_real_forall_internal (GtkContainer *container,
775 gboolean include_internal,
776 GtkCallback callback,
779 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
781 if (window->priv->menubar)
782 callback (window->priv->menubar, user_data);
784 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
785 ->forall (container, include_internal, callback, user_data);
790 gtk_application_window_get_property (GObject *object,
795 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
799 case PROP_SHOW_MENUBAR:
800 g_value_set_boolean (value, window->priv->show_menubar);
804 g_assert_not_reached ();
809 gtk_application_window_set_property (GObject *object,
814 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
818 case PROP_SHOW_MENUBAR:
819 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
823 g_assert_not_reached ();
828 gtk_application_window_dispose (GObject *object)
830 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
832 if (window->priv->menubar)
834 gtk_widget_unparent (window->priv->menubar);
835 window->priv->menubar = NULL;
838 free_accel_closures (window);
840 g_clear_object (&window->priv->app_menu_section);
841 g_clear_object (&window->priv->menubar_section);
842 g_clear_object (&window->priv->actions);
843 g_clear_object (&window->priv->accels);
844 g_clear_object (&window->priv->muxer);
846 G_OBJECT_CLASS (gtk_application_window_parent_class)
851 gtk_application_window_init (GtkApplicationWindow *window)
853 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
855 window->priv->actions = gtk_application_window_actions_new (window);
856 window->priv->app_menu_section = g_menu_new ();
857 window->priv->menubar_section = g_menu_new ();
858 window->priv->accels = gtk_accel_group_new ();
859 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
861 /* window->priv->actions is the one and only ref on the group, so when
862 * we dispose, the action group will die, disconnecting all signals.
864 g_signal_connect_swapped (window->priv->actions, "action-added",
865 G_CALLBACK (g_action_group_action_added), window);
866 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
867 G_CALLBACK (g_action_group_action_enabled_changed), window);
868 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
869 G_CALLBACK (g_action_group_action_state_changed), window);
870 g_signal_connect_swapped (window->priv->actions, "action-removed",
871 G_CALLBACK (g_action_group_action_removed), window);
875 gtk_application_window_class_init (GtkApplicationWindowClass *class)
877 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
878 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
879 GObjectClass *object_class = G_OBJECT_CLASS (class);
881 container_class->forall = gtk_application_window_real_forall_internal;
882 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
883 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
884 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
885 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
886 widget_class->size_allocate = gtk_application_window_real_size_allocate;
887 widget_class->realize = gtk_application_window_real_realize;
888 widget_class->unrealize = gtk_application_window_real_unrealize;
889 widget_class->map = gtk_application_window_real_map;
890 object_class->get_property = gtk_application_window_get_property;
891 object_class->set_property = gtk_application_window_set_property;
892 object_class->dispose = gtk_application_window_dispose;
895 * GtkApplicationWindow:show-menubar:
897 * If this property is %TRUE, the window will display a menubar
898 * that includes the app menu and menubar, unless these are
899 * shown by the desktop shell. See g_application_set_app_menu()
900 * and g_application_set_menubar().
902 * If %FALSE, the window will not display a menubar, regardless
903 * of whether the desktop shell is showing the menus or not.
905 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
906 g_param_spec_boolean ("show-menubar",
907 P_("Show a menubar"),
908 P_("TRUE if the window should show a "
909 "menubar at the top of the window"),
910 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
911 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
912 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
916 * gtk_application_window_new:
917 * @application: a #GtkApplication
919 * Creates a new #GtkApplicationWindow.
921 * Returns: a newly created #GtkApplicationWindow
926 gtk_application_window_new (GtkApplication *application)
928 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
930 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
931 "application", application,
936 * gtk_application_window_get_show_menubar:
937 * @window: a #GtkApplicationWindow
939 * Returns whether the window will display a menubar for the app menu
940 * and menubar as needed.
942 * Returns: %TRUE if @window will display a menubar when needed
947 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
949 return window->priv->show_menubar;
953 * gtk_application_window_set_show_menubar:
954 * @window: a #GtkApplicationWindow
955 * @show_menubar: whether to show a menubar when needed
957 * Sets whether the window will display a menubar for the app menu
958 * and menubar as needed.
963 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
964 gboolean show_menubar)
966 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
968 show_menubar = !!show_menubar;
970 if (window->priv->show_menubar != show_menubar)
972 window->priv->show_menubar = show_menubar;
974 gtk_application_window_update_menubar (window);
976 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);