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