Merge remote branch 'origin/master' into import-gui
[pspp] / src / ui / gui / page-formats.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "ui/gui/text-data-import-dialog.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <gtk/gtk.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
27
28 #include "data/data-in.h"
29 #include "data/data-out.h"
30 #include "data/format-guesser.h"
31 #include "data/value-labels.h"
32 #include "language/data-io/data-parser.h"
33 #include "language/lexer/lexer.h"
34 #include "libpspp/assertion.h"
35 #include "libpspp/i18n.h"
36 #include "libpspp/line-reader.h"
37 #include "libpspp/message.h"
38 #include "ui/gui/checkbox-treeview.h"
39 #include "ui/gui/dialog-common.h"
40 #include "ui/gui/executor.h"
41 #include "ui/gui/helper.h"
42 #include "ui/gui/builder-wrapper.h"
43 #include "ui/gui/psppire-data-window.h"
44 #include "ui/gui/psppire-dialog.h"
45 #include "ui/gui/psppire-encoding-selector.h"
46 #include "ui/gui/psppire-empty-list-store.h"
47 #include "ui/gui/psppire-var-sheet.h"
48 #include "ui/gui/psppire-scanf.h"
49
50 #include "gl/error.h"
51 #include "gl/intprops.h"
52 #include "gl/xalloc.h"
53
54 #include "gettext.h"
55 #define _(msgid) gettext (msgid)
56 #define N_(msgid) msgid
57
58 /* Page where the user verifies and adjusts input formats. */
59 struct formats_page
60   {
61     GtkWidget *page;
62     PsppSheetView *data_tree_view;
63     PsppireDict *psppire_dict;
64     struct variable **modified_vars;
65     size_t modified_var_cnt;
66   };
67
68 /* The "formats" page of the assistant. */
69
70 static void on_variable_change (PsppireDict *dict, int idx,
71                                 struct import_assistant *);
72 static void clear_modified_vars (struct import_assistant *);
73
74 /* Initializes IA's formats substructure. */
75
76 struct formats_page *
77 formats_page_create (struct import_assistant *ia)
78 {
79   GtkBuilder *builder = ia->asst.builder;
80   struct formats_page *p = xzalloc (sizeof *p);
81
82   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Formats"),
83                          GTK_ASSISTANT_PAGE_CONFIRM);
84
85   p->data_tree_view = PSPP_SHEET_VIEW (get_widget_assert (builder, "data"));
86   p->modified_vars = NULL;
87   p->modified_var_cnt = 0;
88
89   return p;
90 }
91
92 /* Frees IA's formats substructure. */
93 void
94 destroy_formats_page (struct import_assistant *ia)
95 {
96   struct formats_page *p = ia->formats;
97
98   if (p->psppire_dict != NULL)
99     {
100       dict_destroy (p->psppire_dict->dict);
101       g_object_unref (p->psppire_dict);
102     }
103   clear_modified_vars (ia);
104 }
105
106 /* Called just before the formats page of the assistant is
107    displayed. */
108 void
109 prepare_formats_page (struct import_assistant *ia)
110 {
111   struct dictionary *dict;
112   PsppireDict *psppire_dict;
113   GtkBin *vars_scroller;
114   GtkWidget *old_var_sheet;
115   PsppireVarSheet *var_sheet;
116   struct formats_page *p = ia->formats;
117   struct fmt_guesser *fg;
118   unsigned long int number = 0;
119   size_t column_idx;
120
121   push_watch_cursor (ia);
122
123   dict = dict_create (get_default_encoding ());
124   fg = fmt_guesser_create ();
125   for (column_idx = 0; column_idx < ia->column_cnt; column_idx++)
126     {
127       struct variable *modified_var = 
128         (column_idx < p->modified_var_cnt ? p->modified_vars[column_idx] : NULL);
129       if (modified_var == NULL)
130         {
131           struct column *column = &ia->columns[column_idx];
132           struct variable *var;
133           struct fmt_spec format;
134           char *name;
135           size_t row;
136
137           /* Choose variable name. */
138           name = dict_make_unique_var_name (dict, column->name, &number);
139
140           /* Choose variable format. */
141           fmt_guesser_clear (fg);
142           for (row = ia->skip_lines; row < ia->file.line_cnt; row++)
143             fmt_guesser_add (fg, column->contents[row]);
144           fmt_guesser_guess (fg, &format);
145           fmt_fix_input (&format);
146
147           /* Create variable. */
148           var = dict_create_var_assert (dict, name, fmt_var_width (&format));
149           var_set_both_formats (var, &format);
150
151           free (name);
152         }
153       else
154         {
155           char *name;
156
157           name = dict_make_unique_var_name (dict, var_get_name (modified_var),
158                                             &number);
159           dict_clone_var_as_assert (dict, modified_var, name);
160           free (name);
161         }
162     }
163   fmt_guesser_destroy (fg);
164
165   psppire_dict = psppire_dict_new_from_dict (dict);
166   g_signal_connect (psppire_dict, "variable_changed",
167                     G_CALLBACK (on_variable_change), ia);
168   ia->dict = dict;
169   ia->formats->psppire_dict = psppire_dict;
170
171   /* XXX: PsppireVarStore doesn't hold a reference to
172      psppire_dict for now, but it should.  After it does, we
173      should g_object_ref the psppire_dict here, since we also
174      hold a reference via ia->formats->dict. */
175   var_sheet = PSPPIRE_VAR_SHEET (psppire_var_sheet_new ());
176   g_object_set (var_sheet,
177                 "dictionary", psppire_dict,
178                 "may-create-vars", FALSE,
179                 "may-delete-vars", FALSE,
180                 "format-use", FMT_FOR_INPUT,
181                 "enable-grid-lines", PSPP_SHEET_VIEW_GRID_LINES_BOTH,
182                 (void *) NULL);
183
184   vars_scroller = GTK_BIN (get_widget_assert (ia->asst.builder, "vars-scroller"));
185   old_var_sheet = gtk_bin_get_child (vars_scroller);
186   if (old_var_sheet != NULL)
187     gtk_widget_destroy (old_var_sheet);
188   gtk_container_add (GTK_CONTAINER (vars_scroller), GTK_WIDGET (var_sheet));
189   gtk_widget_show (GTK_WIDGET (var_sheet));
190
191   gtk_widget_destroy (GTK_WIDGET (ia->formats->data_tree_view));
192   ia->formats->data_tree_view = create_data_tree_view (
193     false,
194     GTK_CONTAINER (get_widget_assert (ia->asst.builder, "data-scroller")),
195     ia);
196
197   gtk_widget_show (ia->asst.paste_button);
198
199   pop_watch_cursor (ia);
200 }
201
202 /* Clears the set of user-modified variables from IA's formats
203    substructure.  This discards user modifications to variable
204    formats, thereby causing formats to revert to their
205    defaults.  */
206 static void
207 clear_modified_vars (struct import_assistant *ia)
208 {
209   struct formats_page *p = ia->formats;
210   size_t i;
211
212   for (i = 0; i < p->modified_var_cnt; i++)
213     var_destroy (p->modified_vars[i]);
214   free (p->modified_vars);
215   p->modified_vars = NULL;
216   p->modified_var_cnt = 0;
217 }
218
219 /* Resets the formats page to its defaults, discarding user
220    modifications. */
221 void
222 reset_formats_page (struct import_assistant *ia)
223 {
224   clear_modified_vars (ia);
225   prepare_formats_page (ia);
226 }
227
228
229
230 /* Called when the user changes one of the variables in the
231    dictionary. */
232 static void
233 on_variable_change (PsppireDict *dict, int dict_idx,
234                     struct import_assistant *ia)
235 {
236   struct formats_page *p = ia->formats;
237   PsppSheetView *tv = ia->formats->data_tree_view;
238   gint column_idx = dict_idx + 1;
239
240   push_watch_cursor (ia);
241
242   /* Remove previous column and replace with new column. */
243   pspp_sheet_view_remove_column (tv, pspp_sheet_view_get_column (tv, column_idx));
244   pspp_sheet_view_insert_column (tv, make_data_column (ia, tv, false, dict_idx),
245                                  column_idx);
246
247   /* Save a copy of the modified variable in modified_vars, so
248      that its attributes will be preserved if we back up to the
249      previous page with the Prev button and then come back
250      here. */
251   if (dict_idx >= p->modified_var_cnt)
252     {
253       size_t i;
254       p->modified_vars = xnrealloc (p->modified_vars, dict_idx + 1,
255                                     sizeof *p->modified_vars);
256       for (i = 0; i <= dict_idx; i++)
257         p->modified_vars[i] = NULL;
258       p->modified_var_cnt = dict_idx + 1;
259     }
260   if (p->modified_vars[dict_idx])
261     var_destroy (p->modified_vars[dict_idx]);
262   p->modified_vars[dict_idx]
263     = var_clone (psppire_dict_get_variable (dict, dict_idx));
264
265   pop_watch_cursor (ia);
266 }
267
268
269
270
271 void
272 formats_append_syntax (const struct import_assistant *ia, struct string *s)
273 {
274   int i;
275   int var_cnt;
276   ds_put_cstr (s, "  /VARIABLES=\n");
277   
278   var_cnt = dict_get_var_cnt (ia->dict);
279   for (i = 0; i < var_cnt; i++)
280     {
281       struct variable *var = dict_get_var (ia->dict, i);
282       char format_string[FMT_STRING_LEN_MAX + 1];
283       fmt_to_string (var_get_print_format (var), format_string);
284       ds_put_format (s, "    %s %s%s\n",
285                      var_get_name (var), format_string,
286                      i == var_cnt - 1 ? "." : "");
287     }
288 }