merge master->gtk3, fixed psppire-output-view.c refactoring; this compiles and runs...
[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  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     OPT_MEASURE_STARTUP,
54     N_STARTUP_OPTIONS
55   };
56
57 static const struct argv_option startup_options[N_STARTUP_OPTIONS] =
58   {
59     {"help",      'h', no_argument, OPT_HELP},
60     {"version",   'V', no_argument, OPT_VERSION},
61     {"no-splash", 'q', no_argument, OPT_NO_SPLASH},
62     {"measure-startup", 0, no_argument, OPT_MEASURE_STARTUP},
63   };
64
65 /* --measure-startup: Prints the elapsed time to start up and load any file
66    specified on the command line. */
67 static gboolean measure_startup;
68 static GTimer *startup;
69
70 static void
71 usage (void)
72 {
73   char *inc_path = string_array_join (include_path_default (), " ");
74   GOptionGroup *gtk_options;
75   GOptionContext *ctx;
76   gchar *gtk_help_base, *gtk_help;
77
78   /* Get help text for GTK+ options.  */
79   ctx = g_option_context_new ("psppire");
80   gtk_options = gtk_get_option_group (FALSE);
81   gtk_help_base = g_option_context_get_help (ctx, FALSE, gtk_options);
82   g_option_context_free (ctx);
83
84   /* The GTK+ help text starts with usage instructions that we don't want,
85      followed by a blank line.  Trim off everything up to and including the
86      first blank line. */
87   gtk_help = strstr (gtk_help_base, "\n\n");
88   gtk_help = gtk_help != NULL ? gtk_help + 2 : gtk_help_base;
89
90   printf (_("\
91 PSPPIRE, a GUI for PSPP, a program for statistical analysis of sampled data.\n\
92 Usage: %s [OPTION]... FILE\n\
93 \n\
94 Arguments to long options also apply to equivalent short options.\n\
95 \n\
96 GUI options:\n\
97   -q, --no-splash           don't show splash screen during startup\n\
98 \n\
99 %s\
100 Language options:\n\
101   -I, --include=DIR         append DIR to search path\n\
102   -I-, --no-include         clear search path\n\
103   -a, --algorithm={compatible|enhanced}\n\
104                             set to `compatible' if you want output\n\
105                             calculated from broken algorithms\n\
106   -x, --syntax={compatible|enhanced}\n\
107                             set to `compatible' to disable PSPP extensions\n\
108   -i, --interactive         interpret syntax in interactive mode\n\
109   -s, --safer               don't allow some unsafe operations\n\
110 Default search path: %s\n\
111 \n\
112 Informative output:\n\
113   -h, --help                display this help and exit\n\
114   -V, --version             output version information and exit\n\
115 \n\
116 A non-option argument is interpreted as a data file in .sav or .zsav or .por\n\
117 format or a syntax file to load.\n"),
118           program_name, gtk_help, inc_path);
119
120   free (inc_path);
121   g_free (gtk_help_base);
122
123   emit_bug_reporting_address ();
124   exit (EXIT_SUCCESS);
125 }
126
127 static void
128 startup_option_callback (int id, void *show_splash_)
129 {
130   gboolean *show_splash = show_splash_;
131
132   switch (id)
133     {
134     case OPT_HELP:
135       usage ();
136       break;
137
138     case OPT_VERSION:
139       version_etc (stdout, "psppire", PACKAGE_NAME, PACKAGE_VERSION,
140                    "Ben Pfaff", "John Darrington", "Jason Stover",
141                    NULL_SENTINEL);
142       exit (EXIT_SUCCESS);
143
144     case OPT_NO_SPLASH:
145       *show_splash = FALSE;
146       break;
147
148     case OPT_MEASURE_STARTUP:
149       measure_startup = TRUE;
150       break;
151
152     default:
153       NOT_REACHED ();
154     }
155 }
156 \f
157 static GtkWidget *
158 create_splash_window (void)
159 {
160   GtkWidget *splash ;
161   GtkWidget *image;
162
163   gtk_window_set_auto_startup_notification (FALSE);
164
165   splash = gtk_window_new (GTK_WINDOW_POPUP);
166
167   gtk_window_set_position (GTK_WINDOW (splash),
168                            GTK_WIN_POS_CENTER_ALWAYS);
169
170   gtk_window_set_type_hint (GTK_WINDOW (splash),
171                             GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
172
173   image = gtk_image_new_from_file (relocate (PKGDATADIR "/splash.png"));
174
175   gtk_container_add (GTK_CONTAINER (splash), image);
176
177   gtk_widget_show (image);
178
179   return splash;
180 }
181
182 static gboolean
183 hide_splash_window (gpointer data)
184 {
185   GtkWidget *splash = data;
186   gtk_widget_destroy (splash);
187   gtk_window_set_auto_startup_notification (TRUE);
188   return FALSE;
189 }
190
191 static gboolean
192 print_startup_time (gpointer data)
193 {
194   g_timer_stop (startup);
195   printf ("%.3f seconds elapsed\n", g_timer_elapsed (startup, NULL));
196   g_timer_destroy (startup);
197   startup = NULL;
198
199   return FALSE;
200 }
201
202 /*
203 static gboolean
204 quit_one_loop (gpointer data)
205 {
206   gtk_main_quit ();
207   return FALSE;
208 }
209 */
210
211 struct initialisation_parameters
212 {
213   const char *data_file;
214   GtkWidget *splash_window;
215 };
216
217
218 static gboolean
219 run_inner_loop (gpointer data)
220 {
221   struct initialisation_parameters *ip = data;
222   initialize (ip->data_file);
223
224   g_timeout_add (500, hide_splash_window, ip->splash_window);
225
226   if (measure_startup)
227     {
228       GSource *source = g_idle_source_new ();
229       g_source_set_priority (source, G_PRIORITY_LOW);
230       g_source_set_callback (source, print_startup_time, NULL, NULL);
231       g_source_attach (source, NULL);
232       g_source_unref (source);
233     }
234
235   gtk_main ();
236
237   de_initialize ();
238
239   return FALSE;
240 }
241
242
243 static GMemVTable vtable =
244   {
245     xmalloc,
246     xrealloc,
247     free,
248     xcalloc,
249     malloc,
250     realloc
251   };
252
253 #ifdef __APPLE__
254 static const bool apple = true;
255 #else
256 static const bool apple = false;
257 #endif
258
259 /* Searches ARGV for the -psn_xxxx option that the desktop application
260    launcher passes in, and removes it if it finds it.  Returns the new value
261    of ARGC. */
262 static inline int
263 remove_psn (int argc, char **argv)
264 {
265   if (apple)
266     {
267       int i;
268
269       for (i = 0; i < argc; i++)
270         {
271           if (!strncmp (argv[i], "-psn", 4))
272             {
273               remove_element (argv, argc + 1, sizeof *argv, i);
274               return argc - 1;
275             }
276         }
277     }
278   return argc;
279 }
280
281 int
282 main (int argc, char *argv[])
283 {
284   struct initialisation_parameters init_p;
285   gboolean show_splash = TRUE;
286   struct argv_parser *parser;
287   const gchar *vers;
288
289   set_program_name (argv[0]);
290
291   g_mem_set_vtable (&vtable);
292
293 #if !GLIB_CHECK_VERSION(2,32,0)
294   /* g_thread_init() was required before glib 2.32, but it is deprecated since
295      then and calling it yields a compile-time warning. */
296   g_thread_init (NULL);
297 #endif
298
299   gtk_disable_setlocale ();
300
301   startup = g_timer_new ();
302   g_timer_start (startup);
303
304   if ( ! gtk_parse_args (&argc, &argv) )
305     {
306       perror ("Error parsing arguments");
307       exit (1);
308     }
309
310   if ( (vers = gtk_check_version (GTK_MAJOR_VERSION,
311                                  GTK_MINOR_VERSION,
312                                  GTK_MICRO_VERSION)) )
313     {
314       g_warning ("%s", vers);
315     }
316
317   argc = remove_psn (argc, argv);
318
319   /* Parse our own options. 
320      This must come BEFORE gdk_init otherwise options such as 
321      --help --version which ought to work without an X server, won't.
322   */
323   parser = argv_parser_create ();
324   argv_parser_add_options (parser, startup_options, N_STARTUP_OPTIONS,
325                            startup_option_callback, &show_splash);
326   source_init_register_argv_parser (parser);
327   if (!argv_parser_run (parser, argc, argv))
328     exit (EXIT_FAILURE);
329   argv_parser_destroy (parser);
330
331   /* Initialise GDK.  Theoretically this call can remove options from argc,argv if
332      it thinks they are gdk options.
333      However there shouldn't be any here because of the gtk_parse_args call above. */
334   gdk_init (&argc, &argv);
335
336   init_p.splash_window = create_splash_window ();
337   init_p.data_file = optind < argc ? argv[optind] : NULL;
338
339   //  if ( show_splash )
340   //    gtk_widget_show (init_p.splash_window);
341
342   //  g_idle_add (quit_one_loop, 0);
343
344   //  gtk_quit_add (0, run_inner_loop, &init_p);
345   run_inner_loop (&init_p);
346   //  gtk_main ();
347
348   return 0;
349 }