Added the ability to resize string variables from the GUI. Thanks to
[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-object.h>
24
25 #include <glib.h>
26 #include "helper.h"
27 #include "message-dialog.h"
28 #include <data/data-in.h>
29 #include <data/data-out.h>
30 #include <data/dictionary.h>
31 #include <libpspp/message.h>
32
33 #include <libpspp/i18n.h>
34
35 #include <ctype.h>
36 #include <string.h>
37 #include <data/settings.h>
38
39 #include <language/command.h>
40 #include <data/procedure.h>
41 #include <language/lexer/lexer.h>
42 #include "psppire-data-store.h"
43
44
45 #include <gettext.h>
46
47 /* Formats a value according to FORMAT
48    The returned string must be freed when no longer required */
49 gchar *
50 value_to_text (union value v, struct fmt_spec format)
51 {
52   gchar *s = 0;
53
54   s = g_new (gchar, format.w + 1);
55   data_out (&v, &format, s);
56   s[format.w]='\0';
57   g_strchug (s);
58
59   return s;
60 }
61
62
63
64 gboolean
65 text_to_value (const gchar *text, union value *v,
66               struct fmt_spec format)
67 {
68   bool ok;
69
70   if ( format.type != FMT_A)
71     {
72       if ( ! text ) return FALSE;
73
74       {
75         const gchar *s = text;
76         while (*s)
77           {
78             if ( !isspace (*s))
79               break;
80             s++;
81           }
82
83         if ( !*s) return FALSE;
84       }
85     }
86
87   msg_disable ();
88   ok = data_in (ss_cstr (text), format.type, 0, 0,
89                 v, fmt_var_width (&format));
90   msg_enable ();
91
92   return ok;
93 }
94
95
96 GtkWidget *
97 get_widget_assert (GladeXML *xml, const gchar *name)
98 {
99   GtkWidget *w;
100   g_assert (xml);
101   g_assert (name);
102
103   w = glade_xml_get_widget (xml, name);
104
105   if ( !w )
106     g_warning ("Widget \"%s\" could not be found\n", name);
107
108   return w;
109 }
110
111 /* Converts a string in the pspp locale to utf-8.
112    The return value must be freed when no longer required*/
113 gchar *
114 pspp_locale_to_utf8 (const gchar *text, gssize len, GError **err)
115 {
116   return recode_string (CONV_PSPP_TO_UTF8, text, len);
117 }
118
119 #define _(msgid) gettext (msgid)
120 #define N_(msgid) msgid
121
122
123 static void
124 give_help (void)
125 {
126   static struct msg m = {
127     MSG_GENERAL,
128     MSG_NOTE,
129     {0, -1},
130     0,
131   };
132
133   if (! m.text)
134     m.text=g_strdup (_("Sorry. The help system hasn't yet been implemented."));
135
136   popup_message (&m);
137 }
138
139 void
140 connect_help (GladeXML *xml)
141 {
142   GList *helps = glade_xml_get_widget_prefix (xml, "help_button_");
143
144   GList *i;
145   for ( i = g_list_first (helps); i ; i = g_list_next (i))
146     g_signal_connect (GTK_WIDGET (i->data), "clicked", give_help, 0);
147 }
148
149
150
151 void
152 reference_manual (GtkMenuItem *menu, gpointer data)
153 {
154   GError *err = NULL;
155   if ( ! g_spawn_command_line_async ("yelp info:pspp", &err) )
156     {
157       msg (ME, _("Cannot open reference manual: %s"), err->message);
158     }
159   g_clear_error (&err);
160 }
161
162
163 extern struct dataset *the_dataset;
164 extern struct source_stream *the_source_stream;
165 extern PsppireDataStore *the_data_store;
166
167 gboolean
168 execute_syntax (struct getl_interface *sss)
169 {
170   gboolean status;
171   struct lexer *lexer;
172
173   struct casereader *reader = psppire_data_store_get_reader (the_data_store);
174
175   proc_set_active_file_data (the_dataset, reader);
176
177   g_return_val_if_fail (proc_has_active_file (the_dataset), FALSE);
178
179   lexer = lex_create (the_source_stream);
180
181   getl_append_source (the_source_stream, sss);
182
183   for (;;)
184     {
185       int result = cmd_parse (lexer, the_dataset);
186
187       if (result == CMD_EOF || result == CMD_FINISH)
188         break;
189     }
190
191   getl_abort_noninteractive (the_source_stream);
192
193   lex_destroy (lexer);
194
195   /* GUI syntax needs this implicit EXECUTE command at the end of
196      every script.  Otherwise commands like GET could leave the GUI
197      without a datasheet. */
198   status = proc_execute (the_dataset);
199
200   psppire_dict_replace_dictionary (the_data_store->dict,
201                                    dataset_dict (the_dataset));
202
203   {
204     PsppireCaseFile *pcf = psppire_case_file_new (dataset_source (the_dataset));
205
206     psppire_data_store_set_case_file (the_data_store, pcf);
207   }
208
209   return status;
210 }
211
212
213
214 #ifdef G_ENABLE_DEBUG
215 # define g_marshal_value_peek_int(v)      g_value_get_int (v)
216 #else
217 # define g_marshal_value_peek_int(v)      (v)->data[0].v_int
218 #endif
219
220
221 /* VOID:INT,INT,INT */
222 void
223 marshaller_VOID__INT_INT_INT (GClosure     *closure,
224                         GValue       *return_value,
225                         guint         n_param_values,
226                         const GValue *param_values,
227                         gpointer      invocation_hint,
228                         gpointer      marshal_data)
229 {
230   typedef void (*GMarshalFunc_VOID__INT_INT_INT) (gpointer     data1,
231                                                   gint         arg_1,
232                                                   gint         arg_2,
233                                                   gint         arg_3,
234                                                   gpointer     data2);
235   register GMarshalFunc_VOID__INT_INT_INT callback;
236   register GCClosure *cc = (GCClosure*) closure;
237   register gpointer data1, data2;
238
239   g_return_if_fail (n_param_values == 4);
240
241   if (G_CCLOSURE_SWAP_DATA (closure))
242     {
243       data1 = closure->data;
244       data2 = g_value_peek_pointer (param_values + 0);
245     }
246   else
247     {
248       data1 = g_value_peek_pointer (param_values + 0);
249       data2 = closure->data;
250     }
251   callback = (GMarshalFunc_VOID__INT_INT_INT) (marshal_data ? marshal_data : cc->callback);
252
253   callback (data1,
254             g_marshal_value_peek_int (param_values + 1),
255             g_marshal_value_peek_int (param_values + 2),
256             g_marshal_value_peek_int (param_values + 3),
257             data2);
258 }