]> Pileus Git - ~andy/gtk/blob - gtk/gtkapplicationwindow.c
Never add separators to menubars
[~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   if (have_menubar && !should_have_menubar)
86     {
87       gtk_widget_unparent (window->priv->menubar);
88       window->priv->menubar = NULL;
89
90       gtk_widget_queue_resize (GTK_WIDGET (window));
91     }
92
93   if (!have_menubar && should_have_menubar)
94     {
95       GActionMuxer *muxer;
96       GMenu *combined;
97
98       muxer = g_action_muxer_new ();
99       g_action_muxer_insert (muxer, "app", G_ACTION_GROUP (gtk_window_get_application (GTK_WINDOW (window))));
100       g_action_muxer_insert (muxer, "win", G_ACTION_GROUP (window));
101
102       combined = g_menu_new ();
103       g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->app_menu_section));
104       g_menu_append_section (combined, NULL, G_MENU_MODEL (window->priv->menubar_section));
105
106       window->priv->menubar = gtk_application_window_create_menubar (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (muxer));
107       gtk_widget_set_parent (window->priv->menubar, GTK_WIDGET (window));
108       gtk_widget_show_all (window->priv->menubar);
109       g_object_unref (combined);
110       g_object_unref (muxer);
111
112       gtk_widget_queue_resize (GTK_WIDGET (window));
113     }
114 }
115
116 static void
117 gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window)
118 {
119   gboolean shown_by_shell;
120
121   g_object_get (gtk_widget_get_settings (GTK_WIDGET (window)), "gtk-shell-shows-app-menu", &shown_by_shell, NULL);
122
123   if (shown_by_shell)
124     {
125       /* the shell shows it, so don't show it locally */
126       if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0)
127         g_menu_remove (window->priv->app_menu_section, 0);
128     }
129   else
130     {
131       /* the shell does not show it, so make sure we show it */
132       if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0)
133         {
134           GMenuModel *app_menu;
135
136           app_menu = g_application_get_app_menu (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
137
138           if (app_menu != NULL)
139             g_menu_append_submenu (window->priv->app_menu_section, _("Application"), app_menu);
140         }
141     }
142 }
143
144 static void
145 gtk_application_window_update_shell_shows_menubar (GtkApplicationWindow *window)
146 {
147   gboolean shown_by_shell;
148
149   g_object_get (gtk_widget_get_settings (GTK_WIDGET (window)), "gtk-shell-shows-menubar", &shown_by_shell, NULL);
150
151   if (shown_by_shell)
152     {
153       /* the shell shows it, so don't show it locally */
154       if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) != 0)
155         g_menu_remove (window->priv->menubar_section, 0);
156     }
157   else
158     {
159       /* the shell does not show it, so make sure we show it */
160       if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->menubar_section)) == 0)
161         {
162           GMenuModel *menubar;
163
164           menubar = g_application_get_menubar (G_APPLICATION (gtk_window_get_application (GTK_WINDOW (window))));
165
166           if (menubar != NULL)
167             g_menu_append_section (window->priv->menubar_section, NULL, menubar);
168         }
169     }
170 }
171
172 static void
173 gtk_application_window_shell_shows_app_menu_changed (GObject    *object,
174                                                      GParamSpec *pspec,
175                                                      gpointer    user_data)
176 {
177   GtkApplicationWindow *window = user_data;
178
179   gtk_application_window_update_shell_shows_app_menu (window);
180   gtk_application_window_update_menubar (window);
181 }
182
183 static void
184 gtk_application_window_shell_shows_menubar_changed (GObject    *object,
185                                                     GParamSpec *pspec,
186                                                     gpointer    user_data)
187 {
188   GtkApplicationWindow *window = user_data;
189
190   gtk_application_window_update_shell_shows_menubar (window);
191   gtk_application_window_update_menubar (window);
192 }
193
194 static gchar **
195 gtk_application_window_list_actions (GActionGroup *group)
196 {
197   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
198
199   return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
200 }
201
202 static gboolean
203 gtk_application_window_query_action (GActionGroup        *group,
204                                      const gchar         *action_name,
205                                      gboolean            *enabled,
206                                      const GVariantType **parameter_type,
207                                      const GVariantType **state_type,
208                                      GVariant           **state_hint,
209                                      GVariant           **state)
210 {
211   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
212
213   return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
214                                       action_name, enabled, parameter_type, state_type, state_hint, state);
215 }
216
217 static void
218 gtk_application_window_activate_action (GActionGroup *group,
219                                         const gchar  *action_name,
220                                         GVariant     *parameter)
221 {
222   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
223
224   return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
225 }
226
227 static void
228 gtk_application_window_change_action_state (GActionGroup *group,
229                                             const gchar  *action_name,
230                                             GVariant     *state)
231 {
232   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
233
234   return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
235 }
236
237 static GAction *
238 gtk_application_window_lookup_action (GActionMap  *action_map,
239                                       const gchar *action_name)
240 {
241   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
242
243   return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
244 }
245
246 static void
247 gtk_application_window_add_action (GActionMap *action_map,
248                                    GAction    *action)
249 {
250   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
251
252   g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
253 }
254
255 static void
256 gtk_application_window_remove_action (GActionMap  *action_map,
257                                       const gchar *action_name)
258 {
259   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
260
261   g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
262 }
263
264 static void
265 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
266 {
267   iface->list_actions = gtk_application_window_list_actions;
268   iface->query_action = gtk_application_window_query_action;
269   iface->activate_action = gtk_application_window_activate_action;
270   iface->change_action_state = gtk_application_window_change_action_state;
271 }
272
273 static void
274 gtk_application_window_map_iface_init (GActionMapInterface *iface)
275 {
276   iface->lookup_action = gtk_application_window_lookup_action;
277   iface->add_action = gtk_application_window_add_action;
278   iface->remove_action = gtk_application_window_remove_action;
279 }
280
281 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
282                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
283                          G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
284
285 enum {
286   PROP_0,
287   PROP_SHOW_MENUBAR,
288   N_PROPS
289 };
290 static GParamSpec *gtk_application_window_properties[N_PROPS];
291
292 static void
293 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
294                                                   gint      *minimum_height,
295                                                   gint      *natural_height)
296 {
297   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
298
299   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
300     ->get_preferred_height (widget, minimum_height, natural_height);
301
302   if (window->priv->menubar != NULL)
303     {
304       gint menubar_min_height, menubar_nat_height;
305
306       gtk_widget_get_preferred_height (window->priv->menubar, &menubar_min_height, &menubar_nat_height);
307       *minimum_height += menubar_min_height;
308       *natural_height += menubar_nat_height;
309     }
310 }
311
312 static void
313 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
314                                                             gint       width,
315                                                             gint      *minimum_height,
316                                                             gint      *natural_height)
317 {
318   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
319
320   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
321     ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
322
323   if (window->priv->menubar != NULL)
324     {
325       gint menubar_min_height, menubar_nat_height;
326
327       gtk_widget_get_preferred_height_for_width (window->priv->menubar, width, &menubar_min_height, &menubar_nat_height);
328       *minimum_height += menubar_min_height;
329       *natural_height += menubar_nat_height;
330     }
331 }
332
333 static void
334 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
335                                                  gint      *minimum_width,
336                                                  gint      *natural_width)
337 {
338   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
339
340   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
341     ->get_preferred_width (widget, minimum_width, natural_width);
342
343   if (window->priv->menubar != NULL)
344     {
345       gint menubar_min_width, menubar_nat_width;
346
347       gtk_widget_get_preferred_width (window->priv->menubar, &menubar_min_width, &menubar_nat_width);
348       *minimum_width = MAX (*minimum_width, menubar_min_width);
349       *natural_width = MAX (*natural_width, menubar_nat_width);
350     }
351 }
352
353 static void
354 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
355                                                             gint       height,
356                                                             gint      *minimum_width,
357                                                             gint      *natural_width)
358 {
359   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
360
361   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
362     ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
363
364   if (window->priv->menubar != NULL)
365     {
366       gint menubar_min_width, menubar_nat_width;
367
368       gtk_widget_get_preferred_width_for_height (window->priv->menubar, height, &menubar_min_width, &menubar_nat_width);
369       *minimum_width = MAX (*minimum_width, menubar_min_width);
370       *natural_width = MAX (*natural_width, menubar_nat_width);
371     }
372 }
373
374 static void
375 gtk_application_window_real_size_allocate (GtkWidget     *widget,
376                                            GtkAllocation *allocation)
377 {
378   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
379
380   if (window->priv->menubar != NULL)
381     {
382       GtkAllocation menubar_allocation = *allocation;
383       gint menubar_min_height, menubar_nat_height;
384       GtkWidget *child;
385
386       gtk_widget_get_preferred_height_for_width (window->priv->menubar, allocation->width, &menubar_min_height, &menubar_nat_height);
387
388       menubar_allocation.height = menubar_min_height;
389       gtk_widget_size_allocate (window->priv->menubar, &menubar_allocation);
390
391       child = gtk_bin_get_child (GTK_BIN (window));
392       if (child != NULL && gtk_widget_get_visible (child))
393         {
394           GtkAllocation child_allocation = *allocation;
395           gint border_width;
396
397           child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
398
399           border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
400           child_allocation.x += border_width;
401           child_allocation.y += border_width + menubar_min_height;
402           child_allocation.width -= border_width * 2;
403           child_allocation.height -= border_width * 2 - menubar_min_height;
404           gtk_widget_size_allocate (child, &child_allocation);
405         }
406
407       gtk_widget_set_allocation (widget, allocation);
408     }
409   else
410     GTK_WIDGET_CLASS (gtk_application_window_parent_class)
411       ->size_allocate (widget, allocation);
412 }
413
414 static void
415 gtk_application_window_real_realize (GtkWidget *widget)
416 {
417   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
418
419   gtk_application_window_update_shell_shows_app_menu (window);
420   gtk_application_window_update_shell_shows_menubar (window);
421   gtk_application_window_update_menubar (window);
422
423   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
424     ->realize (widget);
425 }
426
427 static void
428 gtk_application_window_real_map (GtkWidget *widget)
429 {
430   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
431
432   /* XXX could eliminate this by tweaking gtk_window_map */
433   if (window->priv->menubar)
434     gtk_widget_map (window->priv->menubar);
435
436   GTK_WIDGET_CLASS (gtk_application_window_parent_class)
437     ->map (widget);
438 }
439
440 static void
441 gtk_application_window_real_forall_internal (GtkContainer *container,
442                                              gboolean      include_internal,
443                                              GtkCallback   callback,
444                                              gpointer      user_data)
445 {
446   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
447
448   if (window->priv->menubar)
449     callback (window->priv->menubar, user_data);
450
451   GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
452     ->forall (container, include_internal, callback, user_data);
453 }
454
455
456 static void
457 gtk_application_window_get_property (GObject    *object,
458                                      guint       prop_id,
459                                      GValue     *value,
460                                      GParamSpec *pspec)
461 {
462   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
463
464   switch (prop_id)
465     {
466     case PROP_SHOW_MENUBAR:
467       g_value_set_boolean (value, window->priv->show_menubar);
468       break;
469
470     default:
471       g_assert_not_reached ();
472     }
473 }
474
475 static void
476 gtk_application_window_set_property (GObject      *object,
477                                      guint         prop_id,
478                                      const GValue *value,
479                                      GParamSpec   *pspec)
480 {
481   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
482
483   switch (prop_id)
484     {
485     case PROP_SHOW_MENUBAR:
486       gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
487       break;
488
489     default:
490       g_assert_not_reached ();
491     }
492 }
493
494 static void
495 gtk_application_window_dispose (GObject *object)
496 {
497   GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
498
499   if (window->priv->menubar)
500     {
501       gtk_widget_unparent (window->priv->menubar);
502       window->priv->menubar = NULL;
503     }
504
505   g_clear_object (&window->priv->app_menu_section);
506   g_clear_object (&window->priv->menubar_section);
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 (!GTK_IS_MENU_BAR (menu) && *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 (!GTK_IS_MENU_BAR (menu)  && *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 }