Fixed procedure execution in GUI.
[pspp-builds.git] / src / ui / gui / helper.c
1 /*
2     PSPPIRE --- A Graphical User Interface for PSPP
3     Copyright (C) 2007  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 2 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, write to the Free Software
17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18     02110-1301, USA. */
19
20
21 /* This file is a rubbish bin where stuff gets put when it doesn't seem to
22    belong anywhere else.
23 */
24 #include <config.h>
25
26 #include <glib.h>
27 #include "helper.h"
28 #include "message-dialog.h"
29 #include <data/data-in.h>
30 #include <data/data-out.h>
31 #include <data/dictionary.h>
32 #include <libpspp/message.h>
33
34 #include <libpspp/i18n.h>
35
36 #include <ctype.h>
37 #include <string.h>
38 #include <data/settings.h>
39
40 #include <language/command.h>
41 #include <data/procedure.h>
42 #include <language/lexer/lexer.h>
43 #include "psppire-data-store.h"
44
45
46 #include <gettext.h>
47
48 /* Formats a value according to FORMAT
49    The returned string must be freed when no longer required */
50 gchar *
51 value_to_text (union value v, struct fmt_spec format)
52 {
53   gchar *s = 0;
54
55   s = g_new (gchar, format.w + 1);
56   data_out (&v, &format, s);
57   s[format.w]='\0';
58   g_strchug (s);
59
60   return s;
61 }
62
63
64
65 gboolean
66 text_to_value (const gchar *text, union value *v,
67               struct fmt_spec format)
68 {
69   bool ok;
70
71   if ( format.type != FMT_A)
72     {
73       if ( ! text ) return FALSE;
74
75       {
76         const gchar *s = text;
77         while (*s)
78           {
79             if ( !isspace (*s))
80               break;
81             s++;
82           }
83
84         if ( !*s) return FALSE;
85       }
86     }
87
88   msg_disable ();
89   ok = data_in (ss_cstr (text), format.type, 0, 0,
90                 v, fmt_var_width (&format));
91   msg_enable ();
92
93   return ok;
94 }
95
96
97 GtkWidget *
98 get_widget_assert (GladeXML *xml, const gchar *name)
99 {
100   GtkWidget *w;
101   g_assert (xml);
102   g_assert (name);
103
104   w = glade_xml_get_widget (xml, name);
105
106   if ( !w )
107     g_warning ("Widget \"%s\" could not be found\n", name);
108
109   return w;
110 }
111
112 /* Converts a string in the pspp locale to utf-8.
113    The return value must be freed when no longer required*/
114 gchar *
115 pspp_locale_to_utf8 (const gchar *text, gssize len, GError **err)
116 {
117   return recode_string (CONV_PSPP_TO_UTF8, text, len);
118 }
119
120 #define _(msgid) gettext (msgid)
121 #define N_(msgid) msgid
122
123
124 static void
125 give_help (void)
126 {
127   static struct msg m = {
128     MSG_GENERAL,
129     MSG_NOTE,
130     {0, -1},
131     0,
132   };
133
134   if (! m.text)
135     m.text=g_strdup (_("Sorry. The help system hasn't yet been implemented."));
136
137   popup_message (&m);
138 }
139
140 void
141 connect_help (GladeXML *xml)
142 {
143   GList *helps = glade_xml_get_widget_prefix (xml, "help_button_");
144
145   GList *i;
146   for ( i = g_list_first (helps); i ; i = g_list_next (i))
147     g_signal_connect (GTK_WIDGET (i->data), "clicked", give_help, 0);
148 }
149
150
151
152 void
153 reference_manual (GtkMenuItem *menu, gpointer data)
154 {
155   GError *err = NULL;
156   if ( ! g_spawn_command_line_async ("yelp info:pspp", &err) )
157     {
158       msg (ME, _("Cannot open reference manual: %s"), err->message);
159     }
160   g_clear_error (&err);
161 }
162
163
164 extern struct dataset *the_dataset;
165 extern struct source_stream *the_source_stream;
166 extern PsppireDataStore *the_data_store;
167
168 gboolean
169 execute_syntax (struct getl_interface *sss)
170 {
171   gboolean status;
172   struct lexer *lexer;
173
174   struct casereader *reader = psppire_data_store_get_reader (the_data_store);
175
176   proc_set_active_file_data (the_dataset, reader);
177
178   g_return_val_if_fail (proc_has_active_file (the_dataset), FALSE);
179
180   lexer = lex_create (the_source_stream);
181
182   getl_append_source (the_source_stream, sss);
183
184   for (;;)
185     {
186       int result = cmd_parse (lexer, the_dataset);
187
188       if (result == CMD_EOF || result == CMD_FINISH)
189         break;
190     }
191
192   getl_abort_noninteractive (the_source_stream);
193
194   lex_destroy (lexer);
195
196   /* GUI syntax needs this implicit EXECUTE command at the end of
197      every script.  Otherwise commands like GET could leave the GUI
198      without a datasheet. */
199   status = proc_execute (the_dataset);
200
201   psppire_dict_replace_dictionary (the_data_store->dict,
202                                    dataset_dict (the_dataset));
203
204   {
205     PsppireCaseFile *pcf = psppire_case_file_new (dataset_source (the_dataset));
206
207     psppire_data_store_set_case_file (the_data_store, pcf);
208   }
209
210   return status;
211 }
212
213