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/pivot-table.h"
37 #include "output/spv/spv.h"
38 #include "output/spv/spv-output.h"
39 #include "output/spv/spv-select.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"
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);
52 static GObjectClass *parent_class;
55 psppire_window_get_type (void)
57 static GType psppire_window_type = 0;
59 if (!psppire_window_type)
61 static const GTypeInfo psppire_window_info =
63 sizeof (PsppireWindowClass),
64 (GBaseInitFunc) psppire_window_base_init,
65 (GBaseFinalizeFunc) NULL,
66 (GClassInitFunc) psppire_window_class_init,
67 (GClassFinalizeFunc) NULL,
69 sizeof (PsppireWindow),
71 (GInstanceInitFunc) psppire_window_init,
75 g_type_register_static (PSPPIRE_TYPE_WINDOW_BASE, "PsppireWindow",
76 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
79 return psppire_window_type;
94 psppire_window_set_title (PsppireWindow *window)
96 GString *title = g_string_sized_new (80);
98 if (window->edited != NULL)
99 g_string_append_c (title, '*');
101 if (window->basename || window->id)
103 if (window->basename)
104 g_string_append_printf (title, "%s ", window->basename);
107 g_string_append_printf (title, "[%s] ", window->id);
109 g_string_append_unichar (title, 0x2014); /* em dash */
110 g_string_append_c (title, ' '); /* em dash */
113 g_string_append_printf (title, "PSPPIRE %s", window->description);
116 sscanf (bare_version, "%*d.%d.%*d", &minor);
118 g_string_append_printf (title, " - Test version! Please report bugs to %s", PACKAGE_BUGREPORT);
120 gtk_window_set_title (GTK_WINDOW (window), title->str);
122 g_string_free (title, TRUE);
126 psppire_window_update_list_name (PsppireWindow *window)
128 PsppireWindowRegister *reg = psppire_window_register_new ();
129 GString *candidate = g_string_sized_new (80);
135 /* Compose a name. */
136 g_string_truncate (candidate, 0);
137 if (window->filename)
139 gchar *display_filename = g_filename_display_name (window->filename);
140 g_string_append (candidate, display_filename);
141 g_free (display_filename);
144 g_string_append_printf (candidate, " [%s]", window->id);
147 g_string_append_printf (candidate, "[%s]", window->id);
149 g_string_append (candidate, window->description);
152 g_string_append_printf (candidate, " #%d", n);
154 if (window->list_name && !strcmp (candidate->str, window->list_name))
156 /* Keep the existing name. */
157 g_string_free (candidate, TRUE);
161 while (psppire_window_register_lookup (reg, candidate->str));
163 if (window->list_name)
164 psppire_window_register_remove (reg, window->list_name);
166 g_free (window->list_name);
167 window->list_name = g_string_free (candidate, FALSE);
169 psppire_window_register_insert (reg, window, window->list_name);
173 psppire_window_name_changed (PsppireWindow *window)
175 psppire_window_set_title (window);
176 psppire_window_update_list_name (window);
180 psppire_window_set_property (GObject *object,
185 PsppireWindow *window = PSPPIRE_WINDOW (object);
189 case PROP_DESCRIPTION:
190 g_free (window->description);
191 window->description = g_value_dup_string (value);
192 psppire_window_set_title (window);
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)
201 psppire_window_name_changed (window);
205 window->id = g_value_dup_string (value);
206 psppire_window_name_changed (window);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 psppire_window_get_property (GObject *object,
221 PsppireWindow *window = PSPPIRE_WINDOW (object);
226 g_value_set_string (value, window->filename);
228 case PROP_DESCRIPTION:
229 g_value_set_string (value, window->description);
232 g_value_set_string (value, window->id);
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 psppire_window_finalize (GObject *object)
244 PsppireWindow *window = PSPPIRE_WINDOW (object);
246 PsppireWindowRegister *reg = psppire_window_register_new ();
249 g_date_time_unref (window->edited);
251 g_signal_handler_disconnect (reg, window->remove_handler);
252 g_signal_handler_disconnect (reg, window->insert_handler);
253 psppire_window_register_remove (reg, window->list_name);
254 g_free (window->filename);
255 g_free (window->basename);
257 g_free (window->description);
258 g_free (window->list_name);
260 g_hash_table_destroy (window->menuitem_table);
262 if (G_OBJECT_CLASS (parent_class)->finalize)
263 G_OBJECT_CLASS (parent_class)->finalize (object);
267 psppire_window_class_init (PsppireWindowClass *class)
269 GObjectClass *object_class = G_OBJECT_CLASS (class);
271 GParamSpec *description_spec =
272 null_if_empty_param ("description",
274 "A string describing the usage of the window",
275 NULL, /*Should be overridden by derived classes */
276 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
278 GParamSpec *filename_spec =
279 null_if_empty_param ("filename",
281 "The name of the file associated with this window, if any",
283 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
285 GParamSpec *id_spec =
286 null_if_empty_param ("id",
288 "The PSPP language identifier for the data associated "
289 "with this window (e.g. dataset name)",
291 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
293 object_class->set_property = psppire_window_set_property;
294 object_class->get_property = psppire_window_get_property;
296 g_object_class_install_property (object_class,
300 g_object_class_install_property (object_class,
304 g_object_class_install_property (object_class,
308 parent_class = g_type_class_peek_parent (class);
313 psppire_window_base_init (PsppireWindowClass *class)
315 GObjectClass *object_class = G_OBJECT_CLASS (class);
317 object_class->finalize = psppire_window_finalize;
323 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
327 filename = g_filename_display_name (key);
328 item = gtk_check_menu_item_new_with_label (filename);
331 g_hash_table_insert (window->menuitem_table, key, item);
335 insert_item (gpointer key, gpointer value, gpointer data)
337 PsppireWindow *window = PSPPIRE_WINDOW (data);
339 if (NULL != g_hash_table_lookup (window->menuitem_table, key))
342 insert_menuitem_into_menu (window, key);
345 /* Insert a new item into the window menu */
347 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
349 PsppireWindow *window = PSPPIRE_WINDOW (data);
351 insert_menuitem_into_menu (window, (gpointer) key);
356 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
358 PsppireWindow *window = PSPPIRE_WINDOW (data);
359 g_hash_table_remove (window->menuitem_table, key);
363 insert_existing_items (PsppireWindow *window)
365 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
370 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
372 PsppireWindowRegister *reg = psppire_window_register_new ();
374 if (w->edited != NULL)
376 gint response = psppire_window_query_save (w);
381 case GTK_RESPONSE_CANCEL:
384 case GTK_RESPONSE_APPLY:
385 psppire_window_save (w);
386 if (w->edited != NULL)
388 /* Save failed, or user exited Save As dialog with Cancel. */
392 case GTK_RESPONSE_REJECT:
397 if (1 == psppire_window_register_n_items (reg))
405 psppire_window_init (PsppireWindow *window)
407 window->filename = NULL;
408 window->basename = NULL;
410 window->description = NULL;
411 window->list_name = NULL;
412 window->edited = NULL;
414 window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
417 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
419 window->insert_handler = g_signal_connect (psppire_window_register_new (),
421 G_CALLBACK (insert_menuitem),
424 window->remove_handler = g_signal_connect (psppire_window_register_new (),
426 G_CALLBACK (remove_menuitem),
429 window->added_separator = FALSE;
431 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
433 g_object_set (window, "icon-name", "pspp", NULL);
437 Ask the user if the buffer should be saved.
441 psppire_window_query_save (PsppireWindow *se)
445 GtkWidget *cancel_button;
449 GDateTime *now = g_date_time_new_now_utc ();
450 GTimeSpan timespan = g_date_time_difference (now, se->edited);
451 g_date_time_unref (now);
454 description = g_filename_display_basename (se->filename);
456 description = g_strdup (se->id);
458 description = g_strdup (se->description);
460 gtk_message_dialog_new (GTK_WINDOW (se),
464 _("Save the changes to `%s' before closing?"),
466 g_free (description);
468 g_object_set (dialog, "icon-name", "pspp", NULL);
470 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
471 _("If you don't save, changes from the last %ld seconds will be permanently lost."),
472 timespan / G_TIME_SPAN_SECOND);
474 gtk_dialog_add_button (GTK_DIALOG (dialog),
475 _("Close _without saving"),
476 GTK_RESPONSE_REJECT);
478 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
480 GTK_RESPONSE_CANCEL);
482 gtk_dialog_add_button (GTK_DIALOG (dialog),
486 gtk_widget_grab_focus (cancel_button);
488 response = gtk_dialog_run (GTK_DIALOG (dialog));
490 gtk_widget_destroy (dialog);
496 /* The return value is encoded in the glib filename encoding. */
498 psppire_window_get_filename (PsppireWindow *w)
504 /* FILENAME must be encoded in the glib filename encoding. */
506 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
508 g_object_set (w, "filename", filename, NULL);
512 psppire_window_set_unsaved (PsppireWindow *w)
514 if (w->edited == NULL)
515 w->edited = g_date_time_new_now_utc ();
517 psppire_window_set_title (w);
521 psppire_window_get_unsaved (PsppireWindow *w)
523 return w->edited != NULL;
531 minimise_window (gpointer key, gpointer value, gpointer data)
533 gtk_window_iconify (GTK_WINDOW (value));
538 psppire_window_minimise_all (void)
540 PsppireWindowRegister *reg = psppire_window_register_new ();
542 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
549 psppire_window_model_get_type (void)
551 static GType window_model_type = 0;
553 if (! window_model_type)
555 static const GTypeInfo window_model_info =
557 sizeof (PsppireWindowIface), /* class_size */
558 NULL, /* base_init */
559 NULL, /* base_finalize */
561 NULL, /* class_finalize */
562 NULL, /* class_data */
569 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
570 &window_model_info, 0);
572 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
575 return window_model_type;
580 psppire_window_save (PsppireWindow *w)
582 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
585 g_return_if_fail (i->save);
587 if (w->filename == NULL)
588 psppire_window_save_as (w);
593 g_date_time_unref (w->edited);
596 psppire_window_set_title (w);
601 psppire_window_save_as (PsppireWindow *w)
603 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
607 g_return_if_fail (i->pick_filename);
609 old_filename = w->filename;
612 i->pick_filename (w);
613 if (w->filename == NULL)
614 w->filename = old_filename;
617 g_free (old_filename);
618 psppire_window_save (w);
622 static void delete_recent (const char *file_name);
625 psppire_window_load (PsppireWindow *w, const gchar *file,
626 const gchar *encoding, gpointer hint)
629 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
631 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
635 g_return_val_if_fail (i->load, FALSE);
637 ok = i->load (w, file, encoding, hint);
641 psppire_window_set_filename (w, file);
643 g_date_time_unref (w->edited);
647 delete_recent (file);
654 psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
656 GtkFileFilter *filter = gtk_file_filter_new ();
658 gtk_file_chooser_dialog_new (_("Open"),
659 GTK_WINDOW (toplevel),
660 GTK_FILE_CHOOSER_ACTION_OPEN,
661 _("Cancel"), GTK_RESPONSE_CANCEL,
662 _("Open"), GTK_RESPONSE_ACCEPT,
665 g_object_set (dialog, "local-only", FALSE, NULL);
667 gtk_file_filter_set_name (filter, _("Data and Syntax Files"));
668 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
669 gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
670 gtk_file_filter_add_mime_type (filter, "application/x-spss-spv");
671 gtk_file_filter_add_pattern (filter, "*.zsav");
672 gtk_file_filter_add_pattern (filter, "*.sps");
673 gtk_file_filter_add_pattern (filter, "*.SPS");
674 gtk_file_filter_add_pattern (filter, "*.spv");
675 gtk_file_filter_add_pattern (filter, "*.SPV");
676 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
678 filter = gtk_file_filter_new ();
679 gtk_file_filter_set_name (filter, _("System Files (*.sav, *.zsav)"));
680 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
681 gtk_file_filter_add_pattern (filter, "*.zsav");
682 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
684 filter = gtk_file_filter_new ();
685 gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
686 gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
687 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
689 filter = gtk_file_filter_new ();
690 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
691 gtk_file_filter_add_pattern (filter, "*.sps");
692 gtk_file_filter_add_pattern (filter, "*.SPS");
693 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
695 filter = gtk_file_filter_new ();
696 gtk_file_filter_set_name (filter, _("Output Files (*.spv) "));
697 gtk_file_filter_add_pattern (filter, "*.spv");
698 gtk_file_filter_add_pattern (filter, "*.SPV");
699 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
701 filter = gtk_file_filter_new ();
702 gtk_file_filter_set_name (filter, _("All Files"));
703 gtk_file_filter_add_pattern (filter, "*");
704 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
706 if (toplevel->filename)
708 const gchar *filename = toplevel->filename;
711 if (! g_path_is_absolute (filename))
714 g_build_filename (g_get_current_dir (), filename, NULL);
715 dir_name = g_path_get_dirname (path);
720 dir_name = g_path_get_dirname (filename);
722 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
727 gtk_file_chooser_set_extra_widget (
728 GTK_FILE_CHOOSER (dialog),
729 psppire_encoding_selector_new ("Auto", true));
736 const struct spv_item **nodes;
740 const struct spv_item *stub[N_STUB];
744 swap_nodes (const struct spv_item **a, const struct spv_item **b)
746 const struct spv_item *tmp = *a;
752 get_path (const struct spv_item *item, struct item_path *path)
754 size_t allocated = 10;
755 path->nodes = path->stub;
760 if (path->n >= allocated)
762 if (path->nodes == path->stub)
763 path->nodes = xmemdup (path->stub, sizeof path->stub);
764 path->nodes = x2nrealloc (path->nodes, &allocated,
765 sizeof *path->nodes);
767 path->nodes[path->n++] = item;
771 for (size_t i = 0; i < path->n / 2; i++)
772 swap_nodes (&path->nodes[i], &path->nodes[path->n - i - 1]);
776 free_path (struct item_path *path)
778 if (path && path->nodes != path->stub)
783 dump_heading_transition (const struct spv_item *old,
784 const struct spv_item *new)
789 struct item_path old_path, new_path;
790 get_path (old, &old_path);
791 get_path (new, &new_path);
794 for (; common < old_path.n && common < new_path.n; common++)
795 if (old_path.nodes[common] != new_path.nodes[common])
798 for (size_t i = common; i < old_path.n; i++)
799 group_close_item_submit (group_close_item_create ());
800 for (size_t i = common; i < new_path.n; i++)
801 group_open_item_submit (group_open_item_create (
802 new_path.nodes[i]->command_id));
804 free_path (&old_path);
805 free_path (&new_path);
809 read_spv_file (const char *filename)
811 struct spv_reader *spv;
812 char *error = spv_open (filename, &spv);
816 fprintf (stderr, "%s\n", error);
820 struct spv_item **items;
822 spv_select (spv, NULL, 0, &items, &n_items);
823 struct spv_item *prev_heading = spv_get_root (spv);
824 for (size_t i = 0; i < n_items; i++)
826 struct spv_item *heading
827 = items[i]->type == SPV_ITEM_HEADING ? items[i] : items[i]->parent;
828 dump_heading_transition (prev_heading, heading);
829 if (items[i]->type == SPV_ITEM_TEXT)
830 spv_text_submit (items[i]);
831 else if (items[i]->type == SPV_ITEM_TABLE)
832 pivot_table_submit (spv_item_get_table (items[i]));
833 prev_heading = heading;
835 dump_heading_transition (prev_heading, spv_get_root (spv));
840 /* Callback for the file_open action.
841 Prompts for a filename and opens it */
843 psppire_window_open (PsppireWindow *de)
845 GtkWidget *dialog = psppire_window_file_chooser_dialog (de);
847 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), relocate (examples_dir), NULL);
849 switch (gtk_dialog_run (GTK_DIALOG (dialog)))
851 case GTK_RESPONSE_ACCEPT:
854 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
856 const gchar **cs = NULL;
857 g_get_filename_charsets (&cs);
859 gchar *encoding = psppire_encoding_selector_get_encoding (
860 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
862 struct file_handle *fh = fh_create_file (NULL, name, cs[0], fh_default_properties ());
864 int retval = any_reader_detect (fh, NULL);
866 open_data_window (de, name, encoding, NULL);
867 else if (retval == 0)
869 char *error = spv_detect (name);
871 read_spv_file (name);
875 open_syntax_window (name, encoding);
888 gtk_widget_destroy (dialog);
892 /* Puts FILE_NAME (encoded in the glib file name encoding) into the recent list
893 with associated MIME_TYPE. If it's already in the list, it moves it to the
896 add_most_recent (const char *file_name,
897 const char *mime_type, const char *encoding)
899 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
902 GtkRecentData recent_data;
903 gchar *full_mime_type;
905 if (encoding && encoding[0])
906 full_mime_type = g_strdup_printf ("%s; charset=%s",
907 mime_type, encoding);
909 full_mime_type = g_strdup (mime_type);
911 recent_data.display_name = NULL;
912 recent_data.description = NULL;
913 recent_data.mime_type = full_mime_type;
914 recent_data.app_name = CONST_CAST (gchar *, g_get_application_name ());
915 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
916 recent_data.groups = NULL;
917 recent_data.is_private = FALSE;
919 gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
922 g_free (recent_data.app_exec);
923 g_free (full_mime_type);
932 If FILE_NAME exists in the recent list, then delete it.
935 delete_recent (const char *file_name)
937 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
940 gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);