1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 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/>. */
19 #include "ui/gui/text-data-import-dialog.h"
23 #include <gtk-contrib/psppire-sheet.h>
29 #include "data/data-in.h"
30 #include "data/data-out.h"
31 #include "data/format-guesser.h"
32 #include "data/value-labels.h"
33 #include "language/data-io/data-parser.h"
34 #include "language/lexer/lexer.h"
35 #include "libpspp/assertion.h"
36 #include "libpspp/i18n.h"
37 #include "libpspp/line-reader.h"
38 #include "libpspp/message.h"
39 #include "ui/gui/checkbox-treeview.h"
40 #include "ui/gui/dialog-common.h"
41 #include "ui/gui/executor.h"
42 #include "ui/gui/helper.h"
43 #include "ui/gui/builder-wrapper.h"
44 #include "ui/gui/psppire-data-window.h"
45 #include "ui/gui/psppire-dialog.h"
46 #include "ui/gui/psppire-encoding-selector.h"
47 #include "ui/gui/psppire-empty-list-store.h"
48 #include "ui/gui/psppire-var-sheet.h"
49 #include "ui/gui/psppire-var-store.h"
50 #include "ui/gui/psppire-scanf.h"
51 #include "ui/syntax-gen.h"
54 #include "gl/intprops.h"
55 #include "gl/xalloc.h"
58 #define _(msgid) gettext (msgid)
59 #define N_(msgid) msgid
61 /* Page where the user verifies and adjusts input formats. */
65 GtkTreeView *data_tree_view;
66 PsppireDict *psppire_dict;
67 struct variable **modified_vars;
68 size_t modified_var_cnt;
71 /* The "formats" page of the assistant. */
73 static void on_variable_change (PsppireDict *dict, int idx,
74 struct import_assistant *);
75 static void clear_modified_vars (struct import_assistant *);
77 /* Initializes IA's formats substructure. */
80 formats_page_create (struct import_assistant *ia)
82 GtkBuilder *builder = ia->asst.builder;
83 struct formats_page *p = xzalloc (sizeof *p);
86 p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Formats"),
87 GTK_ASSISTANT_PAGE_CONFIRM);
89 p->data_tree_view = GTK_TREE_VIEW (get_widget_assert (builder, "data"));
90 p->modified_vars = NULL;
91 p->modified_var_cnt = 0;
96 /* Frees IA's formats substructure. */
98 destroy_formats_page (struct import_assistant *ia)
100 struct formats_page *p = ia->formats;
102 if (p->psppire_dict != NULL)
104 dict_destroy (p->psppire_dict->dict);
105 g_object_unref (p->psppire_dict);
107 clear_modified_vars (ia);
110 /* Called just before the formats page of the assistant is
113 prepare_formats_page (struct import_assistant *ia)
115 struct dictionary *dict;
116 PsppireDict *psppire_dict;
117 PsppireVarStore *var_store;
118 GtkBin *vars_scroller;
119 GtkWidget *old_var_sheet;
120 PsppireVarSheet *var_sheet;
121 struct separators_page *seps = ia->separators;
122 struct formats_page *p = ia->formats;
123 struct fmt_guesser *fg;
124 unsigned long int number = 0;
127 push_watch_cursor (ia);
129 dict = dict_create (get_default_encoding ());
130 fg = fmt_guesser_create ();
131 for (column_idx = 0; column_idx < ia->column_cnt; column_idx++)
133 struct variable *modified_var;
135 modified_var = (column_idx < p->modified_var_cnt
136 ? p->modified_vars[column_idx] : NULL);
137 if (modified_var == NULL)
139 struct column *column = &ia->columns[column_idx];
140 struct variable *var;
141 struct fmt_spec format;
145 /* Choose variable name. */
146 name = dict_make_unique_var_name (dict, column->name, &number);
148 /* Choose variable format. */
149 fmt_guesser_clear (fg);
150 for (row = ia->skip_lines; row < ia->file.line_cnt; row++)
151 fmt_guesser_add (fg, column->contents[row]);
152 fmt_guesser_guess (fg, &format);
153 fmt_fix_input (&format);
155 /* Create variable. */
156 var = dict_create_var_assert (dict, name, fmt_var_width (&format));
157 var_set_both_formats (var, &format);
165 name = dict_make_unique_var_name (dict, var_get_name (modified_var),
167 dict_clone_var_as_assert (dict, modified_var, name);
171 fmt_guesser_destroy (fg);
173 psppire_dict = psppire_dict_new_from_dict (dict);
174 g_signal_connect (psppire_dict, "variable_changed",
175 G_CALLBACK (on_variable_change), ia);
177 ia->formats->psppire_dict = psppire_dict;
179 /* XXX: PsppireVarStore doesn't hold a reference to
180 psppire_dict for now, but it should. After it does, we
181 should g_object_ref the psppire_dict here, since we also
182 hold a reference via ia->formats->dict. */
183 var_store = psppire_var_store_new (psppire_dict);
184 g_object_set (var_store,
185 "format-type", PSPPIRE_VAR_STORE_INPUT_FORMATS,
187 var_sheet = PSPPIRE_VAR_SHEET (psppire_var_sheet_new ());
188 g_object_set (var_sheet,
190 "may-create-vars", FALSE,
193 vars_scroller = GTK_BIN (get_widget_assert (ia->asst.builder, "vars-scroller"));
194 old_var_sheet = gtk_bin_get_child (vars_scroller);
195 if (old_var_sheet != NULL)
196 gtk_widget_destroy (old_var_sheet);
197 gtk_container_add (GTK_CONTAINER (vars_scroller), GTK_WIDGET (var_sheet));
198 gtk_widget_show (GTK_WIDGET (var_sheet));
200 gtk_widget_destroy (GTK_WIDGET (ia->formats->data_tree_view));
201 ia->formats->data_tree_view = create_data_tree_view (
203 GTK_CONTAINER (get_widget_assert (ia->asst.builder, "data-scroller")),
206 pop_watch_cursor (ia);
209 /* Clears the set of user-modified variables from IA's formats
210 substructure. This discards user modifications to variable
211 formats, thereby causing formats to revert to their
214 clear_modified_vars (struct import_assistant *ia)
216 struct formats_page *p = ia->formats;
219 for (i = 0; i < p->modified_var_cnt; i++)
220 var_destroy (p->modified_vars[i]);
221 free (p->modified_vars);
222 p->modified_vars = NULL;
223 p->modified_var_cnt = 0;
226 /* Resets the formats page to its defaults, discarding user
229 reset_formats_page (struct import_assistant *ia)
231 clear_modified_vars (ia);
232 prepare_formats_page (ia);
237 /* Called when the user changes one of the variables in the
240 on_variable_change (PsppireDict *dict, int dict_idx,
241 struct import_assistant *ia)
243 struct formats_page *p = ia->formats;
244 GtkTreeView *tv = ia->formats->data_tree_view;
245 gint column_idx = dict_idx + 1;
247 push_watch_cursor (ia);
249 /* Remove previous column and replace with new column. */
250 gtk_tree_view_remove_column (tv, gtk_tree_view_get_column (tv, column_idx));
251 gtk_tree_view_insert_column (tv, make_data_column (ia, tv, false, dict_idx),
254 /* Save a copy of the modified variable in modified_vars, so
255 that its attributes will be preserved if we back up to the
256 previous page with the Prev button and then come back
258 if (dict_idx >= p->modified_var_cnt)
261 p->modified_vars = xnrealloc (p->modified_vars, dict_idx + 1,
262 sizeof *p->modified_vars);
263 for (i = 0; i <= dict_idx; i++)
264 p->modified_vars[i] = NULL;
265 p->modified_var_cnt = dict_idx + 1;
267 if (p->modified_vars[dict_idx])
268 var_destroy (p->modified_vars[dict_idx]);
269 p->modified_vars[dict_idx]
270 = var_clone (psppire_dict_get_variable (dict, dict_idx));
272 pop_watch_cursor (ia);