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 * #GtkApplicationWindow 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 * <example><title>Handling fallback yourself</title>
112 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../examples/sunny.c">
113 * <xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback>
118 * The XML format understood by #GtkBuilder for #GMenuModel consists
119 * of a toplevel <tag class="starttag">menu</tag> element, which contains
120 * one or more <tag class="starttag">item</tag> elements. Each
121 * <tag class="starttag">item</tag> element contains
122 * <tag class="starttag">attribute</tag> and <tag class="starttag">link</tag>
123 * elements with a mandatory name attribute.
124 * <tag class="starttag">link</tag> elements have the same content
125 * model as <tag class="starttag">menu</tag>.
127 * Attribute values can be translated using gettext, like other #GtkBuilder
128 * content. <tag class="starttag">attribute</tag> elements can be marked for
129 * translation with a <literal>translatable="yes"</literal> attribute.
130 * It is also possible to specify message context and translator comments,
131 * using the context and comments attributes. To make use of this, the
132 * #GtkBuilder must have been given the gettext domain to use.
135 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
138 GSimpleActionGroup parent_instance;
140 } GtkApplicationWindowActions;
142 static GType gtk_application_window_actions_get_type (void);
143 static void gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
144 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
145 G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
148 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
149 const gchar *action_name,
151 GVariant *platform_data)
153 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
154 GApplication *application;
155 GApplicationClass *class;
157 application = G_APPLICATION (gtk_window_get_application (actions->window));
158 class = G_APPLICATION_GET_CLASS (application);
160 class->before_emit (application, platform_data);
161 g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
162 class->after_emit (application, platform_data);
166 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
167 const gchar *action_name,
169 GVariant *platform_data)
171 GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
172 GApplication *application;
173 GApplicationClass *class;
175 application = G_APPLICATION (gtk_window_get_application (actions->window));
176 class = G_APPLICATION_GET_CLASS (application);
178 class->before_emit (application, platform_data);
179 g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
180 class->after_emit (application, platform_data);
184 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
189 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
191 iface->activate_action_full = gtk_application_window_actions_activate_action_full;
192 iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
196 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
200 static GSimpleActionGroup *
201 gtk_application_window_actions_new (GtkApplicationWindow *window)
203 GtkApplicationWindowActions *actions;
205 actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
206 actions->window = GTK_WINDOW (window);
208 return G_SIMPLE_ACTION_GROUP (actions);
211 /* Now onto GtkApplicationWindow... */
213 struct _GtkApplicationWindowPrivate
215 GSimpleActionGroup *actions;
216 GActionObservable *muxer;
217 gboolean muxer_initialised;
219 GtkAccelGroup *accels;
220 GSList *accel_closures;
222 GMenu *app_menu_section;
223 GMenu *menubar_section;
224 gboolean show_menubar;
226 GDBusConnection *session;
234 gtk_application_window_update_menubar (GtkApplicationWindow *window)
236 gboolean should_have_menubar;
237 gboolean have_menubar;
239 have_menubar = window->priv->menubar != NULL;
241 should_have_menubar = window->priv->show_menubar &&
242 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
243 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
245 if (have_menubar && !should_have_menubar)
247 gtk_widget_unparent (window->priv->menubar);
248 window->priv->menubar = NULL;
250 gtk_widget_queue_resize (GTK_WIDGET (window));
253 if (!have_menubar && should_have_menubar)
257 combined = g_menu_new ();
258 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
259 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
261 window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->accels);
262 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
263 gtk_widget_show_all (window->priv->menubar);
264 g_object_unref (combined);
266 gtk_widget_queue_resize (GTK_WIDGET (window));
271 gtk_application_window_get_app_desktop_name (void)
273 gchar *retval = NULL;
276 GDesktopAppInfo *app_info;
277 const gchar *app_name = NULL;
280 desktop_file = g_strconcat (g_get_prgname (), ".desktop", NULL);
281 app_info = g_desktop_app_info_new (desktop_file);
282 g_free (desktop_file);
284 if (app_info != NULL)
285 app_name = g_app_info_get_name (G_APP_INFO (app_info));
287 if (app_name != NULL)
288 retval = g_strdup (app_name);
290 g_clear_object (&app_info);
291 #endif /* HAVE_GIO_UNIX */
297 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
298 GtkSettings *settings)
300 gboolean shown_by_shell;
302 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
306 /* the shell shows it, so don't show it locally */
307 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
308 g_menu_remove (window->priv->app_menu_section, 0);
312 /* the shell does not show it, so make sure we show it */
313 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
315 GMenuModel *app_menu;
317 app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
319 if (app_menu != NULL)
321 const gchar *app_name;
324 app_name = g_get_application_name ();
325 if (app_name != g_get_prgname ())
327 /* the app has set its application name, use it */
328 name = g_strdup (app_name);
332 /* get the name from .desktop file */
333 name = gtk_application_window_get_app_desktop_name ();
335 name = g_strdup (_("Application"));
338 g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
346 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
347 GtkSettings *settings)
349 gboolean shown_by_shell;
351 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
355 /* the shell shows it, so don't show it locally */
356 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
357 g_menu_remove (window->priv->menubar_section, 0);
361 /* the shell does not show it, so make sure we show it */
362 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
366 menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
369 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
381 accel_activate (GClosure *closure,
382 GValue *return_value,
383 guint n_param_values,
384 const GValue *param_values,
385 gpointer invocation_hint,
386 gpointer marshal_data)
388 AccelClosure *aclosure = (AccelClosure*)closure;
389 GActionGroup *actions;
391 actions = G_ACTION_GROUP (closure->data);
392 if (g_action_group_get_action_enabled (actions, aclosure->action_name))
394 g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
396 /* we handled the accelerator */
397 g_value_set_boolean (return_value, TRUE);
402 free_accel_closures (GtkApplicationWindow *window)
406 for (l = window->priv->accel_closures; l; l = l->next)
408 AccelClosure *closure = l->data;
410 gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
412 g_object_unref (closure->closure.data);
413 if (closure->parameter)
414 g_variant_unref (closure->parameter);
415 g_free (closure->action_name);
416 g_closure_invalidate (&closure->closure);
417 g_closure_unref (&closure->closure);
419 g_slist_free (window->priv->accel_closures);
420 window->priv->accel_closures = NULL;
424 GtkApplicationWindow *window;
425 GActionGroup *actions;
428 /* Hack. We iterate over the accel map instead of the actions,
429 * in order to pull the parameters out of accel map entries
432 add_accel_closure (gpointer data,
433 const gchar *accel_path,
435 GdkModifierType accel_mods,
439 GtkApplicationWindow *window = d->window;
440 GActionGroup *actions = d->actions;
445 AccelClosure *closure;
450 if (!g_str_has_prefix (accel_path, "<GAction>/"))
453 path = accel_path + strlen ("<GAction>/");
454 p = strchr (path, '/');
457 action_name = g_strndup (path, p - path);
458 parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
460 g_warning ("Failed to parse parameter from '%s'\n", accel_path);
464 action_name = g_strdup (path);
468 if (g_action_group_has_action (actions, action_name))
470 closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
471 g_closure_set_marshal (&closure->closure, accel_activate);
473 closure->action_name = g_strdup (action_name);
474 closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
476 window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
477 g_closure_sink (&closure->closure);
479 gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
482 g_free (action_name);
486 gtk_application_window_update_accels (GtkApplicationWindow *window)
490 free_accel_closures (window);
492 data.window = window;
493 data.actions = G_ACTION_GROUP (window->priv->muxer);
495 gtk_accel_map_foreach (&data, add_accel_closure);
499 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
503 GtkApplicationWindow *window = user_data;
505 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
506 gtk_application_window_update_menubar (window);
510 gtk_application_window_shell_shows_menubar_changed (GObject *object,
514 GtkApplicationWindow *window = user_data;
516 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
517 gtk_application_window_update_menubar (window);
521 gtk_application_window_list_actions (GActionGroup *group)
523 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
525 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
529 gtk_application_window_query_action (GActionGroup *group,
530 const gchar *action_name,
532 const GVariantType **parameter_type,
533 const GVariantType **state_type,
534 GVariant **state_hint,
537 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
539 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
540 action_name, enabled, parameter_type, state_type, state_hint, state);
544 gtk_application_window_activate_action (GActionGroup *group,
545 const gchar *action_name,
548 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
550 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
554 gtk_application_window_change_action_state (GActionGroup *group,
555 const gchar *action_name,
558 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
560 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
564 gtk_application_window_lookup_action (GActionMap *action_map,
565 const gchar *action_name)
567 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
569 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
573 gtk_application_window_add_action (GActionMap *action_map,
576 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
578 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
582 gtk_application_window_remove_action (GActionMap *action_map,
583 const gchar *action_name)
585 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
587 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
591 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
593 iface->list_actions = gtk_application_window_list_actions;
594 iface->query_action = gtk_application_window_query_action;
595 iface->activate_action = gtk_application_window_activate_action;
596 iface->change_action_state = gtk_application_window_change_action_state;
600 gtk_application_window_map_iface_init (GActionMapInterface *iface)
602 iface->lookup_action = gtk_application_window_lookup_action;
603 iface->add_action = gtk_application_window_add_action;
604 iface->remove_action = gtk_application_window_remove_action;
607 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
608 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
609 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
616 static GParamSpec *gtk_application_window_properties[N_PROPS];
619 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
620 gint *minimum_height,
621 gint *natural_height)
623 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
625 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
626 ->get_preferred_height (widget, minimum_height, natural_height);
628 if (window->priv->menubar != NULL)
630 gint menubar_min_height, menubar_nat_height;
632 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
633 *minimum_height += menubar_min_height;
634 *natural_height += menubar_nat_height;
639 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
641 gint *minimum_height,
642 gint *natural_height)
644 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
646 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
647 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
649 if (window->priv->menubar != NULL)
651 gint menubar_min_height, menubar_nat_height;
653 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
654 *minimum_height += menubar_min_height;
655 *natural_height += menubar_nat_height;
660 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
664 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
666 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
667 ->get_preferred_width (widget, minimum_width, natural_width);
669 if (window->priv->menubar != NULL)
671 gint menubar_min_width, menubar_nat_width;
673 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
674 *minimum_width = MAX (*minimum_width, menubar_min_width);
675 *natural_width = MAX (*natural_width, menubar_nat_width);
680 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
685 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
688 if (window->priv->menubar != NULL)
689 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
693 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
694 ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
696 if (window->priv->menubar != NULL)
698 gint menubar_min_width, menubar_nat_width;
700 gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
701 *minimum_width = MAX (*minimum_width, menubar_min_width);
702 *natural_width = MAX (*natural_width, menubar_nat_width);
707 gtk_application_window_real_size_allocate (GtkWidget *widget,
708 GtkAllocation *allocation)
710 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
712 if (window->priv->menubar != NULL)
714 GtkAllocation menubar_allocation = *allocation;
718 _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
720 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
722 menubar_allocation.height = menubar_height;
723 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
725 child = gtk_bin_get_child (GTK_BIN (window));
726 if (child != NULL && gtk_widget_get_visible (child))
728 GtkAllocation child_allocation = *allocation;
731 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
732 child_allocation.x += border_width;
733 child_allocation.y += border_width + menubar_height;
734 child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
735 child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
737 gtk_widget_size_allocate (child, &child_allocation);
741 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
742 ->size_allocate (widget, allocation);
746 gtk_application_window_real_realize (GtkWidget *widget)
748 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
749 GtkApplication *application;
750 GtkSettings *settings;
752 application = gtk_window_get_application (GTK_WINDOW (window));
753 settings = gtk_widget_get_settings (widget);
755 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
756 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
757 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
758 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
760 if (!window->priv->muxer_initialised)
762 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "app", G_ACTION_GROUP (application));
763 g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "win", G_ACTION_GROUP (window));
764 window->priv->muxer_initialised = TRUE;
767 gtk_application_window_update_shell_shows_app_menu (window, settings);
768 gtk_application_window_update_shell_shows_menubar (window, settings);
769 gtk_application_window_update_menubar (window);
770 gtk_application_window_update_accels (window);
772 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
775 #ifdef GDK_WINDOWING_X11
777 GdkWindow *gdkwindow;
779 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
781 if (GDK_IS_X11_WINDOW (gdkwindow) && window->priv->session)
783 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
784 g_application_get_application_id (G_APPLICATION (application)));
786 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
787 g_dbus_connection_get_unique_name (window->priv->session));
789 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
790 g_application_get_dbus_object_path (G_APPLICATION (application)));
792 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
793 window->priv->object_path);
795 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
796 gtk_application_get_app_menu_object_path (application));
798 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
799 gtk_application_get_menubar_object_path (application));
806 gtk_application_window_real_unrealize (GtkWidget *widget)
808 GtkSettings *settings;
810 settings = gtk_widget_get_settings (widget);
812 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
813 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
815 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
816 ->unrealize (widget);
820 gtk_application_window_publish (GtkApplicationWindow *window,
821 GDBusConnection *session,
822 const gchar *object_path,
825 g_assert (window->priv->session == NULL);
826 g_assert (window->priv->export_id == 0);
827 g_assert (window->priv->object_path == NULL);
828 g_assert (window->priv->id == 0);
830 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
831 G_ACTION_GROUP (window->priv->actions),
834 if (window->priv->export_id == 0)
837 window->priv->session = session;
838 window->priv->object_path = g_strdup (object_path);
839 window->priv->id = object_id;
845 gtk_application_window_unpublish (GtkApplicationWindow *window)
847 g_assert (window->priv->session != NULL);
848 g_assert (window->priv->export_id != 0);
849 g_assert (window->priv->object_path != NULL);
850 g_assert (window->priv->id != 0);
852 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
853 window->priv->session = NULL;
854 window->priv->export_id = 0;
855 window->priv->id = 0;
857 g_free (window->priv->object_path);
858 window->priv->object_path = NULL;
862 gtk_application_window_real_map (GtkWidget *widget)
864 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
866 /* XXX could eliminate this by tweaking gtk_window_map */
867 if (window->priv->menubar)
868 gtk_widget_map (window->priv->menubar);
870 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
875 gtk_application_window_real_forall_internal (GtkContainer *container,
876 gboolean include_internal,
877 GtkCallback callback,
880 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
882 if (window->priv->menubar)
883 callback (window->priv->menubar, user_data);
885 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
886 ->forall (container, include_internal, callback, user_data);
891 gtk_application_window_get_property (GObject *object,
896 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
900 case PROP_SHOW_MENUBAR:
901 g_value_set_boolean (value, window->priv->show_menubar);
905 g_assert_not_reached ();
910 gtk_application_window_set_property (GObject *object,
915 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
919 case PROP_SHOW_MENUBAR:
920 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
924 g_assert_not_reached ();
929 gtk_application_window_dispose (GObject *object)
931 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
933 if (window->priv->menubar)
935 gtk_widget_unparent (window->priv->menubar);
936 window->priv->menubar = NULL;
939 free_accel_closures (window);
941 g_clear_object (&window->priv->app_menu_section);
942 g_clear_object (&window->priv->menubar_section);
943 g_clear_object (&window->priv->actions);
944 g_clear_object (&window->priv->accels);
945 g_clear_object (&window->priv->muxer);
947 G_OBJECT_CLASS (gtk_application_window_parent_class)
952 gtk_application_window_init (GtkApplicationWindow *window)
954 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
956 window->priv->actions = gtk_application_window_actions_new (window);
957 window->priv->app_menu_section = g_menu_new ();
958 window->priv->menubar_section = g_menu_new ();
959 window->priv->accels = gtk_accel_group_new ();
960 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
962 gtk_widget_insert_action_group (GTK_WIDGET (window), "win", G_ACTION_GROUP (window->priv->actions));
964 /* window->priv->actions is the one and only ref on the group, so when
965 * we dispose, the action group will die, disconnecting all signals.
967 g_signal_connect_swapped (window->priv->actions, "action-added",
968 G_CALLBACK (g_action_group_action_added), window);
969 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
970 G_CALLBACK (g_action_group_action_enabled_changed), window);
971 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
972 G_CALLBACK (g_action_group_action_state_changed), window);
973 g_signal_connect_swapped (window->priv->actions, "action-removed",
974 G_CALLBACK (g_action_group_action_removed), window);
976 window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
980 gtk_application_window_class_init (GtkApplicationWindowClass *class)
982 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
983 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
984 GObjectClass *object_class = G_OBJECT_CLASS (class);
986 container_class->forall = gtk_application_window_real_forall_internal;
987 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
988 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
989 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
990 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
991 widget_class->size_allocate = gtk_application_window_real_size_allocate;
992 widget_class->realize = gtk_application_window_real_realize;
993 widget_class->unrealize = gtk_application_window_real_unrealize;
994 widget_class->map = gtk_application_window_real_map;
995 object_class->get_property = gtk_application_window_get_property;
996 object_class->set_property = gtk_application_window_set_property;
997 object_class->dispose = gtk_application_window_dispose;
1000 * GtkApplicationWindow:show-menubar:
1002 * If this property is %TRUE, the window will display a menubar
1003 * that includes the app menu and menubar, unless these are
1004 * shown by the desktop shell. See gtk_application_set_app_menu()
1005 * and gtk_application_set_menubar().
1007 * If %FALSE, the window will not display a menubar, regardless
1008 * of whether the desktop shell is showing the menus or not.
1010 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
1011 g_param_spec_boolean ("show-menubar",
1012 P_("Show a menubar"),
1013 P_("TRUE if the window should show a "
1014 "menubar at the top of the window"),
1015 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
1016 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
1017 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
1021 * gtk_application_window_new:
1022 * @application: a #GtkApplication
1024 * Creates a new #GtkApplicationWindow.
1026 * Returns: a newly created #GtkApplicationWindow
1031 gtk_application_window_new (GtkApplication *application)
1033 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1035 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
1036 "application", application,
1041 * gtk_application_window_get_show_menubar:
1042 * @window: a #GtkApplicationWindow
1044 * Returns whether the window will display a menubar for the app menu
1045 * and menubar as needed.
1047 * Returns: %TRUE if @window will display a menubar when needed
1052 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
1054 return window->priv->show_menubar;
1058 * gtk_application_window_set_show_menubar:
1059 * @window: a #GtkApplicationWindow
1060 * @show_menubar: whether to show a menubar when needed
1062 * Sets whether the window will display a menubar for the app menu
1063 * and menubar as needed.
1068 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
1069 gboolean show_menubar)
1071 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
1073 show_menubar = !!show_menubar;
1075 if (window->priv->show_menubar != show_menubar)
1077 window->priv->show_menubar = show_menubar;
1079 gtk_application_window_update_menubar (window);
1081 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1085 GSimpleActionObserver *
1086 gtk_application_window_create_observer (GtkApplicationWindow *window,
1087 const gchar *action_name,
1090 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
1092 return g_simple_action_observer_new (window->priv->muxer, action_name, target);
1096 gtk_application_window_get_observable (GtkApplicationWindow *window)
1098 return G_ACTION_OBSERVABLE (window->priv->muxer);
1102 gtk_application_window_get_accel_group (GtkApplicationWindow *window)
1104 return window->priv->accels;
1108 * gtk_application_window_get_id:
1109 * @window: a #GtkApplicationWindow
1111 * Returns the unique ID of the window. If the window has not yet been added to
1112 * a #GtkApplication, returns <literal>0</literal>.
1114 * Returns: the unique ID for @window, or <literal>0</literal> if the window
1115 * has not yet been added to a #GtkApplication
1120 gtk_application_window_get_id (GtkApplicationWindow *window)
1122 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), 0);
1124 return window->priv->id;