002d576ac5c1d5bbd1d55283e56a81a2260280e1
[pspp-builds.git] / src / ui / gui / main.c
1 /*
2    PSPPIRE --- A Graphical User Interface for PSPP
3    Copyright (C) 2004, 2005, 2006  Free Software Foundation
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <gtk/gtk.h>
21 #include "psppire.h"
22 #include "progname.h"
23 #include <stdlib.h>
24 #include <getopt.h>
25
26 #include <libpspp/version.h>
27 #include <libpspp/copyleft.h>
28
29 static gboolean parse_command_line (int *argc, char ***argv, gchar **filename,
30                                     gboolean *show_splash, GError **err);
31
32
33
34 static GtkWidget *
35 create_splash_window (void)
36 {
37   GtkWidget *splash ;
38   GtkWidget *image;
39
40   gtk_window_set_auto_startup_notification (FALSE);
41
42   splash = gtk_window_new (GTK_WINDOW_POPUP);
43
44   gtk_window_set_position (GTK_WINDOW (splash),
45                            GTK_WIN_POS_CENTER_ALWAYS);
46
47   image = gtk_image_new_from_file (PKGDATADIR "/splash.png");
48
49   gtk_container_add (GTK_CONTAINER (splash), image);
50
51   gtk_widget_show (image);
52
53   return splash;
54 }
55
56 static gboolean
57 hide_splash_window (gpointer data)
58 {
59   GtkWidget *splash = data;
60   gtk_widget_hide (splash);
61   gtk_window_set_auto_startup_notification (TRUE);
62   return FALSE;
63 }
64
65
66 static gboolean
67 quit_one_loop (gpointer data)
68 {
69   gtk_main_quit ();
70   return FALSE;
71 }
72
73
74 static gboolean
75 run_inner_loop (gpointer data)
76 {
77   initialize ();
78
79   g_timeout_add (500, hide_splash_window, data);
80
81   gtk_main ();
82
83   de_initialize ();
84
85   return FALSE;
86 }
87
88
89
90 int
91 main (int argc, char *argv[])
92 {
93   GtkWidget *splash_window;
94   gchar *filename = 0;
95   gboolean show_splash = TRUE;
96   GError *err = 0;
97   gchar *vers;
98
99   set_program_name (argv[0]);
100
101   if ( ! gtk_parse_args (&argc, &argv) )
102     {
103       perror ("Error parsing arguments");
104       exit (1);
105     }
106
107   if ( (vers = gtk_check_version (GTK_MAJOR_VERSION,
108                                  GTK_MINOR_VERSION,
109                                  GTK_MICRO_VERSION)) )
110     {
111       g_critical (vers);
112     }
113
114   /* Deal with options like --version, --help etc */
115   if ( ! parse_command_line (&argc, &argv, &filename, &show_splash, &err) )
116     {
117       g_clear_error (&err);
118       return 0;
119     }
120
121   gdk_init (&argc, &argv);
122
123   splash_window = create_splash_window ();
124   if ( show_splash )
125     gtk_widget_show (splash_window);
126
127   gtk_idle_add (quit_one_loop, 0);
128
129   gtk_quit_add (0, run_inner_loop, splash_window);
130   gtk_main ();
131
132
133   return 0;
134 }
135
136
137 /* Parses the command line specified by ARGC and ARGV as received by
138    main ().  Returns true if normal execution should proceed,
139    false if the command-line indicates that PSPP should exit. */
140 static gboolean
141 parse_command_line (int *argc, char ***argv, gchar **filename,
142                     gboolean *show_splash, GError **err)
143 {
144
145   static struct option long_options[] =
146     {
147       {"help", no_argument, NULL, 'h'},
148       {"version", no_argument, NULL, 'V'},
149       {"no-splash", no_argument, NULL, 'q'},
150       {0, 0, 0, 0},
151     };
152
153   int c;
154
155   for (;;)
156     {
157       c = getopt_long (*argc, *argv, "hVq", long_options, NULL);
158       if (c == -1)
159         break;
160
161       switch (c)
162         {
163         case 'h':
164           g_print ("Usage: psppire {|--help|--version|--no-splash}\n");
165           return FALSE;
166         case 'V':
167           g_print (version);
168           g_print ("\n");
169           g_print (legal);
170           return FALSE;
171         case 'q':
172           *show_splash = FALSE;
173           break;
174         default:
175           return FALSE;
176         }
177     }
178
179   if ( optind < *argc)
180     {
181       *filename = (*argv)[optind];
182     }
183
184   return TRUE;
185 }