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 <data/casefile.h>
32 #include <data/data-in.h>
34 #include <math/sort.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->casefile);
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 var_cnt)
156 PsppireCaseFile *cf = g_object_new (G_TYPE_PSPPIRE_CASE_FILE, NULL);
158 cf->casefile = casefile_create(var_cnt);
165 /* Append a case to the case file */
167 psppire_case_file_append_case(PsppireCaseFile *cf,
173 g_return_val_if_fail(cf, FALSE);
174 g_return_val_if_fail(cf->casefile, FALSE);
176 posn = casefile_get_case_cnt(cf->casefile);
178 result = casefile_append(cf->casefile, c);
180 g_signal_emit(cf, signal[CASE_INSERTED], 0, posn);
187 psppire_case_file_get_case_count(const PsppireCaseFile *cf)
189 g_return_val_if_fail(cf, FALSE);
194 return casefile_get_case_cnt(cf->casefile);
197 /* Return the IDXth value from case CASENUM.
198 The return value must not be freed or written to
201 psppire_case_file_get_value(const PsppireCaseFile *cf, gint casenum, gint idx)
203 const union value *v;
205 struct casereader *reader = casefile_get_random_reader (cf->casefile);
207 casereader_seek(reader, casenum);
209 casereader_read(reader, &c);
211 v = case_data(&c, idx);
212 casereader_destroy(reader);
219 psppire_case_file_clear(PsppireCaseFile *cf)
221 casefile_destroy(cf->casefile);
223 g_signal_emit(cf, signal[CASES_DELETED], 0, 0, -1);
226 /* Set the IDXth value of case C using FF and DATA */
228 psppire_case_file_set_value(PsppireCaseFile *cf, gint casenum, gint idx,
229 struct data_in *d_in)
233 struct casereader *reader = casefile_get_random_reader (cf->casefile);
235 casereader_seek(reader, casenum);
236 casereader_read(reader, &cc);
238 /* Cast away const in flagrant abuse of the casefile */
239 d_in->v = (union value *) case_data(&cc, idx);
241 if ( ! data_in(d_in) )
242 g_warning("Cant set value\n");
245 casereader_destroy(reader);
247 g_signal_emit(cf, signal[CASE_CHANGED], 0, casenum);
254 psppire_case_file_sort(PsppireCaseFile *cf, const struct sort_criteria *sc)
257 struct casereader *reader = casefile_get_reader(cf->casefile);
258 cf->casefile = sort_execute(reader, sc);
260 /* FIXME: Need to have a signal to change a range of cases, instead of
261 calling a signal many times */
262 for ( c = 0 ; c < casefile_get_case_cnt(cf->casefile) ; ++c )
263 g_signal_emit(cf, signal[CASE_CHANGED], 0, c);