1f903beda993c75616491e08f11f7100408ac2aa
[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, 2016  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
20 #include <assert.h>
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/message-item.h"
42
43 #include "ui/gui/dict-display.h"
44 #include "ui/gui/executor.h"
45 #include "ui/gui/psppire-data-store.h"
46 #include "ui/gui/psppire-data-window.h"
47 #include "ui/gui/psppire-dict.h"
48 #include "ui/gui/psppire.h"
49 #include "ui/gui/psppire-output-window.h"
50 #include "ui/gui/psppire-syntax-window.h"
51 #include "ui/gui/psppire-selector.h"
52 #include "ui/gui/psppire-var-view.h"
53 #include "ui/gui/psppire-means-layer.h"
54 #include "ui/gui/psppire-window-register.h"
55 #include "ui/gui/widgets.h"
56 #include "ui/source-init-opts.h"
57 #include "ui/syntax-gen.h"
58
59
60 #include "gl/configmake.h"
61 #include "gl/xalloc.h"
62 #include "gl/relocatable.h"
63
64 void create_icon_factory (void);
65
66 #define _(msgid) gettext (msgid)
67 #define N_(msgid) msgid
68
69 void
70 register_selection_functions (void)
71 {
72   psppire_selector_set_default_selection_func (GTK_TYPE_ENTRY, insert_source_row_into_entry);
73   psppire_selector_set_default_selection_func (PSPPIRE_VAR_VIEW_TYPE, insert_source_row_into_tree_view);
74   psppire_selector_set_default_selection_func (GTK_TYPE_TREE_VIEW, insert_source_row_into_tree_view);
75   psppire_selector_set_default_selection_func (PSPPIRE_TYPE_MEANS_LAYER, insert_source_row_into_layers);
76 }
77
78 bool
79 initialize (const struct init_source *is)
80 {
81   switch (is->state)
82     {
83     case 0:
84       i18n_init ();
85       break;
86     case 1:
87       preregister_widgets ();
88       break;
89     case 2:
90       gsl_set_error_handler_off ();
91       break;
92     case 3:
93       output_engine_push ();
94       break;
95     case 4:
96       settings_init ();
97       break;
98     case 5:
99       fh_init ();
100       break;
101     case 6:
102       psppire_set_lexer (NULL);
103       break;
104     case 7:
105       bind_textdomain_codeset (PACKAGE, "UTF-8");
106       break;
107     case 8:
108       if ( ! gtk_parse_args (is->argc, is->argv) )
109         {
110           perror ("Error parsing arguments");
111           exit (1);
112         }
113       break;
114     case 9:
115       journal_init ();
116       break;
117     case 10:
118       textdomain (PACKAGE);
119       break;
120     default:
121       return TRUE;
122       break;
123     }
124   return FALSE;
125 }
126
127
128 void
129 de_initialize (void)
130 {
131   settings_done ();
132   output_engine_pop ();
133   i18n_done ();
134 }
135
136 void
137 psppire_quit (GApplication *app)
138 {
139   g_application_quit (app);
140 }
141
142 struct icon_size
143 {
144   int resolution;  /* The dimension of the images which will be used */
145   size_t n_sizes;  /* The number of items in the array below. */
146   const GtkIconSize *usage; /* An array determining for what the icon set is used */
147 };
148 \f
149 static void
150 handle_msg (const struct msg *m_, void *lexer_)
151 {
152   struct lexer *lexer = lexer_;
153   struct msg m = *m_;
154
155   if (lexer != NULL && m.file_name == NULL)
156     {
157       m.file_name = CONST_CAST (char *, lex_get_file_name (lexer));
158       m.first_line = lex_get_first_line_number (lexer, 0);
159       m.last_line = lex_get_last_line_number (lexer, 0);
160       m.first_column = lex_get_first_column (lexer, 0);
161       m.last_column = lex_get_last_column (lexer, 0);
162     }
163   m.command_name = CONST_CAST (char *, output_get_command_name ());
164
165   message_item_submit (message_item_create (&m));
166 }
167
168 void
169 psppire_set_lexer (struct lexer *lexer)
170 {
171   msg_set_handler (handle_msg, lexer);
172 }
173
174
175 GtkWindow *
176 psppire_preload_file (const gchar *file)
177 {
178   const gchar *local_encoding = "UTF-8";
179
180   struct file_handle *fh = fh_create_file (NULL,
181                                            file,
182                                            local_encoding,
183                                            fh_default_properties ());
184   const char *filename = fh_get_file_name (fh);
185
186   int retval = any_reader_detect (fh, NULL);
187
188   GtkWindow *w = NULL;
189   /* Check to see if the file is a .sav or a .por file.  If not
190      assume that it is a syntax file */
191   if (retval == 1)
192     w = open_data_window (NULL, filename, NULL, NULL);
193   else if (retval == 0)
194     {
195       create_data_window ();
196       w = open_syntax_window (filename, NULL);
197     }
198
199   fh_unref (fh);
200   return w;
201 }