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