2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2006, 2007 Free Software Foundation
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include <glade/glade.h>
27 #include "window-manager.h"
28 #include <gtksheet/gtksheet.h>
32 #include "psppire-dialog.h"
33 #include "psppire-selector.h"
34 #include "weight-cases-dialog.h"
35 #include "split-file-dialog.h"
36 #include "transpose-dialog.h"
37 #include "sort-cases-dialog.h"
38 #include "compute-dialog.h"
39 #include "dict-display.h"
41 #define _(msgid) gettext (msgid)
42 #define N_(msgid) msgid
44 #include "data-editor.h"
45 #include "syntax-editor.h"
46 #include <language/syntax-string-source.h>
47 #include <libpspp/syntax-gen.h>
48 #include "window-manager.h"
50 #include "psppire-data-store.h"
51 #include "psppire-var-store.h"
53 static void register_data_editor_actions (struct data_editor *de);
55 static void insert_variable (GtkCheckMenuItem *m, gpointer data);
59 /* Switch between the VAR SHEET and the DATA SHEET */
60 enum {PAGE_DATA_SHEET = 0, PAGE_VAR_SHEET};
62 static gboolean click2column (GtkWidget *w, gint col, gpointer data);
64 static gboolean click2row (GtkWidget *w, gint row, gpointer data);
67 static void select_sheet (struct data_editor *de, guint page_num);
70 /* Callback for when the dictionary changes properties*/
71 static void on_weight_change (GObject *, gint, gpointer);
72 static void on_filter_change (GObject *, gint, gpointer);
73 static void on_split_change (PsppireDict *, gpointer);
75 static void data_var_select (GtkNotebook *notebook,
76 GtkNotebookPage *page,
80 static void status_bar_activate (GtkCheckMenuItem *, gpointer);
82 static void grid_lines_activate (GtkCheckMenuItem *, gpointer);
84 static void data_sheet_activate (GtkCheckMenuItem *, gpointer);
86 static void variable_sheet_activate (GtkCheckMenuItem *, gpointer );
88 static void fonts_activate (GtkMenuItem *, gpointer);
90 static void value_labels_activate (GtkCheckMenuItem *, gpointer);
91 static void value_labels_toggled (GtkToggleToolButton *, gpointer);
94 static void file_quit (GtkCheckMenuItem *, gpointer );
96 static void on_clear_activate (GtkMenuItem *, gpointer);
99 enable_edit_clear (GtkWidget *w, gint row, gpointer data)
101 struct data_editor *de = data;
103 GtkWidget *menuitem = get_widget_assert (de->xml, "edit_clear");
105 gtk_widget_set_sensitive (menuitem, TRUE);
109 disable_edit_clear (GtkWidget *w, gint x, gint y, gpointer data)
111 struct data_editor *de = data;
113 GtkWidget *menuitem = get_widget_assert (de->xml, "edit_clear");
115 gtk_widget_set_sensitive (menuitem, FALSE);
120 static void open_data_file (const gchar *, struct data_editor *);
124 #if RECENT_LISTS_AVAILABLE
127 on_recent_data_select (GtkMenuShell *menushell, gpointer user_data)
131 struct data_editor *de = user_data;
134 gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (menushell));
136 file = g_filename_from_uri (uri, NULL, NULL);
140 open_data_file (file, de);
146 on_recent_files_select (GtkMenuShell *menushell, gpointer user_data)
150 struct syntax_editor *se ;
153 gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (menushell));
155 file = g_filename_from_uri (uri, NULL, NULL);
159 se = (struct syntax_editor *)
160 window_create (WINDOW_SYNTAX, file);
162 load_editor_from_file (se, file, NULL);
172 Create a new data editor.
175 new_data_editor (void)
177 struct data_editor *de ;
178 struct editor_window *e;
179 GtkSheet *var_sheet ;
182 de = g_malloc0 (sizeof (*de));
184 e = (struct editor_window *) de;
186 de->xml = XML_NEW ("data-editor.glade");
188 var_sheet = GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
190 vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
192 g_assert(vs); /* Traps a possible bug in win32 build */
194 g_signal_connect (vs->dict, "weight-changed",
195 G_CALLBACK (on_weight_change),
198 g_signal_connect (vs->dict, "filter-changed",
199 G_CALLBACK (on_filter_change),
202 g_signal_connect (vs->dict, "split-changed",
203 G_CALLBACK (on_split_change),
206 connect_help (de->xml);
209 register_data_editor_actions (de);
211 de->invoke_weight_cases_dialog =
212 gtk_action_new ("weight-cases-dialog",
214 _("Weight cases by variable"),
215 "pspp-weight-cases");
217 g_signal_connect (de->invoke_weight_cases_dialog, "activate",
218 G_CALLBACK (weight_cases_dialog), de);
221 de->invoke_transpose_dialog =
222 gtk_action_new ("transpose-dialog",
224 _("Transpose the cases with the variables"),
228 g_signal_connect (de->invoke_transpose_dialog, "activate",
229 G_CALLBACK (transpose_dialog), de);
233 de->invoke_split_file_dialog =
234 gtk_action_new ("split-file-dialog",
236 _("Split the active file"),
239 g_signal_connect (de->invoke_split_file_dialog, "activate",
240 G_CALLBACK (split_file_dialog), de);
244 de->invoke_sort_cases_dialog =
245 gtk_action_new ("sort-cases-dialog",
247 _("Sort cases in the active file"),
250 g_signal_connect (de->invoke_sort_cases_dialog, "activate",
251 G_CALLBACK (sort_cases_dialog), de);
254 de->invoke_compute_dialog =
255 gtk_action_new ("compute-dialog",
257 _("Compute new values for a variable"),
260 g_signal_connect (de->invoke_compute_dialog, "activate",
261 G_CALLBACK (compute_dialog), de);
264 e->window = GTK_WINDOW (get_widget_assert (de->xml, "data_editor"));
266 g_signal_connect_swapped (get_widget_assert (de->xml,"file_new_data"),
268 G_CALLBACK (gtk_action_activate),
269 de->action_data_new);
271 g_signal_connect_swapped (get_widget_assert (de->xml,"file_open_data"),
273 G_CALLBACK (gtk_action_activate),
274 de->action_data_open);
277 #if RECENT_LISTS_AVAILABLE
279 GtkRecentManager *rm = gtk_recent_manager_get_default ();
280 GtkWidget *recent_data = get_widget_assert (de->xml, "file_recent-data");
281 GtkWidget *recent_files = get_widget_assert (de->xml, "file_recent-files");
283 GtkWidget *menu = gtk_recent_chooser_menu_new_for_manager (rm);
285 GtkRecentFilter *filter = gtk_recent_filter_new ();
287 gtk_recent_filter_add_pattern (filter, "*.sav");
288 gtk_recent_filter_add_pattern (filter, "*.SAV");
290 gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (menu), filter);
292 gtk_widget_set_sensitive (recent_data, TRUE);
293 g_signal_connect (menu, "selection-done",
294 G_CALLBACK (on_recent_data_select), de);
296 gtk_menu_item_set_submenu (GTK_MENU_ITEM (recent_data), menu);
299 filter = gtk_recent_filter_new ();
300 menu = gtk_recent_chooser_menu_new_for_manager (rm);
302 gtk_recent_filter_add_pattern (filter, "*.sps");
303 gtk_recent_filter_add_pattern (filter, "*.SPS");
305 gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (menu), filter);
307 gtk_widget_set_sensitive (recent_files, TRUE);
308 g_signal_connect (menu, "selection-done",
309 G_CALLBACK (on_recent_files_select), de);
311 gtk_menu_item_set_submenu (GTK_MENU_ITEM (recent_files), menu);
315 g_signal_connect (get_widget_assert (de->xml,"file_new_syntax"),
317 G_CALLBACK (new_syntax_window),
320 g_signal_connect (get_widget_assert (de->xml,"file_open_syntax"),
322 G_CALLBACK (open_syntax_window),
325 g_signal_connect_swapped (get_widget_assert (de->xml,"file_save"),
327 G_CALLBACK (gtk_action_activate),
328 de->action_data_save);
330 g_signal_connect_swapped (get_widget_assert (de->xml,"file_save_as"),
332 G_CALLBACK (gtk_action_activate),
333 de->action_data_save_as);
336 g_signal_connect (get_widget_assert (de->xml,"edit_clear"),
338 G_CALLBACK (on_clear_activate),
342 g_signal_connect (get_widget_assert (de->xml,"data_insert-variable"),
344 G_CALLBACK (insert_variable),
347 gtk_action_connect_proxy (de->invoke_weight_cases_dialog,
348 get_widget_assert (de->xml, "data_weight-cases")
351 gtk_action_connect_proxy (de->invoke_transpose_dialog,
352 get_widget_assert (de->xml, "data_transpose")
355 gtk_action_connect_proxy (de->invoke_split_file_dialog,
356 get_widget_assert (de->xml, "data_split-file")
359 gtk_action_connect_proxy (de->invoke_sort_cases_dialog,
360 get_widget_assert (de->xml, "data_sort-cases")
363 gtk_action_connect_proxy (de->invoke_compute_dialog,
364 get_widget_assert (de->xml, "transform_compute")
368 g_signal_connect (get_widget_assert (de->xml,"help_about"),
370 G_CALLBACK (about_new),
374 g_signal_connect (get_widget_assert (de->xml,"help_reference"),
376 G_CALLBACK (reference_manual),
381 g_signal_connect (get_widget_assert (de->xml,"data_sheet"),
382 "double-click-column",
383 G_CALLBACK (click2column),
387 g_signal_connect (get_widget_assert (de->xml, "variable_sheet"),
389 GTK_SIGNAL_FUNC (click2row),
393 g_signal_connect (get_widget_assert (de->xml, "variable_sheet"),
395 GTK_SIGNAL_FUNC (enable_edit_clear),
398 g_signal_connect (get_widget_assert (de->xml, "variable_sheet"),
400 GTK_SIGNAL_FUNC (disable_edit_clear),
404 g_signal_connect (get_widget_assert (de->xml, "notebook"),
406 G_CALLBACK (data_var_select), de);
410 g_signal_connect (get_widget_assert (de->xml, "view_statusbar"),
412 G_CALLBACK (status_bar_activate), de);
415 g_signal_connect (get_widget_assert (de->xml, "view_gridlines"),
417 G_CALLBACK (grid_lines_activate), de);
421 g_signal_connect (get_widget_assert (de->xml, "view_data"),
423 G_CALLBACK (data_sheet_activate), de);
425 g_signal_connect (get_widget_assert (de->xml, "view_variables"),
427 G_CALLBACK (variable_sheet_activate), de);
431 g_signal_connect (get_widget_assert (de->xml, "view_fonts"),
433 G_CALLBACK (fonts_activate), de);
437 g_signal_connect (get_widget_assert (de->xml, "view_valuelabels"),
439 G_CALLBACK (value_labels_activate), de);
442 g_signal_connect (get_widget_assert (de->xml, "togglebutton-value-labels"),
444 G_CALLBACK (value_labels_toggled), de);
446 gtk_action_connect_proxy (de->action_data_open,
447 get_widget_assert (de->xml, "button-open")
450 gtk_action_connect_proxy (de->action_data_save,
451 get_widget_assert (de->xml, "button-save")
454 gtk_action_connect_proxy (de->invoke_weight_cases_dialog,
455 get_widget_assert (de->xml, "button-weight-cases")
458 gtk_action_connect_proxy (de->invoke_split_file_dialog,
459 get_widget_assert (de->xml, "button-split-file")
462 g_signal_connect (get_widget_assert (de->xml, "file_quit"),
464 G_CALLBACK (file_quit), de);
467 g_signal_connect (get_widget_assert (de->xml, "windows_minimise_all"),
469 G_CALLBACK (minimise_all_windows), NULL);
472 select_sheet (de, PAGE_DATA_SHEET);
478 /* Callback which occurs when the var sheet's row title
479 button is double clicked */
481 click2row (GtkWidget *w, gint row, gpointer data)
483 struct data_editor *de = data;
485 gint current_row, current_column;
487 GtkWidget *data_sheet = get_widget_assert (de->xml, "data_sheet");
489 data_editor_select_sheet (de, PAGE_DATA_SHEET);
491 gtk_sheet_get_active_cell (GTK_SHEET (data_sheet),
492 ¤t_row, ¤t_column);
494 gtk_sheet_set_active_cell (GTK_SHEET (data_sheet), current_row, row);
500 /* Callback which occurs when the data sheet's column title
503 click2column (GtkWidget *w, gint col, gpointer data)
505 struct data_editor *de = data;
507 gint current_row, current_column;
509 GtkWidget *var_sheet = get_widget_assert (de->xml, "variable_sheet");
511 data_editor_select_sheet (de, PAGE_VAR_SHEET);
513 gtk_sheet_get_active_cell (GTK_SHEET (var_sheet),
514 ¤t_row, ¤t_column);
516 gtk_sheet_set_active_cell (GTK_SHEET (var_sheet), col, current_column);
523 new_data_window (GtkMenuItem *menuitem, gpointer parent)
525 window_create (WINDOW_DATA, NULL);
530 select_sheet (struct data_editor *de, guint page_num)
532 GtkWidget *insert_variable = get_widget_assert (de->xml, "data_insert-variable");
533 GtkWidget *insert_cases = get_widget_assert (de->xml, "insert-cases");
535 GtkWidget *view_data = get_widget_assert (de->xml, "view_data");
536 GtkWidget *view_variables = get_widget_assert (de->xml, "view_variables");
541 gtk_widget_hide (view_variables);
542 gtk_widget_show (view_data);
543 gtk_widget_set_sensitive (insert_variable, TRUE);
544 gtk_widget_set_sensitive (insert_cases, FALSE);
546 case PAGE_DATA_SHEET:
547 gtk_widget_show (view_variables);
548 gtk_widget_hide (view_data);
550 gtk_widget_set_sensitive (insert_cases, TRUE);
554 g_assert_not_reached ();
561 data_var_select (GtkNotebook *notebook,
562 GtkNotebookPage *page,
566 struct data_editor *de = user_data;
568 select_sheet (de, page_num);
575 data_editor_select_sheet (struct data_editor *de, gint page)
577 gtk_notebook_set_current_page
579 GTK_NOTEBOOK (get_widget_assert (de->xml,"notebook")), page
585 status_bar_activate (GtkCheckMenuItem *menuitem, gpointer data)
587 struct data_editor *de = data;
588 GtkWidget *statusbar = get_widget_assert (de->xml, "status-bar");
590 if ( gtk_check_menu_item_get_active (menuitem) )
591 gtk_widget_show (statusbar);
593 gtk_widget_hide (statusbar);
598 grid_lines_activate (GtkCheckMenuItem *menuitem, gpointer data)
600 struct data_editor *de = data;
601 const bool grid_visible = gtk_check_menu_item_get_active (menuitem);
603 gtk_sheet_show_grid (GTK_SHEET (get_widget_assert (de->xml,
607 gtk_sheet_show_grid (GTK_SHEET (get_widget_assert (de->xml, "data_sheet")),
614 data_sheet_activate (GtkCheckMenuItem *menuitem, gpointer data)
616 struct data_editor *de = data;
618 data_editor_select_sheet (de, PAGE_DATA_SHEET);
623 variable_sheet_activate (GtkCheckMenuItem *menuitem, gpointer data)
625 struct data_editor *de = data;
627 data_editor_select_sheet (de, PAGE_VAR_SHEET);
632 fonts_activate (GtkMenuItem *menuitem, gpointer data)
634 struct data_editor *de = data;
636 gtk_font_selection_dialog_new (_("Font Selection"));
638 gtk_window_set_transient_for (GTK_WINDOW (dialog),
639 GTK_WINDOW (get_widget_assert (de->xml,
641 if ( GTK_RESPONSE_OK == gtk_dialog_run (GTK_DIALOG (dialog)) )
643 GtkSheet *data_sheet =
644 GTK_SHEET (get_widget_assert (de->xml, "data_sheet"));
646 GtkSheet *var_sheet =
647 GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
649 PsppireDataStore *ds = PSPPIRE_DATA_STORE (gtk_sheet_get_model (data_sheet));
650 PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
652 const gchar *font = gtk_font_selection_dialog_get_font_name
653 (GTK_FONT_SELECTION_DIALOG (dialog));
655 PangoFontDescription* font_desc =
656 pango_font_description_from_string (font);
658 psppire_var_store_set_font (vs, font_desc);
659 psppire_data_store_set_font (ds, font_desc);
662 gtk_widget_hide (dialog);
666 /* The next two callbacks are mutually co-operative */
668 /* Callback for the value labels menu item */
670 value_labels_activate (GtkCheckMenuItem *menuitem, gpointer data)
672 struct data_editor *de = data;
674 GtkSheet *data_sheet = GTK_SHEET (get_widget_assert (de->xml, "data_sheet"));
676 GtkToggleToolButton *tb =
677 GTK_TOGGLE_TOOL_BUTTON (get_widget_assert (de->xml,
678 "togglebutton-value-labels"));
680 PsppireDataStore *ds = PSPPIRE_DATA_STORE (gtk_sheet_get_model (data_sheet));
682 gboolean show_value_labels = gtk_check_menu_item_get_active (menuitem);
684 gtk_toggle_tool_button_set_active (tb, show_value_labels);
686 psppire_data_store_show_labels (ds, show_value_labels);
690 /* Callback for the value labels tooglebutton */
692 value_labels_toggled (GtkToggleToolButton *toggle_tool_button,
695 struct data_editor *de = data;
697 GtkSheet *data_sheet = GTK_SHEET (get_widget_assert (de->xml, "data_sheet"));
699 GtkCheckMenuItem *item =
700 GTK_CHECK_MENU_ITEM (get_widget_assert (de->xml, "view_valuelabels"));
702 PsppireDataStore *ds = PSPPIRE_DATA_STORE (gtk_sheet_get_model (data_sheet));
704 gboolean show_value_labels =
705 gtk_toggle_tool_button_get_active (toggle_tool_button);
707 gtk_check_menu_item_set_active (item, show_value_labels);
709 psppire_data_store_show_labels (ds, show_value_labels);
714 file_quit (GtkCheckMenuItem *menuitem, gpointer data)
716 /* FIXME: Need to be more intelligent here.
717 Give the user the opportunity to save any unsaved data.
724 /* Callback for when the Clear item in the edit menu is activated */
726 on_clear_activate (GtkMenuItem *menuitem, gpointer data)
728 struct data_editor *de = data;
730 GtkNotebook *notebook = GTK_NOTEBOOK (get_widget_assert (de->xml,
733 switch ( gtk_notebook_get_current_page (notebook) )
737 GtkSheet *var_sheet =
738 GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
740 PsppireVarStore *vs = PSPPIRE_VAR_STORE
741 (gtk_sheet_get_model (var_sheet) );
743 /* This shouldn't be able to happen, because the menuitem
744 should be disabled */
745 g_return_if_fail (var_sheet->state == GTK_SHEET_ROW_SELECTED );
747 psppire_dict_delete_variables (vs->dict,
748 var_sheet->range.row0,
750 var_sheet->range.rowi -
751 var_sheet->range.row0 );
754 case PAGE_DATA_SHEET:
757 g_assert_not_reached ();
762 /* Insert a new variable before the current row in the variable sheet,
763 or before the current column in the data sheet, whichever is selected */
765 insert_variable (GtkCheckMenuItem *m, gpointer data)
767 struct data_editor *de = data;
770 GtkWidget *notebook = get_widget_assert (de->xml, "notebook");
772 GtkSheet *var_sheet =
773 GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
775 PsppireVarStore *vs = PSPPIRE_VAR_STORE
776 (gtk_sheet_get_model (var_sheet) );
778 switch ( gtk_notebook_get_current_page ( GTK_NOTEBOOK (notebook)) )
781 posn = var_sheet->active_cell.row;
783 case PAGE_DATA_SHEET:
785 GtkSheet *data_sheet =
786 GTK_SHEET (get_widget_assert (de->xml, "data_sheet"));
788 if ( data_sheet->state == GTK_SHEET_COLUMN_SELECTED )
789 posn = data_sheet->range.col0;
791 posn = data_sheet->active_cell.col;
795 g_assert_not_reached ();
798 psppire_dict_insert_variable (vs->dict, posn, NULL);
801 /* Callback for when the dictionary changes its split variables */
803 on_split_change (PsppireDict *dict, gpointer data)
805 struct data_editor *de = data;
807 size_t n_split_vars = dict_get_split_cnt (dict->dict);
809 GtkWidget *split_status_area =
810 get_widget_assert (de->xml, "split-file-status-area");
812 if ( n_split_vars == 0 )
814 gtk_label_set_text (GTK_LABEL (split_status_area), _("No Split"));
820 const struct variable *const * split_vars = dict_get_split_vars (dict->dict);
822 text = g_string_new (_("Split by "));
824 for (i = 0 ; i < n_split_vars - 1; ++i )
826 g_string_append_printf (text, "%s, ", var_get_name (split_vars[i]));
828 g_string_append (text, var_get_name (split_vars[i]));
830 gtk_label_set_text (GTK_LABEL (split_status_area), text->str);
832 g_string_free (text, TRUE);
837 /* Callback for when the dictionary changes its filter variable */
839 on_filter_change (GObject *o, gint filter_index, gpointer data)
841 struct data_editor *de = data;
842 GtkWidget *filter_status_area =
843 get_widget_assert (de->xml, "filter-use-status-area");
845 if ( filter_index == -1 )
847 gtk_label_set_text (GTK_LABEL (filter_status_area), _("Filter off"));
851 GtkSheet *var_sheet =
852 GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
854 PsppireVarStore *vs = PSPPIRE_VAR_STORE
855 (gtk_sheet_get_model (var_sheet) );
857 struct variable *var = psppire_dict_get_variable (vs->dict,
860 gchar *text = g_strdup_printf (_("Filter by %s"), var_get_name (var));
862 gtk_label_set_text (GTK_LABEL (filter_status_area), text);
868 /* Callback for when the dictionary changes its weights */
870 on_weight_change (GObject *o, gint weight_index, gpointer data)
872 struct data_editor *de = data;
873 GtkWidget *weight_status_area =
874 get_widget_assert (de->xml, "weight-status-area");
876 if ( weight_index == -1 )
878 gtk_label_set_text (GTK_LABEL (weight_status_area), _("Weights off"));
882 GtkSheet *var_sheet =
883 GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
885 PsppireVarStore *vs = PSPPIRE_VAR_STORE
886 (gtk_sheet_get_model (var_sheet) );
888 struct variable *var = psppire_dict_get_variable (vs->dict,
891 gchar *text = g_strdup_printf (_("Weight by %s"), var_get_name (var));
893 gtk_label_set_text (GTK_LABEL (weight_status_area), text);
902 static void data_save_as_dialog (GtkAction *, struct data_editor *de);
903 static void new_file (GtkAction *, struct editor_window *de);
904 static void open_data_dialog (GtkAction *, struct data_editor *de);
905 static void data_save (GtkAction *action, struct data_editor *e);
908 /* Create the GtkActions and connect to their signals */
910 register_data_editor_actions (struct data_editor *de)
912 de->action_data_open =
913 gtk_action_new ("data-open-dialog",
915 _("Open a data file"),
918 g_signal_connect (de->action_data_open, "activate",
919 G_CALLBACK (open_data_dialog), de);
922 de->action_data_save = gtk_action_new ("data-save",
924 _("Save data to file"),
927 g_signal_connect (de->action_data_save, "activate",
928 G_CALLBACK (data_save), de);
932 de->action_data_save_as = gtk_action_new ("data-save-as-dialog",
934 _("Save data to file"),
937 g_signal_connect (de->action_data_save_as, "activate",
938 G_CALLBACK (data_save_as_dialog), de);
940 de->action_data_new =
941 gtk_action_new ("data-new",
946 g_signal_connect (de->action_data_new, "activate",
947 G_CALLBACK (new_file), de);
950 /* Returns true if NAME has a suffix which might denote a PSPP file */
952 name_has_suffix (const gchar *name)
954 if ( g_str_has_suffix (name, ".sav"))
956 if ( g_str_has_suffix (name, ".SAV"))
958 if ( g_str_has_suffix (name, ".por"))
960 if ( g_str_has_suffix (name, ".POR"))
966 /* Append SUFFIX to the filename of DE */
968 append_filename_suffix (struct data_editor *de, const gchar *suffix)
970 if ( ! name_has_suffix (de->file_name))
972 gchar *s = de->file_name;
973 de->file_name = g_strconcat (de->file_name, suffix, NULL);
978 /* Save DE to file */
980 save_file (struct data_editor *de)
982 struct getl_interface *sss;
983 struct string file_name ;
985 g_assert (de->file_name);
987 ds_init_cstr (&file_name, de->file_name);
988 gen_quoted_string (&file_name);
990 if ( de->save_as_portable )
992 append_filename_suffix (de, ".por");
993 sss = create_syntax_string_source ("EXPORT OUTFILE=%s.",
994 ds_cstr (&file_name));
998 append_filename_suffix (de, ".sav");
999 sss = create_syntax_string_source ("SAVE OUTFILE=%s.",
1000 ds_cstr (&file_name));
1003 ds_destroy (&file_name);
1005 execute_syntax (sss);
1009 /* Callback for data_save action.
1010 If there's an existing file name, then just save,
1011 otherwise prompt for a file name, then save */
1013 data_save (GtkAction *action, struct data_editor *de)
1018 data_save_as_dialog (action, de);
1022 /* Callback for data_save_as action. Prompt for a filename and save */
1024 data_save_as_dialog (GtkAction *action, struct data_editor *de)
1026 struct editor_window *e = (struct editor_window *) de;
1028 GtkWidget *button_sys;
1030 gtk_file_chooser_dialog_new (_("Save"),
1031 GTK_WINDOW (e->window),
1032 GTK_FILE_CHOOSER_ACTION_SAVE,
1033 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1034 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1037 GtkFileFilter *filter = gtk_file_filter_new ();
1038 gtk_file_filter_set_name (filter, _("System Files (*.sav)"));
1039 gtk_file_filter_add_pattern (filter, "*.sav");
1040 gtk_file_filter_add_pattern (filter, "*.SAV");
1041 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
1043 filter = gtk_file_filter_new ();
1044 gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
1045 gtk_file_filter_add_pattern (filter, "*.por");
1046 gtk_file_filter_add_pattern (filter, "*.POR");
1047 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
1049 filter = gtk_file_filter_new ();
1050 gtk_file_filter_set_name (filter, _("All Files"));
1051 gtk_file_filter_add_pattern (filter, "*");
1052 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
1055 GtkWidget *button_por;
1056 GtkWidget *vbox = gtk_vbox_new (TRUE, 5);
1058 gtk_radio_button_new_with_label (NULL, _("System File"));
1061 gtk_radio_button_new_with_label
1062 (gtk_radio_button_get_group (GTK_RADIO_BUTTON(button_sys)),
1063 _("Portable File"));
1065 gtk_box_pack_start_defaults (GTK_BOX (vbox), button_sys);
1066 gtk_box_pack_start_defaults (GTK_BOX (vbox), button_por);
1068 gtk_widget_show_all (vbox);
1070 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER(dialog), vbox);
1073 switch (gtk_dialog_run (GTK_DIALOG (dialog)))
1075 case GTK_RESPONSE_ACCEPT:
1077 g_free (de->file_name);
1080 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1082 de->save_as_portable =
1083 ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_sys));
1087 window_set_name_from_filename (e, de->file_name);
1094 gtk_widget_destroy (dialog);
1098 /* Callback for data_new action.
1099 Performs the NEW FILE command */
1101 new_file (GtkAction *action, struct editor_window *e)
1103 struct data_editor *de = (struct data_editor *) e;
1105 struct getl_interface *sss =
1106 create_syntax_string_source ("NEW FILE.");
1108 execute_syntax (sss);
1110 g_free (de->file_name);
1111 de->file_name = NULL;
1113 default_window_name (e);
1118 open_data_file (const gchar *file_name, struct data_editor *de)
1120 struct getl_interface *sss;
1121 struct string filename;
1123 ds_init_cstr (&filename, file_name);
1125 gen_quoted_string (&filename);
1127 sss = create_syntax_string_source ("GET FILE=%s.",
1128 ds_cstr (&filename));
1130 execute_syntax (sss);
1131 ds_destroy (&filename);
1133 window_set_name_from_filename ((struct editor_window *) de, file_name);
1137 /* Callback for the data_open action.
1138 Prompts for a filename and opens it */
1140 open_data_dialog (GtkAction *action, struct data_editor *de)
1142 struct editor_window *e = (struct editor_window *) de;
1145 gtk_file_chooser_dialog_new (_("Open"),
1146 GTK_WINDOW (e->window),
1147 GTK_FILE_CHOOSER_ACTION_OPEN,
1148 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1149 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1152 GtkFileFilter *filter = gtk_file_filter_new ();
1153 gtk_file_filter_set_name (filter, _("System Files (*.sav)"));
1154 gtk_file_filter_add_pattern (filter, "*.sav");
1155 gtk_file_filter_add_pattern (filter, "*.SAV");
1156 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
1158 filter = gtk_file_filter_new ();
1159 gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
1160 gtk_file_filter_add_pattern (filter, "*.por");
1161 gtk_file_filter_add_pattern (filter, "*.POR");
1162 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
1164 filter = gtk_file_filter_new ();
1165 gtk_file_filter_set_name (filter, _("All Files"));
1166 gtk_file_filter_add_pattern (filter, "*");
1167 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
1169 switch (gtk_dialog_run (GTK_DIALOG (dialog)))
1171 case GTK_RESPONSE_ACCEPT:
1173 g_free (de->file_name);
1175 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1177 open_data_file (de->file_name, de);
1179 #if RECENT_LISTS_AVAILABLE
1181 GtkRecentManager *manager = gtk_recent_manager_get_default();
1182 gchar *uri = g_filename_to_uri (de->file_name, NULL, NULL);
1184 if ( ! gtk_recent_manager_add_item (manager, uri))
1185 g_warning ("Could not add item %s to recent list\n",uri);
1197 gtk_widget_destroy (dialog);