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/output-item.h"
36 #include "output/pivot-table.h"
37 #include "output/spv/spv.h"
40 #include "psppire-data-window.h"
41 #include "psppire-encoding-selector.h"
42 #include "psppire-syntax-window.h"
43 #include "psppire-window-register.h"
45 static void psppire_window_class_init (PsppireWindowClass *class);
46 static void psppire_window_init (PsppireWindow *window);
48 static GObjectClass *parent_class;
50 G_DEFINE_ABSTRACT_TYPE (PsppireWindow, psppire_window, PSPPIRE_TYPE_WINDOW_BASE)
63 psppire_window_set_title (PsppireWindow *window)
65 GString *title = g_string_sized_new (80);
67 if (window->edited != NULL)
68 g_string_append_c (title, '*');
70 if (window->basename || window->id)
73 g_string_append_printf (title, "%s ", window->basename);
76 g_string_append_printf (title, "[%s] ", window->id);
78 g_string_append_unichar (title, 0x2014); /* em dash */
79 g_string_append_c (title, ' '); /* em dash */
82 g_string_append_printf (title, "PSPPIRE %s", window->description);
85 sscanf (bare_version, "%*d.%d.%*d", &minor);
87 g_string_append_printf (title, " - Test version! Please report bugs to %s", PACKAGE_BUGREPORT);
89 gtk_window_set_title (GTK_WINDOW (window), title->str);
91 g_string_free (title, TRUE);
95 psppire_window_update_list_name (PsppireWindow *window)
97 PsppireWindowRegister *reg = psppire_window_register_new ();
98 GString *candidate = g_string_sized_new (80);
104 /* Compose a name. */
105 g_string_truncate (candidate, 0);
106 if (window->filename)
108 gchar *display_filename = g_filename_display_name (window->filename);
109 g_string_append (candidate, display_filename);
110 g_free (display_filename);
113 g_string_append_printf (candidate, " [%s]", window->id);
116 g_string_append_printf (candidate, "[%s]", window->id);
118 g_string_append (candidate, window->description);
121 g_string_append_printf (candidate, " #%d", n);
123 if (window->list_name && !strcmp (candidate->str, window->list_name))
125 /* Keep the existing name. */
126 g_string_free (candidate, TRUE);
130 while (psppire_window_register_lookup (reg, candidate->str));
132 if (window->list_name)
133 psppire_window_register_remove (reg, window->list_name);
135 g_free (window->list_name);
136 window->list_name = g_string_free (candidate, FALSE);
138 psppire_window_register_insert (reg, window, window->list_name);
142 psppire_window_name_changed (PsppireWindow *window)
144 psppire_window_set_title (window);
145 psppire_window_update_list_name (window);
149 psppire_window_set_property (GObject *object,
154 PsppireWindow *window = PSPPIRE_WINDOW (object);
158 case PROP_DESCRIPTION:
159 g_free (window->description);
160 window->description = g_value_dup_string (value);
161 psppire_window_set_title (window);
164 g_free (window->filename);
165 window->filename = g_value_dup_string (value);
166 g_free (window->basename);
167 window->basename = (window->filename
168 ? g_filename_display_basename (window->filename)
170 psppire_window_name_changed (window);
174 window->id = g_value_dup_string (value);
175 psppire_window_name_changed (window);
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185 psppire_window_get_property (GObject *object,
190 PsppireWindow *window = PSPPIRE_WINDOW (object);
195 g_value_set_string (value, window->filename);
197 case PROP_DESCRIPTION:
198 g_value_set_string (value, window->description);
201 g_value_set_string (value, window->id);
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
211 psppire_window_finalize (GObject *object)
213 PsppireWindow *window = PSPPIRE_WINDOW (object);
215 PsppireWindowRegister *reg = psppire_window_register_new ();
218 g_date_time_unref (window->edited);
220 g_signal_handler_disconnect (reg, window->remove_handler);
221 g_signal_handler_disconnect (reg, window->insert_handler);
222 psppire_window_register_remove (reg, window->list_name);
223 g_free (window->filename);
224 g_free (window->basename);
226 g_free (window->description);
227 g_free (window->list_name);
229 g_hash_table_destroy (window->menuitem_table);
231 if (G_OBJECT_CLASS (parent_class)->finalize)
232 G_OBJECT_CLASS (parent_class)->finalize (object);
236 psppire_window_class_init (PsppireWindowClass *class)
238 GObjectClass *object_class = G_OBJECT_CLASS (class);
240 object_class->finalize = psppire_window_finalize;
242 GParamSpec *description_spec =
243 null_if_empty_param ("description",
245 "A string describing the usage of the window",
246 NULL, /*Should be overridden by derived classes */
247 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
249 GParamSpec *filename_spec =
250 null_if_empty_param ("filename",
252 "The name of the file associated with this window, if any",
254 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
256 GParamSpec *id_spec =
257 null_if_empty_param ("id",
259 "The PSPP language identifier for the data associated "
260 "with this window (e.g. dataset name)",
262 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
264 object_class->set_property = psppire_window_set_property;
265 object_class->get_property = psppire_window_get_property;
267 g_object_class_install_property (object_class,
271 g_object_class_install_property (object_class,
275 g_object_class_install_property (object_class,
279 parent_class = g_type_class_peek_parent (class);
284 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
288 filename = g_filename_display_name (key);
289 item = gtk_check_menu_item_new_with_label (filename);
290 g_object_ref_sink (item);
293 g_hash_table_insert (window->menuitem_table, key, item);
297 insert_item (gpointer key, gpointer value, gpointer data)
299 PsppireWindow *window = PSPPIRE_WINDOW (data);
301 if (NULL != g_hash_table_lookup (window->menuitem_table, key))
304 insert_menuitem_into_menu (window, key);
307 /* Insert a new item into the window menu */
309 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
311 PsppireWindow *window = PSPPIRE_WINDOW (data);
313 insert_menuitem_into_menu (window, (gpointer) key);
318 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
320 PsppireWindow *window = PSPPIRE_WINDOW (data);
321 g_hash_table_remove (window->menuitem_table, key);
325 insert_existing_items (PsppireWindow *window)
327 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
332 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
334 PsppireWindowRegister *reg = psppire_window_register_new ();
336 if (w->edited != NULL)
338 gint response = psppire_window_query_save (w);
343 case GTK_RESPONSE_CANCEL:
346 case GTK_RESPONSE_APPLY:
347 psppire_window_save (w);
348 if (w->edited != NULL)
350 /* Save failed, or user exited Save As dialog with Cancel. */
354 case GTK_RESPONSE_REJECT:
359 if (1 == psppire_window_register_n_items (reg))
367 psppire_window_init (PsppireWindow *window)
369 window->filename = NULL;
370 window->basename = NULL;
372 window->description = NULL;
373 window->list_name = NULL;
374 window->edited = NULL;
376 window->menuitem_table = g_hash_table_new_full (g_str_hash, g_str_equal,
377 NULL, g_object_unref);
380 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
382 PsppireWindowRegister *reg = psppire_window_register_new ();
383 window->insert_handler = g_signal_connect (reg,
385 G_CALLBACK (insert_menuitem),
388 window->remove_handler = g_signal_connect (reg,
390 G_CALLBACK (remove_menuitem),
393 window->added_separator = FALSE;
395 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
397 g_object_set (window, "icon-name", "org.gnu.pspp", NULL);
401 Ask the user if the buffer should be saved.
405 psppire_window_query_save (PsppireWindow *se)
409 GtkWidget *cancel_button;
413 GDateTime *now = g_date_time_new_now_utc ();
414 GTimeSpan timespan = g_date_time_difference (now, se->edited);
415 g_date_time_unref (now);
418 description = g_filename_display_basename (se->filename);
420 description = g_strdup (se->id);
422 description = g_strdup (se->description);
424 gtk_message_dialog_new (GTK_WINDOW (se),
428 _("Save the changes to `%s' before closing?"),
430 g_free (description);
432 g_object_set (dialog, "icon-name", "org.gnu.pspp", NULL);
434 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
435 _("If you don't save, changes from the last %ld seconds will be permanently lost."),
436 (long int) (timespan / G_TIME_SPAN_SECOND));
438 gtk_dialog_add_button (GTK_DIALOG (dialog),
439 _("Close _without saving"),
440 GTK_RESPONSE_REJECT);
442 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
444 GTK_RESPONSE_CANCEL);
446 gtk_dialog_add_button (GTK_DIALOG (dialog),
450 gtk_widget_grab_focus (cancel_button);
452 response = gtk_dialog_run (GTK_DIALOG (dialog));
454 gtk_widget_destroy (dialog);
460 /* The return value is encoded in the glib filename encoding. */
462 psppire_window_get_filename (PsppireWindow *w)
468 /* FILENAME must be encoded in the glib filename encoding. */
470 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
472 g_object_set (w, "filename", filename, NULL);
476 psppire_window_set_unsaved (PsppireWindow *w)
478 if (w->edited == NULL)
479 w->edited = g_date_time_new_now_utc ();
481 psppire_window_set_title (w);
485 psppire_window_get_unsaved (PsppireWindow *w)
487 return w->edited != NULL;
495 minimise_window (gpointer key, gpointer value, gpointer data)
497 gtk_window_iconify (GTK_WINDOW (value));
502 psppire_window_minimise_all (void)
504 PsppireWindowRegister *reg = psppire_window_register_new ();
506 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
513 psppire_window_model_get_type (void)
515 static GType window_model_type = 0;
517 if (! window_model_type)
519 static const GTypeInfo window_model_info =
521 sizeof (PsppireWindowIface), /* class_size */
522 NULL, /* base_init */
523 NULL, /* base_finalize */
525 NULL, /* class_finalize */
526 NULL, /* class_data */
530 NULL /* value_table */
534 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
535 &window_model_info, 0);
537 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
540 return window_model_type;
545 psppire_window_save (PsppireWindow *w)
547 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
550 g_return_if_fail (i->save);
552 if (w->filename == NULL)
553 psppire_window_save_as (w);
558 g_date_time_unref (w->edited);
561 psppire_window_set_title (w);
566 psppire_window_save_as (PsppireWindow *w)
568 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
572 g_return_if_fail (i->pick_filename);
574 old_filename = w->filename;
577 i->pick_filename (w);
578 if (w->filename == NULL)
579 w->filename = old_filename;
582 g_free (old_filename);
583 psppire_window_save (w);
587 static void delete_recent (const char *file_name);
590 psppire_window_load (PsppireWindow *w, const gchar *file,
591 const gchar *encoding, gpointer hint)
594 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
596 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
600 g_return_val_if_fail (i->load, FALSE);
602 ok = i->load (w, file, encoding, hint);
606 psppire_window_set_filename (w, file);
608 g_date_time_unref (w->edited);
612 delete_recent (file);
619 psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
621 GtkFileFilter *filter = gtk_file_filter_new ();
623 gtk_file_chooser_dialog_new (_("Open"),
624 GTK_WINDOW (toplevel),
625 GTK_FILE_CHOOSER_ACTION_OPEN,
626 _("Cancel"), GTK_RESPONSE_CANCEL,
627 _("Open"), GTK_RESPONSE_ACCEPT,
630 g_object_set (dialog, "local-only", FALSE, NULL);
632 gtk_file_filter_set_name (filter, _("Data and Syntax Files"));
633 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
634 gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
635 gtk_file_filter_add_mime_type (filter, "application/x-spss-spv");
636 gtk_file_filter_add_pattern (filter, "*.zsav");
637 gtk_file_filter_add_pattern (filter, "*.sps");
638 gtk_file_filter_add_pattern (filter, "*.SPS");
639 gtk_file_filter_add_pattern (filter, "*.spv");
640 gtk_file_filter_add_pattern (filter, "*.SPV");
641 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
643 filter = gtk_file_filter_new ();
644 gtk_file_filter_set_name (filter, _("System Files (*.sav, *.zsav)"));
645 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
646 gtk_file_filter_add_pattern (filter, "*.zsav");
647 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
649 filter = gtk_file_filter_new ();
650 gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
651 gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
652 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
654 filter = gtk_file_filter_new ();
655 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
656 gtk_file_filter_add_pattern (filter, "*.sps");
657 gtk_file_filter_add_pattern (filter, "*.SPS");
658 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
660 filter = gtk_file_filter_new ();
661 gtk_file_filter_set_name (filter, _("Output Files (*.spv) "));
662 gtk_file_filter_add_pattern (filter, "*.spv");
663 gtk_file_filter_add_pattern (filter, "*.SPV");
664 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
666 filter = gtk_file_filter_new ();
667 gtk_file_filter_set_name (filter, _("All Files"));
668 gtk_file_filter_add_pattern (filter, "*");
669 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
671 if (toplevel->filename)
673 const gchar *filename = toplevel->filename;
676 if (! g_path_is_absolute (filename))
679 g_build_filename (g_get_current_dir (), filename, NULL);
680 dir_name = g_path_get_dirname (path);
685 dir_name = g_path_get_dirname (filename);
687 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
692 gtk_file_chooser_set_extra_widget (
693 GTK_FILE_CHOOSER (dialog),
694 psppire_encoding_selector_new ("Auto", true));
700 read_spv_file (const char *filename)
702 struct output_item *root;
703 char *error = spv_read (filename, &root, NULL);
707 fprintf (stderr, "%s\n", error);
711 output_item_submit_children (root);
714 /* Callback for the file_open action.
715 Prompts for a filename and opens it */
717 psppire_window_open (PsppireWindow *de)
719 GtkWidget *dialog = psppire_window_file_chooser_dialog (de);
721 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), relocate (examples_dir), NULL);
723 switch (gtk_dialog_run (GTK_DIALOG (dialog)))
725 case GTK_RESPONSE_ACCEPT:
728 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
730 const gchar **cs = NULL;
731 g_get_filename_charsets (&cs);
733 gchar *encoding = psppire_encoding_selector_get_encoding (
734 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
736 struct file_handle *fh = fh_create_file (NULL, name, cs[0], fh_default_properties ());
738 int retval = any_reader_detect (fh, NULL);
740 open_data_window (de, name, encoding, NULL);
741 else if (retval == 0)
743 char *error = spv_detect (name);
745 read_spv_file (name);
749 open_syntax_window (name, encoding);
762 gtk_widget_destroy (dialog);
766 /* Puts FILE_NAME (encoded in the glib file name encoding) into the recent list
767 with associated MIME_TYPE. If it's already in the list, it moves it to the
770 add_most_recent (const char *file_name,
771 const char *mime_type, const char *encoding)
773 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
776 GtkRecentData recent_data;
777 gchar *full_mime_type;
779 if (encoding && encoding[0])
780 full_mime_type = g_strdup_printf ("%s; charset=%s",
781 mime_type, encoding);
783 full_mime_type = g_strdup (mime_type);
785 recent_data.display_name = NULL;
786 recent_data.description = NULL;
787 recent_data.mime_type = full_mime_type;
788 recent_data.app_name = CONST_CAST (gchar *, g_get_application_name ());
789 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
790 recent_data.groups = NULL;
791 recent_data.is_private = FALSE;
793 gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
796 g_free (recent_data.app_exec);
797 g_free (full_mime_type);
806 If FILE_NAME exists in the recent list, then delete it.
809 delete_recent (const char *file_name)
811 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
814 gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);