d1a4798fd11574b3933f09f0e59b54b616abd818
[pspp] / src / ui / gui / main.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2005, 2006, 2010, 2011, 2012, 2013, 2014, 2015  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
46 GdkWindow *create_splash_window (GMainContext *context);
47 gboolean destroy_splash_window (gpointer ud);
48
49
50
51 \f
52 /* Arguments to be interpreted before the X server gets initialised */
53
54 enum
55   {
56     OPT_HELP,
57     OPT_VERSION,
58     OPT_NO_SPLASH,
59     OPT_MEASURE_STARTUP,
60     N_STARTUP_OPTIONS
61   };
62
63 static const struct argv_option startup_options[N_STARTUP_OPTIONS] =
64   {
65     {"help",      'h', no_argument, OPT_HELP},
66     {"version",   'V', no_argument, OPT_VERSION},
67     {"no-splash", 'q', no_argument, OPT_NO_SPLASH},
68     {"measure-startup", 0, no_argument, OPT_MEASURE_STARTUP},
69   };
70
71 /* --measure-startup: Prints the elapsed time to start up and load any file
72    specified on the command line. */
73 static gboolean measure_startup;
74 static GTimer *startup;
75
76 static void
77 usage (void)
78 {
79   char *inc_path = string_array_join (include_path_default (), " ");
80   GOptionGroup *gtk_options;
81   GOptionContext *ctx;
82   gchar *gtk_help_base, *gtk_help;
83
84   /* Get help text for GTK+ options.  */
85   ctx = g_option_context_new ("psppire");
86   gtk_options = gtk_get_option_group (FALSE);
87   gtk_help_base = g_option_context_get_help (ctx, FALSE, gtk_options);
88   g_option_context_free (ctx);
89
90   /* The GTK+ help text starts with usage instructions that we don't want,
91      followed by a blank line.  Trim off everything up to and including the
92      first blank line. */
93   gtk_help = strstr (gtk_help_base, "\n\n");
94   gtk_help = gtk_help != NULL ? gtk_help + 2 : gtk_help_base;
95
96   printf (_("\
97 PSPPIRE, a GUI for PSPP, a program for statistical analysis of sampled data.\n\
98 Usage: %s [OPTION]... FILE\n\
99 \n\
100 Arguments to long options also apply to equivalent short options.\n\
101 \n\
102 GUI options:\n\
103   -q, --no-splash           don't show splash screen during startup\n\
104 \n\
105 %s\
106 Language options:\n\
107   -I, --include=DIR         append DIR to search path\n\
108   -I-, --no-include         clear search path\n\
109   -a, --algorithm={compatible|enhanced}\n\
110                             set to `compatible' if you want output\n\
111                             calculated from broken algorithms\n\
112   -x, --syntax={compatible|enhanced}\n\
113                             set to `compatible' to disable PSPP extensions\n\
114   -i, --interactive         interpret syntax in interactive mode\n\
115   -s, --safer               don't allow some unsafe operations\n\
116 Default search path: %s\n\
117 \n\
118 Informative output:\n\
119   -h, --help                display this help and exit\n\
120   -V, --version             output version information and exit\n\
121 \n\
122 A non-option argument is interpreted as a data file in .sav or .zsav or .por\n\
123 format or a syntax file to load.\n"),
124           program_name, gtk_help, inc_path);
125
126   free (inc_path);
127   g_free (gtk_help_base);
128
129   emit_bug_reporting_address ();
130   exit (EXIT_SUCCESS);
131 }
132
133 static void
134 startup_option_callback (int id, void *show_splash_)
135 {
136   gboolean *show_splash = show_splash_;
137
138   switch (id)
139     {
140     case OPT_HELP:
141       usage ();
142       break;
143
144     case OPT_VERSION:
145       version_etc (stdout, "psppire", PACKAGE_NAME, PACKAGE_VERSION,
146                    "Ben Pfaff", "John Darrington", "Jason Stover",
147                    NULL_SENTINEL);
148       exit (EXIT_SUCCESS);
149
150     case OPT_NO_SPLASH:
151       *show_splash = FALSE;
152       break;
153
154     case OPT_MEASURE_STARTUP:
155       measure_startup = TRUE;
156       break;
157
158     default:
159       NOT_REACHED ();
160     }
161 }
162
163 static gboolean UNUSED
164 print_startup_time (gpointer data)
165 {
166   g_timer_stop (startup);
167   printf ("%.3f seconds elapsed\n", g_timer_elapsed (startup, NULL));
168   g_timer_destroy (startup);
169   startup = NULL;
170
171   return FALSE;
172 }
173
174 static GMemVTable vtable =
175   {
176     xmalloc,
177     xrealloc,
178     free,
179     xcalloc,
180     malloc,
181     realloc
182   };
183
184 #ifdef __APPLE__
185 static const bool apple = true;
186 #else
187 static const bool apple = false;
188 #endif
189
190 /* Searches ARGV for the -psn_xxxx option that the desktop application
191    launcher passes in, and removes it if it finds it.  Returns the new value
192    of ARGC. */
193 static inline int
194 remove_psn (int argc, char **argv)
195 {
196   if (apple)
197     {
198       int i;
199
200       for (i = 0; i < argc; i++)
201         {
202           if (!strncmp (argv[i], "-psn", 4))
203             {
204               remove_element (argv, argc + 1, sizeof *argv, i);
205               return argc - 1;
206             }
207         }
208     }
209   return argc;
210 }
211
212 \f
213 gboolean
214 init_prepare (GSource *source, gint *timeout_)
215 {
216   return TRUE;
217 }
218
219
220
221 gboolean
222 init_check (GSource *source)
223 {
224   return TRUE;
225 }
226
227
228 gboolean
229 init_dispatch (GSource *ss,
230                GSourceFunc callback,
231                gpointer user_data)
232 {
233   struct init_source *is = (struct init_source *)ss;
234
235   bool finished = initialize (is);
236   is->state++;
237   
238   if (finished)
239     {
240       g_main_loop_quit (is->loop);
241       return FALSE;
242     }
243
244   return TRUE;
245 }
246
247 static GSourceFuncs init_funcs = {init_prepare, init_check, init_dispatch, NULL};
248
249 \f
250
251 int
252 main (int argc, char *argv[])
253 {
254   gboolean show_splash = TRUE;
255   struct argv_parser *parser;
256   const gchar *vers;
257
258   set_program_name (argv[0]);
259
260   g_mem_set_vtable (&vtable);
261
262 #if !GLIB_CHECK_VERSION(2,32,0)
263   /* g_thread_init() was required before glib 2.32, but it is deprecated since
264      then and calling it yields a compile-time warning. */
265   g_thread_init (NULL);
266 #endif
267
268   gtk_disable_setlocale ();
269
270   startup = g_timer_new ();
271   g_timer_start (startup);
272
273   if ( (vers = gtk_check_version (GTK_MAJOR_VERSION,
274                                  GTK_MINOR_VERSION,
275                                  GTK_MICRO_VERSION)) )
276     {
277       g_warning ("%s", vers);
278     }
279
280   argc = remove_psn (argc, argv);
281
282   /* Parse our own options. 
283      This must come BEFORE gdk_init otherwise options such as 
284      --help --version which ought to work without an X server, won't.
285   */
286   parser = argv_parser_create ();
287   argv_parser_add_options (parser, startup_options, N_STARTUP_OPTIONS,
288                            startup_option_callback, &show_splash);
289   source_init_register_argv_parser (parser);
290   if (!argv_parser_run (parser, argc, argv))
291     exit (EXIT_FAILURE);
292   argv_parser_destroy (parser);
293
294   /* Initialise GDK.  GTK gets initialized later. */
295   gdk_init (&argc, &argv);
296
297   GMainContext *context = g_main_context_new ();
298   
299   GdkWindow *win = show_splash ? create_splash_window (context) : NULL;
300
301   GMainLoop *loop = g_main_loop_new (context, FALSE);
302
303   GSource *ss = g_source_new (&init_funcs,
304                               sizeof (struct init_source));
305   
306   ((struct init_source *) ss)->state = 0;
307   
308   g_source_set_priority (ss, G_PRIORITY_DEFAULT);
309     
310   g_source_attach (ss, context);
311
312   ((struct init_source *) ss)->argc = &argc;
313   ((struct init_source *) ss)->argv = &argv;
314   ((struct init_source *) ss)->loop = loop;
315   ((struct init_source *) ss)->file = optind < argc ? argv[optind] : NULL;
316   
317   g_source_unref (ss);
318
319   g_main_loop_run (loop);
320
321   g_main_loop_unref (loop);
322   g_main_context_unref (context);
323
324   if (win)
325     g_timeout_add (500, destroy_splash_window, win);
326
327   gtk_main ();
328
329   /* Not much point in this except to check for memory leaks */
330   de_initialize ();
331   
332   return 0;
333 }
334
335
336
337 \f
338
339 struct splash_source
340 {
341   GSource parent;
342   cairo_surface_t *sfc;
343 };
344
345 void
346 fill_splash_window (GdkWindow *win, cairo_surface_t *sfce)
347 {
348   cairo_t *cr = gdk_cairo_create (win);
349   
350   cairo_set_source_surface (cr, sfce, 0, 0);
351   
352   cairo_paint (cr);
353   cairo_destroy (cr);
354 }
355
356 gboolean
357 splash_prepare  (GSource    *source,
358             gint       *timeout_)
359 {
360   GdkEvent *e = gdk_event_peek ();
361   if (!e)
362     return FALSE;
363
364   gdk_event_free (e);
365   return TRUE;
366 }
367
368 gboolean
369 splash_check   (GSource    *source)
370 {
371   GdkEvent *e = gdk_event_peek ();
372   if (!e)
373     return FALSE;
374
375   gdk_event_free (e);
376   return TRUE;
377 }
378
379
380 gboolean
381 splash_dispatch (GSource *ss,
382             GSourceFunc callback,
383             gpointer    user_data)
384 {
385   struct splash_source *source = (struct splash_source *) ss;
386   GdkEvent *e = gdk_event_get ();
387   if (!e)
388     return TRUE;
389
390   GdkWindow *w = ((GdkEventAny *)e)->window;
391
392   if (!w)
393     {
394       gdk_event_free (e);
395       return TRUE;
396     }
397
398   fill_splash_window (w, source->sfc);
399   gdk_display_flush (gdk_window_get_display (w));
400
401   gdk_event_free (e);
402
403   return TRUE;
404 }
405
406
407 gboolean
408 destroy_splash_window (gpointer ud)
409 {
410   GdkWindow *win = GDK_WINDOW (ud);
411   gdk_window_withdraw (win);
412   gdk_display_flush (gdk_window_get_display (win));
413   gdk_window_destroy (win);
414   
415   return FALSE;
416 }
417
418 GSourceFuncs splash_funcs = {splash_prepare, splash_check, splash_dispatch, NULL};
419
420
421 GdkWindow *
422 create_splash_window (GMainContext *context)
423 {
424   const gchar *filename = PKGDATADIR "/splash.png";
425
426   const char *relocated_filename = relocate (filename);
427   cairo_surface_t *the_surface = 
428     cairo_image_surface_create_from_png  (relocated_filename);
429
430   if (filename != relocated_filename)
431     free (CONST_CAST (char *, relocated_filename));
432
433   
434   g_return_val_if_fail (the_surface, NULL);
435     
436   int attr_mask = GDK_WA_TYPE_HINT;
437   GdkWindowAttr attr;
438     
439   attr.width =  cairo_image_surface_get_width (the_surface);
440   attr.height = cairo_image_surface_get_height (the_surface);
441   attr.wclass = GDK_INPUT_OUTPUT; 
442   attr.window_type = GDK_WINDOW_TOPLEVEL;
443     
444   attr.type_hint = GDK_WINDOW_TYPE_HINT_SPLASHSCREEN;
445     
446   GdkWindow *win = gdk_window_new (NULL, &attr, attr_mask);
447     
448   gdk_window_set_events (win, GDK_EXPOSURE_MASK);
449   gdk_window_set_keep_above (win, TRUE);
450   gdk_window_show (win);
451   
452
453   GSource *ss = g_source_new (&splash_funcs,
454                               sizeof (struct splash_source));
455   
456   ((struct splash_source *) ss)->sfc = the_surface;
457   g_source_set_priority (ss, G_PRIORITY_HIGH);
458     
459   g_source_attach (ss, context);
460
461   g_source_unref (ss);
462     
463   return win;
464 }
465
466