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