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