Added (missing) functions atk_add_key_listener() and atk_remove_key_listener()
[platform/upstream/atk.git] / atk / atkutil.c
1 /* ATK -  Accessibility Toolkit
2  * Copyright 2001 Sun Microsystems Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "atkutil.h"
21 #include "atkmarshal.c"
22
23
24 static void atk_util_class_init (AtkUtilClass *klass);
25
26 GType
27 atk_util_get_type (void)
28 {
29   static GType type = 0;
30
31   if (!type)
32     {
33       static const GTypeInfo typeInfo =
34       {
35         sizeof (AtkUtilClass),
36         (GBaseInitFunc) NULL,
37         (GBaseFinalizeFunc) NULL,
38         (GClassInitFunc) atk_util_class_init,
39         (GClassFinalizeFunc) NULL,
40         NULL,
41         sizeof (AtkUtil),
42         0,
43         (GInstanceInitFunc) NULL,
44       } ;
45       type = g_type_register_static (G_TYPE_OBJECT, "AtkUtil", &typeInfo, 0) ;
46     }
47   return type;
48 }
49
50 static void
51 atk_util_class_init (AtkUtilClass *klass)
52 {
53   klass->add_global_event_listener = NULL;
54   klass->remove_global_event_listener = NULL;
55   klass->get_root = NULL;
56   klass->get_toolkit_name = NULL;
57   klass->get_toolkit_version = NULL;
58 }
59
60 /*
61  * This file supports the addition and removal of multiple focus handlers
62  * as long as they are all called in the same thread.
63  */
64 static AtkEventListenerInit  focus_tracker_init = (AtkEventListenerInit) NULL;
65
66 static gboolean init_done = FALSE;
67
68 /*
69  * Array of FocusTracker structs
70  */
71 static GArray *trackers = NULL;
72 static guint  index = 0;
73
74 typedef struct _FocusTracker FocusTracker;
75
76 struct _FocusTracker {
77   guint index;
78   AtkEventListener func;
79 };
80
81 /**
82  * atk_focus_tracker_init:
83  * @add_function: Function to be called for focus tracker initialization
84  *
85  * Specifies the function to be called for focus tracker initialization.
86  * This function should be called by an implementation of the
87  * ATK interface if any specific work needs to be done to enable
88  * focus tracking.
89  **/
90 void
91 atk_focus_tracker_init (AtkEventListenerInit    init)
92 {
93   if (!focus_tracker_init)
94     focus_tracker_init = init;
95 }
96
97 /**
98  * atk_add_focus_tracker:
99  * @focus_tracker: Function to be added to the list of functions to be called
100  * when an object receives focus.
101  *
102  * Adds the specified function to the list of functions to be called
103  * when an object receives focus.
104  *
105  * Returns: added focus tracker id, or 0 on failure.
106  **/
107 guint
108 atk_add_focus_tracker (AtkEventListener   focus_tracker)
109 {
110   g_return_val_if_fail (focus_tracker, 0);
111
112   if (!init_done)
113   {
114     if (focus_tracker_init)
115     {
116       focus_tracker_init ();
117     }
118     trackers = g_array_sized_new (FALSE, TRUE, sizeof (FocusTracker), 0);
119     init_done = TRUE;
120   }
121   if (init_done)
122   {
123     FocusTracker item;
124
125     item.index = ++index;
126     item.func = focus_tracker;
127     trackers = g_array_append_val (trackers, item);
128     return index;
129   }
130   else
131   {
132     return 0;
133   }
134 }
135
136 /**
137  * atk_remove_focus_tracker:
138  * @tracker_id: the id of the focus tracker to remove
139  *
140  * Removes the specified focus tracker from the list of functions
141  * to be called when any object receives focus.
142  **/
143 void
144 atk_remove_focus_tracker (guint            tracker_id)
145 {
146   FocusTracker *item;
147   guint i;
148
149   if (trackers == NULL)
150     return;
151
152   if (tracker_id == 0)
153     return;
154
155   for (i = 0; i < trackers->len; i++)
156   {
157     item = &g_array_index (trackers, FocusTracker, i);
158     if (item->index == tracker_id)
159     {
160       trackers = g_array_remove_index (trackers, i);
161       break;
162     }
163   }
164 }
165
166 /**
167  * atk_focus_tracker_notify:
168  * @object: an #AtkObject
169  *
170  * Cause the focus tracker functions which have been specified to be
171  * executed for the object.
172  **/
173 void
174 atk_focus_tracker_notify (AtkObject       *object)
175 {
176   FocusTracker *item;
177   guint i;
178
179   if (trackers == NULL)
180     return;
181
182   for (i = 0; i < trackers->len; i++)
183   {
184     item = &g_array_index (trackers, FocusTracker, i);
185     g_return_if_fail (item != NULL);
186     item->func (object);
187   }
188 }
189
190 /**
191  * atk_add_global_event_listener:
192  * @listener: the listener to notify
193  * @event_type: the type of event for which notification is requested
194  *
195  * Adds the specified function to the list of functions to be called
196  * when an event of type event_type occurs.
197  *
198  * Returns: added event listener id, or 0 on failure.
199  **/
200 guint
201 atk_add_global_event_listener (GSignalEmissionHook listener, gchar* event_type)
202 {
203   guint retval;
204   AtkUtilClass *klass = g_type_class_ref (ATK_TYPE_UTIL);
205   if (klass->add_global_event_listener)
206     {
207       retval = klass->add_global_event_listener (listener, event_type);
208     }
209   else
210     {
211       retval = -1;
212     }
213   g_type_class_unref (klass);
214
215   return retval;
216 }
217
218 /**
219  * atk_remove_global_event_listener:
220  * @listener_id: the id of the event listener to remove
221  *
222  * Removes the specified event listener
223  **/
224 void
225 atk_remove_global_event_listener (guint listener_id)
226 {
227   AtkUtilClass *klass = g_type_class_peek (ATK_TYPE_UTIL);
228
229   if (klass->remove_global_event_listener)
230     klass->remove_global_event_listener (listener_id);
231 }
232
233 /**
234  * atk_add_key_event_listener:
235  * @listener: the listener to notify
236  * @event_type: the type of event for which notification is requested
237  *
238  * Adds the specified function to the list of functions to be called
239  * when an event of type event_type occurs.
240  *
241  * Returns: added event listener id, or 0 on failure.
242  **/
243 guint
244 atk_add_key_event_listener (AtkKeySnoopFunc *listener, gpointer data)
245 {
246   guint retval;
247   AtkUtilClass *klass = g_type_class_ref (ATK_TYPE_UTIL);
248   if (klass->add_key_event_listener)
249     {
250       retval = klass->add_key_event_listener (listener, data);
251     }
252   else
253     {
254       retval = -1;
255     }
256   g_type_class_unref (klass);
257
258   return retval;
259 }
260
261 /**
262  * atk_remove_key_event_listener:
263  * @listener_id: the id of the event listener to remove
264  *
265  * Removes the specified event listener
266  **/
267 void
268 atk_remove_key_event_listener (guint listener_id)
269 {
270   AtkUtilClass *klass = g_type_class_peek (ATK_TYPE_UTIL);
271
272   if (klass->remove_key_event_listener)
273     klass->remove_key_event_listener (listener_id);
274 }
275
276 /**
277  * atk_get_root:
278  *
279  * Gets the root accessible container for the current application.
280  *
281  * Returns: the root accessible container for the current application
282  **/
283 AtkObject*
284 atk_get_root(void)
285 {
286   AtkUtilClass *klass = g_type_class_peek (ATK_TYPE_UTIL);
287   if (klass->get_root)
288     {
289       return klass->get_root ();
290     }
291   else
292     {
293       return NULL;
294     }
295 }
296
297 /**
298  * atk_get_toolkit_name:
299  *
300  * Gets name string for the GUI toolkit implementing ATK for this application.
301  *
302  * Returns: name string for the GUI toolkit implementing ATK for this application
303  **/
304 gchar* atk_get_toolkit_name(void)
305 {
306   AtkUtilClass *klass = g_type_class_peek (ATK_TYPE_UTIL);
307   if (klass->get_toolkit_name)
308     {
309       return klass->get_toolkit_name ();
310     }
311   else
312     {
313       return NULL;
314     }
315 }
316
317 /**
318  * atk_get_toolkit_version:
319  *
320  * Gets version string for the GUI toolkit implementing ATK for this application.
321  *
322  * Returns: version string for the GUI toolkit implementing ATK for this application
323  **/
324 gchar*
325 atk_get_toolkit_version(void)
326 {
327   AtkUtilClass *klass = g_type_class_peek (ATK_TYPE_UTIL);
328   if (klass->get_toolkit_version)
329     {
330       return klass->get_toolkit_version ();
331     }
332   else
333     {
334       return NULL;
335     }
336 }
337