Make sheet spec page opaque
[pspp] / src / ui / gui / page-sheet-spec.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 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 "data/gnumeric-reader.h"
34 #include "data/ods-reader.h"
35 #include "data/spreadsheet-reader.h"
36 #include "language/data-io/data-parser.h"
37 #include "language/lexer/lexer.h"
38 #include "libpspp/assertion.h"
39 #include "libpspp/i18n.h"
40 #include "libpspp/line-reader.h"
41 #include "libpspp/message.h"
42 #include "ui/gui/checkbox-treeview.h"
43 #include "ui/gui/dialog-common.h"
44 #include "ui/gui/executor.h"
45 #include "ui/gui/helper.h"
46 #include "ui/gui/builder-wrapper.h"
47 #include "ui/gui/psppire-data-window.h"
48 #include "ui/gui/psppire-dialog.h"
49 #include "ui/gui/psppire-encoding-selector.h"
50 #include "ui/gui/psppire-empty-list-store.h"
51 #include "ui/gui/psppire-var-sheet.h"
52 #include "ui/gui/psppire-var-store.h"
53 #include "ui/gui/psppire-scanf.h"
54 #include "ui/syntax-gen.h"
55
56 #include "gl/error.h"
57 #include "gl/intprops.h"
58 #include "gl/xalloc.h"
59
60 #include "gettext.h"
61 #define _(msgid) gettext (msgid)
62 #define N_(msgid) msgid
63
64 struct import_assistant;
65
66
67 /* The "sheet-spec" page of the assistant. */
68
69 /* The sheet_spec page of the assistant (only relevant for spreadsheet imports). */
70 struct sheet_spec_page
71   {
72     GtkWidget *page;
73     struct casereader *reader;
74     struct dictionary *dict;
75     struct spreadsheet *spreadsheet;
76     
77     struct spreadsheet_read_info sri;
78     struct spreadsheet_read_options opts;
79   };
80
81
82
83
84
85 /* Initializes IA's sheet_spec substructure. */
86 struct sheet_spec_page *
87 sheet_spec_page_create (struct import_assistant *ia)
88 {
89   GtkBuilder *builder = ia->asst.builder;
90   struct sheet_spec_page *p = xzalloc (sizeof (*p));
91
92   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Sheet"),
93                                    GTK_ASSISTANT_PAGE_INTRO);
94
95   return p;
96 }
97
98 /* Prepares IA's sheet_spec page. */
99 void
100 prepare_sheet_spec_page (struct import_assistant *ia)
101 {
102   struct sheet_spec_page *p = ia->sheet_spec;
103
104   GtkBuilder *builder = ia->asst.builder;
105   GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
106
107   printf ("%s %d\n", __FUNCTION__, p->spreadsheet->sheets);
108
109   gtk_spin_button_set_digits (GTK_SPIN_BUTTON (sheet_entry), 0);
110   gtk_spin_button_set_range (GTK_SPIN_BUTTON (sheet_entry), 1, p->spreadsheet->sheets);
111 }
112
113
114 /* Resets IA's sheet_spec page to its initial state. */
115 void
116 reset_sheet_spec_page (struct import_assistant *ia)
117 {
118   printf ("%s\n", __FUNCTION__);
119 }
120
121 /* Called when the Forward button is clicked, 
122    but before displaying the new page.
123 */
124 void
125 post_sheet_spec_page (struct import_assistant *ia)
126 {
127   int row_start = -1;
128   int row_stop = -1;
129   int col_start = -1;
130   int col_stop = -1;
131
132   GtkBuilder *builder = ia->asst.builder;
133
134   struct sheet_spec_page *ssp = ia->sheet_spec;
135   struct casereader *creader = NULL;
136   struct dictionary *dict = NULL;
137
138   GtkWidget *sheet_entry = get_widget_assert (builder, "sheet-entry");
139   GtkWidget *range_entry = get_widget_assert (builder, "cell-range-entry");
140   GtkWidget *readnames_checkbox = get_widget_assert (builder, "readnames-checkbox");
141
142   gint num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (sheet_entry));
143
144   const gchar *range = gtk_entry_get_text (GTK_ENTRY (range_entry));
145
146   if ( num < 1 )
147     num = 1;
148   
149   ssp->opts.sheet_name = NULL;
150   ssp->opts.cell_range = NULL;
151   ssp->opts.sheet_index = num;
152
153   if ( convert_cell_ref (range, &col_start, &row_start, &col_stop, &row_stop))
154     {
155       ssp->opts.cell_range = range;
156     }
157
158   ssp->sri.read_names = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (readnames_checkbox));
159   ssp->sri.asw = -1;
160
161   switch (ssp->spreadsheet->type)
162     {
163     case SPREADSHEET_ODS:
164       {
165         creader = ods_make_reader (ssp->spreadsheet, &ssp->sri, &ssp->opts);
166         dict = ssp->spreadsheet->dict;
167       }
168       break;
169     case SPREADSHEET_GNUMERIC:
170       {
171         creader = gnumeric_make_reader (ssp->spreadsheet, &ssp->sri, &ssp->opts);
172         dict = ssp->spreadsheet->dict;
173       }
174       break;
175     default:
176       g_assert_not_reached ();
177       break;
178     }
179
180   ssp->dict = dict;
181   ssp->reader = creader;
182
183   if (creader && dict)
184     {
185       update_assistant (ia);
186     }
187   else
188     {
189       GtkWidget * dialog = gtk_message_dialog_new (NULL,
190                               GTK_DIALOG_MODAL,
191                               GTK_MESSAGE_ERROR,
192                               GTK_BUTTONS_CLOSE,
193                               _("An error occurred reading the spreadsheet file."));
194
195       gtk_dialog_run (GTK_DIALOG (dialog));
196       gtk_widget_destroy (dialog);
197     }
198 }
199
200
201 /*
202   Update IA according to the contents of DICT and CREADER.
203   CREADER will be destroyed by this function.
204 */
205 void 
206 update_assistant (struct import_assistant *ia)
207 {
208   struct sheet_spec_page *ssp = ia->sheet_spec;
209   //  struct file *file = &ia->file;
210   struct separators_page *sepp = ia->separators;
211   int rows = 0;
212
213
214   if (ssp->dict)
215     {
216       struct ccase *c;
217       int col;
218
219       sepp->column_cnt = dict_get_var_cnt (ssp->dict);
220       sepp->columns = xcalloc (sepp->column_cnt, sizeof (*sepp->columns));
221       for (col = 0; col < sepp->column_cnt ; ++col)
222         {
223           const struct variable *var = dict_get_var (ssp->dict, col);
224           sepp->columns[col].name = xstrdup (var_get_name (var));
225           sepp->columns[col].contents = NULL;
226         }
227
228       for (; (c = casereader_read (ssp->reader)) != NULL; case_unref (c))
229         {
230           rows++;
231           for (col = 0; col < sepp->column_cnt ; ++col)
232             {
233               char *ss;
234               const struct variable *var = dict_get_var (ssp->dict, col);
235
236               sepp->columns[col].contents = xrealloc (sepp->columns[col].contents,
237                                                       sizeof (struct substring) * rows);
238
239               ss = data_out (case_data (c, var), dict_get_encoding (ssp->dict), 
240                              var_get_print_format (var));
241
242               sepp->columns[col].contents[rows - 1] = ss_cstr (ss);
243             }
244
245           if (rows > MAX_PREVIEW_LINES)
246             {
247               case_unref (c);
248               break;
249             }
250         }
251     }
252   
253   //  file->line_cnt = rows;
254 }