]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplicationwindow.c
GtkApplicationWindow: Don't unref a NULL object
[~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_mnemonic (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   if (action != NULL)
638     {
639       a = g_new0 (ActionData, 1);
640       a->group = g_object_ref (group);
641       a->name = g_strdup (action);
642       g_object_set_data_full (G_OBJECT (w), "action", a, action_data_free);
643
644       if (!g_action_group_get_action_enabled (group, action))
645         gtk_widget_set_sensitive (w, FALSE);
646
647       s = g_strconcat ("action-enabled-changed::", action, NULL);
648       a->enabled_changed_id = g_signal_connect (group, s,
649                                                 G_CALLBACK (enabled_changed), w);
650       g_free (s);
651       a->activate_handler = g_signal_connect (w, "activate",
652                                               G_CALLBACK (item_activated), NULL);
653
654       if (type == NULL)
655         {
656           /* all set */
657         }
658       else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
659         {
660           s = g_strconcat ("action-state-changed::", action, NULL);
661           a->state_changed_id = g_signal_connect (group, s,
662                                                   G_CALLBACK (toggle_state_changed), w);
663           g_free (s);
664           v = g_action_group_get_action_state (group, action);
665           gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
666                                           g_variant_get_boolean (v));
667           g_variant_unref (v);
668         }
669       else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
670         {
671           s = g_strconcat ("action-state-changed::", action, NULL);
672           a->state_changed_id = g_signal_connect (group, s,
673                                                   G_CALLBACK (radio_state_changed), w);
674           g_free (s);
675           g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_TARGET, "s", &target);
676           a->target = g_strdup (target);
677           v = g_action_group_get_action_state (group, action);
678           gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
679                                           g_strcmp0 (g_variant_get_string (v, NULL), target) == 0);
680           g_variant_unref (v);
681           g_free (target);
682         }
683       else
684         g_assert_not_reached ();
685     }
686
687   g_free (label);
688   g_free (action);
689
690   return w;
691 }
692
693 static void populate_menu_from_model (GtkMenuShell *menu,
694                                       GMenuModel   *model,
695                                       GActionGroup *group);
696
697 static void
698 append_items_from_model (GtkMenuShell *menu,
699                          GMenuModel   *model,
700                          GActionGroup *group,
701                          gboolean     *need_separator,
702                          const gchar  *heading)
703 {
704   gint n;
705   gint i;
706   GtkWidget *w;
707   GtkWidget *menuitem;
708   GtkWidget *submenu;
709   GMenuModel *m;
710   gchar *label;
711
712   n = g_menu_model_get_n_items (model);
713
714   if (*need_separator && n > 0)
715     {
716       w = gtk_separator_menu_item_new ();
717       gtk_widget_show (w);
718       gtk_menu_shell_append (menu, w);
719       *need_separator = FALSE;
720     }
721
722   if (heading != NULL)
723     {
724       w = gtk_menu_item_new_with_label (heading);
725       gtk_widget_show (w);
726       gtk_widget_set_sensitive (w, FALSE);
727       gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
728     }
729
730   for (i = 0; i < n; i++)
731     {
732       if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION)))
733         {
734           label = NULL;
735           g_menu_model_get_item_attribute (model, i, G_MENU_ATTRIBUTE_LABEL, "s", &label);
736           append_items_from_model (menu, m, group, need_separator, label);
737           g_object_unref (m);
738           g_free (label);
739           continue;
740         }
741
742       if (*need_separator)
743         {
744           w = gtk_separator_menu_item_new ();
745           gtk_widget_show (w);
746           gtk_menu_shell_append (menu, w);
747           *need_separator = FALSE;
748         }
749
750       menuitem = create_menuitem_from_model (model, i, group);
751
752       if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SUBMENU)))
753         {
754           submenu = gtk_menu_new ();
755           populate_menu_from_model (GTK_MENU_SHELL (submenu), m, group);
756           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
757           g_object_unref (m);
758         }
759
760       gtk_widget_show (menuitem);
761       gtk_menu_shell_append (menu, menuitem);
762
763       *need_separator = TRUE;
764     }
765 }
766
767 static void
768 populate_menu_from_model (GtkMenuShell *menu,
769                           GMenuModel   *model,
770                           GActionGroup *group)
771 {
772   gboolean need_separator;
773
774   need_separator = FALSE;
775   append_items_from_model (menu, model, group, &need_separator, NULL);
776 }
777
778 typedef struct {
779   GtkApplication *application;
780   GtkMenuShell   *menu;
781   guint           update_idle;
782   GHashTable     *connected;
783 } ItemsChangedData;
784
785 static void
786 free_items_changed_data (gpointer data)
787 {
788   ItemsChangedData *d = data;
789
790   g_object_unref (d->application);
791
792   if (d->update_idle != 0)
793     g_source_remove (d->update_idle);
794
795   g_hash_table_unref (d->connected);
796
797   g_free (d);
798 }
799
800 static gboolean
801 repopulate_menu (gpointer data)
802 {
803   ItemsChangedData *d = data;
804   GList *children, *l;
805   GtkWidget *child;
806   GMenuModel *model;
807
808   /* remove current children */
809   children = gtk_container_get_children (GTK_CONTAINER (d->menu));
810   for (l = children; l; l = l->next)
811     {
812       child = l->data;
813       gtk_container_remove (GTK_CONTAINER (d->menu), child);
814     }
815   g_list_free (children);
816
817   /* repopulate */
818   model = g_application_get_app_menu (G_APPLICATION (d->application));
819   populate_menu_from_model (d->menu, model, G_ACTION_GROUP (d->application));
820
821   d->update_idle = 0;
822
823   return FALSE;
824 }
825
826 static void
827 connect_to_items_changed (GMenuModel *model,
828                           GCallback   callback,
829                           gpointer    data)
830 {
831   ItemsChangedData *d = data;
832   gint i;
833   GMenuModel *m;
834   GMenuLinkIter *iter;
835
836   if (!g_hash_table_lookup (d->connected, model))
837     {
838       g_signal_connect (model, "items-changed", callback, data);
839       g_hash_table_insert (d->connected, model, model);
840     }
841
842   for (i = 0; i < g_menu_model_get_n_items (model); i++)
843     {
844       iter = g_menu_model_iterate_item_links (model, i);
845       while (g_menu_link_iter_next (iter))
846         {
847           m = g_menu_link_iter_get_value (iter);
848           connect_to_items_changed (m, callback, data);
849           g_object_unref (m);
850         }
851       g_object_unref (iter);
852     }
853 }
854
855 static void
856 items_changed (GMenuModel *model,
857                gint        position,
858                gint        removed,
859                gint        added,
860                gpointer    data)
861 {
862   ItemsChangedData *d = data;
863
864   if (d->update_idle == 0)
865     d->update_idle = gdk_threads_add_idle (repopulate_menu, data);
866   connect_to_items_changed (model, G_CALLBACK (items_changed), data);
867 }
868
869 /**
870  * gtk_application_window_get_app_menu:
871  * @window: a #GtkApplicationWindow
872  *
873  * Populates a menu widget from a menu model that is
874  * associated with @window. See g_application_set_menu().
875  * The menu items will be connected to actions of @window or
876  * its associated #GtkApplication, as indicated by the menu model.
877  * The menus contents will be updated automatically in response
878  * to menu model changes.
879  *
880  * It is the callers responsibility to add the menu at a
881  * suitable place in the widget hierarchy.
882  *
883  * This function returns %NULL if @window has no associated
884  * menu model.
885  *
886  * Returns: A #GtkMenu that has been populated from the
887  *     #GMenuModel that is associated with @window, or %NULL
888  */
889 GtkWidget *
890 gtk_application_window_get_app_menu (GtkApplicationWindow *window)
891 {
892   GtkApplication *application;
893   GtkWidget *menu;
894   GMenuModel *model;
895   ItemsChangedData *data;
896   GActionMuxer *muxer;
897
898   application = gtk_window_get_application (GTK_WINDOW (window));
899
900   model = g_application_get_app_menu (G_APPLICATION (application));
901
902   if (!model)
903     return NULL;
904
905   menu = gtk_menu_new ();
906
907   muxer = g_action_muxer_new ();
908   g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (application));
909   g_action_muxer_insert (muxer, "win", G_ACTION_GROUP (window));
910   populate_menu_from_model (GTK_MENU_SHELL (menu), model, G_ACTION_GROUP (muxer));
911   g_object_unref (muxer);
912
913   data = g_new (ItemsChangedData, 1);
914   data->application = g_object_ref (application);
915   data->menu = GTK_MENU_SHELL (menu);
916   data->update_idle = 0;
917   data->connected = g_hash_table_new (NULL, NULL);
918
919   g_object_set_data_full (G_OBJECT (menu), "gtk-application-menu-data",
920                           data, free_items_changed_data);
921
922   connect_to_items_changed (model, G_CALLBACK (items_changed), data);
923
924   return menu;
925 }