- GtkWidget *scrolled_window = get_widget_assert (ia->builder, "first-line-scroller");
-
- if (ia->first_line_tree_view == NULL)
- {
- ia->first_line_tree_view = gtk_tree_view_new ();
-
- gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (ia->first_line_tree_view), TRUE);
-
- GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
- GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes (_("Line"), renderer,
- "text", 0,
- NULL);
- gtk_tree_view_append_column (GTK_TREE_VIEW (ia->first_line_tree_view), column);
-
- renderer = gtk_cell_renderer_text_new ();
- column = gtk_tree_view_column_new_with_attributes (_("Text"), renderer, "text", 1, NULL);
- gtk_tree_view_append_column (GTK_TREE_VIEW (ia->first_line_tree_view), column);
-
- gtk_container_add (GTK_CONTAINER (scrolled_window), ia->first_line_tree_view);
-
- g_signal_connect_swapped (ia->first_line_tree_view, "cursor-changed",
- G_CALLBACK (on_treeview_selection_change), ia);
- }
- gtk_widget_show_all (scrolled_window);
-
- ia->variable_names_cb = get_widget_assert (ia->builder, "variable-names");
-
-#if SHEET_MERGE
- pspp_sheet_selection_set_mode (
- pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (ia->tree_view)),
- PSPP_SHEET_SELECTION_BROWSE);
- pspp_sheet_view_set_rubber_banding (PSPP_SHEET_VIEW (ia->tree_view), TRUE);
-
-
- g_signal_connect_swapped (pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (ia->tree_view)),
- "changed", G_CALLBACK (set_first_line_options), ia);
-
- g_signal_connect_swapped (ia->variable_names_cb, "toggled",
- G_CALLBACK (set_first_line_options), ia);
-
- g_object_set_data (G_OBJECT (w), "on-reset", reset_first_line_page);
-#endif
-}
-
-
-
-
-static void
-intro_on_enter (PsppireImportAssistant *ia)
-{
- GtkBuilder *builder = ia->builder;
- GtkWidget *table = get_widget_assert (builder, "button-table");
-
- struct string s;
-
- ds_init_empty (&s);
- ds_put_cstr (&s, _("This assistant will guide you through the process of "
- "importing data into PSPP from a text file with one line "
- "per case, in which fields are separated by tabs, "
- "commas, or other delimiters.\n\n"));
-
- if (ia->text_file)
- {
- if (ia->text_file->total_is_exact)
- {
- ds_put_format (
- &s, ngettext ("The selected file contains %'lu line of text. ",
- "The selected file contains %'lu lines of text. ",
- ia->text_file->total_lines),
- ia->text_file->total_lines);
- }
- else if (ia->text_file->total_lines > 0)
- {
- ds_put_format (
- &s, ngettext (
- "The selected file contains approximately %'lu line of text. ",
- "The selected file contains approximately %'lu lines of text. ",
- ia->text_file->total_lines),
- ia->text_file->total_lines);
- ds_put_format (
- &s, ngettext (
- "Only the first %zu line of the file will be shown for "
- "preview purposes in the following screens. ",
- "Only the first %zu lines of the file will be shown for "
- "preview purposes in the following screens. ",
- ia->text_file->line_cnt),
- ia->text_file->line_cnt);
- }
- }
-
- ds_put_cstr (&s, _("You may choose below how much of the file should "
- "actually be imported."));
-
- gtk_label_set_text (GTK_LABEL (get_widget_assert (builder, "intro-label")),
- ds_cstr (&s));
- ds_destroy (&s);
-
- GtkWidget *w = gtk_grid_get_child_at (GTK_GRID (table), 1, 1);
- int old_value = w ? gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)) : 1;
- if (w)
- gtk_container_remove (GTK_CONTAINER (table), w);
-
- w = gtk_grid_get_child_at (GTK_GRID (table), 1, 2);
- if (w)
- gtk_container_remove (GTK_CONTAINER (table), w);
-
-
- GtkWidget *hbox_n_cases = psppire_scanf_new (_("Only the first %4d cases"), &ia->n_cases_spin);
-
- GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (ia->n_cases_spin));
- gtk_adjustment_set_lower (adj, 1.0);
-
- if (ia->text_file)
- {
- if (psppire_text_file_get_total_exact (ia->text_file))
- {
- gulong total_lines = psppire_text_file_get_n_lines (ia->text_file);
- gtk_adjustment_set_upper (adj, total_lines);
- gtk_adjustment_set_value (adj, old_value);
- }
- else
- gtk_adjustment_set_upper (adj, DBL_MAX);
- }
- gtk_grid_attach (GTK_GRID (table), hbox_n_cases,
- 1, 1,
- 1, 1);
-
-
- GtkWidget *hbox_percent = psppire_scanf_new (_("Only the first %3d %% of file (approximately)"),
- &ia->percent_spin);
-
- gtk_grid_attach (GTK_GRID (table), hbox_percent,
- 1, 2,
- 1, 1);
-
- gtk_widget_show_all (table);
-
- on_intro_amount_changed (ia);
-}
-
-/* Initializes IA's intro substructure. */
-static void
-intro_page_create (PsppireImportAssistant *ia)
-{
- GtkBuilder *builder = ia->builder;
-
- GtkWidget *w = get_widget_assert (builder, "Intro");
-
- ia->percent_spin = gtk_spin_button_new_with_range (0, 100, 10);
-
-
- add_page_to_assistant (ia, w, GTK_ASSISTANT_PAGE_CONTENT, _("Select the Lines to Import"));
-
- ia->all_cases_button = get_widget_assert (builder, "import-all-cases");
-
- ia->n_cases_button = get_widget_assert (builder, "import-n-cases");
-
- ia->percent_button = get_widget_assert (builder, "import-percent");
-
- g_signal_connect_swapped (ia->all_cases_button, "toggled",
- G_CALLBACK (on_intro_amount_changed), ia);
- g_signal_connect_swapped (ia->n_cases_button, "toggled",
- G_CALLBACK (on_intro_amount_changed), ia);
- g_signal_connect_swapped (ia->percent_button, "toggled",
- G_CALLBACK (on_intro_amount_changed), ia);
-
-
- g_object_set_data (G_OBJECT (w), "on-entering", intro_on_enter);
- g_object_set_data (G_OBJECT (w), "on-reset", reset_intro_page);
-}
-
-
-GtkWidget *
-psppire_import_assistant_new (GtkWindow *toplevel)
-{
- return GTK_WIDGET (g_object_new (PSPPIRE_TYPE_IMPORT_ASSISTANT,
- /* Some window managers (notably ratpoison)
- ignore the maximise command when a window is
- transient. This causes problems for this
- window. */
- /* "transient-for", toplevel, */
- NULL));
-}
-
-
-
-\f
-
-struct column
-{
- /* Variable name for this column. This is the variable name
- used on the separators page; it can be overridden by the
- user on the formats page. */
- char *name;
-
- /* Maximum length of any row in this column. */
- size_t width;
-
- /* Contents of this column: contents[row] is the contents for
- the given row.
-
- A null substring indicates a missing column for that row
- (because the line contains an insufficient number of
- separators).
-
- contents[] elements may be substrings of the lines[]
- strings that represent the whole lines of the file, to
- save memory. Other elements are dynamically allocated
- with ss_alloc_substring. */
- struct substring *contents;
-};
-
-#if SHEET_MERGE
-
-/* Called to render one of the cells in the fields preview tree
- view. */
-static void
-render_input_cell (PsppSheetViewColumn *tree_column, GtkCellRenderer *cell,
- GtkTreeModel *model, GtkTreeIter *iter,
- gpointer ia_)
-{
- PsppireImportAssistant *ia = ia_;
- struct substring field;
- size_t row;
- gint column;
-
- column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
- "column-number"));
- row = empty_list_store_iter_to_row (iter) + ia->skip_lines;
- field = ia->columns[column].contents[row];
- if (field.string != NULL)
- {
- GValue text = {0, };
- g_value_init (&text, G_TYPE_STRING);
- g_value_take_string (&text, ss_xstrdup (field));
- g_object_set_property (G_OBJECT (cell), "text", &text);
- g_value_unset (&text);
- g_object_set (cell, "background-set", FALSE, (void *) NULL);
- }
- else
- g_object_set (cell,
- "text", "",
- "background", "red",
- "background-set", TRUE,
- (void *) NULL);
-}
-
-/* Parses the contents of the field at (ROW,COLUMN) according to
- its variable format. If OUTPUTP is non-null, then *OUTPUTP
- receives the formatted output for that field (which must be
- freed with free). If TOOLTIPP is non-null, then *TOOLTIPP
- receives a message suitable for use in a tooltip, if one is
- needed, or a null pointer otherwise. Returns TRUE if a
- tooltip message is needed, otherwise FALSE. */
-static bool
-parse_field (PsppireImportAssistant *ia,
- size_t row, size_t column,
- char **outputp, char **tooltipp)
-{
- const struct fmt_spec *in;
- struct fmt_spec out;
- char *tooltip;
- bool ok;
-
- struct substring field = ia->columns[column].contents[row];
- const 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);
- tooltip = NULL;
- if (field.string != NULL)
- {
- char *error = data_in (field, "UTF-8", in->type, &val, var_get_width (var),
- dict_get_encoding (ia->dict));
- if (error != NULL)
- {
- tooltip = xasprintf (_("Cannot parse field content `%.*s' as "
- "format %s: %s"),
- (int) field.length, field.string,
- fmt_name (in->type), error);
- free (error);
- }
- }
- else
- {
- tooltip = xstrdup (_("This input line has too few separators "
- "to fill in this field."));
- value_set_missing (&val, var_get_width (var));
- }
- if (outputp != NULL)
- {
- *outputp = data_out (&val, dict_get_encoding (ia->dict), &out);
- }
- value_destroy (&val, var_get_width (var));
-
- ok = tooltip == NULL;
- if (tooltipp != NULL)
- *tooltipp = tooltip;
- else
- free (tooltip);
- return ok;
-}
-
-/* Called to render one of the cells in the data preview tree
- view. */
-static void
-render_output_cell (PsppSheetViewColumn *tree_column,
- GtkCellRenderer *cell,
- GtkTreeModel *model,
- GtkTreeIter *iter,
- gpointer ia_)
-{
- PsppireImportAssistant *ia = ia_;
- char *output;
- GValue gvalue = { 0, };
- bool ok = parse_field (ia,
- (empty_list_store_iter_to_row (iter)
- + ia->skip_lines),
- GPOINTER_TO_INT (g_object_get_data (G_OBJECT (tree_column),
- "column-number")),
- &output, NULL);
-
- g_value_init (&gvalue, G_TYPE_STRING);
- g_value_take_string (&gvalue, output);
- g_object_set_property (G_OBJECT (cell), "text", &gvalue);
- g_value_unset (&gvalue);
-
- if (ok)
- g_object_set (cell, "background-set", FALSE, (void *) NULL);
- else
- g_object_set (cell,
- "background", "red",
- "background-set", TRUE,
- (void *) NULL);
-}
-
-
-/* Utility functions used by multiple pages of the assistant. */
-
-static gboolean
-get_tooltip_location (GtkWidget *widget, gint wx, gint wy,
- const PsppireImportAssistant *ia,
- size_t *row, size_t *column)
-{
- PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
- gint bx, by;
- GtkTreePath *path;
- GtkTreeIter iter;
- PsppSheetViewColumn *tree_column;
- GtkTreeModel *tree_model;
- bool ok;
-
- 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 = 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->skip_lines;
- return TRUE;
-}
-
-
-
-\f
-
-
-/* Called to render a tooltip on one of the cells in the fields
- preview tree view. */
-static gboolean
-on_query_input_tooltip (GtkWidget *widget, gint wx, gint wy,
- gboolean keyboard_mode UNUSED,
- GtkTooltip *tooltip, PsppireImportAssistant *ia)
-{
- size_t row, column;
-
- if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
- return FALSE;
-
- if (ia->columns[column].contents[row].string != NULL)
- return FALSE;
-
- gtk_tooltip_set_text (tooltip,
- _("This input line has too few separators "
- "to fill in this field."));
- return TRUE;
-}
-
-
-/* Called to render a tooltip for one of the cells in the data
- preview tree view. */
-static gboolean
-on_query_output_tooltip (GtkWidget *widget, gint wx, gint wy,
- gboolean keyboard_mode UNUSED,
- GtkTooltip *tooltip, PsppireImportAssistant *ia)
-{
- size_t row, column;
- char *text;
-
- if (!gtk_widget_get_mapped (widget))
- return FALSE;
-
- if (!get_tooltip_location (widget, wx, wy, ia, &row, &column))
- return FALSE;
-
- if (parse_field (ia, row, column, NULL, &text))
- return FALSE;
-
- gtk_tooltip_set_text (tooltip, text);
- free (text);
- return TRUE;
-}
-#endif
-\f
-
-
-static void
-set_quote_list (GtkComboBox *cb)
-{
- GtkListStore *list = gtk_list_store_new (1, G_TYPE_STRING);
- GtkTreeIter iter;
- gint i;
- const gchar *seperator[3] = {"'\"", "\'", "\""};
-
- for (i = 0; i < 3; i++)
- {
- const gchar *s = seperator[i];
-
- /* Add a new row to the model */
- gtk_list_store_append (list, &iter);
- gtk_list_store_set (list, &iter,
- 0, s,
- -1);
-
- }
-
- gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
- g_object_unref (list);
-
- gtk_combo_box_set_entry_text_column (cb, 0);
-}
-
-
-#if SHEET_MERGE
-
-/* Sets IA's separators substructure to match the widgets. */
-static void
-get_separators (PsppireImportAssistant *ia)
-{
- int i;
-
- ds_clear (&ia->separators);
- for (i = 0; i < SEPARATOR_CNT; i++)
- {
- const struct separator *sep = &separators[i];
- GtkWidget *button = get_widget_assert (ia->builder, sep->name);
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
- ds_put_byte (&ia->separators, sep->c);
- }
-
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->custom_cb)))
- ds_put_cstr (&ia->separators,
- gtk_entry_get_text (GTK_ENTRY (ia->custom_entry)));
-
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->quote_cb)))
- {
- const gchar *text = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (ia->quote_combo))));
- ds_assign_cstr (&ia->quotes, text);
- }
- else
- ds_clear (&ia->quotes);
-}
-
-
-
-
-
-
-/* Breaks the file data in IA into columns based on the
- separators set in IA's separators substructure. */
-static void
-split_fields (PsppireImportAssistant *ia)
-{
- size_t columns_allocated;
- bool space_sep;
- size_t row = 0;
-
- /* Is space in the set of separators? */
- space_sep = ss_find_byte (ds_ss (&ia->separators), ' ') != SIZE_MAX;
-
- /* Split all the lines, not just those from
- ia->first_line.skip_lines on, so that we split the line that
- contains variables names if ia->first_line.variable_names is
- TRUE. */
- columns_allocated = 0;
-
- gint n_lines = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (ia->text_file), NULL);
- GtkTreeIter iter;
- gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ia->text_file), &iter);
- while (gtk_tree_model_iter_next (GTK_TREE_MODEL (ia->text_file), &iter))
- {
- row++;
- gchar *xxx;
- gtk_tree_model_get (GTK_TREE_MODEL (ia->text_file), &iter, 1, &xxx, -1);
- struct substring text = ss_cstr (xxx);
- g_free (xxx);
- size_t column_idx;
-
- for (column_idx = 0; ;column_idx++)
- {
- struct substring field = SS_EMPTY_INITIALIZER;
- struct column *column;
-
- if (space_sep)
- {
- ss_ltrim (&text, ss_cstr (" "));
- }
- if (ss_is_empty (text))
- {
- if (column_idx != 0)
- break;
- field = text;
- }
- else if (!ds_is_empty (&ia->quotes)
- && ds_find_byte (&ia->quotes, text.string[0]) != SIZE_MAX)
- {
- int quote = ss_get_byte (&text);
- struct string s;
- int c;
-
- ds_init_empty (&s);
- while ((c = ss_get_byte (&text)) != EOF)
- if (c != quote)
- ds_put_byte (&s, c);
- else if (ss_match_byte (&text, quote))
- ds_put_byte (&s, quote);
- else
- break;
- field = ds_ss (&s);
- }
- else
- {
- ss_get_bytes (&text, ss_cspan (text, ds_ss (&ia->separators)),
- &field);
- }
-
- if (column_idx >= ia->column_cnt)
- {
- struct column *column;
-
- if (ia->column_cnt >= columns_allocated)
- {
- ia->columns = x2nrealloc (ia->columns, &columns_allocated,
- sizeof *ia->columns);
- }
- column = &ia->columns[ia->column_cnt++];
- column->name = NULL;
- column->width = 0;
- column->contents = xcalloc (n_lines,
- sizeof *column->contents);
- }
- column = &ia->columns[column_idx];
- column->contents[row] = field;
- if (ss_length (field) > column->width)
- column->width = ss_length (field);
-
- if (space_sep)
- ss_ltrim (&text, ss_cstr (" "));
- if (ss_is_empty (text))
- break;
- if (ss_find_byte (ds_ss (&ia->separators), ss_first (text))
- != SIZE_MAX)
- ss_advance (&text, 1);
- }
- }
-}
-
-#endif
-
-/* Chooses a name for each column on the separators page */
-static void
-choose_column_names (PsppireImportAssistant *ia)
-{
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->variable_names_cb)))
- {
- int i;
- unsigned long int generated_name_count = 0;
- dict_clear (ia->dict);
- for (i = 0; i < gtk_tree_model_get_n_columns (ia->delimiters_model) - 1; ++i)
- {
- const gchar *candidate_name =
- psppire_delimited_text_get_header_title (PSPPIRE_DELIMITED_TEXT (ia->delimiters_model), i);
-
- char *name = dict_make_unique_var_name (ia->dict, candidate_name, &generated_name_count);
- dict_create_var_assert (ia->dict, name, 0);
- free (name);
- }
- }
-}
-
-
-
-/* Called when the user toggles one of the separators
- checkboxes. */
-static void
-on_separator_toggle (GtkToggleButton *toggle UNUSED,
- PsppireImportAssistant *ia)
-{
- int i;
- GSList *delimiters = NULL;
- for (i = 0; i < SEPARATOR_CNT; i++)
- {
- const struct separator *s = &separators[i];
- GtkWidget *button = get_widget_assert (ia->builder, s->name);
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
- {
- delimiters = g_slist_prepend (delimiters, GINT_TO_POINTER (s->c));
- }
- }
-
- g_object_set (ia->delimiters_model, "delimiters", delimiters, NULL);
-
- // revise_fields_preview (ia);
-}
-
-
-/* Called when the user changes the entry field for custom
- separators. */
-static void
-on_separators_custom_entry_notify (GObject *gobject UNUSED,
- GParamSpec *arg1 UNUSED,
- PsppireImportAssistant *ia)
-{
- revise_fields_preview (ia);
-}
-
-/* Called when the user toggles the checkbox that enables custom
- separators. */
-static void
-on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
- PsppireImportAssistant *ia)
-{
- bool is_active = gtk_toggle_button_get_active (custom_cb);
- gtk_widget_set_sensitive (ia->custom_entry, is_active);
- revise_fields_preview (ia);
-}
-
-/* Called when the user changes the selection in the combo box
- that selects a quote character. */
-static void
-on_quote_combo_change (GtkComboBox *combo, PsppireImportAssistant *ia)
-{
- // revise_fields_preview (ia);
-}
-
-/* Called when the user toggles the checkbox that enables
- quoting. */
-static void
-on_quote_cb_toggle (GtkToggleButton *quote_cb, PsppireImportAssistant *ia)
-{
- bool is_active = gtk_toggle_button_get_active (quote_cb);
- gtk_widget_set_sensitive (ia->quote_combo, is_active);
- revise_fields_preview (ia);
-}
-
-/* Initializes IA's separators substructure. */
-static void
-separators_page_create (PsppireImportAssistant *ia)
-{
- GtkBuilder *builder = ia->builder;
-
- size_t i;
-
- GtkWidget *w = get_widget_assert (builder, "Separators");
-
- g_object_set_data (G_OBJECT (w), "on-entering", prepare_separators_page);
- g_object_set_data (G_OBJECT (w), "on-reset", prepare_separators_page);
-
- add_page_to_assistant (ia, w, GTK_ASSISTANT_PAGE_CONTENT, _("Choose Separators"));
-
- ia->custom_cb = get_widget_assert (builder, "custom-cb");
- ia->custom_entry = get_widget_assert (builder, "custom-entry");
- ia->quote_combo = get_widget_assert (builder, "quote-combo");
- ia->quote_entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (ia->quote_combo)));
- ia->quote_cb = get_widget_assert (builder, "quote-cb");
-
- set_quote_list (GTK_COMBO_BOX (ia->quote_combo));
-
- if (ia->fields_tree_view == NULL)
- {
- GtkWidget *scroller = get_widget_assert (ia->builder, "fields-scroller");
- ia->fields_tree_view = gtk_tree_view_new ();
- gtk_container_add (GTK_CONTAINER (scroller), GTK_WIDGET (ia->fields_tree_view));
- gtk_widget_show_all (scroller);
- }
-
- g_signal_connect (ia->quote_combo, "changed",
- G_CALLBACK (on_quote_combo_change), ia);
- g_signal_connect (ia->quote_cb, "toggled",
- G_CALLBACK (on_quote_cb_toggle), ia);
- g_signal_connect (ia->custom_entry, "notify::text",
- G_CALLBACK (on_separators_custom_entry_notify), ia);
- g_signal_connect (ia->custom_cb, "toggled",
- G_CALLBACK (on_separators_custom_cb_toggle), ia);
- for (i = 0; i < SEPARATOR_CNT; i++)
- g_signal_connect (get_widget_assert (builder, separators[i].name),
- "toggled", G_CALLBACK (on_separator_toggle), ia);
-
-}
-
-
-
-\f
-
-#if SHEET_MERGE
-
-/* Called when the user changes one of the variables in the
- dictionary. */
-static void
-on_variable_change (PsppireDict *dict, int dict_idx,
- unsigned int what, const struct variable *oldvar,
- PsppireImportAssistant *ia)
-{
- PsppSheetView *tv = PSPP_SHEET_VIEW (ia->data_tree_view);
- gint column_idx = dict_idx + 1;
-
- push_watch_cursor (ia);
-
- /* Remove previous column and replace with new column. */
- pspp_sheet_view_remove_column (tv, pspp_sheet_view_get_column (PSPP_SHEET_VIEW (ia->data_tree_view), column_idx));
- pspp_sheet_view_insert_column (tv, PSPP_SHEET_VIEW_COLUMN (make_data_column (ia, ia->data_tree_view, FALSE, dict_idx)),
- column_idx);
-
- /* Save a copy of the modified variable in modified_vars, so
- that its attributes will be preserved if we back up to the
- previous page with the Prev button and then come back
- here. */
- if (dict_idx >= ia->modified_var_cnt)
- {
- size_t i;
- ia->modified_vars = xnrealloc (ia->modified_vars, dict_idx + 1,
- sizeof *ia->modified_vars);
- for (i = 0; i <= dict_idx; i++)
- ia->modified_vars[i] = NULL;
- ia->modified_var_cnt = dict_idx + 1;
- }
- if (ia->modified_vars[dict_idx])
- var_destroy (ia->modified_vars[dict_idx]);
- ia->modified_vars[dict_idx]
- = var_clone (psppire_dict_get_variable (dict, dict_idx));
-
- pop_watch_cursor (ia);
-}
-
-#endif
-
-
-static struct casereader_random_class my_casereader_class;
-
-static struct ccase *
-my_read (struct casereader *reader, void *aux, casenumber idx)
-{
- PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (aux);
- GtkTreeModel *tm = GTK_TREE_MODEL (ia->delimiters_model);
-
- GtkTreePath *tp = gtk_tree_path_new_from_indices (idx, -1);
-
- const struct caseproto *proto = casereader_get_proto (reader);
-
- GtkTreeIter iter;
- struct ccase *c = NULL;
- if (gtk_tree_model_get_iter (tm, &iter, tp))
- {
- c = case_create (proto);
- int i;
- for (i = 0 ; i < caseproto_get_n_widths (proto); ++i)
- {
- GValue value = {0};
- gtk_tree_model_get_value (tm, &iter, i + 1, &value);
-
- const struct variable *var = dict_get_var (ia->dict, i);
-
- const gchar *ss = g_value_get_string (&value);
-
- union value *v = case_data_rw (c, var);
- char *xx = data_in (ss_cstr (ss),
- "UTF-8",
- var_get_write_format (var)->type,
- v, var_get_width (var), "UTF-8");
-
- /* if (xx) */
- /* g_print ("%s:%d Err %s\n", __FILE__, __LINE__, xx); */
- free (xx);
- g_value_unset (&value);
- }
- }
-
- gtk_tree_path_free (tp);
-
- return c;
-}
-
-static void
-my_destroy (struct casereader *reader, void *aux)
-{
- g_print ("%s:%d %p\n", __FILE__, __LINE__, reader);
-}
-
-static void
-my_advance (struct casereader *reader, void *aux, casenumber cnt)
-{
- g_print ("%s:%d\n", __FILE__, __LINE__);
-}
-
-
-static void
-foo (struct dictionary *dict, void *aux)
-{
- PsppireImportAssistant *ia = PSPPIRE_IMPORT_ASSISTANT (aux);
- g_print ("%s:%d\n", __FILE__, __LINE__);
-
- struct caseproto *proto = caseproto_create ();
-
- int i;
- for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
- {
- const struct variable *var = dict_get_var (ia->dict, i);
- proto = caseproto_add_width (proto, var_get_width (var));
- }
-
-
- gint n_rows = gtk_tree_model_iter_n_children (ia->delimiters_model, NULL);
-
- struct casereader *reader =
- casereader_create_random (proto, n_rows, &my_casereader_class, ia);
-
-
- PsppireDataStore *store = NULL;
-
- g_object_get (ia->data_sheet, "data-model", &store, NULL);
-
- psppire_data_store_set_reader (store, reader);
-}
-
-/* Called just before the formats page of the assistant is
- displayed. */
-static void
-prepare_formats_page (PsppireImportAssistant *ia)
-{
- PsppireDict *dict = psppire_dict_new_from_dict (ia->dict);
- g_object_set (ia->var_sheet, "data-model", dict, NULL);
-
- my_casereader_class.read = my_read;
- my_casereader_class.destroy = my_destroy;
- my_casereader_class.advance = my_advance;
-
- struct caseproto *proto = caseproto_create ();
- int i;
-
- struct fmt_guesser **fg = xcalloc (sizeof *fg, dict_get_var_cnt (ia->dict));
- for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
- {
- fg[i] = fmt_guesser_create ();
- }
-
- gint n_rows = gtk_tree_model_iter_n_children (ia->delimiters_model, NULL);
-
- GtkTreeIter iter;
- gboolean ok;
- for (ok = gtk_tree_model_get_iter_first (ia->delimiters_model, &iter);
- ok;
- ok = gtk_tree_model_iter_next (ia->delimiters_model, &iter))
- {
- for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
- {
- gchar *s = NULL;
- gtk_tree_model_get (ia->delimiters_model, &iter, i+1, &s, -1);
- fmt_guesser_add (fg[i], ss_cstr (s));
- free (s);
- }
- }
-
- for (i = 0 ; i < dict_get_var_cnt (ia->dict); ++i)
- {
- struct fmt_spec fs;
- fmt_guesser_guess (fg[i], &fs);
-
- fmt_fix (&fs, FMT_FOR_INPUT);
-
- struct variable *var = dict_get_var (ia->dict, i);
-
- int width = fmt_var_width (&fs);
-
- var_set_width_and_formats (var, width,
- &fs, &fs);
-
- proto = caseproto_add_width (proto, width);
- fmt_guesser_destroy (fg[i]);
- }
-
- free (fg);
-
- // dict_set_change_callback (ia->dict, foo, ia);
-
- struct casereader *reader =
- casereader_create_random (proto, n_rows, &my_casereader_class, ia);
-
- PsppireDataStore *store = psppire_data_store_new (dict);
- psppire_data_store_set_reader (store, reader);
-
- g_object_set (ia->data_sheet, "data-model", store, NULL);
-
-
- gint pmax;
- g_object_get (get_widget_assert (ia->builder, "vpaned1"),
- "max-position", &pmax, NULL);
-
-
- g_object_set (get_widget_assert (ia->builder, "vpaned1"),
- "position", pmax / 2, NULL);
-
- gtk_widget_show (ia->paste_button);
-}
-
-static void
-formats_page_create (PsppireImportAssistant *ia)
-{
- GtkBuilder *builder = ia->builder;
-
- GtkWidget *w = get_widget_assert (builder, "Formats");
- g_object_set_data (G_OBJECT (w), "on-entering", prepare_formats_page);
- g_object_set_data (G_OBJECT (w), "on-reset", reset_formats_page);
-
- GtkWidget *vars_scroller = get_widget_assert (builder, "vars-scroller");
- if (ia->var_sheet == NULL)
- {
- ia->var_sheet = psppire_variable_sheet_new ();
-
- gtk_container_add (GTK_CONTAINER (vars_scroller), ia->var_sheet);
-
- ia->dict = dict_create (get_default_encoding ());
-
- gtk_widget_show_all (vars_scroller);
- }
- GtkWidget *data_scroller = get_widget_assert (builder, "data-scroller");
- if (ia->data_sheet == NULL)
- {
- ia->data_sheet = psppire_data_sheet_new ();
-
- gtk_container_add (GTK_CONTAINER (data_scroller), ia->data_sheet);
-
- gtk_widget_show_all (data_scroller);
- }
-
- add_page_to_assistant (ia, w,
- GTK_ASSISTANT_PAGE_CONFIRM, _("Adjust Variable Formats"));
-
- ia->modified_vars = NULL;
- ia->modified_var_cnt = 0;
-}
-
-
-\f
-
-static void
-separators_append_syntax (const PsppireImportAssistant *ia, struct string *s)
-{
- int i;
-
- ds_put_cstr (s, " /DELIMITERS=\"");
-
- if (gtk_toggle_button_get_active (get_widget_assert (ia->builder, "tab")))
- ds_put_cstr (s, "\\t");
- for (i = 0; i < SEPARATOR_CNT; i++)
- {
- const struct separator *seps = &separators[i];
- GtkWidget *button = get_widget_assert (ia->builder, seps->name);
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
- {
- if (seps->c == '\t')
- continue;
-
- ds_put_byte (s, seps->c);
- }
- }
- ds_put_cstr (s, "\"\n");
- if (!ds_is_empty (&ia->quotes))
- syntax_gen_pspp (s, " /QUALIFIER=%sq\n", ds_cstr (&ia->quotes));
-}
-
-static void
-formats_append_syntax (const PsppireImportAssistant *ia, struct string *s)
-{
- int i;
- int var_cnt;
-
- g_return_if_fail (ia->dict);
-
- ds_put_cstr (s, " /VARIABLES=\n");
-
- var_cnt = dict_get_var_cnt (ia->dict);
- for (i = 0; i < var_cnt; i++)
- {
- struct variable *var = dict_get_var (ia->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 ? "." : "");
- }
-}
-
-static void
-first_line_append_syntax (const PsppireImportAssistant *ia, struct string *s)
-{
- gint first_case = 0;
- g_object_get (ia->delimiters_model, "first-line", &first_case, NULL);
-
- if (first_case > 0)
- ds_put_format (s, " /FIRSTCASE=%d\n", first_case + 1);
-}
-
-static void
-intro_append_syntax (const PsppireImportAssistant *ia, struct string *s)
-{
- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->n_cases_button)))
- ds_put_format (s, "N OF CASES %d.\n",
- gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (ia->n_cases_spin)));
- else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ia->percent_button)))
- ds_put_format (s, "SAMPLE %.4g.\n",
- gtk_spin_button_get_value (GTK_SPIN_BUTTON (ia->percent_spin)) / 100.0);
-}
-
-
-/* Emits PSPP syntax to S that applies the dictionary attributes
- (such as missing values and value labels) of the variables in
- DICT. */
-static void
-apply_dict (const struct dictionary *dict, struct string *s)
-{
- size_t var_cnt = dict_get_var_cnt (dict);
- size_t i;
-
- for (i = 0; i < var_cnt; i++)
- {
- struct variable *var = dict_get_var (dict, i);
- const char *name = var_get_name (var);
- 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);
-
- if (var_has_missing_values (var))
- {
- const struct missing_values *mv = var_get_missing_values (var);
- size_t j;
-
- syntax_gen_pspp (s, "MISSING VALUES %ss (", name);
- for (j = 0; j < mv_n_values (mv); j++)
- {
- if (j)
- ds_put_cstr (s, ", ");
- syntax_gen_value (s, mv_get_value (mv, j), width, format);
- }
-
- if (mv_has_range (mv))
- {
- double low, high;
- if (mv_has_value (mv))
- ds_put_cstr (s, ", ");
- mv_get_range (mv, &low, &high);
- syntax_gen_num_range (s, low, high, format);
- }
- ds_put_cstr (s, ").\n");
- }
- if (var_has_value_labels (var))
- {
- const struct val_labs *vls = var_get_value_labels (var);
- const struct val_lab **labels = val_labs_sorted (vls);
- size_t n_labels = val_labs_count (vls);
- size_t i;
-
- syntax_gen_pspp (s, "VALUE LABELS %ss", name);
- for (i = 0; i < n_labels; i++)
- {
- const struct val_lab *vl = labels[i];
- ds_put_cstr (s, "\n ");
- syntax_gen_value (s, &vl->value, width, format);
- ds_put_byte (s, ' ');
- syntax_gen_string (s, ss_cstr (val_lab_get_escaped_label (vl)));
- }
- free (labels);
- ds_put_cstr (s, ".\n");
- }
- if (var_has_label (var))
- syntax_gen_pspp (s, "VARIABLE LABELS %ss %sq.\n",
- name, var_get_label (var));
- if (measure != var_default_measure (type))
- syntax_gen_pspp (s, "VARIABLE LEVEL %ss (%ss).\n",
- 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_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));
- }
-}
-
-
-
-static char *
-sheet_spec_gen_syntax (PsppireImportAssistant *ia)
-{
- GtkBuilder *builder = ia->builder;
- GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
- GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
- GtkWidget *rnc = get_widget_assert (builder, "readnames-checkbox");
- const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
- int sheet_index = 1 + gtk_combo_box_get_active (GTK_COMBO_BOX (sheet_entry));
- gboolean read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rnc));
-
- struct string s = DS_EMPTY_INITIALIZER;