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