6195a16b2ce1c1b421880a2d9e031d2f426dc684
[pspp-builds.git] / src / ui / gui / psppire-variable.c
1 /* 
2     PSPPIRE --- A Graphical User Interface for PSPP
3     Copyright (C) 2004, 2006  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 #include <libpspp/message.h>
28
29 #include <libpspp/misc.h>
30
31 #include "psppire-variable.h"
32 #include "psppire-dict.h"
33
34
35
36 gboolean
37 psppire_variable_set_name(struct PsppireVariable *pv, const gchar *text)
38 {
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);
42
43   if ( !text) 
44     return FALSE;
45
46   if ( 0 == strcmp(var_get_name (pv->v), text))
47     return FALSE;
48
49   if ( ! psppire_dict_check_name(pv->dict, text, TRUE) )
50     return FALSE;
51
52   dict_rename_var(pv->dict->dict, pv->v, text);
53
54   psppire_dict_var_changed(pv->dict, pv->v->index);
55
56   return TRUE;
57 }
58
59
60 gboolean
61 psppire_variable_set_columns(struct PsppireVariable *pv, gint columns)
62 {
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);
66
67   var_set_display_width (pv->v, columns);
68   
69   psppire_dict_var_changed(pv->dict, pv->v->index);
70
71   return TRUE;
72 }
73
74 gboolean
75 psppire_variable_set_label(struct PsppireVariable *pv, const gchar *label)
76 {
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);
80
81   var_set_label (pv->v, 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 = *var_get_write_format (pv->v);
99   fmt.d = decimals;
100
101   return psppire_variable_set_format(pv, &fmt);
102 }
103
104
105
106 gboolean
107 psppire_variable_set_width(struct PsppireVariable *pv, gint width)
108 {
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);
113
114   fmt = *var_get_write_format (pv->v);
115   fmt.w = width;
116
117   if (var_is_alpha (pv->v))
118     {
119       gint old_var_cnt , new_var_cnt ;
120
121       if ( var_get_width (pv->v) == 0 ) 
122         old_var_cnt = 1;
123       else
124         old_var_cnt = DIV_RND_UP(var_get_width (pv->v), MAX_SHORT_STRING);
125       
126       new_var_cnt = DIV_RND_UP(width, MAX_SHORT_STRING);
127       pv->v->width = width;
128
129       psppire_dict_resize_variable(pv->dict, pv,
130                                    old_var_cnt, new_var_cnt);
131     }
132
133   return psppire_variable_set_format(pv, &fmt);
134 }
135
136
137 gboolean
138 psppire_variable_set_type(struct PsppireVariable *pv, int type)
139 {
140   gint old_var_cnt , new_var_cnt ;
141
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);
145
146   if ( var_get_width (pv->v) ) 
147     old_var_cnt = 1;
148   else
149     old_var_cnt = DIV_RND_UP (var_get_width (pv->v), MAX_SHORT_STRING);
150
151   if ( type == NUMERIC ) 
152     pv->v->width = 0;
153   else if (var_get_width (pv->v))
154     pv->v->width = 1;
155
156   if ( var_get_width (pv->v) == 0 ) 
157     new_var_cnt = 1;
158   else
159     new_var_cnt = DIV_RND_UP (var_get_width (pv->v), MAX_SHORT_STRING);
160
161   psppire_dict_resize_variable(pv->dict, pv,
162                                old_var_cnt, new_var_cnt);
163
164   psppire_dict_var_changed(pv->dict, pv->v->index);
165   return TRUE;
166 }
167
168
169 gboolean
170 psppire_variable_set_format(struct PsppireVariable *pv, struct fmt_spec *fmt)
171 {
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);
175
176   msg_disable ();
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))) 
180     {
181       msg_enable ();
182       var_set_both_formats (pv->v, fmt);
183       psppire_dict_var_changed(pv->dict, pv->v->index);
184       return TRUE;
185     }
186   msg_enable ();
187
188   return FALSE;
189 }
190
191
192 gboolean
193 psppire_variable_set_value_labels(const struct PsppireVariable *pv,
194                                const struct val_labs *vls)
195 {
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);
199
200   val_labs_destroy(pv->v->val_labs);
201   pv->v->val_labs = val_labs_copy(vls);
202
203   psppire_dict_var_changed(pv->dict, pv->v->index);
204   return TRUE;
205 }
206
207 gboolean 
208 psppire_variable_set_missing(const struct PsppireVariable *pv,
209                           const struct missing_values *miss)
210 {
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);
214
215   var_set_missing_values (pv->v, miss);
216
217   psppire_dict_var_changed(pv->dict, pv->v->index);
218   return TRUE;
219 }
220
221 gboolean
222 psppire_variable_set_write_spec(const struct PsppireVariable *pv, struct fmt_spec fmt)
223 {
224   g_return_val_if_fail(pv, FALSE);
225   g_return_val_if_fail(pv->v, FALSE);
226
227   var_set_write_format (pv->v, &fmt);
228
229   psppire_dict_var_changed(pv->dict, pv->v->index);
230   return TRUE;
231 }
232
233 gboolean
234 psppire_variable_set_print_spec(const struct PsppireVariable *pv, struct fmt_spec fmt)
235 {
236   g_return_val_if_fail(pv, FALSE);
237   g_return_val_if_fail(pv->v, FALSE);
238
239   var_set_print_format (pv->v, &fmt);
240
241   psppire_dict_var_changed(pv->dict, pv->v->index);
242   return TRUE;
243 }
244
245
246
247 gboolean
248 psppire_variable_set_alignment(struct PsppireVariable *pv, gint align)
249 {
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);
253
254   var_set_alignment (pv->v, align);
255
256   psppire_dict_var_changed(pv->dict, pv->v->index);
257   return TRUE;
258 }
259
260
261 gboolean
262 psppire_variable_set_measure(struct PsppireVariable *pv, gint measure)
263 {
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);
267
268   var_set_measure (pv->v, measure + 1);
269
270   psppire_dict_var_changed(pv->dict, pv->v->index);
271   return TRUE;
272 }
273
274
275 const struct fmt_spec *
276 psppire_variable_get_write_spec(const struct PsppireVariable *pv)
277 {
278   g_return_val_if_fail(pv, NULL);
279   g_return_val_if_fail(pv->v, NULL);
280
281   return var_get_write_format (pv->v);
282 }
283
284
285 const gchar *
286 psppire_variable_get_name(const struct PsppireVariable *pv)
287 {
288   g_return_val_if_fail(pv, NULL);
289   g_return_val_if_fail(pv->v, NULL);
290
291   return var_get_name (pv->v);
292 }
293
294
295 gint
296 psppire_variable_get_columns(const struct PsppireVariable *pv)
297 {
298   g_return_val_if_fail(pv, -1);
299   g_return_val_if_fail(pv->v, -1);
300
301   return var_get_display_width (pv->v);
302 }
303
304
305
306 const gchar *
307 psppire_variable_get_label(const struct PsppireVariable *pv)
308 {
309   g_return_val_if_fail(pv, NULL);
310   g_return_val_if_fail(pv->v, NULL);
311
312   return var_get_label (pv->v);
313 }
314
315
316 const struct missing_values *
317 psppire_variable_get_missing(const struct PsppireVariable *pv)
318 {
319   g_return_val_if_fail(pv, NULL);
320   g_return_val_if_fail(pv->v, NULL);
321
322   return var_get_missing_values (pv->v);
323 }
324
325
326 const struct val_labs *
327 psppire_variable_get_value_labels(const struct PsppireVariable *pv)
328 {
329   g_return_val_if_fail(pv, NULL);
330   g_return_val_if_fail(pv->v, NULL);
331
332   return pv->v->val_labs;
333 }
334
335
336 gint
337 psppire_variable_get_alignment(const struct PsppireVariable *pv)
338 {
339   g_return_val_if_fail(pv, -1);
340   g_return_val_if_fail(pv->v, -1);
341
342   return var_get_alignment (pv->v);
343 }
344
345
346
347 gint
348 psppire_variable_get_measure(const struct PsppireVariable *pv)
349 {
350   g_return_val_if_fail(pv, -1);
351   g_return_val_if_fail(pv->v, -1);
352
353   return var_get_measure (pv->v) - 1;
354 }
355
356 gint
357 psppire_variable_get_type(const struct PsppireVariable *pv)
358 {
359   g_return_val_if_fail(pv, -1);
360   g_return_val_if_fail(pv->v, -1);
361
362   return var_get_type (pv->v);
363 }
364
365
366 gint
367 psppire_variable_get_width(const struct PsppireVariable *pv)
368 {
369   g_return_val_if_fail(pv, -1);
370   g_return_val_if_fail(pv->v, -1);
371
372   return var_get_width (pv->v);
373 }
374
375
376 gint
377 psppire_variable_get_fv(const struct PsppireVariable *pv)
378 {
379   g_return_val_if_fail(pv, -1);
380   g_return_val_if_fail(pv->v, -1);
381
382   return pv->v->fv;
383 }
384
385
386
387 gint
388 psppire_variable_get_index(const struct PsppireVariable *pv)
389 {
390   g_return_val_if_fail(pv, -1);
391   g_return_val_if_fail(pv->v, -1);
392
393   return pv->v->index;
394 }
395