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