1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009, 2010, 2011, 2013, 2014, 2020 Free Software Foundation
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.
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.
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/>. */
18 #include <gl/relocatable.h>
20 #include "psppire-window.h"
21 #include "psppire-window-base.h"
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
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"
43 #include "psppire-data-window.h"
44 #include "psppire-encoding-selector.h"
45 #include "psppire-syntax-window.h"
46 #include "psppire-window-register.h"
48 static void psppire_window_class_init (PsppireWindowClass *class);
49 static void psppire_window_init (PsppireWindow *window);
51 static GObjectClass *parent_class;
53 G_DEFINE_ABSTRACT_TYPE (PsppireWindow, psppire_window, PSPPIRE_TYPE_WINDOW_BASE)
66 psppire_window_set_title (PsppireWindow *window)
68 GString *title = g_string_sized_new (80);
70 if (window->edited != NULL)
71 g_string_append_c (title, '*');
73 if (window->basename || window->id)
76 g_string_append_printf (title, "%s ", window->basename);
79 g_string_append_printf (title, "[%s] ", window->id);
81 g_string_append_unichar (title, 0x2014); /* em dash */
82 g_string_append_c (title, ' '); /* em dash */
85 g_string_append_printf (title, "PSPPIRE %s", window->description);
88 sscanf (bare_version, "%*d.%d.%*d", &minor);
90 g_string_append_printf (title, " - Test version! Please report bugs to %s", PACKAGE_BUGREPORT);
92 gtk_window_set_title (GTK_WINDOW (window), title->str);
94 g_string_free (title, TRUE);
98 psppire_window_update_list_name (PsppireWindow *window)
100 PsppireWindowRegister *reg = psppire_window_register_new ();
101 GString *candidate = g_string_sized_new (80);
107 /* Compose a name. */
108 g_string_truncate (candidate, 0);
109 if (window->filename)
111 gchar *display_filename = g_filename_display_name (window->filename);
112 g_string_append (candidate, display_filename);
113 g_free (display_filename);
116 g_string_append_printf (candidate, " [%s]", window->id);
119 g_string_append_printf (candidate, "[%s]", window->id);
121 g_string_append (candidate, window->description);
124 g_string_append_printf (candidate, " #%d", n);
126 if (window->list_name && !strcmp (candidate->str, window->list_name))
128 /* Keep the existing name. */
129 g_string_free (candidate, TRUE);
133 while (psppire_window_register_lookup (reg, candidate->str));
135 if (window->list_name)
136 psppire_window_register_remove (reg, window->list_name);
138 g_free (window->list_name);
139 window->list_name = g_string_free (candidate, FALSE);
141 psppire_window_register_insert (reg, window, window->list_name);
145 psppire_window_name_changed (PsppireWindow *window)
147 psppire_window_set_title (window);
148 psppire_window_update_list_name (window);
152 psppire_window_set_property (GObject *object,
157 PsppireWindow *window = PSPPIRE_WINDOW (object);
161 case PROP_DESCRIPTION:
162 g_free (window->description);
163 window->description = g_value_dup_string (value);
164 psppire_window_set_title (window);
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)
173 psppire_window_name_changed (window);
177 window->id = g_value_dup_string (value);
178 psppire_window_name_changed (window);
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 psppire_window_get_property (GObject *object,
193 PsppireWindow *window = PSPPIRE_WINDOW (object);
198 g_value_set_string (value, window->filename);
200 case PROP_DESCRIPTION:
201 g_value_set_string (value, window->description);
204 g_value_set_string (value, window->id);
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 psppire_window_finalize (GObject *object)
216 PsppireWindow *window = PSPPIRE_WINDOW (object);
218 PsppireWindowRegister *reg = psppire_window_register_new ();
221 g_date_time_unref (window->edited);
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);
229 g_free (window->description);
230 g_free (window->list_name);
232 g_hash_table_destroy (window->menuitem_table);
234 if (G_OBJECT_CLASS (parent_class)->finalize)
235 G_OBJECT_CLASS (parent_class)->finalize (object);
239 psppire_window_class_init (PsppireWindowClass *class)
241 GObjectClass *object_class = G_OBJECT_CLASS (class);
243 object_class->finalize = psppire_window_finalize;
245 GParamSpec *description_spec =
246 null_if_empty_param ("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);
252 GParamSpec *filename_spec =
253 null_if_empty_param ("filename",
255 "The name of the file associated with this window, if any",
257 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
259 GParamSpec *id_spec =
260 null_if_empty_param ("id",
262 "The PSPP language identifier for the data associated "
263 "with this window (e.g. dataset name)",
265 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
267 object_class->set_property = psppire_window_set_property;
268 object_class->get_property = psppire_window_get_property;
270 g_object_class_install_property (object_class,
274 g_object_class_install_property (object_class,
278 g_object_class_install_property (object_class,
282 parent_class = g_type_class_peek_parent (class);
287 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
291 filename = g_filename_display_name (key);
292 item = gtk_check_menu_item_new_with_label (filename);
293 g_object_ref_sink (item);
296 g_hash_table_insert (window->menuitem_table, key, item);
300 insert_item (gpointer key, gpointer value, gpointer data)
302 PsppireWindow *window = PSPPIRE_WINDOW (data);
304 if (NULL != g_hash_table_lookup (window->menuitem_table, key))
307 insert_menuitem_into_menu (window, key);
310 /* Insert a new item into the window menu */
312 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
314 PsppireWindow *window = PSPPIRE_WINDOW (data);
316 insert_menuitem_into_menu (window, (gpointer) key);
321 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
323 PsppireWindow *window = PSPPIRE_WINDOW (data);
324 g_hash_table_remove (window->menuitem_table, key);
328 insert_existing_items (PsppireWindow *window)
330 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
335 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
337 PsppireWindowRegister *reg = psppire_window_register_new ();
339 if (w->edited != NULL)
341 gint response = psppire_window_query_save (w);
346 case GTK_RESPONSE_CANCEL:
349 case GTK_RESPONSE_APPLY:
350 psppire_window_save (w);
351 if (w->edited != NULL)
353 /* Save failed, or user exited Save As dialog with Cancel. */
357 case GTK_RESPONSE_REJECT:
362 if (1 == psppire_window_register_n_items (reg))
370 psppire_window_init (PsppireWindow *window)
372 window->filename = NULL;
373 window->basename = NULL;
375 window->description = NULL;
376 window->list_name = NULL;
377 window->edited = NULL;
379 window->menuitem_table = g_hash_table_new_full (g_str_hash, g_str_equal,
380 NULL, g_object_unref);
383 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
385 PsppireWindowRegister *reg = psppire_window_register_new ();
386 window->insert_handler = g_signal_connect (reg,
388 G_CALLBACK (insert_menuitem),
391 window->remove_handler = g_signal_connect (reg,
393 G_CALLBACK (remove_menuitem),
396 window->added_separator = FALSE;
398 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
400 g_object_set (window, "icon-name", "pspp", NULL);
404 Ask the user if the buffer should be saved.
408 psppire_window_query_save (PsppireWindow *se)
412 GtkWidget *cancel_button;
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);
421 description = g_filename_display_basename (se->filename);
423 description = g_strdup (se->id);
425 description = g_strdup (se->description);
427 gtk_message_dialog_new (GTK_WINDOW (se),
431 _("Save the changes to `%s' before closing?"),
433 g_free (description);
435 g_object_set (dialog, "icon-name", "pspp", NULL);
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));
441 gtk_dialog_add_button (GTK_DIALOG (dialog),
442 _("Close _without saving"),
443 GTK_RESPONSE_REJECT);
445 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
447 GTK_RESPONSE_CANCEL);
449 gtk_dialog_add_button (GTK_DIALOG (dialog),
453 gtk_widget_grab_focus (cancel_button);
455 response = gtk_dialog_run (GTK_DIALOG (dialog));
457 gtk_widget_destroy (dialog);
463 /* The return value is encoded in the glib filename encoding. */
465 psppire_window_get_filename (PsppireWindow *w)
471 /* FILENAME must be encoded in the glib filename encoding. */
473 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
475 g_object_set (w, "filename", filename, NULL);
479 psppire_window_set_unsaved (PsppireWindow *w)
481 if (w->edited == NULL)
482 w->edited = g_date_time_new_now_utc ();
484 psppire_window_set_title (w);
488 psppire_window_get_unsaved (PsppireWindow *w)
490 return w->edited != NULL;
498 minimise_window (gpointer key, gpointer value, gpointer data)
500 gtk_window_iconify (GTK_WINDOW (value));
505 psppire_window_minimise_all (void)
507 PsppireWindowRegister *reg = psppire_window_register_new ();
509 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
516 psppire_window_model_get_type (void)
518 static GType window_model_type = 0;
520 if (! window_model_type)
522 static const GTypeInfo window_model_info =
524 sizeof (PsppireWindowIface), /* class_size */
525 NULL, /* base_init */
526 NULL, /* base_finalize */
528 NULL, /* class_finalize */
529 NULL, /* class_data */
533 NULL /* value_table */
537 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
538 &window_model_info, 0);
540 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
543 return window_model_type;
548 psppire_window_save (PsppireWindow *w)
550 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
553 g_return_if_fail (i->save);
555 if (w->filename == NULL)
556 psppire_window_save_as (w);
561 g_date_time_unref (w->edited);
564 psppire_window_set_title (w);
569 psppire_window_save_as (PsppireWindow *w)
571 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
575 g_return_if_fail (i->pick_filename);
577 old_filename = w->filename;
580 i->pick_filename (w);
581 if (w->filename == NULL)
582 w->filename = old_filename;
585 g_free (old_filename);
586 psppire_window_save (w);
590 static void delete_recent (const char *file_name);
593 psppire_window_load (PsppireWindow *w, const gchar *file,
594 const gchar *encoding, gpointer hint)
597 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
599 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
603 g_return_val_if_fail (i->load, FALSE);
605 ok = i->load (w, file, encoding, hint);
609 psppire_window_set_filename (w, file);
611 g_date_time_unref (w->edited);
615 delete_recent (file);
622 psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
624 GtkFileFilter *filter = gtk_file_filter_new ();
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,
633 g_object_set (dialog, "local-only", FALSE, NULL);
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);
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);
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);
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);
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);
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);
674 if (toplevel->filename)
676 const gchar *filename = toplevel->filename;
679 if (! g_path_is_absolute (filename))
682 g_build_filename (g_get_current_dir (), filename, NULL);
683 dir_name = g_path_get_dirname (path);
688 dir_name = g_path_get_dirname (filename);
690 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
695 gtk_file_chooser_set_extra_widget (
696 GTK_FILE_CHOOSER (dialog),
697 psppire_encoding_selector_new ("Auto", true));
704 const struct spv_item **nodes;
708 const struct spv_item *stub[N_STUB];
712 swap_nodes (const struct spv_item **a, const struct spv_item **b)
714 const struct spv_item *tmp = *a;
720 get_path (const struct spv_item *item, struct item_path *path)
722 size_t allocated = 10;
723 path->nodes = path->stub;
728 if (path->n >= allocated)
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);
735 path->nodes[path->n++] = item;
739 for (size_t i = 0; i < path->n / 2; i++)
740 swap_nodes (&path->nodes[i], &path->nodes[path->n - i - 1]);
744 free_path (struct item_path *path)
746 if (path && path->nodes != path->stub)
751 dump_heading_transition (const struct spv_item *old,
752 const struct spv_item *new)
757 struct item_path old_path, new_path;
758 get_path (old, &old_path);
759 get_path (new, &new_path);
762 for (; common < old_path.n && common < new_path.n; common++)
763 if (old_path.nodes[common] != new_path.nodes[common])
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));
773 free_path (&old_path);
774 free_path (&new_path);
778 read_spv_file (const char *filename)
780 struct spv_reader *spv;
781 char *error = spv_open (filename, &spv);
785 fprintf (stderr, "%s\n", error);
789 struct spv_item **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++)
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)
804 cairo_surface_t *image = spv_item_get_image (items[i]);
805 image_item_submit (image_item_create (cairo_surface_reference (
808 prev_heading = heading;
810 dump_heading_transition (prev_heading, spv_get_root (spv));
815 /* Callback for the file_open action.
816 Prompts for a filename and opens it */
818 psppire_window_open (PsppireWindow *de)
820 GtkWidget *dialog = psppire_window_file_chooser_dialog (de);
822 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), relocate (examples_dir), NULL);
824 switch (gtk_dialog_run (GTK_DIALOG (dialog)))
826 case GTK_RESPONSE_ACCEPT:
829 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
831 const gchar **cs = NULL;
832 g_get_filename_charsets (&cs);
834 gchar *encoding = psppire_encoding_selector_get_encoding (
835 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
837 struct file_handle *fh = fh_create_file (NULL, name, cs[0], fh_default_properties ());
839 int retval = any_reader_detect (fh, NULL);
841 open_data_window (de, name, encoding, NULL);
842 else if (retval == 0)
844 char *error = spv_detect (name);
846 read_spv_file (name);
850 open_syntax_window (name, encoding);
863 gtk_widget_destroy (dialog);
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
871 add_most_recent (const char *file_name,
872 const char *mime_type, const char *encoding)
874 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
877 GtkRecentData recent_data;
878 gchar *full_mime_type;
880 if (encoding && encoding[0])
881 full_mime_type = g_strdup_printf ("%s; charset=%s",
882 mime_type, encoding);
884 full_mime_type = g_strdup (mime_type);
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;
894 gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
897 g_free (recent_data.app_exec);
898 g_free (full_mime_type);
907 If FILE_NAME exists in the recent list, then delete it.
910 delete_recent (const char *file_name)
912 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
915 gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);