7344b9816146f867b7804eee824ebe169601fe61
[pspp] / src / ui / gui / psppire.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2004, 2005, 2006, 2009, 2010, 2011, 2012, 2013, 2014,
3    2016  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 3 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, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20
21 #include <gsl/gsl_errno.h>
22 #include <gtk/gtk.h>
23 #include <libintl.h>
24 #include <unistd.h>
25
26 #include "data/any-reader.h"
27 #include "data/casereader.h"
28 #include "data/dataset.h"
29 #include "data/datasheet.h"
30 #include "data/file-handle-def.h"
31 #include "data/session.h"
32 #include "data/settings.h"
33
34 #include "language/lexer/lexer.h"
35 #include "libpspp/i18n.h"
36 #include "libpspp/message.h"
37 #include "libpspp/version.h"
38
39 #include "output/driver.h"
40 #include "output/journal.h"
41 #include "output/output-item.h"
42 #include "output/spv/spv.h"
43
44 #include "ui/gui/dict-display.h"
45 #include "ui/gui/executor.h"
46 #include "ui/gui/psppire-data-store.h"
47 #include "ui/gui/psppire-data-window.h"
48 #include "ui/gui/psppire-dict.h"
49 #include "ui/gui/psppire.h"
50 #include "ui/gui/psppire-output-window.h"
51 #include "ui/gui/psppire-syntax-window.h"
52 #include "ui/gui/psppire-selector.h"
53 #include "ui/gui/psppire-var-view.h"
54 #include "ui/gui/psppire-means-layer.h"
55 #include "ui/gui/psppire-window-register.h"
56 #include "ui/gui/widgets.h"
57 #include "ui/source-init-opts.h"
58 #include "ui/syntax-gen.h"
59
60
61 #include "gl/configmake.h"
62 #include "gl/xalloc.h"
63 #include "gl/relocatable.h"
64
65 void create_icon_factory (void);
66
67 #define _(msgid) gettext (msgid)
68 #define N_(msgid) msgid
69
70 void
71 register_selection_functions (void)
72 {
73   psppire_selector_set_default_selection_func (GTK_TYPE_ENTRY, insert_source_row_into_entry);
74   psppire_selector_set_default_selection_func (PSPPIRE_VAR_VIEW_TYPE, insert_source_row_into_tree_view);
75   psppire_selector_set_default_selection_func (GTK_TYPE_TREE_VIEW, insert_source_row_into_tree_view);
76   psppire_selector_set_default_selection_func (PSPPIRE_TYPE_MEANS_LAYER, insert_source_row_into_layers);
77 }
78
79 bool
80 initialize (const struct init_source *is)
81 {
82   switch (is->state)
83     {
84     case 0:
85       i18n_init ();
86       break;
87     case 1:
88       preregister_widgets ();
89       break;
90     case 2:
91       gsl_set_error_handler_off ();
92       break;
93     case 3:
94       output_engine_push ();
95       break;
96     case 4:
97       settings_init ();
98       break;
99     case 5:
100       fh_init ();
101       break;
102     case 6:
103       psppire_set_lexer (NULL);
104       break;
105     case 7:
106       bind_textdomain_codeset (PACKAGE, "UTF-8");
107       break;
108     case 8:
109       if (! gtk_parse_args (is->argc, is->argv))
110         {
111           perror ("Error parsing arguments");
112           exit (1);
113         }
114       break;
115     case 9:
116       journal_init ();
117       break;
118     case 10:
119       textdomain (PACKAGE);
120       break;
121     default:
122       return TRUE;
123       break;
124     }
125   return FALSE;
126 }
127
128
129 void
130 de_initialize (void)
131 {
132   settings_done ();
133   output_engine_pop ();
134   i18n_done ();
135 }
136
137 void
138 psppire_quit (GApplication *app)
139 {
140   g_application_quit (app);
141 }
142
143 \f
144 static void
145 handle_msg (const struct msg *m_, void *lexer_)
146 {
147   struct lexer *lexer = lexer_;
148   struct msg m = {
149     .category = m_->category,
150     .severity = m_->severity,
151     .location = (m_->location ? m_->location
152                  : lexer ? lex_get_location (lexer, 0, 0)
153                  : NULL),
154     .command_name = output_get_uppercase_command_name (),
155     .text = m_->text,
156   };
157
158   output_item_submit (message_item_create (&m));
159
160   free (m.command_name);
161   if (m.location != m_->location)
162     msg_location_destroy (m.location);
163 }
164
165 void
166 psppire_set_lexer (struct lexer *lexer)
167 {
168   msg_set_handler (handle_msg, lexer);
169 }
170
171
172 GtkWindow *
173 psppire_preload_file (const gchar *file, GtkWindow *victim)
174 {
175   const gchar *local_encoding = "UTF-8";
176
177   struct file_handle *fh = fh_create_file (NULL,
178                                            file,
179                                            local_encoding,
180                                            fh_default_properties ());
181   const char *filename = fh_get_file_name (fh);
182
183   int retval = any_reader_detect (fh, NULL);
184
185   GtkWindow *w = NULL;
186   /* Check to see if the file is a .sav or a .por file.  If not
187      assume that it is a syntax file */
188   if (retval == 1)
189     w = open_data_window (PSPPIRE_WINDOW (victim), filename, NULL, NULL);
190   else if (retval == 0)
191     {
192       char *error = spv_detect (filename);
193       if (!error)
194         read_spv_file (filename);
195       else
196         {
197           free (error);
198           create_data_window ();
199           open_syntax_window (filename, NULL);
200         }
201     }
202
203   fh_unref (fh);
204   return w;
205 }