John's original code for patch #6210.
[pspp-builds.git] / src / ui / gui / psppire.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2005, 2006  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 <assert.h>
20 #include <libintl.h>
21
22 #include "relocatable.h"
23
24 #include "data-editor.h"
25 #include "psppire.h"
26
27 #include <unistd.h>
28 #include <data/casereader.h>
29 #include <data/datasheet.h>
30 #include <data/file-handle-def.h>
31 #include <data/format.h>
32 #include <data/settings.h>
33 #include <data/file-name.h>
34 #include <data/procedure.h>
35 #include <libpspp/getl.h>
36 #include <language/lexer/lexer.h>
37 #include <libpspp/version.h>
38 #include <output/output.h>
39 #include <output/journal.h>
40
41 #include <gtk/gtk.h>
42 #include <glade/glade.h>
43 #include "psppire-dict.h"
44 #include "psppire-var-store.h"
45 #include "psppire-data-store.h"
46 #include "helper.h"
47 #include "data-sheet.h"
48 #include "var-sheet.h"
49 #include "message-dialog.h"
50
51 #include "output-viewer.h"
52
53 PsppireDataStore *the_data_store = 0;
54 PsppireVarStore *the_var_store = 0;
55
56 static void create_icon_factory (void);
57
58 struct source_stream *the_source_stream ;
59 struct dataset * the_dataset = NULL;
60
61
62 static void
63 replace_casereader (struct casereader *s)
64 {
65   PsppireCaseFile *pcf = psppire_case_file_new (s);
66
67   psppire_data_store_set_case_file (the_data_store, pcf);
68 }
69
70 void
71 initialize (void)
72 {
73   PsppireDict *dictionary = 0;
74
75   /* gtk_init messes with the locale.
76      So unset the bits we want to control ourselves */
77   setlocale (LC_NUMERIC, "C");
78
79   bindtextdomain (PACKAGE, locale_dir);
80
81   textdomain (PACKAGE);
82
83   glade_init ();
84
85   fmt_init ();
86   fn_init ();
87   outp_init ();
88   settings_init (&viewer_width, &viewer_length);
89   fh_init ();
90   the_source_stream =
91     create_source_stream (
92                           fn_getenv_default ("STAT_INCLUDE_PATH", include_path)
93                           );
94
95   the_dataset = create_dataset ();
96
97
98   message_dialog_init (the_source_stream);
99
100   dictionary = psppire_dict_new_from_dict (dataset_dict (the_dataset));
101
102   bind_textdomain_codeset (PACKAGE, "UTF-8");
103
104   /* Create the model for the var_sheet */
105   the_var_store = psppire_var_store_new (dictionary);
106
107   the_data_store = psppire_data_store_new (dictionary);
108   replace_casereader (NULL);
109
110   create_icon_factory ();
111
112   outp_configure_driver_line (
113     ss_cstr ("gui:ascii:screen:squeeze=on headers=off top-margin=0 "
114              "bottom-margin=0 paginate=off length=50 "
115              "width=" OUTPUT_LINE_WIDTH_str " emphasis=none "
116              "output-file=\"" OUTPUT_FILE_NAME "\" append=yes"));
117
118   unlink (OUTPUT_FILE_NAME);
119
120   journal_enable ();
121
122   new_data_window (NULL, NULL);
123 }
124
125
126 void
127 de_initialize (void)
128 {
129   destroy_source_stream (the_source_stream);
130   message_dialog_done ();
131   settings_done ();
132   outp_done ();
133 }
134
135
136 struct icon_info
137 {
138   const char *file_name;
139   const gchar *id;
140 };
141
142
143 static const struct icon_info icons[] =
144   {
145     {PKGDATADIR "/value-labels.png",    "pspp-value-labels"},
146     {PKGDATADIR "/weight-cases.png",    "pspp-weight-cases"},
147     {PKGDATADIR "/goto-variable.png",   "pspp-goto-variable"},
148     {PKGDATADIR "/insert-variable.png", "pspp-insert-variable"},
149     {PKGDATADIR "/insert-case.png",     "pspp-insert-case"},
150     {PKGDATADIR "/split-file.png",      "pspp-split-file"},
151     {PKGDATADIR "/select-cases.png",    "pspp-select-cases"},
152     {PKGDATADIR "/recent-dialogs.png",  "pspp-recent-dialogs"},
153     {PKGDATADIR "/nominal.png",         "var-nominal"},
154     {PKGDATADIR "/ordinal.png",         "var-ordinal"},
155     {PKGDATADIR "/scale.png",           "var-scale"},
156     {PKGDATADIR "/string.png",          "var-string"},
157     {PKGDATADIR "/date-scale.png",      "var-date-scale"}
158   };
159
160 static void
161 create_icon_factory (void)
162 {
163   gint i;
164   GtkIconFactory *factory = gtk_icon_factory_new ();
165
166   for (i = 0 ; i < sizeof (icons) / sizeof(icons[0]); ++i)
167     {
168       GError *err = NULL;
169       GdkPixbuf *pixbuf =
170         gdk_pixbuf_new_from_file (relocate (icons[i].file_name), &err);
171
172       if ( pixbuf )
173         {
174           GtkIconSet *icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
175           g_object_unref (pixbuf);
176           gtk_icon_factory_add ( factory, icons[i].id, icon_set);
177         }
178       else
179         {
180           g_warning ("Cannot create icon: %s", err->message);
181           g_clear_error (&err);
182         }
183     }
184
185   gtk_icon_factory_add_default (factory);
186 }
187