fixed goto case dialog
[pspp] / src / ui / gui / psppire-window.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2009, 2010, 2011, 2013, 2014  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program 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
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <gl/relocatable.h>
19
20 #include "psppire-window.h"
21 #include "psppire-window-base.h"
22
23 #include <gtk/gtk.h>
24
25 #include <stdlib.h>
26
27 #include <gettext.h>
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
30
31 #include "data/any-reader.h"
32 #include "data/file-handle-def.h"
33 #include "data/dataset.h"
34 #include "libpspp/version.h"
35 #include "output/group-item.h"
36 #include "output/pivot-table.h"
37 #include "output/spv/spv.h"
38 #include "output/spv/spv-output.h"
39 #include "output/spv/spv-select.h"
40
41 #include "helper.h"
42 #include "psppire-data-window.h"
43 #include "psppire-encoding-selector.h"
44 #include "psppire-syntax-window.h"
45 #include "psppire-window-register.h"
46
47 static void psppire_window_base_init     (PsppireWindowClass *class);
48 static void psppire_window_class_init    (PsppireWindowClass *class);
49 static void psppire_window_init          (PsppireWindow      *window);
50
51
52 static GObjectClass *parent_class;
53
54 GType
55 psppire_window_get_type (void)
56 {
57   static GType psppire_window_type = 0;
58
59   if (!psppire_window_type)
60     {
61       static const GTypeInfo psppire_window_info =
62       {
63         sizeof (PsppireWindowClass),
64         (GBaseInitFunc) psppire_window_base_init,
65         (GBaseFinalizeFunc) NULL,
66         (GClassInitFunc) psppire_window_class_init,
67         (GClassFinalizeFunc) NULL,
68         NULL,
69         sizeof (PsppireWindow),
70         0,
71         (GInstanceInitFunc) psppire_window_init,
72       };
73
74       psppire_window_type =
75         g_type_register_static (PSPPIRE_TYPE_WINDOW_BASE, "PsppireWindow",
76                                 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
77     }
78
79   return psppire_window_type;
80 }
81
82
83 /* Properties */
84 enum
85 {
86   PROP_0,
87   PROP_FILENAME,
88   PROP_DESCRIPTION,
89   PROP_ID
90 };
91
92
93 static void
94 psppire_window_set_title (PsppireWindow *window)
95 {
96   GString *title = g_string_sized_new (80);
97
98   if (window->dirty)
99     g_string_append_c (title, '*');
100
101   if (window->basename || window->id)
102     {
103       if (window->basename)
104         g_string_append_printf (title, "%s ", window->basename);
105
106       if (window->id)
107         g_string_append_printf (title, "[%s] ", window->id);
108
109       g_string_append_unichar (title, 0x2014); /* em dash */
110       g_string_append_c (title, ' '); /* em dash */
111     }
112
113   g_string_append_printf (title, "PSPPIRE %s", window->description);
114
115   int minor = 1;
116   sscanf (bare_version, "%*d.%d.%*d", &minor);
117   if (minor % 2)
118     g_string_append_printf (title, " - Test version! Please report bugs to %s", PACKAGE_BUGREPORT);
119
120   gtk_window_set_title (GTK_WINDOW (window), title->str);
121
122   g_string_free (title, TRUE);
123 }
124
125 static void
126 psppire_window_update_list_name (PsppireWindow *window)
127 {
128   PsppireWindowRegister *reg = psppire_window_register_new ();
129   GString *candidate = g_string_sized_new (80);
130   int n;
131
132   n = 1;
133   do
134     {
135       /* Compose a name. */
136       g_string_truncate (candidate, 0);
137       if (window->filename)
138         {
139           gchar *display_filename = g_filename_display_name (window->filename);
140           g_string_append (candidate, display_filename);
141           g_free (display_filename);
142
143           if (window->id)
144             g_string_append_printf (candidate, " [%s]", window->id);
145         }
146       else if (window->id)
147         g_string_append_printf (candidate, "[%s]", window->id);
148       else
149         g_string_append (candidate, window->description);
150
151       if (n++ > 1)
152         g_string_append_printf (candidate, " #%d", n);
153
154       if (window->list_name && !strcmp (candidate->str, window->list_name))
155         {
156           /* Keep the existing name. */
157           g_string_free (candidate, TRUE);
158           return;
159         }
160     }
161   while (psppire_window_register_lookup (reg, candidate->str));
162
163   if (window->list_name)
164     psppire_window_register_remove (reg, window->list_name);
165
166   g_free (window->list_name);
167   window->list_name = g_string_free (candidate, FALSE);
168
169   psppire_window_register_insert (reg, window, window->list_name);
170 }
171
172 static void
173 psppire_window_name_changed (PsppireWindow *window)
174 {
175   psppire_window_set_title (window);
176   psppire_window_update_list_name (window);
177 }
178
179 static void
180 psppire_window_set_property (GObject         *object,
181                              guint            prop_id,
182                              const GValue    *value,
183                              GParamSpec      *pspec)
184 {
185   PsppireWindow *window = PSPPIRE_WINDOW (object);
186
187   switch (prop_id)
188     {
189     case PROP_DESCRIPTION:
190       g_free (window->description);
191       window->description = g_value_dup_string (value);
192       psppire_window_set_title (window);
193       break;
194     case PROP_FILENAME:
195       g_free (window->filename);
196       window->filename = g_value_dup_string (value);
197       g_free (window->basename);
198       window->basename = (window->filename
199                           ? g_filename_display_basename (window->filename)
200                           : NULL);
201       psppire_window_name_changed (window);
202       break;
203     case PROP_ID:
204       g_free (window->id);
205       window->id = g_value_dup_string (value);
206       psppire_window_name_changed (window);
207       break;
208     default:
209       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210       break;
211     };
212 }
213
214
215 static void
216 psppire_window_get_property (GObject         *object,
217                              guint            prop_id,
218                              GValue          *value,
219                              GParamSpec      *pspec)
220 {
221   PsppireWindow *window = PSPPIRE_WINDOW (object);
222
223   switch (prop_id)
224     {
225     case PROP_FILENAME:
226       g_value_set_string (value, window->filename);
227       break;
228     case PROP_DESCRIPTION:
229       g_value_set_string (value, window->description);
230       break;
231     case PROP_ID:
232       g_value_set_string (value, window->id);
233       break;
234     default:
235       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236       break;
237     };
238 }
239
240
241 static void
242 psppire_window_finalize (GObject *object)
243 {
244   PsppireWindow *window = PSPPIRE_WINDOW (object);
245
246   PsppireWindowRegister *reg = psppire_window_register_new ();
247
248   g_signal_handler_disconnect (reg, window->remove_handler);
249   g_signal_handler_disconnect (reg, window->insert_handler);
250   psppire_window_register_remove (reg, window->list_name);
251   g_free (window->filename);
252   g_free (window->basename);
253   g_free (window->id);
254   g_free (window->description);
255   g_free (window->list_name);
256
257   g_hash_table_destroy (window->menuitem_table);
258
259   if (G_OBJECT_CLASS (parent_class)->finalize)
260     G_OBJECT_CLASS (parent_class)->finalize (object);
261 }
262
263 static void
264 psppire_window_class_init (PsppireWindowClass *class)
265 {
266   GObjectClass *object_class = G_OBJECT_CLASS (class);
267
268   GParamSpec *description_spec =
269     null_if_empty_param ("description",
270                        "Description",
271                        "A string describing the usage of the window",
272                          NULL, /*Should be overridden by derived classes */
273                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
274
275   GParamSpec *filename_spec =
276     null_if_empty_param ("filename",
277                        "File name",
278                        "The name of the file associated with this window, if any",
279                          NULL,
280                          G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
281
282   GParamSpec *id_spec =
283     null_if_empty_param ("id",
284                          "Identifier",
285                          "The PSPP language identifier for the data associated "
286                          "with this window (e.g. dataset name)",
287                          NULL,
288                          G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
289
290   object_class->set_property = psppire_window_set_property;
291   object_class->get_property = psppire_window_get_property;
292
293   g_object_class_install_property (object_class,
294                                    PROP_DESCRIPTION,
295                                    description_spec);
296
297   g_object_class_install_property (object_class,
298                                    PROP_FILENAME,
299                                    filename_spec);
300
301   g_object_class_install_property (object_class,
302                                    PROP_ID,
303                                    id_spec);
304
305   parent_class = g_type_class_peek_parent (class);
306 }
307
308
309 static void
310 psppire_window_base_init (PsppireWindowClass *class)
311 {
312   GObjectClass *object_class = G_OBJECT_CLASS (class);
313
314   object_class->finalize = psppire_window_finalize;
315 }
316
317
318
319 static void
320 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
321 {
322   gchar *filename;
323   GtkWidget *item;
324   filename = g_filename_display_name (key);
325   item = gtk_check_menu_item_new_with_label (filename);
326   g_free (filename);
327
328   g_hash_table_insert (window->menuitem_table, key, item);
329 }
330
331 static void
332 insert_item (gpointer key, gpointer value, gpointer data)
333 {
334   PsppireWindow *window = PSPPIRE_WINDOW (data);
335
336   if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
337     return;
338
339   insert_menuitem_into_menu (window, key);
340 }
341
342 /* Insert a new item into the window menu */
343 static void
344 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
345 {
346   PsppireWindow *window = PSPPIRE_WINDOW (data);
347
348   insert_menuitem_into_menu (window, (gpointer) key);
349 }
350
351
352 static void
353 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
354 {
355   PsppireWindow *window = PSPPIRE_WINDOW (data);
356   g_hash_table_remove (window->menuitem_table, key);
357 }
358
359 static void
360 insert_existing_items (PsppireWindow *window)
361 {
362   psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
363 }
364
365
366 static gboolean
367 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
368 {
369   PsppireWindowRegister *reg = psppire_window_register_new ();
370
371   if ( w->dirty )
372     {
373       gint response = psppire_window_query_save (w);
374
375       switch (response)
376         {
377         default:
378         case GTK_RESPONSE_CANCEL:
379           return TRUE;
380           break;
381         case GTK_RESPONSE_APPLY:
382           psppire_window_save (w);
383           if (w->dirty)
384             {
385               /* Save failed, or user exited Save As dialog with Cancel. */
386               return TRUE;
387             }
388           break;
389         case GTK_RESPONSE_REJECT:
390           break;
391         }
392     }
393
394   if ( 1 == psppire_window_register_n_items (reg))
395     gtk_main_quit ();
396
397   return FALSE;
398 }
399
400
401 static void
402 psppire_window_init (PsppireWindow *window)
403 {
404   window->filename = NULL;
405   window->basename = NULL;
406   window->id = NULL;
407   window->description = NULL;
408   window->list_name = NULL;
409
410   window->menuitem_table  = g_hash_table_new (g_str_hash, g_str_equal);
411
412
413   g_signal_connect (window,  "realize", G_CALLBACK (insert_existing_items), NULL);
414
415   window->insert_handler = g_signal_connect (psppire_window_register_new (),
416                                              "inserted",
417                                              G_CALLBACK (insert_menuitem),
418                                              window);
419
420   window->remove_handler = g_signal_connect (psppire_window_register_new (),
421                                              "removed",
422                                              G_CALLBACK (remove_menuitem),
423                                              window);
424
425   window->added_separator = FALSE;
426   window->dirty = FALSE;
427
428   g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
429
430   g_object_set (window, "icon-name", "pspp", NULL);
431 }
432
433 /*
434    Ask the user if the buffer should be saved.
435    Return the response.
436 */
437 gint
438 psppire_window_query_save (PsppireWindow *se)
439 {
440   gint response;
441   GtkWidget *dialog;
442   GtkWidget *cancel_button;
443
444   gchar *description;
445
446   GTimeVal time;
447
448   g_get_current_time (&time);
449
450   if (se->filename)
451     description = g_filename_display_basename (se->filename);
452   else if (se->id)
453     description = g_strdup (se->id);
454   else
455     description = g_strdup (se->description);
456   dialog =
457     gtk_message_dialog_new (GTK_WINDOW (se),
458                             GTK_DIALOG_MODAL,
459                             GTK_MESSAGE_WARNING,
460                             GTK_BUTTONS_NONE,
461                             _("Save the changes to `%s' before closing?"),
462                             description);
463   g_free (description);
464
465   g_object_set (dialog, "icon-name", "pspp", NULL);
466
467   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
468                                             _("If you don't save, changes from the last %ld seconds will be permanently lost."),
469                                             time.tv_sec - se->savetime.tv_sec);
470
471   gtk_dialog_add_button  (GTK_DIALOG (dialog),
472                           _("Close _without saving"),
473                           GTK_RESPONSE_REJECT);
474
475   cancel_button = gtk_dialog_add_button  (GTK_DIALOG (dialog),
476                                           _("Cancel"),
477                                           GTK_RESPONSE_CANCEL);
478
479   gtk_dialog_add_button  (GTK_DIALOG (dialog),
480                           _("Save"),
481                           GTK_RESPONSE_APPLY);
482
483   gtk_widget_grab_focus (cancel_button);
484
485   response = gtk_dialog_run (GTK_DIALOG (dialog));
486
487   gtk_widget_destroy (dialog);
488
489   return response;
490 }
491
492
493 /* The return value is encoded in the glib filename encoding. */
494 const gchar *
495 psppire_window_get_filename (PsppireWindow *w)
496 {
497   return w->filename;
498 }
499
500
501 /* FILENAME must be encoded in the glib filename encoding. */
502 void
503 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
504 {
505   g_object_set (w, "filename", filename, NULL);
506 }
507
508 void
509 psppire_window_set_unsaved (PsppireWindow *w)
510 {
511   if ( w->dirty == FALSE)
512     g_get_current_time (&w->savetime);
513
514   w->dirty = TRUE;
515
516   psppire_window_set_title (w);
517 }
518
519 gboolean
520 psppire_window_get_unsaved (PsppireWindow *w)
521 {
522   return w->dirty;
523 }
524
525
526 \f
527
528
529 static void
530 minimise_window (gpointer key, gpointer value, gpointer data)
531 {
532   gtk_window_iconify (GTK_WINDOW (value));
533 }
534
535
536 void
537 psppire_window_minimise_all (void)
538 {
539   PsppireWindowRegister *reg = psppire_window_register_new ();
540
541   g_hash_table_foreach (reg->name_table, minimise_window, NULL);
542 }
543
544
545 \f
546
547 GType
548 psppire_window_model_get_type (void)
549 {
550   static GType window_model_type = 0;
551
552   if (! window_model_type)
553     {
554       static const GTypeInfo window_model_info =
555       {
556         sizeof (PsppireWindowIface), /* class_size */
557         NULL,           /* base_init */
558         NULL,           /* base_finalize */
559         NULL,
560         NULL,           /* class_finalize */
561         NULL,           /* class_data */
562         0,
563         0,              /* n_preallocs */
564         NULL
565       };
566
567       window_model_type =
568         g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
569                                 &window_model_info, 0);
570
571       g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
572     }
573
574   return window_model_type;
575 }
576
577
578 void
579 psppire_window_save (PsppireWindow *w)
580 {
581   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
582
583   g_assert (i);
584   g_return_if_fail (i->save);
585
586   if (w->filename == NULL)
587     psppire_window_save_as (w);
588   else
589     {
590       i->save (w);
591       w->dirty = FALSE;
592       psppire_window_set_title (w);
593     }
594 }
595
596 void
597 psppire_window_save_as (PsppireWindow *w)
598 {
599   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
600   gchar *old_filename;
601
602   g_assert (i);
603   g_return_if_fail (i->pick_filename);
604
605   old_filename = w->filename;
606   w->filename = NULL;
607
608   i->pick_filename (w);
609   if (w->filename == NULL)
610     w->filename = old_filename;
611   else
612     {
613       g_free (old_filename);
614       psppire_window_save (w);
615     }
616 }
617
618 static void delete_recent (const char *file_name);
619
620 gboolean
621 psppire_window_load (PsppireWindow *w, const gchar *file,
622                      const gchar *encoding, gpointer hint)
623 {
624   gboolean ok;
625   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
626
627   g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
628
629   g_assert (i);
630
631   g_return_val_if_fail (i->load, FALSE);
632
633   ok = i->load (w, file, encoding, hint);
634
635   if ( ok )
636     {
637       psppire_window_set_filename (w, file);
638       w->dirty = FALSE;
639     }
640   else
641     delete_recent (file);
642
643   return ok;
644 }
645
646
647 GtkWidget *
648 psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
649 {
650   GtkFileFilter *filter = gtk_file_filter_new ();
651   GtkWidget *dialog =
652     gtk_file_chooser_dialog_new (_("Open"),
653                                  GTK_WINDOW (toplevel),
654                                  GTK_FILE_CHOOSER_ACTION_OPEN,
655                                  _("Cancel"), GTK_RESPONSE_CANCEL,
656                                  _("Open"), GTK_RESPONSE_ACCEPT,
657                                  NULL);
658
659   g_object_set (dialog, "local-only", FALSE, NULL);
660
661   gtk_file_filter_set_name (filter, _("Data and Syntax Files"));
662   gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
663   gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
664   gtk_file_filter_add_mime_type (filter, "application/x-spss-spv");
665   gtk_file_filter_add_pattern (filter, "*.zsav");
666   gtk_file_filter_add_pattern (filter, "*.sps");
667   gtk_file_filter_add_pattern (filter, "*.SPS");
668   gtk_file_filter_add_pattern (filter, "*.spv");
669   gtk_file_filter_add_pattern (filter, "*.SPV");
670   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
671
672   filter = gtk_file_filter_new ();
673   gtk_file_filter_set_name (filter, _("System Files (*.sav, *.zsav)"));
674   gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
675   gtk_file_filter_add_pattern (filter, "*.zsav");
676   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
677
678   filter = gtk_file_filter_new ();
679   gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
680   gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
681   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
682
683   filter = gtk_file_filter_new ();
684   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
685   gtk_file_filter_add_pattern (filter, "*.sps");
686   gtk_file_filter_add_pattern (filter, "*.SPS");
687   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
688
689   filter = gtk_file_filter_new ();
690   gtk_file_filter_set_name (filter, _("Output Files (*.spv) "));
691   gtk_file_filter_add_pattern (filter, "*.spv");
692   gtk_file_filter_add_pattern (filter, "*.SPV");
693   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
694
695   filter = gtk_file_filter_new ();
696   gtk_file_filter_set_name (filter, _("All Files"));
697   gtk_file_filter_add_pattern (filter, "*");
698   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
699
700   if (toplevel->filename)
701     {
702       const gchar *filename = toplevel->filename;
703       gchar *dir_name;
704
705       if ( ! g_path_is_absolute (filename))
706         {
707           gchar *path =
708             g_build_filename (g_get_current_dir (), filename, NULL);
709           dir_name = g_path_get_dirname (path);
710           g_free (path);
711         }
712       else
713         {
714           dir_name = g_path_get_dirname (filename);
715         }
716       gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
717                                            dir_name);
718       free (dir_name);
719     }
720
721     gtk_file_chooser_set_extra_widget (
722       GTK_FILE_CHOOSER (dialog),
723       psppire_encoding_selector_new ("Auto", true));
724
725   return dialog;
726 }
727
728 struct item_path
729   {
730     const struct spv_item **nodes;
731     size_t n;
732
733 #define N_STUB 10
734     const struct spv_item *stub[N_STUB];
735   };
736
737 static void
738 swap_nodes (const struct spv_item **a, const struct spv_item **b)
739 {
740   const struct spv_item *tmp = *a;
741   *a = *b;
742   *b = tmp;
743 }
744
745 static void
746 get_path (const struct spv_item *item, struct item_path *path)
747 {
748   size_t allocated = 10;
749   path->nodes = path->stub;
750   path->n = 0;
751
752   while (item)
753     {
754       if (path->n >= allocated)
755         {
756           if (path->nodes == path->stub)
757             path->nodes = xmemdup (path->stub, sizeof path->stub);
758           path->nodes = x2nrealloc (path->nodes, &allocated,
759                                     sizeof *path->nodes);
760         }
761       path->nodes[path->n++] = item;
762       item = item->parent;
763     }
764
765   for (size_t i = 0; i < path->n / 2; i++)
766     swap_nodes (&path->nodes[i], &path->nodes[path->n - i - 1]);
767 }
768
769 static void
770 free_path (struct item_path *path)
771 {
772   if (path && path->nodes != path->stub)
773     free (path->nodes);
774 }
775
776 static void
777 dump_heading_transition (const struct spv_item *old,
778                          const struct spv_item *new)
779 {
780   if (old == new)
781     return;
782
783   struct item_path old_path, new_path;
784   get_path (old, &old_path);
785   get_path (new, &new_path);
786
787   size_t common = 0;
788   for (; common < old_path.n && common < new_path.n; common++)
789     if (old_path.nodes[common] != new_path.nodes[common])
790       break;
791
792   for (size_t i = common; i < old_path.n; i++)
793     group_close_item_submit (group_close_item_create ());
794   for (size_t i = common; i < new_path.n; i++)
795     group_open_item_submit (group_open_item_create (
796                               new_path.nodes[i]->command_id));
797
798   free_path (&old_path);
799   free_path (&new_path);
800 }
801
802 void
803 read_spv_file (const char *filename)
804 {
805   struct spv_reader *spv;
806   char *error = spv_open (filename, &spv);
807   if (error)
808     {
809       /* XXX */
810       fprintf (stderr, "%s\n", error);
811       return;
812     }
813
814   struct spv_item **items;
815   size_t n_items;
816   spv_select (spv, NULL, 0, &items, &n_items);
817   struct spv_item *prev_heading = spv_get_root (spv);
818   for (size_t i = 0; i < n_items; i++)
819     {
820       struct spv_item *heading
821         = items[i]->type == SPV_ITEM_HEADING ? items[i] : items[i]->parent;
822       dump_heading_transition (prev_heading, heading);
823       if (items[i]->type == SPV_ITEM_TEXT)
824         spv_text_submit (items[i]);
825       else if (items[i]->type == SPV_ITEM_TABLE)
826         pivot_table_submit (spv_item_get_table (items[i]));
827       prev_heading = heading;
828     }
829   dump_heading_transition (prev_heading, spv_get_root (spv));
830   free (items);
831   spv_close (spv);
832 }
833
834 /* Callback for the file_open action.
835    Prompts for a filename and opens it */
836 void
837 psppire_window_open (PsppireWindow *de)
838 {
839   GtkWidget *dialog = psppire_window_file_chooser_dialog (de);
840
841   gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), relocate (examples_dir), NULL);
842
843   switch (gtk_dialog_run (GTK_DIALOG (dialog)))
844     {
845     case GTK_RESPONSE_ACCEPT:
846       {
847         gchar *name =
848           gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
849
850         const gchar **cs = NULL;
851         g_get_filename_charsets (&cs);
852
853         gchar *encoding = psppire_encoding_selector_get_encoding (
854           gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
855
856         struct file_handle *fh = fh_create_file (NULL, name, cs[0], fh_default_properties ());
857
858         int retval = any_reader_detect (fh, NULL);
859         if (retval == 1)
860           open_data_window (de, name, encoding, NULL);
861         else if (retval == 0)
862           {
863             char *error = spv_detect (name);
864             if (!error)
865               read_spv_file (name);
866             else
867               {
868                 free (error);
869                 open_syntax_window (name, encoding);
870               }
871           }
872
873         g_free (encoding);
874         fh_unref (fh);
875         g_free (name);
876       }
877       break;
878     default:
879       break;
880     }
881
882   gtk_widget_destroy (dialog);
883 }
884
885
886 /* Puts FILE_NAME (encoded in the glib file name encoding) into the recent list
887    with associated MIME_TYPE.  If it's already in the list, it moves it to the
888    top. */
889 void
890 add_most_recent (const char *file_name,
891                  const char *mime_type, const char *encoding)
892 {
893   gchar *uri = g_filename_to_uri  (file_name, NULL, NULL);
894   if ( uri )
895     {
896       GtkRecentData recent_data;
897       gchar *full_mime_type;
898
899       if (encoding && encoding[0])
900         full_mime_type = g_strdup_printf ("%s; charset=%s",
901                                           mime_type, encoding);
902       else
903         full_mime_type = g_strdup (mime_type);
904
905       recent_data.display_name = NULL;
906       recent_data.description = NULL;
907       recent_data.mime_type = full_mime_type;
908       recent_data.app_name = CONST_CAST (gchar *, g_get_application_name ());
909       recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
910       recent_data.groups = NULL;
911       recent_data.is_private = FALSE;
912
913       gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
914                                    uri, &recent_data);
915
916       g_free (recent_data.app_exec);
917       g_free (full_mime_type);
918     }
919
920   g_free (uri);
921 }
922
923
924
925 /*
926    If FILE_NAME exists in the recent list, then  delete it.
927  */
928 static void
929 delete_recent (const char *file_name)
930 {
931   gchar *uri = g_filename_to_uri  (file_name, NULL, NULL);
932
933   if ( uri )
934     gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);
935
936   g_free (uri);
937 }
938