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