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