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