Merge remote-tracking branch 'origin/master' into sheet
[pspp] / src / ui / gui / psppire-import-assistant.c
index 98219fd6f497a9237934fe8c1b521b27ca5de5a0..9374f7f68e15fbb2ecc4d4c94e30ab3deddeeb26 100644 (file)
 
 #include "builder-wrapper.h"
 #include "helper.h"
-#include "pspp-sheet-view.h"
-#include "pspp-sheet-selection.h"
 #include "psppire-import-assistant.h"
 #include "psppire-scanf.h"
 #include "psppire-dialog.h"
 #include "psppire-empty-list-store.h"
 #include "psppire-encoding-selector.h"
 #include "psppire-spreadsheet-model.h"
-#include "psppire-var-sheet.h"
 #include "ui/syntax-gen.h"
 
 #include <gettext.h>
@@ -201,6 +198,7 @@ on_paste (GtkButton *button, PsppireImportAssistant *ia)
   close_assistant (ia, PSPPIRE_RESPONSE_PASTE);
 }
 
+
 /* Revises the contents of the fields tree view based on the
    currently chosen set of separators. */
 static void
@@ -598,6 +596,8 @@ process_file (PsppireImportAssistant *ia)
   return TRUE;
 }
 
+#if SHEET_MERGE
+
 
 static void
 render_line_number (PsppSheetViewColumn *tree_column,
@@ -615,7 +615,6 @@ render_line_number (PsppSheetViewColumn *tree_column,
 }
 
 
-
 static gint
 get_string_width (GtkWidget *treeview, GtkCellRenderer *renderer,
                  const char *string)
@@ -687,6 +686,8 @@ make_tree_view (const PsppireImportAssistant *ia)
   return tree_view;
 }
 
+#endif
+
 static GtkWidget *
 add_page_to_assistant (PsppireImportAssistant *ia,
                       GtkWidget *page, GtkAssistantPageType type, const gchar *);
@@ -724,7 +725,6 @@ prepare_sheet_spec_page (PsppireImportAssistant *ia)
 }
 
 
-
 /* Initializes IA's sheet_spec substructure. */
 static void
 sheet_spec_page_create (PsppireImportAssistant *ia)
@@ -748,7 +748,6 @@ sheet_spec_page_create (PsppireImportAssistant *ia)
   g_object_set_data (G_OBJECT (page), "on-entering", prepare_sheet_spec_page);
 }
 
-
 static void 
 on_chosen (PsppireImportAssistant *ia, GtkWidget *page)
 {
@@ -803,6 +802,8 @@ on_map (PsppireImportAssistant *ia, GtkWidget *page)
   on_chosen (ia, page);
 }
 
+
+
 static void
 chooser_page_enter (PsppireImportAssistant *ia, GtkWidget *page)
 {
@@ -835,6 +836,8 @@ chooser_page_reset (PsppireImportAssistant *ia, GtkWidget *page)
   on_chosen (ia, page);
 }
 
+
+
 static void
 chooser_page_create (PsppireImportAssistant *ia)
 {
@@ -909,6 +912,7 @@ chooser_page_create (PsppireImportAssistant *ia)
 }
 
 
+
 static void
 psppire_import_assistant_init (PsppireImportAssistant *ia)
 {
@@ -990,6 +994,8 @@ on_intro_amount_changed (PsppireImportAssistant *p)
 }
 
 
+#if SHEET_MERGE
+
 static void
 render_line (PsppSheetViewColumn *tree_column,
              GtkCellRenderer *cell,
@@ -1006,17 +1012,19 @@ render_line (PsppSheetViewColumn *tree_column,
   g_object_set (cell, "text", ds_cstr (&lines[row]), NULL);
 }
 
+#endif
+
 /* Sets the widgets to match IA's first_line substructure. */
 static void
 set_first_line (PsppireImportAssistant *ia)
 {
   GtkTreePath *path = gtk_tree_path_new_from_indices (ia->skip_lines, -1);
 
-  
+#if SHEET_MERGE  
   set_model_on_treeview (ia, ia->tree_view, 0);
-
   pspp_sheet_view_set_cursor (PSPP_SHEET_VIEW (ia->tree_view),
                              path, NULL, false);
+#endif  
   gtk_tree_path_free (path);
 
   gtk_toggle_button_set_active (
@@ -1026,6 +1034,7 @@ set_first_line (PsppireImportAssistant *ia)
                             ia->skip_lines > 0);
 }
 
+#if SHEET_MERGE
 
 /* Creates and returns a tree view that contains each of the
    lines in IA's file as a row. */
@@ -1071,6 +1080,7 @@ create_lines_tree_view (GtkContainer *parent, PsppireImportAssistant *ia)
 }
 
 
+
 /* Sets IA's first_line substructure to match the widgets. */
 static void
 set_first_line_options (PsppireImportAssistant *ia)
@@ -1094,6 +1104,8 @@ set_first_line_options (PsppireImportAssistant *ia)
   gtk_widget_set_sensitive (ia->variable_names_cb, ia->skip_lines > 0);
 }
 
+
+
 static void
 reset_first_line_page (PsppireImportAssistant *ia)
 {
@@ -1103,7 +1115,7 @@ reset_first_line_page (PsppireImportAssistant *ia)
   gtk_widget_set_sensitive (ia->variable_names_cb, FALSE);
 }
 
-
+#endif
 /* Initializes IA's first_line substructure. */
 static void
 first_line_page_create (PsppireImportAssistant *ia)
@@ -1115,6 +1127,8 @@ first_line_page_create (PsppireImportAssistant *ia)
   add_page_to_assistant (ia, w,
                         GTK_ASSISTANT_PAGE_CONTENT, _("Select the First Line"));
 
+#if SHEET_MERGE
+  
   ia->tree_view = GTK_WIDGET (create_lines_tree_view (
                                                      GTK_CONTAINER (get_widget_assert (ia->builder, "first-line-scroller")), ia));
   ia->variable_names_cb = get_widget_assert (ia->builder, "variable-names");
@@ -1132,9 +1146,12 @@ first_line_page_create (PsppireImportAssistant *ia)
 
 
   g_object_set_data (G_OBJECT (w), "on-reset", reset_first_line_page);
+#endif
 }
 
 
+
+
 static void
 intro_on_enter (PsppireImportAssistant *ia)
 {
@@ -1263,6 +1280,8 @@ psppire_import_assistant_new (GtkWindow *toplevel)
                                   NULL));
 }
 
+
+
 \f
 
 struct column
@@ -1303,6 +1322,8 @@ destroy_columns (PsppireImportAssistant *ia)
   free (ia->columns);
 }
 
+#if SHEET_MERGE
+
 /* Called to render one of the cells in the fields preview tree
    view. */
 static void
@@ -1336,6 +1357,7 @@ render_input_cell (PsppSheetViewColumn *tree_column, GtkCellRenderer *cell,
                   (void *) NULL);
 }
 
+#endif
 
 /* Parses the contents of the field at (ROW,COLUMN) according to
    its variable format.  If OUTPUTP is non-null, then *OUTPUTP
@@ -1395,6 +1417,7 @@ parse_field (PsppireImportAssistant *ia,
   return ok;
 }
 
+#if SHEET_MERGE
 
 /* Called to render one of the cells in the data preview tree
    view. */
@@ -1465,6 +1488,7 @@ get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
 }
 
 
+
 \f
 
 
@@ -1513,7 +1537,7 @@ on_query_output_tooltip (GtkWidget *widget, gint wx, gint wy,
   free (text);
   return TRUE;
 }
-
+#endif
 \f
 
 
@@ -1708,6 +1732,8 @@ split_fields (PsppireImportAssistant *ia)
     }
 }
 
+
+#if SHEET_MERGE
 static PsppSheetViewColumn *
 make_data_column (PsppireImportAssistant *ia, GtkWidget *tree_view,
                   bool input, gint dict_idx)
@@ -1754,15 +1780,19 @@ make_data_column (PsppireImportAssistant *ia, GtkWidget *tree_view,
   return tree_column;
 }
 
+#endif
 
 static GtkWidget *
 create_data_tree_view (gboolean input, GtkContainer *parent,
                        PsppireImportAssistant *ia)
 {
   gint i;
+#if SHEET_MERGE  
   GtkWidget *tree_view = make_tree_view (ia);
 
   set_model_on_treeview (ia, tree_view, ia->skip_lines);
+
+
   
   pspp_sheet_selection_set_mode (pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (tree_view)),
                                  PSPP_SHEET_SELECTION_NONE);
@@ -1792,9 +1822,11 @@ create_data_tree_view (gboolean input, GtkContainer *parent,
   gtk_widget_show (tree_view);
 
   return tree_view;
+#endif
 }
 
 
+
 /* Chooses a name for each column on the separators page */
 static void
 choose_column_names (PsppireImportAssistant *ia)
@@ -1911,6 +1943,9 @@ separators_page_create (PsppireImportAssistant *ia)
 
 
 \f
+
+#if SHEET_MERGE
+
 /* Called when the user changes one of the variables in the
    dictionary. */
 static void
@@ -1949,7 +1984,7 @@ on_variable_change (PsppireDict *dict, int dict_idx,
   pop_watch_cursor (ia);
 }
 
-
+#endif
 
 
 /* Called just before the formats page of the assistant is
@@ -1957,200 +1992,6 @@ on_variable_change (PsppireDict *dict, int dict_idx,
 static void
 prepare_formats_page (PsppireImportAssistant *ia)
 {
-  PsppireDict *psppire_dict = NULL;
-  PsppireVarSheet *var_sheet;
-  GtkBin *vars_scroller;
-  GtkWidget *old_var_sheet;
-
-  
-  push_watch_cursor (ia);
-
-  if (ia->spreadsheet == NULL)
-    {
-      struct fmt_guesser *fg;
-      unsigned long int number = 0;
-      size_t column_idx;
-
-      
-      ia->dict = dict_create (get_default_encoding ());
-      fg = fmt_guesser_create ();
-      for (column_idx = 0; column_idx < ia->column_cnt; column_idx++)
-       {
-         struct variable *modified_var = 
-           (column_idx < ia->modified_var_cnt ? ia->modified_vars[column_idx] : NULL);
-
-         if (modified_var == NULL)
-           {
-             struct column *column = &ia->columns[column_idx];
-             struct variable *var;
-             struct fmt_spec format;
-             char *name;
-             size_t row;
-
-             /* Choose variable name. */
-             name = dict_make_unique_var_name (ia->dict, column->name, &number);
-
-             /* Choose variable format. */
-             fmt_guesser_clear (fg);
-             for (row = ia->skip_lines; row < ia->line_cnt; row++)
-               fmt_guesser_add (fg, column->contents[row]);
-             fmt_guesser_guess (fg, &format);
-             fmt_fix_input (&format);
-
-             /* Create variable. */
-             var = dict_create_var_assert (ia->dict, name, fmt_var_width (&format));
-             var_set_both_formats (var, &format);
-
-             free (name);
-           }
-         else
-           {
-             char *name;
-
-             name = dict_make_unique_var_name (ia->dict, var_get_name (modified_var),
-                                               &number);
-             dict_clone_var_as_assert (ia->dict, modified_var, name);
-             free (name);
-           }
-       }
-      fmt_guesser_destroy (fg);
-    }
-  else
-    {
-      int row_start = -1;
-      int row_stop = -1;
-      int col_start = -1;
-      int col_stop = -1;
-
-      GtkBuilder *builder = ia->builder;
-
-      struct casereader *reader = NULL;
-
-      GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
-      GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
-      const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
-      GtkWidget *combo_box = get_widget_assert (builder, "sheet-entry");
-
-      gint num = gtk_combo_box_get_active (GTK_COMBO_BOX (combo_box));
-
-      struct spreadsheet_read_options sro;
-  
-      sro.sheet_name = NULL;
-      sro.cell_range = NULL;
-      sro.sheet_index = num + 1;
-
-      if ( convert_cell_ref (range, &col_start, &row_start, &col_stop, &row_stop))
-       {
-         sro.cell_range = g_strdup (range);
-       }
-
-      sro.read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (readnames_checkbox));
-      sro.asw = -1;
-  
-      switch (ia->spreadsheet->type)
-       {
-       case SPREADSHEET_ODS:
-       case SPREADSHEET_GNUMERIC:
-         {
-           reader = spreadsheet_make_reader (ia->spreadsheet, &sro);
-           ia->dict = dict_clone (ia->spreadsheet->dict);
-         }
-         break;
-       default:
-         g_assert_not_reached ();
-         break;
-       }
-      g_free (sro.cell_range);
-
-      if (reader && ia->dict)
-       {
-         struct ccase *c;
-         int col;
-
-         ia->column_cnt = dict_get_var_cnt (ia->dict);
-         ia->columns = xcalloc (ia->column_cnt, sizeof (*ia->columns));
-         for (col = 0; col < ia->column_cnt ; ++col)
-           {
-             const struct variable *var = dict_get_var (ia->dict, col);
-             ia->columns[col].name = xstrdup (var_get_name (var));
-             ia->columns[col].contents = NULL;
-           }
-
-         casenumber rows = 0;
-         for (; (c = casereader_read (reader)) != NULL; case_unref (c))
-           {
-             rows++;
-             for (col = 0; col < ia->column_cnt ; ++col)
-               {
-                 char *ss;
-                 const struct variable *var = dict_get_var (ia->dict, col);
-             
-                 ia->columns[col].contents = xrealloc (ia->columns[col].contents,
-                                                       sizeof (struct substring) * rows);
-             
-                 ss = data_out (case_data (c, var), dict_get_encoding (ia->dict), 
-                                var_get_print_format (var));
-             
-                 ia->columns[col].contents[rows - 1] = ss_cstr (ss);
-               }
-         
-             if (rows > MAX_PREVIEW_LINES)
-               {
-                 case_unref (c);
-                 break;
-               }
-           }
-         casereader_destroy (reader);
-         ia->line_cnt = rows;
-       }
-      else
-       {
-         GtkWidget * dialog = gtk_message_dialog_new (NULL,
-                                                      GTK_DIALOG_MODAL,
-                                                      GTK_MESSAGE_ERROR,
-                                                      GTK_BUTTONS_CLOSE,
-                                                      _("An error occurred reading the spreadsheet file."));
-
-         gtk_dialog_run (GTK_DIALOG (dialog));
-         gtk_widget_destroy (dialog);
-       }
-    }
-
-  psppire_dict = psppire_dict_new_from_dict (ia->dict);
-  g_signal_connect (psppire_dict, "variable-changed",
-                   G_CALLBACK (on_variable_change), ia);
-  ia->psppire_dict = psppire_dict;
-
-  
-  /* XXX: PsppireVarStore doesn't hold a reference to
-     psppire_dict for now, but it should.  After it does, we
-     should g_object_ref the psppire_dict here, since we also
-     hold a reference via ia->formats->dict. */
-  var_sheet = PSPPIRE_VAR_SHEET (psppire_var_sheet_new ());
-  g_object_set (var_sheet,
-               "dictionary", psppire_dict,
-               "may-create-vars", FALSE,
-               "may-delete-vars", FALSE,
-               "format-use", FMT_FOR_INPUT,
-               "enable-grid-lines", PSPP_SHEET_VIEW_GRID_LINES_BOTH,
-               (void *) NULL);
-
-  vars_scroller = GTK_BIN (get_widget_assert (ia->builder, "vars-scroller"));
-  old_var_sheet = gtk_bin_get_child (GTK_BIN (vars_scroller));
-  if (old_var_sheet != NULL)
-    gtk_container_remove (GTK_CONTAINER (vars_scroller),  old_var_sheet);
-  gtk_container_add (GTK_CONTAINER (vars_scroller), GTK_WIDGET (var_sheet));
-  gtk_widget_show (GTK_WIDGET (var_sheet));
-
-  ia->data_tree_view =
-    GTK_WIDGET (create_data_tree_view (
-                                      FALSE,
-                                      GTK_CONTAINER (get_widget_assert (ia->builder, "data-scroller")),
-                                      ia));
-
-  gtk_widget_show (ia->paste_button);
-
-  pop_watch_cursor (ia);
 }
 
 static void
@@ -2365,11 +2206,13 @@ sheet_spec_gen_syntax (PsppireImportAssistant *ia)
   return ds_cstr (&s);
 }
 
+
 gchar *
 psppire_import_assistant_generate_syntax (PsppireImportAssistant *ia)
 {
   struct string s = DS_EMPTY_INITIALIZER;
 
+#if SHEET_MERGE
   if (!ia->spreadsheet)
     {
       if (ia->file_name == NULL)
@@ -2398,6 +2241,8 @@ psppire_import_assistant_generate_syntax (PsppireImportAssistant *ia)
     {
       return sheet_spec_gen_syntax (ia);
     }
+
+#endif
   
   return ds_cstr (&s);
 }