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