2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 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 "psppire-object.h"
26 #include "psppire-case-file.h"
28 #include <gtksheet/gtkextra-marshal.h>
30 #include <data/case.h>
31 #include <ui/flexifile.h>
32 #include <data/casefile.h>
33 #include <data/data-in.h>
34 #include <math/sort.h>
35 #include <libpspp/misc.h>
37 /* --- prototypes --- */
38 static void psppire_case_file_class_init (PsppireCaseFileClass *class);
39 static void psppire_case_file_init (PsppireCaseFile *case_file);
40 static void psppire_case_file_finalize (GObject *object);
43 /* --- variables --- */
44 static GObjectClass *parent_class = NULL;
51 static guint signal[n_SIGNALS];
54 /* --- functions --- */
56 * psppire_case_file_get_type:
57 * @returns: the type ID for accelerator groups.
60 psppire_case_file_get_type (void)
62 static GType object_type = 0;
66 static const GTypeInfo object_info = {
67 sizeof (PsppireCaseFileClass),
69 (GBaseFinalizeFunc) NULL,
70 (GClassInitFunc) psppire_case_file_class_init,
71 NULL, /* class_finalize */
72 NULL, /* class_data */
73 sizeof (PsppireCaseFile),
75 (GInstanceInitFunc) psppire_case_file_init,
78 object_type = g_type_register_static (G_TYPE_PSPPIRE_OBJECT, "PsppireCaseFile",
87 psppire_case_file_class_init (PsppireCaseFileClass *class)
89 GObjectClass *object_class = G_OBJECT_CLASS (class);
91 parent_class = g_type_class_peek_parent (class);
93 object_class->finalize = psppire_case_file_finalize;
95 signal[CASE_CHANGED] =
96 g_signal_new ("case_changed",
97 G_TYPE_FROM_CLASS(class),
101 g_cclosure_marshal_VOID__INT,
107 signal[CASE_INSERTED] =
108 g_signal_new ("case_inserted",
109 G_TYPE_FROM_CLASS(class),
113 g_cclosure_marshal_VOID__INT,
119 signal[CASES_DELETED] =
120 g_signal_new ("cases_deleted",
121 G_TYPE_FROM_CLASS(class),
125 gtkextra_VOID__INT_INT,
128 G_TYPE_INT, G_TYPE_INT);
132 psppire_case_file_finalize (GObject *object)
134 PsppireCaseFile *cf = PSPPIRE_CASE_FILE (object);
137 casefile_destroy(cf->flexifile);
139 G_OBJECT_CLASS (parent_class)->finalize (object);
143 psppire_case_file_init (PsppireCaseFile *cf)
149 * psppire_case_file_new:
150 * @returns: a new #PsppireCaseFile object
152 * Creates a new #PsppireCaseFile.
155 psppire_case_file_new (gint val_cnt)
157 PsppireCaseFile *cf = g_object_new (G_TYPE_PSPPIRE_CASE_FILE, NULL);
159 cf->flexifile = flexifile_create (val_cnt);
166 psppire_case_file_delete_cases(PsppireCaseFile *cf, gint n_cases, gint first)
170 g_return_val_if_fail(cf, FALSE);
171 g_return_val_if_fail(cf->flexifile, FALSE);
173 result = flexifile_delete_cases(FLEXIFILE(cf->flexifile), n_cases, first);
175 g_signal_emit(cf, signal[CASES_DELETED], 0, n_cases, first);
180 /* Insert case CC into the case file before POSN */
182 psppire_case_file_insert_case(PsppireCaseFile *cf,
188 g_return_val_if_fail(cf, FALSE);
189 g_return_val_if_fail(cf->flexifile, FALSE);
191 result = flexifile_insert_case(FLEXIFILE(cf->flexifile), cc, posn);
194 g_signal_emit(cf, signal[CASE_INSERTED], 0, posn);
196 g_warning("Cannot insert case at position %d\n", posn);
202 /* Append a case to the case file */
204 psppire_case_file_append_case(PsppireCaseFile *cf,
210 g_return_val_if_fail(cf, FALSE);
211 g_return_val_if_fail(cf->flexifile, FALSE);
213 posn = casefile_get_case_cnt(cf->flexifile);
215 result = casefile_append(cf->flexifile, c);
217 g_signal_emit(cf, signal[CASE_INSERTED], 0, posn);
224 psppire_case_file_get_case_count(const PsppireCaseFile *cf)
226 g_return_val_if_fail(cf, FALSE);
228 if ( ! cf->flexifile)
231 return casefile_get_case_cnt(cf->flexifile);
234 /* Return the IDXth value from case CASENUM.
235 The return value must not be freed or written to
238 psppire_case_file_get_value(const PsppireCaseFile *cf, gint casenum, gint idx)
240 const union value *v;
243 g_return_val_if_fail(cf, NULL);
244 g_return_val_if_fail(cf->flexifile, NULL);
246 g_return_val_if_fail(idx < casefile_get_value_cnt(cf->flexifile), NULL);
248 flexifile_get_case(FLEXIFILE(cf->flexifile), casenum, &c);
250 v = case_data(&c, idx);
258 psppire_case_file_clear(PsppireCaseFile *cf)
260 casefile_destroy(cf->flexifile);
262 g_signal_emit(cf, signal[CASES_DELETED], 0, 0, -1);
265 /* Set the IDXth value of case C to SYSMIS/EMPTY */
267 psppire_case_file_set_value(PsppireCaseFile *cf, gint casenum, gint idx,
268 union value *v, gint width)
273 g_return_val_if_fail(cf, FALSE);
274 g_return_val_if_fail(cf->flexifile, FALSE);
276 g_return_val_if_fail(idx < casefile_get_value_cnt(cf->flexifile), FALSE);
278 if ( ! flexifile_get_case(FLEXIFILE(cf->flexifile), casenum, &cc) )
282 bytes = MAX_SHORT_STRING;
284 bytes = DIV_RND_UP(width, MAX_SHORT_STRING) * MAX_SHORT_STRING ;
286 /* Cast away const in flagrant abuse of the casefile */
287 memcpy((union value *)case_data(&cc, idx), v, bytes);
289 g_signal_emit(cf, signal[CASE_CHANGED], 0, casenum);
296 /* Set the IDXth value of case C using D_IN */
298 psppire_case_file_data_in(PsppireCaseFile *cf, gint casenum, gint idx,
299 struct substring input, const struct fmt_spec *fmt)
303 g_return_val_if_fail(cf, FALSE);
304 g_return_val_if_fail(cf->flexifile, FALSE);
306 g_return_val_if_fail(idx < casefile_get_value_cnt(cf->flexifile), FALSE);
308 if ( ! flexifile_get_case(FLEXIFILE(cf->flexifile), casenum, &cc) )
311 /* Cast away const in flagrant abuse of the casefile */
312 if (!data_in (input, fmt->type, 0, 0,
313 (union value *) case_data(&cc, idx), fmt_var_width (fmt)))
314 g_warning("Cant set value\n");
316 g_signal_emit(cf, signal[CASE_CHANGED], 0, casenum);
323 psppire_case_file_sort(PsppireCaseFile *cf, const struct sort_criteria *sc)
327 struct casefile *cfile;
328 struct casereader *reader = casefile_get_reader (cf->flexifile, NULL);
329 const int value_cnt = casefile_get_value_cnt(cf->flexifile);
331 cfile = sort_execute(reader, sc);
333 casefile_destroy(cf->flexifile);
335 /* Copy casefile into flexifile */
337 reader = casefile_get_destructive_reader(cfile);
338 cf->flexifile = flexifile_create(value_cnt);
339 while(casereader_read(reader, &cc))
340 casefile_append(cf->flexifile, &cc);
343 /* FIXME: Need to have a signal to change a range of cases, instead of
344 calling a signal many times */
345 for ( c = 0 ; c < casefile_get_case_cnt(cf->flexifile) ; ++c )
346 g_signal_emit(cf, signal[CASE_CHANGED], 0, c);
350 /* Resize the cases in the casefile, by inserting N_VALUES into every
353 psppire_case_file_insert_values (PsppireCaseFile *cf,
354 gint n_values, gint before)
356 g_return_val_if_fail(cf, FALSE);
358 if ( ! cf->flexifile )
360 cf->flexifile = flexifile_create(n_values);
364 return flexifile_resize (FLEXIFILE(cf->flexifile), n_values, before);
367 /* Fills C with the CASENUMth case.
368 Returns true on success, false otherwise.
371 psppire_case_file_get_case (const PsppireCaseFile *cf, gint casenum,
374 g_return_val_if_fail (cf, FALSE);
375 g_return_val_if_fail (cf->flexifile, FALSE);
377 return flexifile_get_case (FLEXIFILE(cf->flexifile), casenum, c);