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