lexer: Reimplement for better testability and internationalization.
[pspp-builds.git] / src / ui / gui / main.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2005, 2006, 2010  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 "ui/gui/psppire.h"
20
21 #include <gtk/gtk.h>
22 #include <stdlib.h>
23
24 #include "language/lexer/include-path.h"
25 #include "libpspp/argv-parser.h"
26 #include "libpspp/assertion.h"
27 #include "libpspp/cast.h"
28 #include "libpspp/copyleft.h"
29 #include "libpspp/str.h"
30 #include "libpspp/string-array.h"
31 #include "libpspp/version.h"
32 #include "ui/source-init-opts.h"
33
34 #include "gl/configmake.h"
35 #include "gl/progname.h"
36 #include "gl/relocatable.h"
37 #include "gl/version-etc.h"
38 #include "gl/xalloc.h"
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42 #define N_(msgid) msgid
43
44 \f
45 /* Arguments to be interpreted before the X server gets initialised */
46
47 enum
48   {
49     OPT_HELP,
50     OPT_VERSION,
51     OPT_NO_SPLASH,
52     N_STARTUP_OPTIONS
53   };
54
55 static const struct argv_option startup_options[N_STARTUP_OPTIONS] =
56   {
57     {"help",      'h', no_argument, OPT_HELP},
58     {"version",   'V', no_argument, OPT_VERSION},
59     {"no-splash", 'q', no_argument, OPT_NO_SPLASH}
60   };
61
62 static void
63 usage (void)
64 {
65   char *inc_path = string_array_join (include_path_default (), " ");
66   GOptionGroup *gtk_options;
67   GOptionContext *ctx;
68   gchar *gtk_help_base, *gtk_help;
69
70   /* Get help text for GTK+ options.  */
71   ctx = g_option_context_new ("psppire");
72   gtk_options = gtk_get_option_group (FALSE);
73   gtk_help_base = g_option_context_get_help (ctx, FALSE, gtk_options);
74   g_option_context_free (ctx);
75
76   /* The GTK+ help text starts with usage instructions that we don't want,
77      followed by a blank line.  Trim off everything up to and including the
78      first blank line. */
79   gtk_help = strstr (gtk_help_base, "\n\n");
80   gtk_help = gtk_help != NULL ? gtk_help + 2 : gtk_help_base;
81
82   printf (_("\
83 PSPPIRE, a GUI for PSPP, a program for statistical analysis of sample data.\n\
84 Usage: %s [OPTION]... FILE\n\
85 \n\
86 Arguments to long options also apply to equivalent short options.\n\
87 \n\
88 GUI options:\n\
89   -q, --no-splash           don't show splash screen during startup\n\
90 \n\
91 %s\
92 Language options:\n\
93   -I, --include=DIR         append DIR to search path\n\
94   -I-, --no-include         clear search path\n\
95   -a, --algorithm={compatible|enhanced}\n\
96                             set to `compatible' if you want output\n\
97                             calculated from broken algorithms\n\
98   -x, --syntax={compatible|enhanced}\n\
99                             set to `compatible' to disable PSPP extensions\n\
100   -i, --interactive         interpret syntax in interactive mode\n\
101   -s, --safer               don't allow some unsafe operations\n\
102 Default search path: %s\n\
103 \n\
104 Informative output:\n\
105   -h, --help                display this help and exit\n\
106   -V, --version             output version information and exit\n\
107 \n\
108 A non-option argument is interpreted as a .sav or .por file to load.\n"),
109           program_name, gtk_help, inc_path);
110
111   free (inc_path);
112   g_free (gtk_help_base);
113
114   emit_bug_reporting_address ();
115   exit (EXIT_SUCCESS);
116 }
117
118 static void
119 startup_option_callback (int id, void *show_splash_)
120 {
121   gboolean *show_splash = show_splash_;
122
123   switch (id)
124     {
125     case OPT_HELP:
126       usage ();
127       break;
128
129     case OPT_VERSION:
130       version_etc (stdout, "psppire", PACKAGE_NAME, PACKAGE_VERSION,
131                    "Ben Pfaff", "John Darrington", "Jason Stover",
132                    NULL_SENTINEL);
133       exit (EXIT_SUCCESS);
134
135     case OPT_NO_SPLASH:
136       *show_splash = FALSE;
137       break;
138
139     default:
140       NOT_REACHED ();
141     }
142 }
143 \f
144 static GtkWidget *
145 create_splash_window (void)
146 {
147   GtkWidget *splash ;
148   GtkWidget *image;
149
150   gtk_window_set_auto_startup_notification (FALSE);
151
152   splash = gtk_window_new (GTK_WINDOW_POPUP);
153
154   gtk_window_set_position (GTK_WINDOW (splash),
155                            GTK_WIN_POS_CENTER_ALWAYS);
156
157   gtk_window_set_type_hint (GTK_WINDOW (splash),
158                             GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
159
160   image = gtk_image_new_from_file (relocate (PKGDATADIR "/splash.png"));
161
162   gtk_container_add (GTK_CONTAINER (splash), image);
163
164   gtk_widget_show (image);
165
166   return splash;
167 }
168
169 static gboolean
170 hide_splash_window (gpointer data)
171 {
172   GtkWidget *splash = data;
173   gtk_widget_destroy (splash);
174   gtk_window_set_auto_startup_notification (TRUE);
175   return FALSE;
176 }
177
178
179 static gboolean
180 quit_one_loop (gpointer data)
181 {
182   gtk_main_quit ();
183   return FALSE;
184 }
185
186 struct initialisation_parameters
187 {
188   const char *data_file;
189   GtkWidget *splash_window;
190 };
191
192
193 static gboolean
194 run_inner_loop (gpointer data)
195 {
196   struct initialisation_parameters *ip = data;
197   initialize (ip->data_file);
198
199   g_timeout_add (500, hide_splash_window, ip->splash_window);
200
201   gtk_main ();
202
203   de_initialize ();
204
205   return FALSE;
206 }
207
208
209 static GMemVTable vtable =
210   {
211     xmalloc,
212     xrealloc,
213     free,
214     xcalloc,
215     malloc,
216     realloc
217   };
218
219 int
220 main (int argc, char *argv[])
221 {
222   struct initialisation_parameters init_p;
223   gboolean show_splash = TRUE;
224   struct argv_parser *parser;
225   const gchar *vers;
226
227   set_program_name (argv[0]);
228
229   g_mem_set_vtable (&vtable);
230
231   gtk_disable_setlocale ();
232
233
234   if ( ! gtk_parse_args (&argc, &argv) )
235     {
236       perror ("Error parsing arguments");
237       exit (1);
238     }
239
240   if ( (vers = gtk_check_version (GTK_MAJOR_VERSION,
241                                  GTK_MINOR_VERSION,
242                                  GTK_MICRO_VERSION)) )
243     {
244       g_warning ("%s", vers);
245     }
246
247
248   /* Parse our own options. 
249      This must come BEFORE gdk_init otherwise options such as 
250      --help --version which ought to work without an X server, won't.
251   */
252   parser = argv_parser_create ();
253   argv_parser_add_options (parser, startup_options, N_STARTUP_OPTIONS,
254                            startup_option_callback, &show_splash);
255   source_init_register_argv_parser (parser);
256   if (!argv_parser_run (parser, argc, argv))
257     exit (EXIT_FAILURE);
258   argv_parser_destroy (parser);
259
260   /* Initialise GDK.  Theoretically this call can remove options from argc,argv if
261      it thinks they are gdk options.
262      However there shouldn't be any here because of the gtk_parse_args call above. */
263   gdk_init (&argc, &argv);
264
265   init_p.splash_window = create_splash_window ();
266   init_p.data_file = optind < argc ? argv[optind] : NULL;
267
268   if ( show_splash )
269     gtk_widget_show (init_p.splash_window);
270
271   g_idle_add (quit_one_loop, 0);
272
273   gtk_quit_add (0, run_inner_loop, &init_p);
274   gtk_main ();
275
276   return 0;
277 }