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