Started polymorphism of ODS
[pspp] / src / ui / gui / text-data-import-dialog.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 #include "ui/syntax-gen.h"
52
53 #include "gl/error.h"
54 #include "gl/intprops.h"
55 #include "gl/xalloc.h"
56
57 #include "gettext.h"
58 #define _(msgid) gettext (msgid)
59 #define N_(msgid) msgid
60
61 struct import_assistant;
62
63 static void apply_dict (const struct dictionary *, struct string *);
64 static char *generate_syntax (const struct import_assistant *);
65
66 static void add_line_number_column (const struct import_assistant *,
67                                     GtkTreeView *);
68
69 /* Pops up the Text Data Import assistant. */
70 void
71 text_data_import_assistant (PsppireDataWindow *dw)
72 {
73   GtkWindow *parent_window = GTK_WINDOW (dw);
74   struct import_assistant *ia;
75   struct sheet_spec_page *ssp ;
76
77   ia = xzalloc (sizeof *ia);
78   if (!init_file (ia, parent_window))
79     {
80       free (ia);
81       return;
82     }
83
84   init_assistant (ia, parent_window);
85   ssp = &ia->sheet_spec;
86   if (ssp->spreadsheet == NULL)
87     {
88       init_intro_page (ia);
89       init_first_line_page (ia);
90       init_separators_page (ia);
91     }
92   else
93     {
94       init_sheet_spec_page (ia);
95     }
96
97   init_formats_page (ia);
98
99   gtk_widget_show_all (GTK_WIDGET (ia->asst.assistant));
100
101   ia->asst.main_loop = g_main_loop_new (NULL, false);
102   g_main_loop_run (ia->asst.main_loop);
103   g_main_loop_unref (ia->asst.main_loop);
104
105   switch (ia->asst.response)
106     {
107     case GTK_RESPONSE_APPLY:
108       free (execute_syntax_string (dw, generate_syntax (ia)));
109       break;
110     case PSPPIRE_RESPONSE_PASTE:
111       free (paste_syntax_to_window (generate_syntax (ia)));
112       break;
113     default:
114       break;
115     }
116
117   if (ssp->spreadsheet == NULL)
118     {
119       destroy_formats_page (ia);
120       destroy_separators_page (ia);
121     }
122
123   destroy_assistant (ia);
124   destroy_file (ia);
125   free (ia);
126 }
127
128 /* Emits PSPP syntax to S that applies the dictionary attributes
129    (such as missing values and value labels) of the variables in
130    DICT.  */
131 static void
132 apply_dict (const struct dictionary *dict, struct string *s)
133 {
134   size_t var_cnt = dict_get_var_cnt (dict);
135   size_t i;
136
137   for (i = 0; i < var_cnt; i++)
138     {
139       struct variable *var = dict_get_var (dict, i);
140       const char *name = var_get_name (var);
141       enum val_type type = var_get_type (var);
142       int width = var_get_width (var);
143       enum measure measure = var_get_measure (var);
144       enum alignment alignment = var_get_alignment (var);
145       const struct fmt_spec *format = var_get_print_format (var);
146
147       if (var_has_missing_values (var))
148         {
149           const struct missing_values *mv = var_get_missing_values (var);
150           size_t j;
151
152           syntax_gen_pspp (s, "MISSING VALUES %ss (", name);
153           for (j = 0; j < mv_n_values (mv); j++)
154             {
155               if (j)
156                 ds_put_cstr (s, ", ");
157               syntax_gen_value (s, mv_get_value (mv, j), width, format);
158             }
159
160           if (mv_has_range (mv))
161             {
162               double low, high;
163               if (mv_has_value (mv))
164                 ds_put_cstr (s, ", ");
165               mv_get_range (mv, &low, &high);
166               syntax_gen_num_range (s, low, high, format);
167             }
168           ds_put_cstr (s, ").\n");
169         }
170       if (var_has_value_labels (var))
171         {
172           const struct val_labs *vls = var_get_value_labels (var);
173           const struct val_lab **labels = val_labs_sorted (vls);
174           size_t n_labels = val_labs_count (vls);
175           size_t i;
176
177           syntax_gen_pspp (s, "VALUE LABELS %ss", name);
178           for (i = 0; i < n_labels; i++)
179             {
180               const struct val_lab *vl = labels[i];
181               ds_put_cstr (s, "\n  ");
182               syntax_gen_value (s, &vl->value, width, format);
183               ds_put_byte (s, ' ');
184               syntax_gen_string (s, ss_cstr (val_lab_get_escaped_label (vl)));
185             }
186           free (labels);
187           ds_put_cstr (s, ".\n");
188         }
189       if (var_has_label (var))
190         syntax_gen_pspp (s, "VARIABLE LABELS %ss %sq.\n",
191                          name, var_get_label (var));
192       if (measure != var_default_measure (type))
193         syntax_gen_pspp (s, "VARIABLE LEVEL %ss (%ss).\n",
194                          name,
195                          (measure == MEASURE_NOMINAL ? "NOMINAL"
196                           : measure == MEASURE_ORDINAL ? "ORDINAL"
197                           : "SCALE"));
198       if (alignment != var_default_alignment (type))
199         syntax_gen_pspp (s, "VARIABLE ALIGNMENT %ss (%ss).\n",
200                          name,
201                          (alignment == ALIGN_LEFT ? "LEFT"
202                           : alignment == ALIGN_CENTRE ? "CENTER"
203                           : "RIGHT"));
204       if (var_get_display_width (var) != var_default_display_width (width))
205         syntax_gen_pspp (s, "VARIABLE WIDTH %ss (%d).\n",
206                          name, var_get_display_width (var));
207     }
208 }
209
210 /* Generates and returns PSPP syntax to execute the import
211    operation described by IA.  The caller must free the syntax
212    with free(). */
213 static char *
214 generate_syntax (const struct import_assistant *ia)
215 {
216   struct sheet_spec_page *ssp = &ia->sheet_spec;
217
218   struct string s = DS_EMPTY_INITIALIZER;
219
220   if (ssp->spreadsheet == NULL)
221     {
222       size_t var_cnt;
223       size_t i;
224       syntax_gen_pspp (&s,
225                        "GET DATA"
226                        "\n  /TYPE=TXT"
227                        "\n  /FILE=%sq\n",
228                        ia->file.file_name);
229       if (ia->file.encoding && strcmp (ia->file.encoding, "Auto"))
230         syntax_gen_pspp (&s, "  /ENCODING=%sq\n", ia->file.encoding);
231       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (
232                                                            ia->intro.n_cases_button)))
233         ds_put_format (&s, "  /IMPORTCASES=FIRST %d\n",
234                        gtk_spin_button_get_value_as_int (
235                                                          GTK_SPIN_BUTTON (ia->intro.n_cases_spin)));
236       else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (
237                                                                 ia->intro.percent_button)))
238         ds_put_format (&s, "  /IMPORTCASES=PERCENT %d\n",
239                        gtk_spin_button_get_value_as_int (
240                                                          GTK_SPIN_BUTTON (ia->intro.percent_spin)));
241       else
242         ds_put_cstr (&s, "  /IMPORTCASES=ALL\n");
243       ds_put_cstr (&s,
244                    "  /ARRANGEMENT=DELIMITED\n"
245                    "  /DELCASE=LINE\n");
246       if (ia->first_line.skip_lines > 0)
247         ds_put_format (&s, "  /FIRSTCASE=%d\n", ia->first_line.skip_lines + 1);
248       ds_put_cstr (&s, "  /DELIMITERS=\"");
249       if (ds_find_byte (&ia->separators.separators, '\t') != SIZE_MAX)
250         ds_put_cstr (&s, "\\t");
251       if (ds_find_byte (&ia->separators.separators, '\\') != SIZE_MAX)
252         ds_put_cstr (&s, "\\\\");
253       for (i = 0; i < ds_length (&ia->separators.separators); i++)
254         {
255           char c = ds_at (&ia->separators.separators, i);
256           if (c == '"')
257             ds_put_cstr (&s, "\"\"");
258           else if (c != '\t' && c != '\\')
259             ds_put_byte (&s, c);
260         }
261       ds_put_cstr (&s, "\"\n");
262       if (!ds_is_empty (&ia->separators.quotes))
263         syntax_gen_pspp (&s, "  /QUALIFIER=%sq\n", ds_cstr (&ia->separators.quotes));
264       if (!ds_is_empty (&ia->separators.quotes) && ia->separators.escape)
265         ds_put_cstr (&s, "  /ESCAPE\n");
266       ds_put_cstr (&s, "  /VARIABLES=\n");
267
268       var_cnt = dict_get_var_cnt (ia->formats.dict);
269       for (i = 0; i < var_cnt; i++)
270         {
271           struct variable *var = dict_get_var (ia->formats.dict, i);
272           char format_string[FMT_STRING_LEN_MAX + 1];
273           fmt_to_string (var_get_print_format (var), format_string);
274           ds_put_format (&s, "    %s %s%s\n",
275                          var_get_name (var), format_string,
276                          i == var_cnt - 1 ? "." : "");
277         }
278
279       apply_dict (ia->formats.dict, &s);
280     }
281   else
282     {
283       const struct sheet_spec_page *ssp = &ia->sheet_spec;
284
285       printf ("%s:%d %p %d\n", __FILE__, __LINE__, ssp->spreadsheet, ssp->spreadsheet->type);
286
287       syntax_gen_pspp (&s,
288                        "GET DATA"
289                        "\n  /TYPE=%ss"
290                        "\n  /FILE=%sq"
291                        "\n  /SHEET=index %d"
292                        "\n  /READNAMES=%ss",
293                        (ssp->spreadsheet->type == SPREADSHEET_GNUMERIC) ? "GNM" : "ODS",
294                        ia->file.file_name,                       
295                        ssp->opts.sheet_index,
296                        ssp->sri.read_names ? "ON" : "OFF");
297
298
299       if ( ssp->opts.cell_range)
300         {
301           syntax_gen_pspp (&s,
302                            "\n  /CELLRANGE=RANGE %sq",
303                            ssp->opts.cell_range);
304         }
305       else
306         {
307           syntax_gen_pspp (&s,
308                            "\n  /CELLRANGE=FULL");
309         }
310
311
312       syntax_gen_pspp (&s, ".");
313     }
314
315   return ds_cstr (&s);
316 }
317
318
319
320 static void render_input_cell (GtkTreeViewColumn *tree_column,
321                                GtkCellRenderer *cell,
322                                GtkTreeModel *model, GtkTreeIter *iter,
323                                gpointer ia);
324
325 static gboolean on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
326                                         gboolean keyboard_mode UNUSED,
327                                         GtkTooltip *tooltip,
328                                         struct import_assistant *);
329
330
331
332 /* Called to render one of the cells in the fields preview tree
333    view. */
334 static void
335 render_input_cell (GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
336                    GtkTreeModel *model, GtkTreeIter *iter,
337                    gpointer ia_)
338 {
339   struct import_assistant *ia = ia_;
340   struct substring field;
341   size_t row;
342   gint column;
343
344   column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
345                                                "column-number"));
346   row = empty_list_store_iter_to_row (iter) + ia->first_line.skip_lines;
347   field = ia->separators.columns[column].contents[row];
348   if (field.string != NULL)
349     {
350       GValue text = {0, };
351       g_value_init (&text, G_TYPE_STRING);
352       g_value_take_string (&text, ss_xstrdup (field));
353       g_object_set_property (G_OBJECT (cell), "text", &text);
354       g_value_unset (&text);
355       g_object_set (cell, "background-set", FALSE, (void *) NULL);
356     }
357   else
358     g_object_set (cell,
359                   "text", "",
360                   "background", "red",
361                   "background-set", TRUE,
362                   (void *) NULL);
363 }
364
365 static gboolean
366 get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
367                       const struct import_assistant *ia,
368                       size_t *row, size_t *column);
369
370
371 /* Called to render a tooltip on one of the cells in the fields
372    preview tree view. */
373 static gboolean
374 on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
375                         gboolean keyboard_mode UNUSED,
376                         GtkTooltip *tooltip, struct import_assistant *ia)
377 {
378   size_t row, column;
379
380   if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
381     return FALSE;
382
383   if (ia->separators.columns[column].contents[row].string != NULL)
384     return FALSE;
385
386   gtk_tooltip_set_text (tooltip,
387                         _("This input line has too few separators "
388                           "to fill in this field."));
389   return TRUE;
390 }
391
392
393 /* Parses the contents of the field at (ROW,COLUMN) according to
394    its variable format.  If OUTPUTP is non-null, then *OUTPUTP
395    receives the formatted output for that field (which must be
396    freed with free).  If TOOLTIPP is non-null, then *TOOLTIPP
397    receives a message suitable for use in a tooltip, if one is
398    needed, or a null pointer otherwise.  Returns true if a
399    tooltip message is needed, otherwise false. */
400 static bool
401 parse_field (struct import_assistant *ia,
402              size_t row, size_t column,
403              char **outputp, char **tooltipp)
404 {
405   struct substring field;
406   union value val;
407   struct variable *var;
408   const struct fmt_spec *in;
409   struct fmt_spec out;
410   char *tooltip;
411   bool ok;
412
413   field = ia->separators.columns[column].contents[row];
414   var = dict_get_var (ia->formats.dict, column);
415   value_init (&val, var_get_width (var));
416   in = var_get_print_format (var);
417   out = fmt_for_output_from_input (in);
418   tooltip = NULL;
419   if (field.string != NULL)
420     {
421       char *error;
422
423       error = data_in (field, "UTF-8", in->type, &val, var_get_width (var),
424                        dict_get_encoding (ia->formats.dict));
425       if (error != NULL)
426         {
427           tooltip = xasprintf (_("Cannot parse field content `%.*s' as "
428                                  "format %s: %s"),
429                                (int) field.length, field.string,
430                                fmt_name (in->type), error);
431           free (error);
432         }
433     }
434   else
435     {
436       tooltip = xstrdup (_("This input line has too few separators "
437                            "to fill in this field."));
438       value_set_missing (&val, var_get_width (var));
439     }
440   if (outputp != NULL)
441     {
442       *outputp = data_out (&val, dict_get_encoding (ia->formats.dict),  &out);
443     }
444   value_destroy (&val, var_get_width (var));
445
446   ok = tooltip == NULL;
447   if (tooltipp != NULL)
448     *tooltipp = tooltip;
449   else
450     free (tooltip);
451   return ok;
452 }
453
454 /* Called to render one of the cells in the data preview tree
455    view. */
456 static void
457 render_output_cell (GtkTreeViewColumn *tree_column,
458                     GtkCellRenderer *cell,
459                     GtkTreeModel *model,
460                     GtkTreeIter *iter,
461                     gpointer ia_)
462 {
463   struct import_assistant *ia = ia_;
464   char *output;
465   GValue gvalue = { 0, };
466   bool ok;
467
468   ok = parse_field (ia,
469                     (empty_list_store_iter_to_row (iter)
470                      + ia->first_line.skip_lines),
471                     GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
472                                                         "column-number")),
473                     &output, NULL);
474
475   g_value_init (&gvalue, G_TYPE_STRING);
476   g_value_take_string (&gvalue, output);
477   g_object_set_property (G_OBJECT (cell), "text", &gvalue);
478   g_value_unset (&gvalue);
479
480   if (ok)
481     g_object_set (cell, "background-set", FALSE, (void *) NULL);
482   else
483     g_object_set (cell,
484                   "background", "red",
485                   "background-set", TRUE,
486                   (void *) NULL);
487 }
488
489 /* Called to render a tooltip for one of the cells in the data
490    preview tree view. */
491 static gboolean
492 on_query_output_tooltip (GtkWidget *widget, gint wx, gint wy,
493                          gboolean keyboard_mode UNUSED,
494                          GtkTooltip *tooltip, struct import_assistant *ia)
495 {
496   size_t row, column;
497   char *text;
498
499   if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
500     return FALSE;
501
502   if (parse_field (ia, row, column, NULL, &text))
503     return FALSE;
504
505   gtk_tooltip_set_text (tooltip, text);
506   free (text);
507   return TRUE;
508 }
509 \f
510 /* Utility functions used by multiple pages of the assistant. */
511
512 static gboolean
513 get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
514                       const struct import_assistant *ia,
515                       size_t *row, size_t *column)
516 {
517   GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
518   gint bx, by;
519   GtkTreePath *path;
520   GtkTreeIter iter;
521   GtkTreeViewColumn *tree_column;
522   GtkTreeModel *tree_model;
523   bool ok;
524
525   /* Check that WIDGET is really visible on the screen before we
526      do anything else.  This is a bug fix for a sticky situation:
527      when text_data_import_assistant() returns, it frees the data
528      necessary to compose the tool tip message, but there may be
529      a tool tip under preparation at that point (even if there is
530      no visible tool tip) that will call back into us a little
531      bit later.  Perhaps the correct solution to this problem is
532      to make the data related to the tool tips part of a GObject
533      that only gets destroyed when all references are released,
534      but this solution appears to be effective too. */
535   if (!gtk_widget_get_mapped (widget))
536     return FALSE;
537
538   gtk_tree_view_convert_widget_to_bin_window_coords (tree_view,
539                                                      wx, wy, &bx, &by);
540   if (!gtk_tree_view_get_path_at_pos (tree_view, bx, by,
541                                       &path, &tree_column, NULL, NULL))
542     return FALSE;
543
544   *column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
545                                                 "column-number"));
546
547   tree_model = gtk_tree_view_get_model (tree_view);
548   ok = gtk_tree_model_get_iter (tree_model, &iter, path);
549   gtk_tree_path_free (path);
550   if (!ok)
551     return FALSE;
552
553   *row = empty_list_store_iter_to_row (&iter) + ia->first_line.skip_lines;
554   return TRUE;
555 }
556
557 void
558 make_tree_view (const struct import_assistant *ia,
559                 size_t first_line,
560                 GtkTreeView **tree_view)
561 {
562   GtkTreeModel *model;
563
564   *tree_view = GTK_TREE_VIEW (gtk_tree_view_new ());
565   model = GTK_TREE_MODEL (psppire_empty_list_store_new (
566                                                         ia->file.line_cnt - first_line));
567   g_object_set_data (G_OBJECT (model), "lines", ia->file.lines + first_line);
568   g_object_set_data (G_OBJECT (model), "first-line",
569                      GINT_TO_POINTER (first_line));
570   gtk_tree_view_set_model (*tree_view, model);
571   g_object_unref (model);
572
573   add_line_number_column (ia, *tree_view);
574 }
575
576 static void
577 render_line_number (GtkTreeViewColumn *tree_column,
578                     GtkCellRenderer *cell,
579                     GtkTreeModel *tree_model,
580                     GtkTreeIter *iter,
581                     gpointer data)
582 {
583   gint row = empty_list_store_iter_to_row (iter);
584   char s[INT_BUFSIZE_BOUND (int)];
585   int first_line;
586
587   first_line = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_model),
588                                                    "first-line"));
589   sprintf (s, "%d", first_line + row);
590   g_object_set (cell, "text", s, NULL);
591 }
592
593 static void
594 add_line_number_column (const struct import_assistant *ia,
595                         GtkTreeView *treeview)
596 {
597   GtkTreeViewColumn *column;
598
599   column = gtk_tree_view_column_new_with_attributes (
600                                                      _("Line"), ia->asst.prop_renderer, (void *) NULL);
601   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
602   gtk_tree_view_column_set_fixed_width (
603                                         column, get_monospace_width (treeview, ia->asst.prop_renderer, 5));
604   gtk_tree_view_column_set_resizable (column, TRUE);
605   gtk_tree_view_column_set_cell_data_func (column, ia->asst.prop_renderer,
606                                            render_line_number, NULL, NULL);
607   gtk_tree_view_append_column (treeview, column);
608 }
609
610 gint
611 get_monospace_width (GtkTreeView *treeview, GtkCellRenderer *renderer,
612                      size_t char_cnt)
613 {
614   struct string s;
615   gint width;
616
617   ds_init_empty (&s);
618   ds_put_byte_multiple (&s, '0', char_cnt);
619   ds_put_byte (&s, ' ');
620   width = get_string_width (treeview, renderer, ds_cstr (&s));
621   ds_destroy (&s);
622
623   return width;
624 }
625
626 gint
627 get_string_width (GtkTreeView *treeview, GtkCellRenderer *renderer,
628                   const char *string)
629 {
630   gint width;
631   g_object_set (G_OBJECT (renderer), "text", string, (void *) NULL);
632   gtk_cell_renderer_get_size (renderer, GTK_WIDGET (treeview),
633                               NULL, NULL, NULL, &width, NULL);
634   return width;
635 }
636
637 GtkTreeViewColumn *
638 make_data_column (struct import_assistant *ia, GtkTreeView *tree_view,
639                   bool input, gint dict_idx)
640 {
641   struct variable *var = NULL;
642   struct column *column = NULL;
643   size_t char_cnt;
644   gint content_width, header_width;
645   GtkTreeViewColumn *tree_column;
646   char *name;
647
648   if (input)
649     column = &ia->separators.columns[dict_idx];
650   else
651     var = dict_get_var (ia->formats.dict, dict_idx);
652
653   name = escape_underscores (input ? column->name : var_get_name (var));
654   char_cnt = input ? column->width : var_get_print_format (var)->w;
655   content_width = get_monospace_width (tree_view, ia->asst.fixed_renderer,
656                                        char_cnt);
657   header_width = get_string_width (tree_view, ia->asst.prop_renderer,
658                                    name);
659
660   tree_column = gtk_tree_view_column_new ();
661   g_object_set_data (G_OBJECT (tree_column), "column-number",
662                      GINT_TO_POINTER (dict_idx));
663   gtk_tree_view_column_set_title (tree_column, name);
664   gtk_tree_view_column_pack_start (tree_column, ia->asst.fixed_renderer,
665                                    FALSE);
666   gtk_tree_view_column_set_cell_data_func (
667                                            tree_column, ia->asst.fixed_renderer,
668                                            input ? render_input_cell : render_output_cell, ia, NULL);
669   gtk_tree_view_column_set_sizing (tree_column, GTK_TREE_VIEW_COLUMN_FIXED);
670   gtk_tree_view_column_set_fixed_width (tree_column, MAX (content_width,
671                                                           header_width));
672
673   free (name);
674
675   return tree_column;
676 }
677
678 GtkTreeView *
679 create_data_tree_view (bool input, GtkContainer *parent,
680                        struct import_assistant *ia)
681 {
682   GtkTreeView *tree_view;
683   gint i;
684
685   make_tree_view (ia, ia->first_line.skip_lines, &tree_view);
686   gtk_tree_selection_set_mode (gtk_tree_view_get_selection (tree_view),
687                                GTK_SELECTION_NONE);
688
689   for (i = 0; i < ia->separators.column_cnt; i++)
690     gtk_tree_view_append_column (tree_view,
691                                  make_data_column (ia, tree_view, input, i));
692
693   g_object_set (G_OBJECT (tree_view), "has-tooltip", TRUE, (void *) NULL);
694   g_signal_connect (tree_view, "query-tooltip",
695                     G_CALLBACK (input ? on_query_input_tooltip
696                                 : on_query_output_tooltip), ia);
697   gtk_tree_view_set_fixed_height_mode (tree_view, true);
698
699   gtk_container_add (parent, GTK_WIDGET (tree_view));
700   gtk_widget_show (GTK_WIDGET (tree_view));
701
702   return tree_view;
703 }
704
705 /* Increments the "watch cursor" level, setting the cursor for
706    the assistant window to a watch face to indicate to the user
707    that the ongoing operation may take some time. */
708 void
709 push_watch_cursor (struct import_assistant *ia)
710 {
711   if (++ia->asst.watch_cursor == 1)
712     {
713       GtkWidget *widget = GTK_WIDGET (ia->asst.assistant);
714       GdkDisplay *display = gtk_widget_get_display (widget);
715       GdkCursor *cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
716       gdk_window_set_cursor (widget->window, cursor);
717       gdk_cursor_unref (cursor);
718       gdk_display_flush (display);
719     }
720 }
721
722 /* Decrements the "watch cursor" level.  If the level reaches
723    zero, the cursor is reset to its default shape. */
724 void
725 pop_watch_cursor (struct import_assistant *ia)
726 {
727   if (--ia->asst.watch_cursor == 0)
728     {
729       GtkWidget *widget = GTK_WIDGET (ia->asst.assistant);
730       gdk_window_set_cursor (widget->window, NULL);
731     }
732 }