Finish converting struct variable to an opaque type. In this
[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 <config.h>
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include <data/missing-values.h>
27 #include <data/value-labels.h>
28 #include <data/format.h>
29 #include <libpspp/message.h>
30
31 #include <libpspp/misc.h>
32
33 #include "psppire-variable.h"
34 #include "psppire-dict.h"
35
36
37
38 gboolean
39 psppire_variable_set_name(struct PsppireVariable *pv, const gchar *text)
40 {
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);
44
45   if ( !text) 
46     return FALSE;
47
48   if ( 0 == strcmp(var_get_name (pv->v), text))
49     return FALSE;
50
51   if ( ! psppire_dict_check_name(pv->dict, text, TRUE) )
52     return FALSE;
53
54   dict_rename_var(pv->dict->dict, pv->v, text);
55
56   psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
57
58   return TRUE;
59 }
60
61
62 gboolean
63 psppire_variable_set_columns(struct PsppireVariable *pv, gint columns)
64 {
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);
68
69   var_set_display_width (pv->v, columns);
70   
71   psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
72
73   return TRUE;
74 }
75
76 gboolean
77 psppire_variable_set_label(struct PsppireVariable *pv, const gchar *label)
78 {
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);
82
83   var_set_label (pv->v, label);
84
85   psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
86
87   return TRUE;
88 }
89
90
91 gboolean
92 psppire_variable_set_decimals(struct PsppireVariable *pv, gint decimals)
93 {
94   struct fmt_spec fmt;
95
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);
99
100   fmt = *var_get_write_format (pv->v);
101   fmt.d = decimals;
102
103   return psppire_variable_set_format(pv, &fmt);
104 }
105
106
107
108 gboolean
109 psppire_variable_set_width(struct PsppireVariable *pv, gint width)
110 {
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);
115
116   fmt = *var_get_write_format (pv->v);
117   fmt.w = width;
118
119   if (var_is_alpha (pv->v))
120     {
121       gint old_var_cnt , new_var_cnt ;
122
123       if ( var_get_width (pv->v) == 0 ) 
124         old_var_cnt = 1;
125       else
126         old_var_cnt = DIV_RND_UP(var_get_width (pv->v), MAX_SHORT_STRING);
127       
128       new_var_cnt = DIV_RND_UP(width, MAX_SHORT_STRING);
129       var_set_width (pv->v, 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   if ( var_get_width (pv->v) ) 
149     old_var_cnt = 1;
150   else
151     old_var_cnt = DIV_RND_UP (var_get_width (pv->v), MAX_SHORT_STRING);
152
153   var_set_width (pv->v, type == VAR_NUMERIC ? 0 : 1);
154
155   if ( var_get_width (pv->v) == 0 ) 
156     new_var_cnt = 1;
157   else
158     new_var_cnt = DIV_RND_UP (var_get_width (pv->v), MAX_SHORT_STRING);
159
160   psppire_dict_resize_variable(pv->dict, pv,
161                                old_var_cnt, new_var_cnt);
162
163   psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
164   return TRUE;
165 }
166
167
168 gboolean
169 psppire_variable_set_format(struct PsppireVariable *pv, struct fmt_spec *fmt)
170 {
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);
174
175   msg_disable ();
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))) 
179     {
180       msg_enable ();
181       var_set_both_formats (pv->v, fmt);
182       psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
183       return TRUE;
184     }
185   msg_enable ();
186
187   return FALSE;
188 }
189
190
191 gboolean
192 psppire_variable_set_value_labels(const struct PsppireVariable *pv,
193                                const struct val_labs *vls)
194 {
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);
198
199   var_set_value_labels (pv->v, vls);
200
201   psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
202   return TRUE;
203 }
204
205 gboolean 
206 psppire_variable_set_missing(const struct PsppireVariable *pv,
207                           const struct missing_values *miss)
208 {
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);
212
213   var_set_missing_values (pv->v, miss);
214
215   psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
216   return TRUE;
217 }
218
219 gboolean
220 psppire_variable_set_write_spec(const struct PsppireVariable *pv, struct fmt_spec fmt)
221 {
222   g_return_val_if_fail(pv, FALSE);
223   g_return_val_if_fail(pv->v, FALSE);
224
225   var_set_write_format (pv->v, &fmt);
226
227   psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
228   return TRUE;
229 }
230
231 gboolean
232 psppire_variable_set_print_spec(const struct PsppireVariable *pv, struct fmt_spec fmt)
233 {
234   g_return_val_if_fail(pv, FALSE);
235   g_return_val_if_fail(pv->v, FALSE);
236
237   var_set_print_format (pv->v, &fmt);
238
239   psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
240   return TRUE;
241 }
242
243
244
245 gboolean
246 psppire_variable_set_alignment(struct PsppireVariable *pv, gint align)
247 {
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);
251
252   var_set_alignment (pv->v, align);
253
254   psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
255   return TRUE;
256 }
257
258
259 gboolean
260 psppire_variable_set_measure(struct PsppireVariable *pv, gint measure)
261 {
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);
265
266   var_set_measure (pv->v, measure + 1);
267
268   psppire_dict_var_changed(pv->dict, var_get_dict_index (pv->v));
269   return TRUE;
270 }
271
272
273 const struct fmt_spec *
274 psppire_variable_get_write_spec(const struct PsppireVariable *pv)
275 {
276   g_return_val_if_fail(pv, NULL);
277   g_return_val_if_fail(pv->v, NULL);
278
279   return var_get_write_format (pv->v);
280 }
281
282
283 const gchar *
284 psppire_variable_get_name(const struct PsppireVariable *pv)
285 {
286   g_return_val_if_fail(pv, NULL);
287   g_return_val_if_fail(pv->v, NULL);
288
289   return var_get_name (pv->v);
290 }
291
292
293 gint
294 psppire_variable_get_columns(const struct PsppireVariable *pv)
295 {
296   g_return_val_if_fail(pv, -1);
297   g_return_val_if_fail(pv->v, -1);
298
299   return var_get_display_width (pv->v);
300 }
301
302
303
304 const gchar *
305 psppire_variable_get_label(const struct PsppireVariable *pv)
306 {
307   g_return_val_if_fail(pv, NULL);
308   g_return_val_if_fail(pv->v, NULL);
309
310   return var_get_label (pv->v);
311 }
312
313
314 const struct missing_values *
315 psppire_variable_get_missing(const struct PsppireVariable *pv)
316 {
317   g_return_val_if_fail(pv, NULL);
318   g_return_val_if_fail(pv->v, NULL);
319
320   return var_get_missing_values (pv->v);
321 }
322
323
324 const struct val_labs *
325 psppire_variable_get_value_labels(const struct PsppireVariable *pv)
326 {
327   g_return_val_if_fail(pv, NULL);
328   g_return_val_if_fail(pv->v, NULL);
329
330   return var_get_value_labels (pv->v);
331 }
332
333
334 gint
335 psppire_variable_get_alignment(const struct PsppireVariable *pv)
336 {
337   g_return_val_if_fail(pv, -1);
338   g_return_val_if_fail(pv->v, -1);
339
340   return var_get_alignment (pv->v);
341 }
342
343
344
345 gint
346 psppire_variable_get_measure(const struct PsppireVariable *pv)
347 {
348   g_return_val_if_fail(pv, -1);
349   g_return_val_if_fail(pv->v, -1);
350
351   return var_get_measure (pv->v) - 1;
352 }
353
354 gint
355 psppire_variable_get_type(const struct PsppireVariable *pv)
356 {
357   g_return_val_if_fail(pv, -1);
358   g_return_val_if_fail(pv->v, -1);
359
360   return var_get_type (pv->v);
361 }
362
363
364 gint
365 psppire_variable_get_width(const struct PsppireVariable *pv)
366 {
367   g_return_val_if_fail(pv, -1);
368   g_return_val_if_fail(pv->v, -1);
369
370   return var_get_width (pv->v);
371 }
372
373
374 gint
375 psppire_variable_get_fv(const struct PsppireVariable *pv)
376 {
377   g_return_val_if_fail(pv, -1);
378   g_return_val_if_fail(pv->v, -1);
379
380   return var_get_case_index (pv->v);
381 }
382
383
384
385 gint
386 psppire_variable_get_index(const struct PsppireVariable *pv)
387 {
388   g_return_val_if_fail(pv, -1);
389   g_return_val_if_fail(pv->v, -1);
390
391   return var_get_dict_index (pv->v);
392 }
393