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