Merge commit 'origin/stable'
[pspp-builds.git] / src / ui / gui / main.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2005, 2006  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 #include <gl/xalloc.h>
19 #include <gtk/gtk.h>
20 #include "psppire.h"
21 #include "progname.h"
22 #include <stdlib.h>
23 #include <argp.h>
24 #include <gl/relocatable.h>
25 #include <ui/command-line.h>
26 #include <ui/source-init-opts.h>
27
28 #include <libpspp/version.h>
29 #include <libpspp/copyleft.h>
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33 #define N_(msgid) msgid
34
35 const char *argp_program_version = version;
36 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
37
38 \f
39 /* Arguments to be interpreted before the X server gets initialised */
40
41 static const struct argp_option startup_options [] =
42   {
43     {"no-splash",  'q',  0,  0,  N_("Don't show the splash screen"), 0 },
44     { 0, 0, 0, 0, 0, 0 }
45   };
46
47 static error_t
48 parse_startup_opts (int key, char *arg, struct argp_state *state)
49 {
50   gboolean *showsplash = state->input;
51
52   switch (key)
53     {
54     case 'q':
55       *showsplash = FALSE;
56       break;
57     default:
58       return ARGP_ERR_UNKNOWN;
59     }
60   return 0;
61 }
62
63 static const struct argp startup_argp = {startup_options, parse_startup_opts, 0, 0, 0, 0, 0};
64
65 \f
66
67 static GtkWidget *
68 create_splash_window (void)
69 {
70   GtkWidget *splash ;
71   GtkWidget *image;
72
73   gtk_window_set_auto_startup_notification (FALSE);
74
75   splash = gtk_window_new (GTK_WINDOW_POPUP);
76
77   gtk_window_set_position (GTK_WINDOW (splash),
78                            GTK_WIN_POS_CENTER_ALWAYS);
79
80   gtk_window_set_type_hint (GTK_WINDOW (splash),
81                             GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
82
83   image = gtk_image_new_from_file (relocate (PKGDATADIR "/splash.png"));
84
85   gtk_container_add (GTK_CONTAINER (splash), image);
86
87   gtk_widget_show (image);
88
89   return splash;
90 }
91
92 static gboolean
93 hide_splash_window (gpointer data)
94 {
95   GtkWidget *splash = data;
96   gtk_widget_destroy (splash);
97   gtk_window_set_auto_startup_notification (TRUE);
98   return FALSE;
99 }
100
101
102 static gboolean
103 quit_one_loop (gpointer data)
104 {
105   gtk_main_quit ();
106   return FALSE;
107 }
108
109 struct initialisation_parameters
110 {
111   int argc;
112   char **argv;
113   GtkWidget *splash_window;
114   struct command_line_processor *clp;
115 };
116
117
118 static gboolean
119 run_inner_loop (gpointer data)
120 {
121   struct initialisation_parameters *ip = data;
122   initialize (ip->clp, ip->argc, ip->argv);
123
124   g_timeout_add (500, hide_splash_window, ip->splash_window);
125
126   gtk_main ();
127
128   de_initialize ();
129
130   return FALSE;
131 }
132
133
134 static GMemVTable vtable =
135   {
136     xmalloc,
137     xrealloc,
138     free,
139     xcalloc,
140     malloc,
141     realloc
142   };
143
144 int
145 main (int argc, char *argv[])
146 {
147   struct command_line_processor *clp ;
148   struct initialisation_parameters init_p;
149   gboolean show_splash = TRUE;
150
151   const gchar *vers;
152
153   set_program_name (argv[0]);
154
155   g_mem_set_vtable (&vtable);
156
157   gtk_disable_setlocale ();
158
159
160   if ( ! gtk_parse_args (&argc, &argv) )
161     {
162       perror ("Error parsing arguments");
163       exit (1);
164     }
165
166   if ( (vers = gtk_check_version (GTK_MAJOR_VERSION,
167                                  GTK_MINOR_VERSION,
168                                  GTK_MICRO_VERSION)) )
169     {
170       g_warning (vers);
171     }
172
173   clp = command_line_processor_create (_("PSPPIRE --- A user interface for PSPP"), "[ DATA-FILE ]", 0);
174
175   command_line_processor_add_options (clp, &startup_argp, _("Miscellaneous options:"),  &show_splash);
176   command_line_processor_add_options (clp, &post_init_argp,
177                                       _("Options affecting syntax and behavior:"),  NULL);
178   command_line_processor_add_options (clp, &non_option_argp, NULL, NULL);
179
180   command_line_processor_parse (clp, argc, argv);
181
182   gdk_init (&argc, &argv);
183
184   init_p.splash_window = create_splash_window ();
185   init_p.argc = argc;
186   init_p.argv = argv;
187   init_p.clp = clp;
188
189   if ( show_splash )
190     gtk_widget_show (init_p.splash_window);
191
192   g_idle_add (quit_one_loop, 0);
193
194   gtk_quit_add (0, run_inner_loop, &init_p);
195   gtk_main ();
196
197   return 0;
198 }