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