fe2bf09e402c86d998113e4f297a3d651b3b0136
[pspp-builds.git] / src / ui / gui / psppire.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 <config.h>
21
22 #include <assert.h>
23 #include <libintl.h>
24
25
26 #include "data-editor.h"
27 #include <libpspp/version.h>
28 #include <libpspp/copyleft.h>
29 #include <data/file-handle-def.h>
30 #include <data/format.h>
31 #include <data/storage-stream.h>
32 #include <data/case-source.h>
33 #include <data/settings.h>
34 #include <data/file-name.h>
35 #include <data/procedure.h>
36 #include <libpspp/getl.h>
37 #include <language/lexer/lexer.h>
38 #include <ui/flexifile.h>
39
40 #include <getopt.h>
41 #include <gtk/gtk.h>
42 #include <gtk/gtk.h>
43 #include <glade/glade.h>
44 #include "psppire-dict.h"
45 #include "psppire-var-store.h"
46 #include "psppire-data-store.h"
47 #include "helper.h"
48 #include "data-sheet.h"
49 #include "var-sheet.h"
50 #include "message-dialog.h"
51 #include "flexifile-factory.h"
52
53 PsppireDataStore *the_data_store = 0;
54
55
56 static bool parse_command_line (int *argc, char ***argv,
57                                 gchar **filename, GError **err);
58
59
60 PsppireVarStore *the_var_store = 0;
61
62 void create_icon_factory (void);
63
64 struct source_stream *the_source_stream ;
65 struct dataset * the_dataset = NULL;
66
67 static void
68 replace_dictionary (struct dictionary *d)
69 {
70   psppire_dict_replace_dictionary (the_data_store->dict,
71                                    d);
72 }
73
74
75 static void
76 replace_flexifile (struct case_source *s)
77 {
78   if ( NULL == s )
79     psppire_case_file_replace_flexifile (the_data_store->case_file,
80                                          (struct flexifile *) flexifile_create (0));
81   else
82     {
83       if ( ! case_source_is_class (s, &storage_source_class))
84         return ;
85
86       psppire_case_file_replace_flexifile (the_data_store->case_file,
87                                            (struct flexifile *)
88                                            storage_source_get_casefile (s));
89     }
90 }
91
92
93 int
94 main (int argc, char *argv[])
95 {
96   struct casefile_factory *factory;
97   PsppireDict *dictionary = 0;
98
99   gchar *filename=0;
100   GError *err = 0;
101   gchar *vers;
102
103   if ( ! gtk_parse_args (&argc, &argv) ) 
104     {
105       perror ("Error parsing arguments");
106       exit (1);
107     }
108
109   if ( (vers = gtk_check_version (GTK_MAJOR_VERSION,
110                                  GTK_MINOR_VERSION,
111                                  GTK_MICRO_VERSION)) )
112     {
113       g_critical (vers);
114     }
115
116
117   /* gtk_init messes with the locale.
118      So unset the bits we want to control ourselves */
119   setlocale (LC_NUMERIC, "C");
120
121   bindtextdomain (PACKAGE, locale_dir);
122
123   textdomain (PACKAGE);
124
125   if ( ! parse_command_line (&argc, &argv, &filename, &err) )
126     {
127       g_clear_error (&err);
128       return 0;
129     }
130
131   glade_init ();
132
133   fmt_init ();
134   settings_init ();
135   fh_init ();
136   factory = flexifile_factory_create ();
137   the_source_stream = create_source_stream (
138                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
139                           );
140
141   the_dataset = create_dataset (factory,
142                                 replace_flexifile,
143                                 replace_dictionary);
144
145   message_dialog_init (the_source_stream);
146
147   dictionary = psppire_dict_new_from_dict (
148                                            dataset_dict (the_dataset)
149                                            );
150
151   bind_textdomain_codeset (PACKAGE, "UTF-8");
152
153   gdk_init (&argc, &argv);
154
155   /* Create the model for the var_sheet */
156   the_var_store = psppire_var_store_new (dictionary);
157
158
159   the_data_store = psppire_data_store_new (dictionary);
160
161   proc_set_source (the_dataset,
162                    storage_source_create (the_data_store->case_file->flexifile)
163                    );
164
165   create_icon_factory ();
166
167   new_data_window (NULL, NULL);
168
169   /* start the event loop */
170   gtk_main ();
171
172   destroy_source_stream (the_source_stream);
173   message_dialog_done ();
174
175   settings_done ();
176
177   return 0;
178 }
179
180
181 /* Parses the command line specified by ARGC and ARGV as received by
182    main ().  Returns true if normal execution should proceed,
183    false if the command-line indicates that PSPP should exit. */
184 static bool
185 parse_command_line (int *argc, char ***argv, gchar **filename, GError **err)
186 {
187   static struct option long_options[] =
188     {
189       {"help", no_argument, NULL, 'h'},
190       {"version", no_argument, NULL, 'V'},
191       {0, 0, 0, 0},
192     };
193
194   int c;
195
196   for (;;)
197     {
198       c = getopt_long (*argc, *argv, "hV", long_options, NULL);
199       if (c == -1)
200         break;
201
202       switch (c)
203         {
204         case 'h':
205           g_print ("Usage: psppire {|--help|--version}\n");
206           return false;
207         case 'V':
208           g_print (version);
209           g_print ("\n");
210           g_print (legal);
211           return false;
212         default:
213           return false;
214         }
215     }
216
217   if ( optind < *argc)
218     {
219       *filename = (*argv)[optind];
220     }
221
222   return true;
223 }
224
225
226
227 void
228 create_icon_factory (void)
229 {
230   GtkIconFactory *factory = gtk_icon_factory_new ();
231
232   GtkIconSet *icon_set;
233
234   GdkPixbuf *pixbuf;
235
236   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/value-labels.png", 0);
237   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
238   g_object_unref (pixbuf);
239   gtk_icon_factory_add ( factory, "pspp-value-labels", icon_set);
240
241   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/weight-cases.png", 0);
242   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
243   g_object_unref (pixbuf);
244   gtk_icon_factory_add ( factory, "pspp-weight-cases", icon_set);
245
246   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/goto-variable.png", 0);
247   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
248   g_object_unref (pixbuf);
249   gtk_icon_factory_add ( factory, "pspp-goto-variable", icon_set);
250
251   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/insert-variable.png", 0);
252   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
253   g_object_unref (pixbuf);
254   gtk_icon_factory_add ( factory, "pspp-insert-variable", icon_set);
255
256   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/insert-case.png", 0);
257   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
258   g_object_unref (pixbuf);
259   gtk_icon_factory_add ( factory, "pspp-insert-case", icon_set);
260
261   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/split-file.png", 0);
262   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
263   g_object_unref (pixbuf);
264   gtk_icon_factory_add ( factory, "pspp-split-file", icon_set);
265
266   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/select-cases.png", 0);
267   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
268   g_object_unref (pixbuf);
269   gtk_icon_factory_add ( factory, "pspp-select-cases", icon_set);
270
271   gtk_icon_factory_add_default (factory);
272 }
273