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 "gtkwidgetprivate.h"
26 #include "gtkwindowprivate.h"
27 #include "gtkaccelgroup.h"
28 #include "gtkaccelmap.h"
29 #include "gtkmenubar.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;
217 GtkAccelGroup *accels;
218 GSList *accel_closures;
220 GMenu *app_menu_section;
221 GMenu *menubar_section;
222 gboolean show_menubar;
224 GDBusConnection *session;
232 gtk_application_window_update_menubar (GtkApplicationWindow *window)
234 gboolean should_have_menubar;
235 gboolean have_menubar;
237 have_menubar = window->priv->menubar != NULL;
239 should_have_menubar = window->priv->show_menubar &&
240 (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
241 g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
243 if (have_menubar && !should_have_menubar)
245 gtk_widget_unparent (window->priv->menubar);
246 window->priv->menubar = NULL;
248 gtk_widget_queue_resize (GTK_WIDGET (window));
251 if (!have_menubar && should_have_menubar)
255 combined = g_menu_new ();
256 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
257 g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
259 window->priv->menubar = gtk_menu_bar_new_from_model (G_MENU_MODEL (combined));
260 gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
261 gtk_widget_show_all (window->priv->menubar);
262 g_object_unref (combined);
264 gtk_widget_queue_resize (GTK_WIDGET (window));
269 gtk_application_window_get_app_desktop_name (void)
271 gchar *retval = NULL;
274 GDesktopAppInfo *app_info;
275 const gchar *app_name = NULL;
278 desktop_file = g_strconcat (g_get_prgname (), ".desktop", NULL);
279 app_info = g_desktop_app_info_new (desktop_file);
280 g_free (desktop_file);
282 if (app_info != NULL)
283 app_name = g_app_info_get_name (G_APP_INFO (app_info));
285 if (app_name != NULL)
286 retval = g_strdup (app_name);
288 g_clear_object (&app_info);
289 #endif /* HAVE_GIO_UNIX */
295 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
296 GtkSettings *settings)
298 gboolean shown_by_shell;
300 g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
304 /* the shell shows it, so don't show it locally */
305 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
306 g_menu_remove (window->priv->app_menu_section, 0);
310 /* the shell does not show it, so make sure we show it */
311 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
313 GMenuModel *app_menu;
315 app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
317 if (app_menu != NULL)
319 const gchar *app_name;
322 app_name = g_get_application_name ();
323 if (app_name != g_get_prgname ())
325 /* the app has set its application name, use it */
326 name = g_strdup (app_name);
330 /* get the name from .desktop file */
331 name = gtk_application_window_get_app_desktop_name ();
333 name = g_strdup (_("Application"));
336 g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
344 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
345 GtkSettings *settings)
347 gboolean shown_by_shell;
349 g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
353 /* the shell shows it, so don't show it locally */
354 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
355 g_menu_remove (window->priv->menubar_section, 0);
359 /* the shell does not show it, so make sure we show it */
360 if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
364 menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
367 g_menu_append_section (window->priv->menubar_section, NULL, menubar);
379 accel_activate (GClosure *closure,
380 GValue *return_value,
381 guint n_param_values,
382 const GValue *param_values,
383 gpointer invocation_hint,
384 gpointer marshal_data)
386 AccelClosure *aclosure = (AccelClosure*)closure;
387 GActionGroup *actions;
389 actions = G_ACTION_GROUP (closure->data);
390 if (g_action_group_get_action_enabled (actions, aclosure->action_name))
392 g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
394 /* we handled the accelerator */
395 g_value_set_boolean (return_value, TRUE);
400 free_accel_closures (GtkApplicationWindow *window)
404 for (l = window->priv->accel_closures; l; l = l->next)
406 AccelClosure *closure = l->data;
408 gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
410 g_object_unref (closure->closure.data);
411 if (closure->parameter)
412 g_variant_unref (closure->parameter);
413 g_free (closure->action_name);
414 g_closure_invalidate (&closure->closure);
415 g_closure_unref (&closure->closure);
417 g_slist_free (window->priv->accel_closures);
418 window->priv->accel_closures = NULL;
421 /* Hack. We iterate over the accel map instead of the actions,
422 * in order to pull the parameters out of accel map entries
425 add_accel_closure (gpointer data,
426 const gchar *accel_path,
428 GdkModifierType accel_mods,
431 GtkApplicationWindow *window = data;
432 GActionGroup *actions;
437 AccelClosure *closure;
442 if (!g_str_has_prefix (accel_path, "<GAction>/"))
445 path = accel_path + strlen ("<GAction>/");
446 p = strchr (path, '/');
449 action_name = g_strndup (path, p - path);
450 parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
452 g_warning ("Failed to parse parameter from '%s'\n", accel_path);
456 action_name = g_strdup (path);
460 actions = G_ACTION_GROUP (_gtk_widget_get_action_muxer (GTK_WIDGET (window)));
461 if (g_action_group_has_action (actions, action_name))
463 closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
464 g_closure_set_marshal (&closure->closure, accel_activate);
466 closure->action_name = g_strdup (action_name);
467 closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
469 window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
470 g_closure_sink (&closure->closure);
472 gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
475 g_free (action_name);
479 gtk_application_window_update_accels (GtkApplicationWindow *window)
481 free_accel_closures (window);
483 gtk_accel_map_foreach (window, add_accel_closure);
487 gtk_application_window_shell_shows_app_menu_changed (GObject *object,
491 GtkApplicationWindow *window = user_data;
493 gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
494 gtk_application_window_update_menubar (window);
498 gtk_application_window_shell_shows_menubar_changed (GObject *object,
502 GtkApplicationWindow *window = user_data;
504 gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
505 gtk_application_window_update_menubar (window);
509 gtk_application_window_list_actions (GActionGroup *group)
511 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
513 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
517 gtk_application_window_query_action (GActionGroup *group,
518 const gchar *action_name,
520 const GVariantType **parameter_type,
521 const GVariantType **state_type,
522 GVariant **state_hint,
525 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
527 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
528 action_name, enabled, parameter_type, state_type, state_hint, state);
532 gtk_application_window_activate_action (GActionGroup *group,
533 const gchar *action_name,
536 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
538 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
542 gtk_application_window_change_action_state (GActionGroup *group,
543 const gchar *action_name,
546 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
548 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
552 gtk_application_window_lookup_action (GActionMap *action_map,
553 const gchar *action_name)
555 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
557 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
561 gtk_application_window_add_action (GActionMap *action_map,
564 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
566 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
570 gtk_application_window_remove_action (GActionMap *action_map,
571 const gchar *action_name)
573 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
575 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
579 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
581 iface->list_actions = gtk_application_window_list_actions;
582 iface->query_action = gtk_application_window_query_action;
583 iface->activate_action = gtk_application_window_activate_action;
584 iface->change_action_state = gtk_application_window_change_action_state;
588 gtk_application_window_map_iface_init (GActionMapInterface *iface)
590 iface->lookup_action = gtk_application_window_lookup_action;
591 iface->add_action = gtk_application_window_add_action;
592 iface->remove_action = gtk_application_window_remove_action;
595 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
596 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
597 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
604 static GParamSpec *gtk_application_window_properties[N_PROPS];
607 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
608 gint *minimum_height,
609 gint *natural_height)
611 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
613 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
614 ->get_preferred_height (widget, minimum_height, natural_height);
616 if (window->priv->menubar != NULL)
618 gint menubar_min_height, menubar_nat_height;
620 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
621 *minimum_height += menubar_min_height;
622 *natural_height += menubar_nat_height;
627 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
629 gint *minimum_height,
630 gint *natural_height)
632 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
634 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
635 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
637 if (window->priv->menubar != NULL)
639 gint menubar_min_height, menubar_nat_height;
641 gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
642 *minimum_height += menubar_min_height;
643 *natural_height += menubar_nat_height;
648 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
652 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
654 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
655 ->get_preferred_width (widget, minimum_width, natural_width);
657 if (window->priv->menubar != NULL)
659 gint menubar_min_width, menubar_nat_width;
661 gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
662 *minimum_width = MAX (*minimum_width, menubar_min_width);
663 *natural_width = MAX (*natural_width, menubar_nat_width);
668 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
673 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
676 if (window->priv->menubar != NULL)
677 gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
681 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
682 ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
684 if (window->priv->menubar != NULL)
686 gint menubar_min_width, menubar_nat_width;
688 gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
689 *minimum_width = MAX (*minimum_width, menubar_min_width);
690 *natural_width = MAX (*natural_width, menubar_nat_width);
695 gtk_application_window_real_size_allocate (GtkWidget *widget,
696 GtkAllocation *allocation)
698 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
700 if (window->priv->menubar != NULL)
702 GtkAllocation menubar_allocation = *allocation;
706 _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
708 gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
710 menubar_allocation.height = menubar_height;
711 gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
713 child = gtk_bin_get_child (GTK_BIN (window));
714 if (child != NULL && gtk_widget_get_visible (child))
716 GtkAllocation child_allocation = *allocation;
719 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
720 child_allocation.x += border_width;
721 child_allocation.y += border_width + menubar_height;
722 child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
723 child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
725 gtk_widget_size_allocate (child, &child_allocation);
729 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
730 ->size_allocate (widget, allocation);
734 gtk_application_window_real_realize (GtkWidget *widget)
736 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
737 GtkApplication *application;
738 GtkSettings *settings;
740 application = gtk_window_get_application (GTK_WINDOW (window));
741 settings = gtk_widget_get_settings (widget);
743 g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
744 G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
745 g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
746 G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
748 gtk_application_window_update_shell_shows_app_menu (window, settings);
749 gtk_application_window_update_shell_shows_menubar (window, settings);
750 gtk_application_window_update_menubar (window);
751 gtk_application_window_update_accels (window);
753 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
756 #ifdef GDK_WINDOWING_X11
758 GdkWindow *gdkwindow;
760 gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
762 if (GDK_IS_X11_WINDOW (gdkwindow) && window->priv->session)
764 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
765 g_application_get_application_id (G_APPLICATION (application)));
767 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
768 g_dbus_connection_get_unique_name (window->priv->session));
770 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
771 g_application_get_dbus_object_path (G_APPLICATION (application)));
773 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
774 window->priv->object_path);
776 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
777 gtk_application_get_app_menu_object_path (application));
779 gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
780 gtk_application_get_menubar_object_path (application));
787 gtk_application_window_real_unrealize (GtkWidget *widget)
789 GtkSettings *settings;
791 settings = gtk_widget_get_settings (widget);
793 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
794 g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
796 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
797 ->unrealize (widget);
801 gtk_application_window_publish (GtkApplicationWindow *window,
802 GDBusConnection *session,
803 const gchar *object_path,
806 g_assert (window->priv->session == NULL);
807 g_assert (window->priv->export_id == 0);
808 g_assert (window->priv->object_path == NULL);
809 g_assert (window->priv->id == 0);
811 window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
812 G_ACTION_GROUP (window->priv->actions),
815 if (window->priv->export_id == 0)
818 window->priv->session = session;
819 window->priv->object_path = g_strdup (object_path);
820 window->priv->id = object_id;
826 gtk_application_window_unpublish (GtkApplicationWindow *window)
828 g_assert (window->priv->session != NULL);
829 g_assert (window->priv->export_id != 0);
830 g_assert (window->priv->object_path != NULL);
831 g_assert (window->priv->id != 0);
833 g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
834 window->priv->session = NULL;
835 window->priv->export_id = 0;
836 window->priv->id = 0;
838 g_free (window->priv->object_path);
839 window->priv->object_path = NULL;
843 gtk_application_window_real_map (GtkWidget *widget)
845 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
847 /* XXX could eliminate this by tweaking gtk_window_map */
848 if (window->priv->menubar)
849 gtk_widget_map (window->priv->menubar);
851 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
856 gtk_application_window_real_forall_internal (GtkContainer *container,
857 gboolean include_internal,
858 GtkCallback callback,
861 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
863 if (window->priv->menubar)
864 callback (window->priv->menubar, user_data);
866 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
867 ->forall (container, include_internal, callback, user_data);
871 gtk_application_window_get_property (GObject *object,
876 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
880 case PROP_SHOW_MENUBAR:
881 g_value_set_boolean (value, window->priv->show_menubar);
885 g_assert_not_reached ();
890 gtk_application_window_set_property (GObject *object,
895 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
899 case PROP_SHOW_MENUBAR:
900 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
904 g_assert_not_reached ();
909 gtk_application_window_dispose (GObject *object)
911 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
913 if (window->priv->menubar)
915 gtk_widget_unparent (window->priv->menubar);
916 window->priv->menubar = NULL;
919 free_accel_closures (window);
921 g_clear_object (&window->priv->app_menu_section);
922 g_clear_object (&window->priv->menubar_section);
923 g_clear_object (&window->priv->actions);
924 g_clear_object (&window->priv->accels);
926 G_OBJECT_CLASS (gtk_application_window_parent_class)
931 gtk_application_window_init (GtkApplicationWindow *window)
933 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
935 window->priv->actions = gtk_application_window_actions_new (window);
936 window->priv->app_menu_section = g_menu_new ();
937 window->priv->menubar_section = g_menu_new ();
938 window->priv->accels = gtk_accel_group_new ();
939 gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
941 gtk_widget_insert_action_group (GTK_WIDGET (window), "win", G_ACTION_GROUP (window->priv->actions));
943 /* window->priv->actions is the one and only ref on the group, so when
944 * we dispose, the action group will die, disconnecting all signals.
946 g_signal_connect_swapped (window->priv->actions, "action-added",
947 G_CALLBACK (g_action_group_action_added), window);
948 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
949 G_CALLBACK (g_action_group_action_enabled_changed), window);
950 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
951 G_CALLBACK (g_action_group_action_state_changed), window);
952 g_signal_connect_swapped (window->priv->actions, "action-removed",
953 G_CALLBACK (g_action_group_action_removed), window);
957 gtk_application_window_class_init (GtkApplicationWindowClass *class)
959 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
960 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
961 GObjectClass *object_class = G_OBJECT_CLASS (class);
963 container_class->forall = gtk_application_window_real_forall_internal;
964 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
965 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
966 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
967 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
968 widget_class->size_allocate = gtk_application_window_real_size_allocate;
969 widget_class->realize = gtk_application_window_real_realize;
970 widget_class->unrealize = gtk_application_window_real_unrealize;
971 widget_class->map = gtk_application_window_real_map;
972 object_class->get_property = gtk_application_window_get_property;
973 object_class->set_property = gtk_application_window_set_property;
974 object_class->dispose = gtk_application_window_dispose;
977 * GtkApplicationWindow:show-menubar:
979 * If this property is %TRUE, the window will display a menubar
980 * that includes the app menu and menubar, unless these are
981 * shown by the desktop shell. See gtk_application_set_app_menu()
982 * and gtk_application_set_menubar().
984 * If %FALSE, the window will not display a menubar, regardless
985 * of whether the desktop shell is showing the menus or not.
987 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
988 g_param_spec_boolean ("show-menubar",
989 P_("Show a menubar"),
990 P_("TRUE if the window should show a "
991 "menubar at the top of the window"),
992 TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
993 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
994 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
998 * gtk_application_window_new:
999 * @application: a #GtkApplication
1001 * Creates a new #GtkApplicationWindow.
1003 * Returns: a newly created #GtkApplicationWindow
1008 gtk_application_window_new (GtkApplication *application)
1010 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1012 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
1013 "application", application,
1018 * gtk_application_window_get_show_menubar:
1019 * @window: a #GtkApplicationWindow
1021 * Returns whether the window will display a menubar for the app menu
1022 * and menubar as needed.
1024 * Returns: %TRUE if @window will display a menubar when needed
1029 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
1031 return window->priv->show_menubar;
1035 * gtk_application_window_set_show_menubar:
1036 * @window: a #GtkApplicationWindow
1037 * @show_menubar: whether to show a menubar when needed
1039 * Sets whether the window will display a menubar for the app menu
1040 * and menubar as needed.
1045 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
1046 gboolean show_menubar)
1048 g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
1050 show_menubar = !!show_menubar;
1052 if (window->priv->show_menubar != show_menubar)
1054 window->priv->show_menubar = show_menubar;
1056 gtk_application_window_update_menubar (window);
1058 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1063 gtk_application_window_get_accel_group (GtkApplicationWindow *window)
1065 return window->priv->accels;
1069 * gtk_application_window_get_id:
1070 * @window: a #GtkApplicationWindow
1072 * Returns the unique ID of the window. If the window has not yet been added to
1073 * a #GtkApplication, returns <literal>0</literal>.
1075 * Returns: the unique ID for @window, or <literal>0</literal> if the window
1076 * has not yet been added to a #GtkApplication
1081 gtk_application_window_get_id (GtkApplicationWindow *window)
1083 g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), 0);
1085 return window->priv->id;