Use the sum of weights of only those cases which are valid to
[pspp-builds.git] / src / ui / gui / psppire.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2005, 2006, 2009  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 <locale.h>
20 #include <assert.h>
21 #include <libintl.h>
22 #include <gsl/gsl_errno.h>
23
24 #include <argp.h>
25 #include <ui/command-line.h>
26 #include "relocatable.h"
27
28 #include "data-editor.h"
29 #include "psppire.h"
30
31 #include <libpspp/getl.h>
32 #include <unistd.h>
33 #include <data/casereader.h>
34 #include <data/datasheet.h>
35 #include <data/file-handle-def.h>
36 #include <data/settings.h>
37 #include <data/file-name.h>
38 #include <data/procedure.h>
39 #include <libpspp/getl.h>
40 #include <language/lexer/lexer.h>
41 #include <libpspp/version.h>
42 #include <output/output.h>
43 #include <output/journal.h>
44 #include <language/syntax-string-source.h>
45
46 #include <gtk/gtk.h>
47 #include <glade/glade.h>
48 #include "psppire-dict.h"
49 #include "psppire-var-store.h"
50 #include "psppire-data-store.h"
51 #include "helper.h"
52 #include "message-dialog.h"
53 #include <ui/syntax-gen.h>
54
55 #include "output-viewer.h"
56
57 #include <data/sys-file-reader.h>
58 #include <data/por-file-reader.h>
59
60 #include <ui/source-init-opts.h>
61
62 PsppireDataStore *the_data_store = 0;
63 PsppireVarStore *the_var_store = 0;
64
65 static void create_icon_factory (void);
66
67 struct source_stream *the_source_stream ;
68 struct dataset * the_dataset = NULL;
69
70
71 static void
72 replace_casereader (struct casereader *s)
73 {
74   psppire_data_store_set_reader (the_data_store, s);
75 }
76
77 #define _(msgid) gettext (msgid)
78 #define N_(msgid) msgid
79
80 void
81 initialize (struct command_line_processor *clp, int argc, char **argv)
82 {
83   PsppireDict *dictionary = 0;
84
85   /* gtk_init messes with the locale.
86      So unset the bits we want to control ourselves */
87   setlocale (LC_NUMERIC, "C");
88
89   bindtextdomain (PACKAGE, locale_dir);
90
91
92   glade_init ();
93
94   gsl_set_error_handler_off ();
95   fn_init ();
96   outp_init ();
97   settings_init (&viewer_width, &viewer_length);
98   fh_init ();
99   the_source_stream =
100     create_source_stream (
101                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
102                           );
103
104   the_dataset = create_dataset ();
105
106
107   message_dialog_init (the_source_stream);
108
109   dictionary = psppire_dict_new_from_dict (dataset_dict (the_dataset));
110
111   bind_textdomain_codeset (PACKAGE, "UTF-8");
112
113   /* Create the model for the var_sheet */
114   the_var_store = psppire_var_store_new (dictionary);
115
116   the_data_store = psppire_data_store_new (dictionary);
117   replace_casereader (NULL);
118
119
120
121   create_icon_factory ();
122
123   {
124     const char *filename = output_file_name ();
125
126     struct string config_string;
127
128     ds_init_empty (&config_string);
129
130     ds_put_format (&config_string,
131                    "gui:ascii:screen:squeeze=on headers=off top-margin=0 "
132                    "bottom-margin=0 paginate=off length=auto width=auto "
133                    "emphasis=none "
134                    "output-file=\"%s\" append=yes", filename);
135
136     outp_configure_driver_line (ds_ss (&config_string));
137
138     unlink (filename);
139
140     ds_destroy (&config_string);
141   }
142
143   journal_enable ();
144   textdomain (PACKAGE);
145
146   command_line_processor_replace_aux (clp, &post_init_argp, the_source_stream);
147   command_line_processor_replace_aux (clp, &non_option_argp, the_source_stream);
148
149   command_line_processor_parse (clp, argc, argv);
150
151   new_data_window (NULL, NULL);
152
153   execute_syntax (create_syntax_string_source (""));
154 }
155
156
157 void
158 de_initialize (void)
159 {
160   destroy_source_stream (the_source_stream);
161   message_dialog_done ();
162   settings_done ();
163   outp_done ();
164 }
165
166
167
168 struct icon_info
169 {
170   const char *file_name;
171   const gchar *id;
172 };
173
174
175 static const struct icon_info icons[] =
176   {
177     {PKGDATADIR "/value-labels.png",    "pspp-value-labels"},
178     {PKGDATADIR "/weight-cases.png",    "pspp-weight-cases"},
179     {PKGDATADIR "/goto-variable.png",   "pspp-goto-variable"},
180     {PKGDATADIR "/insert-variable.png", "pspp-insert-variable"},
181     {PKGDATADIR "/insert-case.png",     "pspp-insert-case"},
182     {PKGDATADIR "/split-file.png",      "pspp-split-file"},
183     {PKGDATADIR "/select-cases.png",    "pspp-select-cases"},
184     {PKGDATADIR "/recent-dialogs.png",  "pspp-recent-dialogs"},
185     {PKGDATADIR "/nominal.png",         "var-nominal"},
186     {PKGDATADIR "/ordinal.png",         "var-ordinal"},
187     {PKGDATADIR "/scale.png",           "var-scale"},
188     {PKGDATADIR "/string.png",          "var-string"},
189     {PKGDATADIR "/date-scale.png",      "var-date-scale"}
190   };
191
192 static void
193 create_icon_factory (void)
194 {
195   gint i;
196   GtkIconFactory *factory = gtk_icon_factory_new ();
197
198   for (i = 0 ; i < sizeof (icons) / sizeof(icons[0]); ++i)
199     {
200       GError *err = NULL;
201       GdkPixbuf *pixbuf =
202         gdk_pixbuf_new_from_file (relocate (icons[i].file_name), &err);
203
204       if ( pixbuf )
205         {
206           GtkIconSet *icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
207           g_object_unref (pixbuf);
208           gtk_icon_factory_add ( factory, icons[i].id, icon_set);
209         }
210       else
211         {
212           g_warning ("Cannot create icon: %s", err->message);
213           g_clear_error (&err);
214         }
215     }
216
217   {
218     /* Create our own "pspp-stock-reset" item, using the
219        GTK_STOCK_REFRESH icon set */
220
221     GtkStockItem items[] = {
222       {"pspp-stock-reset", N_("_Reset"), 0, 0, PACKAGE},
223       {"pspp-stock-select", N_("_Select"), 0, 0, PACKAGE}
224     };
225
226
227     gtk_stock_add (items, 2);
228     gtk_icon_factory_add (factory, "pspp-stock-reset",
229                           gtk_icon_factory_lookup_default (GTK_STOCK_REFRESH)
230                           );
231
232     gtk_icon_factory_add (factory, "pspp-stock-select",
233                           gtk_icon_factory_lookup_default (GTK_STOCK_INDEX)
234                           );
235   }
236
237   gtk_icon_factory_add_default (factory);
238 }
239
240 \f
241
242 static error_t
243 parse_non_options (int key, char *arg, struct argp_state *state)
244 {
245   struct source_stream *ss = state->input;
246
247   if ( NULL == ss )
248     return 0;
249
250   switch (key)
251     {
252     case ARGP_KEY_ARG:
253       {
254         struct string syntax;
255         FILE *fp = fopen (arg, "r");
256         if (NULL == fp)
257           {
258             const int errnum = errno;
259             fprintf (state->err_stream, _("Cannot open %s: %s.\n"),
260                      arg, strerror (errnum));
261             return 0;
262           }
263         if ( sfm_detect (fp))
264           {
265             ds_init_cstr (&syntax, "GET FILE=");
266             goto close;
267           }
268         rewind (fp);
269         if (pfm_detect (fp))
270           {
271             ds_init_cstr (&syntax, "IMPORT FILE=");
272             goto close;
273           }
274
275         fclose (fp);
276         msg (ME, _("%s is neither a system nor portable file"), arg);
277         break;
278
279       close:
280         fclose (fp);
281
282         syntax_gen_string (&syntax, ss_cstr (arg));
283         ds_put_cstr (&syntax, ".");
284
285         getl_append_source (ss,
286                             create_syntax_string_source (ds_cstr (&syntax)),
287                             GETL_BATCH,
288                             ERRMODE_CONTINUE);
289
290         ds_destroy (&syntax);
291         break;
292       }
293     default:
294       return ARGP_ERR_UNKNOWN;
295     }
296   return 0;
297 }
298
299
300 const struct argp non_option_argp = {NULL, parse_non_options, 0, 0, 0, 0, 0};