First line page construction is initialisation
[pspp] / src / ui / gui / page-first-line.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "ui/gui/text-data-import-dialog.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <gtk-contrib/psppire-sheet.h>
24 #include <gtk/gtk.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28
29 #include "data/data-in.h"
30 #include "data/data-out.h"
31 #include "data/format-guesser.h"
32 #include "data/value-labels.h"
33 #include "language/data-io/data-parser.h"
34 #include "language/lexer/lexer.h"
35 #include "libpspp/assertion.h"
36 #include "libpspp/i18n.h"
37 #include "libpspp/line-reader.h"
38 #include "libpspp/message.h"
39 #include "ui/gui/checkbox-treeview.h"
40 #include "ui/gui/dialog-common.h"
41 #include "ui/gui/executor.h"
42 #include "ui/gui/helper.h"
43 #include "ui/gui/builder-wrapper.h"
44 #include "ui/gui/psppire-data-window.h"
45 #include "ui/gui/psppire-dialog.h"
46 #include "ui/gui/psppire-encoding-selector.h"
47 #include "ui/gui/psppire-empty-list-store.h"
48 #include "ui/gui/psppire-var-sheet.h"
49 #include "ui/gui/psppire-var-store.h"
50 #include "ui/gui/psppire-scanf.h"
51 #include "ui/syntax-gen.h"
52
53 #include "gl/error.h"
54 #include "gl/intprops.h"
55 #include "gl/xalloc.h"
56
57 #include "gettext.h"
58 #define _(msgid) gettext (msgid)
59 #define N_(msgid) msgid
60
61
62
63 /* The "first line" page of the assistant. */
64
65 /* Page where the user chooses the first line of data. */
66 struct first_line_page
67   {
68     GtkWidget *page;
69     GtkTreeView *tree_view;
70     GtkWidget *variable_names_cb;
71   };
72
73 static GtkTreeView *create_lines_tree_view (GtkContainer *parent_window,
74                                             struct import_assistant *);
75 static void on_first_line_change (GtkTreeSelection *,
76                                   struct import_assistant *);
77 static void on_variable_names_cb_toggle (GtkToggleButton *,
78                                          struct import_assistant *);
79 static void set_first_line (struct import_assistant *);
80 static void get_first_line (struct import_assistant *);
81
82 /* Initializes IA's first_line substructure. */
83 struct first_line_page *
84 first_line_page_create (struct import_assistant *ia)
85 {
86   struct first_line_page *p = xzalloc (sizeof *p);
87
88   GtkBuilder *builder = ia->asst.builder;
89
90   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "FirstLine"),
91                                    GTK_ASSISTANT_PAGE_CONTENT);
92   gtk_widget_destroy (get_widget_assert (builder, "first-line"));
93   p->tree_view = create_lines_tree_view (
94     GTK_CONTAINER (get_widget_assert (builder, "first-line-scroller")), ia);
95   p->variable_names_cb = get_widget_assert (builder, "variable-names");
96   gtk_tree_selection_set_mode (
97     gtk_tree_view_get_selection (GTK_TREE_VIEW (p->tree_view)),
98     GTK_SELECTION_BROWSE);
99   g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (p->tree_view)),
100                     "changed", G_CALLBACK (on_first_line_change), ia);
101   g_signal_connect (p->variable_names_cb, "toggled",
102                     G_CALLBACK (on_variable_names_cb_toggle), ia);
103   return p;
104 }
105
106 /* Resets the first_line page to its initial content. */
107 void
108 reset_first_line_page (struct import_assistant *ia)
109 {
110   ia->skip_lines = 0;
111   ia->variable_names = false;
112   set_first_line (ia);
113 }
114
115 static void
116 render_line (GtkTreeViewColumn *tree_column,
117              GtkCellRenderer *cell,
118              GtkTreeModel *tree_model,
119              GtkTreeIter *iter,
120              gpointer data)
121 {
122   gint row = empty_list_store_iter_to_row (iter);
123   struct string *lines;
124
125   lines = g_object_get_data (G_OBJECT (tree_model), "lines");
126   g_return_if_fail (lines != NULL);
127
128   g_object_set (cell, "text", ds_cstr (&lines[row]), NULL);
129 }
130
131
132 /* Creates and returns a tree view that contains each of the
133    lines in IA's file as a row. */
134 static GtkTreeView *
135 create_lines_tree_view (GtkContainer *parent, struct import_assistant *ia)
136 {
137   GtkTreeView *tree_view = NULL;
138   GtkTreeViewColumn *column;
139   size_t max_line_length;
140   gint content_width, header_width;
141   size_t i;
142   const gchar *title = _("Text");
143
144   make_tree_view (ia, 0, &tree_view);
145
146   column = gtk_tree_view_column_new_with_attributes (
147      title, ia->asst.fixed_renderer, (void *) NULL);
148   gtk_tree_view_column_set_cell_data_func (column, ia->asst.fixed_renderer,
149                                            render_line, NULL, NULL);
150   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
151
152   max_line_length = 0;
153   for (i = 0; i < ia->file.line_cnt; i++)
154     {
155       size_t w = ds_length (&ia->file.lines[i]);
156       max_line_length = MAX (max_line_length, w);
157     }
158
159   content_width = get_monospace_width (tree_view, ia->asst.fixed_renderer,
160                                        max_line_length);
161   header_width = get_string_width (tree_view, ia->asst.prop_renderer, title);
162   gtk_tree_view_column_set_fixed_width (column, MAX (content_width,
163                                                      header_width));
164   gtk_tree_view_append_column (tree_view, column);
165
166   gtk_tree_view_set_fixed_height_mode (tree_view, true);
167
168   gtk_container_add (parent, GTK_WIDGET (tree_view));
169   gtk_widget_show (GTK_WIDGET (tree_view));
170
171   return tree_view;
172 }
173
174 /* Called when the line selected in the first_line tree view
175    changes. */
176 static void
177 on_first_line_change (GtkTreeSelection *selection UNUSED,
178                       struct import_assistant *ia)
179 {
180   get_first_line (ia);
181 }
182
183 /* Called when the checkbox that indicates whether variable
184    names are in the row above the first line is toggled. */
185 static void
186 on_variable_names_cb_toggle (GtkToggleButton *variable_names_cb UNUSED,
187                              struct import_assistant *ia)
188 {
189   get_first_line (ia);
190 }
191
192 /* Sets the widgets to match IA's first_line substructure. */
193 static void
194 set_first_line (struct import_assistant *ia)
195 {
196   GtkTreePath *path;
197
198   path = gtk_tree_path_new_from_indices (ia->skip_lines, -1);
199   gtk_tree_view_set_cursor (GTK_TREE_VIEW (ia->first_line->tree_view),
200                             path, NULL, false);
201   gtk_tree_path_free (path);
202
203   gtk_toggle_button_set_active (
204     GTK_TOGGLE_BUTTON (ia->first_line->variable_names_cb),
205     ia->variable_names);
206   gtk_widget_set_sensitive (ia->first_line->variable_names_cb,
207                             ia->skip_lines > 0);
208 }
209
210 /* Sets IA's first_line substructure to match the widgets. */
211 static void
212 get_first_line (struct import_assistant *ia)
213 {
214   GtkTreeSelection *selection;
215   GtkTreeIter iter;
216   GtkTreeModel *model;
217
218   selection = gtk_tree_view_get_selection (ia->first_line->tree_view);
219   if (gtk_tree_selection_get_selected (selection, &model, &iter))
220     {
221       GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
222       int row = gtk_tree_path_get_indices (path)[0];
223       gtk_tree_path_free (path);
224
225       ia->skip_lines = row;
226       ia->variable_names =
227         (ia->skip_lines > 0
228          && gtk_toggle_button_get_active (
229            GTK_TOGGLE_BUTTON (ia->first_line->variable_names_cb)));
230     }
231   gtk_widget_set_sensitive (ia->first_line->variable_names_cb,
232                             ia->skip_lines > 0);
233 }