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