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_class_init (PsppireWindowClass *class);
48 static void psppire_window_init (PsppireWindow *window);
50 static GObjectClass *parent_class;
52 G_DEFINE_ABSTRACT_TYPE (PsppireWindow, psppire_window, PSPPIRE_TYPE_WINDOW_BASE)
65 psppire_window_set_title (PsppireWindow *window)
67 GString *title = g_string_sized_new (80);
69 if (window->edited != NULL)
70 g_string_append_c (title, '*');
72 if (window->basename || window->id)
75 g_string_append_printf (title, "%s ", window->basename);
78 g_string_append_printf (title, "[%s] ", window->id);
80 g_string_append_unichar (title, 0x2014); /* em dash */
81 g_string_append_c (title, ' '); /* em dash */
84 g_string_append_printf (title, "PSPPIRE %s", window->description);
87 sscanf (bare_version, "%*d.%d.%*d", &minor);
89 g_string_append_printf (title, " - Test version! Please report bugs to %s", PACKAGE_BUGREPORT);
91 gtk_window_set_title (GTK_WINDOW (window), title->str);
93 g_string_free (title, TRUE);
97 psppire_window_update_list_name (PsppireWindow *window)
99 PsppireWindowRegister *reg = psppire_window_register_new ();
100 GString *candidate = g_string_sized_new (80);
106 /* Compose a name. */
107 g_string_truncate (candidate, 0);
108 if (window->filename)
110 gchar *display_filename = g_filename_display_name (window->filename);
111 g_string_append (candidate, display_filename);
112 g_free (display_filename);
115 g_string_append_printf (candidate, " [%s]", window->id);
118 g_string_append_printf (candidate, "[%s]", window->id);
120 g_string_append (candidate, window->description);
123 g_string_append_printf (candidate, " #%d", n);
125 if (window->list_name && !strcmp (candidate->str, window->list_name))
127 /* Keep the existing name. */
128 g_string_free (candidate, TRUE);
132 while (psppire_window_register_lookup (reg, candidate->str));
134 if (window->list_name)
135 psppire_window_register_remove (reg, window->list_name);
137 g_free (window->list_name);
138 window->list_name = g_string_free (candidate, FALSE);
140 psppire_window_register_insert (reg, window, window->list_name);
144 psppire_window_name_changed (PsppireWindow *window)
146 psppire_window_set_title (window);
147 psppire_window_update_list_name (window);
151 psppire_window_set_property (GObject *object,
156 PsppireWindow *window = PSPPIRE_WINDOW (object);
160 case PROP_DESCRIPTION:
161 g_free (window->description);
162 window->description = g_value_dup_string (value);
163 psppire_window_set_title (window);
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)
172 psppire_window_name_changed (window);
176 window->id = g_value_dup_string (value);
177 psppire_window_name_changed (window);
180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187 psppire_window_get_property (GObject *object,
192 PsppireWindow *window = PSPPIRE_WINDOW (object);
197 g_value_set_string (value, window->filename);
199 case PROP_DESCRIPTION:
200 g_value_set_string (value, window->description);
203 g_value_set_string (value, window->id);
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213 psppire_window_finalize (GObject *object)
215 PsppireWindow *window = PSPPIRE_WINDOW (object);
217 PsppireWindowRegister *reg = psppire_window_register_new ();
220 g_date_time_unref (window->edited);
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);
228 g_free (window->description);
229 g_free (window->list_name);
231 g_hash_table_destroy (window->menuitem_table);
233 if (G_OBJECT_CLASS (parent_class)->finalize)
234 G_OBJECT_CLASS (parent_class)->finalize (object);
238 psppire_window_class_init (PsppireWindowClass *class)
240 GObjectClass *object_class = G_OBJECT_CLASS (class);
242 object_class->finalize = psppire_window_finalize;
244 GParamSpec *description_spec =
245 null_if_empty_param ("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);
251 GParamSpec *filename_spec =
252 null_if_empty_param ("filename",
254 "The name of the file associated with this window, if any",
256 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
258 GParamSpec *id_spec =
259 null_if_empty_param ("id",
261 "The PSPP language identifier for the data associated "
262 "with this window (e.g. dataset name)",
264 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
266 object_class->set_property = psppire_window_set_property;
267 object_class->get_property = psppire_window_get_property;
269 g_object_class_install_property (object_class,
273 g_object_class_install_property (object_class,
277 g_object_class_install_property (object_class,
281 parent_class = g_type_class_peek_parent (class);
286 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
290 filename = g_filename_display_name (key);
291 item = gtk_check_menu_item_new_with_label (filename);
292 g_object_ref_sink (item);
295 g_hash_table_insert (window->menuitem_table, key, item);
299 insert_item (gpointer key, gpointer value, gpointer data)
301 PsppireWindow *window = PSPPIRE_WINDOW (data);
303 if (NULL != g_hash_table_lookup (window->menuitem_table, key))
306 insert_menuitem_into_menu (window, key);
309 /* Insert a new item into the window menu */
311 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
313 PsppireWindow *window = PSPPIRE_WINDOW (data);
315 insert_menuitem_into_menu (window, (gpointer) key);
320 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
322 PsppireWindow *window = PSPPIRE_WINDOW (data);
323 g_hash_table_remove (window->menuitem_table, key);
327 insert_existing_items (PsppireWindow *window)
329 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
334 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
336 PsppireWindowRegister *reg = psppire_window_register_new ();
338 if (w->edited != NULL)
340 gint response = psppire_window_query_save (w);
345 case GTK_RESPONSE_CANCEL:
348 case GTK_RESPONSE_APPLY:
349 psppire_window_save (w);
350 if (w->edited != NULL)
352 /* Save failed, or user exited Save As dialog with Cancel. */
356 case GTK_RESPONSE_REJECT:
361 if (1 == psppire_window_register_n_items (reg))
369 psppire_window_init (PsppireWindow *window)
371 window->filename = NULL;
372 window->basename = NULL;
374 window->description = NULL;
375 window->list_name = NULL;
376 window->edited = NULL;
378 window->menuitem_table = g_hash_table_new_full (g_str_hash, g_str_equal,
379 NULL, g_object_unref);
382 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
384 PsppireWindowRegister *reg = psppire_window_register_new ();
385 window->insert_handler = g_signal_connect (reg,
387 G_CALLBACK (insert_menuitem),
390 window->remove_handler = g_signal_connect (reg,
392 G_CALLBACK (remove_menuitem),
395 window->added_separator = FALSE;
397 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
399 g_object_set (window, "icon-name", "pspp", NULL);
403 Ask the user if the buffer should be saved.
407 psppire_window_query_save (PsppireWindow *se)
411 GtkWidget *cancel_button;
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);
420 description = g_filename_display_basename (se->filename);
422 description = g_strdup (se->id);
424 description = g_strdup (se->description);
426 gtk_message_dialog_new (GTK_WINDOW (se),
430 _("Save the changes to `%s' before closing?"),
432 g_free (description);
434 g_object_set (dialog, "icon-name", "pspp", NULL);
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 timespan / G_TIME_SPAN_SECOND);
440 gtk_dialog_add_button (GTK_DIALOG (dialog),
441 _("Close _without saving"),
442 GTK_RESPONSE_REJECT);
444 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
446 GTK_RESPONSE_CANCEL);
448 gtk_dialog_add_button (GTK_DIALOG (dialog),
452 gtk_widget_grab_focus (cancel_button);
454 response = gtk_dialog_run (GTK_DIALOG (dialog));
456 gtk_widget_destroy (dialog);
462 /* The return value is encoded in the glib filename encoding. */
464 psppire_window_get_filename (PsppireWindow *w)
470 /* FILENAME must be encoded in the glib filename encoding. */
472 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
474 g_object_set (w, "filename", filename, NULL);
478 psppire_window_set_unsaved (PsppireWindow *w)
480 if (w->edited == NULL)
481 w->edited = g_date_time_new_now_utc ();
483 psppire_window_set_title (w);
487 psppire_window_get_unsaved (PsppireWindow *w)
489 return w->edited != NULL;
497 minimise_window (gpointer key, gpointer value, gpointer data)
499 gtk_window_iconify (GTK_WINDOW (value));
504 psppire_window_minimise_all (void)
506 PsppireWindowRegister *reg = psppire_window_register_new ();
508 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
515 psppire_window_model_get_type (void)
517 static GType window_model_type = 0;
519 if (! window_model_type)
521 static const GTypeInfo window_model_info =
523 sizeof (PsppireWindowIface), /* class_size */
524 NULL, /* base_init */
525 NULL, /* base_finalize */
527 NULL, /* class_finalize */
528 NULL, /* class_data */
532 NULL /* value_table */
536 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
537 &window_model_info, 0);
539 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
542 return window_model_type;
547 psppire_window_save (PsppireWindow *w)
549 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
552 g_return_if_fail (i->save);
554 if (w->filename == NULL)
555 psppire_window_save_as (w);
560 g_date_time_unref (w->edited);
563 psppire_window_set_title (w);
568 psppire_window_save_as (PsppireWindow *w)
570 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
574 g_return_if_fail (i->pick_filename);
576 old_filename = w->filename;
579 i->pick_filename (w);
580 if (w->filename == NULL)
581 w->filename = old_filename;
584 g_free (old_filename);
585 psppire_window_save (w);
589 static void delete_recent (const char *file_name);
592 psppire_window_load (PsppireWindow *w, const gchar *file,
593 const gchar *encoding, gpointer hint)
596 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
598 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
602 g_return_val_if_fail (i->load, FALSE);
604 ok = i->load (w, file, encoding, hint);
608 psppire_window_set_filename (w, file);
610 g_date_time_unref (w->edited);
614 delete_recent (file);
621 psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
623 GtkFileFilter *filter = gtk_file_filter_new ();
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,
632 g_object_set (dialog, "local-only", FALSE, NULL);
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);
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);
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);
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);
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);
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);
673 if (toplevel->filename)
675 const gchar *filename = toplevel->filename;
678 if (! g_path_is_absolute (filename))
681 g_build_filename (g_get_current_dir (), filename, NULL);
682 dir_name = g_path_get_dirname (path);
687 dir_name = g_path_get_dirname (filename);
689 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
694 gtk_file_chooser_set_extra_widget (
695 GTK_FILE_CHOOSER (dialog),
696 psppire_encoding_selector_new ("Auto", true));
703 const struct spv_item **nodes;
707 const struct spv_item *stub[N_STUB];
711 swap_nodes (const struct spv_item **a, const struct spv_item **b)
713 const struct spv_item *tmp = *a;
719 get_path (const struct spv_item *item, struct item_path *path)
721 size_t allocated = 10;
722 path->nodes = path->stub;
727 if (path->n >= allocated)
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);
734 path->nodes[path->n++] = item;
738 for (size_t i = 0; i < path->n / 2; i++)
739 swap_nodes (&path->nodes[i], &path->nodes[path->n - i - 1]);
743 free_path (struct item_path *path)
745 if (path && path->nodes != path->stub)
750 dump_heading_transition (const struct spv_item *old,
751 const struct spv_item *new)
756 struct item_path old_path, new_path;
757 get_path (old, &old_path);
758 get_path (new, &new_path);
761 for (; common < old_path.n && common < new_path.n; common++)
762 if (old_path.nodes[common] != new_path.nodes[common])
765 for (size_t i = common; i < old_path.n; i++)
766 group_close_item_submit (group_close_item_create ());
767 for (size_t i = common; i < new_path.n; i++)
768 group_open_item_submit (group_open_item_create (
769 new_path.nodes[i]->command_id));
771 free_path (&old_path);
772 free_path (&new_path);
776 read_spv_file (const char *filename)
778 struct spv_reader *spv;
779 char *error = spv_open (filename, &spv);
783 fprintf (stderr, "%s\n", error);
787 struct spv_item **items;
789 spv_select (spv, NULL, 0, &items, &n_items);
790 struct spv_item *prev_heading = spv_get_root (spv);
791 for (size_t i = 0; i < n_items; i++)
793 struct spv_item *heading
794 = items[i]->type == SPV_ITEM_HEADING ? items[i] : items[i]->parent;
795 dump_heading_transition (prev_heading, heading);
796 if (items[i]->type == SPV_ITEM_TEXT)
797 spv_text_submit (items[i]);
798 else if (items[i]->type == SPV_ITEM_TABLE)
799 pivot_table_submit (spv_item_get_table (items[i]));
800 prev_heading = heading;
802 dump_heading_transition (prev_heading, spv_get_root (spv));
807 /* Callback for the file_open action.
808 Prompts for a filename and opens it */
810 psppire_window_open (PsppireWindow *de)
812 GtkWidget *dialog = psppire_window_file_chooser_dialog (de);
814 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), relocate (examples_dir), NULL);
816 switch (gtk_dialog_run (GTK_DIALOG (dialog)))
818 case GTK_RESPONSE_ACCEPT:
821 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
823 const gchar **cs = NULL;
824 g_get_filename_charsets (&cs);
826 gchar *encoding = psppire_encoding_selector_get_encoding (
827 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
829 struct file_handle *fh = fh_create_file (NULL, name, cs[0], fh_default_properties ());
831 int retval = any_reader_detect (fh, NULL);
833 open_data_window (de, name, encoding, NULL);
834 else if (retval == 0)
836 char *error = spv_detect (name);
838 read_spv_file (name);
842 open_syntax_window (name, encoding);
855 gtk_widget_destroy (dialog);
859 /* Puts FILE_NAME (encoded in the glib file name encoding) into the recent list
860 with associated MIME_TYPE. If it's already in the list, it moves it to the
863 add_most_recent (const char *file_name,
864 const char *mime_type, const char *encoding)
866 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
869 GtkRecentData recent_data;
870 gchar *full_mime_type;
872 if (encoding && encoding[0])
873 full_mime_type = g_strdup_printf ("%s; charset=%s",
874 mime_type, encoding);
876 full_mime_type = g_strdup (mime_type);
878 recent_data.display_name = NULL;
879 recent_data.description = NULL;
880 recent_data.mime_type = full_mime_type;
881 recent_data.app_name = CONST_CAST (gchar *, g_get_application_name ());
882 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
883 recent_data.groups = NULL;
884 recent_data.is_private = FALSE;
886 gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
889 g_free (recent_data.app_exec);
890 g_free (full_mime_type);
899 If FILE_NAME exists in the recent list, then delete it.
902 delete_recent (const char *file_name)
904 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
907 gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);