Whitespace changes only
[pspp] / src / ui / gui / page-separators.c
index 5432e276637a45b687a41d1c7144a216edd7c99d..6e59202369f960c1b47b9f61c450996c9b9beb27 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, 2015  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 <config.h>
 
+#include "page-separators.h"
+
 #include "ui/gui/text-data-import-dialog.h"
 
 #include <errno.h>
 #include <fcntl.h>
-#include <gtk-contrib/psppire-sheet.h>
 #include <gtk/gtk.h>
 #include <limits.h>
 #include <stdlib.h>
@@ -36,7 +37,6 @@
 #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/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"
 
 #define _(msgid) gettext (msgid)
 #define N_(msgid) msgid
 
-\f
+/* Page where the user chooses field separators. */
+struct separators_page
+  {
+    /* How to break lines into columns. */
+    struct string separators;   /* Field separators. */
+    struct string quotes;       /* Quote characters. */
+
+    GtkWidget *page;
+    GtkWidget *custom_cb;
+    GtkWidget *custom_entry;
+    GtkWidget *quote_cb;
+    GtkWidget *quote_combo;
+    GtkEntry *quote_entry;
+    PsppSheetView *fields_tree_view;
+  };
+
 /* The "separators" page of the assistant. */
 
 static void revise_fields_preview (struct import_assistant *ia);
@@ -104,7 +117,7 @@ static const struct separator separators[] =
 #define SEPARATOR_CNT (sizeof separators / sizeof *separators)
 
 static void
-set_quote_list (GtkComboBoxEntry *cb)
+set_quote_list (GtkComboBox *cb)
 {
   GtkListStore *list =  gtk_list_store_new (1, G_TYPE_STRING);
   GtkTreeIter iter;
@@ -126,31 +139,31 @@ set_quote_list (GtkComboBoxEntry *cb)
   gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
   g_object_unref (list);
 
-  gtk_combo_box_entry_set_text_column (cb, 0);
+  gtk_combo_box_set_entry_text_column (cb, 0);
 }
 
 /* Initializes IA's separators substructure. */
-void
-init_separators_page (struct import_assistant *ia)
+
+struct separators_page *
+separators_page_create (struct import_assistant *ia)
 {
   GtkBuilder *builder = ia->asst.builder;
-  struct separators_page *p = &ia->separators;
+
   size_t i;
 
-  choose_likely_separators (ia);
+  struct separators_page *p = xzalloc (sizeof *p);
 
   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Separators"),
                                    GTK_ASSISTANT_PAGE_CONTENT);
+
   p->custom_cb = get_widget_assert (builder, "custom-cb");
   p->custom_entry = get_widget_assert (builder, "custom-entry");
   p->quote_combo = get_widget_assert (builder, "quote-combo");
   p->quote_entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (p->quote_combo)));
   p->quote_cb = get_widget_assert (builder, "quote-cb");
-  p->escape_cb = get_widget_assert (builder, "escape");
 
-  set_separators (ia);
-  set_quote_list (GTK_COMBO_BOX_ENTRY (p->quote_combo));
-  p->fields_tree_view = GTK_TREE_VIEW (get_widget_assert (builder, "fields"));
+  set_quote_list (GTK_COMBO_BOX (p->quote_combo));
+  p->fields_tree_view = PSPP_SHEET_VIEW (get_widget_assert (builder, "fields"));
   g_signal_connect (p->quote_combo, "changed",
                     G_CALLBACK (on_quote_combo_change), ia);
   g_signal_connect (p->quote_cb, "toggled",
@@ -162,15 +175,15 @@ init_separators_page (struct import_assistant *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);
-  g_signal_connect (p->escape_cb, "toggled",
-                    G_CALLBACK (on_separator_toggle), ia);
+
+  return p;
 }
 
 /* Frees IA's separators substructure. */
 void
 destroy_separators_page (struct import_assistant *ia)
 {
-  struct separators_page *s = &ia->separators;
+  struct separators_page *s = ia->separators;
 
   ds_destroy (&s->separators);
   ds_destroy (&s->quotes);
@@ -199,9 +212,7 @@ reset_separators_page (struct import_assistant *ia)
 static void
 clear_fields (struct import_assistant *ia)
 {
-  struct separators_page *s = &ia->separators;
-
-  if (s->column_cnt > 0)
+  if (ia->column_cnt > 0)
     {
       struct column *col;
       size_t row;
@@ -212,7 +223,7 @@ clear_fields (struct import_assistant *ia)
           const char *line_start = ds_data (line);
           const char *line_end = ds_end (line);
 
-          for (col = s->columns; col < &s->columns[s->column_cnt]; col++)
+          for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
             {
               char *s = ss_data (col->contents[row]);
               if (!(s >= line_start && s <= line_end))
@@ -220,15 +231,15 @@ clear_fields (struct import_assistant *ia)
             }
         }
 
-      for (col = s->columns; col < &s->columns[s->column_cnt]; col++)
+      for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
         {
           free (col->name);
           free (col->contents);
         }
 
-      free (s->columns);
-      s->columns = NULL;
-      s->column_cnt = 0;
+      free (ia->columns);
+      ia->columns = NULL;
+      ia->column_cnt = 0;
     }
 }
 
@@ -237,7 +248,7 @@ clear_fields (struct import_assistant *ia)
 static void
 split_fields (struct import_assistant *ia)
 {
-  struct separators_page *s = &ia->separators;
+  struct separators_page *s = ia->separators;
   size_t columns_allocated;
   bool space_sep;
   size_t row;
@@ -275,42 +286,37 @@ split_fields (struct import_assistant *ia)
                    && ds_find_byte (&s->quotes, text.string[0]) != SIZE_MAX)
             {
               int quote = ss_get_byte (&text);
-              if (!s->escape)
-                ss_get_until (&text, quote, &field);
-              else
-                {
-                  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);
-                }
+              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 (&s->separators)),
                           &field);
 
-          if (column_idx >= s->column_cnt)
+          if (column_idx >= ia->column_cnt)
             {
               struct column *column;
 
-              if (s->column_cnt >= columns_allocated)
-                s->columns = x2nrealloc (s->columns, &columns_allocated,
-                                         sizeof *s->columns);
-              column = &s->columns[s->column_cnt++];
+              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 (ia->file.line_cnt,
                                           sizeof *column->contents);
             }
-          column = &s->columns[column_idx];
+          column = &ia->columns[column_idx];
           column->contents[row] = field;
           if (ss_length (field) > column->width)
             column->width = ss_length (field);
@@ -330,16 +336,14 @@ split_fields (struct import_assistant *ia)
 static void
 choose_column_names (struct import_assistant *ia)
 {
-  const struct first_line_page *f = &ia->first_line;
-  struct separators_page *s = &ia->separators;
   struct dictionary *dict;
   unsigned long int generated_name_count = 0;
   struct column *col;
   size_t name_row;
 
   dict = dict_create (get_default_encoding ());
-  name_row = f->variable_names && f->skip_lines ? f->skip_lines : 0;
-  for (col = s->columns; col < &s->columns[s->column_cnt]; col++)
+  name_row = ia->variable_names && ia->skip_lines ? ia->skip_lines : 0;
+  for (col = ia->columns; col < &ia->columns[ia->column_cnt]; col++)
     {
       char *hint, *name;
 
@@ -372,10 +376,8 @@ choose_likely_separators (struct import_assistant *ia)
         histogram[(unsigned char) line.string[i]]++;
     }
 
-  find_commonest_chars (histogram, "\"'", "", &ia->separators.quotes);
-  find_commonest_chars (histogram, ",;:/|!\t-", ",",
-                        &ia->separators.separators);
-  ia->separators.escape = true;
+  find_commonest_chars (histogram, "\"'", "", &ia->separators->quotes);
+  find_commonest_chars (histogram, ",;:/|!\t-", ",", &ia->separators->separators);
 }
 
 /* Chooses the most common character among those in TARGETS,
@@ -420,12 +422,12 @@ revise_fields_preview (struct import_assistant *ia)
 
   push_watch_cursor (ia);
 
-  w = GTK_WIDGET (ia->separators.fields_tree_view);
+  w = GTK_WIDGET (ia->separators->fields_tree_view);
   gtk_widget_destroy (w);
   get_separators (ia);
   split_fields (ia);
   choose_column_names (ia);
-  ia->separators.fields_tree_view = create_data_tree_view (
+  ia->separators->fields_tree_view = create_data_tree_view (
     true,
     GTK_CONTAINER (get_widget_assert (ia->asst.builder, "fields-scroller")),
     ia);
@@ -437,7 +439,7 @@ revise_fields_preview (struct import_assistant *ia)
 static void
 set_separators (struct import_assistant *ia)
 {
-  struct separators_page *s = &ia->separators;
+  struct separators_page *s = ia->separators;
   unsigned int seps;
   struct string custom;
   bool any_custom;
@@ -485,17 +487,14 @@ set_separators (struct import_assistant *ia)
                       any_quotes ? ds_cstr (&s->quotes) : "\"");
   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->quote_cb),
                                 any_quotes);
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (s->escape_cb),
-                                s->escape);
   gtk_widget_set_sensitive (s->quote_combo, any_quotes);
-  gtk_widget_set_sensitive (s->escape_cb, any_quotes);
 }
 
 /* Sets IA's separators substructure to match the widgets. */
 static void
 get_separators (struct import_assistant *ia)
 {
-  struct separators_page *s = &ia->separators;
+  struct separators_page *s = ia->separators;
   int i;
 
   ds_clear (&s->separators);
@@ -513,14 +512,11 @@ get_separators (struct import_assistant *ia)
 
   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->quote_cb)))
     {
-      gchar *text = gtk_combo_box_get_active_text (
-                      GTK_COMBO_BOX (s->quote_combo));
+      const gchar *text = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (s->quote_combo))));
       ds_assign_cstr (&s->quotes, text);
-      g_free (text);
     }
   else
     ds_clear (&s->quotes);
-  s->escape = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (s->escape_cb));
 }
 
 /* Called when the user changes the entry field for custom
@@ -540,7 +536,7 @@ on_separators_custom_cb_toggle (GtkToggleButton *custom_cb,
                                 struct import_assistant *ia)
 {
   bool is_active = gtk_toggle_button_get_active (custom_cb);
-  gtk_widget_set_sensitive (ia->separators.custom_entry, is_active);
+  gtk_widget_set_sensitive (ia->separators->custom_entry, is_active);
   revise_fields_preview (ia);
 }
 
@@ -558,8 +554,7 @@ static void
 on_quote_cb_toggle (GtkToggleButton *quote_cb, struct import_assistant *ia)
 {
   bool is_active = gtk_toggle_button_get_active (quote_cb);
-  gtk_widget_set_sensitive (ia->separators.quote_combo, is_active);
-  gtk_widget_set_sensitive (ia->separators.escape_cb, is_active);
+  gtk_widget_set_sensitive (ia->separators->quote_combo, is_active);
   revise_fields_preview (ia);
 }
 
@@ -572,3 +567,26 @@ on_separator_toggle (GtkToggleButton *toggle UNUSED,
   revise_fields_preview (ia);
 }
 
+
+
+void 
+separators_append_syntax (const struct import_assistant *ia, struct string *s)
+{
+  int i;
+  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));
+}