Take advantage of Gnulib configmake module.
[pspp-builds.git] / src / ui / gui / main.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2005, 2006, 2010  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 "libpspp/argv-parser.h"
25 #include "libpspp/assertion.h"
26 #include "libpspp/getl.h"
27 #include "libpspp/version.h"
28 #include "libpspp/copyleft.h"
29 #include "ui/source-init-opts.h"
30
31 #include "gl/configmake.h"
32 #include "gl/progname.h"
33 #include "gl/relocatable.h"
34 #include "gl/xalloc.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
39
40 \f
41 /* Arguments to be interpreted before the X server gets initialised */
42
43 enum
44   {
45     OPT_NO_SPLASH,
46     N_STARTUP_OPTIONS
47   };
48
49 static const struct argv_option startup_options[N_STARTUP_OPTIONS] =
50   {
51     {"no-splash", 'q', no_argument, OPT_NO_SPLASH}
52   };
53
54 static void
55 startup_option_callback (int id, void *show_splash_)
56 {
57   gboolean *show_splash = show_splash_;
58
59   switch (id)
60     {
61     case OPT_NO_SPLASH:
62       *show_splash = FALSE;
63       break;
64
65     default:
66       NOT_REACHED ();
67     }
68 }
69 \f
70 static GtkWidget *
71 create_splash_window (void)
72 {
73   GtkWidget *splash ;
74   GtkWidget *image;
75
76   gtk_window_set_auto_startup_notification (FALSE);
77
78   splash = gtk_window_new (GTK_WINDOW_POPUP);
79
80   gtk_window_set_position (GTK_WINDOW (splash),
81                            GTK_WIN_POS_CENTER_ALWAYS);
82
83   gtk_window_set_type_hint (GTK_WINDOW (splash),
84                             GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
85
86   image = gtk_image_new_from_file (relocate (PKGDATADIR "/splash.png"));
87
88   gtk_container_add (GTK_CONTAINER (splash), image);
89
90   gtk_widget_show (image);
91
92   return splash;
93 }
94
95 static gboolean
96 hide_splash_window (gpointer data)
97 {
98   GtkWidget *splash = data;
99   gtk_widget_destroy (splash);
100   gtk_window_set_auto_startup_notification (TRUE);
101   return FALSE;
102 }
103
104
105 static gboolean
106 quit_one_loop (gpointer data)
107 {
108   gtk_main_quit ();
109   return FALSE;
110 }
111
112 struct initialisation_parameters
113 {
114   struct source_stream *ss;
115   const char *data_file;
116   GtkWidget *splash_window;
117 };
118
119
120 static gboolean
121 run_inner_loop (gpointer data)
122 {
123   struct initialisation_parameters *ip = data;
124   initialize (ip->ss, ip->data_file);
125
126   g_timeout_add (500, hide_splash_window, ip->splash_window);
127
128   gtk_main ();
129
130   de_initialize ();
131
132   return FALSE;
133 }
134
135
136 static GMemVTable vtable =
137   {
138     xmalloc,
139     xrealloc,
140     free,
141     xcalloc,
142     malloc,
143     realloc
144   };
145
146 int
147 main (int argc, char *argv[])
148 {
149   struct initialisation_parameters init_p;
150   gboolean show_splash = TRUE;
151   struct argv_parser *parser;
152   struct source_stream *ss;
153   const gchar *vers;
154
155   set_program_name (argv[0]);
156
157   g_mem_set_vtable (&vtable);
158
159   gtk_disable_setlocale ();
160
161
162   if ( ! gtk_parse_args (&argc, &argv) )
163     {
164       perror ("Error parsing arguments");
165       exit (1);
166     }
167
168   if ( (vers = gtk_check_version (GTK_MAJOR_VERSION,
169                                  GTK_MINOR_VERSION,
170                                  GTK_MICRO_VERSION)) )
171     {
172       g_warning ("%s", vers);
173     }
174
175   /* Let GDK remove any options that it owns. */
176   gdk_init (&argc, &argv);
177
178   /* Parse our own options. */
179   ss = create_source_stream ();
180   parser = argv_parser_create ();
181   argv_parser_add_options (parser, startup_options, N_STARTUP_OPTIONS,
182                            startup_option_callback, &show_splash);
183   source_init_register_argv_parser (parser, ss);
184   if (!argv_parser_run (parser, argc, argv))
185     exit (EXIT_FAILURE);
186   argv_parser_destroy (parser);
187
188   init_p.splash_window = create_splash_window ();
189   init_p.ss = ss;
190   init_p.data_file = optind < argc ? argv[optind] : NULL;
191
192   if ( show_splash )
193     gtk_widget_show (init_p.splash_window);
194
195   g_idle_add (quit_one_loop, 0);
196
197   gtk_quit_add (0, run_inner_loop, &init_p);
198   gtk_main ();
199
200   return 0;
201 }