]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplicationwindow.c
807caa3e5c813fa04bda9395a6e64c63c2476559
[~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       if (window->priv->menubar)
496         gtk_widget_unparent (GTK_WIDGET (window->priv->menubar));
497       g_clear_object (&window->priv->menubar);
498     }
499 }
500
501 void
502 gtk_application_window_set_show_app_menu (GtkApplicationWindow *window,
503                                           gboolean              show_app_menu)
504 {
505   show_app_menu = !!show_app_menu;
506   window->priv->did_override_show_app_menu = TRUE;
507   if (window->priv->override_show_app_menu != show_app_menu)
508     {
509       window->priv->override_show_app_menu = show_app_menu;
510       recalculate_app_menu_state (window);
511       g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_APP_MENU]);
512     }
513 }
514
515 /* GtkMenu construction {{{1 */
516
517 typedef struct {
518   GActionGroup *group;
519   gchar        *name;
520   gchar        *target;
521   gulong        enabled_changed_id;
522   gulong        state_changed_id;
523   gulong        activate_handler;
524 } ActionData;
525
526 static void
527 action_data_free (gpointer data)
528 {
529   ActionData *a = data;
530
531   if (a->enabled_changed_id)
532     g_signal_handler_disconnect (a->group, a->enabled_changed_id);
533
534   if (a->state_changed_id)
535     g_signal_handler_disconnect (a->group, a->state_changed_id);
536
537   g_object_unref (a->group);
538   g_free (a->name);
539   g_free (a->target);
540
541   g_free (a);
542 }
543
544 static void
545 enabled_changed (GActionGroup *group,
546                  const gchar  *action_name,
547                  gboolean      enabled,
548                  GtkWidget    *widget)
549 {
550   gtk_widget_set_sensitive (widget, enabled);
551 }
552
553 static void
554 item_activated (GtkWidget *w,
555                 gpointer   data)
556 {
557   ActionData *a;
558   GVariant *parameter;
559
560   a = g_object_get_data (G_OBJECT (w), "action");
561   if (a->target)
562     parameter = g_variant_ref_sink (g_variant_new_string (a->target));
563   else
564     parameter = NULL;
565   g_action_group_activate_action (a->group, a->name, parameter);
566   if (parameter)
567     g_variant_unref (parameter);
568 }
569
570 static void
571 toggle_state_changed (GActionGroup     *group,
572                       const gchar      *name,
573                       GVariant         *state,
574                       GtkCheckMenuItem *w)
575 {
576   ActionData *a;
577
578   a = g_object_get_data (G_OBJECT (w), "action");
579   g_signal_handler_block (w, a->activate_handler);
580   gtk_check_menu_item_set_active (w, g_variant_get_boolean (state));
581   g_signal_handler_unblock (w, a->activate_handler);
582 }
583
584 static void
585 radio_state_changed (GActionGroup     *group,
586                      const gchar      *name,
587                      GVariant         *state,
588                      GtkCheckMenuItem *w)
589 {
590   ActionData *a;
591   gboolean b;
592
593   a = g_object_get_data (G_OBJECT (w), "action");
594   g_signal_handler_block (w, a->activate_handler);
595   b = g_strcmp0 (a->target, g_variant_get_string (state, NULL)) == 0;
596   gtk_check_menu_item_set_active (w, b);
597   g_signal_handler_unblock (w, a->activate_handler);
598 }
599
600 static GtkWidget *
601 create_menuitem_from_model (GMenuModel   *model,
602                             gint          item,
603                             GActionGroup *group)
604 {
605   GtkWidget *w;
606   gchar *label;
607   gchar *action;
608   gchar *target;
609   gchar *s;
610   ActionData *a;
611   const GVariantType *type;
612   GVariant *v;
613
614   label = NULL;
615   g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_LABEL, "s", &label);
616
617   action = NULL;
618   g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_ACTION, "s", &action);
619
620   if (action != NULL)
621     type = g_action_group_get_action_state_type (group, action);
622   else
623     type = NULL;
624
625   if (type == NULL)
626     w = gtk_menu_item_new_with_label (label);
627   else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
628     w = gtk_check_menu_item_new_with_label (label);
629   else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
630     {
631       w = gtk_check_menu_item_new_with_label (label);
632       gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (w), TRUE);
633     }
634   else
635     g_assert_not_reached ();
636
637   gtk_menu_item_set_use_underline (GTK_MENU_ITEM (w), TRUE);
638
639   if (action != NULL)
640     {
641       a = g_new0 (ActionData, 1);
642       a->group = g_object_ref (group);
643       a->name = g_strdup (action);
644       g_object_set_data_full (G_OBJECT (w), "action", a, action_data_free);
645
646       if (!g_action_group_get_action_enabled (group, action))
647         gtk_widget_set_sensitive (w, FALSE);
648
649       s = g_strconcat ("action-enabled-changed::", action, NULL);
650       a->enabled_changed_id = g_signal_connect (group, s,
651                                                 G_CALLBACK (enabled_changed), w);
652       g_free (s);
653       a->activate_handler = g_signal_connect (w, "activate",
654                                               G_CALLBACK (item_activated), NULL);
655
656       if (type == NULL)
657         {
658           /* all set */
659         }
660       else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
661         {
662           s = g_strconcat ("action-state-changed::", action, NULL);
663           a->state_changed_id = g_signal_connect (group, s,
664                                                   G_CALLBACK (toggle_state_changed), w);
665           g_free (s);
666           v = g_action_group_get_action_state (group, action);
667           gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
668                                           g_variant_get_boolean (v));
669           g_variant_unref (v);
670         }
671       else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
672         {
673           s = g_strconcat ("action-state-changed::", action, NULL);
674           a->state_changed_id = g_signal_connect (group, s,
675                                                   G_CALLBACK (radio_state_changed), w);
676           g_free (s);
677           g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_TARGET, "s", &target);
678           a->target = g_strdup (target);
679           v = g_action_group_get_action_state (group, action);
680           gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
681                                           g_strcmp0 (g_variant_get_string (v, NULL), target) == 0);
682           g_variant_unref (v);
683           g_free (target);
684         }
685       else
686         g_assert_not_reached ();
687     }
688
689   g_free (label);
690   g_free (action);
691
692   return w;
693 }
694
695 static void populate_menu_from_model (GtkMenuShell *menu,
696                                       GMenuModel   *model,
697                                       GActionGroup *group);
698
699 static void
700 append_items_from_model (GtkMenuShell *menu,
701                          GMenuModel   *model,
702                          GActionGroup *group,
703                          gboolean     *need_separator,
704                          const gchar  *heading)
705 {
706   gint n;
707   gint i;
708   GtkWidget *w;
709   GtkWidget *menuitem;
710   GtkWidget *submenu;
711   GMenuModel *m;
712   gchar *label;
713
714   n = g_menu_model_get_n_items (model);
715
716   if (*need_separator && n > 0)
717     {
718       w = gtk_separator_menu_item_new ();
719       gtk_widget_show (w);
720       gtk_menu_shell_append (menu, w);
721       *need_separator = FALSE;
722     }
723
724   if (heading != NULL)
725     {
726       w = gtk_menu_item_new_with_label (heading);
727       gtk_widget_show (w);
728       gtk_widget_set_sensitive (w, FALSE);
729       gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
730     }
731
732   for (i = 0; i < n; i++)
733     {
734       if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION)))
735         {
736           label = NULL;
737           g_menu_model_get_item_attribute (model, i, G_MENU_ATTRIBUTE_LABEL, "s", &label);
738           append_items_from_model (menu, m, group, need_separator, label);
739           g_object_unref (m);
740           g_free (label);
741           continue;
742         }
743
744       if (*need_separator)
745         {
746           w = gtk_separator_menu_item_new ();
747           gtk_widget_show (w);
748           gtk_menu_shell_append (menu, w);
749           *need_separator = FALSE;
750         }
751
752       menuitem = create_menuitem_from_model (model, i, group);
753
754       if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SUBMENU)))
755         {
756           submenu = gtk_menu_new ();
757           populate_menu_from_model (GTK_MENU_SHELL (submenu), m, group);
758           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
759           g_object_unref (m);
760         }
761
762       gtk_widget_show (menuitem);
763       gtk_menu_shell_append (menu, menuitem);
764
765       *need_separator = TRUE;
766     }
767 }
768
769 static void
770 populate_menu_from_model (GtkMenuShell *menu,
771                           GMenuModel   *model,
772                           GActionGroup *group)
773 {
774   gboolean need_separator;
775
776   need_separator = FALSE;
777   append_items_from_model (menu, model, group, &need_separator, NULL);
778 }
779
780 typedef struct {
781   GtkApplication *application;
782   GtkMenuShell   *menu;
783   guint           update_idle;
784   GHashTable     *connected;
785 } ItemsChangedData;
786
787 static void
788 free_items_changed_data (gpointer data)
789 {
790   ItemsChangedData *d = data;
791
792   g_object_unref (d->application);
793
794   if (d->update_idle != 0)
795     g_source_remove (d->update_idle);
796
797   g_hash_table_unref (d->connected);
798
799   g_free (d);
800 }
801
802 static gboolean
803 repopulate_menu (gpointer data)
804 {
805   ItemsChangedData *d = data;
806   GList *children, *l;
807   GtkWidget *child;
808   GMenuModel *model;
809
810   /* remove current children */
811   children = gtk_container_get_children (GTK_CONTAINER (d->menu));
812   for (l = children; l; l = l->next)
813     {
814       child = l->data;
815       gtk_container_remove (GTK_CONTAINER (d->menu), child);
816     }
817   g_list_free (children);
818
819   /* repopulate */
820   model = g_application_get_app_menu (G_APPLICATION (d->application));
821   populate_menu_from_model (d->menu, model, G_ACTION_GROUP (d->application));
822
823   d->update_idle = 0;
824
825   return FALSE;
826 }
827
828 static void
829 connect_to_items_changed (GMenuModel *model,
830                           GCallback   callback,
831                           gpointer    data)
832 {
833   ItemsChangedData *d = data;
834   gint i;
835   GMenuModel *m;
836   GMenuLinkIter *iter;
837
838   if (!g_hash_table_lookup (d->connected, model))
839     {
840       g_signal_connect (model, "items-changed", callback, data);
841       g_hash_table_insert (d->connected, model, model);
842     }
843
844   for (i = 0; i < g_menu_model_get_n_items (model); i++)
845     {
846       iter = g_menu_model_iterate_item_links (model, i);
847       while (g_menu_link_iter_next (iter))
848         {
849           m = g_menu_link_iter_get_value (iter);
850           connect_to_items_changed (m, callback, data);
851           g_object_unref (m);
852         }
853       g_object_unref (iter);
854     }
855 }
856
857 static void
858 items_changed (GMenuModel *model,
859                gint        position,
860                gint        removed,
861                gint        added,
862                gpointer    data)
863 {
864   ItemsChangedData *d = data;
865
866   if (d->update_idle == 0)
867     d->update_idle = gdk_threads_add_idle (repopulate_menu, data);
868   connect_to_items_changed (model, G_CALLBACK (items_changed), data);
869 }
870
871 /**
872  * gtk_application_window_get_app_menu:
873  * @window: a #GtkApplicationWindow
874  *
875  * Populates a menu widget from a menu model that is
876  * associated with @window. See g_application_set_menu().
877  * The menu items will be connected to actions of @window or
878  * its associated #GtkApplication, as indicated by the menu model.
879  * The menus contents will be updated automatically in response
880  * to menu model changes.
881  *
882  * It is the callers responsibility to add the menu at a
883  * suitable place in the widget hierarchy.
884  *
885  * This function returns %NULL if @window has no associated
886  * menu model.
887  *
888  * Returns: A #GtkMenu that has been populated from the
889  *     #GMenuModel that is associated with @window, or %NULL
890  */
891 GtkWidget *
892 gtk_application_window_get_app_menu (GtkApplicationWindow *window)
893 {
894   GtkApplication *application;
895   GtkWidget *menu;
896   GMenuModel *model;
897   ItemsChangedData *data;
898   GActionMuxer *muxer;
899
900   application = gtk_window_get_application (GTK_WINDOW (window));
901
902   model = g_application_get_app_menu (G_APPLICATION (application));
903
904   if (!model)
905     return NULL;
906
907   menu = gtk_menu_new ();
908
909   muxer = g_action_muxer_new ();
910   g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (application));
911   g_action_muxer_insert (muxer, "win", G_ACTION_GROUP (window));
912   populate_menu_from_model (GTK_MENU_SHELL (menu), model, G_ACTION_GROUP (muxer));
913   g_object_unref (muxer);
914
915   data = g_new (ItemsChangedData, 1);
916   data->application = g_object_ref (application);
917   data->menu = GTK_MENU_SHELL (menu);
918   data->update_idle = 0;
919   data->connected = g_hash_table_new (NULL, NULL);
920
921   g_object_set_data_full (G_OBJECT (menu), "gtk-application-menu-data",
922                           data, free_items_changed_data);
923
924   connect_to_items_changed (model, G_CALLBACK (items_changed), data);
925
926   return menu;
927 }