Merge 'psppsheet' into 'master'.
[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 "page-first-line.h"
20
21 #include "ui/gui/text-data-import-dialog.h"
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <gtk/gtk.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29
30 #include "data/data-in.h"
31 #include "data/data-out.h"
32 #include "data/format-guesser.h"
33 #include "data/value-labels.h"
34 #include "language/data-io/data-parser.h"
35 #include "language/lexer/lexer.h"
36 #include "libpspp/assertion.h"
37 #include "libpspp/i18n.h"
38 #include "libpspp/line-reader.h"
39 #include "libpspp/message.h"
40 #include "ui/gui/checkbox-treeview.h"
41 #include "ui/gui/dialog-common.h"
42 #include "ui/gui/executor.h"
43 #include "ui/gui/helper.h"
44 #include "ui/gui/builder-wrapper.h"
45 #include "ui/gui/pspp-sheet-selection.h"
46 #include "ui/gui/pspp-sheet-view-column.h"
47 #include "ui/gui/pspp-sheet-view.h"
48 #include "ui/gui/psppire-data-window.h"
49 #include "ui/gui/psppire-dialog.h"
50 #include "ui/gui/psppire-encoding-selector.h"
51 #include "ui/gui/psppire-empty-list-store.h"
52 #include "ui/gui/psppire-var-sheet.h"
53 #include "ui/gui/psppire-scanf.h"
54
55 #include "gl/error.h"
56 #include "gl/intprops.h"
57 #include "gl/xalloc.h"
58
59 #include "gettext.h"
60 #define _(msgid) gettext (msgid)
61 #define N_(msgid) msgid
62
63
64
65 /* The "first line" page of the assistant. */
66
67 /* Page where the user chooses the first line of data. */
68 struct first_line_page
69   {
70     GtkWidget *page;
71     PsppSheetView *tree_view;
72     GtkWidget *variable_names_cb;
73   };
74
75 static PsppSheetView *create_lines_tree_view (GtkContainer *parent_window,
76                                               struct import_assistant *);
77 static void on_first_line_change (GtkTreeSelection *,
78                                   struct import_assistant *);
79 static void on_variable_names_cb_toggle (GtkToggleButton *,
80                                          struct import_assistant *);
81 static void set_first_line (struct import_assistant *);
82 static void get_first_line (struct import_assistant *);
83
84 /* Initializes IA's first_line substructure. */
85 struct first_line_page *
86 first_line_page_create (struct import_assistant *ia)
87 {
88   struct first_line_page *p = xzalloc (sizeof *p);
89
90   GtkBuilder *builder = ia->asst.builder;
91
92   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "FirstLine"),
93                                    GTK_ASSISTANT_PAGE_CONTENT);
94
95   gtk_widget_destroy (get_widget_assert (builder, "first-line"));
96   p->tree_view = create_lines_tree_view (
97     GTK_CONTAINER (get_widget_assert (builder, "first-line-scroller")), ia);
98   p->variable_names_cb = get_widget_assert (builder, "variable-names");
99   pspp_sheet_selection_set_mode (
100     pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (p->tree_view)),
101     PSPP_SHEET_SELECTION_BROWSE);
102   pspp_sheet_view_set_rubber_banding (PSPP_SHEET_VIEW (p->tree_view), TRUE);
103   g_signal_connect (pspp_sheet_view_get_selection (PSPP_SHEET_VIEW (p->tree_view)),
104                     "changed", G_CALLBACK (on_first_line_change), ia);
105   g_signal_connect (p->variable_names_cb, "toggled",
106                     G_CALLBACK (on_variable_names_cb_toggle), ia);
107   return p;
108 }
109
110 /* Resets the first_line page to its initial content. */
111 void
112 reset_first_line_page (struct import_assistant *ia)
113 {
114   ia->skip_lines = 0;
115   ia->variable_names = false;
116   set_first_line (ia);
117 }
118
119 static void
120 render_line (PsppSheetViewColumn *tree_column,
121              GtkCellRenderer *cell,
122              GtkTreeModel *tree_model,
123              GtkTreeIter *iter,
124              gpointer data)
125 {
126   gint row = empty_list_store_iter_to_row (iter);
127   struct string *lines;
128
129   lines = g_object_get_data (G_OBJECT (tree_model), "lines");
130   g_return_if_fail (lines != NULL);
131
132   g_object_set (cell, "text", ds_cstr (&lines[row]), NULL);
133 }
134
135
136 /* Creates and returns a tree view that contains each of the
137    lines in IA's file as a row. */
138 static PsppSheetView *
139 create_lines_tree_view (GtkContainer *parent, struct import_assistant *ia)
140 {
141   PsppSheetView *tree_view = NULL;
142   PsppSheetViewColumn *column;
143   size_t max_line_length;
144   gint content_width, header_width;
145   size_t i;
146   const gchar *title = _("Text");
147
148   make_tree_view (ia, 0, &tree_view);
149
150   column = pspp_sheet_view_column_new_with_attributes (
151      title, ia->asst.fixed_renderer, (void *) NULL);
152   pspp_sheet_view_column_set_cell_data_func (column, ia->asst.fixed_renderer,
153                                            render_line, NULL, NULL);
154   pspp_sheet_view_column_set_resizable (column, TRUE);
155
156   max_line_length = 0;
157   for (i = 0; i < ia->file.line_cnt; i++)
158     {
159       size_t w = ds_length (&ia->file.lines[i]);
160       max_line_length = MAX (max_line_length, w);
161     }
162
163   content_width = get_monospace_width (tree_view, ia->asst.fixed_renderer,
164                                        max_line_length);
165   header_width = get_string_width (tree_view, ia->asst.prop_renderer, title);
166   pspp_sheet_view_column_set_fixed_width (column, MAX (content_width,
167                                                      header_width));
168   pspp_sheet_view_append_column (tree_view, column);
169
170   gtk_container_add (parent, GTK_WIDGET (tree_view));
171   gtk_widget_show (GTK_WIDGET (tree_view));
172
173   return tree_view;
174 }
175
176 /* Called when the line selected in the first_line tree view
177    changes. */
178 static void
179 on_first_line_change (GtkTreeSelection *selection UNUSED,
180                       struct import_assistant *ia)
181 {
182   get_first_line (ia);
183 }
184
185 /* Called when the checkbox that indicates whether variable
186    names are in the row above the first line is toggled. */
187 static void
188 on_variable_names_cb_toggle (GtkToggleButton *variable_names_cb UNUSED,
189                              struct import_assistant *ia)
190 {
191   get_first_line (ia);
192 }
193
194 /* Sets the widgets to match IA's first_line substructure. */
195 static void
196 set_first_line (struct import_assistant *ia)
197 {
198   GtkTreePath *path;
199
200   path = gtk_tree_path_new_from_indices (ia->skip_lines, -1);
201   pspp_sheet_view_set_cursor (PSPP_SHEET_VIEW (ia->first_line->tree_view),
202                             path, NULL, false);
203   gtk_tree_path_free (path);
204
205   gtk_toggle_button_set_active (
206     GTK_TOGGLE_BUTTON (ia->first_line->variable_names_cb),
207     ia->variable_names);
208   gtk_widget_set_sensitive (ia->first_line->variable_names_cb,
209                             ia->skip_lines > 0);
210 }
211
212 /* Sets IA's first_line substructure to match the widgets. */
213 static void
214 get_first_line (struct import_assistant *ia)
215 {
216   PsppSheetSelection *selection;
217   GtkTreeIter iter;
218   GtkTreeModel *model;
219
220   selection = pspp_sheet_view_get_selection (ia->first_line->tree_view);
221   if (pspp_sheet_selection_get_selected (selection, &model, &iter))
222     {
223       GtkTreePath *path = gtk_tree_model_get_path (model, &iter);
224       int row = gtk_tree_path_get_indices (path)[0];
225       gtk_tree_path_free (path);
226
227       ia->skip_lines = row;
228       ia->variable_names =
229         (ia->skip_lines > 0
230          && gtk_toggle_button_get_active (
231            GTK_TOGGLE_BUTTON (ia->first_line->variable_names_cb)));
232     }
233   gtk_widget_set_sensitive (ia->first_line->variable_names_cb,
234                             ia->skip_lines > 0);
235 }
236
237
238
239 void
240 first_line_append_syntax (const struct import_assistant *ia, struct string *s)
241 {
242   if (ia->skip_lines > 0)
243     ds_put_format (s, "  /FIRSTCASE=%d\n", ia->skip_lines + 1);
244 }