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