ed4c790b95a38797dbbe8e730ffa9cfa1c9b0fe4
[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 <libpspp/version.h>
27 #include <libpspp/copyleft.h>
28 #include <data/file-handle-def.h>
29 #include <data/format.h>
30 #include <data/settings.h>
31 #include <data/file-name.h>
32 #include <data/procedure.h>
33 #include <libpspp/getl.h>
34 #include <language/lexer/lexer.h>
35
36 #include <getopt.h>
37 #include <gtk/gtk.h>
38 #include <gtk/gtk.h>
39 #include <glade/glade.h>
40 #include "menu-actions.h"
41 #include "psppire-dict.h"
42 #include "psppire-var-store.h"
43 #include "psppire-data-store.h"
44 #include "helper.h"
45 #include "data-sheet.h"
46 #include "var-sheet.h"
47 #include "message-dialog.h"
48 #include "flexifile-factory.h"
49
50 GladeXML *xml;
51
52
53 PsppireDict *the_dictionary = 0;
54
55 PsppireDataStore *data_store = 0;
56
57
58 static bool parse_command_line (int *argc, char ***argv, 
59                                 gchar **filename, GError **err);
60
61
62 #define _(msgid) gettext (msgid)
63 #define N_(msgid) msgid
64
65 static void
66 give_help(void)
67 {
68   static struct msg m = {
69     MSG_GENERAL, 
70     MSG_NOTE,
71     {0, -1},
72     0, 
73   };
74
75   if (! m.text) 
76     m.text=g_strdup(_("Sorry. The help system hasn't yet been implemented."));
77
78   popup_message(&m);
79 }
80
81 PsppireVarStore *var_store = 0;
82
83 void create_icon_factory (void);
84
85 struct source_stream *the_source_stream ;
86 struct lexer *the_lexer;
87 struct dataset * the_dataset = NULL;
88
89
90 int 
91 main(int argc, char *argv[]) 
92 {
93   struct casefile_factory *factory;
94
95   GtkWidget *data_editor ;
96   GtkSheet *var_sheet ; 
97   GtkSheet *data_sheet ;
98
99   gchar *filename=0;
100   GError *err = 0;
101   gchar *vers;
102
103   gtk_init(&argc, &argv);
104   if ( (vers = gtk_check_version(GTK_MAJOR_VERSION, 
105                                  GTK_MINOR_VERSION, 
106                                  GTK_MICRO_VERSION)) )
107     {
108       g_critical(vers);
109     }
110         
111
112   /* gtk_init messes with the locale. 
113      So unset the bits we want to control ourselves */
114   setlocale (LC_NUMERIC, "C");
115
116   bindtextdomain (PACKAGE, locale_dir);
117
118   textdomain (PACKAGE);
119
120   if ( ! parse_command_line(&argc, &argv, &filename, &err) ) 
121     {
122       g_clear_error(&err);
123       return 0;
124     }
125
126   glade_init();
127
128   fmt_init();
129   settings_init();
130   fh_init ();
131   factory = flexifile_factory_create ();
132   the_source_stream = create_source_stream (
133                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
134                           );
135
136   the_lexer = lex_create (the_source_stream);
137
138   the_dataset = create_dataset (factory);
139
140   message_dialog_init (the_source_stream);
141
142   the_dictionary =
143     psppire_dict_new_from_dict (
144                                 dataset_dict (the_dataset)
145                                 );
146
147   bind_textdomain_codeset(PACKAGE, "UTF-8");
148
149   /* Create the model for the var_sheet */
150   var_store = psppire_var_store_new(the_dictionary);
151
152   data_store = psppire_data_store_new(the_dictionary);
153
154   create_icon_factory();
155
156   /* load the interface */
157   xml = glade_xml_new(PKGDATADIR "/psppire.glade", NULL, NULL);
158
159   if ( !xml ) return 1;
160
161   data_editor = get_widget_assert(xml, "data_editor");
162   gtk_window_set_icon_from_file(GTK_WINDOW(data_editor),
163                                 PKGDATADIR "/psppicon.png",0);
164
165   /* connect the signals in the interface */
166   glade_xml_signal_autoconnect(xml);
167
168   var_sheet  = GTK_SHEET(get_widget_assert(xml, "variable_sheet"));
169   data_sheet = GTK_SHEET(get_widget_assert(xml, "data_sheet"));
170
171   gtk_sheet_set_model(var_sheet, G_SHEET_MODEL(var_store));
172
173   gtk_sheet_set_model(data_sheet, G_SHEET_MODEL(data_store));
174
175   if (filename)
176     gtk_init_add((GtkFunction)load_system_file, filename);
177   else
178     gtk_init_add((GtkFunction)clear_file, 0);
179
180   var_data_selection_init();
181
182   {
183   GList *helps = glade_xml_get_widget_prefix(xml, "help_button_");
184
185   GList *i;
186   for ( i = g_list_first(helps); i ; i = g_list_next(i))
187       g_signal_connect(GTK_WIDGET(i->data), "clicked", give_help, 0);
188   }
189
190
191   /* start the event loop */
192   gtk_main();
193
194   destroy_source_stream (the_source_stream);
195   message_dialog_done();
196
197   settings_done();
198
199   return 0;
200 }
201
202
203 /* Parses the command line specified by ARGC and ARGV as received by
204    main().  Returns true if normal execution should proceed,
205    false if the command-line indicates that PSPP should exit. */
206 static bool
207 parse_command_line (int *argc, char ***argv, gchar **filename, GError **err)
208 {
209   static struct option long_options[] =
210     {
211       {"help", no_argument, NULL, 'h'},
212       {"version", no_argument, NULL, 'V'},
213       {0, 0, 0, 0},
214     };
215
216   int c;
217
218   for (;;)
219     {
220       c = getopt_long (*argc, *argv, "hV", long_options, NULL);
221       if (c == -1)
222         break;
223
224       switch (c)
225         {
226         case 'h':
227           g_print ("Usage: psppire {|--help|--version}\n");
228           return false;
229         case 'V':
230           g_print (version);
231           g_print ("\n");
232           g_print (legal);
233           return false;
234         default:
235           return false;
236         }
237     }
238
239   if ( optind < *argc) 
240     {
241       *filename = (*argv)[optind];
242     }
243
244   return true;
245 }
246
247
248
249 void 
250 create_icon_factory (void)
251 {
252   GtkIconFactory *factory = gtk_icon_factory_new();
253
254   GtkIconSet *icon_set;
255   
256   GdkPixbuf *pixbuf;
257
258   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/value-labels.png", 0);
259   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
260   g_object_unref (pixbuf);
261   gtk_icon_factory_add ( factory, "pspp-value-labels", icon_set);
262
263   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/weight-cases.png", 0);
264   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
265   g_object_unref (pixbuf);
266   gtk_icon_factory_add ( factory, "pspp-weight-cases", icon_set);
267
268   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/goto-variable.png", 0);
269   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
270   g_object_unref (pixbuf);
271   gtk_icon_factory_add ( factory, "pspp-goto-variable", icon_set);
272
273   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/insert-variable.png", 0);
274   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
275   g_object_unref (pixbuf);
276   gtk_icon_factory_add ( factory, "pspp-insert-variable", icon_set);
277
278   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/insert-case.png", 0);
279   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
280   g_object_unref (pixbuf);
281   gtk_icon_factory_add ( factory, "pspp-insert-case", icon_set);
282
283   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/split-file.png", 0);
284   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
285   g_object_unref (pixbuf);
286   gtk_icon_factory_add ( factory, "pspp-split-file", icon_set);
287
288   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/select-cases.png", 0);
289   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
290   g_object_unref (pixbuf);
291   gtk_icon_factory_add ( factory, "pspp-select-cases", icon_set);
292
293   gtk_icon_factory_add_default (factory);
294 }