2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2004, 2006 Free Software Foundation
4 Written by John Darrington
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <gtksheet/gtkextra-marshal.c>
27 #include "psppire-object.h"
28 #include "psppire-dict.h"
29 #include <data/format.h>
30 #include <data/dictionary.h>
31 #include <data/missing-values.h>
32 #include <data/value-labels.h>
35 #include "message-dialog.h"
36 #include "psppire-variable.h"
38 /* --- prototypes --- */
39 static void psppire_dict_class_init (PsppireDictClass *class);
40 static void psppire_dict_init (PsppireDict *dict);
41 static void psppire_dict_finalize (GObject *object);
44 /* --- variables --- */
45 static GObjectClass *parent_class = NULL;
47 enum {VARIABLE_CHANGED,
52 static guint signal[n_SIGNALS];
56 /* --- functions --- */
58 * psppire_dict_get_type:
59 * @returns: the type ID for accelerator groups.
62 psppire_dict_get_type (void)
64 static GType object_type = 0;
68 static const GTypeInfo object_info = {
69 sizeof (PsppireDictClass),
71 (GBaseFinalizeFunc) NULL,
72 (GClassInitFunc) psppire_dict_class_init,
73 NULL, /* class_finalize */
74 NULL, /* class_data */
77 (GInstanceInitFunc) psppire_dict_init,
80 object_type = g_type_register_static (G_TYPE_PSPPIRE_OBJECT, "PsppireDict",
89 psppire_dict_class_init (PsppireDictClass *class)
91 GObjectClass *object_class = G_OBJECT_CLASS (class);
93 parent_class = g_type_class_peek_parent (class);
95 object_class->finalize = psppire_dict_finalize;
97 signal[VARIABLE_CHANGED] =
98 g_signal_new ("variable_changed",
99 G_TYPE_FROM_CLASS(class),
103 g_cclosure_marshal_VOID__INT,
110 signal[VARIABLE_INSERTED] =
111 g_signal_new ("variable_inserted",
112 G_TYPE_FROM_CLASS(class),
116 g_cclosure_marshal_VOID__INT,
122 signal[VARIABLES_DELETED] =
123 g_signal_new ("variables_deleted",
124 G_TYPE_FROM_CLASS(class),
128 gtkextra_VOID__INT_INT,
137 psppire_dict_finalize (GObject *object)
140 PsppireDict *d = PSPPIRE_DICT (object);
142 for (v = 0 ; v < psppire_dict_get_var_cnt(d) ; ++v )
143 g_free(d->variables[v]);
145 g_free(d->variables);
148 dict_destroy(d->dict);
150 G_OBJECT_CLASS (parent_class)->finalize (object);
154 psppire_dict_init (PsppireDict *psppire_dict)
156 psppire_dict->dict = dict_create();
158 psppire_dict->variables = 0;
159 psppire_dict->cache_size = 0;
164 * @returns: a new #PsppireDict object
166 * Creates a new #PsppireDict.
169 psppire_dict_new (void)
171 return g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
176 * psppire_dict_new_from_dict:
177 * @returns: a new #PsppireDict object
179 * Creates a new #PsppireDict.
182 psppire_dict_new_from_dict (struct dictionary *d)
184 PsppireDict *new_dict = g_object_new (G_TYPE_PSPPIRE_DICT, NULL);
186 new_dict->cache_size = dict_get_var_cnt(d);
187 new_dict->variables = g_malloc0(sizeof(struct PsppireVariable *) *
188 new_dict->cache_size);
195 /* Returns a valid name for a new variable in DICT.
196 The return value is statically allocated */
198 auto_generate_var_name(PsppireDict *dict)
201 static gchar name[10];
204 while (g_snprintf(name, 10, "VAR%05d",d++),
205 psppire_dict_lookup_var(dict, name))
211 /* Insert a new variable at posn IDX, with the name NAME.
212 If NAME is null, then a name will be automatically assigned.
215 psppire_dict_insert_variable(PsppireDict *d, gint idx, const gchar *name)
217 struct variable *var ;
220 g_return_if_fail(G_IS_PSPPIRE_DICT(d));
223 /* Invalidate the cache from IDX onwards */
224 for ( i = idx ; i < d->cache_size ; ++i )
226 g_free(d->variables[i]);
230 /* Ensure that the cache is large enough */
231 if ( dict_get_var_cnt(d->dict) >= d->cache_size )
233 d->variables = g_realloc(d->variables, sizeof(struct PsppireVariable*) *
234 (d->cache_size + CACHE_CHUNK));
235 d->cache_size += CACHE_CHUNK;
238 /* Zero the new pointers */
239 for ( ; i < d->cache_size ; ++i )
245 name = auto_generate_var_name(d);
247 var = dict_create_var(d->dict, name, 0);
249 dict_reorder_var(d->dict, var, idx);
251 d->variables[idx] = g_malloc(sizeof (struct PsppireVariable));
252 d->variables[idx]->v = var;
253 d->variables[idx]->dict = d;
255 g_signal_emit(d, signal[VARIABLE_INSERTED], 0, idx );
258 /* Delete N variables beginning at FIRST */
260 psppire_dict_delete_variables(PsppireDict *d, gint first, gint n)
264 g_return_if_fail(d->dict);
265 g_return_if_fail(G_IS_PSPPIRE_DICT(d));
267 /* Invalidate all pvs from FIRST onwards */
268 for ( idx = first ; idx < d->cache_size ; ++idx )
270 g_free(d->variables[idx]);
271 d->variables[idx] = 0;
274 for (idx = 0 ; idx < n ; ++idx )
276 struct variable *var;
278 /* Do nothing if it's out of bounds */
279 if ( first >= dict_get_var_cnt (d->dict))
282 var = dict_get_var(d->dict, first);
283 dict_delete_var (d->dict, var);
286 g_signal_emit(d, signal[VARIABLES_DELETED], 0, first, idx );
291 psppire_dict_set_name(PsppireDict* d, gint idx, const gchar *name)
293 struct variable *var;
295 g_assert(G_IS_PSPPIRE_DICT(d));
298 if ( idx < dict_get_var_cnt(d->dict))
300 /* This is an existing variable? */
301 var = dict_get_var(d->dict, idx);
302 dict_rename_var(d->dict, var, name);
303 g_signal_emit(d, signal[VARIABLE_CHANGED], 0, idx);
308 dict_create_var(d->dict, name, 0);
309 g_print("Emitting variable-inserted signal\n");
310 g_signal_emit(d, signal[VARIABLE_INSERTED], 0, idx);
316 /* Return the IDXth variable */
317 struct PsppireVariable *
318 psppire_dict_get_variable(PsppireDict *d, gint idx)
320 struct PsppireVariable *var ;
321 g_return_val_if_fail(d, NULL);
322 g_return_val_if_fail(d->dict, NULL);
327 if (idx < 0 || idx >= psppire_dict_get_var_cnt(d))
330 var = d->variables[idx] ;
334 var = g_malloc(sizeof (*var));
336 var->v = dict_get_var(d->dict, idx);
337 d->variables[idx] = var;
344 /* Return the number of variables in the dictionary */
346 psppire_dict_get_var_cnt(const PsppireDict *d)
348 g_return_val_if_fail(d, -1);
349 g_return_val_if_fail(d->dict, -1);
352 return dict_get_var_cnt(d->dict);
356 /* Return a variable by name.
357 Return NULL if it doesn't exist
360 psppire_dict_lookup_var (const PsppireDict *d, const gchar *name)
362 g_return_val_if_fail(d, NULL);
363 g_return_val_if_fail(d->dict, NULL);
365 return dict_lookup_var(d->dict, name);
370 psppire_dict_var_changed(PsppireDict *d, gint idx)
374 g_signal_emit(d, signal[VARIABLE_CHANGED], 0, idx);
378 /* Clears the contents of D */
380 psppire_dict_clear(PsppireDict *d)
383 g_return_if_fail(d->dict);
386 const gint n_vars = dict_get_var_cnt(d->dict);
391 /* Invalidate the entire cache */
392 for ( i = 0 ; i < d->cache_size ; ++i )
394 g_free(d->variables[i]);
398 g_signal_emit(d, signal[VARIABLES_DELETED], 0, 0, n_vars );
404 /* Return true is NAME would be a valid name of a variable to add to the
405 dictionary. False otherwise.
406 If REPORT is true, then invalid names will be reported as such as errors
409 psppire_dict_check_name(const PsppireDict *dict,
410 const gchar *name, gboolean report)
412 if ( ! var_is_valid_name(name, report ) )
415 if (psppire_dict_lookup_var(dict, name))
418 msg(ME,"Duplicate variable name.");