Patch #5676 (syntax string source).
[pspp-builds.git] / src / ui / gui / helper.c
1 /* This file is a rubbish bin where stuff gets put when it doesn't seem to
2    belong anywhere else.
3 */
4 #include <config.h>
5
6 #include <glib.h>
7 #include "helper.h"
8 #include "message-dialog.h"
9 #include <data/data-in.h>
10 #include <data/data-out.h>
11 #include <data/dictionary.h>
12 #include <libpspp/message.h>
13
14 #include <libpspp/i18n.h>
15
16 #include <ctype.h>
17 #include <string.h>
18 #include <data/settings.h>
19
20 #include <language/command.h>
21 #include <data/procedure.h>
22 #include <language/lexer/lexer.h>
23
24
25 #include <gettext.h>
26
27 /* Formats a value according to FORMAT
28    The returned string must be freed when no longer required */
29 gchar *
30 value_to_text (union value v, struct fmt_spec format)
31 {
32   gchar *s = 0;
33
34   s = g_new (gchar, format.w + 1);
35   data_out (&v, &format, s);
36   s[format.w]='\0';
37   g_strchug (s);
38
39   return s;
40 }
41
42
43
44 gboolean
45 text_to_value (const gchar *text, union value *v,
46               struct fmt_spec format)
47 {
48   bool ok;
49
50   if ( format.type != FMT_A)
51     {
52       if ( ! text ) return FALSE;
53
54       {
55         const gchar *s = text;
56         while (*s)
57           {
58             if ( !isspace (*s))
59               break;
60             s++;
61           }
62
63         if ( !*s) return FALSE;
64       }
65     }
66
67   msg_disable ();
68   ok = data_in (ss_cstr (text), format.type, 0, 0,
69                 v, fmt_var_width (&format));
70   msg_enable ();
71
72   return ok;
73 }
74
75
76 GtkWidget *
77 get_widget_assert (GladeXML *xml, const gchar *name)
78 {
79   GtkWidget *w;
80   g_assert (xml);
81   g_assert (name);
82
83   w = glade_xml_get_widget (xml, name);
84
85   if ( !w )
86     g_warning ("Widget \"%s\" could not be found\n", name);
87
88   return w;
89 }
90
91 /* Converts a string in the pspp locale to utf-8 */
92 char *
93 pspp_locale_to_utf8 (const gchar *text, gssize len, GError **err)
94 {
95   return recode_string (CONV_PSPP_TO_UTF8, text, len);
96 }
97
98 #define _(msgid) gettext (msgid)
99 #define N_(msgid) msgid
100
101
102 static void
103 give_help (void)
104 {
105   static struct msg m = {
106     MSG_GENERAL,
107     MSG_NOTE,
108     {0, -1},
109     0,
110   };
111
112   if (! m.text)
113     m.text=g_strdup (_("Sorry. The help system hasn't yet been implemented."));
114
115   popup_message (&m);
116 }
117
118 void
119 connect_help (GladeXML *xml)
120 {
121   GList *helps = glade_xml_get_widget_prefix (xml, "help_button_");
122
123   GList *i;
124   for ( i = g_list_first (helps); i ; i = g_list_next (i))
125     g_signal_connect (GTK_WIDGET (i->data), "clicked", give_help, 0);
126 }
127
128
129
130 void
131 reference_manual (GtkMenuItem *menu, gpointer data)
132 {
133   GError *err = NULL;
134   if ( ! g_spawn_command_line_async ("yelp info:pspp", &err) )
135     {
136       msg (ME, _("Cannot open reference manual: %s"), err->message);
137     }
138   g_clear_error (&err);
139 }
140
141
142 extern struct dataset *the_dataset;
143 extern struct source_stream *the_source_stream;
144
145 void
146 execute_syntax (struct getl_interface *sss)
147 {
148   struct lexer *lexer;
149   g_return_if_fail (proc_has_source (the_dataset));
150
151   lexer = lex_create (the_source_stream);
152
153   getl_append_source (the_source_stream, sss);
154
155   for (;;)
156     {
157       const struct dictionary *dict = dataset_dict (the_dataset);
158
159       int result = cmd_parse (lexer, the_dataset,
160                               dict_get_var_cnt (dict) > 0 ?
161                               CMD_STATE_DATA : CMD_STATE_INITIAL);
162
163       if (result == CMD_EOF || result == CMD_FINISH)
164         break;
165     }
166
167   getl_abort_noninteractive (the_source_stream);
168
169   lex_destroy (lexer);
170 }
171
172