9bd278d3397965ef1c0b5d8b52d141decc930bdf
[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 #ifdef __APPLE__
175 static const bool apple = true;
176 #else
177 static const bool apple = false;
178 #endif
179
180 /* Searches ARGV for the -psn_xxxx option that the desktop application
181    launcher passes in, and removes it if it finds it.  Returns the new value
182    of ARGC. */
183 static inline int
184 remove_psn (int argc, char **argv)
185 {
186   if (apple)
187     {
188       int i;
189
190       for (i = 0; i < argc; i++)
191         {
192           if (!strncmp (argv[i], "-psn", 4))
193             {
194               remove_element (argv, argc + 1, sizeof *argv, i);
195               return argc - 1;
196             }
197         }
198     }
199   return argc;
200 }
201
202 \f
203 gboolean
204 init_prepare (GSource *source, gint *timeout_)
205 {
206   return TRUE;
207 }
208
209
210
211 gboolean
212 init_check (GSource *source)
213 {
214   return TRUE;
215 }
216
217
218 gboolean
219 init_dispatch (GSource *ss,
220                GSourceFunc callback,
221                gpointer user_data)
222 {
223   struct init_source *is = (struct init_source *)ss;
224
225   bool finished = initialize (is);
226   is->state++;
227   
228   if (finished)
229     {
230       g_main_loop_quit (is->loop);
231       return FALSE;
232     }
233
234   return TRUE;
235 }
236
237 static GSourceFuncs init_funcs = {init_prepare, init_check, init_dispatch, NULL};
238
239 \f
240
241 int
242 main (int argc, char *argv[])
243 {
244   gboolean show_splash = TRUE;
245   struct argv_parser *parser;
246   const gchar *vers;
247
248   set_program_name (argv[0]);
249
250 #if !GLIB_CHECK_VERSION(2,32,0)
251   /* g_thread_init() was required before glib 2.32, but it is deprecated since
252      then and calling it yields a compile-time warning. */
253   g_thread_init (NULL);
254 #endif
255
256   gtk_disable_setlocale ();
257
258   startup = g_timer_new ();
259   g_timer_start (startup);
260
261   if ( (vers = gtk_check_version (GTK_MAJOR_VERSION,
262                                  GTK_MINOR_VERSION,
263                                  GTK_MICRO_VERSION)) )
264     {
265       g_warning ("%s", vers);
266     }
267
268   argc = remove_psn (argc, argv);
269
270   /* Parse our own options. 
271      This must come BEFORE gdk_init otherwise options such as 
272      --help --version which ought to work without an X server, won't.
273   */
274   parser = argv_parser_create ();
275   argv_parser_add_options (parser, startup_options, N_STARTUP_OPTIONS,
276                            startup_option_callback, &show_splash);
277   source_init_register_argv_parser (parser);
278   if (!argv_parser_run (parser, argc, argv))
279     exit (EXIT_FAILURE);
280   argv_parser_destroy (parser);
281
282   /* Initialise GDK.  GTK gets initialized later. */
283   gdk_init (&argc, &argv);
284
285   GMainContext *context = g_main_context_new ();
286   
287   GdkWindow *win = show_splash ? create_splash_window (context) : NULL;
288
289   GMainLoop *loop = g_main_loop_new (context, FALSE);
290
291   GSource *ss = g_source_new (&init_funcs,
292                               sizeof (struct init_source));
293   
294   ((struct init_source *) ss)->state = 0;
295   
296   g_source_set_priority (ss, G_PRIORITY_DEFAULT);
297     
298   g_source_attach (ss, context);
299
300   ((struct init_source *) ss)->argc = &argc;
301   ((struct init_source *) ss)->argv = &argv;
302   ((struct init_source *) ss)->loop = loop;
303   ((struct init_source *) ss)->file = optind < argc ? argv[optind] : NULL;
304   
305   g_source_unref (ss);
306
307   g_main_loop_run (loop);
308
309   g_main_loop_unref (loop);
310   g_main_context_unref (context);
311
312   if (win)
313     g_timeout_add (500, destroy_splash_window, win);
314
315   gtk_main ();
316
317   /* Not much point in this except to check for memory leaks */
318   de_initialize ();
319   
320   return 0;
321 }
322
323
324
325 \f
326
327 struct splash_source
328 {
329   GSource parent;
330   cairo_surface_t *sfc;
331 };
332
333 void
334 fill_splash_window (GdkWindow *win, cairo_surface_t *sfce)
335 {
336   cairo_t *cr = gdk_cairo_create (win);
337   
338   cairo_set_source_surface (cr, sfce, 0, 0);
339   
340   cairo_paint (cr);
341   cairo_destroy (cr);
342 }
343
344 gboolean
345 splash_prepare  (GSource    *source,
346             gint       *timeout_)
347 {
348   GdkEvent *e = gdk_event_peek ();
349   if (!e)
350     return FALSE;
351
352   gdk_event_free (e);
353   return TRUE;
354 }
355
356 gboolean
357 splash_check   (GSource    *source)
358 {
359   GdkEvent *e = gdk_event_peek ();
360   if (!e)
361     return FALSE;
362
363   gdk_event_free (e);
364   return TRUE;
365 }
366
367
368 gboolean
369 splash_dispatch (GSource *ss,
370             GSourceFunc callback,
371             gpointer    user_data)
372 {
373   struct splash_source *source = (struct splash_source *) ss;
374   GdkEvent *e = gdk_event_get ();
375   if (!e)
376     return TRUE;
377
378   GdkWindow *w = ((GdkEventAny *)e)->window;
379
380   if (!w)
381     {
382       gdk_event_free (e);
383       return TRUE;
384     }
385
386   fill_splash_window (w, source->sfc);
387   gdk_display_flush (gdk_window_get_display (w));
388
389   gdk_event_free (e);
390
391   return TRUE;
392 }
393
394
395 gboolean
396 destroy_splash_window (gpointer ud)
397 {
398   GdkWindow *win = GDK_WINDOW (ud);
399   gdk_window_withdraw (win);
400   gdk_display_flush (gdk_window_get_display (win));
401   gdk_window_destroy (win);
402   
403   return FALSE;
404 }
405
406 GSourceFuncs splash_funcs = {splash_prepare, splash_check, splash_dispatch, NULL};
407
408
409 GdkWindow *
410 create_splash_window (GMainContext *context)
411 {
412   const gchar *filename = PKGDATADIR "/splash.png";
413
414   const char *relocated_filename = relocate (filename);
415   cairo_surface_t *the_surface = 
416     cairo_image_surface_create_from_png  (relocated_filename);
417
418   if (filename != relocated_filename)
419     free (CONST_CAST (char *, relocated_filename));
420
421   
422   g_return_val_if_fail (the_surface, NULL);
423     
424   int attr_mask = GDK_WA_TYPE_HINT;
425   GdkWindowAttr attr;
426     
427   attr.width =  cairo_image_surface_get_width (the_surface);
428   attr.height = cairo_image_surface_get_height (the_surface);
429   attr.wclass = GDK_INPUT_OUTPUT; 
430   attr.window_type = GDK_WINDOW_TOPLEVEL;
431     
432   attr.type_hint = GDK_WINDOW_TYPE_HINT_SPLASHSCREEN;
433     
434   GdkWindow *win = gdk_window_new (NULL, &attr, attr_mask);
435     
436   gdk_window_set_events (win, GDK_EXPOSURE_MASK);
437   gdk_window_set_keep_above (win, TRUE);
438   gdk_window_show (win);
439   
440
441   GSource *ss = g_source_new (&splash_funcs,
442                               sizeof (struct splash_source));
443   
444   ((struct splash_source *) ss)->sfc = the_surface;
445   g_source_set_priority (ss, G_PRIORITY_HIGH);
446     
447   g_source_attach (ss, context);
448
449   g_source_unref (ss);
450     
451   return win;
452 }
453
454