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