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