e434f0c4a508a6c2d42223b3415489582db7aabc
[pspp-builds.git] / src / ui / gui / psppire.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2005, 2006, 2009  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <locale.h>
20 #include <assert.h>
21 #include <libintl.h>
22 #include <gsl/gsl_errno.h>
23
24 #include <argp.h>
25 #include <ui/command-line.h>
26 #include "relocatable.h"
27
28 #include "psppire-data-window.h"
29 #include "psppire.h"
30
31 #include <libpspp/getl.h>
32 #include <unistd.h>
33 #include <data/casereader.h>
34 #include <data/datasheet.h>
35 #include <data/file-handle-def.h>
36 #include <data/settings.h>
37 #include <data/file-name.h>
38 #include <data/procedure.h>
39 #include <libpspp/getl.h>
40 #include <language/lexer/lexer.h>
41 #include <libpspp/version.h>
42 #include <output/output.h>
43 #include <output/journal.h>
44 #include <language/syntax-string-source.h>
45
46 #include <gtk/gtk.h>
47 #include "psppire-dict.h"
48 #include "psppire-var-store.h"
49 #include "psppire-data-store.h"
50 #include "helper.h"
51 #include "message-dialog.h"
52 #include <ui/syntax-gen.h>
53
54
55 #include "psppire-output-window.h"
56
57 #include <data/sys-file-reader.h>
58 #include <data/por-file-reader.h>
59
60 #include <ui/source-init-opts.h>
61
62 PsppireDataStore *the_data_store = 0;
63 PsppireVarStore *the_var_store = 0;
64
65 static void create_icon_factory (void);
66
67 struct source_stream *the_source_stream ;
68 struct dataset * the_dataset = NULL;
69
70 static GtkWidget *the_data_window;
71
72 static void
73 replace_casereader (struct casereader *s)
74 {
75   psppire_data_store_set_reader (the_data_store, s);
76 }
77
78 #define _(msgid) gettext (msgid)
79 #define N_(msgid) msgid
80
81
82 const char * output_file_name (void);
83
84
85 void
86 initialize (struct command_line_processor *clp, int argc, char **argv)
87 {
88   PsppireDict *dictionary = 0;
89
90   /* gtk_init messes with the locale.
91      So unset the bits we want to control ourselves */
92   setlocale (LC_NUMERIC, "C");
93
94   bindtextdomain (PACKAGE, locale_dir);
95
96
97   gsl_set_error_handler_off ();
98   fn_init ();
99   outp_init ();
100   settings_init (&viewer_width, &viewer_length);
101   fh_init ();
102   the_source_stream =
103     create_source_stream (
104                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
105                           );
106
107   the_dataset = create_dataset ();
108
109
110   message_dialog_init (the_source_stream);
111
112   dictionary = psppire_dict_new_from_dict (dataset_dict (the_dataset));
113
114   bind_textdomain_codeset (PACKAGE, "UTF-8");
115
116   /* Create the model for the var_sheet */
117   the_var_store = psppire_var_store_new (dictionary);
118
119   the_data_store = psppire_data_store_new (dictionary);
120   replace_casereader (NULL);
121
122   create_icon_factory ();
123
124   {
125     const char *filename = output_file_name ();
126
127     struct string config_string;
128
129     ds_init_empty (&config_string);
130
131     ds_put_format (&config_string,
132                    "gui:ascii:screen:squeeze=on headers=off top-margin=0 "
133                    "bottom-margin=0 paginate=off length=auto width=auto "
134                    "emphasis=none "
135                    "output-file=\"%s\" append=yes", filename);
136
137     outp_configure_driver_line (ds_ss (&config_string));
138
139     unlink (filename);
140
141     ds_destroy (&config_string);
142   }
143
144   journal_enable ();
145   textdomain (PACKAGE);
146
147   the_data_window = psppire_data_window_new ();
148
149   command_line_processor_replace_aux (clp, &post_init_argp, the_source_stream);
150   command_line_processor_replace_aux (clp, &non_option_argp, the_source_stream);
151
152   command_line_processor_parse (clp, argc, argv);
153
154   execute_syntax (create_syntax_string_source (""));
155
156   gtk_widget_show (the_data_window);
157 }
158
159
160 void
161 de_initialize (void)
162 {
163   destroy_source_stream (the_source_stream);
164   message_dialog_done ();
165   settings_done ();
166   outp_done ();
167 }
168
169
170
171 struct icon_info
172 {
173   const char *file_name;
174   const gchar *id;
175 };
176
177
178 static const struct icon_info icons[] =
179   {
180     {PKGDATADIR "/value-labels.png",    "pspp-value-labels"},
181     {PKGDATADIR "/weight-cases.png",    "pspp-weight-cases"},
182     {PKGDATADIR "/goto-variable.png",   "pspp-goto-variable"},
183     {PKGDATADIR "/insert-variable.png", "pspp-insert-variable"},
184     {PKGDATADIR "/insert-case.png",     "pspp-insert-case"},
185     {PKGDATADIR "/split-file.png",      "pspp-split-file"},
186     {PKGDATADIR "/select-cases.png",    "pspp-select-cases"},
187     {PKGDATADIR "/recent-dialogs.png",  "pspp-recent-dialogs"},
188     {PKGDATADIR "/nominal.png",         "var-nominal"},
189     {PKGDATADIR "/ordinal.png",         "var-ordinal"},
190     {PKGDATADIR "/scale.png",           "var-scale"},
191     {PKGDATADIR "/string.png",          "var-string"},
192     {PKGDATADIR "/date-scale.png",      "var-date-scale"}
193   };
194
195 static void
196 create_icon_factory (void)
197 {
198   gint i;
199   GtkIconFactory *factory = gtk_icon_factory_new ();
200
201   for (i = 0 ; i < sizeof (icons) / sizeof(icons[0]); ++i)
202     {
203       GError *err = NULL;
204       GdkPixbuf *pixbuf =
205         gdk_pixbuf_new_from_file (relocate (icons[i].file_name), &err);
206
207       if ( pixbuf )
208         {
209           GtkIconSet *icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
210           g_object_unref (pixbuf);
211           gtk_icon_factory_add ( factory, icons[i].id, icon_set);
212         }
213       else
214         {
215           g_warning ("Cannot create icon: %s", err->message);
216           g_clear_error (&err);
217         }
218     }
219
220   {
221     /* Create our own "pspp-stock-reset" item, using the
222        GTK_STOCK_REFRESH icon set */
223
224     GtkStockItem items[] = {
225       {"pspp-stock-reset", N_("_Reset"), 0, 0, PACKAGE},
226       {"pspp-stock-select", N_("_Select"), 0, 0, PACKAGE}
227     };
228
229
230     gtk_stock_add (items, 2);
231     gtk_icon_factory_add (factory, "pspp-stock-reset",
232                           gtk_icon_factory_lookup_default (GTK_STOCK_REFRESH)
233                           );
234
235     gtk_icon_factory_add (factory, "pspp-stock-select",
236                           gtk_icon_factory_lookup_default (GTK_STOCK_INDEX)
237                           );
238   }
239
240   gtk_icon_factory_add_default (factory);
241 }
242
243 \f
244
245 static error_t
246 parse_non_options (int key, char *arg, struct argp_state *state)
247 {
248   struct source_stream *ss = state->input;
249
250   if ( NULL == ss )
251     return 0;
252
253   switch (key)
254     {
255     case ARGP_KEY_ARG:
256       {
257         psppire_data_window_load_file (PSPPIRE_DATA_WINDOW (the_data_window),
258                                        arg);
259         break;
260       }
261     default:
262       return ARGP_ERR_UNKNOWN;
263     }
264   return 0;
265 }
266
267
268 const struct argp non_option_argp = {NULL, parse_non_options, 0, 0, 0, 0, 0};
269
270
271 const char *
272 output_file_name (void)
273 {
274   const char *dir = default_output_path ();
275   static char *filename = NULL;
276
277   if ( NULL == filename )
278     filename = xasprintf ("%s%s", dir, OUTPUT_FILE_NAME);
279
280
281   return filename;
282 }