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