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