Add "dictionary" property to PsppireVarStore and use it.
[pspp-builds.git] / src / ui / gui / psppire-var-ptr.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <config.h>
19 #include <data/variable.h>
20 #include "psppire-var-ptr.h"
21
22
23 /* This module registers a type with Glib to hold pointers
24    to a {struct variable}.  It also registers some tranformation functions so
25    that variables may be converted to strings and ints.
26    Note that the type is just a pointer.  It's the user's responsibility to
27    ensure that it always points to something valid.
28 */
29
30
31 /* Shallow copy the pointer */
32 static gpointer
33 variable_copy (gpointer var)
34 {
35   return var;
36 }
37
38 /* Do nothing. It's a pointer only! */
39 static void
40 variable_free (gpointer var)
41 {
42 }
43
44
45 /* Convert to a string, by using the variable's name */
46 static void
47 variable_to_string (const GValue *src,
48                     GValue *dest)
49 {
50   const struct variable *v = g_value_get_boxed (src);
51
52   if ( v == NULL)
53     g_value_set_string (dest, "");
54   else
55     g_value_set_string (dest, var_get_name (v));
56 }
57
58
59 /* Convert to an int, using the dictionary index. */
60 static void
61 variable_to_int (const GValue *src,
62                  GValue *dest)
63 {
64   const struct variable *v = g_value_get_boxed (src);
65
66   if ( v == NULL)
67     g_value_set_int (dest, -1);
68   else
69     g_value_set_int (dest, var_get_dict_index (v));
70 }
71
72
73
74
75 GType
76 psppire_var_ptr_get_type (void)
77 {
78   static GType t = 0;
79
80   if (t == 0 )
81     {
82       t = g_boxed_type_register_static  ("psppire-var-ptr",
83                                          (GBoxedCopyFunc) variable_copy,
84                                          (GBoxedFreeFunc) variable_free);
85
86       g_value_register_transform_func (t, G_TYPE_STRING,
87                                        variable_to_string);
88
89       g_value_register_transform_func (t, G_TYPE_INT,
90                                        variable_to_int);
91
92     }
93
94   return t;
95 }
96
97