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
26 #include <data/missing-values.h>
27 #include <data/value-labels.h>
28 #include <data/format.h>
29 #include <libpspp/message.h>
31 #include <libpspp/misc.h>
33 #include "psppire-variable.h"
34 #include "psppire-dict.h"
39 psppire_variable_set_name(struct PsppireVariable *pv, const gchar *text)
41 g_return_val_if_fail(pv, FALSE);
42 g_return_val_if_fail(pv->dict, FALSE);
43 g_return_val_if_fail(pv->v, FALSE);
48 if ( 0 == strcmp(var_get_name (pv->v), text))
51 if ( ! psppire_dict_check_name(pv->dict, text, TRUE) )
54 dict_rename_var(pv->dict->dict, pv->v, text);
56 psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
63 psppire_variable_set_columns(struct PsppireVariable *pv, gint columns)
65 g_return_val_if_fail(pv, FALSE);
66 g_return_val_if_fail(pv->dict, FALSE);
67 g_return_val_if_fail(pv->v, FALSE);
69 var_set_display_width (pv->v, columns);
71 psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
77 psppire_variable_set_label(struct PsppireVariable *pv, const gchar *label)
79 g_return_val_if_fail(pv, FALSE);
80 g_return_val_if_fail(pv->dict, FALSE);
81 g_return_val_if_fail(pv->v, FALSE);
83 var_set_label (pv->v, label);
85 psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
92 psppire_variable_set_decimals(struct PsppireVariable *pv, gint decimals)
96 g_return_val_if_fail(pv, FALSE);
97 g_return_val_if_fail(pv->dict, FALSE);
98 g_return_val_if_fail(pv->v, FALSE);
100 fmt = *var_get_write_format (pv->v);
103 return psppire_variable_set_format(pv, &fmt);
109 psppire_variable_set_width(struct PsppireVariable *pv, gint width)
111 struct fmt_spec fmt ;
112 g_return_val_if_fail(pv, FALSE);
113 g_return_val_if_fail(pv->dict, FALSE);
114 g_return_val_if_fail(pv->v, FALSE);
116 fmt = *var_get_write_format (pv->v);
119 if (var_is_alpha (pv->v))
121 gint old_var_cnt , new_var_cnt ;
123 if ( var_get_width (pv->v) == 0 )
126 old_var_cnt = DIV_RND_UP(var_get_width (pv->v), MAX_SHORT_STRING);
128 new_var_cnt = DIV_RND_UP(width, MAX_SHORT_STRING);
129 var_set_width (pv->v, 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);
148 if ( var_get_width (pv->v) )
151 old_var_cnt = DIV_RND_UP (var_get_width (pv->v), MAX_SHORT_STRING);
153 var_set_width (pv->v, type == VAR_NUMERIC ? 0 : 1);
155 if ( var_get_width (pv->v) == 0 )
158 new_var_cnt = DIV_RND_UP (var_get_width (pv->v), MAX_SHORT_STRING);
160 psppire_dict_resize_variable(pv->dict, pv,
161 old_var_cnt, new_var_cnt);
163 psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
169 psppire_variable_set_format(struct PsppireVariable *pv, struct fmt_spec *fmt)
171 g_return_val_if_fail(pv, FALSE);
172 g_return_val_if_fail(pv->dict, FALSE);
173 g_return_val_if_fail(pv->v, FALSE);
176 if ( fmt_check_output(fmt)
177 && fmt_check_type_compat (fmt, var_get_type (pv->v))
178 && fmt_check_width_compat (fmt, var_get_width (pv->v)))
181 var_set_both_formats (pv->v, fmt);
182 psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
192 psppire_variable_set_value_labels(const struct PsppireVariable *pv,
193 const struct val_labs *vls)
195 g_return_val_if_fail(pv, FALSE);
196 g_return_val_if_fail(pv->dict, FALSE);
197 g_return_val_if_fail(pv->v, FALSE);
199 var_set_value_labels (pv->v, vls);
201 psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
206 psppire_variable_set_missing(const struct PsppireVariable *pv,
207 const struct missing_values *miss)
209 g_return_val_if_fail(pv, FALSE);
210 g_return_val_if_fail(pv->dict, FALSE);
211 g_return_val_if_fail(pv->v, FALSE);
213 var_set_missing_values (pv->v, miss);
215 psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
220 psppire_variable_set_write_spec(const struct PsppireVariable *pv, struct fmt_spec fmt)
222 g_return_val_if_fail(pv, FALSE);
223 g_return_val_if_fail(pv->v, FALSE);
225 var_set_write_format (pv->v, &fmt);
227 psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
232 psppire_variable_set_print_spec(const struct PsppireVariable *pv, struct fmt_spec fmt)
234 g_return_val_if_fail(pv, FALSE);
235 g_return_val_if_fail(pv->v, FALSE);
237 var_set_print_format (pv->v, &fmt);
239 psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
246 psppire_variable_set_alignment(struct PsppireVariable *pv, gint align)
248 g_return_val_if_fail(pv, FALSE);
249 g_return_val_if_fail(pv->dict, FALSE);
250 g_return_val_if_fail(pv->v, FALSE);
252 var_set_alignment (pv->v, align);
254 psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
260 psppire_variable_set_measure(struct PsppireVariable *pv, gint measure)
262 g_return_val_if_fail(pv, FALSE);
263 g_return_val_if_fail(pv->dict, FALSE);
264 g_return_val_if_fail(pv->v, FALSE);
266 var_set_measure (pv->v, measure + 1);
268 psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
273 const struct fmt_spec *
274 psppire_variable_get_write_spec(const struct PsppireVariable *pv)
276 g_return_val_if_fail(pv, NULL);
277 g_return_val_if_fail(pv->v, NULL);
279 return var_get_write_format (pv->v);
284 psppire_variable_get_name(const struct PsppireVariable *pv)
286 g_return_val_if_fail(pv, NULL);
287 g_return_val_if_fail(pv->v, NULL);
289 return var_get_name (pv->v);
294 psppire_variable_get_columns(const struct PsppireVariable *pv)
296 g_return_val_if_fail(pv, -1);
297 g_return_val_if_fail(pv->v, -1);
299 return var_get_display_width (pv->v);
305 psppire_variable_get_label(const struct PsppireVariable *pv)
307 g_return_val_if_fail(pv, NULL);
308 g_return_val_if_fail(pv->v, NULL);
310 return var_get_label (pv->v);
314 const struct missing_values *
315 psppire_variable_get_missing(const struct PsppireVariable *pv)
317 g_return_val_if_fail(pv, NULL);
318 g_return_val_if_fail(pv->v, NULL);
320 return var_get_missing_values (pv->v);
324 const struct val_labs *
325 psppire_variable_get_value_labels(const struct PsppireVariable *pv)
327 g_return_val_if_fail(pv, NULL);
328 g_return_val_if_fail(pv->v, NULL);
330 return var_get_value_labels (pv->v);
335 psppire_variable_get_alignment(const struct PsppireVariable *pv)
337 g_return_val_if_fail(pv, -1);
338 g_return_val_if_fail(pv->v, -1);
340 return var_get_alignment (pv->v);
346 psppire_variable_get_measure(const struct PsppireVariable *pv)
348 g_return_val_if_fail(pv, -1);
349 g_return_val_if_fail(pv->v, -1);
351 return var_get_measure (pv->v) - 1;
355 psppire_variable_get_type(const struct PsppireVariable *pv)
357 g_return_val_if_fail(pv, -1);
358 g_return_val_if_fail(pv->v, -1);
360 return var_get_type (pv->v);
365 psppire_variable_get_width(const struct PsppireVariable *pv)
367 g_return_val_if_fail(pv, -1);
368 g_return_val_if_fail(pv->v, -1);
370 return var_get_width (pv->v);
375 psppire_variable_get_fv(const struct PsppireVariable *pv)
377 g_return_val_if_fail(pv, -1);
378 g_return_val_if_fail(pv->v, -1);
380 return var_get_case_index (pv->v);
386 psppire_variable_get_index(const struct PsppireVariable *pv)
388 g_return_val_if_fail(pv, -1);
389 g_return_val_if_fail(pv->v, -1);
391 return var_get_dict_index (pv->v);