1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2004, 2005, 2006, 2010, 2011, 2012, 2013, 2014, 2015 Free Software Foundation
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.
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.
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/>. */
19 #include "ui/gui/psppire.h"
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"
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"
42 #define _(msgid) gettext (msgid)
43 #define N_(msgid) msgid
46 GdkWindow *create_splash_window (GMainContext *context);
47 gboolean destroy_splash_window (gpointer ud);
52 /* Arguments to be interpreted before the X server gets initialised */
63 static const struct argv_option startup_options[N_STARTUP_OPTIONS] =
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},
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;
79 char *inc_path = string_array_join (include_path_default (), " ");
80 GOptionGroup *gtk_options;
82 gchar *gtk_help_base, *gtk_help;
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);
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
93 gtk_help = strstr (gtk_help_base, "\n\n");
94 gtk_help = gtk_help != NULL ? gtk_help + 2 : gtk_help_base;
97 PSPPIRE, a GUI for PSPP, a program for statistical analysis of sampled data.\n\
98 Usage: %s [OPTION]... FILE\n\
100 Arguments to long options also apply to equivalent short options.\n\
103 -q, --no-splash don't show splash screen during startup\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\
118 Informative output:\n\
119 -h, --help display this help and exit\n\
120 -V, --version output version information and exit\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);
127 g_free (gtk_help_base);
129 emit_bug_reporting_address ();
134 startup_option_callback (int id, void *show_splash_)
136 gboolean *show_splash = show_splash_;
145 version_etc (stdout, "psppire", PACKAGE_NAME, PACKAGE_VERSION,
146 "Ben Pfaff", "John Darrington", "Jason Stover",
151 *show_splash = FALSE;
154 case OPT_MEASURE_STARTUP:
155 measure_startup = TRUE;
164 print_startup_time (gpointer data)
166 g_timer_stop (startup);
167 printf ("%.3f seconds elapsed\n", g_timer_elapsed (startup, NULL));
168 g_timer_destroy (startup);
174 static GMemVTable vtable =
185 static const bool apple = true;
187 static const bool apple = false;
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
194 remove_psn (int argc, char **argv)
200 for (i = 0; i < argc; i++)
202 if (!strncmp (argv[i], "-psn", 4))
204 remove_element (argv, argc + 1, sizeof *argv, i);
224 init_prepare (GSource *source, gint *timeout_)
232 init_check (GSource *source)
239 init_dispatch (GSource *ss,
240 GSourceFunc callback,
243 struct init_source *is = (struct init_source *)ss;
245 bool finished = initialize (is->file, is->state++);
249 g_main_loop_quit (is->loop);
256 static GSourceFuncs init_funcs = {init_prepare, init_check, init_dispatch, NULL};
261 main (int argc, char *argv[])
263 gboolean show_splash = TRUE;
264 struct argv_parser *parser;
267 set_program_name (argv[0]);
269 g_mem_set_vtable (&vtable);
271 #if !GLIB_CHECK_VERSION(2,32,0)
272 /* g_thread_init() was required before glib 2.32, but it is deprecated since
273 then and calling it yields a compile-time warning. */
274 g_thread_init (NULL);
277 gtk_disable_setlocale ();
279 startup = g_timer_new ();
280 g_timer_start (startup);
282 if ( ! gtk_parse_args (&argc, &argv) )
284 perror ("Error parsing arguments");
288 if ( (vers = gtk_check_version (GTK_MAJOR_VERSION,
290 GTK_MICRO_VERSION)) )
292 g_warning ("%s", vers);
295 argc = remove_psn (argc, argv);
297 /* Parse our own options.
298 This must come BEFORE gdk_init otherwise options such as
299 --help --version which ought to work without an X server, won't.
301 parser = argv_parser_create ();
302 argv_parser_add_options (parser, startup_options, N_STARTUP_OPTIONS,
303 startup_option_callback, &show_splash);
304 source_init_register_argv_parser (parser);
305 if (!argv_parser_run (parser, argc, argv))
307 argv_parser_destroy (parser);
309 /* Initialise GDK. Theoretically this call can remove options from argc,argv if
310 it thinks they are gdk options.
311 However there shouldn't be any here because of the gtk_parse_args call above. */
312 gdk_init (&argc, &argv);
314 GMainContext *context = g_main_context_new ();
316 GdkWindow *win = show_splash ? create_splash_window (context) : NULL;
318 GMainLoop *loop = g_main_loop_new (context, FALSE);
320 GSource *ss = g_source_new (&init_funcs,
321 sizeof (struct init_source));
323 ((struct init_source *) ss)->state = 0;
325 g_source_set_priority (ss, G_PRIORITY_DEFAULT);
327 g_source_attach (ss, context);
329 ((struct init_source *) ss)->loop = loop;
330 ((struct init_source *) ss)->file = optind < argc ? argv[optind] : NULL;
334 g_main_loop_run (loop);
336 g_main_loop_unref (loop);
337 g_main_context_unref (context);
340 g_timeout_add (500, destroy_splash_window, win);
344 /* Not much point in this except to check for memory leaks */
357 cairo_surface_t *sfc;
361 fill_splash_window (GdkWindow *win, cairo_surface_t *sfce)
363 cairo_t *cr = gdk_cairo_create (win);
365 cairo_set_source_surface (cr, sfce, 0, 0);
372 splash_prepare (GSource *source,
379 splash_check (GSource *source)
385 splash_dispatch (GSource *ss,
386 GSourceFunc callback,
389 struct splash_source *source = (struct splash_source *) ss;
391 GdkEvent *e = gdk_event_get ();
395 GdkWindow *w = ((GdkEventAny *)e)->window;
403 fill_splash_window (w, source->sfc);
413 destroy_splash_window (gpointer ud)
415 GdkWindow *win = GDK_WINDOW (ud);
416 gdk_window_withdraw (win);
417 gdk_display_flush (gdk_window_get_display (win));
418 gdk_window_destroy (win);
423 GSourceFuncs splash_funcs = {splash_prepare, splash_check, splash_dispatch, NULL};
427 create_splash_window (GMainContext *context)
429 const gchar *filename = PKGDATADIR "/splash.png";
431 const char *relocated_filename = relocate (filename);
432 cairo_surface_t *the_surface =
433 cairo_image_surface_create_from_png (relocated_filename);
435 if (filename != relocated_filename)
436 free (CONST_CAST (char *, relocated_filename));
439 g_return_val_if_fail (the_surface, NULL);
441 int attr_mask = GDK_WA_TYPE_HINT;
444 attr.width = cairo_image_surface_get_width (the_surface);
445 attr.height = cairo_image_surface_get_height (the_surface);
446 attr.wclass = GDK_INPUT_OUTPUT;
447 attr.window_type = GDK_WINDOW_TOPLEVEL;
449 attr.type_hint = GDK_WINDOW_TYPE_HINT_SPLASHSCREEN;
451 GdkWindow *win = gdk_window_new (NULL, &attr, attr_mask);
453 gdk_window_set_events (win, GDK_EXPOSURE_MASK);
454 gdk_window_set_keep_above (win, TRUE);
455 gdk_window_show (win);
458 GSource *ss = g_source_new (&splash_funcs,
459 sizeof (struct splash_source));
461 ((struct splash_source *) ss)->sfc = the_surface;
462 g_source_set_priority (ss, G_PRIORITY_HIGH);
464 g_source_attach (ss, context);