2ccdb918b21c0d90540ea83063f34de90e3fcdb4
[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 /* The "first line" page of the assistant. */
63
64 static GtkTreeView *create_lines_tree_view (GtkContainer *parent_window,
65                                             struct import_assistant *);
66 static void on_first_line_change (GtkTreeSelection *,
67                                   struct import_assistant *);
68 static void on_variable_names_cb_toggle (GtkToggleButton *,
69                                          struct import_assistant *);
70 static void set_first_line (struct import_assistant *);
71 static void get_first_line (struct import_assistant *);
72
73 /* Initializes IA's first_line substructure. */
74 void
75 init_first_line_page (struct import_assistant *ia)
76 {
77   struct first_line_page *p = &ia->first_line;
78   GtkBuilder *builder = ia->asst.builder;
79
80   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "FirstLine"),
81                                    GTK_ASSISTANT_PAGE_CONTENT);
82   gtk_widget_destroy (get_widget_assert (builder, "first-line"));
83   p->tree_view = create_lines_tree_view (
84     GTK_CONTAINER (get_widget_assert (builder, "first-line-scroller")), ia);
85   p->variable_names_cb = get_widget_assert (builder, "variable-names");
86   gtk_tree_selection_set_mode (
87     gtk_tree_view_get_selection (GTK_TREE_VIEW (p->tree_view)),
88     GTK_SELECTION_BROWSE);
89   set_first_line (ia);
90   g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (p->tree_view)),
91                     "changed", G_CALLBACK (on_first_line_change), ia);
92   g_signal_connect (p->variable_names_cb, "toggled",
93                     G_CALLBACK (on_variable_names_cb_toggle), ia);
94 }
95
96 /* Resets the first_line page to its initial content. */
97 void
98 reset_first_line_page (struct import_assistant *ia)
99 {
100   ia->first_line.skip_lines = 0;
101   ia->first_line.variable_names = false;
102   set_first_line (ia);
103 }
104
105 static void
106 render_line (GtkTreeViewColumn *tree_column,
107              GtkCellRenderer *cell,
108              GtkTreeModel *tree_model,
109              GtkTreeIter *iter,
110              gpointer data)
111 {
112   gint row = empty_list_store_iter_to_row (iter);
113   struct string *lines;
114
115   lines = g_object_get_data (G_OBJECT (tree_model), "lines");
116   g_return_if_fail (lines != NULL);
117
118   g_object_set (cell, "text", ds_cstr (&lines[row]), NULL);
119 }
120
121
122 /* Creates and returns a tree view that contains each of the
123    lines in IA's file as a row. */
124 static GtkTreeView *
125 create_lines_tree_view (GtkContainer *parent, struct import_assistant *ia)
126 {
127   GtkTreeView *tree_view;
128   GtkTreeViewColumn *column;
129   size_t max_line_length;
130   gint content_width, header_width;
131   size_t i;
132   const gchar *title = _("Text");
133
134   make_tree_view (ia, 0, &tree_view);
135
136   column = gtk_tree_view_column_new_with_attributes (
137      title, ia->asst.fixed_renderer, (void *) NULL);
138   gtk_tree_view_column_set_cell_data_func (column, ia->asst.fixed_renderer,
139                                            render_line, NULL, NULL);
140   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
141
142   max_line_length = 0;
143   for (i = 0; i < ia->file.line_cnt; i++)
144     {
145       size_t w = ds_length (&ia->file.lines[i]);
146       max_line_length = MAX (max_line_length, w);
147     }
148
149   content_width = get_monospace_width (tree_view, ia->asst.fixed_renderer,
150                                        max_line_length);
151   header_width = get_string_width (tree_view, ia->asst.prop_renderer, title);
152   gtk_tree_view_column_set_fixed_width (column, MAX (content_width,
153                                                      header_width));
154   gtk_tree_view_append_column (tree_view, column);
155
156   gtk_tree_view_set_fixed_height_mode (tree_view, true);
157
158   gtk_container_add (parent, GTK_WIDGET (tree_view));
159   gtk_widget_show (GTK_WIDGET (tree_view));
160
161   return tree_view;
162 }
163
164 /* Called when the line selected in the first_line tree view
165    changes. */
166 static void
167 on_first_line_change (GtkTreeSelection *selection UNUSED,
168                       struct import_assistant *ia)
169 {
170   get_first_line (ia);
171 }
172
173 /* Called when the checkbox that indicates whether variable
174    names are in the row above the first line is toggled. */
175 static void
176 on_variable_names_cb_toggle (GtkToggleButton *variable_names_cb UNUSED,
177                              struct import_assistant *ia)
178 {
179   get_first_line (ia);
180 }
181
182 /* Sets the widgets to match IA's first_line substructure. */
183 static void
184 set_first_line (struct import_assistant *ia)
185 {
186   GtkTreePath *path;
187
188   path = gtk_tree_path_new_from_indices (ia->first_line.skip_lines, -1);
189   gtk_tree_view_set_cursor (GTK_TREE_VIEW (ia->first_line.tree_view),
190                             path, NULL, false);
191   gtk_tree_path_free (path);
192
193   gtk_toggle_button_set_active (
194     GTK_TOGGLE_BUTTON (ia->first_line.variable_names_cb),
195     ia->first_line.variable_names);
196   gtk_widget_set_sensitive (ia->first_line.variable_names_cb,
197                             ia->first_line.skip_lines > 0);
198 }
199
200 /* Sets IA's first_line substructure to match the widgets. */
201 static void
202 get_first_line (struct import_assistant *ia)
203 {
204   GtkTreeSelection *selection;
205   GtkTreeIter iter;
206   GtkTreeModel *model;
207
208   selection = gtk_tree_view_get_selection (ia->first_line.tree_view);
209   if (gtk_tree_selection_get_selected (selection, &model, &iter))
210     {
211       GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
212       int row = gtk_tree_path_get_indices (path)[0];
213       gtk_tree_path_free (path);
214
215       ia->first_line.skip_lines = row;
216       ia->first_line.variable_names =
217         (ia->first_line.skip_lines > 0
218          && gtk_toggle_button_get_active (
219            GTK_TOGGLE_BUTTON (ia->first_line.variable_names_cb)));
220     }
221   gtk_widget_set_sensitive (ia->first_line.variable_names_cb,
222                             ia->first_line.skip_lines > 0);
223 }