1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2009, 2010, 2011 Free Software Foundation
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.
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.
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/>. */
18 /* This file is a rubbish bin where stuff gets put when it doesn't seem to
23 #include <glib-object.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"
35 #include <libpspp/i18n.h>
40 #include <data/settings.h>
42 #include "psppire-data-store.h"
45 #include "gl/configmake.h"
50 /* Formats a value according to VAR's print format and strips white space
51 appropriately for VAR's type. That is, if VAR is numeric, strips leading
52 white space (because numbers are right-justified within their fields), and
53 if VAR is string, strips trailing white space (because spaces pad out string
56 Returns an allocated string. The returned string must be freed when no
59 value_to_text (union value v, const struct variable *var)
63 s = data_out (&v, var_get_encoding (var), var_get_print_format (var));
64 if (var_is_numeric (var))
73 /* Converts TEXT to a value.
75 VAL will be initialised and filled by this function.
76 It is the caller's responsibility to destroy VAL when no longer needed.
77 VAR must be the variable with which VAL is associated.
79 On success, VAL is returned, NULL otherwise.
82 text_to_value (const gchar *text,
83 const struct variable *var,
86 const struct fmt_spec *format = var_get_print_format (var);
87 int width = var_get_width (var);
89 if ( format->type != FMT_A)
91 if ( ! text ) return NULL;
94 const gchar *s = text;
102 if ( !*s) return NULL;
106 value_init (val, width);
107 free (data_in (ss_cstr (text), UTF8, format->type, val, width,
108 var_get_encoding (var)));
115 builder_new_real (const gchar *name)
117 GtkBuilder *builder = gtk_builder_new ();
120 if ( ! gtk_builder_add_from_file (builder, name, &err))
122 g_critical ("Couldnt open user interface file %s: %s", name, err->message);
123 g_clear_error (&err);
131 get_object_assert (GtkBuilder *builder, const gchar *name, GType type)
136 o = gtk_builder_get_object (builder, name);
139 g_critical ("Object `%s' could not be found\n", name);
140 else if ( ! g_type_is_a (G_OBJECT_TYPE (o), type))
142 g_critical ("Object `%s' was expected to have type %s, but in fact has type %s",
143 name, g_type_name (type), G_OBJECT_TYPE_NAME (o));
151 get_action_assert (GtkBuilder *builder, const gchar *name)
153 return GTK_ACTION (get_object_assert (builder, name, GTK_TYPE_ACTION));
157 get_widget_assert (GtkBuilder *builder, const gchar *name)
159 return GTK_WIDGET (get_object_assert (builder, name, GTK_TYPE_WIDGET));
162 /* This function must be used whenever a filename generated by glib,
163 (eg, from gtk_file_chooser_get_filename) and passed to the C library,
164 (eg through a pspp syntax string).
167 convert_glib_filename_to_system_filename (const gchar *fname, GError **err)
172 const gchar *target_encoding;
173 gchar *utf8_name = NULL;
175 g_get_charset (&target_encoding);
177 output_name = g_convert (fname, -1, target_encoding,
178 "UTF-8", NULL, NULL, err);
180 output_name = xstrdup (fname);
188 #define _(msgid) gettext (msgid)
189 #define N_(msgid) msgid
197 dialog = gtk_message_dialog_new (NULL,
201 _("Sorry. The help system hasn't yet "
202 "been implemented."));
203 gtk_dialog_run (GTK_DIALOG (dialog));
204 gtk_widget_destroy (dialog);
208 connect_help (GtkBuilder *xml)
210 GSList *helps = gtk_builder_get_objects (xml);
213 for ( i = helps; i ; i = g_slist_next (i))
215 GObject *o = i->data;
216 if ( GTK_IS_WIDGET (o) )
218 const gchar *name = gtk_buildable_get_name (GTK_BUILDABLE (o));
222 strncpy (s, name, 11);
226 if ( 0 == strcmp ("help_button", s))
228 g_signal_connect (o, "clicked", give_help, 0);
233 g_slist_free (helps);
237 /* Create a deep copy of SRC */
239 clone_list_store (const GtkListStore *src)
241 GtkTreeIter src_iter;
244 const gint n_cols = gtk_tree_model_get_n_columns (GTK_TREE_MODEL (src));
245 GType *types = g_malloc (sizeof (*types) * n_cols);
250 for (i = 0 ; i < n_cols; ++i )
251 types[i] = gtk_tree_model_get_column_type (GTK_TREE_MODEL (src), i);
253 dest = gtk_list_store_newv (n_cols, types);
255 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (src),
258 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (src), &src_iter))
260 GtkTreeIter dest_iter;
261 gtk_list_store_append (dest, &dest_iter);
263 for (i = 0 ; i < n_cols; ++i )
267 gtk_tree_model_get_value (GTK_TREE_MODEL (src), &src_iter, i, &val);
268 gtk_list_store_set_value (dest, &dest_iter, i, &val);
270 g_value_unset (&val);
284 on_delete (GtkWindow *window, GdkEvent *e, GtkWindow **addr)
292 paste_syntax_to_window (gchar *syntax)
294 static GtkWidget *the_syntax_pasteboard = NULL;
296 GtkTextBuffer *buffer = NULL;
298 if ( NULL == the_syntax_pasteboard)
300 the_syntax_pasteboard = psppire_syntax_window_new (NULL);
301 g_signal_connect (the_syntax_pasteboard, "delete-event", G_CALLBACK (on_delete),
302 &the_syntax_pasteboard);
305 buffer = GTK_TEXT_BUFFER (PSPPIRE_SYNTAX_WINDOW (the_syntax_pasteboard)->buffer);
307 gtk_text_buffer_begin_user_action (buffer);
308 gtk_text_buffer_insert_at_cursor (buffer, syntax, -1);
309 gtk_text_buffer_insert_at_cursor (buffer, "\n", 1);
310 gtk_text_buffer_end_user_action (buffer);
312 gtk_widget_show (the_syntax_pasteboard);
318 /* gtk_box_pack_start_defaults is deprecated.
319 Therefore we roll our own until a better solution is found */
321 psppire_box_pack_start_defaults (GtkBox *box, GtkWidget *widget)
323 gtk_box_pack_start (box, widget, TRUE, TRUE, 0);