]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplicationwindow.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkapplicationwindow.c
1 /*
2  * Copyright © 2011 Canonical Limited
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Author: Ryan Lortie <desrt@desrt.ca>
18  */
19
20 #include "config.h"
21
22 #include "gtkapplicationwindow.h"
23
24 #include "gtkapplicationprivate.h"
25 #include "gtkwidgetprivate.h"
26 #include "gtkwindowprivate.h"
27 #include "gtkaccelgroup.h"
28 #include "gtkaccelmap.h"
29 #include "gtkmenubar.h"
30 #include "gtkintl.h"
31 #include "gtksettings.h"
32
33 #include <gdk/gdk.h>
34 #ifdef GDK_WINDOWING_X11
35 #include <gdk/x11/gdkx.h>
36 #endif
37
38 #ifdef HAVE_GIO_UNIX
39 #include <gio/gdesktopappinfo.h>
40 #endif
41
42 /**
43  * SECTION:gtkapplicationwindow
44  * @title: GtkApplicationWindow
45  * @short_description: GtkWindow subclass with GtkApplication support
46  *
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().
52  *
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.
60  *
61  * Note that widgets that are placed inside a GtkApplicationWindow
62  * can also activate these actions, if they implement the
63  * GtkActionable interface.
64  *
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).
68  *
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.
76  *
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.
84  *
85  * <example><title>A GtkApplicationWindow with a menubar</title>
86  * <programlisting><![CDATA[
87  * app = gtk_application_new ();
88  *
89  * builder = gtk_builder_new ();
90  * gtk_builder_add_from_string (builder,
91  *     "<interface>"
92  *     "  <menu id='menubar'>"
93  *     "    <submenu label='_Edit'>"
94  *     "      <item label='_Copy' action='win.copy'/>"
95  *     "      <item label='_Paste' action='win.paste'/>"
96  *     "    </submenu>"
97  *     "  </menu>"
98  *     "</interface>");
99  * gtk_application_set_menubar (G_APPLICATION (app),
100  *                              G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
101  * g_object_unref (builder);
102  *
103  * ...
104  *
105  * window = gtk_application_window_new (app);
106  * ]]>
107  * </programlisting>
108  * </example>
109  *
110  * <example><title>Handling fallback yourself</title>
111  * <programlisting>
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>
114  * </xi:include>
115  * </programlisting>
116  * </example>
117  *
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>.
126  *
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.
133  */
134
135 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
136 typedef struct
137 {
138   GSimpleActionGroup parent_instance;
139   GtkWindow *window;
140 } GtkApplicationWindowActions;
141
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))
146
147 static void
148 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
149                                                      const gchar        *action_name,
150                                                      GVariant           *parameter,
151                                                      GVariant           *platform_data)
152 {
153   GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
154   GApplication *application;
155   GApplicationClass *class;
156
157   application = G_APPLICATION (gtk_window_get_application (actions->window));
158   class = G_APPLICATION_GET_CLASS (application);
159
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);
163 }
164
165 static void
166 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
167                                                          const gchar        *action_name,
168                                                          GVariant           *value,
169                                                          GVariant           *platform_data)
170 {
171   GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
172   GApplication *application;
173   GApplicationClass *class;
174
175   application = G_APPLICATION (gtk_window_get_application (actions->window));
176   class = G_APPLICATION_GET_CLASS (application);
177
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);
181 }
182
183 static void
184 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
185 {
186 }
187
188 static void
189 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
190 {
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;
193 }
194
195 static void
196 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
197 {
198 }
199
200 static GSimpleActionGroup *
201 gtk_application_window_actions_new (GtkApplicationWindow *window)
202 {
203   GtkApplicationWindowActions *actions;
204
205   actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
206   actions->window = GTK_WINDOW (window);
207
208   return G_SIMPLE_ACTION_GROUP (actions);
209 }
210
211 /* Now onto GtkApplicationWindow... */
212
213 struct _GtkApplicationWindowPrivate
214 {
215   GSimpleActionGroup *actions;
216   GtkWidget *menubar;
217   GtkAccelGroup *accels;
218   GSList *accel_closures;
219
220   GMenu *app_menu_section;
221   GMenu *menubar_section;
222   gboolean show_menubar;
223
224   GDBusConnection *session;
225   gchar           *object_path;
226   guint            export_id;
227
228   guint            id;
229 };
230
231 static void
232 gtk_application_window_update_menubar (GtkApplicationWindow *window)
233 {
234   gboolean should_have_menubar;
235   gboolean have_menubar;
236
237   have_menubar = window->priv->menubar != NULL;
238
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)));
242
243   if (have_menubar && !should_have_menubar)
244     {
245       gtk_widget_unparent (window->priv->menubar);
246       window->priv->menubar = NULL;
247
248       gtk_widget_queue_resize (GTK_WIDGET (window));
249     }
250
251   if (!have_menubar && should_have_menubar)
252     {
253       GMenu *combined;
254
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));
258
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);
263
264       gtk_widget_queue_resize (GTK_WIDGET (window));
265     }
266 }
267
268 static gchar *
269 gtk_application_window_get_app_desktop_name (void)
270 {
271   gchar *retval = NULL;
272
273 #ifdef HAVE_GIO_UNIX
274   GDesktopAppInfo *app_info;
275   const gchar *app_name = NULL;
276   gchar *desktop_file;
277
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);
281
282   if (app_info != NULL)
283     app_name = g_app_info_get_name (G_APP_INFO (app_info));
284
285   if (app_name != NULL)
286     retval = g_strdup (app_name);
287
288   g_clear_object (&app_info);
289 #endif /* HAVE_GIO_UNIX */
290
291   return retval;
292 }
293
294 static void
295 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
296                                                     GtkSettings          *settings)
297 {
298   gboolean shown_by_shell;
299
300   g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
301
302   if (shown_by_shell)
303     {
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);
307     }
308   else
309     {
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)
312         {
313           GMenuModel *app_menu;
314
315           app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
316
317           if (app_menu != NULL)
318             {
319               const gchar *app_name;
320               gchar *name;
321
322               app_name = g_get_application_name ();
323               if (app_name != g_get_prgname ())
324                 {
325                   /* the app has set its application name, use it */
326                   name = g_strdup (app_name);
327                 }
328               else
329                 {
330                   /* get the name from .desktop file */
331                   name = gtk_application_window_get_app_desktop_name ();
332                   if (name == NULL)
333                     name = g_strdup (_("Application"));
334                 }
335
336               g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
337               g_free (name);
338             }
339         }
340     }
341 }
342
343 static void
344 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
345                                                    GtkSettings          *settings)
346 {
347   gboolean shown_by_shell;
348
349   g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
350
351   if (shown_by_shell)
352     {
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);
356     }
357   else
358     {
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)
361         {
362           GMenuModel *menubar;
363
364           menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
365
366           if (menubar != NULL)
367             g_menu_append_section (window->priv->menubar_section, NULL, menubar);
368         }
369     }
370 }
371
372 typedef struct {
373   GClosure closure;
374   gchar *action_name;
375   GVariant *parameter;
376 } AccelClosure;
377
378 static void
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)
385 {
386   AccelClosure *aclosure = (AccelClosure*)closure;
387   GActionGroup *actions;
388
389   actions = G_ACTION_GROUP (closure->data);
390   if (g_action_group_get_action_enabled (actions, aclosure->action_name))
391     {
392        g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
393
394       /* we handled the accelerator */
395       g_value_set_boolean (return_value, TRUE);
396     }
397 }
398
399 static void
400 free_accel_closures (GtkApplicationWindow *window)
401 {
402   GSList *l;
403
404   for (l = window->priv->accel_closures; l; l = l->next)
405     {
406        AccelClosure *closure = l->data;
407
408        gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
409
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);
416     }
417   g_slist_free (window->priv->accel_closures);
418   window->priv->accel_closures = NULL;
419 }
420
421 /* Hack. We iterate over the accel map instead of the actions,
422  * in order to pull the parameters out of accel map entries
423  */
424 static void
425 add_accel_closure (gpointer         data,
426                    const gchar     *accel_path,
427                    guint            accel_key,
428                    GdkModifierType  accel_mods,
429                    gboolean         changed)
430 {
431   GtkApplicationWindow *window = data;
432   GActionGroup *actions;
433   const gchar *path;
434   const gchar *p;
435   gchar *action_name;
436   GVariant *parameter;
437   AccelClosure *closure;
438
439   if (accel_key == 0)
440     return;
441
442   if (!g_str_has_prefix (accel_path, "<GAction>/"))
443     return;
444
445   path = accel_path + strlen ("<GAction>/");
446   p = strchr (path, '/');
447   if (p)
448     {
449       action_name = g_strndup (path, p - path);
450       parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
451       if (!parameter)
452         g_warning ("Failed to parse parameter from '%s'\n", accel_path);
453     }
454   else
455     {
456       action_name = g_strdup (path);
457       parameter = NULL;
458     }
459
460   actions = G_ACTION_GROUP (_gtk_widget_get_action_muxer (GTK_WIDGET (window)));
461   if (g_action_group_has_action (actions, action_name))
462     {
463       closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
464       g_closure_set_marshal (&closure->closure, accel_activate);
465
466       closure->action_name = g_strdup (action_name);
467       closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
468
469       window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
470       g_closure_sink (&closure->closure);
471
472       gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
473     }
474   else if (parameter)
475     {
476       g_variant_unref (parameter);
477     }
478
479   g_free (action_name);
480 }
481
482 static void
483 gtk_application_window_update_accels (GtkApplicationWindow *window)
484 {
485   free_accel_closures (window);
486
487   gtk_accel_map_foreach (window, add_accel_closure);
488 }
489
490 static void
491 gtk_application_window_shell_shows_app_menu_changed (GObject    *object,
492                                                      GParamSpec *pspec,
493                                                      gpointer    user_data)
494 {
495   GtkApplicationWindow *window = user_data;
496
497   gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
498   gtk_application_window_update_menubar (window);
499 }
500
501 static void
502 gtk_application_window_shell_shows_menubar_changed (GObject    *object,
503                                                     GParamSpec *pspec,
504                                                     gpointer    user_data)
505 {
506   GtkApplicationWindow *window = user_data;
507
508   gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
509   gtk_application_window_update_menubar (window);
510 }
511
512 static gchar **
513 gtk_application_window_list_actions (GActionGroup *group)
514 {
515   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
516
517   return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
518 }
519
520 static gboolean
521 gtk_application_window_query_action (GActionGroup        *group,
522                                      const gchar         *action_name,
523                                      gboolean            *enabled,
524                                      const GVariantType **parameter_type,
525                                      const GVariantType **state_type,
526                                      GVariant           **state_hint,
527                                      GVariant           **state)
528 {
529   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
530
531   return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
532                                       action_name, enabled, parameter_type, state_type, state_hint, state);
533 }
534
535 static void
536 gtk_application_window_activate_action (GActionGroup *group,
537                                         const gchar  *action_name,
538                                         GVariant     *parameter)
539 {
540   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
541
542   return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
543 }
544
545 static void
546 gtk_application_window_change_action_state (GActionGroup *group,
547                                             const gchar  *action_name,
548                                             GVariant     *state)
549 {
550   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
551
552   return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
553 }
554
555 static GAction *
556 gtk_application_window_lookup_action (GActionMap  *action_map,
557                                       const gchar *action_name)
558 {
559   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
560
561   return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
562 }
563
564 static void
565 gtk_application_window_add_action (GActionMap *action_map,
566                                    GAction    *action)
567 {
568   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
569
570   g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
571 }
572
573 static void
574 gtk_application_window_remove_action (GActionMap  *action_map,
575                                       const gchar *action_name)
576 {
577   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
578
579   g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
580 }
581
582 static void
583 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
584 {
585   iface->list_actions = gtk_application_window_list_actions;
586   iface->query_action = gtk_application_window_query_action;
587   iface->activate_action = gtk_application_window_activate_action;
588   iface->change_action_state = gtk_application_window_change_action_state;
589 }
590
591 static void
592 gtk_application_window_map_iface_init (GActionMapInterface *iface)
593 {
594   iface->lookup_action = gtk_application_window_lookup_action;
595   iface->add_action = gtk_application_window_add_action;
596   iface->remove_action = gtk_application_window_remove_action;
597 }
598
599 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
600                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
601                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
602
603 enum {
604   PROP_0,
605   PROP_SHOW_MENUBAR,
606   N_PROPS
607 };
608 static GParamSpec *gtk_application_window_properties[N_PROPS];
609
610 static void
611 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
612                                                   gint      *minimum_height,
613                                                   gint      *natural_height)
614 {
615   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
616
617   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
618     ->get_preferred_height (widget, minimum_height, natural_height);
619
620   if (window->priv->menubar != NULL)
621     {
622       gint menubar_min_height, menubar_nat_height;
623
624       gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
625       *minimum_height += menubar_min_height;
626       *natural_height += menubar_nat_height;
627     }
628 }
629
630 static void
631 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
632                                                             gint       width,
633                                                             gint      *minimum_height,
634                                                             gint      *natural_height)
635 {
636   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
637
638   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
639     ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
640
641   if (window->priv->menubar != NULL)
642     {
643       gint menubar_min_height, menubar_nat_height;
644
645       gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
646       *minimum_height += menubar_min_height;
647       *natural_height += menubar_nat_height;
648     }
649 }
650
651 static void
652 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
653                                                  gint      *minimum_width,
654                                                  gint      *natural_width)
655 {
656   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
657
658   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
659     ->get_preferred_width (widget, minimum_width, natural_width);
660
661   if (window->priv->menubar != NULL)
662     {
663       gint menubar_min_width, menubar_nat_width;
664
665       gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
666       *minimum_width = MAX (*minimum_width, menubar_min_width);
667       *natural_width = MAX (*natural_width, menubar_nat_width);
668     }
669 }
670
671 static void
672 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
673                                                             gint       height,
674                                                             gint      *minimum_width,
675                                                             gint      *natural_width)
676 {
677   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
678   gint menubar_height;
679
680   if (window->priv->menubar != NULL)
681     gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
682   else
683     menubar_height = 0;
684
685   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
686     ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
687
688   if (window->priv->menubar != NULL)
689     {
690       gint menubar_min_width, menubar_nat_width;
691
692       gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
693       *minimum_width = MAX (*minimum_width, menubar_min_width);
694       *natural_width = MAX (*natural_width, menubar_nat_width);
695     }
696 }
697
698 static void
699 gtk_application_window_real_size_allocate (GtkWidget     *widget,
700                                            GtkAllocation *allocation)
701 {
702   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
703
704   if (window->priv->menubar != NULL)
705     {
706       GtkAllocation menubar_allocation = *allocation;
707       gint menubar_height;
708       GtkWidget *child;
709
710       _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
711
712       gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
713
714       menubar_allocation.height = menubar_height;
715       gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
716
717       child = gtk_bin_get_child (GTK_BIN (window));
718       if (child != NULL && gtk_widget_get_visible (child))
719         {
720           GtkAllocation child_allocation = *allocation;
721           gint border_width;
722
723           border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
724           child_allocation.x += border_width;
725           child_allocation.y += border_width + menubar_height;
726           child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
727           child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
728
729           gtk_widget_size_allocate (child, &child_allocation);
730         }
731     }
732   else
733     GTK_WIDGET_CLASS (gtk_application_window_parent_class)
734       ->size_allocate (widget, allocation);
735 }
736
737 static void
738 gtk_application_window_real_realize (GtkWidget *widget)
739 {
740   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
741   GtkApplication *application;
742   GtkSettings *settings;
743
744   application = gtk_window_get_application (GTK_WINDOW (window));
745   settings = gtk_widget_get_settings (widget);
746
747   g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
748                     G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
749   g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
750                     G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
751
752   gtk_application_window_update_shell_shows_app_menu (window, settings);
753   gtk_application_window_update_shell_shows_menubar (window, settings);
754   gtk_application_window_update_menubar (window);
755   gtk_application_window_update_accels (window);
756
757   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
758     ->realize (widget);
759
760 #ifdef GDK_WINDOWING_X11
761   {
762     GdkWindow *gdkwindow;
763
764     gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
765
766     if (GDK_IS_X11_WINDOW (gdkwindow) && window->priv->session)
767       {
768         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
769                                           g_application_get_application_id (G_APPLICATION (application)));
770
771         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
772                                           g_dbus_connection_get_unique_name (window->priv->session));
773
774         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
775                                           g_application_get_dbus_object_path (G_APPLICATION (application)));
776
777         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
778                                           window->priv->object_path);
779
780         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
781                                           gtk_application_get_app_menu_object_path (application));
782
783         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
784                                           gtk_application_get_menubar_object_path (application));
785       }
786   }
787 #endif
788 }
789
790 static void
791 gtk_application_window_real_unrealize (GtkWidget *widget)
792 {
793   GtkSettings *settings;
794
795   settings = gtk_widget_get_settings (widget);
796
797   g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
798   g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
799
800   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
801     ->unrealize (widget);
802 }
803
804 gboolean
805 gtk_application_window_publish (GtkApplicationWindow *window,
806                                 GDBusConnection      *session,
807                                 const gchar          *object_path,
808                                 guint                 object_id)
809 {
810   g_assert (window->priv->session == NULL);
811   g_assert (window->priv->export_id == 0);
812   g_assert (window->priv->object_path == NULL);
813   g_assert (window->priv->id == 0);
814
815   window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
816                                                                    G_ACTION_GROUP (window->priv->actions),
817                                                                    NULL);
818
819   if (window->priv->export_id == 0)
820     return FALSE;
821
822   window->priv->session = session;
823   window->priv->object_path = g_strdup (object_path);
824   window->priv->id = object_id;
825
826   return TRUE;
827 }
828
829 void
830 gtk_application_window_unpublish (GtkApplicationWindow *window)
831 {
832   g_assert (window->priv->session != NULL);
833   g_assert (window->priv->export_id != 0);
834   g_assert (window->priv->object_path != NULL);
835   g_assert (window->priv->id != 0);
836
837   g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
838   window->priv->session = NULL;
839   window->priv->export_id = 0;
840   window->priv->id = 0;
841
842   g_free (window->priv->object_path);
843   window->priv->object_path = NULL;
844 }
845
846 static void
847 gtk_application_window_real_map (GtkWidget *widget)
848 {
849   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
850
851   /* XXX could eliminate this by tweaking gtk_window_map */
852   if (window->priv->menubar)
853     gtk_widget_map (window->priv->menubar);
854
855   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
856     ->map (widget);
857 }
858
859 static void
860 gtk_application_window_real_forall_internal (GtkContainer *container,
861                                              gboolean      include_internal,
862                                              GtkCallback   callback,
863                                              gpointer      user_data)
864 {
865   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
866
867   if (window->priv->menubar)
868     callback (window->priv->menubar, user_data);
869
870   GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
871     ->forall (container, include_internal, callback, user_data);
872 }
873
874 static void
875 gtk_application_window_get_property (GObject    *object,
876                                      guint       prop_id,
877                                      GValue     *value,
878                                      GParamSpec *pspec)
879 {
880   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
881
882   switch (prop_id)
883     {
884     case PROP_SHOW_MENUBAR:
885       g_value_set_boolean (value, window->priv->show_menubar);
886       break;
887
888     default:
889       g_assert_not_reached ();
890     }
891 }
892
893 static void
894 gtk_application_window_set_property (GObject      *object,
895                                      guint         prop_id,
896                                      const GValue *value,
897                                      GParamSpec   *pspec)
898 {
899   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
900
901   switch (prop_id)
902     {
903     case PROP_SHOW_MENUBAR:
904       gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
905       break;
906
907     default:
908       g_assert_not_reached ();
909     }
910 }
911
912 static void
913 gtk_application_window_dispose (GObject *object)
914 {
915   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
916
917   if (window->priv->menubar)
918     {
919       gtk_widget_unparent (window->priv->menubar);
920       window->priv->menubar = NULL;
921     }
922
923   free_accel_closures (window);
924
925   g_clear_object (&window->priv->app_menu_section);
926   g_clear_object (&window->priv->menubar_section);
927   g_clear_object (&window->priv->actions);
928   g_clear_object (&window->priv->accels);
929
930   G_OBJECT_CLASS (gtk_application_window_parent_class)
931     ->dispose (object);
932 }
933
934 static void
935 gtk_application_window_init (GtkApplicationWindow *window)
936 {
937   window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
938
939   window->priv->actions = gtk_application_window_actions_new (window);
940   window->priv->app_menu_section = g_menu_new ();
941   window->priv->menubar_section = g_menu_new ();
942   window->priv->accels = gtk_accel_group_new ();
943   gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
944
945   gtk_widget_insert_action_group (GTK_WIDGET (window), "win", G_ACTION_GROUP (window->priv->actions));
946
947   /* window->priv->actions is the one and only ref on the group, so when
948    * we dispose, the action group will die, disconnecting all signals.
949    */
950   g_signal_connect_swapped (window->priv->actions, "action-added",
951                             G_CALLBACK (g_action_group_action_added), window);
952   g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
953                             G_CALLBACK (g_action_group_action_enabled_changed), window);
954   g_signal_connect_swapped (window->priv->actions, "action-state-changed",
955                             G_CALLBACK (g_action_group_action_state_changed), window);
956   g_signal_connect_swapped (window->priv->actions, "action-removed",
957                             G_CALLBACK (g_action_group_action_removed), window);
958 }
959
960 static void
961 gtk_application_window_class_init (GtkApplicationWindowClass *class)
962 {
963   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
964   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
965   GObjectClass *object_class = G_OBJECT_CLASS (class);
966
967   container_class->forall = gtk_application_window_real_forall_internal;
968   widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
969   widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
970   widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
971   widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
972   widget_class->size_allocate = gtk_application_window_real_size_allocate;
973   widget_class->realize = gtk_application_window_real_realize;
974   widget_class->unrealize = gtk_application_window_real_unrealize;
975   widget_class->map = gtk_application_window_real_map;
976   object_class->get_property = gtk_application_window_get_property;
977   object_class->set_property = gtk_application_window_set_property;
978   object_class->dispose = gtk_application_window_dispose;
979
980   /**
981    * GtkApplicationWindow:show-menubar:
982    *
983    * If this property is %TRUE, the window will display a menubar
984    * that includes the app menu and menubar, unless these are
985    * shown by the desktop shell. See gtk_application_set_app_menu()
986    * and gtk_application_set_menubar().
987    *
988    * If %FALSE, the window will not display a menubar, regardless
989    * of whether the desktop shell is showing the menus or not.
990    */
991   gtk_application_window_properties[PROP_SHOW_MENUBAR] =
992     g_param_spec_boolean ("show-menubar",
993                           P_("Show a menubar"),
994                           P_("TRUE if the window should show a "
995                              "menubar at the top of the window"),
996                           TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
997   g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
998   g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
999 }
1000
1001 /**
1002  * gtk_application_window_new:
1003  * @application: a #GtkApplication
1004  *
1005  * Creates a new #GtkApplicationWindow.
1006  *
1007  * Returns: a newly created #GtkApplicationWindow
1008  *
1009  * Since: 3.4
1010  */
1011 GtkWidget *
1012 gtk_application_window_new (GtkApplication *application)
1013 {
1014   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1015
1016   return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
1017                        "application", application,
1018                        NULL);
1019 }
1020
1021 /**
1022  * gtk_application_window_get_show_menubar:
1023  * @window: a #GtkApplicationWindow
1024  *
1025  * Returns whether the window will display a menubar for the app menu
1026  * and menubar as needed.
1027  *
1028  * Returns: %TRUE if @window will display a menubar when needed
1029  *
1030  * Since: 3.4
1031  */
1032 gboolean
1033 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
1034 {
1035   return window->priv->show_menubar;
1036 }
1037
1038 /**
1039  * gtk_application_window_set_show_menubar:
1040  * @window: a #GtkApplicationWindow
1041  * @show_menubar: whether to show a menubar when needed
1042  *
1043  * Sets whether the window will display a menubar for the app menu
1044  * and menubar as needed.
1045  *
1046  * Since: 3.4
1047  */
1048 void
1049 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
1050                                          gboolean              show_menubar)
1051 {
1052   g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
1053
1054   show_menubar = !!show_menubar;
1055
1056   if (window->priv->show_menubar != show_menubar)
1057     {
1058       window->priv->show_menubar = show_menubar;
1059
1060       gtk_application_window_update_menubar (window);
1061
1062       g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1063     }
1064 }
1065
1066 GtkAccelGroup *
1067 gtk_application_window_get_accel_group (GtkApplicationWindow *window)
1068 {
1069   return window->priv->accels;
1070 }
1071
1072 /**
1073  * gtk_application_window_get_id:
1074  * @window: a #GtkApplicationWindow
1075  *
1076  * Returns the unique ID of the window. If the window has not yet been added to
1077  * a #GtkApplication, returns <literal>0</literal>.
1078  *
1079  * Returns: the unique ID for @window, or <literal>0</literal> if the window
1080  *   has not yet been added to a #GtkApplication
1081  *
1082  * Since: 3.6
1083  */
1084 guint
1085 gtk_application_window_get_id (GtkApplicationWindow *window)
1086 {
1087   g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), 0);
1088
1089   return window->priv->id;
1090 }