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