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;
163 static gboolean UNUSED
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);
175 static const bool apple = true;
177 static const bool apple = false;
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
184 remove_psn (int argc, char **argv)
190 for (i = 0; i < argc; i++)
192 if (!strncmp (argv[i], "-psn", 4))
194 remove_element (argv, argc + 1, sizeof *argv, i);
204 init_prepare (GSource *source, gint *timeout_)
212 init_check (GSource *source)
219 init_dispatch (GSource *ss,
220 GSourceFunc callback,
223 struct init_source *is = (struct init_source *)ss;
225 bool finished = initialize (is);
230 g_main_loop_quit (is->loop);
237 static GSourceFuncs init_funcs = {init_prepare, init_check, init_dispatch, NULL};
242 main (int argc, char *argv[])
244 gboolean show_splash = TRUE;
245 struct argv_parser *parser;
248 set_program_name (argv[0]);
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);
256 gtk_disable_setlocale ();
258 startup = g_timer_new ();
259 g_timer_start (startup);
261 if ( (vers = gtk_check_version (GTK_MAJOR_VERSION,
263 GTK_MICRO_VERSION)) )
265 g_warning ("%s", vers);
268 argc = remove_psn (argc, argv);
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.
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))
280 argv_parser_destroy (parser);
282 /* Initialise GDK. GTK gets initialized later. */
283 gdk_init (&argc, &argv);
285 GMainContext *context = g_main_context_new ();
287 GdkWindow *win = show_splash ? create_splash_window (context) : NULL;
289 GMainLoop *loop = g_main_loop_new (context, FALSE);
291 GSource *ss = g_source_new (&init_funcs,
292 sizeof (struct init_source));
294 ((struct init_source *) ss)->state = 0;
296 g_source_set_priority (ss, G_PRIORITY_DEFAULT);
298 g_source_attach (ss, context);
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)->filename_arg = optind < argc ? optind : -1;
307 g_main_loop_run (loop);
309 g_main_loop_unref (loop);
310 g_main_context_unref (context);
313 g_timeout_add (500, destroy_splash_window, win);
317 /* Not much point in this except to check for memory leaks */
330 cairo_surface_t *sfc;
334 fill_splash_window (GdkWindow *win, cairo_surface_t *sfce)
336 cairo_t *cr = gdk_cairo_create (win);
338 cairo_set_source_surface (cr, sfce, 0, 0);
345 splash_prepare (GSource *source,
348 GdkEvent *e = gdk_event_peek ();
357 splash_check (GSource *source)
359 GdkEvent *e = gdk_event_peek ();
369 splash_dispatch (GSource *ss,
370 GSourceFunc callback,
373 struct splash_source *source = (struct splash_source *) ss;
374 GdkEvent *e = gdk_event_get ();
378 GdkWindow *w = ((GdkEventAny *)e)->window;
386 fill_splash_window (w, source->sfc);
387 gdk_display_flush (gdk_window_get_display (w));
396 destroy_splash_window (gpointer ud)
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);
406 GSourceFuncs splash_funcs = {splash_prepare, splash_check, splash_dispatch, NULL};
410 create_splash_window (GMainContext *context)
412 const gchar *filename = PKGDATADIR "/splash.png";
414 const char *relocated_filename = relocate (filename);
415 cairo_surface_t *the_surface =
416 cairo_image_surface_create_from_png (relocated_filename);
418 if (filename != relocated_filename)
419 free (CONST_CAST (char *, relocated_filename));
422 g_return_val_if_fail (the_surface, NULL);
424 int attr_mask = GDK_WA_TYPE_HINT;
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;
432 attr.type_hint = GDK_WINDOW_TYPE_HINT_SPLASHSCREEN;
434 GdkWindow *win = gdk_window_new (NULL, &attr, attr_mask);
436 gdk_window_set_events (win, GDK_EXPOSURE_MASK);
437 gdk_window_set_keep_above (win, TRUE);
438 gdk_window_show (win);
441 GSource *ss = g_source_new (&splash_funcs,
442 sizeof (struct splash_source));
444 ((struct splash_source *) ss)->sfc = the_surface;
445 g_source_set_priority (ss, G_PRIORITY_HIGH);
447 g_source_attach (ss, context);