bab489a5aad88b13072822e4b113df887b8094a4
[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 <gtk/gtk.h>
18 #include "psppire.h"
19 #include "progname.h"
20 #include <stdlib.h>
21 #include <getopt.h>
22
23 #include <libpspp/version.h>
24 #include <libpspp/copyleft.h>
25
26 static gboolean parse_command_line (int *argc, char ***argv, gchar **filename,
27                                     gboolean *show_splash, GError **err);
28
29
30
31 static GtkWidget *
32 create_splash_window (void)
33 {
34   GtkWidget *splash ;
35   GtkWidget *image;
36
37   gtk_window_set_auto_startup_notification (FALSE);
38
39   splash = gtk_window_new (GTK_WINDOW_POPUP);
40
41   gtk_window_set_position (GTK_WINDOW (splash),
42                            GTK_WIN_POS_CENTER_ALWAYS);
43
44   gtk_window_set_type_hint (GTK_WINDOW (splash),
45                             GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
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   g_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 }