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 a blank case to the case file */
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 case_create (&cc, casefile_get_value_cnt(cf->flexifile));
193 result = flexifile_insert_case(FLEXIFILE(cf->flexifile), &cc, posn);
198 g_signal_emit(cf, signal[CASE_INSERTED], 0, posn);
200 g_warning("Cannot insert case at position %d\n", posn);
205 /* Append a case to the case file */
207 psppire_case_file_append_case(PsppireCaseFile *cf,
213 g_return_val_if_fail(cf, FALSE);
214 g_return_val_if_fail(cf->flexifile, FALSE);
216 posn = casefile_get_case_cnt(cf->flexifile);
218 result = casefile_append(cf->flexifile, c);
220 g_signal_emit(cf, signal[CASE_INSERTED], 0, posn);
227 psppire_case_file_get_case_count(const PsppireCaseFile *cf)
229 g_return_val_if_fail(cf, FALSE);
231 if ( ! cf->flexifile)
234 return casefile_get_case_cnt(cf->flexifile);
237 /* Return the IDXth value from case CASENUM.
238 The return value must not be freed or written to
241 psppire_case_file_get_value(const PsppireCaseFile *cf, gint casenum, gint idx)
243 const union value *v;
246 g_return_val_if_fail(cf, NULL);
247 g_return_val_if_fail(cf->flexifile, NULL);
249 g_return_val_if_fail(idx < casefile_get_value_cnt(cf->flexifile), NULL);
251 flexifile_get_case(FLEXIFILE(cf->flexifile), casenum, &c);
253 v = case_data(&c, idx);
261 psppire_case_file_clear(PsppireCaseFile *cf)
263 casefile_destroy(cf->flexifile);
265 g_signal_emit(cf, signal[CASES_DELETED], 0, 0, -1);
268 /* Set the IDXth value of case C to SYSMIS/EMPTY */
270 psppire_case_file_set_value(PsppireCaseFile *cf, gint casenum, gint idx,
271 union value *v, gint width)
276 g_return_val_if_fail(cf, FALSE);
277 g_return_val_if_fail(cf->flexifile, FALSE);
279 g_return_val_if_fail(idx < casefile_get_value_cnt(cf->flexifile), FALSE);
281 if ( ! flexifile_get_case(FLEXIFILE(cf->flexifile), casenum, &cc) )
285 bytes = MAX_SHORT_STRING;
287 bytes = DIV_RND_UP(width, MAX_SHORT_STRING) * MAX_SHORT_STRING ;
289 /* Cast away const in flagrant abuse of the casefile */
290 memcpy((union value *)case_data(&cc, idx), v, bytes);
292 g_signal_emit(cf, signal[CASE_CHANGED], 0, casenum);
299 /* Set the IDXth value of case C using D_IN */
301 psppire_case_file_data_in(PsppireCaseFile *cf, gint casenum, gint idx,
302 struct data_in *d_in)
306 g_return_val_if_fail(cf, FALSE);
307 g_return_val_if_fail(cf->flexifile, FALSE);
309 g_return_val_if_fail(idx < casefile_get_value_cnt(cf->flexifile), FALSE);
311 if ( ! flexifile_get_case(FLEXIFILE(cf->flexifile), casenum, &cc) )
314 /* Cast away const in flagrant abuse of the casefile */
315 d_in->v = (union value *) case_data(&cc, idx);
317 if ( ! data_in(d_in) )
318 g_warning("Cant set value\n");
320 g_signal_emit(cf, signal[CASE_CHANGED], 0, casenum);
327 psppire_case_file_sort(PsppireCaseFile *cf, const struct sort_criteria *sc)
331 struct casefile *cfile;
332 struct casereader *reader = casefile_get_reader(cf->flexifile);
333 const int value_cnt = casefile_get_value_cnt(cf->flexifile);
335 cfile = sort_execute(reader, sc);
337 casefile_destroy(cf->flexifile);
339 /* Copy casefile into flexifile */
341 reader = casefile_get_destructive_reader(cfile);
342 cf->flexifile = flexifile_create(value_cnt);
343 while(casereader_read(reader, &cc))
344 casefile_append(cf->flexifile, &cc);
347 /* FIXME: Need to have a signal to change a range of cases, instead of
348 calling a signal many times */
349 for ( c = 0 ; c < casefile_get_case_cnt(cf->flexifile) ; ++c )
350 g_signal_emit(cf, signal[CASE_CHANGED], 0, c);
354 /* Resize the cases in the casefile, by inserting N_VALUES into every
357 psppire_case_file_insert_values(PsppireCaseFile *cf,
358 gint n_values, gint before)
360 g_return_val_if_fail(cf, FALSE);
362 if ( ! cf->flexifile )
364 cf->flexifile = flexifile_create(n_values);
368 return flexifile_resize(FLEXIFILE(cf->flexifile), n_values, before);