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"
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);
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 ();
248 g_signal_handler_disconnect (reg, window->remove_handler);
249 g_signal_handler_disconnect (reg, window->insert_handler);
250 psppire_window_register_remove (reg, window->list_name);
251 g_free (window->filename);
252 g_free (window->basename);
254 g_free (window->description);
255 g_free (window->list_name);
257 g_hash_table_destroy (window->menuitem_table);
259 if (G_OBJECT_CLASS (parent_class)->finalize)
260 G_OBJECT_CLASS (parent_class)->finalize (object);
264 psppire_window_class_init (PsppireWindowClass *class)
266 GObjectClass *object_class = G_OBJECT_CLASS (class);
268 GParamSpec *description_spec =
269 null_if_empty_param ("description",
271 "A string describing the usage of the window",
272 NULL, /*Should be overridden by derived classes */
273 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
275 GParamSpec *filename_spec =
276 null_if_empty_param ("filename",
278 "The name of the file associated with this window, if any",
280 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
282 GParamSpec *id_spec =
283 null_if_empty_param ("id",
285 "The PSPP language identifier for the data associated "
286 "with this window (e.g. dataset name)",
288 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
290 object_class->set_property = psppire_window_set_property;
291 object_class->get_property = psppire_window_get_property;
293 g_object_class_install_property (object_class,
297 g_object_class_install_property (object_class,
301 g_object_class_install_property (object_class,
305 parent_class = g_type_class_peek_parent (class);
310 psppire_window_base_init (PsppireWindowClass *class)
312 GObjectClass *object_class = G_OBJECT_CLASS (class);
314 object_class->finalize = psppire_window_finalize;
320 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
324 filename = g_filename_display_name (key);
325 item = gtk_check_menu_item_new_with_label (filename);
328 g_hash_table_insert (window->menuitem_table, key, item);
332 insert_item (gpointer key, gpointer value, gpointer data)
334 PsppireWindow *window = PSPPIRE_WINDOW (data);
336 if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
339 insert_menuitem_into_menu (window, key);
342 /* Insert a new item into the window menu */
344 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
346 PsppireWindow *window = PSPPIRE_WINDOW (data);
348 insert_menuitem_into_menu (window, (gpointer) key);
353 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
355 PsppireWindow *window = PSPPIRE_WINDOW (data);
356 g_hash_table_remove (window->menuitem_table, key);
360 insert_existing_items (PsppireWindow *window)
362 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
367 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
369 PsppireWindowRegister *reg = psppire_window_register_new ();
373 gint response = psppire_window_query_save (w);
378 case GTK_RESPONSE_CANCEL:
381 case GTK_RESPONSE_APPLY:
382 psppire_window_save (w);
385 /* Save failed, or user exited Save As dialog with Cancel. */
389 case GTK_RESPONSE_REJECT:
394 if ( 1 == psppire_window_register_n_items (reg))
402 psppire_window_init (PsppireWindow *window)
404 window->filename = NULL;
405 window->basename = NULL;
407 window->description = NULL;
408 window->list_name = NULL;
410 window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
413 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
415 window->insert_handler = g_signal_connect (psppire_window_register_new (),
417 G_CALLBACK (insert_menuitem),
420 window->remove_handler = g_signal_connect (psppire_window_register_new (),
422 G_CALLBACK (remove_menuitem),
425 window->added_separator = FALSE;
426 window->dirty = FALSE;
428 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
430 g_object_set (window, "icon-name", "pspp", NULL);
434 Ask the user if the buffer should be saved.
438 psppire_window_query_save (PsppireWindow *se)
442 GtkWidget *cancel_button;
448 g_get_current_time (&time);
451 description = g_filename_display_basename (se->filename);
453 description = g_strdup (se->id);
455 description = g_strdup (se->description);
457 gtk_message_dialog_new (GTK_WINDOW (se),
461 _("Save the changes to `%s' before closing?"),
463 g_free (description);
465 g_object_set (dialog, "icon-name", "pspp", NULL);
467 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
468 _("If you don't save, changes from the last %ld seconds will be permanently lost."),
469 time.tv_sec - se->savetime.tv_sec);
471 gtk_dialog_add_button (GTK_DIALOG (dialog),
472 _("Close _without saving"),
473 GTK_RESPONSE_REJECT);
475 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
477 GTK_RESPONSE_CANCEL);
479 gtk_dialog_add_button (GTK_DIALOG (dialog),
483 gtk_widget_grab_focus (cancel_button);
485 response = gtk_dialog_run (GTK_DIALOG (dialog));
487 gtk_widget_destroy (dialog);
493 /* The return value is encoded in the glib filename encoding. */
495 psppire_window_get_filename (PsppireWindow *w)
501 /* FILENAME must be encoded in the glib filename encoding. */
503 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
505 g_object_set (w, "filename", filename, NULL);
509 psppire_window_set_unsaved (PsppireWindow *w)
511 if ( w->dirty == FALSE)
512 g_get_current_time (&w->savetime);
516 psppire_window_set_title (w);
520 psppire_window_get_unsaved (PsppireWindow *w)
530 minimise_window (gpointer key, gpointer value, gpointer data)
532 gtk_window_iconify (GTK_WINDOW (value));
537 psppire_window_minimise_all (void)
539 PsppireWindowRegister *reg = psppire_window_register_new ();
541 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
548 psppire_window_model_get_type (void)
550 static GType window_model_type = 0;
552 if (! window_model_type)
554 static const GTypeInfo window_model_info =
556 sizeof (PsppireWindowIface), /* class_size */
557 NULL, /* base_init */
558 NULL, /* base_finalize */
560 NULL, /* class_finalize */
561 NULL, /* class_data */
568 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
569 &window_model_info, 0);
571 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
574 return window_model_type;
579 psppire_window_save (PsppireWindow *w)
581 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
584 g_return_if_fail (i->save);
586 if (w->filename == NULL)
587 psppire_window_save_as (w);
592 psppire_window_set_title (w);
597 psppire_window_save_as (PsppireWindow *w)
599 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
603 g_return_if_fail (i->pick_filename);
605 old_filename = w->filename;
608 i->pick_filename (w);
609 if (w->filename == NULL)
610 w->filename = old_filename;
613 g_free (old_filename);
614 psppire_window_save (w);
618 static void delete_recent (const char *file_name);
621 psppire_window_load (PsppireWindow *w, const gchar *file,
622 const gchar *encoding, gpointer hint)
625 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
627 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
631 g_return_val_if_fail (i->load, FALSE);
633 ok = i->load (w, file, encoding, hint);
637 psppire_window_set_filename (w, file);
641 delete_recent (file);
648 psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
650 GtkFileFilter *filter = gtk_file_filter_new ();
652 gtk_file_chooser_dialog_new (_("Open"),
653 GTK_WINDOW (toplevel),
654 GTK_FILE_CHOOSER_ACTION_OPEN,
655 _("Cancel"), GTK_RESPONSE_CANCEL,
656 _("Open"), GTK_RESPONSE_ACCEPT,
659 g_object_set (dialog, "local-only", FALSE, NULL);
661 gtk_file_filter_set_name (filter, _("Data and Syntax Files"));
662 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
663 gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
664 gtk_file_filter_add_mime_type (filter, "application/x-spss-spv");
665 gtk_file_filter_add_pattern (filter, "*.zsav");
666 gtk_file_filter_add_pattern (filter, "*.sps");
667 gtk_file_filter_add_pattern (filter, "*.SPS");
668 gtk_file_filter_add_pattern (filter, "*.spv");
669 gtk_file_filter_add_pattern (filter, "*.SPV");
670 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
672 filter = gtk_file_filter_new ();
673 gtk_file_filter_set_name (filter, _("System Files (*.sav, *.zsav)"));
674 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
675 gtk_file_filter_add_pattern (filter, "*.zsav");
676 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
678 filter = gtk_file_filter_new ();
679 gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
680 gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
681 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
683 filter = gtk_file_filter_new ();
684 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
685 gtk_file_filter_add_pattern (filter, "*.sps");
686 gtk_file_filter_add_pattern (filter, "*.SPS");
687 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
689 filter = gtk_file_filter_new ();
690 gtk_file_filter_set_name (filter, _("Output Files (*.spv) "));
691 gtk_file_filter_add_pattern (filter, "*.spv");
692 gtk_file_filter_add_pattern (filter, "*.SPV");
693 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
695 filter = gtk_file_filter_new ();
696 gtk_file_filter_set_name (filter, _("All Files"));
697 gtk_file_filter_add_pattern (filter, "*");
698 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
700 if (toplevel->filename)
702 const gchar *filename = toplevel->filename;
705 if ( ! g_path_is_absolute (filename))
708 g_build_filename (g_get_current_dir (), filename, NULL);
709 dir_name = g_path_get_dirname (path);
714 dir_name = g_path_get_dirname (filename);
716 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
721 gtk_file_chooser_set_extra_widget (
722 GTK_FILE_CHOOSER (dialog),
723 psppire_encoding_selector_new ("Auto", true));
730 const struct spv_item **nodes;
734 const struct spv_item *stub[N_STUB];
738 swap_nodes (const struct spv_item **a, const struct spv_item **b)
740 const struct spv_item *tmp = *a;
746 get_path (const struct spv_item *item, struct item_path *path)
748 size_t allocated = 10;
749 path->nodes = path->stub;
754 if (path->n >= allocated)
756 if (path->nodes == path->stub)
757 path->nodes = xmemdup (path->stub, sizeof path->stub);
758 path->nodes = x2nrealloc (path->nodes, &allocated,
759 sizeof *path->nodes);
761 path->nodes[path->n++] = item;
765 for (size_t i = 0; i < path->n / 2; i++)
766 swap_nodes (&path->nodes[i], &path->nodes[path->n - i - 1]);
770 free_path (struct item_path *path)
772 if (path && path->nodes != path->stub)
777 dump_heading_transition (const struct spv_item *old,
778 const struct spv_item *new)
783 struct item_path old_path, new_path;
784 get_path (old, &old_path);
785 get_path (new, &new_path);
788 for (; common < old_path.n && common < new_path.n; common++)
789 if (old_path.nodes[common] != new_path.nodes[common])
792 for (size_t i = common; i < old_path.n; i++)
793 group_close_item_submit (group_close_item_create ());
794 for (size_t i = common; i < new_path.n; i++)
795 group_open_item_submit (group_open_item_create (
796 new_path.nodes[i]->command_id));
798 free_path (&old_path);
799 free_path (&new_path);
803 read_spv_file (const char *filename)
805 struct spv_reader *spv;
806 char *error = spv_open (filename, &spv);
810 fprintf (stderr, "%s\n", error);
814 struct spv_item **items;
816 spv_select (spv, NULL, 0, &items, &n_items);
817 struct spv_item *prev_heading = spv_get_root (spv);
818 for (size_t i = 0; i < n_items; i++)
820 struct spv_item *heading
821 = items[i]->type == SPV_ITEM_HEADING ? items[i] : items[i]->parent;
822 dump_heading_transition (prev_heading, heading);
823 if (items[i]->type == SPV_ITEM_TEXT)
824 spv_text_submit (items[i]);
825 else if (items[i]->type == SPV_ITEM_TABLE)
826 pivot_table_submit (spv_item_get_table (items[i]));
827 prev_heading = heading;
829 dump_heading_transition (prev_heading, spv_get_root (spv));
834 /* Callback for the file_open action.
835 Prompts for a filename and opens it */
837 psppire_window_open (PsppireWindow *de)
839 GtkWidget *dialog = psppire_window_file_chooser_dialog (de);
841 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), relocate (examples_dir), NULL);
843 switch (gtk_dialog_run (GTK_DIALOG (dialog)))
845 case GTK_RESPONSE_ACCEPT:
848 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
850 const gchar **cs = NULL;
851 g_get_filename_charsets (&cs);
853 gchar *encoding = psppire_encoding_selector_get_encoding (
854 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
856 struct file_handle *fh = fh_create_file (NULL, name, cs[0], fh_default_properties ());
858 int retval = any_reader_detect (fh, NULL);
860 open_data_window (de, name, encoding, NULL);
861 else if (retval == 0)
863 char *error = spv_detect (name);
865 read_spv_file (name);
869 open_syntax_window (name, encoding);
882 gtk_widget_destroy (dialog);
886 /* Puts FILE_NAME (encoded in the glib file name encoding) into the recent list
887 with associated MIME_TYPE. If it's already in the list, it moves it to the
890 add_most_recent (const char *file_name,
891 const char *mime_type, const char *encoding)
893 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
896 GtkRecentData recent_data;
897 gchar *full_mime_type;
899 if (encoding && encoding[0])
900 full_mime_type = g_strdup_printf ("%s; charset=%s",
901 mime_type, encoding);
903 full_mime_type = g_strdup (mime_type);
905 recent_data.display_name = NULL;
906 recent_data.description = NULL;
907 recent_data.mime_type = full_mime_type;
908 recent_data.app_name = CONST_CAST (gchar *, g_get_application_name ());
909 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
910 recent_data.groups = NULL;
911 recent_data.is_private = FALSE;
913 gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
916 g_free (recent_data.app_exec);
917 g_free (full_mime_type);
926 If FILE_NAME exists in the recent list, then delete it.
929 delete_recent (const char *file_name)
931 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
934 gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);