2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2006 Free Software Foundation
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include "psppire-object.h"
25 #include "psppire-case-file.h"
27 #include <gtksheet/gtkextra-marshal.h>
29 #include <data/case.h>
30 #include <ui/flexifile.h>
31 #include <data/casefile.h>
32 #include <data/data-in.h>
33 #include <math/sort.h>
34 #include <libpspp/misc.h>
36 /* --- prototypes --- */
37 static void psppire_case_file_class_init (PsppireCaseFileClass *class);
38 static void psppire_case_file_init (PsppireCaseFile *case_file);
39 static void psppire_case_file_finalize (GObject *object);
42 /* --- variables --- */
43 static GObjectClass *parent_class = NULL;
50 static guint signal[n_SIGNALS];
53 /* --- functions --- */
55 * psppire_case_file_get_type:
56 * @returns: the type ID for accelerator groups.
59 psppire_case_file_get_type (void)
61 static GType object_type = 0;
65 static const GTypeInfo object_info = {
66 sizeof (PsppireCaseFileClass),
68 (GBaseFinalizeFunc) NULL,
69 (GClassInitFunc) psppire_case_file_class_init,
70 NULL, /* class_finalize */
71 NULL, /* class_data */
72 sizeof (PsppireCaseFile),
74 (GInstanceInitFunc) psppire_case_file_init,
77 object_type = g_type_register_static (G_TYPE_PSPPIRE_OBJECT, "PsppireCaseFile",
86 psppire_case_file_class_init (PsppireCaseFileClass *class)
88 GObjectClass *object_class = G_OBJECT_CLASS (class);
90 parent_class = g_type_class_peek_parent (class);
92 object_class->finalize = psppire_case_file_finalize;
94 signal[CASE_CHANGED] =
95 g_signal_new ("case_changed",
96 G_TYPE_FROM_CLASS(class),
100 g_cclosure_marshal_VOID__INT,
106 signal[CASE_INSERTED] =
107 g_signal_new ("case_inserted",
108 G_TYPE_FROM_CLASS(class),
112 g_cclosure_marshal_VOID__INT,
118 signal[CASES_DELETED] =
119 g_signal_new ("cases_deleted",
120 G_TYPE_FROM_CLASS(class),
124 gtkextra_VOID__INT_INT,
127 G_TYPE_INT, G_TYPE_INT);
131 psppire_case_file_finalize (GObject *object)
133 PsppireCaseFile *cf = PSPPIRE_CASE_FILE (object);
136 casefile_destroy(cf->flexifile);
138 G_OBJECT_CLASS (parent_class)->finalize (object);
142 psppire_case_file_init (PsppireCaseFile *cf)
148 * psppire_case_file_new:
149 * @returns: a new #PsppireCaseFile object
151 * Creates a new #PsppireCaseFile.
154 psppire_case_file_new (gint val_cnt)
156 PsppireCaseFile *cf = g_object_new (G_TYPE_PSPPIRE_CASE_FILE, NULL);
158 cf->flexifile = flexifile_create (val_cnt);
165 psppire_case_file_delete_cases(PsppireCaseFile *cf, gint n_cases, gint first)
169 g_return_val_if_fail(cf, FALSE);
170 g_return_val_if_fail(cf->flexifile, FALSE);
172 result = flexifile_delete_cases(FLEXIFILE(cf->flexifile), n_cases, first);
174 g_signal_emit(cf, signal[CASES_DELETED], 0, n_cases, first);
179 /* Insert case CC into the case file before POSN */
181 psppire_case_file_insert_case(PsppireCaseFile *cf,
187 g_return_val_if_fail(cf, FALSE);
188 g_return_val_if_fail(cf->flexifile, FALSE);
190 result = flexifile_insert_case(FLEXIFILE(cf->flexifile), cc, posn);
193 g_signal_emit(cf, signal[CASE_INSERTED], 0, posn);
195 g_warning("Cannot insert case at position %d\n", posn);
201 /* Append a case to the case file */
203 psppire_case_file_append_case(PsppireCaseFile *cf,
209 g_return_val_if_fail(cf, FALSE);
210 g_return_val_if_fail(cf->flexifile, FALSE);
212 posn = casefile_get_case_cnt(cf->flexifile);
214 result = casefile_append(cf->flexifile, c);
216 g_signal_emit(cf, signal[CASE_INSERTED], 0, posn);
223 psppire_case_file_get_case_count(const PsppireCaseFile *cf)
225 g_return_val_if_fail(cf, FALSE);
227 if ( ! cf->flexifile)
230 return casefile_get_case_cnt(cf->flexifile);
233 /* Return the IDXth value from case CASENUM.
234 The return value must not be freed or written to
237 psppire_case_file_get_value(const PsppireCaseFile *cf, gint casenum, gint idx)
239 const union value *v;
242 g_return_val_if_fail(cf, NULL);
243 g_return_val_if_fail(cf->flexifile, NULL);
245 g_return_val_if_fail(idx < casefile_get_value_cnt(cf->flexifile), NULL);
247 flexifile_get_case(FLEXIFILE(cf->flexifile), casenum, &c);
249 v = case_data_idx(&c, idx);
257 psppire_case_file_clear(PsppireCaseFile *cf)
259 casefile_destroy(cf->flexifile);
261 g_signal_emit(cf, signal[CASES_DELETED], 0, 0, -1);
264 /* Set the IDXth value of case C to SYSMIS/EMPTY */
266 psppire_case_file_set_value(PsppireCaseFile *cf, gint casenum, gint idx,
267 union value *v, gint width)
272 g_return_val_if_fail(cf, FALSE);
273 g_return_val_if_fail(cf->flexifile, FALSE);
275 g_return_val_if_fail(idx < casefile_get_value_cnt(cf->flexifile), FALSE);
277 if ( ! flexifile_get_case(FLEXIFILE(cf->flexifile), casenum, &cc) )
281 bytes = MAX_SHORT_STRING;
283 bytes = DIV_RND_UP(width, MAX_SHORT_STRING) * MAX_SHORT_STRING ;
285 /* Cast away const in flagrant abuse of the casefile */
286 memcpy((union value *)case_data_idx(&cc, idx), v, bytes);
288 g_signal_emit(cf, signal[CASE_CHANGED], 0, casenum);
295 /* Set the IDXth value of case C using D_IN */
297 psppire_case_file_data_in(PsppireCaseFile *cf, gint casenum, gint idx,
298 struct substring input, const struct fmt_spec *fmt)
302 g_return_val_if_fail(cf, FALSE);
303 g_return_val_if_fail(cf->flexifile, FALSE);
305 g_return_val_if_fail(idx < casefile_get_value_cnt(cf->flexifile), FALSE);
307 if ( ! flexifile_get_case(FLEXIFILE(cf->flexifile), casenum, &cc) )
310 /* Cast away const in flagrant abuse of the casefile */
311 if (!data_in (input, fmt->type, 0, 0,
312 (union value *) case_data_idx(&cc, idx), fmt_var_width (fmt)))
313 g_warning("Cant set value\n");
315 g_signal_emit(cf, signal[CASE_CHANGED], 0, casenum);
322 psppire_case_file_sort(PsppireCaseFile *cf, const struct sort_criteria *sc)
326 struct casefile *cfile;
327 struct casereader *reader = casefile_get_reader (cf->flexifile, NULL);
328 const int value_cnt = casefile_get_value_cnt(cf->flexifile);
330 cfile = sort_execute(reader, sc);
332 casefile_destroy(cf->flexifile);
334 /* Copy casefile into flexifile */
336 reader = casefile_get_destructive_reader(cfile);
337 cf->flexifile = flexifile_create(value_cnt);
338 while(casereader_read(reader, &cc))
339 casefile_append(cf->flexifile, &cc);
342 /* FIXME: Need to have a signal to change a range of cases, instead of
343 calling a signal many times */
344 for ( c = 0 ; c < casefile_get_case_cnt(cf->flexifile) ; ++c )
345 g_signal_emit(cf, signal[CASE_CHANGED], 0, c);
349 /* Resize the cases in the casefile, by inserting N_VALUES into every
352 psppire_case_file_insert_values (PsppireCaseFile *cf,
353 gint n_values, gint before)
355 g_return_val_if_fail(cf, FALSE);
357 if ( ! cf->flexifile )
359 cf->flexifile = flexifile_create(n_values);
363 return flexifile_resize (FLEXIFILE(cf->flexifile), n_values, before);
366 /* Fills C with the CASENUMth case.
367 Returns true on success, false otherwise.
370 psppire_case_file_get_case (const PsppireCaseFile *cf, gint casenum,
373 g_return_val_if_fail (cf, FALSE);
374 g_return_val_if_fail (cf->flexifile, FALSE);
376 return flexifile_get_case (FLEXIFILE(cf->flexifile), casenum, c);