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