- return file_name;
-}
-\f
-/* Assistant. */
-
-static void close_assistant (struct import_assistant *, int response);
-static void on_prepare (GtkAssistant *assistant, GtkWidget *page,
- struct import_assistant *);
-static void on_cancel (GtkAssistant *assistant, struct import_assistant *);
-static void on_close (GtkAssistant *assistant, struct import_assistant *);
-static void on_paste (GtkButton *button, struct import_assistant *);
-static void on_reset (GtkButton *button, struct import_assistant *);
-static void close_assistant (struct import_assistant *, int response);
-
-/* Initializes IA's asst substructure. PARENT_WINDOW must be the
- window to use as the assistant window's parent. */
-static void
-init_assistant (struct import_assistant *ia, GtkWindow *parent_window)
-{
- struct assistant *a = &ia->asst;
-
- a->xml = XML_NEW ("text-data-import.glade");
- a->assistant = GTK_ASSISTANT (gtk_assistant_new ());
- g_signal_connect (a->assistant, "prepare", G_CALLBACK (on_prepare), ia);
- g_signal_connect (a->assistant, "cancel", G_CALLBACK (on_cancel), ia);
- g_signal_connect (a->assistant, "close", G_CALLBACK (on_close), ia);
- a->paste_button = gtk_button_new_from_stock (GTK_STOCK_PASTE);
- gtk_assistant_add_action_widget (a->assistant, a->paste_button);
- g_signal_connect (a->paste_button, "clicked", G_CALLBACK (on_paste), ia);
- a->reset_button = gtk_button_new_from_stock ("pspp-stock-reset");
- gtk_assistant_add_action_widget (a->assistant, a->reset_button);
- g_signal_connect (a->reset_button, "clicked", G_CALLBACK (on_reset), ia);
- gtk_window_set_title (GTK_WINDOW (a->assistant),
- _("Importing Delimited Text Data"));
- gtk_window_set_transient_for (GTK_WINDOW (a->assistant), parent_window);
-
- a->prop_renderer = gtk_cell_renderer_text_new ();
- g_object_ref_sink (a->prop_renderer);
- a->fixed_renderer = gtk_cell_renderer_text_new ();
- g_object_ref_sink (a->fixed_renderer);
- g_object_set (G_OBJECT (a->fixed_renderer),
- "family", "Monospace",
- (void *) NULL);
-}
-
-/* Frees IA's asst substructure. */
-static void
-destroy_assistant (struct import_assistant *ia)
-{
- struct assistant *a = &ia->asst;
-
- g_object_unref (a->prop_renderer);
- g_object_unref (a->fixed_renderer);
- g_object_unref (a->xml);
-}
-
-/* Appends a page of the given TYPE, with PAGE as its content, to
- the GtkAssistant encapsulated by IA. Returns the GtkWidget
- that represents the page. */
-static GtkWidget *
-add_page_to_assistant (struct import_assistant *ia,
- GtkWidget *page, GtkAssistantPageType type)
-{
- const char *title;
- char *title_copy;
- GtkWidget *content;
-
- title = gtk_window_get_title (GTK_WINDOW (page));
- title_copy = xstrdup (title ? title : "");
-
- content = gtk_bin_get_child (GTK_BIN (page));
- assert (content);
- g_object_ref (content);
- gtk_container_remove (GTK_CONTAINER (page), content);
-
- gtk_widget_destroy (page);
-
- gtk_assistant_append_page (ia->asst.assistant, content);
- gtk_assistant_set_page_type (ia->asst.assistant, content, type);
- gtk_assistant_set_page_title (ia->asst.assistant, content, title_copy);
- gtk_assistant_set_page_complete (ia->asst.assistant, content, true);
-
- free (title_copy);
-
- return content;
-}
-
-/* Called just before PAGE is displayed as the current page of
- ASSISTANT, this updates IA content according to the new
- page. */
-static void
-on_prepare (GtkAssistant *assistant, GtkWidget *page,
- struct import_assistant *ia)
-{
- if (page == ia->separators.page)
- prepare_separators_page (ia);
- else if (page == ia->formats.page)
- prepare_formats_page (ia);
-
- gtk_widget_show (ia->asst.reset_button);
- if (page == ia->formats.page)
- gtk_widget_show (ia->asst.paste_button);
- else
- gtk_widget_hide (ia->asst.paste_button);
-}
-
-/* Called when the Cancel button in the assistant is clicked. */
-static void
-on_cancel (GtkAssistant *assistant, struct import_assistant *ia)
-{
- close_assistant (ia, GTK_RESPONSE_CANCEL);
-}
-
-/* Called when the Apply button on the last page of the assistant
- is clicked. */
-static void
-on_close (GtkAssistant *assistant, struct import_assistant *ia)
-{
- close_assistant (ia, GTK_RESPONSE_APPLY);
-}
-
-/* Called when the Paste button on the last page of the assistant
- is clicked. */
-static void
-on_paste (GtkButton *button, struct import_assistant *ia)
-{
- close_assistant (ia, PSPPIRE_RESPONSE_PASTE);
-}
-
-/* Called when the Reset button is clicked. */
-static void
-on_reset (GtkButton *button, struct import_assistant *ia)
-{
- gint page_num = gtk_assistant_get_current_page (ia->asst.assistant);
- GtkWidget *page = gtk_assistant_get_nth_page (ia->asst.assistant, page_num);
-
- if (page == ia->intro.page)
- reset_intro_page (ia);
- else if (page == ia->first_line.page)
- reset_first_line_page (ia);
- else if (page == ia->separators.page)
- reset_separators_page (ia);
- else if (page == ia->formats.page)
- reset_formats_page (ia);
-}
-
-/* Causes the assistant to close, returning RESPONSE for
- interpretation by text_data_import_assistant. */
-static void
-close_assistant (struct import_assistant *ia, int response)
-{
- ia->asst.response = response;
- g_main_loop_quit (ia->asst.main_loop);
- gtk_widget_hide (GTK_WIDGET (ia->asst.assistant));
-}
-\f
-/* The "intro" page of the assistant. */
-
-static void on_intro_amount_changed (GtkToggleButton *button,
- struct import_assistant *);
-
-/* Initializes IA's intro substructure. */
-static void
-init_intro_page (struct import_assistant *ia)
-{
- GladeXML *xml = ia->asst.xml;
- struct intro_page *p = &ia->intro;
- struct string s;
-
- p->page = add_page_to_assistant (ia, get_widget_assert (xml, "Intro"),
- GTK_ASSISTANT_PAGE_INTRO);
- p->all_cases_button = get_widget_assert (xml, "import-all-cases");
- p->n_cases_button = get_widget_assert (xml, "import-n-cases");
- p->n_cases_spin = get_widget_assert (xml, "n-cases-spin");
- p->percent_button = get_widget_assert (xml, "import-percent");
- p->percent_spin = get_widget_assert (xml, "percent-spin");
- g_signal_connect (p->all_cases_button, "toggled",
- G_CALLBACK (on_intro_amount_changed), ia);
- g_signal_connect (p->n_cases_button, "toggled",
- G_CALLBACK (on_intro_amount_changed), ia);
- g_signal_connect (p->percent_button, "toggled",
- G_CALLBACK (on_intro_amount_changed), ia);
-
- 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->file.total_is_exact)
- ds_put_format (
- &s, ngettext ("The selected file contains %zu line of text. ",
- "The selected file contains %zu lines of text. ",
- ia->file.line_cnt),
- ia->file.line_cnt);
- else if (ia->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->file.total_lines),
- ia->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->file.line_cnt),
- ia->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 (xml, "intro-label")),
- ds_cstr (&s));
- ds_destroy (&s);
-}
-
-/* Resets IA's intro page to its initial state. */
-static void
-reset_intro_page (struct import_assistant *ia)
-{
- gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->intro.all_cases_button),
- true);
-}
-
-/* Called when one of the radio buttons is clicked. */
-static void
-on_intro_amount_changed (GtkToggleButton *button UNUSED,
- struct import_assistant *ia)
-{
- struct intro_page *p = &ia->intro;
-
- gtk_widget_set_sensitive (p->n_cases_spin,
- gtk_toggle_button_get_active (
- GTK_TOGGLE_BUTTON (p->n_cases_button)));
-
- gtk_widget_set_sensitive (ia->intro.percent_spin,
- gtk_toggle_button_get_active (
- GTK_TOGGLE_BUTTON (p->percent_button)));
-}
-\f
-/* The "first line" page of the assistant. */
-
-static GtkTreeView *create_lines_tree_view (GtkContainer *parent_window,
- struct import_assistant *);
-static void on_first_line_change (GtkTreeSelection *,
- struct import_assistant *);
-static void on_variable_names_cb_toggle (GtkToggleButton *,
- struct import_assistant *);
-static void set_first_line (struct import_assistant *);
-static void get_first_line (struct import_assistant *);
-
-/* Initializes IA's first_line substructure. */
-static void
-init_first_line_page (struct import_assistant *ia)
-{
- struct first_line_page *p = &ia->first_line;
- GladeXML *xml = ia->asst.xml;
-
- p->page = add_page_to_assistant (ia, get_widget_assert (xml, "FirstLine"),
- GTK_ASSISTANT_PAGE_CONTENT);
- gtk_widget_destroy (get_widget_assert (xml, "first-line"));
- p->tree_view = create_lines_tree_view (
- GTK_CONTAINER (get_widget_assert (xml, "first-line-scroller")), ia);
- p->variable_names_cb = get_widget_assert (xml, "variable-names");
- gtk_tree_selection_set_mode (
- gtk_tree_view_get_selection (GTK_TREE_VIEW (p->tree_view)),
- GTK_SELECTION_BROWSE);
- set_first_line (ia);
- g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (p->tree_view)),
- "changed", G_CALLBACK (on_first_line_change), ia);
- g_signal_connect (p->variable_names_cb, "toggled",
- G_CALLBACK (on_variable_names_cb_toggle), ia);
-}
-
-/* Resets the first_line page to its initial content. */
-static void
-reset_first_line_page (struct import_assistant *ia)
-{
- ia->first_line.skip_lines = 0;
- ia->first_line.variable_names = false;
- set_first_line (ia);
-}
-
-/* Creates and returns a tree view that contains each of the
- lines in IA's file as a row. */
-static GtkTreeView *
-create_lines_tree_view (GtkContainer *parent, struct import_assistant *ia)
-{
- GtkTreeView *tree_view;
- GtkTreeViewColumn *column;
- size_t max_line_length;
- gint content_width, header_width;
- size_t i;
-
- make_tree_view (ia, 0, &tree_view);
-
- column = gtk_tree_view_column_new_with_attributes (
- "Text", ia->asst.fixed_renderer,
- "text", TEXT_IMPORT_MODEL_COLUMN_LINE,
- (void *) NULL);
- gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
-
- max_line_length = 0;
- for (i = 0; i < ia->file.line_cnt; i++)
- {
- size_t w = ds_length (&ia->file.lines[i]);
- max_line_length = MAX (max_line_length, w);
- }
-
- content_width = get_monospace_width (tree_view, ia->asst.fixed_renderer,
- max_line_length);
- header_width = get_string_width (tree_view, ia->asst.prop_renderer, "Text");
- gtk_tree_view_column_set_fixed_width (column, MAX (content_width,
- header_width));
- gtk_tree_view_append_column (tree_view, column);
-
- 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));
-
- return tree_view;
-}
-
-/* Called when the line selected in the first_line tree view
- changes. */
-static void
-on_first_line_change (GtkTreeSelection *selection UNUSED,
- struct import_assistant *ia)
-{
- get_first_line (ia);
-}
-
-/* Called when the checkbox that indicates whether variable
- names are in the row above the first line is toggled. */
-static void
-on_variable_names_cb_toggle (GtkToggleButton *variable_names_cb UNUSED,
- struct import_assistant *ia)
-{
- get_first_line (ia);
-}
-
-/* Sets the widgets to match IA's first_line substructure. */
-static void
-set_first_line (struct import_assistant *ia)
-{
- GtkTreePath *path;
-
- path = gtk_tree_path_new_from_indices (ia->first_line.skip_lines, -1);
- gtk_tree_view_set_cursor (GTK_TREE_VIEW (ia->first_line.tree_view),
- path, NULL, false);
- gtk_tree_path_free (path);
-
- gtk_toggle_button_set_active (
- GTK_TOGGLE_BUTTON (ia->first_line.variable_names_cb),
- ia->first_line.variable_names);
- gtk_widget_set_sensitive (ia->first_line.variable_names_cb,
- ia->first_line.skip_lines > 0);
-}
-
-/* Sets IA's first_line substructure to match the widgets. */
-static void
-get_first_line (struct import_assistant *ia)
-{
- GtkTreeSelection *selection;
- GtkTreeIter iter;
- GtkTreeModel *model;
-
- selection = gtk_tree_view_get_selection (ia->first_line.tree_view);
- if (gtk_tree_selection_get_selected (selection, &model, &iter))
- {
- GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
- int row = gtk_tree_path_get_indices (path)[0];
- gtk_tree_path_free (path);
-
- ia->first_line.skip_lines = row;
- ia->first_line.variable_names =
- (ia->first_line.skip_lines > 0
- && gtk_toggle_button_get_active (
- GTK_TOGGLE_BUTTON (ia->first_line.variable_names_cb)));
- }
- gtk_widget_set_sensitive (ia->first_line.variable_names_cb,
- ia->first_line.skip_lines > 0);
-}
-\f
-/* The "separators" page of the assistant. */
-
-static void revise_fields_preview (struct import_assistant *ia);
-static void choose_likely_separators (struct import_assistant *ia);
-static void find_commonest_chars (unsigned long int histogram[UCHAR_MAX + 1],
- const char *targets, const char *def,
- struct string *result);
-static void clear_fields (struct import_assistant *ia);
-static void revise_fields_preview (struct import_assistant *);
-static void set_separators (struct import_assistant *);
-static void get_separators (struct import_assistant *);
-static void on_separators_custom_entry_notify (GObject *UNUSED,
- GParamSpec *UNUSED,
- struct import_assistant *);
-static void on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
- struct import_assistant *);
-static void on_quote_combo_change (GtkComboBox *combo,
- struct import_assistant *);
-static void on_quote_cb_toggle (GtkToggleButton *quote_cb,
- struct import_assistant *);
-static void on_separator_toggle (GtkToggleButton *, struct import_assistant *);