]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplicationwindow.c
GtkApplicationWindow: Further sanitize handling of merging app menu and menubar
[~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 "gtkseparatormenuitem.h"
27 #include "gtkcheckmenuitem.h"
28 #include "gtkmenubar.h"
29 #include "gactionmuxer.h"
30 #include "gtkintl.h"
31
32 /**
33  * SECTION:gtkapplicationwindow
34  * @title: GtkApplicationWindow
35  * @short_description: GtkWindow subclass with GtkApplication support
36  *
37  * GtkApplicationWindow is a #GtkWindow subclass that offers some
38  * extra functionality for better integration with #GtkApplication
39  * features.  Notably, it can handle both the application menu as well
40  * as the menubar.  See g_application_set_app_menu() and
41  * g_application_set_menubar().
42  *
43  * This class implements the #GActionGroup and #GActionMap interfaces,
44  * to let you add window-specific actions that will be exported by the
45  * associated #GtkApplication, together with its application-wide
46  * actions.  Window-specific actions are prefixed with the "win."
47  * prefix and application-wide actions are prefixed with the "app."
48  * prefix.  Actions must be addressed with the prefixed name when
49  * referring to them from a #GMenuModel.
50  *
51  * If the desktop environment does not display the application menu
52  * as part of the desktop shell, then #GApplicationWindow will
53  * automatically show the menu as part of a menubar. This behaviour
54  * can be overridden with the #GtkApplicationWindow:show-app-menu
55  * property.
56  */
57 struct _GtkApplicationWindowPrivate
58 {
59   GSimpleActionGroup *actions;
60   GtkMenuBar *menubar;
61
62   guint initialized_gsetting_monitoring : 1;
63   guint shell_shows_app_menu : 1;
64   guint default_show_menubar : 1;
65   guint did_override_show_menubar : 1;
66   guint override_show_menubar : 1;
67 };
68
69 static void
70 recreate_menubar (GtkApplicationWindow *window);
71 static GtkWidget *
72 gtk_application_window_create_menubar (GtkApplicationWindow *window);
73
74 static void
75 on_shell_shows_app_menu_changed (GtkSettings *settings,
76                                  GParamSpec  *pspec,
77                                  gpointer     user_data)
78 {
79   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (user_data);
80   gboolean val;
81
82   g_object_get (settings, "gtk-shell-shows-app-menu", &val, NULL);
83   
84   if (window->priv->shell_shows_app_menu != val)
85     {
86       window->priv->shell_shows_app_menu = val;
87       recreate_menubar (window);
88     }
89 }
90
91 static gchar **
92 gtk_application_window_list_actions (GActionGroup *group)
93 {
94   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
95
96   return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
97 }
98
99 static gboolean
100 gtk_application_window_query_action (GActionGroup        *group,
101                                      const gchar         *action_name,
102                                      gboolean            *enabled,
103                                      const GVariantType **parameter_type,
104                                      const GVariantType **state_type,
105                                      GVariant           **state_hint,
106                                      GVariant           **state)
107 {
108   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
109
110   return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
111                                       action_name, enabled, parameter_type, state_type, state_hint, state);
112 }
113
114 static void
115 gtk_application_window_activate_action (GActionGroup *group,
116                                         const gchar  *action_name,
117                                         GVariant     *parameter)
118 {
119   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
120
121   return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
122 }
123
124 static void
125 gtk_application_window_change_action_state (GActionGroup *group,
126                                             const gchar  *action_name,
127                                             GVariant     *state)
128 {
129   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
130
131   return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
132 }
133
134 static GAction *
135 gtk_application_window_lookup_action (GActionMap  *action_map,
136                                       const gchar *action_name)
137 {
138   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
139
140   return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
141 }
142
143 static void
144 gtk_application_window_add_action (GActionMap *action_map,
145                                    GAction    *action)
146 {
147   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
148
149   g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
150 }
151
152 static void
153 gtk_application_window_remove_action (GActionMap  *action_map,
154                                       const gchar *action_name)
155 {
156   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
157
158   g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
159 }
160
161 static void
162 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
163 {
164   iface->list_actions = gtk_application_window_list_actions;
165   iface->query_action = gtk_application_window_query_action;
166   iface->activate_action = gtk_application_window_activate_action;
167   iface->change_action_state = gtk_application_window_change_action_state;
168 }
169
170 static void
171 gtk_application_window_map_iface_init (GActionMapInterface *iface)
172 {
173   iface->lookup_action = gtk_application_window_lookup_action;
174   iface->add_action = gtk_application_window_add_action;
175   iface->remove_action = gtk_application_window_remove_action;
176 }
177
178 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
179                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
180                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
181
182 enum {
183   PROP_0,
184   PROP_SHOW_MENUBAR,
185   N_PROPS
186 };
187 static GParamSpec *gtk_application_window_properties[N_PROPS];
188
189 static void
190 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
191                                                   gint      *minimum_height,
192                                                   gint      *natural_height)
193 {
194   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
195
196   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
197     ->get_preferred_height (widget, minimum_height, natural_height);
198
199   if (window->priv->menubar != NULL)
200     {
201       gint menubar_min_height, menubar_nat_height;
202
203       gtk_widget_get_preferred_height (GTK_WIDGET (window->priv->menubar), &menubar_min_height, &menubar_nat_height);
204       *minimum_height += menubar_min_height;
205       *natural_height += menubar_nat_height;
206     }
207 }
208
209 static void
210 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
211                                                             gint       width,
212                                                             gint      *minimum_height,
213                                                             gint      *natural_height)
214 {
215   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
216
217   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
218     ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
219
220   if (window->priv->menubar != NULL)
221     {
222       gint menubar_min_height, menubar_nat_height;
223
224       gtk_widget_get_preferred_height_for_width (GTK_WIDGET (window->priv->menubar), width, &menubar_min_height, &menubar_nat_height);
225       *minimum_height += menubar_min_height;
226       *natural_height += menubar_nat_height;
227     }
228 }
229
230 static void
231 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
232                                                  gint      *minimum_width,
233                                                  gint      *natural_width)
234 {
235   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
236
237   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
238     ->get_preferred_width (widget, minimum_width, natural_width);
239
240   if (window->priv->menubar != NULL)
241     {
242       gint menubar_min_width, menubar_nat_width;
243
244       gtk_widget_get_preferred_width (GTK_WIDGET (window->priv->menubar), &menubar_min_width, &menubar_nat_width);
245       *minimum_width = MAX (*minimum_width, menubar_min_width);
246       *natural_width = MAX (*natural_width, menubar_nat_width);
247     }
248 }
249
250 static void
251 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
252                                                             gint       height,
253                                                             gint      *minimum_width,
254                                                             gint      *natural_width)
255 {
256   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
257
258   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
259     ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
260
261   if (window->priv->menubar != NULL)
262     {
263       gint menubar_min_width, menubar_nat_width;
264
265       gtk_widget_get_preferred_width_for_height (GTK_WIDGET (window->priv->menubar), height, &menubar_min_width, &menubar_nat_width);
266       *minimum_width = MAX (*minimum_width, menubar_min_width);
267       *natural_width = MAX (*natural_width, menubar_nat_width);
268     }
269 }
270
271 static void
272 gtk_application_window_real_size_allocate (GtkWidget     *widget,
273                                            GtkAllocation *allocation)
274 {
275   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
276
277   if (window->priv->menubar != NULL)
278     {
279       GtkAllocation menubar_allocation = *allocation;
280       gint menubar_min_height, menubar_nat_height;
281       GtkWidget *child;
282
283       gtk_widget_get_preferred_height_for_width (GTK_WIDGET (window->priv->menubar), allocation->width, &menubar_min_height, &menubar_nat_height);
284
285       menubar_allocation.height = menubar_min_height;
286       gtk_widget_size_allocate (GTK_WIDGET (window->priv->menubar), &menubar_allocation);
287
288       child = gtk_bin_get_child (GTK_BIN (window));
289       if (child != NULL && gtk_widget_get_visible (child))
290         {
291           GtkAllocation child_allocation = *allocation;
292           gint border_width;
293
294           child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
295
296           border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
297           child_allocation.x += border_width;
298           child_allocation.y += border_width + menubar_min_height;
299           child_allocation.width -= border_width * 2;
300           child_allocation.height -= border_width * 2 - menubar_min_height;
301           gtk_widget_size_allocate (child, &child_allocation);
302         }
303
304       gtk_widget_set_allocation (widget, allocation);
305     }
306   else
307     GTK_WIDGET_CLASS (gtk_application_window_parent_class)
308       ->size_allocate (widget, allocation);
309 }
310
311 static void
312 gtk_application_window_real_realize (GtkWidget *widget)
313 {
314   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
315
316   if (!window->priv->initialized_gsetting_monitoring)
317     {
318       window->priv->initialized_gsetting_monitoring = TRUE;
319       g_signal_connect (gtk_widget_get_settings ((GtkWidget*)window),
320                         "notify::gtk-shell-shows-app-menu",
321                         G_CALLBACK (on_shell_shows_app_menu_changed),
322                         window);
323       on_shell_shows_app_menu_changed (gtk_widget_get_settings ((GtkWidget*)window), NULL, window);
324     }
325
326   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
327     ->realize (widget);
328 }
329
330 static void
331 gtk_application_window_real_map (GtkWidget *widget)
332 {
333   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
334
335   /* XXX could eliminate this by tweaking gtk_window_map */
336   if (window->priv->menubar)
337     gtk_widget_map (GTK_WIDGET (window->priv->menubar));
338
339   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
340     ->map (widget);
341 }
342
343 static void
344 gtk_application_window_real_forall_internal (GtkContainer *container,
345                                              gboolean      include_internal,
346                                              GtkCallback   callback,
347                                              gpointer      user_data)
348 {
349   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
350
351   if (window->priv->menubar)
352     callback (GTK_WIDGET (window->priv->menubar), user_data);
353
354   GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
355     ->forall (container, include_internal, callback, user_data);
356 }
357
358
359 static void
360 gtk_application_window_get_property (GObject    *object,
361                                      guint       prop_id,
362                                      GValue     *value,
363                                      GParamSpec *pspec)
364 {
365   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
366
367   switch (prop_id)
368     {
369     case PROP_SHOW_MENUBAR:
370       g_value_set_boolean (value, window->priv->override_show_menubar);
371       break;
372
373     default:
374       g_assert_not_reached ();
375     }
376 }
377
378 static void
379 gtk_application_window_set_property (GObject      *object,
380                                      guint         prop_id,
381                                      const GValue *value,
382                                      GParamSpec   *pspec)
383 {
384   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
385
386   switch (prop_id)
387     {
388     case PROP_SHOW_MENUBAR:
389       gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
390       break;
391
392     default:
393       g_assert_not_reached ();
394     }
395 }
396
397 static void
398 gtk_application_window_dispose (GObject *object)
399 {
400   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
401
402   g_clear_object (&window->priv->menubar);
403   g_clear_object (&window->priv->actions);
404
405   G_OBJECT_CLASS (gtk_application_window_parent_class)
406     ->dispose (object);
407 }
408
409 static void
410 gtk_application_window_init (GtkApplicationWindow *window)
411 {
412   window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
413
414   window->priv->actions = g_simple_action_group_new ();
415
416   /* window->priv->actions is the one and only ref on the group, so when
417    * we dispose, the action group will die, disconnecting all signals.
418    */
419   g_signal_connect_swapped (window->priv->actions, "action-added",
420                             G_CALLBACK (g_action_group_action_added), window);
421   g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
422                             G_CALLBACK (g_action_group_action_enabled_changed), window);
423   g_signal_connect_swapped (window->priv->actions, "action-state-changed",
424                             G_CALLBACK (g_action_group_action_state_changed), window);
425   g_signal_connect_swapped (window->priv->actions, "action-removed",
426                             G_CALLBACK (g_action_group_action_removed), window);
427 }
428
429 static void
430 gtk_application_window_class_init (GtkApplicationWindowClass *class)
431 {
432   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
433   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
434   GObjectClass *object_class = G_OBJECT_CLASS (class);
435
436   container_class->forall = gtk_application_window_real_forall_internal;
437   widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
438   widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
439   widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
440   widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
441   widget_class->size_allocate = gtk_application_window_real_size_allocate;
442   widget_class->realize = gtk_application_window_real_realize;
443   widget_class->map = gtk_application_window_real_map;
444   object_class->get_property = gtk_application_window_get_property;
445   object_class->set_property = gtk_application_window_set_property;
446   object_class->dispose = gtk_application_window_dispose;
447
448   gtk_application_window_properties[PROP_SHOW_MENUBAR] =
449     g_param_spec_boolean ("show-menubar",
450                           P_("Show a menubar"),
451                           P_("TRUE if the application's menus should be included "
452                              "in the menubar at the top of the window"),
453                           FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
454   g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
455   g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
456 }
457
458 /**
459  * gtk_application_window_new:
460  * @application: a #GtkApplication
461  *
462  * Creates a new #GtkApplicationWindow.
463  *
464  * Returns: a newly created #GtkApplicationWindow
465  *
466  * Since: 3.4
467  */
468 GtkWidget *
469 gtk_application_window_new (GtkApplication *application)
470 {
471   g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
472
473   return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
474                        "application", application,
475                        NULL);
476 }
477
478 gboolean
479 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
480 {
481   return window->priv->override_show_menubar;
482 }
483
484 static void
485 recreate_menubar (GtkApplicationWindow *window)
486 {
487   GtkWidget *new_menubar;
488
489   if (window->priv->menubar)
490     gtk_widget_unparent (GTK_WIDGET (window->priv->menubar));
491   g_clear_object (&window->priv->menubar);
492
493   new_menubar = gtk_application_window_create_menubar (window);
494
495   if (new_menubar)
496     {
497       window->priv->menubar = g_object_ref_sink (new_menubar);
498       gtk_widget_set_parent (new_menubar, GTK_WIDGET (window));
499       gtk_widget_show_all (new_menubar);
500     }
501 }
502
503 void
504 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
505                                          gboolean              show)
506 {
507   show = !!show;
508   window->priv->did_override_show_menubar = TRUE;
509   if (window->priv->override_show_menubar != show)
510     {
511       window->priv->override_show_menubar = show;
512       recreate_menubar (window);
513       g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
514     }
515 }
516
517 /* GtkMenu construction {{{1 */
518
519 typedef struct {
520   GActionGroup *group;
521   gchar        *name;
522   gchar        *target;
523   gulong        enabled_changed_id;
524   gulong        state_changed_id;
525   gulong        activate_handler;
526 } ActionData;
527
528 static void
529 action_data_free (gpointer data)
530 {
531   ActionData *a = data;
532
533   if (a->enabled_changed_id)
534     g_signal_handler_disconnect (a->group, a->enabled_changed_id);
535
536   if (a->state_changed_id)
537     g_signal_handler_disconnect (a->group, a->state_changed_id);
538
539   g_object_unref (a->group);
540   g_free (a->name);
541   g_free (a->target);
542
543   g_free (a);
544 }
545
546 static void
547 enabled_changed (GActionGroup *group,
548                  const gchar  *action_name,
549                  gboolean      enabled,
550                  GtkWidget    *widget)
551 {
552   gtk_widget_set_sensitive (widget, enabled);
553 }
554
555 static void
556 item_activated (GtkWidget *w,
557                 gpointer   data)
558 {
559   ActionData *a;
560   GVariant *parameter;
561
562   a = g_object_get_data (G_OBJECT (w), "action");
563   if (a->target)
564     parameter = g_variant_ref_sink (g_variant_new_string (a->target));
565   else
566     parameter = NULL;
567   g_action_group_activate_action (a->group, a->name, parameter);
568   if (parameter)
569     g_variant_unref (parameter);
570 }
571
572 static void
573 toggle_state_changed (GActionGroup     *group,
574                       const gchar      *name,
575                       GVariant         *state,
576                       GtkCheckMenuItem *w)
577 {
578   ActionData *a;
579
580   a = g_object_get_data (G_OBJECT (w), "action");
581   g_signal_handler_block (w, a->activate_handler);
582   gtk_check_menu_item_set_active (w, g_variant_get_boolean (state));
583   g_signal_handler_unblock (w, a->activate_handler);
584 }
585
586 static void
587 radio_state_changed (GActionGroup     *group,
588                      const gchar      *name,
589                      GVariant         *state,
590                      GtkCheckMenuItem *w)
591 {
592   ActionData *a;
593   gboolean b;
594
595   a = g_object_get_data (G_OBJECT (w), "action");
596   g_signal_handler_block (w, a->activate_handler);
597   b = g_strcmp0 (a->target, g_variant_get_string (state, NULL)) == 0;
598   gtk_check_menu_item_set_active (w, b);
599   g_signal_handler_unblock (w, a->activate_handler);
600 }
601
602 static GtkWidget *
603 create_menuitem_from_model (GMenuModel   *model,
604                             gint          item,
605                             GActionGroup *group)
606 {
607   GtkWidget *w;
608   gchar *label;
609   gchar *action;
610   gchar *target;
611   gchar *s;
612   ActionData *a;
613   const GVariantType *type;
614   GVariant *v;
615
616   label = NULL;
617   g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_LABEL, "s", &label);
618
619   action = NULL;
620   g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_ACTION, "s", &action);
621
622   if (action != NULL)
623     type = g_action_group_get_action_state_type (group, action);
624   else
625     type = NULL;
626
627   if (type == NULL)
628     w = gtk_menu_item_new_with_label (label);
629   else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
630     w = gtk_check_menu_item_new_with_label (label);
631   else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
632     {
633       w = gtk_check_menu_item_new_with_label (label);
634       gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (w), TRUE);
635     }
636   else
637     g_assert_not_reached ();
638
639   gtk_menu_item_set_use_underline (GTK_MENU_ITEM (w), TRUE);
640
641   if (action != NULL)
642     {
643       a = g_new0 (ActionData, 1);
644       a->group = g_object_ref (group);
645       a->name = g_strdup (action);
646       g_object_set_data_full (G_OBJECT (w), "action", a, action_data_free);
647
648       if (!g_action_group_get_action_enabled (group, action))
649         gtk_widget_set_sensitive (w, FALSE);
650
651       s = g_strconcat ("action-enabled-changed::", action, NULL);
652       a->enabled_changed_id = g_signal_connect (group, s,
653                                                 G_CALLBACK (enabled_changed), w);
654       g_free (s);
655       a->activate_handler = g_signal_connect (w, "activate",
656                                               G_CALLBACK (item_activated), NULL);
657
658       if (type == NULL)
659         {
660           /* all set */
661         }
662       else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
663         {
664           s = g_strconcat ("action-state-changed::", action, NULL);
665           a->state_changed_id = g_signal_connect (group, s,
666                                                   G_CALLBACK (toggle_state_changed), w);
667           g_free (s);
668           v = g_action_group_get_action_state (group, action);
669           gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
670                                           g_variant_get_boolean (v));
671           g_variant_unref (v);
672         }
673       else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
674         {
675           s = g_strconcat ("action-state-changed::", action, NULL);
676           a->state_changed_id = g_signal_connect (group, s,
677                                                   G_CALLBACK (radio_state_changed), w);
678           g_free (s);
679           g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_TARGET, "s", &target);
680           a->target = g_strdup (target);
681           v = g_action_group_get_action_state (group, action);
682           gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
683                                           g_strcmp0 (g_variant_get_string (v, NULL), target) == 0);
684           g_variant_unref (v);
685           g_free (target);
686         }
687       else
688         g_assert_not_reached ();
689     }
690
691   g_free (label);
692   g_free (action);
693
694   return w;
695 }
696
697 static void populate_menu_from_model (GtkMenuShell *menu,
698                                       GMenuModel   *model,
699                                       GActionGroup *group);
700
701 static void
702 append_items_from_model (GtkMenuShell *menu,
703                          GMenuModel   *model,
704                          GActionGroup *group,
705                          gboolean     *need_separator,
706                          const gchar  *heading)
707 {
708   gint n;
709   gint i;
710   GtkWidget *w;
711   GtkWidget *menuitem;
712   GtkWidget *submenu;
713   GMenuModel *m;
714   gchar *label;
715
716   n = g_menu_model_get_n_items (model);
717
718   if (*need_separator && n > 0)
719     {
720       w = gtk_separator_menu_item_new ();
721       gtk_widget_show (w);
722       gtk_menu_shell_append (menu, w);
723       *need_separator = FALSE;
724     }
725
726   if (heading != NULL)
727     {
728       w = gtk_menu_item_new_with_label (heading);
729       gtk_widget_show (w);
730       gtk_widget_set_sensitive (w, FALSE);
731       gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
732     }
733
734   for (i = 0; i < n; i++)
735     {
736       if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION)))
737         {
738           label = NULL;
739           g_menu_model_get_item_attribute (model, i, G_MENU_ATTRIBUTE_LABEL, "s", &label);
740           append_items_from_model (menu, m, group, need_separator, label);
741           g_object_unref (m);
742           g_free (label);
743           continue;
744         }
745
746       if (*need_separator)
747         {
748           w = gtk_separator_menu_item_new ();
749           gtk_widget_show (w);
750           gtk_menu_shell_append (menu, w);
751           *need_separator = FALSE;
752         }
753
754       menuitem = create_menuitem_from_model (model, i, group);
755
756       if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SUBMENU)))
757         {
758           submenu = gtk_menu_new ();
759           populate_menu_from_model (GTK_MENU_SHELL (submenu), m, group);
760           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
761           g_object_unref (m);
762         }
763
764       gtk_widget_show (menuitem);
765       gtk_menu_shell_append (menu, menuitem);
766
767       *need_separator = TRUE;
768     }
769 }
770
771 static void
772 populate_menu_from_model (GtkMenuShell *menu,
773                           GMenuModel   *model,
774                           GActionGroup *group)
775 {
776   gboolean need_separator;
777
778   need_separator = FALSE;
779   append_items_from_model (menu, model, group, &need_separator, NULL);
780 }
781
782 typedef struct {
783   GActionMuxer *muxer;
784   GtkApplication *application;
785   GtkApplicationWindow *window;
786   GtkMenuShell   *menu;
787   guint           update_idle;
788   GHashTable     *connected;
789 } ItemsChangedData;
790
791 static void
792 free_items_changed_data (gpointer data)
793 {
794   ItemsChangedData *d = data;
795
796   g_object_unref (d->muxer);
797   g_object_unref (d->window);
798   g_object_unref (d->application);
799
800   if (d->update_idle != 0)
801     g_source_remove (d->update_idle);
802
803   g_hash_table_unref (d->connected);
804
805   g_free (d);
806 }
807
808 static gboolean
809 repopulate_menu (gpointer data)
810 {
811   ItemsChangedData *d = data;
812   GList *children, *l;
813   GtkWidget *child;
814   GtkMenuShell *app_shell;
815   GMenuModel *app_model;
816   GMenuModel *window_model;
817
818   /* remove current children */
819   children = gtk_container_get_children (GTK_CONTAINER (d->menu));
820   for (l = children; l; l = l->next)
821     {
822       child = l->data;
823       gtk_container_remove (GTK_CONTAINER (d->menu), child);
824     }
825   g_list_free (children);
826
827   app_model = g_application_get_app_menu (G_APPLICATION (d->application));
828
829   if (app_model && !d->window->priv->shell_shows_app_menu)
830     {
831       child = gtk_menu_item_new_with_label (_("Application"));
832       app_shell = (GtkMenuShell*)gtk_menu_new ();
833       gtk_menu_item_set_submenu ((GtkMenuItem*)child, (GtkWidget*)app_shell);
834       /* repopulate */
835       populate_menu_from_model (app_shell, app_model, (GActionGroup*)d->muxer);
836       gtk_menu_shell_append ((GtkMenuShell*)d->menu, child);
837     }
838
839   window_model = g_application_get_menubar (G_APPLICATION (d->application));
840   if (window_model)
841     {
842       populate_menu_from_model (d->menu, window_model, (GActionGroup*)d->muxer);
843     }
844
845   d->update_idle = 0;
846
847   return FALSE;
848 }
849
850 static void
851 connect_to_items_changed (GMenuModel *model,
852                           GCallback   callback,
853                           gpointer    data)
854 {
855   ItemsChangedData *d = data;
856   gint i;
857   GMenuModel *m;
858   GMenuLinkIter *iter;
859
860   if (!g_hash_table_lookup (d->connected, model))
861     {
862       g_signal_connect (model, "items-changed", callback, data);
863       g_hash_table_insert (d->connected, model, model);
864     }
865
866   for (i = 0; i < g_menu_model_get_n_items (model); i++)
867     {
868       iter = g_menu_model_iterate_item_links (model, i);
869       while (g_menu_link_iter_next (iter))
870         {
871           m = g_menu_link_iter_get_value (iter);
872           connect_to_items_changed (m, callback, data);
873           g_object_unref (m);
874         }
875       g_object_unref (iter);
876     }
877 }
878
879 static void
880 items_changed (GMenuModel *model,
881                gint        position,
882                gint        removed,
883                gint        added,
884                gpointer    data)
885 {
886   ItemsChangedData *d = data;
887
888   if (d->update_idle == 0)
889     d->update_idle = gdk_threads_add_idle (repopulate_menu, data);
890   connect_to_items_changed (model, G_CALLBACK (items_changed), data);
891 }
892
893 static GtkWidget *
894 gtk_application_window_create_menubar (GtkApplicationWindow *window)
895 {
896   GtkApplication *application;
897   GMenuModel *app_model;
898   GMenuModel *win_model;
899   ItemsChangedData *data;
900   GtkWidget *menubar;
901
902   application = gtk_window_get_application (GTK_WINDOW (window));
903   app_model = g_application_get_app_menu (G_APPLICATION (application));
904   win_model = g_application_get_menubar (G_APPLICATION (application));
905
906   if (!(app_model || win_model)
907       || (window->priv->did_override_show_menubar
908           && window->priv->override_show_menubar == FALSE))
909     return NULL;
910
911   menubar = gtk_menu_bar_new ();
912
913   data = g_new (ItemsChangedData, 1);
914   data->muxer = g_action_muxer_new ();
915   g_action_muxer_insert (data->muxer, "app", G_ACTION_GROUP (application));
916   g_action_muxer_insert (data->muxer, "win", G_ACTION_GROUP (window));
917   data->window = g_object_ref (window);
918   data->application = g_object_ref (application);
919   data->menu = GTK_MENU_SHELL (menubar);
920   data->update_idle = 0;
921   data->connected = g_hash_table_new (NULL, NULL);
922
923   g_object_set_data_full (G_OBJECT (menubar), "gtk-application-menu-data",
924                           data, free_items_changed_data);
925
926   if (app_model)
927     connect_to_items_changed (app_model, G_CALLBACK (items_changed), data);
928   if (win_model)
929     connect_to_items_changed (win_model, G_CALLBACK (items_changed), data);
930
931   repopulate_menu (data);
932
933   return menubar;
934 }