2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2004, 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
24 #include <data/missing-values.h>
25 #include <data/value-labels.h>
26 #include <data/format.h>
27 #include <libpspp/message.h>
29 #include <libpspp/misc.h>
31 #include "psppire-variable.h"
32 #include "psppire-dict.h"
37 psppire_variable_set_name(struct PsppireVariable *pv, const gchar *text)
39 g_return_val_if_fail(pv, FALSE);
40 g_return_val_if_fail(pv->dict, FALSE);
41 g_return_val_if_fail(pv->v, FALSE);
46 if ( 0 == strcmp(var_get_name (pv->v), text))
49 if ( ! psppire_dict_check_name(pv->dict, text, TRUE) )
52 dict_rename_var(pv->dict->dict, pv->v, text);
54 psppire_dict_var_changed(pv->dict, pv->v->index);
61 psppire_variable_set_columns(struct PsppireVariable *pv, gint columns)
63 g_return_val_if_fail(pv, FALSE);
64 g_return_val_if_fail(pv->dict, FALSE);
65 g_return_val_if_fail(pv->v, FALSE);
67 var_set_display_width (pv->v, columns);
69 psppire_dict_var_changed(pv->dict, pv->v->index);
75 psppire_variable_set_label(struct PsppireVariable *pv, const gchar *label)
77 g_return_val_if_fail(pv, FALSE);
78 g_return_val_if_fail(pv->dict, FALSE);
79 g_return_val_if_fail(pv->v, FALSE);
81 var_set_label (pv->v, 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);
98 fmt = *var_get_write_format (pv->v);
101 return psppire_variable_set_format(pv, &fmt);
107 psppire_variable_set_width(struct PsppireVariable *pv, gint width)
109 struct fmt_spec fmt ;
110 g_return_val_if_fail(pv, FALSE);
111 g_return_val_if_fail(pv->dict, FALSE);
112 g_return_val_if_fail(pv->v, FALSE);
114 fmt = *var_get_write_format (pv->v);
117 if (var_is_alpha (pv->v))
119 gint old_var_cnt , new_var_cnt ;
121 if ( var_get_width (pv->v) == 0 )
124 old_var_cnt = DIV_RND_UP(var_get_width (pv->v), MAX_SHORT_STRING);
126 new_var_cnt = DIV_RND_UP(width, MAX_SHORT_STRING);
127 pv->v->width = width;
129 psppire_dict_resize_variable(pv->dict, pv,
130 old_var_cnt, new_var_cnt);
133 return psppire_variable_set_format(pv, &fmt);
138 psppire_variable_set_type(struct PsppireVariable *pv, int type)
140 gint old_var_cnt , new_var_cnt ;
142 g_return_val_if_fail(pv, FALSE);
143 g_return_val_if_fail(pv->dict, FALSE);
144 g_return_val_if_fail(pv->v, FALSE);
146 if ( var_get_width (pv->v) )
149 old_var_cnt = DIV_RND_UP (var_get_width (pv->v), MAX_SHORT_STRING);
151 if ( type == NUMERIC )
153 else if (var_get_width (pv->v))
156 if ( var_get_width (pv->v) == 0 )
159 new_var_cnt = DIV_RND_UP (var_get_width (pv->v), MAX_SHORT_STRING);
161 psppire_dict_resize_variable(pv->dict, pv,
162 old_var_cnt, new_var_cnt);
164 psppire_dict_var_changed(pv->dict, pv->v->index);
170 psppire_variable_set_format(struct PsppireVariable *pv, struct fmt_spec *fmt)
172 g_return_val_if_fail(pv, FALSE);
173 g_return_val_if_fail(pv->dict, FALSE);
174 g_return_val_if_fail(pv->v, FALSE);
177 if ( fmt_check_output(fmt)
178 && fmt_check_type_compat (fmt, var_get_type (pv->v))
179 && fmt_check_width_compat (fmt, var_get_width (pv->v)))
182 var_set_both_formats (pv->v, fmt);
183 psppire_dict_var_changed(pv->dict, pv->v->index);
193 psppire_variable_set_value_labels(const struct PsppireVariable *pv,
194 const struct val_labs *vls)
196 g_return_val_if_fail(pv, FALSE);
197 g_return_val_if_fail(pv->dict, FALSE);
198 g_return_val_if_fail(pv->v, FALSE);
200 val_labs_destroy(pv->v->val_labs);
201 pv->v->val_labs = val_labs_copy(vls);
203 psppire_dict_var_changed(pv->dict, pv->v->index);
208 psppire_variable_set_missing(const struct PsppireVariable *pv,
209 const struct missing_values *miss)
211 g_return_val_if_fail(pv, FALSE);
212 g_return_val_if_fail(pv->dict, FALSE);
213 g_return_val_if_fail(pv->v, FALSE);
215 var_set_missing_values (pv->v, miss);
217 psppire_dict_var_changed(pv->dict, pv->v->index);
222 psppire_variable_set_write_spec(const struct PsppireVariable *pv, struct fmt_spec fmt)
224 g_return_val_if_fail(pv, FALSE);
225 g_return_val_if_fail(pv->v, FALSE);
227 var_set_write_format (pv->v, &fmt);
229 psppire_dict_var_changed(pv->dict, pv->v->index);
234 psppire_variable_set_print_spec(const struct PsppireVariable *pv, struct fmt_spec fmt)
236 g_return_val_if_fail(pv, FALSE);
237 g_return_val_if_fail(pv->v, FALSE);
239 var_set_print_format (pv->v, &fmt);
241 psppire_dict_var_changed(pv->dict, pv->v->index);
248 psppire_variable_set_alignment(struct PsppireVariable *pv, gint align)
250 g_return_val_if_fail(pv, FALSE);
251 g_return_val_if_fail(pv->dict, FALSE);
252 g_return_val_if_fail(pv->v, FALSE);
254 var_set_alignment (pv->v, align);
256 psppire_dict_var_changed(pv->dict, pv->v->index);
262 psppire_variable_set_measure(struct PsppireVariable *pv, gint measure)
264 g_return_val_if_fail(pv, FALSE);
265 g_return_val_if_fail(pv->dict, FALSE);
266 g_return_val_if_fail(pv->v, FALSE);
268 var_set_measure (pv->v, measure + 1);
270 psppire_dict_var_changed(pv->dict, pv->v->index);
275 const struct fmt_spec *
276 psppire_variable_get_write_spec(const struct PsppireVariable *pv)
278 g_return_val_if_fail(pv, NULL);
279 g_return_val_if_fail(pv->v, NULL);
281 return var_get_write_format (pv->v);
286 psppire_variable_get_name(const struct PsppireVariable *pv)
288 g_return_val_if_fail(pv, NULL);
289 g_return_val_if_fail(pv->v, NULL);
291 return var_get_name (pv->v);
296 psppire_variable_get_columns(const struct PsppireVariable *pv)
298 g_return_val_if_fail(pv, -1);
299 g_return_val_if_fail(pv->v, -1);
301 return var_get_display_width (pv->v);
307 psppire_variable_get_label(const struct PsppireVariable *pv)
309 g_return_val_if_fail(pv, NULL);
310 g_return_val_if_fail(pv->v, NULL);
312 return var_get_label (pv->v);
316 const struct missing_values *
317 psppire_variable_get_missing(const struct PsppireVariable *pv)
319 g_return_val_if_fail(pv, NULL);
320 g_return_val_if_fail(pv->v, NULL);
322 return var_get_missing_values (pv->v);
326 const struct val_labs *
327 psppire_variable_get_value_labels(const struct PsppireVariable *pv)
329 g_return_val_if_fail(pv, NULL);
330 g_return_val_if_fail(pv->v, NULL);
332 return pv->v->val_labs;
337 psppire_variable_get_alignment(const struct PsppireVariable *pv)
339 g_return_val_if_fail(pv, -1);
340 g_return_val_if_fail(pv->v, -1);
342 return var_get_alignment (pv->v);
348 psppire_variable_get_measure(const struct PsppireVariable *pv)
350 g_return_val_if_fail(pv, -1);
351 g_return_val_if_fail(pv->v, -1);
353 return var_get_measure (pv->v) - 1;
357 psppire_variable_get_type(const struct PsppireVariable *pv)
359 g_return_val_if_fail(pv, -1);
360 g_return_val_if_fail(pv->v, -1);
362 return var_get_type (pv->v);
367 psppire_variable_get_width(const struct PsppireVariable *pv)
369 g_return_val_if_fail(pv, -1);
370 g_return_val_if_fail(pv->v, -1);
372 return var_get_width (pv->v);
377 psppire_variable_get_fv(const struct PsppireVariable *pv)
379 g_return_val_if_fail(pv, -1);
380 g_return_val_if_fail(pv->v, -1);
388 psppire_variable_get_index(const struct PsppireVariable *pv)
390 g_return_val_if_fail(pv, -1);
391 g_return_val_if_fail(pv->v, -1);