Added "#include <config.h>" to some source files just for completeness/style.
[platform/core/uifw/at-spi2-atk.git] / libspi / util.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 #include <config.h>
25 #include <glib/gmessages.h>
26 #include <glib/gslist.h>
27 #include <Accessibility.h>
28
29 #include "spi-private.h"
30
31 typedef struct {
32         GList **list;
33         GList  *iterator;
34 } Iteration;
35
36 static GSList *working_list = NULL; /* of Iteration */
37
38 static char *spi_atk_bridge_null_string = "";
39
40 Accessibility_Role spi_accessible_role_from_atk_role (AtkRole role);
41
42 Accessibility_Role
43 spi_role_from_atk_role (AtkRole role)
44 {
45     return spi_accessible_role_from_atk_role (role);
46 }
47
48 /*
49  *   deletes an element from the list - in a re-entrant
50  * safe fashion; advances the element pointer to the next
51  * element.
52  */
53 void
54 spi_re_entrant_list_delete_link (GList * const *element_ptr)
55 {
56   GSList    *l;
57   GList     *next;
58   GList     *element;
59   gboolean   first_item;
60
61   g_return_if_fail (element_ptr != NULL);
62
63   element = *element_ptr;
64   g_return_if_fail (element != NULL);
65
66   next = element->next;
67   first_item = (element->prev == NULL);
68
69   g_list_remove_link (NULL, element);
70
71   for (l = working_list; l; l = l->next)
72     {
73        Iteration *i = l->data;
74
75        if (i->iterator == element)
76          {
77            i->iterator = next;
78          }
79
80        if (first_item && *(i->list) == element)
81          {
82            *(i->list) = next;
83          }
84     }
85
86   g_list_free_1 (element);
87 }
88
89 void
90 spi_re_entrant_list_foreach (GList         **list,
91                              SpiReEntrantFn  func,
92                              gpointer        user_data)
93 {
94         Iteration i;
95
96         if (!list || !*list)
97           {
98             return;
99           }
100
101         i.list = list;
102         i.iterator = *list;
103
104         working_list = g_slist_prepend (working_list, &i);
105
106         while (i.iterator) {
107                 GList *l = i.iterator;
108
109                 func (&i.iterator, user_data);
110
111                 if (i.iterator == l)
112                         i.iterator = i.iterator->next;
113         }
114
115         working_list = g_slist_remove (working_list, &i);
116 }
117
118 void 
119 spi_init_any_nil (CORBA_any *any_details, 
120                   Accessibility_Application app, 
121                   Accessibility_Role role,
122                   CORBA_string name)
123 {
124   Accessibility_EventDetails *details = Accessibility_EventDetails__alloc();
125
126   any_details->_type = TC_Accessibility_EventDetails;
127   any_details->_value = details;
128   any_details->_release = TRUE;
129
130   details->host_application = app;
131   details->source_role = role;
132   details->source_name = CORBA_string_dup (name);
133
134   details->any_data._type = TC_null;
135   details->any_data._value = NULL;
136   details->any_data._release = TRUE;
137 }
138
139 void 
140 spi_init_any_object (CORBA_any *any_details, Accessibility_Application app, 
141                      Accessibility_Role role,
142                      CORBA_string name, 
143                      CORBA_Object *o)
144 {
145   Accessibility_EventDetails *details = Accessibility_EventDetails__alloc();
146
147   any_details->_type = TC_Accessibility_EventDetails;
148   any_details->_value = details;
149   any_details->_release = TRUE;
150
151   details->host_application = app;
152   details->source_role = role;
153   details->source_name = CORBA_string_dup (name);
154   
155   details->any_data._type = TC_CORBA_Object;
156   details->any_data._value = o;
157   details->any_data._release = TRUE;
158 }
159
160 void
161 spi_init_any_string (CORBA_any *any_details, Accessibility_Application app, 
162                      Accessibility_Role role,
163                      CORBA_string name, 
164                      char **string_pointer)
165 {  
166   Accessibility_EventDetails *details = Accessibility_EventDetails__alloc();
167
168   any_details->_type = TC_Accessibility_EventDetails;
169   any_details->_value = details;
170   any_details->_release = TRUE;
171
172   details->host_application = app;
173   details->source_role = role;
174   details->source_name = CORBA_string_dup (name);
175   
176   details->any_data._type = (CORBA_TypeCode) TC_CORBA_string;
177   if (string_pointer && *string_pointer)
178     details->any_data._value = string_pointer;
179   else
180     details->any_data._value = &spi_atk_bridge_null_string;
181   details->any_data._release = FALSE;
182 }
183
184 void
185 spi_init_any_rect (CORBA_any *any_details, 
186                    Accessibility_Application app, 
187                    Accessibility_Role role,
188                    CORBA_string name, 
189                    AtkRectangle *rect)
190 {
191   Accessibility_EventDetails *details = Accessibility_EventDetails__alloc();
192   Accessibility_BoundingBox *box = Accessibility_BoundingBox__alloc ();
193
194   any_details->_type = TC_Accessibility_EventDetails;
195   any_details->_value = details;
196   any_details->_release = TRUE;
197
198   details->host_application = app;
199   details->source_role = role;
200   details->source_name = CORBA_string_dup (name);
201   
202   box->x = rect->x;
203   box->y = rect->y;
204   box->width = rect->width;
205   box->height = rect->height;
206   details->any_data._type = TC_Accessibility_BoundingBox;
207   details->any_data._value = box;
208   details->any_data._release = TRUE;
209 }