2 * Copyright © 2011 Canonical Limited
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.
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.
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.
19 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "gtkapplicationwindow.h"
26 #include "gtkseparatormenuitem.h"
27 #include "gtkcheckmenuitem.h"
28 #include "gtkmenubar.h"
29 #include "gactionmuxer.h"
33 * SECTION:gtkapplicationwindow
34 * @title: GtkApplicationWindow
35 * @short_description: GtkWindow subclass with GtkApplication support
37 * GtkApplicationWindow is a #GtkWindow subclass that offers some
38 * extra functionality for better integration with #GtkApplication
39 * features. Notably, it can handle both the application menu as well
40 * as the menubar. See g_application_set_app_menu() and
41 * g_application_set_menubar().
43 * This class implements the #GActionGroup and #GActionMap interfaces,
44 * to let you add window-specific actions that will be exported by the
45 * associated #GtkApplication, together with its application-wide
46 * actions. Window-specific actions are prefixed with the "win."
47 * prefix and application-wide actions are prefixed with the "app."
48 * prefix. Actions must be addressed with the prefixed name when
49 * referring to them from a #GMenuModel.
51 * If the desktop environment does not display the application menu
52 * as part of the desktop shell, then #GApplicationWindow will
53 * automatically show the menu as part of a menubar. This behaviour
54 * can be overridden with the #GtkApplicationWindow:show-app-menu
57 struct _GtkApplicationWindowPrivate
59 GSimpleActionGroup *actions;
62 guint initialized_gsetting_monitoring : 1;
63 guint shell_shows_app_menu : 1;
64 guint default_show_menubar : 1;
65 guint did_override_show_menubar : 1;
66 guint override_show_menubar : 1;
70 recreate_menubar (GtkApplicationWindow *window);
72 gtk_application_window_create_menubar (GtkApplicationWindow *window);
75 on_shell_shows_app_menu_changed (GtkSettings *settings,
79 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (user_data);
82 g_object_get (settings, "gtk-shell-shows-app-menu", &val, NULL);
84 if (window->priv->shell_shows_app_menu != val)
86 window->priv->shell_shows_app_menu = val;
87 recreate_menubar (window);
92 gtk_application_window_list_actions (GActionGroup *group)
94 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
96 return g_action_group_list_actions (G_ACTION_GROUP (window->priv->actions));
100 gtk_application_window_query_action (GActionGroup *group,
101 const gchar *action_name,
103 const GVariantType **parameter_type,
104 const GVariantType **state_type,
105 GVariant **state_hint,
108 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
110 return g_action_group_query_action (G_ACTION_GROUP (window->priv->actions),
111 action_name, enabled, parameter_type, state_type, state_hint, state);
115 gtk_application_window_activate_action (GActionGroup *group,
116 const gchar *action_name,
119 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
121 return g_action_group_activate_action (G_ACTION_GROUP (window->priv->actions), action_name, parameter);
125 gtk_application_window_change_action_state (GActionGroup *group,
126 const gchar *action_name,
129 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (group);
131 return g_action_group_change_action_state (G_ACTION_GROUP (window->priv->actions), action_name, state);
135 gtk_application_window_lookup_action (GActionMap *action_map,
136 const gchar *action_name)
138 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
140 return g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), action_name);
144 gtk_application_window_add_action (GActionMap *action_map,
147 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
149 g_action_map_add_action (G_ACTION_MAP (window->priv->actions), action);
153 gtk_application_window_remove_action (GActionMap *action_map,
154 const gchar *action_name)
156 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (action_map);
158 g_action_map_remove_action (G_ACTION_MAP (window->priv->actions), action_name);
162 gtk_application_window_group_iface_init (GActionGroupInterface *iface)
164 iface->list_actions = gtk_application_window_list_actions;
165 iface->query_action = gtk_application_window_query_action;
166 iface->activate_action = gtk_application_window_activate_action;
167 iface->change_action_state = gtk_application_window_change_action_state;
171 gtk_application_window_map_iface_init (GActionMapInterface *iface)
173 iface->lookup_action = gtk_application_window_lookup_action;
174 iface->add_action = gtk_application_window_add_action;
175 iface->remove_action = gtk_application_window_remove_action;
178 G_DEFINE_TYPE_WITH_CODE (GtkApplicationWindow, gtk_application_window, GTK_TYPE_WINDOW,
179 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, gtk_application_window_group_iface_init)
180 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, gtk_application_window_map_iface_init))
187 static GParamSpec *gtk_application_window_properties[N_PROPS];
190 gtk_application_window_real_get_preferred_height (GtkWidget *widget,
191 gint *minimum_height,
192 gint *natural_height)
194 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
196 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
197 ->get_preferred_height (widget, minimum_height, natural_height);
199 if (window->priv->menubar != NULL)
201 gint menubar_min_height, menubar_nat_height;
203 gtk_widget_get_preferred_height (GTK_WIDGET (window->priv->menubar), &menubar_min_height, &menubar_nat_height);
204 *minimum_height += menubar_min_height;
205 *natural_height += menubar_nat_height;
210 gtk_application_window_real_get_preferred_height_for_width (GtkWidget *widget,
212 gint *minimum_height,
213 gint *natural_height)
215 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
217 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
218 ->get_preferred_height_for_width (widget, width, minimum_height, natural_height);
220 if (window->priv->menubar != NULL)
222 gint menubar_min_height, menubar_nat_height;
224 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (window->priv->menubar), width, &menubar_min_height, &menubar_nat_height);
225 *minimum_height += menubar_min_height;
226 *natural_height += menubar_nat_height;
231 gtk_application_window_real_get_preferred_width (GtkWidget *widget,
235 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
237 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
238 ->get_preferred_width (widget, minimum_width, natural_width);
240 if (window->priv->menubar != NULL)
242 gint menubar_min_width, menubar_nat_width;
244 gtk_widget_get_preferred_width (GTK_WIDGET (window->priv->menubar), &menubar_min_width, &menubar_nat_width);
245 *minimum_width = MAX (*minimum_width, menubar_min_width);
246 *natural_width = MAX (*natural_width, menubar_nat_width);
251 gtk_application_window_real_get_preferred_width_for_height (GtkWidget *widget,
256 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
258 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
259 ->get_preferred_width_for_height (widget, height, minimum_width, natural_width);
261 if (window->priv->menubar != NULL)
263 gint menubar_min_width, menubar_nat_width;
265 gtk_widget_get_preferred_width_for_height (GTK_WIDGET (window->priv->menubar), height, &menubar_min_width, &menubar_nat_width);
266 *minimum_width = MAX (*minimum_width, menubar_min_width);
267 *natural_width = MAX (*natural_width, menubar_nat_width);
272 gtk_application_window_real_size_allocate (GtkWidget *widget,
273 GtkAllocation *allocation)
275 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
277 if (window->priv->menubar != NULL)
279 GtkAllocation menubar_allocation = *allocation;
280 gint menubar_min_height, menubar_nat_height;
283 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (window->priv->menubar), allocation->width, &menubar_min_height, &menubar_nat_height);
285 menubar_allocation.height = menubar_min_height;
286 gtk_widget_size_allocate (GTK_WIDGET (window->priv->menubar), &menubar_allocation);
288 child = gtk_bin_get_child (GTK_BIN (window));
289 if (child != NULL && gtk_widget_get_visible (child))
291 GtkAllocation child_allocation = *allocation;
294 child_allocation.height = MAX (1, child_allocation.height - menubar_min_height);
296 border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
297 child_allocation.x += border_width;
298 child_allocation.y += border_width + menubar_min_height;
299 child_allocation.width -= border_width * 2;
300 child_allocation.height -= border_width * 2 - menubar_min_height;
301 gtk_widget_size_allocate (child, &child_allocation);
304 gtk_widget_set_allocation (widget, allocation);
307 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
308 ->size_allocate (widget, allocation);
312 gtk_application_window_real_realize (GtkWidget *widget)
314 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
316 if (!window->priv->initialized_gsetting_monitoring)
318 window->priv->initialized_gsetting_monitoring = TRUE;
319 g_signal_connect (gtk_widget_get_settings ((GtkWidget*)window),
320 "notify::gtk-shell-shows-app-menu",
321 G_CALLBACK (on_shell_shows_app_menu_changed),
323 on_shell_shows_app_menu_changed (gtk_widget_get_settings ((GtkWidget*)window), NULL, window);
326 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
331 gtk_application_window_real_map (GtkWidget *widget)
333 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (widget);
335 /* XXX could eliminate this by tweaking gtk_window_map */
336 if (window->priv->menubar)
337 gtk_widget_map (GTK_WIDGET (window->priv->menubar));
339 GTK_WIDGET_CLASS (gtk_application_window_parent_class)
344 gtk_application_window_real_forall_internal (GtkContainer *container,
345 gboolean include_internal,
346 GtkCallback callback,
349 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (container);
351 if (window->priv->menubar)
352 callback (GTK_WIDGET (window->priv->menubar), user_data);
354 GTK_CONTAINER_CLASS (gtk_application_window_parent_class)
355 ->forall (container, include_internal, callback, user_data);
360 gtk_application_window_get_property (GObject *object,
365 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
369 case PROP_SHOW_MENUBAR:
370 g_value_set_boolean (value, window->priv->override_show_menubar);
374 g_assert_not_reached ();
379 gtk_application_window_set_property (GObject *object,
384 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
388 case PROP_SHOW_MENUBAR:
389 gtk_application_window_set_show_menubar (window, g_value_get_boolean (value));
393 g_assert_not_reached ();
398 gtk_application_window_dispose (GObject *object)
400 GtkApplicationWindow *window = GTK_APPLICATION_WINDOW (object);
402 g_clear_object (&window->priv->menubar);
403 g_clear_object (&window->priv->actions);
405 G_OBJECT_CLASS (gtk_application_window_parent_class)
410 gtk_application_window_init (GtkApplicationWindow *window)
412 window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, GTK_TYPE_APPLICATION_WINDOW, GtkApplicationWindowPrivate);
414 window->priv->actions = g_simple_action_group_new ();
416 /* window->priv->actions is the one and only ref on the group, so when
417 * we dispose, the action group will die, disconnecting all signals.
419 g_signal_connect_swapped (window->priv->actions, "action-added",
420 G_CALLBACK (g_action_group_action_added), window);
421 g_signal_connect_swapped (window->priv->actions, "action-enabled-changed",
422 G_CALLBACK (g_action_group_action_enabled_changed), window);
423 g_signal_connect_swapped (window->priv->actions, "action-state-changed",
424 G_CALLBACK (g_action_group_action_state_changed), window);
425 g_signal_connect_swapped (window->priv->actions, "action-removed",
426 G_CALLBACK (g_action_group_action_removed), window);
430 gtk_application_window_class_init (GtkApplicationWindowClass *class)
432 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
433 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
434 GObjectClass *object_class = G_OBJECT_CLASS (class);
436 container_class->forall = gtk_application_window_real_forall_internal;
437 widget_class->get_preferred_height = gtk_application_window_real_get_preferred_height;
438 widget_class->get_preferred_height_for_width = gtk_application_window_real_get_preferred_height_for_width;
439 widget_class->get_preferred_width = gtk_application_window_real_get_preferred_width;
440 widget_class->get_preferred_width_for_height = gtk_application_window_real_get_preferred_width_for_height;
441 widget_class->size_allocate = gtk_application_window_real_size_allocate;
442 widget_class->realize = gtk_application_window_real_realize;
443 widget_class->map = gtk_application_window_real_map;
444 object_class->get_property = gtk_application_window_get_property;
445 object_class->set_property = gtk_application_window_set_property;
446 object_class->dispose = gtk_application_window_dispose;
448 gtk_application_window_properties[PROP_SHOW_MENUBAR] =
449 g_param_spec_boolean ("show-menubar",
450 P_("Show a menubar"),
451 P_("TRUE if the application's menus should be included "
452 "in the menubar at the top of the window"),
453 FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
454 g_object_class_install_properties (object_class, N_PROPS, gtk_application_window_properties);
455 g_type_class_add_private (class, sizeof (GtkApplicationWindowPrivate));
459 * gtk_application_window_new:
460 * @application: a #GtkApplication
462 * Creates a new #GtkApplicationWindow.
464 * Returns: a newly created #GtkApplicationWindow
469 gtk_application_window_new (GtkApplication *application)
471 g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
473 return g_object_new (GTK_TYPE_APPLICATION_WINDOW,
474 "application", application,
479 gtk_application_window_get_show_menubar (GtkApplicationWindow *window)
481 return window->priv->override_show_menubar;
485 recreate_menubar (GtkApplicationWindow *window)
487 GtkWidget *new_menubar;
489 if (window->priv->menubar)
490 gtk_widget_unparent (GTK_WIDGET (window->priv->menubar));
491 g_clear_object (&window->priv->menubar);
493 new_menubar = gtk_application_window_create_menubar (window);
497 window->priv->menubar = g_object_ref_sink (new_menubar);
498 gtk_widget_set_parent (new_menubar, GTK_WIDGET (window));
499 gtk_widget_show_all (new_menubar);
504 gtk_application_window_set_show_menubar (GtkApplicationWindow *window,
508 window->priv->did_override_show_menubar = TRUE;
509 if (window->priv->override_show_menubar != show)
511 window->priv->override_show_menubar = show;
512 recreate_menubar (window);
513 g_object_notify_by_pspec (G_OBJECT (window), gtk_application_window_properties[PROP_SHOW_MENUBAR]);
517 /* GtkMenu construction {{{1 */
523 gulong enabled_changed_id;
524 gulong state_changed_id;
525 gulong activate_handler;
529 action_data_free (gpointer data)
531 ActionData *a = data;
533 if (a->enabled_changed_id)
534 g_signal_handler_disconnect (a->group, a->enabled_changed_id);
536 if (a->state_changed_id)
537 g_signal_handler_disconnect (a->group, a->state_changed_id);
539 g_object_unref (a->group);
547 enabled_changed (GActionGroup *group,
548 const gchar *action_name,
552 gtk_widget_set_sensitive (widget, enabled);
556 item_activated (GtkWidget *w,
562 a = g_object_get_data (G_OBJECT (w), "action");
564 parameter = g_variant_ref_sink (g_variant_new_string (a->target));
567 g_action_group_activate_action (a->group, a->name, parameter);
569 g_variant_unref (parameter);
573 toggle_state_changed (GActionGroup *group,
580 a = g_object_get_data (G_OBJECT (w), "action");
581 g_signal_handler_block (w, a->activate_handler);
582 gtk_check_menu_item_set_active (w, g_variant_get_boolean (state));
583 g_signal_handler_unblock (w, a->activate_handler);
587 radio_state_changed (GActionGroup *group,
595 a = g_object_get_data (G_OBJECT (w), "action");
596 g_signal_handler_block (w, a->activate_handler);
597 b = g_strcmp0 (a->target, g_variant_get_string (state, NULL)) == 0;
598 gtk_check_menu_item_set_active (w, b);
599 g_signal_handler_unblock (w, a->activate_handler);
603 create_menuitem_from_model (GMenuModel *model,
613 const GVariantType *type;
617 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_LABEL, "s", &label);
620 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_ACTION, "s", &action);
623 type = g_action_group_get_action_state_type (group, action);
628 w = gtk_menu_item_new_with_label (label);
629 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
630 w = gtk_check_menu_item_new_with_label (label);
631 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
633 w = gtk_check_menu_item_new_with_label (label);
634 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (w), TRUE);
637 g_assert_not_reached ();
639 gtk_menu_item_set_use_underline (GTK_MENU_ITEM (w), TRUE);
643 a = g_new0 (ActionData, 1);
644 a->group = g_object_ref (group);
645 a->name = g_strdup (action);
646 g_object_set_data_full (G_OBJECT (w), "action", a, action_data_free);
648 if (!g_action_group_get_action_enabled (group, action))
649 gtk_widget_set_sensitive (w, FALSE);
651 s = g_strconcat ("action-enabled-changed::", action, NULL);
652 a->enabled_changed_id = g_signal_connect (group, s,
653 G_CALLBACK (enabled_changed), w);
655 a->activate_handler = g_signal_connect (w, "activate",
656 G_CALLBACK (item_activated), NULL);
662 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
664 s = g_strconcat ("action-state-changed::", action, NULL);
665 a->state_changed_id = g_signal_connect (group, s,
666 G_CALLBACK (toggle_state_changed), w);
668 v = g_action_group_get_action_state (group, action);
669 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
670 g_variant_get_boolean (v));
673 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
675 s = g_strconcat ("action-state-changed::", action, NULL);
676 a->state_changed_id = g_signal_connect (group, s,
677 G_CALLBACK (radio_state_changed), w);
679 g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_TARGET, "s", &target);
680 a->target = g_strdup (target);
681 v = g_action_group_get_action_state (group, action);
682 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
683 g_strcmp0 (g_variant_get_string (v, NULL), target) == 0);
688 g_assert_not_reached ();
697 static void populate_menu_from_model (GtkMenuShell *menu,
699 GActionGroup *group);
702 append_items_from_model (GtkMenuShell *menu,
705 gboolean *need_separator,
706 const gchar *heading)
716 n = g_menu_model_get_n_items (model);
718 if (*need_separator && n > 0)
720 w = gtk_separator_menu_item_new ();
722 gtk_menu_shell_append (menu, w);
723 *need_separator = FALSE;
728 w = gtk_menu_item_new_with_label (heading);
730 gtk_widget_set_sensitive (w, FALSE);
731 gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
734 for (i = 0; i < n; i++)
736 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION)))
739 g_menu_model_get_item_attribute (model, i, G_MENU_ATTRIBUTE_LABEL, "s", &label);
740 append_items_from_model (menu, m, group, need_separator, label);
746 w = gtk_separator_menu_item_new ();
748 gtk_menu_shell_append (menu, w);
749 *need_separator = FALSE;
755 menuitem = create_menuitem_from_model (model, i, group);
757 if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SUBMENU)))
759 submenu = gtk_menu_new ();
760 populate_menu_from_model (GTK_MENU_SHELL (submenu), m, group);
761 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
765 gtk_widget_show (menuitem);
766 gtk_menu_shell_append (menu, menuitem);
768 *need_separator = TRUE;
773 populate_menu_from_model (GtkMenuShell *menu,
777 gboolean need_separator;
779 need_separator = FALSE;
780 append_items_from_model (menu, model, group, &need_separator, NULL);
785 GtkApplication *application;
786 GtkApplicationWindow *window;
789 GHashTable *connected;
793 free_items_changed_data (gpointer data)
795 ItemsChangedData *d = data;
797 g_object_unref (d->muxer);
798 g_object_unref (d->window);
799 g_object_unref (d->application);
801 if (d->update_idle != 0)
802 g_source_remove (d->update_idle);
804 g_hash_table_unref (d->connected);
810 repopulate_menu (gpointer data)
812 ItemsChangedData *d = data;
815 GtkMenuShell *app_shell;
816 GMenuModel *app_model;
817 GMenuModel *window_model;
819 /* remove current children */
820 children = gtk_container_get_children (GTK_CONTAINER (d->menu));
821 for (l = children; l; l = l->next)
824 gtk_container_remove (GTK_CONTAINER (d->menu), child);
826 g_list_free (children);
828 app_model = g_application_get_app_menu (G_APPLICATION (d->application));
830 if (app_model && !d->window->priv->shell_shows_app_menu)
832 child = gtk_menu_item_new_with_label (_("Application"));
833 app_shell = (GtkMenuShell*)gtk_menu_new ();
834 gtk_menu_item_set_submenu ((GtkMenuItem*)child, (GtkWidget*)app_shell);
836 populate_menu_from_model (app_shell, app_model, (GActionGroup*)d->muxer);
837 gtk_menu_shell_append ((GtkMenuShell*)d->menu, child);
840 window_model = g_application_get_menubar (G_APPLICATION (d->application));
843 populate_menu_from_model (d->menu, window_model, (GActionGroup*)d->muxer);
852 connect_to_items_changed (GMenuModel *model,
856 ItemsChangedData *d = data;
861 if (!g_hash_table_lookup (d->connected, model))
863 g_signal_connect (model, "items-changed", callback, data);
864 g_hash_table_insert (d->connected, model, model);
867 for (i = 0; i < g_menu_model_get_n_items (model); i++)
869 iter = g_menu_model_iterate_item_links (model, i);
870 while (g_menu_link_iter_next (iter))
872 m = g_menu_link_iter_get_value (iter);
873 connect_to_items_changed (m, callback, data);
876 g_object_unref (iter);
881 items_changed (GMenuModel *model,
887 ItemsChangedData *d = data;
889 if (d->update_idle == 0)
890 d->update_idle = gdk_threads_add_idle (repopulate_menu, data);
891 connect_to_items_changed (model, G_CALLBACK (items_changed), data);
895 gtk_application_window_create_menubar (GtkApplicationWindow *window)
897 GtkApplication *application;
898 GMenuModel *app_model;
899 GMenuModel *win_model;
900 ItemsChangedData *data;
903 application = gtk_window_get_application (GTK_WINDOW (window));
904 app_model = g_application_get_app_menu (G_APPLICATION (application));
905 win_model = g_application_get_menubar (G_APPLICATION (application));
907 if (!(app_model || win_model)
908 || (window->priv->did_override_show_menubar
909 && window->priv->override_show_menubar == FALSE))
912 menubar = gtk_menu_bar_new ();
914 data = g_new (ItemsChangedData, 1);
915 data->muxer = g_action_muxer_new ();
916 g_action_muxer_insert (data->muxer, "app", G_ACTION_GROUP (application));
917 g_action_muxer_insert (data->muxer, "win", G_ACTION_GROUP (window));
918 data->window = g_object_ref (window);
919 data->application = g_object_ref (application);
920 data->menu = GTK_MENU_SHELL (menubar);
921 data->update_idle = 0;
922 data->connected = g_hash_table_new (NULL, NULL);
924 g_object_set_data_full (G_OBJECT (menubar), "gtk-application-menu-data",
925 data, free_items_changed_data);
928 connect_to_items_changed (app_model, G_CALLBACK (items_changed), data);
930 connect_to_items_changed (win_model, G_CALLBACK (items_changed), data);
932 repopulate_menu (data);