sort of got the assistant working with the combo model
[pspp] / src / ui / gui / page-intro.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 #include "ui/gui/psppire-scanf.h"
51 #include "ui/syntax-gen.h"
52
53 #include "gl/error.h"
54 #include "gl/intprops.h"
55 #include "gl/xalloc.h"
56
57 #include "gettext.h"
58 #define _(msgid) gettext (msgid)
59 #define N_(msgid) msgid
60
61 struct import_assistant;
62
63
64 \f
65 /* The "intro" page of the assistant. */
66
67 /* The introduction page of the assistant. */
68 struct intro_page
69   {
70     GtkWidget *page;
71     GtkWidget *all_cases_button;
72     GtkWidget *n_cases_button;
73     GtkWidget *n_cases_spin;
74     GtkWidget *percent_button;
75     GtkWidget *percent_spin;
76   };
77
78 static void on_intro_amount_changed (struct intro_page *);
79
80 /* Initializes IA's intro substructure. */
81 struct intro_page *
82 intro_page_create (struct import_assistant *ia)
83 {
84   GtkBuilder *builder = ia->asst.builder;
85   struct string s;
86   GtkWidget *hbox_n_cases ;
87   GtkWidget *hbox_percent ;
88   GtkWidget *table ;
89
90   struct intro_page *p = xzalloc (sizeof (*p));
91
92   p->n_cases_spin = gtk_spin_button_new_with_range (0, INT_MAX, 100);
93
94   hbox_n_cases = psppire_scanf_new (_("Only the first %4d cases"), &p->n_cases_spin);
95
96   table  = get_widget_assert (builder, "button-table");
97
98   gtk_table_attach_defaults (GTK_TABLE (table), hbox_n_cases,
99                     1, 2,
100                     1, 2);
101
102   p->percent_spin = gtk_spin_button_new_with_range (0, 100, 10);
103
104   hbox_percent = psppire_scanf_new (_("Only the first %3d %% of file (approximately)"), &p->percent_spin);
105
106   gtk_table_attach_defaults (GTK_TABLE (table), hbox_percent,
107                              1, 2,
108                              2, 3);
109
110 #if 0
111   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Intro"),
112                                    GTK_ASSISTANT_PAGE_INTRO);
113
114   g_print ("%s:%d Added page %p\n", __FILE__, __LINE__, p->page);
115 #endif  
116
117   p->all_cases_button = get_widget_assert (builder, "import-all-cases");
118
119   p->n_cases_button = get_widget_assert (builder, "import-n-cases");
120
121   p->percent_button = get_widget_assert (builder, "import-percent");
122
123   g_signal_connect_swapped (p->all_cases_button, "toggled",
124                     G_CALLBACK (on_intro_amount_changed), p);
125   g_signal_connect_swapped (p->n_cases_button, "toggled",
126                     G_CALLBACK (on_intro_amount_changed), p);
127   g_signal_connect_swapped (p->percent_button, "toggled",
128                     G_CALLBACK (on_intro_amount_changed), p);
129
130   on_intro_amount_changed (p);
131
132   ds_init_empty (&s);
133   ds_put_cstr (&s, _("This assistant will guide you through the process of "
134                      "importing data into PSPP from a text file with one line "
135                      "per case,  in which fields are separated by tabs, "
136                      "commas, or other delimiters.\n\n"));
137   if (ia->file.total_is_exact)
138     ds_put_format (
139       &s, ngettext ("The selected file contains %zu line of text.  ",
140                     "The selected file contains %zu lines of text.  ",
141                     ia->file.line_cnt),
142       ia->file.line_cnt);
143   else if (ia->file.total_lines > 0)
144     {
145       ds_put_format (
146         &s, ngettext (
147           "The selected file contains approximately %lu line of text.  ",
148           "The selected file contains approximately %lu lines of text.  ",
149           ia->file.total_lines),
150         ia->file.total_lines);
151       ds_put_format (
152         &s, ngettext (
153           "Only the first %zu line of the file will be shown for "
154           "preview purposes in the following screens.  ",
155           "Only the first %zu lines of the file will be shown for "
156           "preview purposes in the following screens.  ",
157           ia->file.line_cnt),
158         ia->file.line_cnt);
159     }
160   ds_put_cstr (&s, _("You may choose below how much of the file should "
161                      "actually be imported."));
162   gtk_label_set_text (GTK_LABEL (get_widget_assert (builder, "intro-label")),
163                       ds_cstr (&s));
164   ds_destroy (&s);
165
166   return p;
167 }
168
169 /* Resets IA's intro page to its initial state. */
170 void
171 reset_intro_page (struct import_assistant *ia)
172 {
173   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->intro->all_cases_button),
174                                 true);
175 }
176
177 /* Called when one of the radio buttons is clicked. */
178 static void
179 on_intro_amount_changed (struct intro_page *p)
180 {
181   gtk_widget_set_sensitive (p->n_cases_spin,
182                             gtk_toggle_button_get_active (
183                               GTK_TOGGLE_BUTTON (p->n_cases_button)));
184
185   gtk_widget_set_sensitive (p->percent_spin,
186                             gtk_toggle_button_get_active (
187                               GTK_TOGGLE_BUTTON (p->percent_button)));
188 }
189
190
191 void
192 intro_append_syntax (const struct intro_page *p, struct string *s)
193 {
194   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (p->n_cases_button)))
195     ds_put_format (s, "  /IMPORTCASES=FIRST %d\n",
196                    gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (p->n_cases_spin)));
197   else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (p->percent_button)))
198     ds_put_format (s, "  /IMPORTCASES=PERCENT %d\n",
199                    gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (p->percent_spin)));
200   else
201     ds_put_cstr (s, "  /IMPORTCASES=ALL\n");
202 }