Cleaned up GUI, by objectifying the data editor. Removed a number of global variables.
[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/settings.h>
32 #include <data/file-name.h>
33 #include <data/procedure.h>
34 #include <libpspp/getl.h>
35 #include <language/lexer/lexer.h>
36
37 #include <getopt.h>
38 #include <gtk/gtk.h>
39 #include <gtk/gtk.h>
40 #include <glade/glade.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 PsppireDataStore *the_data_store = 0;
51
52
53 static bool parse_command_line (int *argc, char ***argv, 
54                                 gchar **filename, GError **err);
55
56
57 #define _(msgid) gettext (msgid)
58 #define N_(msgid) msgid
59
60 static void
61 give_help(void)
62 {
63   static struct msg m = {
64     MSG_GENERAL, 
65     MSG_NOTE,
66     {0, -1},
67     0, 
68   };
69
70   if (! m.text) 
71     m.text=g_strdup(_("Sorry. The help system hasn't yet been implemented."));
72
73   popup_message(&m);
74 }
75
76 PsppireVarStore *the_var_store = 0;
77
78 void create_icon_factory (void);
79
80 struct source_stream *the_source_stream ;
81 struct lexer *the_lexer;
82 struct dataset * the_dataset = NULL;
83
84
85 int 
86 main(int argc, char *argv[]) 
87 {
88   struct casefile_factory *factory;
89   PsppireDict *dictionary = 0;
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   factory = flexifile_factory_create ();
129   the_source_stream = create_source_stream (
130                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
131                           );
132
133   the_lexer = lex_create (the_source_stream);
134
135   the_dataset = create_dataset (factory);
136
137   message_dialog_init (the_source_stream);
138
139   dictionary =
140     psppire_dict_new_from_dict (
141                                 dataset_dict (the_dataset)
142                                 );
143
144   bind_textdomain_codeset(PACKAGE, "UTF-8");
145
146   /* Create the model for the var_sheet */
147   the_var_store = psppire_var_store_new(dictionary);
148
149
150   the_data_store = psppire_data_store_new (dictionary);
151
152   create_icon_factory();
153
154 #if 0
155   /* load the interface */
156   data_editor_xml = glade_xml_new(PKGDATADIR "/data-editor.glade", NULL, NULL);
157
158   if ( !data_editor_xml ) return 1;
159
160   data_editor = get_widget_assert(data_editor_xml, "data_editor");
161
162   /* connect the signals in the interface */
163   glade_xml_signal_autoconnect(data_editor_xml);
164
165   var_sheet  = GTK_SHEET(get_widget_assert(data_editor_xml, "variable_sheet"));
166   data_sheet = GTK_SHEET(get_widget_assert(data_editor_xml, "data_sheet"));
167
168   gtk_sheet_set_model(var_sheet, G_SHEET_MODEL(the_var_store));
169
170   gtk_sheet_set_model(data_sheet, G_SHEET_MODEL(the_data_store));
171
172   var_data_selection_init();
173
174   {
175   GList *helps = glade_xml_get_widget_prefix(data_editor_xml, "help_button_");
176
177   GList *i;
178   for ( i = g_list_first(helps); i ; i = g_list_next(i))
179       g_signal_connect(GTK_WIDGET(i->data), "clicked", give_help, 0);
180   }
181
182 #endif
183
184   new_data_window (NULL, NULL);
185
186   /* start the event loop */
187   gtk_main();
188
189   destroy_source_stream (the_source_stream);
190   message_dialog_done();
191
192   settings_done();
193
194   return 0;
195 }
196
197
198 /* Parses the command line specified by ARGC and ARGV as received by
199    main().  Returns true if normal execution should proceed,
200    false if the command-line indicates that PSPP should exit. */
201 static bool
202 parse_command_line (int *argc, char ***argv, gchar **filename, GError **err)
203 {
204   static struct option long_options[] =
205     {
206       {"help", no_argument, NULL, 'h'},
207       {"version", no_argument, NULL, 'V'},
208       {0, 0, 0, 0},
209     };
210
211   int c;
212
213   for (;;)
214     {
215       c = getopt_long (*argc, *argv, "hV", long_options, NULL);
216       if (c == -1)
217         break;
218
219       switch (c)
220         {
221         case 'h':
222           g_print ("Usage: psppire {|--help|--version}\n");
223           return false;
224         case 'V':
225           g_print (version);
226           g_print ("\n");
227           g_print (legal);
228           return false;
229         default:
230           return false;
231         }
232     }
233
234   if ( optind < *argc) 
235     {
236       *filename = (*argv)[optind];
237     }
238
239   return true;
240 }
241
242
243
244 void 
245 create_icon_factory (void)
246 {
247   GtkIconFactory *factory = gtk_icon_factory_new();
248
249   GtkIconSet *icon_set;
250   
251   GdkPixbuf *pixbuf;
252
253   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/value-labels.png", 0);
254   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
255   g_object_unref (pixbuf);
256   gtk_icon_factory_add ( factory, "pspp-value-labels", icon_set);
257
258   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/weight-cases.png", 0);
259   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
260   g_object_unref (pixbuf);
261   gtk_icon_factory_add ( factory, "pspp-weight-cases", icon_set);
262
263   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/goto-variable.png", 0);
264   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
265   g_object_unref (pixbuf);
266   gtk_icon_factory_add ( factory, "pspp-goto-variable", icon_set);
267
268   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/insert-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-insert-variable", icon_set);
272
273   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/insert-case.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-case", icon_set);
277
278   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/split-file.png", 0);
279   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
280   g_object_unref (pixbuf);
281   gtk_icon_factory_add ( factory, "pspp-split-file", icon_set);
282
283   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/select-cases.png", 0);
284   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
285   g_object_unref (pixbuf);
286   gtk_icon_factory_add ( factory, "pspp-select-cases", icon_set);
287
288   gtk_icon_factory_add_default (factory);
289 }