3b4181ac71e44849145a8947651811974ebc85e3
[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    Written by John Darrington
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA. */
20
21 #include <config.h>
22
23 #include <assert.h>
24 #include <libintl.h>
25
26 #include <libpspp/version.h>
27 #include <libpspp/copyleft.h>
28 #include <data/format.h>
29 #include <data/settings.h>
30 #include <data/file-name.h>
31 #include <libpspp/getl.h>
32
33 #include <getopt.h>
34 #include <gtk/gtk.h>
35 #include <gtk/gtk.h>
36 #include <glade/glade.h>
37 #include "menu-actions.h"
38 #include "psppire-dict.h"
39 #include "psppire-var-store.h"
40 #include "psppire-data-store.h"
41 #include "helper.h"
42 #include "data-sheet.h"
43 #include "var-sheet.h"
44 #include "message-dialog.h"
45
46 GladeXML *xml;
47
48
49 PsppireDict *the_dictionary = 0;
50
51 PsppireDataStore *data_store = 0;
52
53
54 static bool parse_command_line (int *argc, char ***argv, 
55                                 gchar **filename, GError **err);
56
57
58 #define _(msgid) gettext (msgid)
59 #define N_(msgid) msgid
60
61 static void
62 give_help(void)
63 {
64   static struct msg m = {
65     MSG_GENERAL, 
66     MSG_NOTE,
67     {0, -1},
68     0, 
69   };
70
71   if (! m.text) 
72     m.text=g_strdup(_("Sorry. The help system hasn't yet been implemented."));
73
74   popup_message(&m);
75 }
76
77 PsppireVarStore *var_store = 0;
78
79 void create_icon_factory (void);
80
81 static struct source_stream *the_source_stream ;
82
83 int 
84 main(int argc, char *argv[]) 
85 {
86
87   GtkWidget *data_editor ;
88   GtkSheet *var_sheet ; 
89   GtkSheet *data_sheet ;
90
91   gchar *filename=0;
92   GError *err = 0;
93   gchar *vers;
94
95   gtk_init(&argc, &argv);
96   if ( (vers = gtk_check_version(GTK_MAJOR_VERSION, 
97                                  GTK_MINOR_VERSION, 
98                                  GTK_MICRO_VERSION)) )
99     {
100       g_critical(vers);
101     }
102         
103
104   /* gtk_init messes with the locale. 
105      So unset the bits we want to control ourselves */
106   setlocale (LC_NUMERIC, "C");
107
108   bindtextdomain (PACKAGE, locale_dir);
109
110   textdomain (PACKAGE);
111
112   if ( ! parse_command_line(&argc, &argv, &filename, &err) ) 
113     {
114       g_clear_error(&err);
115       return 0;
116     }
117
118   glade_init();
119
120   fmt_init();
121   settings_init();
122   the_source_stream = create_source_stream (
123                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
124                           );
125
126   message_dialog_init (the_source_stream);
127
128   the_dictionary = psppire_dict_new();
129
130   bind_textdomain_codeset(PACKAGE, "UTF-8");
131
132   /* Create the model for the var_sheet */
133   var_store = psppire_var_store_new(the_dictionary);
134
135   data_store = psppire_data_store_new(the_dictionary);
136
137   create_icon_factory();
138
139   /* load the interface */
140   xml = glade_xml_new(PKGDATADIR "/psppire.glade", NULL, NULL);
141
142   if ( !xml ) return 1;
143
144   data_editor = get_widget_assert(xml, "data_editor");
145   gtk_window_set_icon_from_file(GTK_WINDOW(data_editor),
146                                 PKGDATADIR "/psppicon.png",0);
147
148   /* connect the signals in the interface */
149   glade_xml_signal_autoconnect(xml);
150
151   var_sheet  = GTK_SHEET(get_widget_assert(xml, "variable_sheet"));
152   data_sheet = GTK_SHEET(get_widget_assert(xml, "data_sheet"));
153
154   gtk_sheet_set_model(var_sheet, G_SHEET_MODEL(var_store));
155
156   gtk_sheet_set_model(data_sheet, G_SHEET_MODEL(data_store));
157
158   if (filename)
159     gtk_init_add((GtkFunction)load_system_file, filename);
160   else
161     gtk_init_add((GtkFunction)clear_file, 0);
162
163   var_data_selection_init();
164
165   {
166   GList *helps = glade_xml_get_widget_prefix(xml, "help_button_");
167
168   GList *i;
169   for ( i = g_list_first(helps); i ; i = g_list_next(i))
170       g_signal_connect(GTK_WIDGET(i->data), "clicked", give_help, 0);
171   }
172
173
174   /* start the event loop */
175   gtk_main();
176
177   destroy_source_stream (the_source_stream);
178   message_dialog_done();
179
180   settings_done();
181
182   return 0;
183 }
184
185
186 /* Parses the command line specified by ARGC and ARGV as received by
187    main().  Returns true if normal execution should proceed,
188    false if the command-line indicates that PSPP should exit. */
189 static bool
190 parse_command_line (int *argc, char ***argv, gchar **filename, GError **err)
191 {
192   static struct option long_options[] =
193     {
194       {"help", no_argument, NULL, 'h'},
195       {"version", no_argument, NULL, 'V'},
196       {0, 0, 0, 0},
197     };
198
199   int c;
200
201   for (;;)
202     {
203       c = getopt_long (*argc, *argv, "hV", long_options, NULL);
204       if (c == -1)
205         break;
206
207       switch (c)
208         {
209         case 'h':
210           g_print ("Usage: psppire {|--help|--version}\n");
211           return false;
212         case 'V':
213           g_print (version);
214           g_print ("\n");
215           g_print (legal);
216           return false;
217         default:
218           return false;
219         }
220     }
221
222   if ( optind < *argc) 
223     {
224       *filename = (*argv)[optind];
225     }
226
227   return true;
228 }
229
230
231
232 void 
233 create_icon_factory (void)
234 {
235   GtkIconFactory *factory = gtk_icon_factory_new();
236
237   GtkIconSet *icon_set;
238   
239   GdkPixbuf *pixbuf;
240
241   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/value-labels.png", 0);
242   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
243   g_object_unref (pixbuf);
244   gtk_icon_factory_add ( factory, "pspp-value-labels", icon_set);
245
246   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/weight-cases.png", 0);
247   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
248   g_object_unref (pixbuf);
249   gtk_icon_factory_add ( factory, "pspp-weight-cases", icon_set);
250
251   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/goto-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-goto-variable", icon_set);
255
256   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/insert-variable.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-variable", icon_set);
260
261   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/insert-case.png", 0);
262   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
263   g_object_unref (pixbuf);
264   gtk_icon_factory_add ( factory, "pspp-insert-case", icon_set);
265
266   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/split-file.png", 0);
267   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
268   g_object_unref (pixbuf);
269   gtk_icon_factory_add ( factory, "pspp-split-file", icon_set);
270
271   pixbuf = gdk_pixbuf_new_from_file (PKGDATADIR "/select-cases.png", 0);
272   icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
273   g_object_unref (pixbuf);
274   gtk_icon_factory_add ( factory, "pspp-select-cases", icon_set);
275
276   gtk_icon_factory_add_default (factory);
277 }