1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009, 2010, 2011, 2013, 2014 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"
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
32 #include "data/any-reader.h"
33 #include "data/file-handle-def.h"
34 #include "data/dataset.h"
35 #include "libpspp/version.h"
36 #include "output/group-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_base_init (PsppireWindowClass *class);
49 static void psppire_window_class_init (PsppireWindowClass *class);
50 static void psppire_window_init (PsppireWindow *window);
53 static GObjectClass *parent_class;
56 psppire_window_get_type (void)
58 static GType psppire_window_type = 0;
60 if (!psppire_window_type)
62 static const GTypeInfo psppire_window_info =
64 sizeof (PsppireWindowClass),
65 (GBaseInitFunc) psppire_window_base_init,
66 (GBaseFinalizeFunc) NULL,
67 (GClassInitFunc) psppire_window_class_init,
68 (GClassFinalizeFunc) NULL,
70 sizeof (PsppireWindow),
72 (GInstanceInitFunc) psppire_window_init,
76 g_type_register_static (PSPPIRE_TYPE_WINDOW_BASE, "PsppireWindow",
77 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
80 return psppire_window_type;
95 psppire_window_set_title (PsppireWindow *window)
97 GString *title = g_string_sized_new (80);
100 g_string_append_c (title, '*');
102 if (window->basename || window->id)
104 if (window->basename)
105 g_string_append_printf (title, "%s ", window->basename);
108 g_string_append_printf (title, "[%s] ", window->id);
110 g_string_append_unichar (title, 0x2014); /* em dash */
111 g_string_append_c (title, ' '); /* em dash */
114 g_string_append_printf (title, "PSPPIRE %s", window->description);
117 sscanf (bare_version, "%*d.%d.%*d", &minor);
119 g_string_append_printf (title, " - Test version! Please report bugs to %s", PACKAGE_BUGREPORT);
121 gtk_window_set_title (GTK_WINDOW (window), title->str);
123 g_string_free (title, TRUE);
127 psppire_window_update_list_name (PsppireWindow *window)
129 PsppireWindowRegister *reg = psppire_window_register_new ();
130 GString *candidate = g_string_sized_new (80);
136 /* Compose a name. */
137 g_string_truncate (candidate, 0);
138 if (window->filename)
140 gchar *display_filename = g_filename_display_name (window->filename);
141 g_string_append (candidate, display_filename);
142 g_free (display_filename);
145 g_string_append_printf (candidate, " [%s]", window->id);
148 g_string_append_printf (candidate, "[%s]", window->id);
150 g_string_append (candidate, window->description);
153 g_string_append_printf (candidate, " #%d", n);
155 if (window->list_name && !strcmp (candidate->str, window->list_name))
157 /* Keep the existing name. */
158 g_string_free (candidate, TRUE);
162 while (psppire_window_register_lookup (reg, candidate->str));
164 if (window->list_name)
165 psppire_window_register_remove (reg, window->list_name);
167 g_free (window->list_name);
168 window->list_name = g_string_free (candidate, FALSE);
170 psppire_window_register_insert (reg, window, window->list_name);
174 psppire_window_name_changed (PsppireWindow *window)
176 psppire_window_set_title (window);
177 psppire_window_update_list_name (window);
181 psppire_window_set_property (GObject *object,
186 PsppireWindow *window = PSPPIRE_WINDOW (object);
190 case PROP_DESCRIPTION:
191 g_free (window->description);
192 window->description = g_value_dup_string (value);
193 psppire_window_set_title (window);
196 g_free (window->filename);
197 window->filename = g_value_dup_string (value);
198 g_free (window->basename);
199 window->basename = (window->filename
200 ? g_filename_display_basename (window->filename)
202 psppire_window_name_changed (window);
206 window->id = g_value_dup_string (value);
207 psppire_window_name_changed (window);
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 psppire_window_get_property (GObject *object,
222 PsppireWindow *window = PSPPIRE_WINDOW (object);
227 g_value_set_string (value, window->filename);
229 case PROP_DESCRIPTION:
230 g_value_set_string (value, window->description);
233 g_value_set_string (value, window->id);
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 psppire_window_finalize (GObject *object)
245 PsppireWindow *window = PSPPIRE_WINDOW (object);
247 PsppireWindowRegister *reg = psppire_window_register_new ();
249 g_signal_handler_disconnect (reg, window->remove_handler);
250 g_signal_handler_disconnect (reg, window->insert_handler);
251 psppire_window_register_remove (reg, window->list_name);
252 g_free (window->filename);
253 g_free (window->basename);
255 g_free (window->description);
256 g_free (window->list_name);
258 g_hash_table_destroy (window->menuitem_table);
260 if (G_OBJECT_CLASS (parent_class)->finalize)
261 G_OBJECT_CLASS (parent_class)->finalize (object);
265 psppire_window_class_init (PsppireWindowClass *class)
267 GObjectClass *object_class = G_OBJECT_CLASS (class);
269 GParamSpec *description_spec =
270 null_if_empty_param ("description",
272 "A string describing the usage of the window",
273 NULL, /*Should be overridden by derived classes */
274 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
276 GParamSpec *filename_spec =
277 null_if_empty_param ("filename",
279 "The name of the file associated with this window, if any",
281 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
283 GParamSpec *id_spec =
284 null_if_empty_param ("id",
286 "The PSPP language identifier for the data associated "
287 "with this window (e.g. dataset name)",
289 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
291 object_class->set_property = psppire_window_set_property;
292 object_class->get_property = psppire_window_get_property;
294 g_object_class_install_property (object_class,
298 g_object_class_install_property (object_class,
302 g_object_class_install_property (object_class,
306 parent_class = g_type_class_peek_parent (class);
311 psppire_window_base_init (PsppireWindowClass *class)
313 GObjectClass *object_class = G_OBJECT_CLASS (class);
315 object_class->finalize = psppire_window_finalize;
321 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
325 filename = g_filename_display_name (key);
326 item = gtk_check_menu_item_new_with_label (filename);
329 g_hash_table_insert (window->menuitem_table, key, item);
333 insert_item (gpointer key, gpointer value, gpointer data)
335 PsppireWindow *window = PSPPIRE_WINDOW (data);
337 if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
340 insert_menuitem_into_menu (window, key);
343 /* Insert a new item into the window menu */
345 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
347 PsppireWindow *window = PSPPIRE_WINDOW (data);
349 insert_menuitem_into_menu (window, (gpointer) key);
354 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
356 PsppireWindow *window = PSPPIRE_WINDOW (data);
357 g_hash_table_remove (window->menuitem_table, key);
361 insert_existing_items (PsppireWindow *window)
363 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
368 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
370 PsppireWindowRegister *reg = psppire_window_register_new ();
374 gint response = psppire_window_query_save (w);
379 case GTK_RESPONSE_CANCEL:
382 case GTK_RESPONSE_APPLY:
383 psppire_window_save (w);
386 /* Save failed, or user exited Save As dialog with Cancel. */
390 case GTK_RESPONSE_REJECT:
395 if ( 1 == psppire_window_register_n_items (reg))
403 psppire_window_init (PsppireWindow *window)
405 window->filename = NULL;
406 window->basename = NULL;
408 window->description = NULL;
409 window->list_name = NULL;
411 window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
414 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
416 window->insert_handler = g_signal_connect (psppire_window_register_new (),
418 G_CALLBACK (insert_menuitem),
421 window->remove_handler = g_signal_connect (psppire_window_register_new (),
423 G_CALLBACK (remove_menuitem),
426 window->added_separator = FALSE;
427 window->dirty = FALSE;
429 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
431 g_object_set (window, "icon-name", "pspp", NULL);
435 Ask the user if the buffer should be saved.
439 psppire_window_query_save (PsppireWindow *se)
443 GtkWidget *cancel_button;
449 g_get_current_time (&time);
452 description = g_filename_display_basename (se->filename);
454 description = g_strdup (se->id);
456 description = g_strdup (se->description);
458 gtk_message_dialog_new (GTK_WINDOW (se),
462 _("Save the changes to `%s' before closing?"),
464 g_free (description);
466 g_object_set (dialog, "icon-name", "pspp", NULL);
468 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
469 _("If you don't save, changes from the last %ld seconds will be permanently lost."),
470 time.tv_sec - se->savetime.tv_sec);
472 gtk_dialog_add_button (GTK_DIALOG (dialog),
473 _("Close _without saving"),
474 GTK_RESPONSE_REJECT);
476 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
478 GTK_RESPONSE_CANCEL);
480 gtk_dialog_add_button (GTK_DIALOG (dialog),
484 gtk_widget_grab_focus (cancel_button);
486 response = gtk_dialog_run (GTK_DIALOG (dialog));
488 gtk_widget_destroy (dialog);
494 /* The return value is encoded in the glib filename encoding. */
496 psppire_window_get_filename (PsppireWindow *w)
502 /* FILENAME must be encoded in the glib filename encoding. */
504 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
506 g_object_set (w, "filename", filename, NULL);
510 psppire_window_set_unsaved (PsppireWindow *w)
512 if ( w->dirty == FALSE)
513 g_get_current_time (&w->savetime);
517 psppire_window_set_title (w);
521 psppire_window_get_unsaved (PsppireWindow *w)
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 psppire_window_set_title (w);
598 psppire_window_save_as (PsppireWindow *w)
600 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
604 g_return_if_fail (i->pick_filename);
606 old_filename = w->filename;
609 i->pick_filename (w);
610 if (w->filename == NULL)
611 w->filename = old_filename;
614 g_free (old_filename);
615 psppire_window_save (w);
619 static void delete_recent (const char *file_name);
622 psppire_window_load (PsppireWindow *w, const gchar *file,
623 const gchar *encoding, gpointer hint)
626 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
628 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
632 g_return_val_if_fail (i->load, FALSE);
634 ok = i->load (w, file, encoding, hint);
638 psppire_window_set_filename (w, file);
642 delete_recent (file);
649 psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
651 GtkFileFilter *filter = gtk_file_filter_new ();
653 gtk_file_chooser_dialog_new (_("Open"),
654 GTK_WINDOW (toplevel),
655 GTK_FILE_CHOOSER_ACTION_OPEN,
656 _("Cancel"), GTK_RESPONSE_CANCEL,
657 _("Open"), GTK_RESPONSE_ACCEPT,
660 g_object_set (dialog, "local-only", FALSE, NULL);
662 gtk_file_filter_set_name (filter, _("Data and Syntax Files"));
663 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
664 gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
665 gtk_file_filter_add_mime_type (filter, "application/x-spss-spv");
666 gtk_file_filter_add_pattern (filter, "*.zsav");
667 gtk_file_filter_add_pattern (filter, "*.sps");
668 gtk_file_filter_add_pattern (filter, "*.SPS");
669 gtk_file_filter_add_pattern (filter, "*.spv");
670 gtk_file_filter_add_pattern (filter, "*.SPV");
671 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
673 filter = gtk_file_filter_new ();
674 gtk_file_filter_set_name (filter, _("System Files (*.sav, *.zsav)"));
675 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
676 gtk_file_filter_add_pattern (filter, "*.zsav");
677 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
679 filter = gtk_file_filter_new ();
680 gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
681 gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
682 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
684 filter = gtk_file_filter_new ();
685 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
686 gtk_file_filter_add_pattern (filter, "*.sps");
687 gtk_file_filter_add_pattern (filter, "*.SPS");
688 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
690 filter = gtk_file_filter_new ();
691 gtk_file_filter_set_name (filter, _("Output Files (*.spv) "));
692 gtk_file_filter_add_pattern (filter, "*.spv");
693 gtk_file_filter_add_pattern (filter, "*.SPV");
694 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
696 filter = gtk_file_filter_new ();
697 gtk_file_filter_set_name (filter, _("All Files"));
698 gtk_file_filter_add_pattern (filter, "*");
699 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
701 if (toplevel->filename)
703 const gchar *filename = toplevel->filename;
706 if ( ! g_path_is_absolute (filename))
709 g_build_filename (g_get_current_dir (), filename, NULL);
710 dir_name = g_path_get_dirname (path);
715 dir_name = g_path_get_dirname (filename);
717 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
722 gtk_file_chooser_set_extra_widget (
723 GTK_FILE_CHOOSER (dialog),
724 psppire_encoding_selector_new ("Auto", true));
731 const struct spv_item **nodes;
735 const struct spv_item *stub[N_STUB];
739 swap_nodes (const struct spv_item **a, const struct spv_item **b)
741 const struct spv_item *tmp = *a;
747 get_path (const struct spv_item *item, struct item_path *path)
749 size_t allocated = 10;
750 path->nodes = path->stub;
755 if (path->n >= allocated)
757 if (path->nodes == path->stub)
758 path->nodes = xmemdup (path->stub, sizeof path->stub);
759 path->nodes = x2nrealloc (path->nodes, &allocated,
760 sizeof *path->nodes);
762 path->nodes[path->n++] = item;
766 for (size_t i = 0; i < path->n / 2; i++)
767 swap_nodes (&path->nodes[i], &path->nodes[path->n - i - 1]);
771 free_path (struct item_path *path)
773 if (path && path->nodes != path->stub)
778 dump_heading_transition (const struct spv_item *old,
779 const struct spv_item *new)
784 struct item_path old_path, new_path;
785 get_path (old, &old_path);
786 get_path (new, &new_path);
789 for (; common < old_path.n && common < new_path.n; common++)
790 if (old_path.nodes[common] != new_path.nodes[common])
793 for (size_t i = common; i < old_path.n; i++)
794 group_close_item_submit (group_close_item_create ());
795 for (size_t i = common; i < new_path.n; i++)
796 group_open_item_submit (group_open_item_create (
797 new_path.nodes[i]->command_id));
799 free_path (&old_path);
800 free_path (&new_path);
804 read_spv_file (const char *filename)
806 struct spv_reader *spv;
807 char *error = spv_open (filename, &spv);
811 fprintf (stderr, "%s\n", error);
815 struct spv_criteria criteria = SPV_CRITERIA_INITIALIZER (criteria);
816 struct spv_item **items;
818 spv_select (spv, &criteria, &items, &n_items);
819 struct spv_item *prev_heading = spv_get_root (spv);
820 for (size_t i = 0; i < n_items; i++)
822 struct spv_item *heading
823 = items[i]->type == SPV_ITEM_HEADING ? items[i] : items[i]->parent;
824 dump_heading_transition (prev_heading, heading);
825 if (items[i]->type == SPV_ITEM_TEXT)
826 spv_text_submit (items[i]);
827 else if (items[i]->type == SPV_ITEM_TABLE)
828 pivot_table_submit (spv_item_get_table (items[i]));
829 prev_heading = heading;
831 dump_heading_transition (prev_heading, spv_get_root (spv));
836 /* Callback for the file_open action.
837 Prompts for a filename and opens it */
839 psppire_window_open (PsppireWindow *de)
841 GtkWidget *dialog = psppire_window_file_chooser_dialog (de);
843 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), relocate (examples_dir), NULL);
845 switch (gtk_dialog_run (GTK_DIALOG (dialog)))
847 case GTK_RESPONSE_ACCEPT:
850 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
852 const gchar **cs = NULL;
853 g_get_filename_charsets (&cs);
855 gchar *encoding = psppire_encoding_selector_get_encoding (
856 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
858 struct file_handle *fh = fh_create_file (NULL, name, cs[0], fh_default_properties ());
860 int retval = any_reader_detect (fh, NULL);
862 open_data_window (de, name, encoding, NULL);
863 else if (retval == 0)
865 char *error = spv_detect (name);
867 read_spv_file (name);
871 open_syntax_window (name, encoding);
884 gtk_widget_destroy (dialog);
888 /* Puts FILE_NAME (encoded in the glib file name encoding) into the recent list
889 with associated MIME_TYPE. If it's already in the list, it moves it to the
892 add_most_recent (const char *file_name,
893 const char *mime_type, const char *encoding)
895 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
898 GtkRecentData recent_data;
899 gchar *full_mime_type;
901 if (encoding && encoding[0])
902 full_mime_type = g_strdup_printf ("%s; charset=%s",
903 mime_type, encoding);
905 full_mime_type = g_strdup (mime_type);
907 recent_data.display_name = NULL;
908 recent_data.description = NULL;
909 recent_data.mime_type = full_mime_type;
910 recent_data.app_name = CONST_CAST (gchar *, g_get_application_name ());
911 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
912 recent_data.groups = NULL;
913 recent_data.is_private = FALSE;
915 gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
918 g_free (recent_data.app_exec);
919 g_free (full_mime_type);
928 If FILE_NAME exists in the recent list, then delete it.
931 delete_recent (const char *file_name)
933 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
936 gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);