+static void
+menu_toggled (GtkCheckMenuItem *mi, gpointer data)
+{
+ /* Prohibit changes to the state */
+ mi->active = !mi->active;
+}
+
+
+/* Look up the window associated with this menuitem and present it to the user */
+static void
+menu_activate (GtkMenuItem *mi, gpointer data)
+{
+ const gchar *key = data;
+
+ PsppireWindowRegister *reg = psppire_window_register_new ();
+
+ PsppireWindow *window = psppire_window_register_lookup (reg, key);
+
+ gtk_window_present (GTK_WINDOW (window));
+}
+
+static void
+insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
+{
+ GtkWidget *item = gtk_check_menu_item_new_with_label (key);
+
+ g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
+ g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
+
+ gtk_widget_show (item);
+
+ gtk_menu_shell_append (window->menu, item);
+
+ /* Set the state without emitting a signal */
+ GTK_CHECK_MENU_ITEM (item)->active =
+ (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
+
+ g_hash_table_insert (window->menuitem_table, key, item);
+}
+
+static void
+insert_item (gpointer key, gpointer value, gpointer data)
+{
+ PsppireWindow *window = PSPPIRE_WINDOW (data);
+
+ if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
+ return;
+
+ insert_menuitem_into_menu (window, key);
+}
+
+/* Insert a new item into the window menu */
+static void
+insert_menuitem (GObject *reg, const gchar *key, gpointer data)
+{
+ PsppireWindow *window = PSPPIRE_WINDOW (data);
+
+ insert_menuitem_into_menu (window, (gpointer) key);
+}
+
+
+static void
+remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
+{
+ PsppireWindow *window = PSPPIRE_WINDOW (data);
+ GtkWidget *item ;
+
+ item = g_hash_table_lookup (window->menuitem_table, key);
+
+ g_hash_table_remove (window->menuitem_table, key);
+
+ if (GTK_IS_CONTAINER (window->menu))
+ gtk_container_remove (GTK_CONTAINER (window->menu), item);
+}
+
+static void
+insert_existing_items (PsppireWindow *window)
+{
+ psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
+}
+
+
+static gboolean
+on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
+{
+ PsppireWindowRegister *reg = psppire_window_register_new ();
+
+ if ( w->unsaved )
+ {
+ gint response = psppire_window_query_save (w);
+
+ if ( response == GTK_RESPONSE_CANCEL)
+ return TRUE;
+
+ if ( response == GTK_RESPONSE_ACCEPT)
+ {
+ psppire_window_save (w);
+ }
+ }
+
+ if ( 1 == psppire_window_register_n_items (reg))
+ gtk_main_quit ();
+
+ return FALSE;
+}