2008-03-31 Li Yuan <li.yuan@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, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /* value.c : implements the Value interface */
25 #include <config.h>
26 #include <math.h>
27 #include <stdio.h>
28 #include <libspi/value.h>
29
30 #define PARENT_TYPE SPI_TYPE_BASE
31
32 /* Static function declarations */
33
34 static double
35 get_double_from_gvalue (GValue *gvalue);
36 static void
37 gvalue_set_from_double (GValue *gvalue, double value);
38 static void
39 spi_value_class_init (SpiValueClass *klass);
40 static void
41 spi_value_init (SpiValue *value);
42 static double
43 impl__get_minimumValue (PortableServer_Servant _servant,
44                         CORBA_Environment *    ev);
45 static double
46 impl__get_maximumValue (PortableServer_Servant _servant,
47                         CORBA_Environment *    ev);
48 static double
49 impl__get_currentValue (PortableServer_Servant _servant,
50                         CORBA_Environment *    ev);
51 static void 
52 impl__set_currentValue (PortableServer_Servant _servant,
53                         const CORBA_double     value,
54                         CORBA_Environment *    ev);
55 static double
56 impl__get_minimumIncrement (PortableServer_Servant _servant,
57                         CORBA_Environment *    ev);
58
59
60 BONOBO_TYPE_FUNC_FULL (SpiValue,
61                        Accessibility_Value,
62                        PARENT_TYPE,
63                        spi_value)
64
65
66 static void
67 spi_value_class_init (SpiValueClass *klass)
68 {
69   POA_Accessibility_Value__epv *epv = &klass->epv;
70
71   /* Initialize epv table */
72
73   epv->_get_minimumValue = impl__get_minimumValue;
74   epv->_get_maximumValue = impl__get_maximumValue;
75   epv->_get_currentValue = impl__get_currentValue;
76   epv->_set_currentValue = impl__set_currentValue;
77   epv->_get_minimumIncrement = impl__get_minimumIncrement;
78 }
79
80
81 static void
82 spi_value_init (SpiValue *value)
83 {
84 }
85
86
87 SpiValue *
88 spi_value_interface_new (AtkObject *obj)
89 {
90   SpiValue *new_value = g_object_new (SPI_VALUE_TYPE, NULL);
91
92   spi_base_construct (SPI_BASE (new_value), G_OBJECT(obj));
93
94   return new_value;
95 }
96
97
98 static AtkValue *
99 get_value_from_servant (PortableServer_Servant servant)
100 {
101   SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
102
103   g_return_val_if_fail (object, NULL);
104   g_return_val_if_fail (ATK_IS_OBJECT(object->gobj), NULL);
105   return ATK_VALUE (object->gobj);
106 }
107
108 static double
109 get_double_from_gvalue (GValue *gvalue)
110 {
111   double        retval = 0;
112   if (G_TYPE_IS_FUNDAMENTAL (G_VALUE_TYPE (gvalue)))
113     {
114       switch (gvalue->g_type)
115         {
116           case G_TYPE_DOUBLE:
117             retval = g_value_get_double (gvalue);
118             break;
119           case G_TYPE_FLOAT:
120             retval = g_value_get_float (gvalue);
121             break;
122           case G_TYPE_ULONG:
123             retval = g_value_get_ulong (gvalue);
124             break;
125           case G_TYPE_LONG:
126             retval = g_value_get_long (gvalue);
127             break;
128           case G_TYPE_UINT:
129             retval = g_value_get_uint (gvalue);
130             break;
131           case G_TYPE_INT:
132             retval = g_value_get_int (gvalue);
133             break;
134           case G_TYPE_UCHAR:
135             retval = g_value_get_uchar (gvalue);
136             break;
137           case G_TYPE_CHAR:
138             retval = g_value_get_char (gvalue);
139             break;
140           case G_TYPE_BOOLEAN:
141             retval = g_value_get_boolean (gvalue);
142             break;
143         }
144     }
145   else
146     {
147       g_warning ("SpiValue requested from a non-fundamental type\n");       
148     }
149   return retval;
150 }  
151
152 static void
153 gvalue_set_from_double (GValue *gvalue, double value)
154 {
155   if (G_TYPE_IS_FUNDAMENTAL (G_VALUE_TYPE (gvalue)))
156     {
157       switch (gvalue->g_type)
158         {
159           case G_TYPE_DOUBLE:
160             g_value_set_double (gvalue, value);
161             break;
162           case G_TYPE_FLOAT:
163             g_value_set_float (gvalue, value);
164             break;
165           case G_TYPE_ULONG:
166             g_value_set_ulong (gvalue, value);
167             break;
168           case G_TYPE_LONG:
169             g_value_set_long (gvalue, value);
170             break;
171           case G_TYPE_UINT:
172             g_value_set_uint (gvalue, value);
173             break;
174           case G_TYPE_INT:
175             g_value_set_int (gvalue, value);
176             break;
177           case G_TYPE_UCHAR:
178             g_value_set_uchar (gvalue, value);
179             break;
180           case G_TYPE_CHAR:
181             g_value_set_char (gvalue, value);
182             break;
183           case G_TYPE_BOOLEAN:
184             g_value_set_boolean (gvalue, ((fabs (value) > 0.5) ? 1 : 0));
185             break;
186         }
187     }
188   else
189     {
190       g_warning ("SpiValue change requested for a non-fundamental type\n");         
191     }
192 }
193
194 static double
195 impl__get_minimumValue (PortableServer_Servant servant,
196                         CORBA_Environment     *ev)
197 {
198   GValue    gvalue = {0, };
199   AtkValue *value = get_value_from_servant (servant);
200
201   g_return_val_if_fail (value != NULL, 0.0);
202
203   atk_value_get_minimum_value (value, &gvalue);
204
205   return get_double_from_gvalue (&gvalue);
206 }
207
208
209 static double
210 impl__get_maximumValue (PortableServer_Servant servant,
211                         CORBA_Environment     *ev)
212 {
213   GValue   gvalue = {0, };
214   AtkValue *value = get_value_from_servant (servant);
215
216   g_return_val_if_fail (value != NULL, 0.0);
217
218   atk_value_get_maximum_value (value, &gvalue);
219
220   return get_double_from_gvalue (&gvalue);
221 }
222
223
224 static double
225 impl__get_currentValue (PortableServer_Servant servant,
226                         CORBA_Environment     *ev)
227 {
228   GValue   gvalue = {0, };
229   AtkValue *value = get_value_from_servant (servant);
230
231   g_return_val_if_fail (value != NULL, 0.0);
232
233   atk_value_get_current_value (value, &gvalue);
234
235   return get_double_from_gvalue (&gvalue);
236 }
237
238
239 static void 
240 impl__set_currentValue (PortableServer_Servant servant,
241                         const CORBA_double     value,
242                         CORBA_Environment     *ev)
243 {
244   GValue    gvalue = { 0, };
245   AtkValue *avalue = get_value_from_servant (servant);
246
247   g_return_if_fail (avalue != NULL);
248
249   atk_value_get_current_value (avalue, &gvalue);
250   gvalue_set_from_double (&gvalue, value);
251
252   atk_value_set_current_value (avalue, &gvalue);
253 }
254
255 static double
256 impl__get_minimumIncrement (PortableServer_Servant servant,
257                             CORBA_Environment     *ev)
258 {
259   GValue    gvalue = {0, };
260   AtkValue *value = get_value_from_servant (servant);
261
262   g_return_val_if_fail (value != NULL, 0.0);
263
264   atk_value_get_minimum_increment (value, &gvalue);
265
266   return get_double_from_gvalue (&gvalue);
267 }
268
269