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