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