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