bd6e2198f4efc7ca276dc2b04cc79645e4338534
[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    The return value must be freed when no longer required*/
95 gchar *
96 pspp_locale_to_utf8 (const gchar *text, gssize len, GError **err)
97 {
98   return recode_string (CONV_PSPP_TO_UTF8, text, len);
99 }
100
101 #define _(msgid) gettext (msgid)
102 #define N_(msgid) msgid
103
104
105 static void
106 give_help (void)
107 {
108   static struct msg m = {
109     MSG_GENERAL,
110     MSG_NOTE,
111     {0, -1},
112     0,
113   };
114
115   if (! m.text)
116     m.text=g_strdup (_("Sorry. The help system hasn't yet been implemented."));
117
118   popup_message (&m);
119 }
120
121 void
122 connect_help (GladeXML *xml)
123 {
124   GList *helps = glade_xml_get_widget_prefix (xml, "help_button_");
125
126   GList *i;
127   for ( i = g_list_first (helps); i ; i = g_list_next (i))
128     g_signal_connect (GTK_WIDGET (i->data), "clicked", give_help, 0);
129 }
130
131
132
133 void
134 reference_manual (GtkMenuItem *menu, gpointer data)
135 {
136   GError *err = NULL;
137   if ( ! g_spawn_command_line_async ("yelp info:pspp", &err) )
138     {
139       msg (ME, _("Cannot open reference manual: %s"), err->message);
140     }
141   g_clear_error (&err);
142 }
143
144
145 extern struct dataset *the_dataset;
146 extern struct source_stream *the_source_stream;
147 extern PsppireDataStore *the_data_store;
148
149 gboolean
150 execute_syntax (struct getl_interface *sss)
151 {
152   struct lexer *lexer;
153
154   g_return_val_if_fail (proc_has_source (the_dataset), FALSE);
155
156   lexer = lex_create (the_source_stream);
157
158   getl_append_source (the_source_stream, sss);
159
160   for (;;)
161     {
162       int result = cmd_parse (lexer, the_dataset);
163
164       if (result == CMD_EOF || result == CMD_FINISH)
165         break;
166     }
167
168   getl_abort_noninteractive (the_source_stream);
169
170   lex_destroy (lexer);
171
172   /* The GUI must *always* have a data source, even if it's an empty one.
173      Therefore, we find that there is none, (for example NEW FILE was the last
174      item in the syntax) then we create a new one. */
175   if ( ! proc_has_source (the_dataset))
176     proc_set_source (the_dataset,
177                      storage_source_create (the_data_store->case_file->flexifile)
178                      );
179
180   /* GUI syntax needs this implicit EXECUTE command at the end of
181      every script.  Otherwise commands like GET could leave the GUI without
182      a casefile. */
183   return procedure (the_dataset, NULL, NULL);
184 }
185
186