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