]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplicationwindow.c
Change FSF Address
[~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
32 #include <gdk/gdk.h>
33 #ifdef GDK_WINDOWING_X11
34 #include <gdk/x11/gdkx.h>
35 #endif
36
37 /**
38  * SECTION:gtkapplicationwindow
39  * @title: GtkApplicationWindow
40  * @short_description: GtkWindow subclass with GtkApplication support
41  *
42  * GtkApplicationWindow is a #GtkWindow subclass that offers some
43  * extra functionality for better integration with #GtkApplication
44  * features.  Notably, it can handle both the application menu as well
45  * as the menubar. See gtk_application_set_app_menu() and
46  * gtk_application_set_menubar().
47  *
48  * This class implements the #GActionGroup and #GActionMap interfaces,
49  * to let you add window-specific actions that will be exported by the
50  * associated #GtkApplication, together with its application-wide
51  * actions.  Window-specific actions are prefixed with the "win."
52  * prefix and application-wide actions are prefixed with the "app."
53  * prefix.  Actions must be addressed with the prefixed name when
54  * referring to them from a #GMenuModel.
55  *
56  * Note that widgets that are placed inside a GtkApplicationWindow
57  * can also activate these actions, if they implement the
58  * GtkActionable interface.
59  *
60  * As with #GtkApplication, the GDK lock will be acquired when
61  * processing actions arriving from other processes and should therefore
62  * be held when activating actions locally (if GDK threads are enabled).
63  *
64  * The settings #GtkSettings:gtk-shell-shows-app-menu and
65  * #GtkSettings:gtk-shell-shows-menubar tell GTK+ whether the
66  * desktop environment is showing the application menu and menubar
67  * models outside the application as part of the desktop shell.
68  * For instance, on OS X, both menus will be displayed remotely;
69  * on Windows neither will be. gnome-shell (starting with version 3.4)
70  * will display the application menu, but not the menubar.
71  *
72  * If the desktop environment does not display the menubar, then
73  * #GApplicationWindow will automatically show a #GtkMenubar for it.
74  * (see the #GtkApplication docs for some screenshots of how this
75  * looks on different platforms).
76  * This behaviour can be overridden with the #GtkApplicationWindow:show-menubar
77  * property. If the desktop environment does not display the application
78  * menu, then it will automatically be included in the menubar.
79  *
80  * <example><title>A GtkApplicationWindow with a menubar</title>
81  * <programlisting><![CDATA[
82  * app = gtk_application_new ();
83  *
84  * builder = gtk_builder_new ();
85  * gtk_builder_add_from_string (builder,
86  *     "<interface>"
87  *     "  <menu id='menubar'>"
88  *     "    <submenu label='_Edit'>"
89  *     "      <item label='_Copy' action='win.copy'/>"
90  *     "      <item label='_Paste' action='win.paste'/>"
91  *     "    </submenu>"
92  *     "  </menu>"
93  *     "</interface>");
94  * gtk_application_set_menubar (G_APPLICATION (app),
95  *                              G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
96  * g_object_unref (builder);
97  *
98  * ...
99  *
100  * window = gtk_application_window_new (app);
101  * ]]>
102  * </programlisting>
103  * </example>
104  *
105  * The XML format understood by #GtkBuilder for #GMenuModel consists
106  * of a toplevel <tag class="starttag">menu</tag> element, which contains
107  * one or more <tag class="starttag">item</tag> elements. Each
108  * <tag class="starttag">item</tag> element contains
109  * <tag class="starttag">attribute</tag> and <tag class="starttag">link</tag>
110  * elements with a mandatory name attribute.
111  * <tag class="starttag">link</tag> elements have the same content
112  * model as <tag class="starttag">menu</tag>.
113  *
114  * Attribute values can be translated using gettext, like other #GtkBuilder
115  * content. <tag class="starttag">attribute</tag> elements can be marked for
116  * translation with a <literal>translatable="yes"</literal> attribute.
117  * It is also possible to specify message context and translator comments,
118  * using the context and comments attributes. To make use of this, the
119  * #GtkBuilder must have been given the gettext domain to use.
120  */
121
122 typedef GSimpleActionGroupClass GtkApplicationWindowActionsClass;
123 typedef struct
124 {
125   GSimpleActionGroup parent_instance;
126   GtkWindow *window;
127 } GtkApplicationWindowActions;
128
129 static GType gtk_application_window_actions_get_type   (void);
130 static void  gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface);
131 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindowActions, gtk_application_window_actions, G_TYPE_SIMPLE_ACTION_GROUP,
132                          G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, gtk_application_window_actions_iface_init))
133
134 static void
135 gtk_application_window_actions_activate_action_full (GRemoteActionGroup *remote,
136                                                      const gchar        *action_name,
137                                                      GVariant           *parameter,
138                                                      GVariant           *platform_data)
139 {
140   GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
141   GApplication *application;
142   GApplicationClass *class;
143
144   application = G_APPLICATION (gtk_window_get_application (actions->window));
145   class = G_APPLICATION_GET_CLASS (application);
146
147   class->before_emit (application, platform_data);
148   g_action_group_activate_action (G_ACTION_GROUP (actions), action_name, parameter);
149   class->after_emit (application, platform_data);
150 }
151
152 static void
153 gtk_application_window_actions_change_action_state_full (GRemoteActionGroup *remote,
154                                                          const gchar        *action_name,
155                                                          GVariant           *value,
156                                                          GVariant           *platform_data)
157 {
158   GtkApplicationWindowActions *actions = (GtkApplicationWindowActions *) remote;
159   GApplication *application;
160   GApplicationClass *class;
161
162   application = G_APPLICATION (gtk_window_get_application (actions->window));
163   class = G_APPLICATION_GET_CLASS (application);
164
165   class->before_emit (application, platform_data);
166   g_action_group_change_action_state (G_ACTION_GROUP (actions), action_name, value);
167   class->after_emit (application, platform_data);
168 }
169
170 static void
171 gtk_application_window_actions_init (GtkApplicationWindowActions *actions)
172 {
173 }
174
175 static void
176 gtk_application_window_actions_iface_init (GRemoteActionGroupInterface *iface)
177 {
178   iface->activate_action_full = gtk_application_window_actions_activate_action_full;
179   iface->change_action_state_full = gtk_application_window_actions_change_action_state_full;
180 }
181
182 static void
183 gtk_application_window_actions_class_init (GtkApplicationWindowActionsClass *class)
184 {
185 }
186
187 static GSimpleActionGroup *
188 gtk_application_window_actions_new (GtkApplicationWindow *window)
189 {
190   GtkApplicationWindowActions *actions;
191
192   actions = g_object_new (gtk_application_window_actions_get_type (), NULL);
193   actions->window = GTK_WINDOW (window);
194
195   return G_SIMPLE_ACTION_GROUP (actions);
196 }
197
198 /* Now onto GtkApplicationWindow... */
199
200 struct _GtkApplicationWindowPrivate
201 {
202   GSimpleActionGroup *actions;
203   GActionObservable *muxer;
204   gboolean muxer_initialised;
205   GtkWidget *menubar;
206   GtkAccelGroup *accels;
207   GSList *accel_closures;
208
209   GMenu *app_menu_section;
210   GMenu *menubar_section;
211   gboolean show_menubar;
212
213   GDBusConnection *session;
214   gchar           *object_path;
215   guint            export_id;
216 };
217
218 static void
219 gtk_application_window_update_menubar (GtkApplicationWindow *window)
220 {
221   gboolean should_have_menubar;
222   gboolean have_menubar;
223
224   have_menubar = window->priv->menubar != NULL;
225
226   should_have_menubar = window->priv->show_menubar &&
227                         (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) ||
228                          g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)));
229
230   if (have_menubar && !should_have_menubar)
231     {
232       gtk_widget_unparent (window->priv->menubar);
233       window->priv->menubar = NULL;
234
235       gtk_widget_queue_resize (GTK_WIDGET (window));
236     }
237
238   if (!have_menubar && should_have_menubar)
239     {
240       GMenu *combined;
241
242       combined = g_menu_new ();
243       g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
244       g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
245
246       window->priv->menubar = gtk_model_menu_create_menu_bar (G_MENU_MODEL (combined), window->priv->muxer, window->priv->accels);
247       gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
248       gtk_widget_show_all (window->priv->menubar);
249       g_object_unref (combined);
250
251       gtk_widget_queue_resize (GTK_WIDGET (window));
252     }
253 }
254
255 static void
256 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window,
257                                                     GtkSettings          *settings)
258 {
259   gboolean shown_by_shell;
260
261   g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
262
263   if (shown_by_shell)
264     {
265       /* the shell shows it, so don't show it locally */
266       if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
267         g_menu_remove (window->priv->app_menu_section, 0);
268     }
269   else
270     {
271       /* the shell does not show it, so make sure we show it */
272       if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
273         {
274           GMenuModel *app_menu;
275
276           app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window)));
277
278           if (app_menu != NULL)
279             {
280               const gchar *name;
281
282               name = g_get_application_name ();
283               if (name == g_get_prgname ())
284                 name = _("Application");
285               g_menu_append_submenu (window->priv->app_menu_section, name, app_menu);
286             }
287         }
288     }
289 }
290
291 static void
292 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window,
293                                                    GtkSettings          *settings)
294 {
295   gboolean shown_by_shell;
296
297   g_object_get (settings, "gtk-shell-shows-menubar", &shown_by_shell, NULL);
298
299   if (shown_by_shell)
300     {
301       /* the shell shows it, so don't show it locally */
302       if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
303         g_menu_remove (window->priv->menubar_section, 0);
304     }
305   else
306     {
307       /* the shell does not show it, so make sure we show it */
308       if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
309         {
310           GMenuModel *menubar;
311
312           menubar = gtk_application_get_menubar (gtk_window_get_application (GTK_WINDOW (window)));
313
314           if (menubar != NULL)
315             g_menu_append_section (window->priv->menubar_section, NULL, menubar);
316         }
317     }
318 }
319
320 typedef struct {
321   GClosure closure;
322   gchar *action_name;
323   GVariant *parameter;
324 } AccelClosure;
325
326 static void
327 accel_activate (GClosure     *closure,
328                 GValue       *return_value,
329                 guint         n_param_values,
330                 const GValue *param_values,
331                 gpointer      invocation_hint,
332                 gpointer      marshal_data)
333 {
334   AccelClosure *aclosure = (AccelClosure*)closure;
335   GActionGroup *actions;
336
337   actions = G_ACTION_GROUP (closure->data);
338   if (g_action_group_get_action_enabled (actions, aclosure->action_name))
339     {
340        g_action_group_activate_action (actions, aclosure->action_name, aclosure->parameter);
341
342       /* we handled the accelerator */
343       g_value_set_boolean (return_value, TRUE);
344     }
345 }
346
347 static void
348 free_accel_closures (GtkApplicationWindow *window)
349 {
350   GSList *l;
351
352   for (l = window->priv->accel_closures; l; l = l->next)
353     {
354        AccelClosure *closure = l->data;
355
356        gtk_accel_group_disconnect (window->priv->accels, &closure->closure);
357
358        g_object_unref (closure->closure.data);
359        if (closure->parameter)
360          g_variant_unref (closure->parameter);
361        g_free (closure->action_name);
362        g_closure_invalidate (&closure->closure);
363        g_closure_unref (&closure->closure);
364     }
365   g_slist_free (window->priv->accel_closures);
366   window->priv->accel_closures = NULL;
367 }
368
369 typedef struct {
370   GtkApplicationWindow *window;
371   GActionGroup *actions;
372 } AccelData;
373
374 /* Hack. We iterate over the accel map instead of the actions,
375  * in order to pull the parameters out of accel map entries
376  */
377 static void
378 add_accel_closure (gpointer         data,
379                    const gchar     *accel_path,
380                    guint            accel_key,
381                    GdkModifierType  accel_mods,
382                    gboolean         changed)
383 {
384   AccelData *d = data;
385   GtkApplicationWindow *window = d->window;
386   GActionGroup *actions = d->actions;
387   const gchar *path;
388   const gchar *p;
389   gchar *action_name;
390   GVariant *parameter;
391   AccelClosure *closure;
392
393   if (accel_key == 0)
394     return;
395
396   if (!g_str_has_prefix (accel_path, "<GAction>/"))
397     return;
398
399   path = accel_path + strlen ("<GAction>/");
400   p = strchr (path, '/');
401   if (p)
402     {
403       action_name = g_strndup (path, p - path);
404       parameter = g_variant_parse (NULL, p + 1, NULL, NULL, NULL);
405       if (!parameter)
406         g_warning ("Failed to parse parameter from '%s'\n", accel_path);
407     }
408   else
409     {
410       action_name = g_strdup (path);
411       parameter = NULL;
412     }
413
414   if (g_action_group_has_action (actions, action_name))
415     {
416       closure = (AccelClosure*) g_closure_new_object (sizeof (AccelClosure), g_object_ref (actions));
417       g_closure_set_marshal (&closure->closure, accel_activate);
418
419       closure->action_name = g_strdup (action_name);
420       closure->parameter = parameter ? g_variant_ref_sink (parameter) : NULL;
421
422       window->priv->accel_closures = g_slist_prepend (window->priv->accel_closures, g_closure_ref (&closure->closure));
423       g_closure_sink (&closure->closure);
424
425       gtk_accel_group_connect_by_path (window->priv->accels, accel_path, &closure->closure);
426     }
427
428   g_free (action_name);
429 }
430
431 static void
432 gtk_application_window_update_accels (GtkApplicationWindow *window)
433 {
434   AccelData data;
435
436   free_accel_closures (window);
437
438   data.window = window;
439   data.actions = G_ACTION_GROUP (window->priv->muxer);
440
441   gtk_accel_map_foreach (&data, add_accel_closure);
442 }
443
444 static void
445 gtk_application_window_shell_shows_app_menu_changed (GObject    *object,
446                                                      GParamSpec *pspec,
447                                                      gpointer    user_data)
448 {
449   GtkApplicationWindow *window = user_data;
450
451   gtk_application_window_update_shell_shows_app_menu (window, GTK_SETTINGS (object));
452   gtk_application_window_update_menubar (window);
453 }
454
455 static void
456 gtk_application_window_shell_shows_menubar_changed (GObject    *object,
457                                                     GParamSpec *pspec,
458                                                     gpointer    user_data)
459 {
460   GtkApplicationWindow *window = user_data;
461
462   gtk_application_window_update_shell_shows_menubar (window, GTK_SETTINGS (object));
463   gtk_application_window_update_menubar (window);
464 }
465
466 static gchar **
467 gtk_application_window_list_actions (GActionGroup *group)
468 {
469   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
470
471   return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
472 }
473
474 static gboolean
475 gtk_application_window_query_action (GActionGroup        *group,
476                                      const gchar         *action_name,
477                                      gboolean            *enabled,
478                                      const GVariantType **parameter_type,
479                                      const GVariantType **state_type,
480                                      GVariant           **state_hint,
481                                      GVariant           **state)
482 {
483   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
484
485   return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
486                                       action_name, enabled, parameter_type, state_type, state_hint, state);
487 }
488
489 static void
490 gtk_application_window_activate_action (GActionGroup *group,
491                                         const gchar  *action_name,
492                                         GVariant     *parameter)
493 {
494   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
495
496   return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
497 }
498
499 static void
500 gtk_application_window_change_action_state (GActionGroup *group,
501                                             const gchar  *action_name,
502                                             GVariant     *state)
503 {
504   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
505
506   return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
507 }
508
509 static GAction *
510 gtk_application_window_lookup_action (GActionMap  *action_map,
511                                       const gchar *action_name)
512 {
513   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
514
515   return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
516 }
517
518 static void
519 gtk_application_window_add_action (GActionMap *action_map,
520                                    GAction    *action)
521 {
522   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
523
524   g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
525 }
526
527 static void
528 gtk_application_window_remove_action (GActionMap  *action_map,
529                                       const gchar *action_name)
530 {
531   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
532
533   g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
534 }
535
536 static void
537 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
538 {
539   iface->list_actions = gtk_application_window_list_actions;
540   iface->query_action = gtk_application_window_query_action;
541   iface->activate_action = gtk_application_window_activate_action;
542   iface->change_action_state = gtk_application_window_change_action_state;
543 }
544
545 static void
546 gtk_application_window_map_iface_init (GActionMapInterface *iface)
547 {
548   iface->lookup_action = gtk_application_window_lookup_action;
549   iface->add_action = gtk_application_window_add_action;
550   iface->remove_action = gtk_application_window_remove_action;
551 }
552
553 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
554                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
555                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
556
557 enum {
558   PROP_0,
559   PROP_SHOW_MENUBAR,
560   N_PROPS
561 };
562 static GParamSpec *gtk_application_window_properties[N_PROPS];
563
564 static void
565 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
566                                                   gint      *minimum_height,
567                                                   gint      *natural_height)
568 {
569   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
570
571   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
572     ->get_preferred_height (widget, minimum_height, natural_height);
573
574   if (window->priv->menubar != NULL)
575     {
576       gint menubar_min_height, menubar_nat_height;
577
578       gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
579       *minimum_height += menubar_min_height;
580       *natural_height += menubar_nat_height;
581     }
582 }
583
584 static void
585 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
586                                                             gint       width,
587                                                             gint      *minimum_height,
588                                                             gint      *natural_height)
589 {
590   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
591
592   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
593     ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
594
595   if (window->priv->menubar != NULL)
596     {
597       gint menubar_min_height, menubar_nat_height;
598
599       gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
600       *minimum_height += menubar_min_height;
601       *natural_height += menubar_nat_height;
602     }
603 }
604
605 static void
606 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
607                                                  gint      *minimum_width,
608                                                  gint      *natural_width)
609 {
610   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
611
612   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
613     ->get_preferred_width (widget, minimum_width, natural_width);
614
615   if (window->priv->menubar != NULL)
616     {
617       gint menubar_min_width, menubar_nat_width;
618
619       gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
620       *minimum_width = MAX (*minimum_width, menubar_min_width);
621       *natural_width = MAX (*natural_width, menubar_nat_width);
622     }
623 }
624
625 static void
626 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
627                                                             gint       height,
628                                                             gint      *minimum_width,
629                                                             gint      *natural_width)
630 {
631   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
632   gint menubar_height;
633
634   if (window->priv->menubar != NULL)
635     gtk_widget_get_preferred_height (window->priv->menubar, &menubar_height, NULL);
636   else
637     menubar_height = 0;
638
639   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
640     ->get_preferred_width_for_height (widget, height - menubar_height, minimum_width, natural_width);
641
642   if (window->priv->menubar != NULL)
643     {
644       gint menubar_min_width, menubar_nat_width;
645
646       gtk_widget_get_preferred_width_for_height (window->priv->menubar, menubar_height, &menubar_min_width, &menubar_nat_width);
647       *minimum_width = MAX (*minimum_width, menubar_min_width);
648       *natural_width = MAX (*natural_width, menubar_nat_width);
649     }
650 }
651
652 static void
653 gtk_application_window_real_size_allocate (GtkWidget     *widget,
654                                            GtkAllocation *allocation)
655 {
656   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
657
658   if (window->priv->menubar != NULL)
659     {
660       GtkAllocation menubar_allocation = *allocation;
661       gint menubar_height;
662       GtkWidget *child;
663
664       _gtk_window_set_allocation (GTK_WINDOW (widget), allocation);
665
666       gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_height, NULL);
667
668       menubar_allocation.height = menubar_height;
669       gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
670
671       child = gtk_bin_get_child (GTK_BIN (window));
672       if (child != NULL && gtk_widget_get_visible (child))
673         {
674           GtkAllocation child_allocation = *allocation;
675           gint border_width;
676
677           border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
678           child_allocation.x += border_width;
679           child_allocation.y += border_width + menubar_height;
680           child_allocation.width = MAX (1, child_allocation.width - border_width * 2);
681           child_allocation.height = MAX (1, child_allocation.height - border_width * 2 - menubar_height);
682
683           gtk_widget_size_allocate (child, &child_allocation);
684         }
685     }
686   else
687     GTK_WIDGET_CLASS (gtk_application_window_parent_class)
688       ->size_allocate (widget, allocation);
689 }
690
691 static void
692 gtk_application_window_real_realize (GtkWidget *widget)
693 {
694   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
695   GtkApplication *application;
696   GtkSettings *settings;
697
698   application = gtk_window_get_application (GTK_WINDOW (window));
699   settings = gtk_widget_get_settings (widget);
700
701   g_signal_connect (settings, "notify::gtk-shell-shows-app-menu",
702                     G_CALLBACK (gtk_application_window_shell_shows_app_menu_changed), window);
703   g_signal_connect (settings, "notify::gtk-shell-shows-menubar",
704                     G_CALLBACK (gtk_application_window_shell_shows_menubar_changed), window);
705
706   if (!window->priv->muxer_initialised)
707     {
708       g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "app", G_ACTION_GROUP (application));
709       g_action_muxer_insert (G_ACTION_MUXER (window->priv->muxer), "win", G_ACTION_GROUP (window));
710       window->priv->muxer_initialised = TRUE;
711     }
712
713   gtk_application_window_update_shell_shows_app_menu (window, settings);
714   gtk_application_window_update_shell_shows_menubar (window, settings);
715   gtk_application_window_update_menubar (window);
716   gtk_application_window_update_accels (window);
717
718   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
719     ->realize (widget);
720
721 #ifdef GDK_WINDOWING_X11
722   {
723     GdkWindow *gdkwindow;
724
725     gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
726
727     if (GDK_IS_X11_WINDOW (gdkwindow))
728       {
729         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_ID",
730                                           g_application_get_application_id (G_APPLICATION (application)));
731
732         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_UNIQUE_BUS_NAME",
733                                           g_dbus_connection_get_unique_name (window->priv->session));
734
735         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APPLICATION_OBJECT_PATH",
736                                           gtk_application_get_dbus_object_path (application));
737
738         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_WINDOW_OBJECT_PATH",
739                                           window->priv->object_path);
740
741         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_APP_MENU_OBJECT_PATH",
742                                           gtk_application_get_app_menu_object_path (application));
743
744         gdk_x11_window_set_utf8_property (gdkwindow, "_GTK_MENUBAR_OBJECT_PATH",
745                                           gtk_application_get_menubar_object_path (application));
746       }
747   }
748 #endif
749 }
750
751 static void
752 gtk_application_window_real_unrealize (GtkWidget *widget)
753 {
754   GtkSettings *settings;
755
756   settings = gtk_widget_get_settings (widget);
757
758   g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_app_menu_changed, widget);
759   g_signal_handlers_disconnect_by_func (settings, gtk_application_window_shell_shows_menubar_changed, widget);
760
761   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
762     ->unrealize (widget);
763 }
764
765 gboolean
766 gtk_application_window_publish (GtkApplicationWindow *window,
767                                 GDBusConnection      *session,
768                                 const gchar          *object_path)
769 {
770   g_assert (window->priv->session == NULL);
771   g_assert (window->priv->export_id == 0);
772   g_assert (window->priv->object_path == NULL);
773
774   window->priv->export_id = g_dbus_connection_export_action_group (session, object_path,
775                                                                    G_ACTION_GROUP (window->priv->actions),
776                                                                    NULL);
777
778   if (window->priv->export_id == 0)
779     return FALSE;
780
781   window->priv->session = session;
782   window->priv->object_path = g_strdup (object_path);
783
784   return TRUE;
785 }
786
787 void
788 gtk_application_window_unpublish (GtkApplicationWindow *window)
789 {
790   g_assert (window->priv->session != NULL);
791   g_assert (window->priv->export_id != 0);
792   g_assert (window->priv->object_path != NULL);
793
794   g_dbus_connection_unexport_action_group (window->priv->session, window->priv->export_id);
795   window->priv->session = NULL;
796   window->priv->export_id = 0;
797
798   g_free (window->priv->object_path);
799   window->priv->object_path = NULL;
800 }
801
802 static void
803 gtk_application_window_real_map (GtkWidget *widget)
804 {
805   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
806
807   /* XXX could eliminate this by tweaking gtk_window_map */
808   if (window->priv->menubar)
809     gtk_widget_map (window->priv->menubar);
810
811   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
812     ->map (widget);
813 }
814
815 static void
816 gtk_application_window_real_forall_internal (GtkContainer *container,
817                                              gboolean      include_internal,
818                                              GtkCallback   callback,
819                                              gpointer      user_data)
820 {
821   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
822
823   if (window->priv->menubar)
824     callback (window->priv->menubar, user_data);
825
826   GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
827     ->forall (container, include_internal, callback, user_data);
828 }
829
830
831 static void
832 gtk_application_window_get_property (GObject    *object,
833                                      guint       prop_id,
834                                      GValue     *value,
835                                      GParamSpec *pspec)
836 {
837   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
838
839   switch (prop_id)
840     {
841     case PROP_SHOW_MENUBAR:
842       g_value_set_boolean (value, window->priv->show_menubar);
843       break;
844
845     default:
846       g_assert_not_reached ();
847     }
848 }
849
850 static void
851 gtk_application_window_set_property (GObject      *object,
852                                      guint         prop_id,
853                                      const GValue *value,
854                                      GParamSpec   *pspec)
855 {
856   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
857
858   switch (prop_id)
859     {
860     case PROP_SHOW_MENUBAR:
861       gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
862       break;
863
864     default:
865       g_assert_not_reached ();
866     }
867 }
868
869 static void
870 gtk_application_window_dispose (GObject *object)
871 {
872   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
873
874   if (window->priv->menubar)
875     {
876       gtk_widget_unparent (window->priv->menubar);
877       window->priv->menubar = NULL;
878     }
879
880   free_accel_closures (window);
881
882   g_clear_object (&window->priv->app_menu_section);
883   g_clear_object (&window->priv->menubar_section);
884   g_clear_object (&window->priv->actions);
885   g_clear_object (&window->priv->accels);
886   g_clear_object (&window->priv->muxer);
887
888   G_OBJECT_CLASS (gtk_application_window_parent_class)
889     ->dispose (object);
890 }
891
892 static void
893 gtk_application_window_init (GtkApplicationWindow *window)
894 {
895   window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
896
897   window->priv->actions = gtk_application_window_actions_new (window);
898   window->priv->app_menu_section = g_menu_new ();
899   window->priv->menubar_section = g_menu_new ();
900   window->priv->accels = gtk_accel_group_new ();
901   gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accels);
902
903   /* window->priv->actions is the one and only ref on the group, so when
904    * we dispose, the action group will die, disconnecting all signals.
905    */
906   g_signal_connect_swapped (window->priv->actions, "action-added",
907                             G_CALLBACK (g_action_group_action_added), window);
908   g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
909                             G_CALLBACK (g_action_group_action_enabled_changed), window);
910   g_signal_connect_swapped (window->priv->actions, "action-state-changed",
911                             G_CALLBACK (g_action_group_action_state_changed), window);
912   g_signal_connect_swapped (window->priv->actions, "action-removed",
913                             G_CALLBACK (g_action_group_action_removed), window);
914
915   window->priv->muxer = G_ACTION_OBSERVABLE (g_action_muxer_new ());
916 }
917
918 static void
919 gtk_application_window_class_init (GtkApplicationWindowClass *class)
920 {
921   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
922   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
923   GObjectClass *object_class = G_OBJECT_CLASS (class);
924
925   container_class->forall = gtk_application_window_real_forall_internal;
926   widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
927   widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
928   widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
929   widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
930   widget_class->size_allocate = gtk_application_window_real_size_allocate;
931   widget_class->realize = gtk_application_window_real_realize;
932   widget_class->unrealize = gtk_application_window_real_unrealize;
933   widget_class->map = gtk_application_window_real_map;
934   object_class->get_property = gtk_application_window_get_property;
935   object_class->set_property = gtk_application_window_set_property;
936   object_class->dispose = gtk_application_window_dispose;
937
938   /**
939    * GtkApplicationWindow:show-menubar:
940    *
941    * If this property is %TRUE, the window will display a menubar
942    * that includes the app menu and menubar, unless these are
943    * shown by the desktop shell. See gtk_application_set_app_menu()
944    * and gtk_application_set_menubar().
945    *
946    * If %FALSE, the window will not display a menubar, regardless
947    * of whether the desktop shell is showing the menus or not.
948    */
949   gtk_application_window_properties[PROP_SHOW_MENUBAR] =
950     g_param_spec_boolean ("show-menubar",
951                           P_("Show a menubar"),
952                           P_("TRUE if the window should show a "
953                              "menubar at the top of the window"),
954                           TRUE, G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
955   g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
956   g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
957 }
958
959 /**
960  * gtk_application_window_new:
961  * @application: a #GtkApplication
962  *
963  * Creates a new #GtkApplicationWindow.
964  *
965  * Returns: a newly created #GtkApplicationWindow
966  *
967  * Since: 3.4
968  */
969 GtkWidget *
970 gtk_application_window_new (GtkApplication *application)
971 {
972   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
973
974   return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
975                        "application", application,
976                        NULL);
977 }
978
979 /**
980  * gtk_application_window_get_show_menubar:
981  * @window: a #GtkApplicationWindow
982  *
983  * Returns whether the window will display a menubar for the app menu
984  * and menubar as needed.
985  *
986  * Returns: %TRUE if @window will display a menubar when needed
987  *
988  * Since: 3.4
989  */
990 gboolean
991 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
992 {
993   return window->priv->show_menubar;
994 }
995
996 /**
997  * gtk_application_window_set_show_menubar:
998  * @window: a #GtkApplicationWindow
999  * @show_menubar: whether to show a menubar when needed
1000  *
1001  * Sets whether the window will display a menubar for the app menu
1002  * and menubar as needed.
1003  *
1004  * Since: 3.4
1005  */
1006 void
1007 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
1008                                          gboolean              show_menubar)
1009 {
1010   g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
1011
1012   show_menubar = !!show_menubar;
1013
1014   if (window->priv->show_menubar != show_menubar)
1015     {
1016       window->priv->show_menubar = show_menubar;
1017
1018       gtk_application_window_update_menubar (window);
1019
1020       g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
1021     }
1022 }
1023
1024 GSimpleActionObserver *
1025 gtk_application_window_create_observer (GtkApplicationWindow *window,
1026                                         const gchar          *action_name,
1027                                         GVariant             *target)
1028 {
1029   g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
1030
1031   return g_simple_action_observer_new (window->priv->muxer, action_name, target);
1032 }
1033
1034 GActionObservable *
1035 gtk_application_window_get_observable (GtkApplicationWindow *window)
1036 {
1037   return G_ACTION_OBSERVABLE (window->priv->muxer);
1038 }
1039
1040 GtkAccelGroup *
1041 gtk_application_window_get_accel_group (GtkApplicationWindow *window)
1042 {
1043   return window->priv->accels;
1044 }