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