Added the Compute dialog box.
[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 <data/storage-stream.h>
33 #include <libpspp/message.h>
34
35 #include <libpspp/i18n.h>
36
37 #include <ctype.h>
38 #include <string.h>
39 #include <data/settings.h>
40
41 #include <language/command.h>
42 #include <data/procedure.h>
43 #include <language/lexer/lexer.h>
44 #include "psppire-data-store.h"
45
46
47 #include <gettext.h>
48
49 /* Formats a value according to FORMAT
50    The returned string must be freed when no longer required */
51 gchar *
52 value_to_text (union value v, struct fmt_spec format)
53 {
54   gchar *s = 0;
55
56   s = g_new (gchar, format.w + 1);
57   data_out (&v, &format, s);
58   s[format.w]='\0';
59   g_strchug (s);
60
61   return s;
62 }
63
64
65
66 gboolean
67 text_to_value (const gchar *text, union value *v,
68               struct fmt_spec format)
69 {
70   bool ok;
71
72   if ( format.type != FMT_A)
73     {
74       if ( ! text ) return FALSE;
75
76       {
77         const gchar *s = text;
78         while (*s)
79           {
80             if ( !isspace (*s))
81               break;
82             s++;
83           }
84
85         if ( !*s) return FALSE;
86       }
87     }
88
89   msg_disable ();
90   ok = data_in (ss_cstr (text), format.type, 0, 0,
91                 v, fmt_var_width (&format));
92   msg_enable ();
93
94   return ok;
95 }
96
97
98 GtkWidget *
99 get_widget_assert (GladeXML *xml, const gchar *name)
100 {
101   GtkWidget *w;
102   g_assert (xml);
103   g_assert (name);
104
105   w = glade_xml_get_widget (xml, name);
106
107   if ( !w )
108     g_warning ("Widget \"%s\" could not be found\n", name);
109
110   return w;
111 }
112
113 /* Converts a string in the pspp locale to utf-8.
114    The return value must be freed when no longer required*/
115 gchar *
116 pspp_locale_to_utf8 (const gchar *text, gssize len, GError **err)
117 {
118   return recode_string (CONV_PSPP_TO_UTF8, text, len);
119 }
120
121 #define _(msgid) gettext (msgid)
122 #define N_(msgid) msgid
123
124
125 static void
126 give_help (void)
127 {
128   static struct msg m = {
129     MSG_GENERAL,
130     MSG_NOTE,
131     {0, -1},
132     0,
133   };
134
135   if (! m.text)
136     m.text=g_strdup (_("Sorry. The help system hasn't yet been implemented."));
137
138   popup_message (&m);
139 }
140
141 void
142 connect_help (GladeXML *xml)
143 {
144   GList *helps = glade_xml_get_widget_prefix (xml, "help_button_");
145
146   GList *i;
147   for ( i = g_list_first (helps); i ; i = g_list_next (i))
148     g_signal_connect (GTK_WIDGET (i->data), "clicked", give_help, 0);
149 }
150
151
152
153 void
154 reference_manual (GtkMenuItem *menu, gpointer data)
155 {
156   GError *err = NULL;
157   if ( ! g_spawn_command_line_async ("yelp info:pspp", &err) )
158     {
159       msg (ME, _("Cannot open reference manual: %s"), err->message);
160     }
161   g_clear_error (&err);
162 }
163
164
165 extern struct dataset *the_dataset;
166 extern struct source_stream *the_source_stream;
167 extern PsppireDataStore *the_data_store;
168
169 gboolean
170 execute_syntax (struct getl_interface *sss)
171 {
172   struct lexer *lexer;
173
174   g_return_val_if_fail (proc_has_source (the_dataset), FALSE);
175
176   lexer = lex_create (the_source_stream);
177
178   getl_append_source (the_source_stream, sss);
179
180   for (;;)
181     {
182       int result = cmd_parse (lexer, the_dataset);
183
184       if (result == CMD_EOF || result == CMD_FINISH)
185         break;
186     }
187
188   getl_abort_noninteractive (the_source_stream);
189
190   lex_destroy (lexer);
191
192   /* The GUI must *always* have a data source, even if it's an empty one.
193      Therefore, we find that there is none, (for example NEW FILE was the last
194      item in the syntax) then we create a new one. */
195   if ( ! proc_has_source (the_dataset))
196     proc_set_source (the_dataset,
197                      storage_source_create (the_data_store->case_file->flexifile)
198                      );
199
200   /* GUI syntax needs this implicit EXECUTE command at the end of
201      every script.  Otherwise commands like GET could leave the GUI without
202      a casefile. */
203   return procedure (the_dataset, NULL, NULL);
204 }
205
206