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