Whitespace changes only
[pspp] / src / ui / gui / text-data-import-dialog.c
index 6d7e0777db4ebb27fcb4163dbfed204267fe2a61..34127492fa9393ec03a7a157342cd07d71360fd1 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013  Free Software Foundation
+   Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include "ui/gui/text-data-import-dialog.h"
 
+#include "page-intro.h"
+#include "page-sheet-spec.h"
+#include "page-first-line.h"
+#include "page-separators.h"
+#include "page-formats.h"
+
 #include <errno.h>
 #include <fcntl.h>
-#include <gtk-contrib/psppire-sheet.h>
 #include <gtk/gtk.h>
 #include <limits.h>
 #include <stdlib.h>
 #include "libpspp/i18n.h"
 #include "libpspp/line-reader.h"
 #include "libpspp/message.h"
-#include "ui/gui/checkbox-treeview.h"
 #include "ui/gui/dialog-common.h"
 #include "ui/gui/executor.h"
 #include "ui/gui/helper.h"
 #include "ui/gui/builder-wrapper.h"
+#include "ui/gui/pspp-sheet-selection.h"
+#include "ui/gui/pspp-sheet-view.h"
 #include "ui/gui/psppire-data-window.h"
 #include "ui/gui/psppire-dialog.h"
 #include "ui/gui/psppire-encoding-selector.h"
 #include "ui/gui/psppire-empty-list-store.h"
 #include "ui/gui/psppire-var-sheet.h"
-#include "ui/gui/psppire-var-store.h"
 #include "ui/gui/psppire-scanf.h"
 #include "ui/syntax-gen.h"
 
-#include "gl/error.h"
 #include "gl/intprops.h"
 #include "gl/xalloc.h"
 
@@ -64,7 +68,7 @@ static void apply_dict (const struct dictionary *, struct string *);
 static char *generate_syntax (const struct import_assistant *);
 
 static void add_line_number_column (const struct import_assistant *,
-                                    GtkTreeView *);
+                                    PsppSheetView *);
 
 /* Pops up the Text Data Import assistant. */
 void
@@ -81,28 +85,46 @@ text_data_import_assistant (PsppireDataWindow *dw)
     }
 
   ssp = ia->sheet_spec;
-  if (ssp->spreadsheet == NULL)
+
+  if (ia->spreadsheet)
     {
-      init_first_line_page (ia);
-      init_separators_page (ia);
+      ia->sheet_spec = sheet_spec_page_create (ia);
     }
   else
     {
-      init_sheet_spec_page (ia);
+      ia->intro = intro_page_create (ia);
+      ia->first_line = first_line_page_create (ia);
+      ia->separators = separators_page_create (ia);
     }
-
-  init_formats_page (ia);
+  ia->formats = formats_page_create (ia);
 
   gtk_widget_show_all (GTK_WIDGET (ia->asst.assistant));
 
   ia->asst.main_loop = g_main_loop_new (NULL, false);
-  g_main_loop_run (ia->asst.main_loop);
+
+  {  
+  /*
+    Instead of this block,
+    A simple     g_main_loop_run (ia->asst.main_loop);  should work here.  But it seems to crash.
+    I have no idea why.
+  */
+    GMainContext *ctx = g_main_loop_get_context (ia->asst.main_loop);
+    ia->asst.loop_done = false;
+    while (! ia->asst.loop_done)
+      {
+       g_main_context_iteration (ctx, TRUE);
+      }
+  }
   g_main_loop_unref (ia->asst.main_loop);
 
   switch (ia->asst.response)
     {
     case GTK_RESPONSE_APPLY:
-      free (execute_syntax_string (dw, generate_syntax (ia)));
+      {
+       gchar *fn = g_path_get_basename (ia->file.file_name);
+       open_data_window (PSPPIRE_WINDOW (dw), fn, NULL, generate_syntax (ia));
+       g_free (fn);
+      }
       break;
     case PSPPIRE_RESPONSE_PASTE:
       free (paste_syntax_to_window (generate_syntax (ia)));
@@ -111,15 +133,11 @@ text_data_import_assistant (PsppireDataWindow *dw)
       break;
     }
 
-  if (ssp->spreadsheet == NULL)
+  if (ssp
     {
       destroy_formats_page (ia);
       destroy_separators_page (ia);
     }
-
-  destroy_assistant (ia);
-  destroy_file (ia);
-  free (ia);
 }
 
 /* Emits PSPP syntax to S that applies the dictionary attributes
@@ -138,6 +156,7 @@ apply_dict (const struct dictionary *dict, struct string *s)
       enum val_type type = var_get_type (var);
       int width = var_get_width (var);
       enum measure measure = var_get_measure (var);
+      enum var_role role = var_get_role (var);
       enum alignment alignment = var_get_alignment (var);
       const struct fmt_spec *format = var_get_print_format (var);
 
@@ -188,16 +207,13 @@ apply_dict (const struct dictionary *dict, struct string *s)
                          name, var_get_label (var));
       if (measure != var_default_measure (type))
         syntax_gen_pspp (s, "VARIABLE LEVEL %ss (%ss).\n",
-                         name,
-                         (measure == MEASURE_NOMINAL ? "NOMINAL"
-                          : measure == MEASURE_ORDINAL ? "ORDINAL"
-                          : "SCALE"));
+                         name, measure_to_syntax (measure));
+      if (role != ROLE_INPUT)
+        syntax_gen_pspp (s, "VARIABLE ROLE /%ss %ss.\n",
+                         var_role_to_syntax (role), name);
       if (alignment != var_default_alignment (type))
         syntax_gen_pspp (s, "VARIABLE ALIGNMENT %ss (%ss).\n",
-                         name,
-                         (alignment == ALIGN_LEFT ? "LEFT"
-                          : alignment == ALIGN_CENTRE ? "CENTER"
-                          : "RIGHT"));
+                         name, alignment_to_syntax (alignment));
       if (var_get_display_width (var) != var_default_display_width (width))
         syntax_gen_pspp (s, "VARIABLE WIDTH %ss (%d).\n",
                          name, var_get_display_width (var));
@@ -210,14 +226,10 @@ apply_dict (const struct dictionary *dict, struct string *s)
 static char *
 generate_syntax (const struct import_assistant *ia)
 {
-  struct sheet_spec_page *ssp = ia->sheet_spec;
-
   struct string s = DS_EMPTY_INITIALIZER;
 
-  if (ssp->spreadsheet == NULL)
+  if (ia->spreadsheet == NULL)
     {
-      size_t var_cnt;
-      size_t i;
       syntax_gen_pspp (&s,
                       "GET DATA"
                       "\n  /TYPE=TXT"
@@ -225,96 +237,30 @@ generate_syntax (const struct import_assistant *ia)
                       ia->file.file_name);
       if (ia->file.encoding && strcmp (ia->file.encoding, "Auto"))
        syntax_gen_pspp (&s, "  /ENCODING=%sq\n", ia->file.encoding);
-      if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (
-                                                          ia->intro->n_cases_button)))
-       ds_put_format (&s, "  /IMPORTCASES=FIRST %d\n",
-                      gtk_spin_button_get_value_as_int (
-                                                        GTK_SPIN_BUTTON (ia->intro->n_cases_spin)));
-      else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (
-                                                               ia->intro->percent_button)))
-       ds_put_format (&s, "  /IMPORTCASES=PERCENT %d\n",
-                      gtk_spin_button_get_value_as_int (
-                                                        GTK_SPIN_BUTTON (ia->intro->percent_spin)));
-      else
-       ds_put_cstr (&s, "  /IMPORTCASES=ALL\n");
+
+      intro_append_syntax (ia->intro, &s);
+
+
       ds_put_cstr (&s,
                   "  /ARRANGEMENT=DELIMITED\n"
                   "  /DELCASE=LINE\n");
-      if (ia->first_line->skip_lines > 0)
-       ds_put_format (&s, "  /FIRSTCASE=%d\n", ia->first_line->skip_lines + 1);
-      ds_put_cstr (&s, "  /DELIMITERS=\"");
-      if (ds_find_byte (&ia->separators->separators, '\t') != SIZE_MAX)
-       ds_put_cstr (&s, "\\t");
-      if (ds_find_byte (&ia->separators->separators, '\\') != SIZE_MAX)
-       ds_put_cstr (&s, "\\\\");
-      for (i = 0; i < ds_length (&ia->separators->separators); i++)
-       {
-         char c = ds_at (&ia->separators->separators, i);
-         if (c == '"')
-           ds_put_cstr (&s, "\"\"");
-         else if (c != '\t' && c != '\\')
-           ds_put_byte (&s, c);
-       }
-      ds_put_cstr (&s, "\"\n");
-      if (!ds_is_empty (&ia->separators->quotes))
-       syntax_gen_pspp (&s, "  /QUALIFIER=%sq\n", ds_cstr (&ia->separators->quotes));
-      if (!ds_is_empty (&ia->separators->quotes) && ia->separators->escape)
-       ds_put_cstr (&s, "  /ESCAPE\n");
-      ds_put_cstr (&s, "  /VARIABLES=\n");
-
-      var_cnt = dict_get_var_cnt (ia->formats->dict);
-      for (i = 0; i < var_cnt; i++)
-       {
-         struct variable *var = dict_get_var (ia->formats->dict, i);
-         char format_string[FMT_STRING_LEN_MAX + 1];
-         fmt_to_string (var_get_print_format (var), format_string);
-         ds_put_format (&s, "    %s %s%s\n",
-                        var_get_name (var), format_string,
-                        i == var_cnt - 1 ? "." : "");
-       }
-
-      apply_dict (ia->formats->dict, &s);
+
+      first_line_append_syntax (ia, &s);
+      separators_append_syntax (ia, &s);
+      formats_append_syntax (ia, &s);
+      apply_dict (ia->dict, &s);
     }
   else
     {
-      const struct sheet_spec_page *ssp = ia->sheet_spec;
-
-      printf ("%s:%d %p %d\n", __FILE__, __LINE__, ssp->spreadsheet, ssp->spreadsheet->type);
-
-      syntax_gen_pspp (&s,
-                      "GET DATA"
-                      "\n  /TYPE=%ss"
-                      "\n  /FILE=%sq"
-                      "\n  /SHEET=index %d"
-                      "\n  /READNAMES=%ss",
-                      (ssp->spreadsheet->type == SPREADSHEET_GNUMERIC) ? "GNM" : "ODS",
-                      ia->file.file_name,                       
-                      ssp->opts.sheet_index,
-                      ssp->sri.read_names ? "ON" : "OFF");
-
-
-      if ( ssp->opts.cell_range)
-       {
-         syntax_gen_pspp (&s,
-                          "\n  /CELLRANGE=RANGE %sq",
-                          ssp->opts.cell_range);
-       }
-      else
-       {
-         syntax_gen_pspp (&s,
-                          "\n  /CELLRANGE=FULL");
-       }
-
-
-      syntax_gen_pspp (&s, ".");
+      return sheet_spec_gen_syntax (ia);
     }
-
+  
   return ds_cstr (&s);
 }
 
 
 
-static void render_input_cell (GtkTreeViewColumn *tree_column,
+static void render_input_cell (PsppSheetViewColumn *tree_column,
                                GtkCellRenderer *cell,
                                GtkTreeModel *model, GtkTreeIter *iter,
                                gpointer ia);
@@ -329,7 +275,7 @@ static gboolean on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
 /* Called to render one of the cells in the fields preview tree
    view. */
 static void
-render_input_cell (GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
+render_input_cell (PsppSheetViewColumn *tree_column, GtkCellRenderer *cell,
                    GtkTreeModel *model, GtkTreeIter *iter,
                    gpointer ia_)
 {
@@ -340,8 +286,8 @@ render_input_cell (GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
 
   column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
                                                "column-number"));
-  row = empty_list_store_iter_to_row (iter) + ia->first_line->skip_lines;
-  field = ia->separators->columns[column].contents[row];
+  row = empty_list_store_iter_to_row (iter) + ia->skip_lines;
+  field = ia->columns[column].contents[row];
   if (field.string != NULL)
     {
       GValue text = {0, };
@@ -377,7 +323,7 @@ on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
   if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
     return FALSE;
 
-  if (ia->separators->columns[column].contents[row].string != NULL)
+  if (ia->columns[column].contents[row].string != NULL)
     return FALSE;
 
   gtk_tooltip_set_text (tooltip,
@@ -399,16 +345,15 @@ parse_field (struct import_assistant *ia,
              size_t row, size_t column,
              char **outputp, char **tooltipp)
 {
-  struct substring field;
-  union value val;
-  struct variable *var;
   const struct fmt_spec *in;
   struct fmt_spec out;
   char *tooltip;
   bool ok;
 
-  field = ia->separators->columns[column].contents[row];
-  var = dict_get_var (ia->formats->dict, column);
+  struct substring field = ia->columns[column].contents[row];
+  struct variable *var = dict_get_var (ia->dict, column);
+  union value val;
+
   value_init (&val, var_get_width (var));
   in = var_get_print_format (var);
   out = fmt_for_output_from_input (in);
@@ -418,7 +363,7 @@ parse_field (struct import_assistant *ia,
       char *error;
 
       error = data_in (field, "UTF-8", in->type, &val, var_get_width (var),
-                       dict_get_encoding (ia->formats->dict));
+                       dict_get_encoding (ia->dict));
       if (error != NULL)
         {
           tooltip = xasprintf (_("Cannot parse field content `%.*s' as "
@@ -436,7 +381,7 @@ parse_field (struct import_assistant *ia,
     }
   if (outputp != NULL)
     {
-      *outputp = data_out (&val, dict_get_encoding (ia->formats->dict),  &out);
+      *outputp = data_out (&val, dict_get_encoding (ia->dict),  &out);
     }
   value_destroy (&val, var_get_width (var));
 
@@ -451,7 +396,7 @@ parse_field (struct import_assistant *ia,
 /* Called to render one of the cells in the data preview tree
    view. */
 static void
-render_output_cell (GtkTreeViewColumn *tree_column,
+render_output_cell (PsppSheetViewColumn *tree_column,
                     GtkCellRenderer *cell,
                     GtkTreeModel *model,
                     GtkTreeIter *iter,
@@ -464,7 +409,7 @@ render_output_cell (GtkTreeViewColumn *tree_column,
 
   ok = parse_field (ia,
                     (empty_list_store_iter_to_row (iter)
-                     + ia->first_line->skip_lines),
+                     + ia->skip_lines),
                     GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
                                                         "column-number")),
                     &output, NULL);
@@ -511,11 +456,11 @@ get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
                       const struct import_assistant *ia,
                       size_t *row, size_t *column)
 {
-  GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
+  PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
   gint bx, by;
   GtkTreePath *path;
   GtkTreeIter iter;
-  GtkTreeViewColumn *tree_column;
+  PsppSheetViewColumn *tree_column;
   GtkTreeModel *tree_model;
   bool ok;
 
@@ -532,46 +477,47 @@ get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
   if (!gtk_widget_get_mapped (widget))
     return FALSE;
 
-  gtk_tree_view_convert_widget_to_bin_window_coords (tree_view,
-                                                     wx, wy, &bx, &by);
-  if (!gtk_tree_view_get_path_at_pos (tree_view, bx, by,
+  pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view,
+                                                       wx, wy, &bx, &by);
+  if (!pspp_sheet_view_get_path_at_pos (tree_view, bx, by,
                                       &path, &tree_column, NULL, NULL))
     return FALSE;
 
   *column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
                                                 "column-number"));
 
-  tree_model = gtk_tree_view_get_model (tree_view);
+  tree_model = pspp_sheet_view_get_model (tree_view);
   ok = gtk_tree_model_get_iter (tree_model, &iter, path);
   gtk_tree_path_free (path);
   if (!ok)
     return FALSE;
 
-  *row = empty_list_store_iter_to_row (&iter) + ia->first_line->skip_lines;
+  *row = empty_list_store_iter_to_row (&iter) + ia->skip_lines;
   return TRUE;
 }
 
 void
 make_tree_view (const struct import_assistant *ia,
                 size_t first_line,
-                GtkTreeView **tree_view)
+                PsppSheetView **tree_view)
 {
   GtkTreeModel *model;
 
-  *tree_view = GTK_TREE_VIEW (gtk_tree_view_new ());
+  *tree_view = PSPP_SHEET_VIEW (pspp_sheet_view_new ());
+  pspp_sheet_view_set_grid_lines (*tree_view, PSPP_SHEET_VIEW_GRID_LINES_BOTH);
   model = GTK_TREE_MODEL (psppire_empty_list_store_new (
                                                        ia->file.line_cnt - first_line));
   g_object_set_data (G_OBJECT (model), "lines", ia->file.lines + first_line);
   g_object_set_data (G_OBJECT (model), "first-line",
                      GINT_TO_POINTER (first_line));
-  gtk_tree_view_set_model (*tree_view, model);
+  pspp_sheet_view_set_model (*tree_view, model);
   g_object_unref (model);
 
   add_line_number_column (ia, *tree_view);
 }
 
 static void
-render_line_number (GtkTreeViewColumn *tree_column,
+render_line_number (PsppSheetViewColumn *tree_column,
                     GtkCellRenderer *cell,
                     GtkTreeModel *tree_model,
                     GtkTreeIter *iter,
@@ -589,23 +535,22 @@ render_line_number (GtkTreeViewColumn *tree_column,
 
 static void
 add_line_number_column (const struct import_assistant *ia,
-                        GtkTreeView *treeview)
+                        PsppSheetView *treeview)
 {
-  GtkTreeViewColumn *column;
-
-  column = gtk_tree_view_column_new_with_attributes (
-                                                    _("Line"), ia->asst.prop_renderer, (void *) NULL);
-  gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
-  gtk_tree_view_column_set_fixed_width (
-                                       column, get_monospace_width (treeview, ia->asst.prop_renderer, 5));
-  gtk_tree_view_column_set_resizable (column, TRUE);
-  gtk_tree_view_column_set_cell_data_func (column, ia->asst.prop_renderer,
-                                           render_line_number, NULL, NULL);
-  gtk_tree_view_append_column (treeview, column);
+  PsppSheetViewColumn *column;
+
+  column = pspp_sheet_view_column_new_with_attributes (
+    _("Line"), ia->asst.prop_renderer, (void *) NULL);
+  pspp_sheet_view_column_set_fixed_width (
+    column, get_monospace_width (treeview, ia->asst.prop_renderer, 5));
+  pspp_sheet_view_column_set_resizable (column, TRUE);
+  pspp_sheet_view_column_set_cell_data_func (column, ia->asst.prop_renderer,
+                                             render_line_number, NULL, NULL);
+  pspp_sheet_view_append_column (treeview, column);
 }
 
 gint
-get_monospace_width (GtkTreeView *treeview, GtkCellRenderer *renderer,
+get_monospace_width (PsppSheetView *treeview, GtkCellRenderer *renderer,
                      size_t char_cnt)
 {
   struct string s;
@@ -621,31 +566,31 @@ get_monospace_width (GtkTreeView *treeview, GtkCellRenderer *renderer,
 }
 
 gint
-get_string_width (GtkTreeView *treeview, GtkCellRenderer *renderer,
+get_string_width (PsppSheetView *treeview, GtkCellRenderer *renderer,
                   const char *string)
 {
   gint width;
   g_object_set (G_OBJECT (renderer), "text", string, (void *) NULL);
-  gtk_cell_renderer_get_size (renderer, GTK_WIDGET (treeview),
-                              NULL, NULL, NULL, &width, NULL);
+  gtk_cell_renderer_get_preferred_width (renderer, GTK_WIDGET (treeview),
+                             NULL, &width);
   return width;
 }
 
-GtkTreeViewColumn *
-make_data_column (struct import_assistant *ia, GtkTreeView *tree_view,
+PsppSheetViewColumn *
+make_data_column (struct import_assistant *ia, PsppSheetView *tree_view,
                   bool input, gint dict_idx)
 {
   struct variable *var = NULL;
   struct column *column = NULL;
   size_t char_cnt;
   gint content_width, header_width;
-  GtkTreeViewColumn *tree_column;
+  PsppSheetViewColumn *tree_column;
   char *name;
 
   if (input)
-    column = &ia->separators->columns[dict_idx];
+    column = &ia->columns[dict_idx];
   else
-    var = dict_get_var (ia->formats->dict, dict_idx);
+    var = dict_get_var (ia->dict, dict_idx);
 
   name = escape_underscores (input ? column->name : var_get_name (var));
   char_cnt = input ? column->width : var_get_print_format (var)->w;
@@ -654,44 +599,43 @@ make_data_column (struct import_assistant *ia, GtkTreeView *tree_view,
   header_width = get_string_width (tree_view, ia->asst.prop_renderer,
                                    name);
 
-  tree_column = gtk_tree_view_column_new ();
+  tree_column = pspp_sheet_view_column_new ();
   g_object_set_data (G_OBJECT (tree_column), "column-number",
                      GINT_TO_POINTER (dict_idx));
-  gtk_tree_view_column_set_title (tree_column, name);
-  gtk_tree_view_column_pack_start (tree_column, ia->asst.fixed_renderer,
-                                   FALSE);
-  gtk_tree_view_column_set_cell_data_func (
-                                          tree_column, ia->asst.fixed_renderer,
-                                          input ? render_input_cell : render_output_cell, ia, NULL);
-  gtk_tree_view_column_set_sizing (tree_column, GTK_TREE_VIEW_COLUMN_FIXED);
-  gtk_tree_view_column_set_fixed_width (tree_column, MAX (content_width,
-                                                          header_width));
+  pspp_sheet_view_column_set_title (tree_column, name);
+  pspp_sheet_view_column_pack_start (tree_column, ia->asst.fixed_renderer,
+                                     FALSE);
+  pspp_sheet_view_column_set_cell_data_func (
+    tree_column, ia->asst.fixed_renderer,
+    input ? render_input_cell : render_output_cell, ia, NULL);
+  pspp_sheet_view_column_set_fixed_width (tree_column, MAX (content_width,
+                                                            header_width));
 
   free (name);
 
   return tree_column;
 }
 
-GtkTreeView *
+PsppSheetView *
 create_data_tree_view (bool input, GtkContainer *parent,
                        struct import_assistant *ia)
 {
-  GtkTreeView *tree_view;
+  PsppSheetView *tree_view;
   gint i;
 
-  make_tree_view (ia, ia->first_line->skip_lines, &tree_view);
-  gtk_tree_selection_set_mode (gtk_tree_view_get_selection (tree_view),
-                               GTK_SELECTION_NONE);
+  make_tree_view (ia, ia->skip_lines, &tree_view);
+  pspp_sheet_selection_set_mode (pspp_sheet_view_get_selection (tree_view),
+                                 PSPP_SHEET_SELECTION_NONE);
 
-  for (i = 0; i < ia->separators->column_cnt; i++)
-    gtk_tree_view_append_column (tree_view,
-                                 make_data_column (ia, tree_view, input, i));
+  for (i = 0; i < ia->column_cnt; i++)
+    pspp_sheet_view_append_column (tree_view,
+                                   make_data_column (ia, tree_view, input, i));
 
   g_object_set (G_OBJECT (tree_view), "has-tooltip", TRUE, (void *) NULL);
   g_signal_connect (tree_view, "query-tooltip",
                     G_CALLBACK (input ? on_query_input_tooltip
                                 : on_query_output_tooltip), ia);
-  gtk_tree_view_set_fixed_height_mode (tree_view, true);
+
 
   gtk_container_add (parent, GTK_WIDGET (tree_view));
   gtk_widget_show (GTK_WIDGET (tree_view));
@@ -710,8 +654,8 @@ push_watch_cursor (struct import_assistant *ia)
       GtkWidget *widget = GTK_WIDGET (ia->asst.assistant);
       GdkDisplay *display = gtk_widget_get_display (widget);
       GdkCursor *cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
-      gdk_window_set_cursor (widget->window, cursor);
-      gdk_cursor_unref (cursor);
+      gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
+      g_object_unref (cursor);
       gdk_display_flush (display);
     }
 }
@@ -724,6 +668,6 @@ pop_watch_cursor (struct import_assistant *ia)
   if (--ia->asst.watch_cursor == 0)
     {
       GtkWidget *widget = GTK_WIDGET (ia->asst.assistant);
-      gdk_window_set_cursor (widget->window, NULL);
+      gdk_window_set_cursor (gtk_widget_get_window (widget), NULL);
     }
 }