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