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