2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2004 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
24 #include <data/missing-values.h>
25 #include <data/value-labels.h>
26 #include <data/format.h>
28 #include <libpspp/misc.h>
30 #include "psppire-variable.h"
31 #include "psppire-dict.h"
36 psppire_variable_set_name(struct PsppireVariable *pv, const gchar *text)
38 g_return_val_if_fail(pv, FALSE);
39 g_return_val_if_fail(pv->dict, FALSE);
40 g_return_val_if_fail(pv->v, FALSE);
45 if ( 0 == strcmp(pv->v->name, text))
48 if ( ! psppire_dict_check_name(pv->dict, text, TRUE) )
51 dict_rename_var(pv->dict->dict, pv->v, text);
53 psppire_dict_var_changed(pv->dict, pv->v->index);
60 psppire_variable_set_columns(struct PsppireVariable *pv, gint columns)
62 g_return_val_if_fail(pv, FALSE);
63 g_return_val_if_fail(pv->dict, FALSE);
64 g_return_val_if_fail(pv->v, FALSE);
66 pv->v->display_width = columns;
68 psppire_dict_var_changed(pv->dict, pv->v->index);
74 psppire_variable_set_label(struct PsppireVariable *pv, const gchar *label)
76 g_return_val_if_fail(pv, FALSE);
77 g_return_val_if_fail(pv->dict, FALSE);
78 g_return_val_if_fail(pv->v, FALSE);
81 pv->v->label = g_strdup(label);
83 psppire_dict_var_changed(pv->dict, pv->v->index);
90 psppire_variable_set_decimals(struct PsppireVariable *pv, gint decimals)
94 g_return_val_if_fail(pv, FALSE);
95 g_return_val_if_fail(pv->dict, FALSE);
96 g_return_val_if_fail(pv->v, FALSE);
102 return psppire_variable_set_format(pv, &fmt);
108 psppire_variable_set_width(struct PsppireVariable *pv, gint width)
110 struct fmt_spec fmt ;
111 g_return_val_if_fail(pv, FALSE);
112 g_return_val_if_fail(pv->dict, FALSE);
113 g_return_val_if_fail(pv->v, FALSE);
119 if ( pv->v->type == ALPHA )
121 gint old_var_cnt , new_var_cnt ;
123 if ( pv->v->width == 0 )
126 old_var_cnt = DIV_RND_UP(pv->v->width, MAX_SHORT_STRING);
128 new_var_cnt = DIV_RND_UP(width, MAX_SHORT_STRING);
129 pv->v->width = width;
131 psppire_dict_resize_variable(pv->dict, pv,
132 old_var_cnt, new_var_cnt);
135 return psppire_variable_set_format(pv, &fmt);
140 psppire_variable_set_type(struct PsppireVariable *pv, int type)
142 gint old_var_cnt , new_var_cnt ;
144 g_return_val_if_fail(pv, FALSE);
145 g_return_val_if_fail(pv->dict, FALSE);
146 g_return_val_if_fail(pv->v, FALSE);
150 if ( pv->v->width == 0 )
153 old_var_cnt = DIV_RND_UP(pv->v->width, MAX_SHORT_STRING);
155 if ( type == NUMERIC )
158 if ( pv->v->width == 0 )
161 new_var_cnt = DIV_RND_UP(pv->v->width, MAX_SHORT_STRING);
163 psppire_dict_resize_variable(pv->dict, pv,
164 old_var_cnt, new_var_cnt);
166 psppire_dict_var_changed(pv->dict, pv->v->index);
172 psppire_variable_set_format(struct PsppireVariable *pv, struct fmt_spec *fmt)
174 g_return_val_if_fail(pv, FALSE);
175 g_return_val_if_fail(pv->dict, FALSE);
176 g_return_val_if_fail(pv->v, FALSE);
178 if ( check_output_specifier(fmt, false)
180 check_specifier_type(fmt, pv->v->type, false)
182 check_specifier_width(fmt, pv->v->width, false)
185 pv->v->write = pv->v->print = *fmt;
186 psppire_dict_var_changed(pv->dict, pv->v->index);
195 psppire_variable_set_value_labels(const struct PsppireVariable *pv,
196 const struct val_labs *vls)
198 g_return_val_if_fail(pv, FALSE);
199 g_return_val_if_fail(pv->dict, FALSE);
200 g_return_val_if_fail(pv->v, FALSE);
202 val_labs_destroy(pv->v->val_labs);
203 pv->v->val_labs = val_labs_copy(vls);
205 psppire_dict_var_changed(pv->dict, pv->v->index);
210 psppire_variable_set_missing(const struct PsppireVariable *pv,
211 const struct missing_values *miss)
213 g_return_val_if_fail(pv, FALSE);
214 g_return_val_if_fail(pv->dict, FALSE);
215 g_return_val_if_fail(pv->v, FALSE);
217 mv_copy(&pv->v->miss, miss);
219 psppire_dict_var_changed(pv->dict, pv->v->index);
224 psppire_variable_set_write_spec(const struct PsppireVariable *pv, struct fmt_spec fmt)
226 g_return_val_if_fail(pv, FALSE);
227 g_return_val_if_fail(pv->v, FALSE);
231 psppire_dict_var_changed(pv->dict, pv->v->index);
236 psppire_variable_set_print_spec(const struct PsppireVariable *pv, struct fmt_spec fmt)
238 g_return_val_if_fail(pv, FALSE);
239 g_return_val_if_fail(pv->v, FALSE);
243 psppire_dict_var_changed(pv->dict, pv->v->index);
250 psppire_variable_set_alignment(struct PsppireVariable *pv, gint align)
252 g_return_val_if_fail(pv, FALSE);
253 g_return_val_if_fail(pv->dict, FALSE);
254 g_return_val_if_fail(pv->v, FALSE);
256 pv->v->alignment = align;
258 psppire_dict_var_changed(pv->dict, pv->v->index);
264 psppire_variable_set_measure(struct PsppireVariable *pv, gint measure)
266 g_return_val_if_fail(pv, FALSE);
267 g_return_val_if_fail(pv->dict, FALSE);
268 g_return_val_if_fail(pv->v, FALSE);
270 pv->v->measure = measure + 1;
272 psppire_dict_var_changed(pv->dict, pv->v->index);
277 const struct fmt_spec *
278 psppire_variable_get_write_spec(const struct PsppireVariable *pv)
280 g_return_val_if_fail(pv, NULL);
281 g_return_val_if_fail(pv->v, NULL);
284 return &pv->v->write;
289 psppire_variable_get_name(const struct PsppireVariable *pv)
291 g_return_val_if_fail(pv, NULL);
292 g_return_val_if_fail(pv->v, NULL);
299 psppire_variable_get_columns(const struct PsppireVariable *pv)
301 g_return_val_if_fail(pv, -1);
302 g_return_val_if_fail(pv->v, -1);
304 return pv->v->display_width;
310 psppire_variable_get_label(const struct PsppireVariable *pv)
312 g_return_val_if_fail(pv, NULL);
313 g_return_val_if_fail(pv->v, NULL);
319 const struct missing_values *
320 psppire_variable_get_missing(const struct PsppireVariable *pv)
322 g_return_val_if_fail(pv, NULL);
323 g_return_val_if_fail(pv->v, NULL);
329 const struct val_labs *
330 psppire_variable_get_value_labels(const struct PsppireVariable *pv)
332 g_return_val_if_fail(pv, NULL);
333 g_return_val_if_fail(pv->v, NULL);
335 return pv->v->val_labs;
340 psppire_variable_get_alignment(const struct PsppireVariable *pv)
342 g_return_val_if_fail(pv, -1);
343 g_return_val_if_fail(pv->v, -1);
345 return pv->v->alignment;
351 psppire_variable_get_measure(const struct PsppireVariable *pv)
353 g_return_val_if_fail(pv, -1);
354 g_return_val_if_fail(pv->v, -1);
356 return pv->v->measure - 1;
360 psppire_variable_get_type(const struct PsppireVariable *pv)
362 g_return_val_if_fail(pv, -1);
363 g_return_val_if_fail(pv->v, -1);
370 psppire_variable_get_width(const struct PsppireVariable *pv)
372 g_return_val_if_fail(pv, -1);
373 g_return_val_if_fail(pv->v, -1);
380 psppire_variable_get_fv(const struct PsppireVariable *pv)
382 g_return_val_if_fail(pv, -1);
383 g_return_val_if_fail(pv->v, -1);
391 psppire_variable_get_index(const struct PsppireVariable *pv)
393 g_return_val_if_fail(pv, -1);
394 g_return_val_if_fail(pv->v, -1);