5c884a3fb616e558dc77f0766101209b71434924
[pspp-builds.git] / src / ui / gui / psppire-variable.c
1 /* 
2     PSPPIRE --- A Graphical User Interface for PSPP
3     Copyright (C) 2004  Free Software Foundation
4     Written by John Darrington
5
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.
10
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.
15
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
19     02110-1301, USA. */
20
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include <data/missing-values.h>
25 #include <data/value-labels.h>
26 #include <data/format.h>
27
28 #include <libpspp/misc.h>
29
30 #include "psppire-variable.h"
31 #include "psppire-dict.h"
32
33
34
35 gboolean
36 psppire_variable_set_name(struct PsppireVariable *pv, const gchar *text)
37 {
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);
41
42   if ( !text) 
43     return FALSE;
44
45   if ( 0 == strcmp(pv->v->name, text))
46     return FALSE;
47
48   if ( ! psppire_dict_check_name(pv->dict, text, TRUE) )
49     return FALSE;
50
51   dict_rename_var(pv->dict->dict, pv->v, text);
52
53   psppire_dict_var_changed(pv->dict, pv->v->index);
54
55   return TRUE;
56 }
57
58
59 gboolean
60 psppire_variable_set_columns(struct PsppireVariable *pv, gint columns)
61 {
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);
65
66   pv->v->display_width = columns;
67   
68   psppire_dict_var_changed(pv->dict, pv->v->index);
69
70   return TRUE;
71 }
72
73 gboolean
74 psppire_variable_set_label(struct PsppireVariable *pv, const gchar *label)
75 {
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);
79
80   g_free(pv->v->label);
81   pv->v->label = g_strdup(label);
82
83   psppire_dict_var_changed(pv->dict, pv->v->index);
84
85   return TRUE;
86 }
87
88
89 gboolean
90 psppire_variable_set_decimals(struct PsppireVariable *pv, gint decimals)
91 {
92   struct fmt_spec fmt;
93
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);
97
98   fmt = pv->v->write;
99
100   fmt.d = decimals;
101
102   return psppire_variable_set_format(pv, &fmt);
103 }
104
105
106
107 gboolean
108 psppire_variable_set_width(struct PsppireVariable *pv, gint width)
109 {
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);
114
115   fmt = pv->v->write;
116
117   fmt.w = width;
118
119   if ( pv->v->type == ALPHA ) 
120     {
121       gint old_var_cnt , new_var_cnt ;
122
123       if ( pv->v->width == 0 ) 
124         old_var_cnt = 1;
125       else
126         old_var_cnt = DIV_RND_UP(pv->v->width, MAX_SHORT_STRING);
127       
128       new_var_cnt = DIV_RND_UP(width, MAX_SHORT_STRING);
129     pv->v->width = width;
130
131       psppire_dict_resize_variable(pv->dict, pv,
132                                    old_var_cnt, new_var_cnt);
133     }
134
135   return psppire_variable_set_format(pv, &fmt);
136 }
137
138
139 gboolean
140 psppire_variable_set_type(struct PsppireVariable *pv, int type)
141 {
142   gint old_var_cnt , new_var_cnt ;
143
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);
147
148   pv->v->type = type; 
149
150   if ( pv->v->width == 0 ) 
151     old_var_cnt = 1;
152   else
153     old_var_cnt = DIV_RND_UP(pv->v->width, MAX_SHORT_STRING);
154
155   if ( type == NUMERIC ) 
156     pv->v->width = 0;
157
158   if ( pv->v->width == 0 ) 
159     new_var_cnt = 1;
160   else
161     new_var_cnt = DIV_RND_UP(pv->v->width, MAX_SHORT_STRING);
162
163   psppire_dict_resize_variable(pv->dict, pv,
164                                old_var_cnt, new_var_cnt);
165
166   psppire_dict_var_changed(pv->dict, pv->v->index);
167   return TRUE;
168 }
169
170
171 gboolean
172 psppire_variable_set_format(struct PsppireVariable *pv, struct fmt_spec *fmt)
173 {
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);
177
178   if ( check_output_specifier(fmt, false) 
179        && 
180        check_specifier_type(fmt, pv->v->type, false)
181        && 
182        check_specifier_width(fmt, pv->v->width, false)
183        ) 
184     {
185       pv->v->write = pv->v->print = *fmt;
186       psppire_dict_var_changed(pv->dict, pv->v->index);
187       return TRUE;
188     }
189
190   return FALSE;
191 }
192
193
194 gboolean
195 psppire_variable_set_value_labels(const struct PsppireVariable *pv,
196                                const struct val_labs *vls)
197 {
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);
201
202   val_labs_destroy(pv->v->val_labs);
203   pv->v->val_labs = val_labs_copy(vls);
204
205   psppire_dict_var_changed(pv->dict, pv->v->index);
206   return TRUE;
207 }
208
209 gboolean 
210 psppire_variable_set_missing(const struct PsppireVariable *pv,
211                           const struct missing_values *miss)
212 {
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);
216
217   mv_copy(&pv->v->miss, miss);
218
219   psppire_dict_var_changed(pv->dict, pv->v->index);
220   return TRUE;
221 }
222
223 gboolean
224 psppire_variable_set_write_spec(const struct PsppireVariable *pv, struct fmt_spec fmt)
225 {
226   g_return_val_if_fail(pv, FALSE);
227   g_return_val_if_fail(pv->v, FALSE);
228
229   pv->v->write = fmt;
230
231   psppire_dict_var_changed(pv->dict, pv->v->index);
232   return TRUE;
233 }
234
235 gboolean
236 psppire_variable_set_print_spec(const struct PsppireVariable *pv, struct fmt_spec fmt)
237 {
238   g_return_val_if_fail(pv, FALSE);
239   g_return_val_if_fail(pv->v, FALSE);
240
241   pv->v->print = fmt;
242
243   psppire_dict_var_changed(pv->dict, pv->v->index);
244   return TRUE;
245 }
246
247
248
249 gboolean
250 psppire_variable_set_alignment(struct PsppireVariable *pv, gint align)
251 {
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);
255
256   pv->v->alignment = align;
257
258   psppire_dict_var_changed(pv->dict, pv->v->index);
259   return TRUE;
260 }
261
262
263 gboolean
264 psppire_variable_set_measure(struct PsppireVariable *pv, gint measure)
265 {
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);
269
270   pv->v->measure = measure + 1;
271
272   psppire_dict_var_changed(pv->dict, pv->v->index);
273   return TRUE;
274 }
275
276
277 const struct fmt_spec *
278 psppire_variable_get_write_spec(const struct PsppireVariable *pv)
279 {
280   g_return_val_if_fail(pv, NULL);
281   g_return_val_if_fail(pv->v, NULL);
282
283
284   return &pv->v->write;
285 }
286
287
288 const gchar *
289 psppire_variable_get_name(const struct PsppireVariable *pv)
290 {
291   g_return_val_if_fail(pv, NULL);
292   g_return_val_if_fail(pv->v, NULL);
293
294   return pv->v->name;
295 }
296
297
298 gint
299 psppire_variable_get_columns(const struct PsppireVariable *pv)
300 {
301   g_return_val_if_fail(pv, -1);
302   g_return_val_if_fail(pv->v, -1);
303
304   return pv->v->display_width;
305 }
306
307
308
309 const gchar *
310 psppire_variable_get_label(const struct PsppireVariable *pv)
311 {
312   g_return_val_if_fail(pv, NULL);
313   g_return_val_if_fail(pv->v, NULL);
314
315   return pv->v->label;
316 }
317
318
319 const struct missing_values *
320 psppire_variable_get_missing(const struct PsppireVariable *pv)
321 {
322   g_return_val_if_fail(pv, NULL);
323   g_return_val_if_fail(pv->v, NULL);
324
325   return &pv->v->miss;
326 }
327
328
329 const struct val_labs *
330 psppire_variable_get_value_labels(const struct PsppireVariable *pv)
331 {
332   g_return_val_if_fail(pv, NULL);
333   g_return_val_if_fail(pv->v, NULL);
334
335   return pv->v->val_labs;
336 }
337
338
339 gint
340 psppire_variable_get_alignment(const struct PsppireVariable *pv)
341 {
342   g_return_val_if_fail(pv, -1);
343   g_return_val_if_fail(pv->v, -1);
344
345   return pv->v->alignment;
346 }
347
348
349
350 gint
351 psppire_variable_get_measure(const struct PsppireVariable *pv)
352 {
353   g_return_val_if_fail(pv, -1);
354   g_return_val_if_fail(pv->v, -1);
355
356   return pv->v->measure - 1;
357 }
358
359 gint
360 psppire_variable_get_type(const struct PsppireVariable *pv)
361 {
362   g_return_val_if_fail(pv, -1);
363   g_return_val_if_fail(pv->v, -1);
364
365   return pv->v->type;
366 }
367
368
369 gint
370 psppire_variable_get_width(const struct PsppireVariable *pv)
371 {
372   g_return_val_if_fail(pv, -1);
373   g_return_val_if_fail(pv->v, -1);
374
375   return pv->v->width;
376 }
377
378
379 gint
380 psppire_variable_get_fv(const struct PsppireVariable *pv)
381 {
382   g_return_val_if_fail(pv, -1);
383   g_return_val_if_fail(pv->v, -1);
384
385   return pv->v->fv;
386 }
387
388
389
390 gint
391 psppire_variable_get_index(const struct PsppireVariable *pv)
392 {
393   g_return_val_if_fail(pv, -1);
394   g_return_val_if_fail(pv->v, -1);
395
396   return pv->v->index;
397 }
398