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"
33 #ifdef GDK_WINDOWING_X11
34 #include <gdk/x11/gdkx.h>
38 * SECTION:gtkapplicationwindow
39 * @title: GtkApplicationWindow
40 * @short_description: GtkWindow subclass with GtkApplication support
42 * GtkApplicationWindow is a #GtkWindow subclass that offers some
43 * extra functionality for better integration with #GtkApplication
44 * features. Notably, it can handle both the application menu as well
45 * as the menubar. See gtk_application_set_app_menu() and
46 * gtk_application_set_menubar().
48 * This class implements the #GActionGroup and #GActionMap interfaces,
49 * to let you add window-specific actions that will be exported by the
50 * associated #GtkApplication, together with its application-wide
51 * actions. Window-specific actions are prefixed with the "win."
52 * prefix and application-wide actions are prefixed with the "app."
53 * prefix. Actions must be addressed with the prefixed name when
54 * referring to them from a #GMenuModel.
56 * Note that widgets that are placed inside a GtkApplicationWindow
57 * can also activate these actions, if they implement the
58 * GtkActionable interface.
60 * As with #GtkApplication, the GDK lock will be acquired when
61 * processing actions arriving from other processes and should therefore
62 * be held when activating actions locally (if GDK threads are enabled).
64 * The settings #GtkSettings:gtk-shell-shows-app-menu and
65 * #GtkSettings:gtk-shell-shows-menubar tell GTK+ whether the
66 * desktop environment is showing the application menu and menubar
67 * models outside the application as part of the desktop shell.
68 * For instance, on OS X, both menus will be displayed remotely;
69 * on Windows neither will be. gnome-shell (starting with version 3.4)
70 * will display the application menu, but not the menubar.
72 * If the desktop environment does not display the menubar, then
73 * #GApplicationWindow will automatically show a #GtkMenubar for it.
74 * (see the #GtkApplication docs for some screenshots of how this
75 * looks on different platforms).
76 * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
77 * property. If the desktop environment does not display the application
78 * menu, then it will automatically be included in the menubar.
80 * <example><title>A GtkApplicationWindow with a menubar</title>
81 * <programlisting><![CDATA[
82 * app = gtk_application_new ();
84 * builder = gtk_builder_new ();
85 * gtk_builder_add_from_string (builder,
87 * " <menu id='menubar'>"
88 * " <submenu label='_Edit'>"
89 * " <item label='_Copy' action='win.copy'/>"
90 * " <item label='_Paste' action='win.paste'/>"
94 * gtk_application_set_menubar (G_APPLICATION (app),
95 * G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
96 * g_object_unref (builder);
100 * window = gtk_application_window_new (app);
105 * The XML format understood by #GtkBuilder for #GMenuModel consists
106 * of a toplevel <tag class="starttag">menu</tag> element, which contains
107 * one or more <tag class="starttag">item</tag> elements. Each
108 * <tag class="starttag">item</tag> element contains
109 * <tag class="starttag">attribute</tag> and <tag class="starttag">link</tag>
110 * elements with a mandatory name attribute.
111 * <tag class="starttag">link</tag> elements have the same content
112 * model as <tag class="starttag">menu</tag>.
114 * Attribute values can be translated using gettext, like other #GtkBuilder
115 * content. <tag class="starttag">attribute</tag> elements can be marked for
116 * translation with a <literal>translatable="yes"</literal> attribute.
117 * It is also possible to specify message context and translator comments,
118 * using the context and comments attributes. To make use of this, the
119 * #GtkBuilder must have been given the gettext domain to use.
122 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
125 GSimpleActionGroup parent_instance;
127 } GtkApplicationWindowActions;
129 static GType gtk_application_window_actions_get_type (void);
130 static void gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
131 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
132 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
135 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
136 const gchar *action_name,
138 GVariant *platform_data)
140 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
141 GApplication *application;
142 GApplicationClass *class;
144 application = G_APPLICATION (gtk_window_get_application (actions->window));
145 class = G_APPLICATION_GET_CLASS (application);
147 class->before_emit (application, platform_data);
148 g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
149 class->after_emit (application, platform_data);
153 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
154 const gchar *action_name,
156 GVariant *platform_data)
158 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
159 GApplication *application;
160 GApplicationClass *class;
162 application = G_APPLICATION (gtk_window_get_application (actions->window));
163 class = G_APPLICATION_GET_CLASS (application);
165 class->before_emit (application, platform_data);
166 g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
167 class->after_emit (application, platform_data);
171 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
176 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
178 iface->activate_action_full = gtk_application_window_actions_activate_action_full;
179 iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
183 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
187 static GSimpleActionGroup *
188 gtk_application_window_actions_new (GtkApplicationWindow *window)
190 GtkApplicationWindowActions *actions;
192 actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
193 actions->window = GTK_WINDOW (window);
195 return G_SIMPLE_ACTION_GROUP (actions);
198 /* Now onto GtkApplicationWindow... */
200 struct _GtkApplicationWindowPrivate
202 GSimpleActionGroup *actions;
203 GActionObservable *muxer;
204 gboolean muxer_initialised;
206 GtkAccelGroup *accels;
207 GSList *accel_closures;
209 GMenu *app_menu_section;
210 GMenu *menubar_section;
211 gboolean show_menubar;
213 GDBusConnection *session;
219 gtk_application_window_update_menubar (GtkApplicationWindow *window)
221 gboolean should_have_menubar;
222 gboolean have_menubar;
224 have_menubar = window->priv->menubar != NULL;
226 should_have_menubar = window->priv->show_menubar &&
227 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
228 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
230 if (have_menubar && !should_have_menubar)
232 gtk_widget_unparent (window->priv->menubar);
233 window->priv->menubar = NULL;
235 gtk_widget_queue_resize (GTK_WIDGET (window));
238 if (!have_menubar && should_have_menubar)
242 combined = g_menu_new ();
243 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
244 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
246 window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->muxer, window->priv->accels);
247 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
248 gtk_widget_show_all (window->priv->menubar);
249 g_object_unref (combined);
251 gtk_widget_queue_resize (GTK_WIDGET (window));
256 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
257 GtkSettings *settings)
259 gboolean shown_by_shell;
261 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
265 /* the shell shows it, so don't show it locally */
266 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
267 g_menu_remove (window->priv->app_menu_section, 0);
271 /* the shell does not show it, so make sure we show it */
272 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
274 GMenuModel *app_menu;
276 app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
278 if (app_menu != NULL)
282 name = g_get_application_name ();
283 if (name == g_get_prgname ())
284 name = _("Application");
285 g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
292 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
293 GtkSettings *settings)
295 gboolean shown_by_shell;
297 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
301 /* the shell shows it, so don't show it locally */
302 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
303 g_menu_remove (window->priv->menubar_section, 0);
307 /* the shell does not show it, so make sure we show it */
308 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
312 menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
315 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
327 accel_activate (GClosure *closure,
328 GValue *return_value,
329 guint n_param_values,
330 const GValue *param_values,
331 gpointer invocation_hint,
332 gpointer marshal_data)
334 AccelClosure *aclosure = (AccelClosure*)closure;
335 GActionGroup *actions;
337 actions = G_ACTION_GROUP (closure->data);
338 if (g_action_group_get_action_enabled (actions, aclosure->action_name))
340 g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
342 /* we handled the accelerator */
343 g_value_set_boolean (return_value, TRUE);
348 free_accel_closures (GtkApplicationWindow *window)
352 for (l = window->priv->accel_closures; l; l = l->next)
354 AccelClosure *closure = l->data;
356 gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
358 g_object_unref (closure->closure.data);
359 if (closure->parameter)
360 g_variant_unref (closure->parameter);
361 g_free (closure->action_name);
362 g_closure_invalidate (&closure->closure);
363 g_closure_unref (&closure->closure);
365 g_slist_free (window->priv->accel_closures);
366 window->priv->accel_closures = NULL;
370 GtkApplicationWindow *window;
371 GActionGroup *actions;
374 /* Hack. We iterate over the accel map instead of the actions,
375 * in order to pull the parameters out of accel map entries
378 add_accel_closure (gpointer data,
379 const gchar *accel_path,
381 GdkModifierType accel_mods,
385 GtkApplicationWindow *window = d->window;
386 GActionGroup *actions = d->actions;
391 AccelClosure *closure;
396 if (!g_str_has_prefix (accel_path, "<GAction>/"))
399 path = accel_path + strlen ("<GAction>/");
400 p = strchr (path, '/');
403 action_name = g_strndup (path, p - path);
404 parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
406 g_warning ("Failed to parse parameter from '%s'\n", accel_path);
410 action_name = g_strdup (path);
414 if (g_action_group_has_action (actions, action_name))
416 closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
417 g_closure_set_marshal (&closure->closure, accel_activate);
419 closure->action_name = g_strdup (action_name);
420 closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
422 window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
423 g_closure_sink (&closure->closure);
425 gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
428 g_free (action_name);
432 gtk_application_window_update_accels (GtkApplicationWindow *window)
436 free_accel_closures (window);
438 data.window = window;
439 data.actions = G_ACTION_GROUP (window->priv->muxer);
441 gtk_accel_map_foreach (&data, add_accel_closure);
445 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
449 GtkApplicationWindow *window = user_data;
451 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
452 gtk_application_window_update_menubar (window);
456 gtk_application_window_shell_shows_menubar_changed (GObject *object,
460 GtkApplicationWindow *window = user_data;
462 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
463 gtk_application_window_update_menubar (window);
467 gtk_application_window_list_actions (GActionGroup *group)
469 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
471 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
475 gtk_application_window_query_action (GActionGroup *group,
476 const gchar *action_name,
478 const GVariantType **parameter_type,
479 const GVariantType **state_type,
480 GVariant **state_hint,
483 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
485 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
486 action_name, enabled, parameter_type, state_type, state_hint, state);
490 gtk_application_window_activate_action (GActionGroup *group,
491 const gchar *action_name,
494 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
496 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
500 gtk_application_window_change_action_state (GActionGroup *group,
501 const gchar *action_name,
504 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
506 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
510 gtk_application_window_lookup_action (GActionMap *action_map,
511 const gchar *action_name)
513 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
515 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
519 gtk_application_window_add_action (GActionMap *action_map,
522 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
524 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
528 gtk_application_window_remove_action (GActionMap *action_map,
529 const gchar *action_name)
531 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
533 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
537 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
539 iface->list_actions = gtk_application_window_list_actions;
540 iface->query_action = gtk_application_window_query_action;
541 iface->activate_action = gtk_application_window_activate_action;
542 iface->change_action_state = gtk_application_window_change_action_state;
546 gtk_application_window_map_iface_init (GActionMapInterface *iface)
548 iface->lookup_action = gtk_application_window_lookup_action;
549 iface->add_action = gtk_application_window_add_action;
550 iface->remove_action = gtk_application_window_remove_action;
553 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
554 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
555 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
562 static GParamSpec *gtk_application_window_properties[N_PROPS];
565 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
566 gint *minimum_height,
567 gint *natural_height)
569 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
571 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
572 ->get_preferred_height (widget, minimum_height, natural_height);
574 if (window->priv->menubar != NULL)
576 gint menubar_min_height, menubar_nat_height;
578 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
579 *minimum_height += menubar_min_height;
580 *natural_height += menubar_nat_height;
585 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
587 gint *minimum_height,
588 gint *natural_height)
590 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
592 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
593 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
595 if (window->priv->menubar != NULL)
597 gint menubar_min_height, menubar_nat_height;
599 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
600 *minimum_height += menubar_min_height;
601 *natural_height += menubar_nat_height;
606 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
610 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
612 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
613 ->get_preferred_width (widget, minimum_width, natural_width);
615 if (window->priv->menubar != NULL)
617 gint menubar_min_width, menubar_nat_width;
619 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
620 *minimum_width = MAX (*minimum_width, menubar_min_width);
621 *natural_width = MAX (*natural_width, menubar_nat_width);
626 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
631 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
634 if (window->priv->menubar != NULL)
635 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
639 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
640 ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
642 if (window->priv->menubar != NULL)
644 gint menubar_min_width, menubar_nat_width;
646 gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
647 *minimum_width = MAX (*minimum_width, menubar_min_width);
648 *natural_width = MAX (*natural_width, menubar_nat_width);
653 gtk_application_window_real_size_allocate (GtkWidget *widget,
654 GtkAllocation *allocation)
656 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
658 if (window->priv->menubar != NULL)
660 GtkAllocation menubar_allocation = *allocation;
664 _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
666 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
668 menubar_allocation.height = menubar_height;
669 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
671 child = gtk_bin_get_child (GTK_BIN (window));
672 if (child != NULL && gtk_widget_get_visible (child))
674 GtkAllocation child_allocation = *allocation;
677 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
678 child_allocation.x += border_width;
679 child_allocation.y += border_width + menubar_height;
680 child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
681 child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
683 gtk_widget_size_allocate (child, &child_allocation);
687 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
688 ->size_allocate (widget, allocation);
692 gtk_application_window_real_realize (GtkWidget *widget)
694 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
695 GtkApplication *application;
696 GtkSettings *settings;
698 application = gtk_window_get_application (GTK_WINDOW (window));
699 settings = gtk_widget_get_settings (widget);
701 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
702 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
703 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
704 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
706 if (!window->priv->muxer_initialised)
708 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "app", G_ACTION_GROUP (application));
709 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "win", G_ACTION_GROUP (window));
710 window->priv->muxer_initialised = TRUE;
713 gtk_application_window_update_shell_shows_app_menu (window, settings);
714 gtk_application_window_update_shell_shows_menubar (window, settings);
715 gtk_application_window_update_menubar (window);
716 gtk_application_window_update_accels (window);
718 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
721 #ifdef GDK_WINDOWING_X11
723 GdkWindow *gdkwindow;
725 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
727 if (GDK_IS_X11_WINDOW (gdkwindow))
729 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
730 g_application_get_application_id (G_APPLICATION (application)));
732 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
733 g_dbus_connection_get_unique_name (window->priv->session));
735 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
736 gtk_application_get_dbus_object_path (application));
738 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
739 window->priv->object_path);
741 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
742 gtk_application_get_app_menu_object_path (application));
744 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
745 gtk_application_get_menubar_object_path (application));
752 gtk_application_window_real_unrealize (GtkWidget *widget)
754 GtkSettings *settings;
756 settings = gtk_widget_get_settings (widget);
758 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
759 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
761 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
762 ->unrealize (widget);
766 gtk_application_window_publish (GtkApplicationWindow *window,
767 GDBusConnection *session,
768 const gchar *object_path)
770 g_assert (window->priv->session == NULL);
771 g_assert (window->priv->export_id == 0);
772 g_assert (window->priv->object_path == NULL);
774 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
775 G_ACTION_GROUP (window->priv->actions),
778 if (window->priv->export_id == 0)
781 window->priv->session = session;
782 window->priv->object_path = g_strdup (object_path);
788 gtk_application_window_unpublish (GtkApplicationWindow *window)
790 g_assert (window->priv->session != NULL);
791 g_assert (window->priv->export_id != 0);
792 g_assert (window->priv->object_path != NULL);
794 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
795 window->priv->session = NULL;
796 window->priv->export_id = 0;
798 g_free (window->priv->object_path);
799 window->priv->object_path = NULL;
803 gtk_application_window_real_map (GtkWidget *widget)
805 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
807 /* XXX could eliminate this by tweaking gtk_window_map */
808 if (window->priv->menubar)
809 gtk_widget_map (window->priv->menubar);
811 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
816 gtk_application_window_real_forall_internal (GtkContainer *container,
817 gboolean include_internal,
818 GtkCallback callback,
821 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
823 if (window->priv->menubar)
824 callback (window->priv->menubar, user_data);
826 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
827 ->forall (container, include_internal, callback, user_data);
832 gtk_application_window_get_property (GObject *object,
837 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
841 case PROP_SHOW_MENUBAR:
842 g_value_set_boolean (value, window->priv->show_menubar);
846 g_assert_not_reached ();
851 gtk_application_window_set_property (GObject *object,
856 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
860 case PROP_SHOW_MENUBAR:
861 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
865 g_assert_not_reached ();
870 gtk_application_window_dispose (GObject *object)
872 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
874 if (window->priv->menubar)
876 gtk_widget_unparent (window->priv->menubar);
877 window->priv->menubar = NULL;
880 free_accel_closures (window);
882 g_clear_object (&window->priv->app_menu_section);
883 g_clear_object (&window->priv->menubar_section);
884 g_clear_object (&window->priv->actions);
885 g_clear_object (&window->priv->accels);
886 g_clear_object (&window->priv->muxer);
888 G_OBJECT_CLASS (gtk_application_window_parent_class)
893 gtk_application_window_init (GtkApplicationWindow *window)
895 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
897 window->priv->actions = gtk_application_window_actions_new (window);
898 window->priv->app_menu_section = g_menu_new ();
899 window->priv->menubar_section = g_menu_new ();
900 window->priv->accels = gtk_accel_group_new ();
901 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
903 /* window->priv->actions is the one and only ref on the group, so when
904 * we dispose, the action group will die, disconnecting all signals.
906 g_signal_connect_swapped (window->priv->actions, "action-added",
907 G_CALLBACK (g_action_group_action_added), window);
908 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
909 G_CALLBACK (g_action_group_action_enabled_changed), window);
910 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
911 G_CALLBACK (g_action_group_action_state_changed), window);
912 g_signal_connect_swapped (window->priv->actions, "action-removed",
913 G_CALLBACK (g_action_group_action_removed), window);
915 window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
919 gtk_application_window_class_init (GtkApplicationWindowClass *class)
921 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
922 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
923 GObjectClass *object_class = G_OBJECT_CLASS (class);
925 container_class->forall = gtk_application_window_real_forall_internal;
926 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
927 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
928 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
929 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
930 widget_class->size_allocate = gtk_application_window_real_size_allocate;
931 widget_class->realize = gtk_application_window_real_realize;
932 widget_class->unrealize = gtk_application_window_real_unrealize;
933 widget_class->map = gtk_application_window_real_map;
934 object_class->get_property = gtk_application_window_get_property;
935 object_class->set_property = gtk_application_window_set_property;
936 object_class->dispose = gtk_application_window_dispose;
939 * GtkApplicationWindow:show-menubar:
941 * If this property is %TRUE, the window will display a menubar
942 * that includes the app menu and menubar, unless these are
943 * shown by the desktop shell. See gtk_application_set_app_menu()
944 * and gtk_application_set_menubar().
946 * If %FALSE, the window will not display a menubar, regardless
947 * of whether the desktop shell is showing the menus or not.
949 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
950 g_param_spec_boolean ("show-menubar",
951 P_("Show a menubar"),
952 P_("TRUE if the window should show a "
953 "menubar at the top of the window"),
954 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
955 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
956 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
960 * gtk_application_window_new:
961 * @application: a #GtkApplication
963 * Creates a new #GtkApplicationWindow.
965 * Returns: a newly created #GtkApplicationWindow
970 gtk_application_window_new (GtkApplication *application)
972 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
974 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
975 "application", application,
980 * gtk_application_window_get_show_menubar:
981 * @window: a #GtkApplicationWindow
983 * Returns whether the window will display a menubar for the app menu
984 * and menubar as needed.
986 * Returns: %TRUE if @window will display a menubar when needed
991 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
993 return window->priv->show_menubar;
997 * gtk_application_window_set_show_menubar:
998 * @window: a #GtkApplicationWindow
999 * @show_menubar: whether to show a menubar when needed
1001 * Sets whether the window will display a menubar for the app menu
1002 * and menubar as needed.
1007 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
1008 gboolean show_menubar)
1010 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
1012 show_menubar = !!show_menubar;
1014 if (window->priv->show_menubar != show_menubar)
1016 window->priv->show_menubar = show_menubar;
1018 gtk_application_window_update_menubar (window);
1020 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1024 GSimpleActionObserver *
1025 gtk_application_window_create_observer (GtkApplicationWindow *window,
1026 const gchar *action_name,
1029 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
1031 return g_simple_action_observer_new (window->priv->muxer, action_name, target);
1035 gtk_application_window_get_observable (GtkApplicationWindow *window)
1037 return G_ACTION_OBSERVABLE (window->priv->muxer);
1041 gtk_application_window_get_accel_group (GtkApplicationWindow *window)
1043 return window->priv->accels;