formats page: Construction is initialisation
[pspp] / src / ui / gui / page-assistant.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
51 #include "gl/error.h"
52 #include "gl/intprops.h"
53 #include "gl/xalloc.h"
54
55 #include "gettext.h"
56 #define _(msgid) gettext (msgid)
57 #define N_(msgid) msgid
58
59 /* Assistant. */
60
61 static void close_assistant (struct import_assistant *, int response);
62 static void on_prepare (GtkAssistant *assistant, GtkWidget *page,
63                         struct import_assistant *);
64 static void on_cancel (GtkAssistant *assistant, struct import_assistant *);
65 static void on_close (GtkAssistant *assistant, struct import_assistant *);
66 static void on_paste (GtkButton *button, struct import_assistant *);
67 static void on_reset (GtkButton *button, struct import_assistant *);
68 static void close_assistant (struct import_assistant *, int response);
69
70 /* Initializes IA's asst substructure.  PARENT_WINDOW must be the
71    window to use as the assistant window's parent.  */
72 struct import_assistant *
73 init_assistant (GtkWindow *parent_window)
74 {
75   struct import_assistant *ia = xzalloc (sizeof *ia);
76   struct assistant *a = &ia->asst;
77
78   a->builder = builder_new ("text-data-import.ui");
79   a->assistant = GTK_ASSISTANT (gtk_assistant_new ());
80
81   ia->sheet_spec = sheet_spec_page_create (ia);
82   ia->intro = intro_page_create (ia);
83   ia->separators = separators_page_create (ia);
84
85
86   a->prop_renderer = gtk_cell_renderer_text_new ();
87   g_object_ref_sink (a->prop_renderer);
88   a->fixed_renderer = gtk_cell_renderer_text_new ();
89   g_object_ref_sink (a->fixed_renderer);
90   g_object_set (G_OBJECT (a->fixed_renderer),
91                 "family", "Monospace",
92                 (void *) NULL);
93
94   ia->first_line = first_line_page_create (ia);
95
96   ia->formats = formats_page_create (ia);
97
98   g_signal_connect (a->assistant, "prepare", G_CALLBACK (on_prepare), ia);
99   g_signal_connect (a->assistant, "cancel", G_CALLBACK (on_cancel), ia);
100   g_signal_connect (a->assistant, "close", G_CALLBACK (on_close), ia);
101   a->paste_button = gtk_button_new_from_stock (GTK_STOCK_PASTE);
102   gtk_assistant_add_action_widget (a->assistant, a->paste_button);
103   g_signal_connect (a->paste_button, "clicked", G_CALLBACK (on_paste), ia);
104   a->reset_button = gtk_button_new_from_stock ("pspp-stock-reset");
105   gtk_assistant_add_action_widget (a->assistant, a->reset_button);
106   g_signal_connect (a->reset_button, "clicked", G_CALLBACK (on_reset), ia);
107   gtk_window_set_title (GTK_WINDOW (a->assistant),
108                         _("Importing Delimited Text Data"));
109   gtk_window_set_transient_for (GTK_WINDOW (a->assistant), parent_window);
110   gtk_window_set_icon_name (GTK_WINDOW (a->assistant), "pspp");
111
112   return ia;
113 }
114
115 /* Frees IA's asst substructure. */
116 void
117 destroy_assistant (struct import_assistant *ia)
118 {
119   struct assistant *a = &ia->asst;
120
121   g_object_unref (a->prop_renderer);
122   g_object_unref (a->fixed_renderer);
123   g_object_unref (a->builder);
124 }
125
126 /* Appends a page of the given TYPE, with PAGE as its content, to
127    the GtkAssistant encapsulated by IA.  Returns the GtkWidget
128    that represents the page. */
129 GtkWidget *
130 add_page_to_assistant (struct import_assistant *ia,
131                        GtkWidget *page, GtkAssistantPageType type)
132 {
133   const char *title;
134   char *title_copy;
135   GtkWidget *content;
136
137   title = gtk_window_get_title (GTK_WINDOW (page));
138   title_copy = xstrdup (title ? title : "");
139
140   content = gtk_bin_get_child (GTK_BIN (page));
141   assert (content);
142   g_object_ref (content);
143   gtk_container_remove (GTK_CONTAINER (page), content);
144
145   gtk_widget_destroy (page);
146
147   gtk_assistant_append_page (ia->asst.assistant, content);
148   gtk_assistant_set_page_type (ia->asst.assistant, content, type);
149   gtk_assistant_set_page_title (ia->asst.assistant, content, title_copy);
150   gtk_assistant_set_page_complete (ia->asst.assistant, content, true);
151
152   free (title_copy);
153
154   return content;
155 }
156
157 /* Called just before PAGE is displayed as the current page of
158    ASSISTANT, this updates IA content according to the new
159    page. */
160 static void
161 on_prepare (GtkAssistant *assistant, GtkWidget *page,
162             struct import_assistant *ia)
163 {
164   struct sheet_spec_page *ssp = ia->sheet_spec;
165
166   int pn = gtk_assistant_get_current_page (assistant);
167   g_print ("%s:%d Page %d %p\n", __FILE__, __LINE__, pn, page);
168
169 #if 0  
170   if (pn == 1 && ssp->spreadsheet)
171     post_sheet_spec_page (ia);
172
173   if (gtk_assistant_get_page_type (assistant, page)
174       == GTK_ASSISTANT_PAGE_CONFIRM)
175     gtk_widget_grab_focus (assistant->apply);
176   else
177     gtk_widget_grab_focus (assistant->forward);
178
179
180   /* Prepare .... */
181   if (page == ia->separators->page)
182     prepare_separators_page (ia);
183   else if (page == ia->formats->page)
184     prepare_formats_page (ia);
185   else if (page == ia->sheet_spec->page && ssp->spreadsheet)
186     {
187       prepare_sheet_spec_page (ia);
188     }
189
190
191
192   
193   gtk_widget_show (ia->asst.reset_button);
194   if (page == ia->formats->page)
195     gtk_widget_show (ia->asst.paste_button);
196   else
197     gtk_widget_hide (ia->asst.paste_button);
198
199 #endif
200 }
201
202 /* Called when the Cancel button in the assistant is clicked. */
203 static void
204 on_cancel (GtkAssistant *assistant, struct import_assistant *ia)
205 {
206   close_assistant (ia, GTK_RESPONSE_CANCEL);
207 }
208
209 /* Called when the Apply button on the last page of the assistant
210    is clicked. */
211 static void
212 on_close (GtkAssistant *assistant, struct import_assistant *ia)
213 {
214   close_assistant (ia, GTK_RESPONSE_APPLY);
215 }
216
217 /* Called when the Paste button on the last page of the assistant
218    is clicked. */
219 static void
220 on_paste (GtkButton *button, struct import_assistant *ia)
221 {
222   close_assistant (ia, PSPPIRE_RESPONSE_PASTE);
223 }
224
225 /* Called when the Reset button is clicked. */
226 static void
227 on_reset (GtkButton *button, struct import_assistant *ia)
228 {
229 #if 0
230   gint page_num = gtk_assistant_get_current_page (ia->asst.assistant);
231   GtkWidget *page = gtk_assistant_get_nth_page (ia->asst.assistant, page_num);
232
233   if (page == ia->intro->page)
234     reset_intro_page (ia);
235   else if (page == ia->first_line->page)
236     reset_first_line_page (ia);
237   else if (page == ia->separators->page)
238     reset_separators_page (ia);
239   else if (page == ia->formats->page)
240     reset_formats_page (ia);
241   else if (page == ia->sheet_spec->page)
242     reset_sheet_spec_page (ia);
243 #endif
244 }
245
246 /* Causes the assistant to close, returning RESPONSE for
247    interpretation by text_data_import_assistant. */
248 static void
249 close_assistant (struct import_assistant *ia, int response)
250 {
251   ia->asst.response = response;
252   g_main_loop_quit (ia->asst.main_loop);
253   gtk_widget_hide (GTK_WIDGET (ia->asst.assistant));
254 }
255