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, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
22 #include "gtkapplicationwindow.h"
24 #include "gtkapplicationprivate.h"
25 #include "gtkwindowprivate.h"
26 #include "gtkmodelmenu.h"
27 #include "gactionmuxer.h"
28 #include "gtkaccelgroup.h"
29 #include "gtkaccelmap.h"
31 #include "gtksettings.h"
34 #ifdef GDK_WINDOWING_X11
35 #include <gdk/x11/gdkx.h>
39 #include <gio/gdesktopappinfo.h>
43 * SECTION:gtkapplicationwindow
44 * @title: GtkApplicationWindow
45 * @short_description: GtkWindow subclass with GtkApplication support
47 * GtkApplicationWindow is a #GtkWindow subclass that offers some
48 * extra functionality for better integration with #GtkApplication
49 * features. Notably, it can handle both the application menu as well
50 * as the menubar. See gtk_application_set_app_menu() and
51 * gtk_application_set_menubar().
53 * This class implements the #GActionGroup and #GActionMap interfaces,
54 * to let you add window-specific actions that will be exported by the
55 * associated #GtkApplication, together with its application-wide
56 * actions. Window-specific actions are prefixed with the "win."
57 * prefix and application-wide actions are prefixed with the "app."
58 * prefix. Actions must be addressed with the prefixed name when
59 * referring to them from a #GMenuModel.
61 * Note that widgets that are placed inside a GtkApplicationWindow
62 * can also activate these actions, if they implement the
63 * GtkActionable interface.
65 * As with #GtkApplication, the GDK lock will be acquired when
66 * processing actions arriving from other processes and should therefore
67 * be held when activating actions locally (if GDK threads are enabled).
69 * The settings #GtkSettings:gtk-shell-shows-app-menu and
70 * #GtkSettings:gtk-shell-shows-menubar tell GTK+ whether the
71 * desktop environment is showing the application menu and menubar
72 * models outside the application as part of the desktop shell.
73 * For instance, on OS X, both menus will be displayed remotely;
74 * on Windows neither will be. gnome-shell (starting with version 3.4)
75 * will display the application menu, but not the menubar.
77 * If the desktop environment does not display the menubar, then
78 * #GApplicationWindow will automatically show a #GtkMenubar for it.
79 * (see the #GtkApplication docs for some screenshots of how this
80 * looks on different platforms).
81 * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
82 * property. If the desktop environment does not display the application
83 * menu, then it will automatically be included in the menubar.
85 * <example><title>A GtkApplicationWindow with a menubar</title>
86 * <programlisting><![CDATA[
87 * app = gtk_application_new ();
89 * builder = gtk_builder_new ();
90 * gtk_builder_add_from_string (builder,
92 * " <menu id='menubar'>"
93 * " <submenu label='_Edit'>"
94 * " <item label='_Copy' action='win.copy'/>"
95 * " <item label='_Paste' action='win.paste'/>"
99 * gtk_application_set_menubar (G_APPLICATION (app),
100 * G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
101 * g_object_unref (builder);
105 * window = gtk_application_window_new (app);
110 * The XML format understood by #GtkBuilder for #GMenuModel consists
111 * of a toplevel <tag class="starttag">menu</tag> element, which contains
112 * one or more <tag class="starttag">item</tag> elements. Each
113 * <tag class="starttag">item</tag> element contains
114 * <tag class="starttag">attribute</tag> and <tag class="starttag">link</tag>
115 * elements with a mandatory name attribute.
116 * <tag class="starttag">link</tag> elements have the same content
117 * model as <tag class="starttag">menu</tag>.
119 * Attribute values can be translated using gettext, like other #GtkBuilder
120 * content. <tag class="starttag">attribute</tag> elements can be marked for
121 * translation with a <literal>translatable="yes"</literal> attribute.
122 * It is also possible to specify message context and translator comments,
123 * using the context and comments attributes. To make use of this, the
124 * #GtkBuilder must have been given the gettext domain to use.
127 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
130 GSimpleActionGroup parent_instance;
132 } GtkApplicationWindowActions;
134 static GType gtk_application_window_actions_get_type (void);
135 static void gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
136 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
137 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
140 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
141 const gchar *action_name,
143 GVariant *platform_data)
145 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
146 GApplication *application;
147 GApplicationClass *class;
149 application = G_APPLICATION (gtk_window_get_application (actions->window));
150 class = G_APPLICATION_GET_CLASS (application);
152 class->before_emit (application, platform_data);
153 g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
154 class->after_emit (application, platform_data);
158 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
159 const gchar *action_name,
161 GVariant *platform_data)
163 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
164 GApplication *application;
165 GApplicationClass *class;
167 application = G_APPLICATION (gtk_window_get_application (actions->window));
168 class = G_APPLICATION_GET_CLASS (application);
170 class->before_emit (application, platform_data);
171 g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
172 class->after_emit (application, platform_data);
176 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
181 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
183 iface->activate_action_full = gtk_application_window_actions_activate_action_full;
184 iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
188 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
192 static GSimpleActionGroup *
193 gtk_application_window_actions_new (GtkApplicationWindow *window)
195 GtkApplicationWindowActions *actions;
197 actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
198 actions->window = GTK_WINDOW (window);
200 return G_SIMPLE_ACTION_GROUP (actions);
203 /* Now onto GtkApplicationWindow... */
205 struct _GtkApplicationWindowPrivate
207 GSimpleActionGroup *actions;
208 GActionObservable *muxer;
209 gboolean muxer_initialised;
211 GtkAccelGroup *accels;
212 GSList *accel_closures;
214 GMenu *app_menu_section;
215 GMenu *menubar_section;
216 gboolean show_menubar;
218 GDBusConnection *session;
224 gtk_application_window_update_menubar (GtkApplicationWindow *window)
226 gboolean should_have_menubar;
227 gboolean have_menubar;
229 have_menubar = window->priv->menubar != NULL;
231 should_have_menubar = window->priv->show_menubar &&
232 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
233 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
235 if (have_menubar && !should_have_menubar)
237 gtk_widget_unparent (window->priv->menubar);
238 window->priv->menubar = NULL;
240 gtk_widget_queue_resize (GTK_WIDGET (window));
243 if (!have_menubar && should_have_menubar)
247 combined = g_menu_new ();
248 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
249 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
251 window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->muxer, window->priv->accels);
252 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
253 gtk_widget_show_all (window->priv->menubar);
254 g_object_unref (combined);
256 gtk_widget_queue_resize (GTK_WIDGET (window));
261 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
262 GtkSettings *settings)
264 gboolean shown_by_shell;
266 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
270 /* the shell shows it, so don't show it locally */
271 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
272 g_menu_remove (window->priv->app_menu_section, 0);
276 /* the shell does not show it, so make sure we show it */
277 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
279 GMenuModel *app_menu;
281 app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
283 if (app_menu != NULL)
286 GDesktopAppInfo *app_info = NULL;
288 name = g_get_application_name ();
289 if (name == g_get_prgname ())
291 const gchar *app_name = NULL;
296 desktop_name = g_strconcat (name, ".desktop", NULL);
297 app_info = g_desktop_app_info_new (desktop_name);
298 if (app_info != NULL)
299 app_name = g_app_info_get_name (G_APP_INFO (app_info));
301 g_free (desktop_name);
302 #endif /* HAVE_GIO_UNIX */
304 if (app_name != NULL &&
305 g_strcmp0 (app_name, name) != 0)
308 name = _("Application");
310 g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
311 g_clear_object (&app_info);
318 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
319 GtkSettings *settings)
321 gboolean shown_by_shell;
323 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
327 /* the shell shows it, so don't show it locally */
328 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
329 g_menu_remove (window->priv->menubar_section, 0);
333 /* the shell does not show it, so make sure we show it */
334 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
338 menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
341 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
353 accel_activate (GClosure *closure,
354 GValue *return_value,
355 guint n_param_values,
356 const GValue *param_values,
357 gpointer invocation_hint,
358 gpointer marshal_data)
360 AccelClosure *aclosure = (AccelClosure*)closure;
361 GActionGroup *actions;
363 actions = G_ACTION_GROUP (closure->data);
364 if (g_action_group_get_action_enabled (actions, aclosure->action_name))
366 g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
368 /* we handled the accelerator */
369 g_value_set_boolean (return_value, TRUE);
374 free_accel_closures (GtkApplicationWindow *window)
378 for (l = window->priv->accel_closures; l; l = l->next)
380 AccelClosure *closure = l->data;
382 gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
384 g_object_unref (closure->closure.data);
385 if (closure->parameter)
386 g_variant_unref (closure->parameter);
387 g_free (closure->action_name);
388 g_closure_invalidate (&closure->closure);
389 g_closure_unref (&closure->closure);
391 g_slist_free (window->priv->accel_closures);
392 window->priv->accel_closures = NULL;
396 GtkApplicationWindow *window;
397 GActionGroup *actions;
400 /* Hack. We iterate over the accel map instead of the actions,
401 * in order to pull the parameters out of accel map entries
404 add_accel_closure (gpointer data,
405 const gchar *accel_path,
407 GdkModifierType accel_mods,
411 GtkApplicationWindow *window = d->window;
412 GActionGroup *actions = d->actions;
417 AccelClosure *closure;
422 if (!g_str_has_prefix (accel_path, "<GAction>/"))
425 path = accel_path + strlen ("<GAction>/");
426 p = strchr (path, '/');
429 action_name = g_strndup (path, p - path);
430 parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
432 g_warning ("Failed to parse parameter from '%s'\n", accel_path);
436 action_name = g_strdup (path);
440 if (g_action_group_has_action (actions, action_name))
442 closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
443 g_closure_set_marshal (&closure->closure, accel_activate);
445 closure->action_name = g_strdup (action_name);
446 closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
448 window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
449 g_closure_sink (&closure->closure);
451 gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
454 g_free (action_name);
458 gtk_application_window_update_accels (GtkApplicationWindow *window)
462 free_accel_closures (window);
464 data.window = window;
465 data.actions = G_ACTION_GROUP (window->priv->muxer);
467 gtk_accel_map_foreach (&data, add_accel_closure);
471 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
475 GtkApplicationWindow *window = user_data;
477 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
478 gtk_application_window_update_menubar (window);
482 gtk_application_window_shell_shows_menubar_changed (GObject *object,
486 GtkApplicationWindow *window = user_data;
488 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
489 gtk_application_window_update_menubar (window);
493 gtk_application_window_list_actions (GActionGroup *group)
495 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
497 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
501 gtk_application_window_query_action (GActionGroup *group,
502 const gchar *action_name,
504 const GVariantType **parameter_type,
505 const GVariantType **state_type,
506 GVariant **state_hint,
509 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
511 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
512 action_name, enabled, parameter_type, state_type, state_hint, state);
516 gtk_application_window_activate_action (GActionGroup *group,
517 const gchar *action_name,
520 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
522 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
526 gtk_application_window_change_action_state (GActionGroup *group,
527 const gchar *action_name,
530 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
532 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
536 gtk_application_window_lookup_action (GActionMap *action_map,
537 const gchar *action_name)
539 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
541 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
545 gtk_application_window_add_action (GActionMap *action_map,
548 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
550 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
554 gtk_application_window_remove_action (GActionMap *action_map,
555 const gchar *action_name)
557 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
559 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
563 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
565 iface->list_actions = gtk_application_window_list_actions;
566 iface->query_action = gtk_application_window_query_action;
567 iface->activate_action = gtk_application_window_activate_action;
568 iface->change_action_state = gtk_application_window_change_action_state;
572 gtk_application_window_map_iface_init (GActionMapInterface *iface)
574 iface->lookup_action = gtk_application_window_lookup_action;
575 iface->add_action = gtk_application_window_add_action;
576 iface->remove_action = gtk_application_window_remove_action;
579 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
580 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
581 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
588 static GParamSpec *gtk_application_window_properties[N_PROPS];
591 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
592 gint *minimum_height,
593 gint *natural_height)
595 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
597 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
598 ->get_preferred_height (widget, minimum_height, natural_height);
600 if (window->priv->menubar != NULL)
602 gint menubar_min_height, menubar_nat_height;
604 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
605 *minimum_height += menubar_min_height;
606 *natural_height += menubar_nat_height;
611 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
613 gint *minimum_height,
614 gint *natural_height)
616 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
618 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
619 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
621 if (window->priv->menubar != NULL)
623 gint menubar_min_height, menubar_nat_height;
625 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
626 *minimum_height += menubar_min_height;
627 *natural_height += menubar_nat_height;
632 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
636 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
638 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
639 ->get_preferred_width (widget, minimum_width, natural_width);
641 if (window->priv->menubar != NULL)
643 gint menubar_min_width, menubar_nat_width;
645 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
646 *minimum_width = MAX (*minimum_width, menubar_min_width);
647 *natural_width = MAX (*natural_width, menubar_nat_width);
652 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
657 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
660 if (window->priv->menubar != NULL)
661 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
665 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
666 ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
668 if (window->priv->menubar != NULL)
670 gint menubar_min_width, menubar_nat_width;
672 gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
673 *minimum_width = MAX (*minimum_width, menubar_min_width);
674 *natural_width = MAX (*natural_width, menubar_nat_width);
679 gtk_application_window_real_size_allocate (GtkWidget *widget,
680 GtkAllocation *allocation)
682 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
684 if (window->priv->menubar != NULL)
686 GtkAllocation menubar_allocation = *allocation;
690 _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
692 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
694 menubar_allocation.height = menubar_height;
695 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
697 child = gtk_bin_get_child (GTK_BIN (window));
698 if (child != NULL && gtk_widget_get_visible (child))
700 GtkAllocation child_allocation = *allocation;
703 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
704 child_allocation.x += border_width;
705 child_allocation.y += border_width + menubar_height;
706 child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
707 child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
709 gtk_widget_size_allocate (child, &child_allocation);
713 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
714 ->size_allocate (widget, allocation);
718 gtk_application_window_real_realize (GtkWidget *widget)
720 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
721 GtkApplication *application;
722 GtkSettings *settings;
724 application = gtk_window_get_application (GTK_WINDOW (window));
725 settings = gtk_widget_get_settings (widget);
727 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
728 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
729 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
730 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
732 if (!window->priv->muxer_initialised)
734 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "app", G_ACTION_GROUP (application));
735 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "win", G_ACTION_GROUP (window));
736 window->priv->muxer_initialised = TRUE;
739 gtk_application_window_update_shell_shows_app_menu (window, settings);
740 gtk_application_window_update_shell_shows_menubar (window, settings);
741 gtk_application_window_update_menubar (window);
742 gtk_application_window_update_accels (window);
744 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
747 #ifdef GDK_WINDOWING_X11
749 GdkWindow *gdkwindow;
751 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
753 if (GDK_IS_X11_WINDOW (gdkwindow))
755 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
756 g_application_get_application_id (G_APPLICATION (application)));
758 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
759 g_dbus_connection_get_unique_name (window->priv->session));
761 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
762 gtk_application_get_dbus_object_path (application));
764 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
765 window->priv->object_path);
767 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
768 gtk_application_get_app_menu_object_path (application));
770 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
771 gtk_application_get_menubar_object_path (application));
778 gtk_application_window_real_unrealize (GtkWidget *widget)
780 GtkSettings *settings;
782 settings = gtk_widget_get_settings (widget);
784 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
785 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
787 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
788 ->unrealize (widget);
792 gtk_application_window_publish (GtkApplicationWindow *window,
793 GDBusConnection *session,
794 const gchar *object_path)
796 g_assert (window->priv->session == NULL);
797 g_assert (window->priv->export_id == 0);
798 g_assert (window->priv->object_path == NULL);
800 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
801 G_ACTION_GROUP (window->priv->actions),
804 if (window->priv->export_id == 0)
807 window->priv->session = session;
808 window->priv->object_path = g_strdup (object_path);
814 gtk_application_window_unpublish (GtkApplicationWindow *window)
816 g_assert (window->priv->session != NULL);
817 g_assert (window->priv->export_id != 0);
818 g_assert (window->priv->object_path != NULL);
820 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
821 window->priv->session = NULL;
822 window->priv->export_id = 0;
824 g_free (window->priv->object_path);
825 window->priv->object_path = NULL;
829 gtk_application_window_real_map (GtkWidget *widget)
831 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
833 /* XXX could eliminate this by tweaking gtk_window_map */
834 if (window->priv->menubar)
835 gtk_widget_map (window->priv->menubar);
837 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
842 gtk_application_window_real_forall_internal (GtkContainer *container,
843 gboolean include_internal,
844 GtkCallback callback,
847 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
849 if (window->priv->menubar)
850 callback (window->priv->menubar, user_data);
852 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
853 ->forall (container, include_internal, callback, user_data);
858 gtk_application_window_get_property (GObject *object,
863 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
867 case PROP_SHOW_MENUBAR:
868 g_value_set_boolean (value, window->priv->show_menubar);
872 g_assert_not_reached ();
877 gtk_application_window_set_property (GObject *object,
882 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
886 case PROP_SHOW_MENUBAR:
887 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
891 g_assert_not_reached ();
896 gtk_application_window_dispose (GObject *object)
898 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
900 if (window->priv->menubar)
902 gtk_widget_unparent (window->priv->menubar);
903 window->priv->menubar = NULL;
906 free_accel_closures (window);
908 g_clear_object (&window->priv->app_menu_section);
909 g_clear_object (&window->priv->menubar_section);
910 g_clear_object (&window->priv->actions);
911 g_clear_object (&window->priv->accels);
912 g_clear_object (&window->priv->muxer);
914 G_OBJECT_CLASS (gtk_application_window_parent_class)
919 gtk_application_window_init (GtkApplicationWindow *window)
921 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
923 window->priv->actions = gtk_application_window_actions_new (window);
924 window->priv->app_menu_section = g_menu_new ();
925 window->priv->menubar_section = g_menu_new ();
926 window->priv->accels = gtk_accel_group_new ();
927 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
929 /* window->priv->actions is the one and only ref on the group, so when
930 * we dispose, the action group will die, disconnecting all signals.
932 g_signal_connect_swapped (window->priv->actions, "action-added",
933 G_CALLBACK (g_action_group_action_added), window);
934 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
935 G_CALLBACK (g_action_group_action_enabled_changed), window);
936 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
937 G_CALLBACK (g_action_group_action_state_changed), window);
938 g_signal_connect_swapped (window->priv->actions, "action-removed",
939 G_CALLBACK (g_action_group_action_removed), window);
941 window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
945 gtk_application_window_class_init (GtkApplicationWindowClass *class)
947 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
948 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
949 GObjectClass *object_class = G_OBJECT_CLASS (class);
951 container_class->forall = gtk_application_window_real_forall_internal;
952 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
953 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
954 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
955 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
956 widget_class->size_allocate = gtk_application_window_real_size_allocate;
957 widget_class->realize = gtk_application_window_real_realize;
958 widget_class->unrealize = gtk_application_window_real_unrealize;
959 widget_class->map = gtk_application_window_real_map;
960 object_class->get_property = gtk_application_window_get_property;
961 object_class->set_property = gtk_application_window_set_property;
962 object_class->dispose = gtk_application_window_dispose;
965 * GtkApplicationWindow:show-menubar:
967 * If this property is %TRUE, the window will display a menubar
968 * that includes the app menu and menubar, unless these are
969 * shown by the desktop shell. See gtk_application_set_app_menu()
970 * and gtk_application_set_menubar().
972 * If %FALSE, the window will not display a menubar, regardless
973 * of whether the desktop shell is showing the menus or not.
975 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
976 g_param_spec_boolean ("show-menubar",
977 P_("Show a menubar"),
978 P_("TRUE if the window should show a "
979 "menubar at the top of the window"),
980 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
981 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
982 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
986 * gtk_application_window_new:
987 * @application: a #GtkApplication
989 * Creates a new #GtkApplicationWindow.
991 * Returns: a newly created #GtkApplicationWindow
996 gtk_application_window_new (GtkApplication *application)
998 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1000 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
1001 "application", application,
1006 * gtk_application_window_get_show_menubar:
1007 * @window: a #GtkApplicationWindow
1009 * Returns whether the window will display a menubar for the app menu
1010 * and menubar as needed.
1012 * Returns: %TRUE if @window will display a menubar when needed
1017 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
1019 return window->priv->show_menubar;
1023 * gtk_application_window_set_show_menubar:
1024 * @window: a #GtkApplicationWindow
1025 * @show_menubar: whether to show a menubar when needed
1027 * Sets whether the window will display a menubar for the app menu
1028 * and menubar as needed.
1033 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
1034 gboolean show_menubar)
1036 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
1038 show_menubar = !!show_menubar;
1040 if (window->priv->show_menubar != show_menubar)
1042 window->priv->show_menubar = show_menubar;
1044 gtk_application_window_update_menubar (window);
1046 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1050 GSimpleActionObserver *
1051 gtk_application_window_create_observer (GtkApplicationWindow *window,
1052 const gchar *action_name,
1055 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
1057 return g_simple_action_observer_new (window->priv->muxer, action_name, target);
1061 gtk_application_window_get_observable (GtkApplicationWindow *window)
1063 return G_ACTION_OBSERVABLE (window->priv->muxer);
1067 gtk_application_window_get_accel_group (GtkApplicationWindow *window)
1069 return window->priv->accels;