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