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