Whitespace changes only
[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/gtk.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
27
28 #include "data/data-in.h"
29 #include "data/data-out.h"
30 #include "data/format-guesser.h"
31 #include "data/value-labels.h"
32 #include "language/data-io/data-parser.h"
33 #include "language/lexer/lexer.h"
34 #include "libpspp/assertion.h"
35 #include "libpspp/i18n.h"
36 #include "libpspp/line-reader.h"
37 #include "libpspp/message.h"
38 #include "ui/gui/dialog-common.h"
39 #include "ui/gui/executor.h"
40 #include "ui/gui/helper.h"
41 #include "ui/gui/builder-wrapper.h"
42 #include "ui/gui/psppire-data-window.h"
43 #include "ui/gui/psppire-dialog.h"
44 #include "ui/gui/psppire-encoding-selector.h"
45 #include "ui/gui/psppire-empty-list-store.h"
46 #include "ui/gui/psppire-var-sheet.h"
47
48 #include "gl/intprops.h"
49 #include "gl/xalloc.h"
50
51 #include "gettext.h"
52 #define _(msgid) gettext (msgid)
53 #define N_(msgid) msgid
54
55 /* Assistant. */
56
57 static void close_assistant (struct import_assistant *, int response);
58 static void on_prepare (GtkAssistant *assistant, GtkWidget *page,
59                         struct import_assistant *);
60 static void on_cancel (GtkAssistant *assistant, struct import_assistant *);
61 static void on_close (GtkAssistant *assistant, struct import_assistant *);
62 static void on_paste (GtkButton *button, struct import_assistant *);
63 static void on_reset (GtkButton *button, struct import_assistant *);
64
65 /* Initializes IA's asst substructure.  PARENT_WINDOW must be the
66    window to use as the assistant window's parent.  */
67 struct import_assistant *
68 init_assistant (GtkWindow *parent_window)
69 {
70   struct import_assistant *ia = xzalloc (sizeof *ia);
71   struct assistant *a = &ia->asst;
72
73   a->builder = builder_new ("text-data-import.ui");
74   a->assistant = GTK_ASSISTANT (gtk_assistant_new ());
75
76   a->prop_renderer = gtk_cell_renderer_text_new ();
77   g_object_ref_sink (a->prop_renderer);
78   a->fixed_renderer = gtk_cell_renderer_text_new ();
79   g_object_ref_sink (a->fixed_renderer);
80   g_object_set (G_OBJECT (a->fixed_renderer),
81                 "family", "Monospace",
82                 (void *) NULL);
83
84   g_signal_connect (a->assistant, "prepare", G_CALLBACK (on_prepare), ia);
85   g_signal_connect (a->assistant, "cancel", G_CALLBACK (on_cancel), ia);
86   g_signal_connect (a->assistant, "close", G_CALLBACK (on_close), ia);
87   a->paste_button = gtk_button_new_with_label (_("Paste"));
88   gtk_assistant_add_action_widget (a->assistant, a->paste_button);
89   g_signal_connect (a->paste_button, "clicked", G_CALLBACK (on_paste), ia);
90   a->reset_button = gtk_button_new_with_label (_("Reset"));
91   gtk_assistant_add_action_widget (a->assistant, a->reset_button);
92   g_signal_connect (a->reset_button, "clicked", G_CALLBACK (on_reset), ia);
93   gtk_window_set_title (GTK_WINDOW (a->assistant),
94                         _("Importing Delimited Text Data"));
95   gtk_window_set_transient_for (GTK_WINDOW (a->assistant), parent_window);
96   gtk_window_set_icon_name (GTK_WINDOW (a->assistant), "pspp");
97
98
99   return ia;
100 }
101
102 /* Frees IA's asst substructure. */
103 void
104 destroy_assistant (struct import_assistant *ia)
105 {
106   struct assistant *a = &ia->asst;
107
108   g_object_unref (a->prop_renderer);
109   g_object_unref (a->fixed_renderer);
110   g_object_unref (a->builder);
111 }
112
113 /* Appends a page of the given TYPE, with PAGE as its content, to
114    the GtkAssistant encapsulated by IA.  Returns the GtkWidget
115    that represents the page. */
116 GtkWidget *
117 add_page_to_assistant (struct import_assistant *ia,
118                        GtkWidget *page, GtkAssistantPageType type)
119 {
120   const char *title;
121   char *title_copy;
122   GtkWidget *content;
123
124   title = gtk_window_get_title (GTK_WINDOW (page));
125   title_copy = xstrdup (title ? title : "");
126
127   content = gtk_bin_get_child (GTK_BIN (page));
128   assert (content);
129   g_object_ref (content);
130   gtk_container_remove (GTK_CONTAINER (page), content);
131
132   gtk_widget_destroy (page);
133
134   gtk_assistant_append_page (ia->asst.assistant, content);
135   gtk_assistant_set_page_type (ia->asst.assistant, content, type);
136   gtk_assistant_set_page_title (ia->asst.assistant, content, title_copy);
137   gtk_assistant_set_page_complete (ia->asst.assistant, content, true);
138
139   free (title_copy);
140
141   return content;
142 }
143
144 /* Called just before PAGE is displayed as the current page of
145    ASSISTANT, this updates IA content according to the new
146    page. */
147 static void
148 on_prepare (GtkAssistant *assistant, GtkWidget *page,
149             struct import_assistant *ia)
150 {
151   int pn = gtk_assistant_get_current_page (assistant);
152
153   gtk_widget_show (ia->asst.reset_button);
154   gtk_widget_hide (ia->asst.paste_button);
155
156   if ( ia->spreadsheet) 
157     {
158       if (pn == 0)
159         {
160           prepare_sheet_spec_page (ia);
161         }
162       else if (pn == 1)
163         {
164           post_sheet_spec_page (ia);
165           prepare_formats_page (ia);
166         }
167     }
168   else
169     {
170       switch (pn)
171         {
172         case 0:
173           reset_intro_page (ia);
174           break;
175         case 1:
176           reset_first_line_page (ia);
177           break;
178         case 2:
179           prepare_separators_page (ia);
180           reset_separators_page (ia);
181           break;
182         case 3:
183           prepare_formats_page (ia);
184           reset_formats_page (ia);
185           break;
186         }
187     }
188
189
190 #if GTK3_TRANSITION
191   if (gtk_assistant_get_page_type (assistant, page)
192       == GTK_ASSISTANT_PAGE_CONFIRM)
193     gtk_widget_grab_focus (assistant->apply);
194   else
195     gtk_widget_grab_focus (assistant->forward);
196 #endif
197 }
198
199 /* Called when the Cancel button in the assistant is clicked. */
200 static void
201 on_cancel (GtkAssistant *assistant, struct import_assistant *ia)
202 {
203   close_assistant (ia, GTK_RESPONSE_CANCEL);
204 }
205
206 /* Called when the Apply button on the last page of the assistant
207    is clicked. */
208 static void
209 on_close (GtkAssistant *assistant, struct import_assistant *ia)
210 {
211   close_assistant (ia, GTK_RESPONSE_APPLY);
212 }
213
214 /* Called when the Paste button on the last page of the assistant
215    is clicked. */
216 static void
217 on_paste (GtkButton *button, struct import_assistant *ia)
218 {
219   close_assistant (ia, PSPPIRE_RESPONSE_PASTE);
220 }
221
222 /* Called when the Reset button is clicked. */
223 static void
224 on_reset (GtkButton *button, struct import_assistant *ia)
225 {
226   gint pn = gtk_assistant_get_current_page (ia->asst.assistant);
227   
228   if ( ia->spreadsheet) 
229     {
230       switch (pn)
231         {
232         case 0:
233           reset_sheet_spec_page (ia);
234          break;
235         case 1:
236           reset_formats_page (ia);
237           break;
238         }
239     }
240   else
241     {
242       switch (pn)
243         {
244         case 0:
245           reset_intro_page (ia);
246           break;
247         case 1:
248           reset_first_line_page (ia);
249           break;
250         case 2:
251           reset_separators_page (ia);
252           break;
253         case 3:
254           reset_formats_page (ia);
255           break;
256         }
257     }
258 }
259
260 /* Causes the assistant to close, returning RESPONSE for
261    interpretation by text_data_import_assistant. */
262 static void
263 close_assistant (struct import_assistant *ia, int response)
264 {
265   ia->asst.response = response;
266   /*  Use our loop_done variable until we find out
267       why      g_main_loop_quit (ia->asst.main_loop); doesn't work.
268   */
269   ia->asst.loop_done = true;
270   gtk_widget_hide (GTK_WIDGET (ia->asst.assistant));
271 }
272