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