]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplicationwindow.c
gtkmodelmenu: move to new action regime
[~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 "gtkwindowprivate.h"
26 #include "gtkmodelmenu.h"
27 #include "gactionmuxer.h"
28 #include "gtkaccelgroup.h"
29 #include "gtkaccelmap.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   GActionObservable *muxer;
217   gboolean muxer_initialised;
218   GtkWidget *menubar;
219   GtkAccelGroup *accels;
220   GSList *accel_closures;
221
222   GMenu *app_menu_section;
223   GMenu *menubar_section;
224   gboolean show_menubar;
225
226   GDBusConnection *session;
227   gchar           *object_path;
228   guint            export_id;
229
230   guint            id;
231 };
232
233 static void
234 gtk_application_window_update_menubar (GtkApplicationWindow *window)
235 {
236   gboolean should_have_menubar;
237   gboolean have_menubar;
238
239   have_menubar = window->priv->menubar != NULL;
240
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)));
244
245   if (have_menubar && !should_have_menubar)
246     {
247       gtk_widget_unparent (window->priv->menubar);
248       window->priv->menubar = NULL;
249
250       gtk_widget_queue_resize (GTK_WIDGET (window));
251     }
252
253   if (!have_menubar && should_have_menubar)
254     {
255       GMenu *combined;
256
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));
260
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);
265
266       gtk_widget_queue_resize (GTK_WIDGET (window));
267     }
268 }
269
270 static gchar *
271 gtk_application_window_get_app_desktop_name (void)
272 {
273   gchar *retval = NULL;
274
275 #ifdef HAVE_GIO_UNIX
276   GDesktopAppInfo *app_info;
277   const gchar *app_name = NULL;
278   gchar *desktop_file;
279
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);
283
284   if (app_info != NULL)
285     app_name = g_app_info_get_name (G_APP_INFO (app_info));
286
287   if (app_name != NULL)
288     retval = g_strdup (app_name);
289
290   g_clear_object (&app_info);
291 #endif /* HAVE_GIO_UNIX */
292
293   return retval;
294 }
295
296 static void
297 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
298                                                     GtkSettings          *settings)
299 {
300   gboolean shown_by_shell;
301
302   g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
303
304   if (shown_by_shell)
305     {
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);
309     }
310   else
311     {
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)
314         {
315           GMenuModel *app_menu;
316
317           app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
318
319           if (app_menu != NULL)
320             {
321               const gchar *app_name;
322               gchar *name;
323
324               app_name = g_get_application_name ();
325               if (app_name != g_get_prgname ())
326                 {
327                   /* the app has set its application name, use it */
328                   name = g_strdup (app_name);
329                 }
330               else
331                 {
332                   /* get the name from .desktop file */
333                   name = gtk_application_window_get_app_desktop_name ();
334                   if (name == NULL)
335                     name = g_strdup (_("Application"));
336                 }
337
338               g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
339               g_free (name);
340             }
341         }
342     }
343 }
344
345 static void
346 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
347                                                    GtkSettings          *settings)
348 {
349   gboolean shown_by_shell;
350
351   g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
352
353   if (shown_by_shell)
354     {
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);
358     }
359   else
360     {
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)
363         {
364           GMenuModel *menubar;
365
366           menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
367
368           if (menubar != NULL)
369             g_menu_append_section (window->priv->menubar_section, NULL, menubar);
370         }
371     }
372 }
373
374 typedef struct {
375   GClosure closure;
376   gchar *action_name;
377   GVariant *parameter;
378 } AccelClosure;
379
380 static void
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)
387 {
388   AccelClosure *aclosure = (AccelClosure*)closure;
389   GActionGroup *actions;
390
391   actions = G_ACTION_GROUP (closure->data);
392   if (g_action_group_get_action_enabled (actions, aclosure->action_name))
393     {
394        g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
395
396       /* we handled the accelerator */
397       g_value_set_boolean (return_value, TRUE);
398     }
399 }
400
401 static void
402 free_accel_closures (GtkApplicationWindow *window)
403 {
404   GSList *l;
405
406   for (l = window->priv->accel_closures; l; l = l->next)
407     {
408        AccelClosure *closure = l->data;
409
410        gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
411
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);
418     }
419   g_slist_free (window->priv->accel_closures);
420   window->priv->accel_closures = NULL;
421 }
422
423 typedef struct {
424   GtkApplicationWindow *window;
425   GActionGroup *actions;
426 } AccelData;
427
428 /* Hack. We iterate over the accel map instead of the actions,
429  * in order to pull the parameters out of accel map entries
430  */
431 static void
432 add_accel_closure (gpointer         data,
433                    const gchar     *accel_path,
434                    guint            accel_key,
435                    GdkModifierType  accel_mods,
436                    gboolean         changed)
437 {
438   AccelData *d = data;
439   GtkApplicationWindow *window = d->window;
440   GActionGroup *actions = d->actions;
441   const gchar *path;
442   const gchar *p;
443   gchar *action_name;
444   GVariant *parameter;
445   AccelClosure *closure;
446
447   if (accel_key == 0)
448     return;
449
450   if (!g_str_has_prefix (accel_path, "<GAction>/"))
451     return;
452
453   path = accel_path + strlen ("<GAction>/");
454   p = strchr (path, '/');
455   if (p)
456     {
457       action_name = g_strndup (path, p - path);
458       parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
459       if (!parameter)
460         g_warning ("Failed to parse parameter from '%s'\n", accel_path);
461     }
462   else
463     {
464       action_name = g_strdup (path);
465       parameter = NULL;
466     }
467
468   if (g_action_group_has_action (actions, action_name))
469     {
470       closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
471       g_closure_set_marshal (&closure->closure, accel_activate);
472
473       closure->action_name = g_strdup (action_name);
474       closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
475
476       window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
477       g_closure_sink (&closure->closure);
478
479       gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
480     }
481
482   g_free (action_name);
483 }
484
485 static void
486 gtk_application_window_update_accels (GtkApplicationWindow *window)
487 {
488   AccelData data;
489
490   free_accel_closures (window);
491
492   data.window = window;
493   data.actions = G_ACTION_GROUP (window->priv->muxer);
494
495   gtk_accel_map_foreach (&data, add_accel_closure);
496 }
497
498 static void
499 gtk_application_window_shell_shows_app_menu_changed (GObject    *object,
500                                                      GParamSpec *pspec,
501                                                      gpointer    user_data)
502 {
503   GtkApplicationWindow *window = user_data;
504
505   gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
506   gtk_application_window_update_menubar (window);
507 }
508
509 static void
510 gtk_application_window_shell_shows_menubar_changed (GObject    *object,
511                                                     GParamSpec *pspec,
512                                                     gpointer    user_data)
513 {
514   GtkApplicationWindow *window = user_data;
515
516   gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
517   gtk_application_window_update_menubar (window);
518 }
519
520 static gchar **
521 gtk_application_window_list_actions (GActionGroup *group)
522 {
523   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
524
525   return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
526 }
527
528 static gboolean
529 gtk_application_window_query_action (GActionGroup        *group,
530                                      const gchar         *action_name,
531                                      gboolean            *enabled,
532                                      const GVariantType **parameter_type,
533                                      const GVariantType **state_type,
534                                      GVariant           **state_hint,
535                                      GVariant           **state)
536 {
537   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
538
539   return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
540                                       action_name, enabled, parameter_type, state_type, state_hint, state);
541 }
542
543 static void
544 gtk_application_window_activate_action (GActionGroup *group,
545                                         const gchar  *action_name,
546                                         GVariant     *parameter)
547 {
548   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
549
550   return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
551 }
552
553 static void
554 gtk_application_window_change_action_state (GActionGroup *group,
555                                             const gchar  *action_name,
556                                             GVariant     *state)
557 {
558   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
559
560   return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
561 }
562
563 static GAction *
564 gtk_application_window_lookup_action (GActionMap  *action_map,
565                                       const gchar *action_name)
566 {
567   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
568
569   return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
570 }
571
572 static void
573 gtk_application_window_add_action (GActionMap *action_map,
574                                    GAction    *action)
575 {
576   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
577
578   g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
579 }
580
581 static void
582 gtk_application_window_remove_action (GActionMap  *action_map,
583                                       const gchar *action_name)
584 {
585   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
586
587   g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
588 }
589
590 static void
591 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
592 {
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;
597 }
598
599 static void
600 gtk_application_window_map_iface_init (GActionMapInterface *iface)
601 {
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;
605 }
606
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))
610
611 enum {
612   PROP_0,
613   PROP_SHOW_MENUBAR,
614   N_PROPS
615 };
616 static GParamSpec *gtk_application_window_properties[N_PROPS];
617
618 static void
619 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
620                                                   gint      *minimum_height,
621                                                   gint      *natural_height)
622 {
623   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
624
625   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
626     ->get_preferred_height (widget, minimum_height, natural_height);
627
628   if (window->priv->menubar != NULL)
629     {
630       gint menubar_min_height, menubar_nat_height;
631
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;
635     }
636 }
637
638 static void
639 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
640                                                             gint       width,
641                                                             gint      *minimum_height,
642                                                             gint      *natural_height)
643 {
644   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
645
646   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
647     ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
648
649   if (window->priv->menubar != NULL)
650     {
651       gint menubar_min_height, menubar_nat_height;
652
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;
656     }
657 }
658
659 static void
660 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
661                                                  gint      *minimum_width,
662                                                  gint      *natural_width)
663 {
664   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
665
666   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
667     ->get_preferred_width (widget, minimum_width, natural_width);
668
669   if (window->priv->menubar != NULL)
670     {
671       gint menubar_min_width, menubar_nat_width;
672
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);
676     }
677 }
678
679 static void
680 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
681                                                             gint       height,
682                                                             gint      *minimum_width,
683                                                             gint      *natural_width)
684 {
685   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
686   gint menubar_height;
687
688   if (window->priv->menubar != NULL)
689     gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
690   else
691     menubar_height = 0;
692
693   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
694     ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
695
696   if (window->priv->menubar != NULL)
697     {
698       gint menubar_min_width, menubar_nat_width;
699
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);
703     }
704 }
705
706 static void
707 gtk_application_window_real_size_allocate (GtkWidget     *widget,
708                                            GtkAllocation *allocation)
709 {
710   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
711
712   if (window->priv->menubar != NULL)
713     {
714       GtkAllocation menubar_allocation = *allocation;
715       gint menubar_height;
716       GtkWidget *child;
717
718       _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
719
720       gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
721
722       menubar_allocation.height = menubar_height;
723       gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
724
725       child = gtk_bin_get_child (GTK_BIN (window));
726       if (child != NULL && gtk_widget_get_visible (child))
727         {
728           GtkAllocation child_allocation = *allocation;
729           gint border_width;
730
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);
736
737           gtk_widget_size_allocate (child, &child_allocation);
738         }
739     }
740   else
741     GTK_WIDGET_CLASS (gtk_application_window_parent_class)
742       ->size_allocate (widget, allocation);
743 }
744
745 static void
746 gtk_application_window_real_realize (GtkWidget *widget)
747 {
748   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
749   GtkApplication *application;
750   GtkSettings *settings;
751
752   application = gtk_window_get_application (GTK_WINDOW (window));
753   settings = gtk_widget_get_settings (widget);
754
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);
759
760   if (!window->priv->muxer_initialised)
761     {
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;
765     }
766
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);
771
772   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
773     ->realize (widget);
774
775 #ifdef GDK_WINDOWING_X11
776   {
777     GdkWindow *gdkwindow;
778
779     gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
780
781     if (GDK_IS_X11_WINDOW (gdkwindow) && window->priv->session)
782       {
783         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
784                                           g_application_get_application_id (G_APPLICATION (application)));
785
786         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
787                                           g_dbus_connection_get_unique_name (window->priv->session));
788
789         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
790                                           g_application_get_dbus_object_path (G_APPLICATION (application)));
791
792         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
793                                           window->priv->object_path);
794
795         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
796                                           gtk_application_get_app_menu_object_path (application));
797
798         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
799                                           gtk_application_get_menubar_object_path (application));
800       }
801   }
802 #endif
803 }
804
805 static void
806 gtk_application_window_real_unrealize (GtkWidget *widget)
807 {
808   GtkSettings *settings;
809
810   settings = gtk_widget_get_settings (widget);
811
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);
814
815   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
816     ->unrealize (widget);
817 }
818
819 gboolean
820 gtk_application_window_publish (GtkApplicationWindow *window,
821                                 GDBusConnection      *session,
822                                 const gchar          *object_path,
823                                 guint                 object_id)
824 {
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);
829
830   window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
831                                                                    G_ACTION_GROUP (window->priv->actions),
832                                                                    NULL);
833
834   if (window->priv->export_id == 0)
835     return FALSE;
836
837   window->priv->session = session;
838   window->priv->object_path = g_strdup (object_path);
839   window->priv->id = object_id;
840
841   return TRUE;
842 }
843
844 void
845 gtk_application_window_unpublish (GtkApplicationWindow *window)
846 {
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);
851
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;
856
857   g_free (window->priv->object_path);
858   window->priv->object_path = NULL;
859 }
860
861 static void
862 gtk_application_window_real_map (GtkWidget *widget)
863 {
864   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
865
866   /* XXX could eliminate this by tweaking gtk_window_map */
867   if (window->priv->menubar)
868     gtk_widget_map (window->priv->menubar);
869
870   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
871     ->map (widget);
872 }
873
874 static void
875 gtk_application_window_real_forall_internal (GtkContainer *container,
876                                              gboolean      include_internal,
877                                              GtkCallback   callback,
878                                              gpointer      user_data)
879 {
880   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
881
882   if (window->priv->menubar)
883     callback (window->priv->menubar, user_data);
884
885   GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
886     ->forall (container, include_internal, callback, user_data);
887 }
888
889
890 static void
891 gtk_application_window_get_property (GObject    *object,
892                                      guint       prop_id,
893                                      GValue     *value,
894                                      GParamSpec *pspec)
895 {
896   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
897
898   switch (prop_id)
899     {
900     case PROP_SHOW_MENUBAR:
901       g_value_set_boolean (value, window->priv->show_menubar);
902       break;
903
904     default:
905       g_assert_not_reached ();
906     }
907 }
908
909 static void
910 gtk_application_window_set_property (GObject      *object,
911                                      guint         prop_id,
912                                      const GValue *value,
913                                      GParamSpec   *pspec)
914 {
915   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
916
917   switch (prop_id)
918     {
919     case PROP_SHOW_MENUBAR:
920       gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
921       break;
922
923     default:
924       g_assert_not_reached ();
925     }
926 }
927
928 static void
929 gtk_application_window_dispose (GObject *object)
930 {
931   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
932
933   if (window->priv->menubar)
934     {
935       gtk_widget_unparent (window->priv->menubar);
936       window->priv->menubar = NULL;
937     }
938
939   free_accel_closures (window);
940
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);
946
947   G_OBJECT_CLASS (gtk_application_window_parent_class)
948     ->dispose (object);
949 }
950
951 static void
952 gtk_application_window_init (GtkApplicationWindow *window)
953 {
954   window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
955
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);
961
962   gtk_widget_insert_action_group (GTK_WIDGET (window), "win", G_ACTION_GROUP (window->priv->actions));
963
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.
966    */
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);
975
976   window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
977 }
978
979 static void
980 gtk_application_window_class_init (GtkApplicationWindowClass *class)
981 {
982   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
983   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
984   GObjectClass *object_class = G_OBJECT_CLASS (class);
985
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;
998
999   /**
1000    * GtkApplicationWindow:show-menubar:
1001    *
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().
1006    *
1007    * If %FALSE, the window will not display a menubar, regardless
1008    * of whether the desktop shell is showing the menus or not.
1009    */
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));
1018 }
1019
1020 /**
1021  * gtk_application_window_new:
1022  * @application: a #GtkApplication
1023  *
1024  * Creates a new #GtkApplicationWindow.
1025  *
1026  * Returns: a newly created #GtkApplicationWindow
1027  *
1028  * Since: 3.4
1029  */
1030 GtkWidget *
1031 gtk_application_window_new (GtkApplication *application)
1032 {
1033   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
1034
1035   return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
1036                        "application", application,
1037                        NULL);
1038 }
1039
1040 /**
1041  * gtk_application_window_get_show_menubar:
1042  * @window: a #GtkApplicationWindow
1043  *
1044  * Returns whether the window will display a menubar for the app menu
1045  * and menubar as needed.
1046  *
1047  * Returns: %TRUE if @window will display a menubar when needed
1048  *
1049  * Since: 3.4
1050  */
1051 gboolean
1052 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
1053 {
1054   return window->priv->show_menubar;
1055 }
1056
1057 /**
1058  * gtk_application_window_set_show_menubar:
1059  * @window: a #GtkApplicationWindow
1060  * @show_menubar: whether to show a menubar when needed
1061  *
1062  * Sets whether the window will display a menubar for the app menu
1063  * and menubar as needed.
1064  *
1065  * Since: 3.4
1066  */
1067 void
1068 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
1069                                          gboolean              show_menubar)
1070 {
1071   g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
1072
1073   show_menubar = !!show_menubar;
1074
1075   if (window->priv->show_menubar != show_menubar)
1076     {
1077       window->priv->show_menubar = show_menubar;
1078
1079       gtk_application_window_update_menubar (window);
1080
1081       g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1082     }
1083 }
1084
1085 GSimpleActionObserver *
1086 gtk_application_window_create_observer (GtkApplicationWindow *window,
1087                                         const gchar          *action_name,
1088                                         GVariant             *target)
1089 {
1090   g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
1091
1092   return g_simple_action_observer_new (window->priv->muxer, action_name, target);
1093 }
1094
1095 GActionObservable *
1096 gtk_application_window_get_observable (GtkApplicationWindow *window)
1097 {
1098   return G_ACTION_OBSERVABLE (window->priv->muxer);
1099 }
1100
1101 GtkAccelGroup *
1102 gtk_application_window_get_accel_group (GtkApplicationWindow *window)
1103 {
1104   return window->priv->accels;
1105 }
1106
1107 /**
1108  * gtk_application_window_get_id:
1109  * @window: a #GtkApplicationWindow
1110  *
1111  * Returns the unique ID of the window. If the window has not yet been added to
1112  * a #GtkApplication, returns <literal>0</literal>.
1113  *
1114  * Returns: the unique ID for @window, or <literal>0</literal> if the window
1115  *   has not yet been added to a #GtkApplication
1116  *
1117  * Since: 3.6
1118  */
1119 guint
1120 gtk_application_window_get_id (GtkApplicationWindow *window)
1121 {
1122   g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), 0);
1123
1124   return window->priv->id;
1125 }