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);
608 if (window->priv->menubar != NULL)
609 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
613 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
614 ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
616 if (window->priv->menubar != NULL)
618 gint menubar_min_width, menubar_nat_width;
620 gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
621 *minimum_width = MAX (*minimum_width, menubar_min_width);
622 *natural_width = MAX (*natural_width, menubar_nat_width);
627 gtk_application_window_real_size_allocate (GtkWidget *widget,
628 GtkAllocation *allocation)
630 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
632 if (window->priv->menubar != NULL)
634 GtkAllocation menubar_allocation = *allocation;
638 gtk_widget_set_allocation (widget, allocation);
640 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
642 menubar_allocation.height = menubar_height;
643 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
645 child = gtk_bin_get_child (GTK_BIN (window));
646 if (child != NULL && gtk_widget_get_visible (child))
648 GtkAllocation child_allocation = *allocation;
651 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
652 child_allocation.x += border_width;
653 child_allocation.y += border_width + menubar_height;
654 child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
655 child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
657 gtk_widget_size_allocate (child, &child_allocation);
661 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
662 ->size_allocate (widget, allocation);
666 gtk_application_window_real_realize (GtkWidget *widget)
668 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
669 GtkApplication *application;
670 GtkSettings *settings;
672 application = gtk_window_get_application (GTK_WINDOW (window));
673 settings = gtk_widget_get_settings (widget);
675 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
676 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
677 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
678 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
680 if (!window->priv->muxer_initialised)
682 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "app", G_ACTION_GROUP (application));
683 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "win", G_ACTION_GROUP (window));
684 window->priv->muxer_initialised = TRUE;
687 gtk_application_window_update_shell_shows_app_menu (window, settings);
688 gtk_application_window_update_shell_shows_menubar (window, settings);
689 gtk_application_window_update_menubar (window);
690 gtk_application_window_update_accels (window);
692 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
695 #ifdef GDK_WINDOWING_X11
697 GdkWindow *gdkwindow;
699 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
701 if (GDK_IS_X11_WINDOW (gdkwindow))
703 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
704 g_application_get_application_id (G_APPLICATION (application)));
706 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
707 g_dbus_connection_get_unique_name (window->priv->session));
709 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
710 gtk_application_get_dbus_object_path (application));
712 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
713 window->priv->object_path);
715 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
716 gtk_application_get_app_menu_object_path (application));
718 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
719 gtk_application_get_menubar_object_path (application));
726 gtk_application_window_real_unrealize (GtkWidget *widget)
728 GtkSettings *settings;
730 settings = gtk_widget_get_settings (widget);
732 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
733 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
735 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
736 ->unrealize (widget);
740 gtk_application_window_publish (GtkApplicationWindow *window,
741 GDBusConnection *session,
742 const gchar *object_path)
744 g_assert (window->priv->session == NULL);
745 g_assert (window->priv->export_id == 0);
746 g_assert (window->priv->object_path == NULL);
748 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
749 G_ACTION_GROUP (window->priv->actions),
752 if (window->priv->export_id == 0)
755 window->priv->session = session;
756 window->priv->object_path = g_strdup (object_path);
762 gtk_application_window_unpublish (GtkApplicationWindow *window)
764 g_assert (window->priv->session != NULL);
765 g_assert (window->priv->export_id != 0);
766 g_assert (window->priv->object_path != NULL);
768 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
769 window->priv->session = NULL;
770 window->priv->export_id = 0;
772 g_free (window->priv->object_path);
773 window->priv->object_path = NULL;
777 gtk_application_window_real_map (GtkWidget *widget)
779 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
781 /* XXX could eliminate this by tweaking gtk_window_map */
782 if (window->priv->menubar)
783 gtk_widget_map (window->priv->menubar);
785 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
790 gtk_application_window_real_forall_internal (GtkContainer *container,
791 gboolean include_internal,
792 GtkCallback callback,
795 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
797 if (window->priv->menubar)
798 callback (window->priv->menubar, user_data);
800 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
801 ->forall (container, include_internal, callback, user_data);
806 gtk_application_window_get_property (GObject *object,
811 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
815 case PROP_SHOW_MENUBAR:
816 g_value_set_boolean (value, window->priv->show_menubar);
820 g_assert_not_reached ();
825 gtk_application_window_set_property (GObject *object,
830 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
834 case PROP_SHOW_MENUBAR:
835 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
839 g_assert_not_reached ();
844 gtk_application_window_dispose (GObject *object)
846 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
848 if (window->priv->menubar)
850 gtk_widget_unparent (window->priv->menubar);
851 window->priv->menubar = NULL;
854 free_accel_closures (window);
856 g_clear_object (&window->priv->app_menu_section);
857 g_clear_object (&window->priv->menubar_section);
858 g_clear_object (&window->priv->actions);
859 g_clear_object (&window->priv->accels);
860 g_clear_object (&window->priv->muxer);
862 G_OBJECT_CLASS (gtk_application_window_parent_class)
867 gtk_application_window_init (GtkApplicationWindow *window)
869 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
871 window->priv->actions = gtk_application_window_actions_new (window);
872 window->priv->app_menu_section = g_menu_new ();
873 window->priv->menubar_section = g_menu_new ();
874 window->priv->accels = gtk_accel_group_new ();
875 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
877 /* window->priv->actions is the one and only ref on the group, so when
878 * we dispose, the action group will die, disconnecting all signals.
880 g_signal_connect_swapped (window->priv->actions, "action-added",
881 G_CALLBACK (g_action_group_action_added), window);
882 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
883 G_CALLBACK (g_action_group_action_enabled_changed), window);
884 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
885 G_CALLBACK (g_action_group_action_state_changed), window);
886 g_signal_connect_swapped (window->priv->actions, "action-removed",
887 G_CALLBACK (g_action_group_action_removed), window);
889 window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
893 gtk_application_window_class_init (GtkApplicationWindowClass *class)
895 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
896 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
897 GObjectClass *object_class = G_OBJECT_CLASS (class);
899 container_class->forall = gtk_application_window_real_forall_internal;
900 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
901 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
902 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
903 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
904 widget_class->size_allocate = gtk_application_window_real_size_allocate;
905 widget_class->realize = gtk_application_window_real_realize;
906 widget_class->unrealize = gtk_application_window_real_unrealize;
907 widget_class->map = gtk_application_window_real_map;
908 object_class->get_property = gtk_application_window_get_property;
909 object_class->set_property = gtk_application_window_set_property;
910 object_class->dispose = gtk_application_window_dispose;
913 * GtkApplicationWindow:show-menubar:
915 * If this property is %TRUE, the window will display a menubar
916 * that includes the app menu and menubar, unless these are
917 * shown by the desktop shell. See gtk_application_set_app_menu()
918 * and gtk_application_set_menubar().
920 * If %FALSE, the window will not display a menubar, regardless
921 * of whether the desktop shell is showing the menus or not.
923 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
924 g_param_spec_boolean ("show-menubar",
925 P_("Show a menubar"),
926 P_("TRUE if the window should show a "
927 "menubar at the top of the window"),
928 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
929 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
930 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
934 * gtk_application_window_new:
935 * @application: a #GtkApplication
937 * Creates a new #GtkApplicationWindow.
939 * Returns: a newly created #GtkApplicationWindow
944 gtk_application_window_new (GtkApplication *application)
946 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
948 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
949 "application", application,
954 * gtk_application_window_get_show_menubar:
955 * @window: a #GtkApplicationWindow
957 * Returns whether the window will display a menubar for the app menu
958 * and menubar as needed.
960 * Returns: %TRUE if @window will display a menubar when needed
965 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
967 return window->priv->show_menubar;
971 * gtk_application_window_set_show_menubar:
972 * @window: a #GtkApplicationWindow
973 * @show_menubar: whether to show a menubar when needed
975 * Sets whether the window will display a menubar for the app menu
976 * and menubar as needed.
981 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
982 gboolean show_menubar)
984 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
986 show_menubar = !!show_menubar;
988 if (window->priv->show_menubar != show_menubar)
990 window->priv->show_menubar = show_menubar;
992 gtk_application_window_update_menubar (window);
994 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
998 GSimpleActionObserver *
999 gtk_application_window_create_observer (GtkApplicationWindow *window,
1000 const gchar *action_name,
1003 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
1005 return g_simple_action_observer_new (window->priv->muxer, action_name, target);