a0d753b5d6668bd1d204981f4b4c023824716170
[pspp-builds.git] / src / ui / gui / helper.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2009, 2010  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 <data/format.h>
28 #include <data/data-in.h>
29 #include <data/data-out.h>
30 #include <data/dictionary.h>
31 #include <data/casereader-provider.h>
32 #include <libpspp/message.h>
33 #include "psppire-syntax-window.h"
34 #include <gtk/gtkbuilder.h>
35 #include <libpspp/i18n.h>
36
37 #include <ctype.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <data/settings.h>
41
42 #include "psppire-data-store.h"
43
44 #include "xalloc.h"
45
46 #include <gettext.h>
47
48 /* Formats a value according to FORMAT
49    The returned string must be freed when no longer required */
50 gchar *
51 value_to_text (union value v, const PsppireDict *dict, struct fmt_spec format)
52 {
53   gchar *s = 0;
54
55   s = data_out (&v, dict_get_encoding (dict->dict),  &format);
56   g_strchug (s);
57
58   return s;
59 }
60
61
62 /* Converts TEXT to a value.
63
64    VAL will be initialised and filled by this function.
65    It is the caller's responsibility to destroy VAL when no longer needed.
66    VAR and DICT must be the variable and dictionary with which VAL
67    is associated.
68
69    On success, VAL is returned, NULL otherwise.
70 */
71 union value *
72 text_to_value (const gchar *text,
73                const PsppireDict *dict,
74                const struct variable *var,
75                union value *val)
76 {
77   const struct fmt_spec *format = var_get_print_format (var);
78   int width = var_get_width (var);
79
80   if ( format->type != FMT_A)
81     {
82       if ( ! text ) return NULL;
83
84       {
85         const gchar *s = text;
86         while (*s)
87           {
88             if ( !isspace (*s))
89               break;
90             s++;
91           }
92
93         if ( !*s) return NULL;
94       }
95     }
96
97   value_init (val, width);
98   msg_disable ();
99   data_in (ss_cstr (text), UTF8, format->type, 0, 0, 0,
100                 dict->dict,
101                 val, width);
102   msg_enable ();
103
104   return val;
105 }
106
107
108 GtkBuilder *
109 builder_new_real (const gchar *name)
110 {
111   GtkBuilder *builder = gtk_builder_new ();
112
113   GError *err = NULL;
114   if ( ! gtk_builder_add_from_file (builder, name,  &err))
115     {
116       g_critical ("Couldnt open user interface  file %s: %s", name, err->message);
117       g_clear_error (&err);
118     }
119
120   return builder;
121 }
122
123
124 GObject *
125 get_object_assert (GtkBuilder *builder, const gchar *name, GType type)
126 {
127   GObject *o = NULL;
128   g_assert (name);
129
130   o = gtk_builder_get_object (builder, name);
131
132   if ( !o )
133     g_critical ("Object \"%s\" could not be found\n", name);
134   else if ( ! g_type_is_a (G_OBJECT_TYPE (o), type))
135    {
136      g_critical ("Object \"%s\" was expected to have type %s, but in fact has type %s", 
137         name, g_type_name (type), G_OBJECT_TYPE_NAME (o));
138    }
139
140   return o;
141 }
142
143
144 GtkAction *
145 get_action_assert (GtkBuilder *builder, const gchar *name)
146 {
147   return GTK_ACTION (get_object_assert (builder, name, GTK_TYPE_ACTION));
148 }
149
150 GtkWidget *
151 get_widget_assert (GtkBuilder *builder, const gchar *name)
152 {
153   return GTK_WIDGET (get_object_assert (builder, name, GTK_TYPE_WIDGET));
154 }
155
156 /* This function must be used whenever a filename generated by glib,
157    (eg, from gtk_file_chooser_get_filename) and passed to the C library,
158    (eg through a pspp syntax string).
159 */
160 gchar *
161 convert_glib_filename_to_system_filename (const gchar *fname, GError **err)
162 {
163   gchar *output_name;
164
165 #ifdef G_OS_WIN32
166   const gchar *target_encoding;
167   gchar *utf8_name = NULL;
168
169   g_get_charset (&target_encoding);
170
171   output_name = g_convert (fname, -1, target_encoding,
172                         "UTF-8", NULL, NULL, err);
173 #else
174   output_name = xstrdup (fname);
175 #endif
176
177   return output_name;
178 }
179
180
181
182 #define _(msgid) gettext (msgid)
183 #define N_(msgid) msgid
184
185
186 static void
187 give_help (void)
188 {
189   GtkWidget *dialog;
190
191   dialog = gtk_message_dialog_new (NULL,
192                                    GTK_DIALOG_MODAL,
193                                    GTK_MESSAGE_INFO,
194                                    GTK_BUTTONS_CLOSE,
195                                    _("Sorry. The help system hasn't yet "
196                                      "been implemented."));
197   gtk_dialog_run (GTK_DIALOG (dialog));
198   gtk_widget_destroy (dialog);
199 }
200
201 void
202 connect_help (GtkBuilder *xml)
203 {
204   GSList *helps = gtk_builder_get_objects (xml);
205
206   GSList *i;
207   for ( i = helps; i ; i = g_slist_next (i))
208     {
209       GObject *o = i->data;
210       if ( GTK_IS_WIDGET (o) )
211         {
212           gchar *name = NULL;
213           gchar s[12] = {0};
214           g_object_get (o, "name", &name, NULL);
215
216           if ( name)
217             strncpy (s, name, 11);
218           s[11] = '\0';
219
220
221           if ( 0 == strcmp ("help_button", s))
222             {
223             g_signal_connect (o, "clicked", give_help, 0);
224             }
225         }
226     }
227
228   g_slist_free (helps);
229 }
230
231
232 void
233 reference_manual (GtkMenuItem *menu, gpointer data)
234 {
235   GError *err = NULL;
236   gchar *cmd = g_strdup_printf ("yelp file://%s", relocate (DOCDIR "/pspp.xml"));
237
238   if ( ! g_spawn_command_line_async (cmd, &err) )
239     {
240       msg (ME, _("Cannot open reference manual: %s.  The PSPP user manual is "
241                  "also available at "
242                  "http://www.gnu.org/software/pspp/documentation.html"),
243            err->message);
244     }
245
246   g_free (cmd);
247   g_clear_error (&err);
248 }
249
250
251 /* Create a deep copy of SRC */
252 GtkListStore *
253 clone_list_store (const GtkListStore *src)
254 {
255   GtkTreeIter src_iter;
256   gboolean ok;
257   gint i;
258   const gint n_cols =  gtk_tree_model_get_n_columns (GTK_TREE_MODEL (src));
259   GType *types = g_malloc (sizeof (*types) *  n_cols);
260
261   int row = 0;
262   GtkListStore *dest;
263
264   for (i = 0 ; i < n_cols; ++i )
265     types[i] = gtk_tree_model_get_column_type (GTK_TREE_MODEL (src), i);
266
267   dest = gtk_list_store_newv (n_cols, types);
268
269   for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
270                                            &src_iter);
271        ok;
272        ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (src), &src_iter))
273     {
274       GtkTreeIter dest_iter;
275       gtk_list_store_append  (dest, &dest_iter);
276
277       for (i = 0 ; i < n_cols; ++i )
278         {
279           GValue val = {0};
280
281           gtk_tree_model_get_value (GTK_TREE_MODEL (src), &src_iter, i, &val);
282           gtk_list_store_set_value (dest, &dest_iter, i, &val);
283
284           g_value_unset (&val);
285         }
286       row++;
287     }
288
289   g_free (types);
290
291   return dest;
292 }
293
294
295 void
296 paste_syntax_in_new_window (const gchar *syntax)
297 {
298   GtkWidget *se = psppire_syntax_window_new ();
299
300   gtk_text_buffer_insert_at_cursor (PSPPIRE_SYNTAX_WINDOW (se)->buffer, syntax, -1);
301
302   gtk_widget_show (se);
303 }
304
305
306 /* gtk_box_pack_start_defaults is deprecated.
307    Therefore we roll our own until a better solution is found */
308 void
309 psppire_box_pack_start_defaults (GtkBox *box, GtkWidget *widget)
310 {
311   gtk_box_pack_start (box, widget, TRUE, TRUE, 0);
312 }