Modified Value.idl interface to use unions, removes the assumption that
[platform/core/uifw/at-spi2-atk.git] / libspi / value.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Sun Microsystems Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* value.c : implements the Value interface */
24 #include <config.h>
25 #include <stdio.h>
26 #include <libspi/value.h>
27
28 #define PARENT_TYPE SPI_TYPE_BASE
29
30 /* Static function declarations */
31
32 static Accessibility_SValue
33 gvalue_to_svalue (GValue *gvalue);
34 static void
35 spi_value_class_init (SpiValueClass *klass);
36 static void
37 spi_value_init (SpiValue *value);
38 static Accessibility_SValue
39 impl__get_minimumValue (PortableServer_Servant _servant,
40                         CORBA_Environment *    ev);
41 static Accessibility_SValue
42 impl__get_maximumValue (PortableServer_Servant _servant,
43                         CORBA_Environment *    ev);
44 static Accessibility_SValue
45 impl__get_currentValue (PortableServer_Servant _servant,
46                         CORBA_Environment *    ev);
47 static void 
48 impl__set_currentValue (PortableServer_Servant       _servant,
49                         const Accessibility_SValue * value,
50                         CORBA_Environment *          ev);
51
52
53 BONOBO_TYPE_FUNC_FULL (SpiValue,
54                        Accessibility_Value,
55                        PARENT_TYPE,
56                        spi_value);
57
58
59 static void
60 spi_value_class_init (SpiValueClass *klass)
61 {
62   POA_Accessibility_Value__epv *epv = &klass->epv;
63
64   /* Initialize epv table */
65
66   epv->_get_minimumValue = impl__get_minimumValue;
67   epv->_get_maximumValue = impl__get_maximumValue;
68   epv->_get_currentValue = impl__get_currentValue;
69   epv->_set_currentValue = impl__set_currentValue;
70 }
71
72
73 static void
74 spi_value_init (SpiValue *value)
75 {
76 }
77
78
79 SpiValue *
80 spi_value_interface_new (AtkObject *obj)
81 {
82   SpiValue *new_value = g_object_new (SPI_VALUE_TYPE, NULL);
83
84   spi_base_construct (SPI_BASE (new_value), obj);
85
86   return new_value;
87 }
88
89
90 static AtkValue *
91 get_value_from_servant (PortableServer_Servant servant)
92 {
93   SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
94
95   if (!object)
96     {
97       return NULL;
98     }
99
100   return ATK_VALUE (object->atko);
101 }
102
103 static Accessibility_SValue
104 gvalue_to_svalue (GValue *gvalue)
105 {
106   Accessibility_SValue sval;
107   if (G_VALUE_HOLDS_DOUBLE (gvalue))
108   {
109       sval._d = Accessibility_DOUBLEVAL;
110       sval._u.val_double = g_value_get_double (gvalue);
111   }
112   else if (G_VALUE_HOLDS_FLOAT (gvalue))
113   {
114       sval._d = Accessibility_FLOATVAL;
115       sval._u.val_float = g_value_get_float (gvalue);
116   }
117   else if (G_VALUE_HOLDS_ULONG (gvalue))
118   {
119       sval._d = Accessibility_ULONGVAL;
120       sval._u.val_ulong = g_value_get_ulong (gvalue); 
121   }
122   else if (G_VALUE_HOLDS_LONG (gvalue))
123   {
124       sval._d = Accessibility_LONGVAL;
125       sval._u.val_long = g_value_get_long (gvalue);
126   }
127   else if (G_VALUE_HOLDS_UINT (gvalue))
128   {
129       sval._d = Accessibility_USHORTVAL;
130       sval._u.val_ushort = CLAMP (g_value_get_uint (gvalue), 0, G_MAXUSHORT);
131   }
132   else if (G_VALUE_HOLDS_INT (gvalue))
133   {
134       sval._d = Accessibility_SHORTVAL;
135       sval._u.val_short = CLAMP (g_value_get_int (gvalue), G_MINSHORT, G_MAXSHORT);
136   }
137   else
138   {
139       sval._d = Accessibility_SHORTVAL;
140       sval._u.val_short = 0;
141   }
142   return sval;
143 }
144
145 static void
146 gvalue_set_from_svalue (GValue *gvalue, Accessibility_SValue *sval)
147 {
148   switch (sval->_d)
149     {
150       case Accessibility_DOUBLEVAL:
151         g_value_init (gvalue, G_TYPE_DOUBLE);
152         g_value_set_double (gvalue, sval->_u.val_double);
153         break;
154       case Accessibility_FLOATVAL:
155         g_value_init (gvalue, G_TYPE_FLOAT);
156         g_value_set_float (gvalue, sval->_u.val_float);
157         break;
158       case Accessibility_ULONGVAL:
159         g_value_init (gvalue, G_TYPE_ULONG);
160         g_value_set_ulong (gvalue, sval->_u.val_ulong);
161         break;
162       case Accessibility_LONGVAL:
163         g_value_init (gvalue, G_TYPE_LONG);
164         g_value_set_long (gvalue, sval->_u.val_long);
165         break;
166       case Accessibility_USHORTVAL:
167         g_value_init (gvalue, G_TYPE_UINT);
168         g_value_set_uint (gvalue, sval->_u.val_ushort);
169         break;
170       case Accessibility_SHORTVAL:
171         g_value_init (gvalue, G_TYPE_INT);
172         g_value_set_int (gvalue, sval->_u.val_short);
173         break;
174       default:
175         g_value_init (gvalue, G_TYPE_INT);
176         g_value_set_int (gvalue, 0);
177     }
178 }
179
180 static Accessibility_SValue
181 impl__get_minimumValue (PortableServer_Servant servant,
182                         CORBA_Environment     *ev)
183 {
184   GValue    gvalue = {0, };
185   AtkValue *value = get_value_from_servant (servant);
186
187   g_return_val_if_fail (value != NULL, gvalue_to_svalue (&gvalue));
188
189   g_value_init (&gvalue, G_TYPE_DOUBLE);
190   atk_value_get_minimum_value (value, &gvalue);
191
192   return gvalue_to_svalue (&gvalue);
193 }
194
195
196 static Accessibility_SValue
197 impl__get_maximumValue (PortableServer_Servant servant,
198                         CORBA_Environment     *ev)
199 {
200   GValue   gvalue = {0, };
201   AtkValue *value = get_value_from_servant (servant);
202
203   g_return_val_if_fail (value != NULL, gvalue_to_svalue (&gvalue));
204
205   g_value_init (&gvalue, G_TYPE_DOUBLE);
206   atk_value_get_maximum_value (value, &gvalue);
207
208   return gvalue_to_svalue (&gvalue);
209 }
210
211
212 static Accessibility_SValue
213 impl__get_currentValue (PortableServer_Servant servant,
214                         CORBA_Environment     *ev)
215 {
216   GValue   gvalue = {0, };
217   AtkValue *value = get_value_from_servant (servant);
218
219   g_return_val_if_fail (value != NULL, gvalue_to_svalue (&gvalue));
220
221   g_value_init (&gvalue, G_TYPE_DOUBLE);
222   atk_value_get_current_value (value, &gvalue);
223
224   return gvalue_to_svalue (&gvalue);
225 }
226
227
228 static void 
229 impl__set_currentValue (PortableServer_Servant servant,
230                         const Accessibility_SValue *value,
231                         CORBA_Environment     *ev)
232 {
233   GValue    gvalue = {0, };
234   AtkValue *avalue = get_value_from_servant (servant);
235
236   g_return_if_fail (avalue != NULL);
237
238   gvalue_set_from_svalue (&gvalue, value);
239
240   atk_value_set_current_value (avalue, &gvalue);
241 }
242
243