6db2bee688ffe6fe2b73461a2682ef8e674ae991
[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 "page-intro.h"
20
21 #include "ui/gui/text-data-import-dialog.h"
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <gtk/gtk.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29
30 #include "data/data-in.h"
31 #include "data/data-out.h"
32 #include "data/format-guesser.h"
33 #include "data/value-labels.h"
34 #include "language/data-io/data-parser.h"
35 #include "language/lexer/lexer.h"
36 #include "libpspp/assertion.h"
37 #include "libpspp/i18n.h"
38 #include "libpspp/line-reader.h"
39 #include "libpspp/message.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-scanf.h"
50 #include "ui/syntax-gen.h"
51
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 struct import_assistant;
60
61
62 \f
63 /* The "intro" page of the assistant. */
64
65 /* The introduction page of the assistant. */
66 struct intro_page
67   {
68     GtkWidget *page;
69     GtkWidget *all_cases_button;
70     GtkWidget *n_cases_button;
71     GtkWidget *n_cases_spin;
72     GtkWidget *percent_button;
73     GtkWidget *percent_spin;
74   };
75
76 static void on_intro_amount_changed (struct intro_page *);
77
78 /* Initializes IA's intro substructure. */
79 struct intro_page *
80 intro_page_create (struct import_assistant *ia)
81 {
82   GtkBuilder *builder = ia->asst.builder;
83   struct string s;
84   GtkWidget *hbox_n_cases ;
85   GtkWidget *hbox_percent ;
86   GtkWidget *table ;
87
88   struct intro_page *p = xzalloc (sizeof (*p));
89
90   p->n_cases_spin = gtk_spin_button_new_with_range (0, INT_MAX, 100);
91
92   hbox_n_cases = psppire_scanf_new (_("Only the first %4d cases"), &p->n_cases_spin);
93
94   table  = get_widget_assert (builder, "button-table");
95
96   gtk_grid_attach (GTK_GRID (table), hbox_n_cases,
97                     1, 1,
98                     1, 1);
99
100   p->percent_spin = gtk_spin_button_new_with_range (0, 100, 10);
101
102   hbox_percent = psppire_scanf_new (_("Only the first %3d %% of file (approximately)"), &p->percent_spin);
103
104   gtk_grid_attach (GTK_GRID (table), hbox_percent,
105                    1, 2,
106                    1, 1);
107
108   p->page = add_page_to_assistant (ia, get_widget_assert (builder, "Intro"),
109                                    GTK_ASSISTANT_PAGE_INTRO);
110
111   p->all_cases_button = get_widget_assert (builder, "import-all-cases");
112
113   p->n_cases_button = get_widget_assert (builder, "import-n-cases");
114
115   p->percent_button = get_widget_assert (builder, "import-percent");
116
117   g_signal_connect_swapped (p->all_cases_button, "toggled",
118                     G_CALLBACK (on_intro_amount_changed), p);
119   g_signal_connect_swapped (p->n_cases_button, "toggled",
120                     G_CALLBACK (on_intro_amount_changed), p);
121   g_signal_connect_swapped (p->percent_button, "toggled",
122                     G_CALLBACK (on_intro_amount_changed), p);
123
124   on_intro_amount_changed (p);
125
126   ds_init_empty (&s);
127   ds_put_cstr (&s, _("This assistant will guide you through the process of "
128                      "importing data into PSPP from a text file with one line "
129                      "per case,  in which fields are separated by tabs, "
130                      "commas, or other delimiters.\n\n"));
131   if (ia->file.total_is_exact)
132     ds_put_format (
133       &s, ngettext ("The selected file contains %'zu line of text.  ",
134                     "The selected file contains %'zu lines of text.  ",
135                     ia->file.line_cnt),
136       ia->file.line_cnt);
137   else if (ia->file.total_lines > 0)
138     {
139       ds_put_format (
140         &s, ngettext (
141           "The selected file contains approximately %'lu line of text.  ",
142           "The selected file contains approximately %'lu lines of text.  ",
143           ia->file.total_lines),
144         ia->file.total_lines);
145       ds_put_format (
146         &s, ngettext (
147           "Only the first %zu line of the file will be shown for "
148           "preview purposes in the following screens.  ",
149           "Only the first %zu lines of the file will be shown for "
150           "preview purposes in the following screens.  ",
151           ia->file.line_cnt),
152         ia->file.line_cnt);
153     }
154   ds_put_cstr (&s, _("You may choose below how much of the file should "
155                      "actually be imported."));
156   gtk_label_set_text (GTK_LABEL (get_widget_assert (builder, "intro-label")),
157                       ds_cstr (&s));
158   ds_destroy (&s);
159
160   return p;
161 }
162
163 /* Resets IA's intro page to its initial state. */
164 void
165 reset_intro_page (struct import_assistant *ia)
166 {
167   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ia->intro->all_cases_button),
168                                 true);
169 }
170
171 /* Called when one of the radio buttons is clicked. */
172 static void
173 on_intro_amount_changed (struct intro_page *p)
174 {
175   gtk_widget_set_sensitive (p->n_cases_spin,
176                             gtk_toggle_button_get_active (
177                               GTK_TOGGLE_BUTTON (p->n_cases_button)));
178
179   gtk_widget_set_sensitive (p->percent_spin,
180                             gtk_toggle_button_get_active (
181                               GTK_TOGGLE_BUTTON (p->percent_button)));
182 }
183
184
185 void
186 intro_append_syntax (const struct intro_page *p, struct string *s)
187 {
188   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (p->n_cases_button)))
189     ds_put_format (s, "  /IMPORTCASE=FIRST %d\n",
190                    gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (p->n_cases_spin)));
191   else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (p->percent_button)))
192     ds_put_format (s, "  /IMPORTCASE=PERCENT %d\n",
193                    gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (p->percent_spin)));
194   else
195     ds_put_cstr (s, "  /IMPORTCASE=ALL\n");
196 }