2001-12-18 Marc Mulcahy <marc.mulcahy@sun.com>
[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 double
33 get_double_from_gvalue (GValue *gvalue);
34 static void
35 gvalue_set_from_double (GValue *gvalue, double value);
36 static void
37 spi_value_class_init (SpiValueClass *klass);
38 static void
39 spi_value_init (SpiValue *value);
40 static double
41 impl__get_minimumValue (PortableServer_Servant _servant,
42                         CORBA_Environment *    ev);
43 static double
44 impl__get_maximumValue (PortableServer_Servant _servant,
45                         CORBA_Environment *    ev);
46 static double
47 impl__get_currentValue (PortableServer_Servant _servant,
48                         CORBA_Environment *    ev);
49 static void 
50 impl__set_currentValue (PortableServer_Servant _servant,
51                         const CORBA_double     value,
52                         CORBA_Environment *    ev);
53
54
55 BONOBO_TYPE_FUNC_FULL (SpiValue,
56                        Accessibility_Value,
57                        PARENT_TYPE,
58                        spi_value);
59
60
61 static void
62 spi_value_class_init (SpiValueClass *klass)
63 {
64   POA_Accessibility_Value__epv *epv = &klass->epv;
65
66   /* Initialize epv table */
67
68   epv->_get_minimumValue = impl__get_minimumValue;
69   epv->_get_maximumValue = impl__get_maximumValue;
70   epv->_get_currentValue = impl__get_currentValue;
71   epv->_set_currentValue = impl__set_currentValue;
72 }
73
74
75 static void
76 spi_value_init (SpiValue *value)
77 {
78 }
79
80
81 SpiValue *
82 spi_value_interface_new (AtkObject *obj)
83 {
84   SpiValue *new_value = g_object_new (SPI_VALUE_TYPE, NULL);
85
86   spi_base_construct (SPI_BASE (new_value), G_OBJECT(obj));
87
88   return new_value;
89 }
90
91
92 static AtkValue *
93 get_value_from_servant (PortableServer_Servant servant)
94 {
95   SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
96
97   g_return_val_if_fail (object, NULL);
98   g_return_val_if_fail (ATK_IS_OBJECT(object->gobj), NULL);
99   return ATK_VALUE (object->gobj);
100 }
101
102 static double
103 get_double_from_gvalue (GValue *gvalue)
104 {
105   double        retval = 0;
106   if (G_TYPE_IS_FUNDAMENTAL (G_VALUE_TYPE (gvalue)))
107     {
108       switch (gvalue->g_type)
109         {
110           case G_TYPE_DOUBLE:
111             retval = g_value_get_double (gvalue);
112             break;
113           case G_TYPE_FLOAT:
114             retval = g_value_get_float (gvalue);
115             break;
116           case G_TYPE_ULONG:
117             retval = g_value_get_ulong (gvalue);
118             break;
119           case G_TYPE_LONG:
120             retval = g_value_get_long (gvalue);
121             break;
122           case G_TYPE_UINT:
123             retval = g_value_get_uint (gvalue);
124             break;
125           case G_TYPE_INT:
126             retval = g_value_get_int (gvalue);
127             break;
128           case G_TYPE_UCHAR:
129             retval = g_value_get_uchar (gvalue);
130             break;
131           case G_TYPE_CHAR:
132             retval = g_value_get_char (gvalue);
133             break;
134           case G_TYPE_BOOLEAN:
135             retval = g_value_get_boolean (gvalue);
136             break;
137         }
138     }
139   else
140     {
141       g_warning ("SpiValue requested from a non-fundamental type\n");       
142     }
143   return retval;
144 }  
145
146 static void
147 gvalue_set_from_double (GValue *gvalue, double value)
148 {
149   if (G_TYPE_IS_FUNDAMENTAL (G_VALUE_TYPE (gvalue)))
150     {
151       switch (gvalue->g_type)
152         {
153           case G_TYPE_DOUBLE:
154             g_value_set_double (gvalue, value);
155             break;
156           case G_TYPE_FLOAT:
157             g_value_set_float (gvalue, value);
158             break;
159           case G_TYPE_ULONG:
160             g_value_set_ulong (gvalue, value);
161             break;
162           case G_TYPE_LONG:
163             g_value_set_long (gvalue, value);
164             break;
165           case G_TYPE_UINT:
166             g_value_set_uint (gvalue, value);
167             break;
168           case G_TYPE_INT:
169             g_value_set_int (gvalue, value);
170             break;
171           case G_TYPE_UCHAR:
172             g_value_set_uchar (gvalue, value);
173             break;
174           case G_TYPE_CHAR:
175             g_value_set_char (gvalue, value);
176             break;
177           case G_TYPE_BOOLEAN:
178             g_value_set_boolean (gvalue, ((fabs (value) > 0.5) ? 1 : 0));
179             break;
180         }
181     }
182   else
183     {
184       g_warning ("SpiValue change requested for a non-fundamental type\n");         
185     }
186 }
187
188 static double
189 impl__get_minimumValue (PortableServer_Servant servant,
190                         CORBA_Environment     *ev)
191 {
192   GValue    gvalue = {0, };
193   AtkValue *value = get_value_from_servant (servant);
194
195   g_return_val_if_fail (value != NULL, 0.0);
196
197   atk_value_get_minimum_value (value, &gvalue);
198
199   return get_double_from_gvalue (&gvalue);
200 }
201
202
203 static double
204 impl__get_maximumValue (PortableServer_Servant servant,
205                         CORBA_Environment     *ev)
206 {
207   GValue   gvalue = {0, };
208   AtkValue *value = get_value_from_servant (servant);
209
210   g_return_val_if_fail (value != NULL, 0.0);
211
212   atk_value_get_maximum_value (value, &gvalue);
213
214   return get_double_from_gvalue (&gvalue);
215 }
216
217
218 static double
219 impl__get_currentValue (PortableServer_Servant servant,
220                         CORBA_Environment     *ev)
221 {
222   GValue   gvalue = {0, };
223   AtkValue *value = get_value_from_servant (servant);
224
225   g_return_val_if_fail (value != NULL, 0.0);
226
227   atk_value_get_current_value (value, &gvalue);
228
229   return get_double_from_gvalue (&gvalue);
230 }
231
232
233 static void 
234 impl__set_currentValue (PortableServer_Servant servant,
235                         const CORBA_double     value,
236                         CORBA_Environment     *ev)
237 {
238   GValue    gvalue = { 0, };
239   AtkValue *avalue = get_value_from_servant (servant);
240
241   g_return_if_fail (avalue != NULL);
242
243   atk_value_get_current_value (avalue, &gvalue);
244   gvalue_set_from_double (&gvalue, value);
245
246   atk_value_set_current_value (avalue, &gvalue);
247 }
248
249