1 /* ATK - Accessibility Toolkit
2 * Copyright 2001 Sun Microsystems Inc.
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.
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.
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.
21 #include "atkmarshal.c"
24 static void atk_util_class_init (AtkUtilClass *klass);
26 static AtkObject *previous_focus_object = NULL;
28 typedef struct _AtkUtilListenerInfo AtkUtilListenerInfo;
29 struct _AtkUtilListenerInfo
35 static GHashTable *listener_list = NULL;
38 atk_util_get_type (void)
40 static GType type = 0;
44 static const GTypeInfo typeInfo =
46 sizeof (AtkUtilClass),
48 (GBaseFinalizeFunc) NULL,
49 (GClassInitFunc) atk_util_class_init,
50 (GClassFinalizeFunc) NULL,
54 (GInstanceInitFunc) NULL,
56 type = g_type_register_static (G_TYPE_OBJECT, "AtkUtil", &typeInfo, 0) ;
62 * This file supports the addition and removal of multiple focus handlers
63 * as long as they are all called in the same thread.
65 static AtkEventListenerInit focus_tracker_init = (AtkEventListenerInit) NULL;
67 static gboolean init_done = FALSE;
70 * Array of FocusTracker structs
72 static GArray *trackers = NULL;
73 static guint global_index = 0;
75 typedef struct _FocusTracker FocusTracker;
77 struct _FocusTracker {
79 AtkEventListener func;
83 * atk_focus_tracker_init:
84 * @init: Function to be called for focus tracker initialization
86 * Specifies the function to be called for focus tracker initialization.
87 * This function should be called by an implementation of the
88 * ATK interface if any specific work needs to be done to enable
92 atk_focus_tracker_init (AtkEventListenerInit init)
94 if (!focus_tracker_init)
95 focus_tracker_init = init;
99 * atk_add_focus_tracker:
100 * @focus_tracker: Function to be added to the list of functions to be called
101 * when an object receives focus.
103 * Adds the specified function to the list of functions to be called
104 * when an object receives focus.
106 * Returns: added focus tracker id, or 0 on failure.
109 atk_add_focus_tracker (AtkEventListener focus_tracker)
111 g_return_val_if_fail (focus_tracker, 0);
115 if (focus_tracker_init)
117 focus_tracker_init ();
119 trackers = g_array_sized_new (FALSE, TRUE, sizeof (FocusTracker), 0);
126 item.index = ++global_index;
127 item.func = focus_tracker;
128 trackers = g_array_append_val (trackers, item);
138 * atk_remove_focus_tracker:
139 * @tracker_id: the id of the focus tracker to remove
141 * Removes the specified focus tracker from the list of functions
142 * to be called when any object receives focus.
145 atk_remove_focus_tracker (guint tracker_id)
150 if (trackers == NULL)
156 for (i = 0; i < trackers->len; i++)
158 item = &g_array_index (trackers, FocusTracker, i);
159 if (item->index == tracker_id)
161 trackers = g_array_remove_index (trackers, i);
168 * atk_focus_tracker_notify:
169 * @object: an #AtkObject
171 * Cause the focus tracker functions which have been specified to be
172 * executed for the object.
175 atk_focus_tracker_notify (AtkObject *object)
180 if (trackers == NULL)
183 if (object == previous_focus_object)
187 if (previous_focus_object)
188 g_object_unref (previous_focus_object);
190 previous_focus_object = object;
193 g_object_ref (object);
195 for (i = 0; i < trackers->len; i++)
197 item = &g_array_index (trackers, FocusTracker, i);
198 g_return_if_fail (item != NULL);
207 add_listener (GSignalEmissionHook listener,
208 const gchar *object_type,
209 const gchar *signal_name,
210 const gchar *detail_string,
211 const gchar *hook_data)
216 static gint listener_idx = 1;
217 GQuark detail_quark = 0;
219 type = g_type_from_name (object_type);
222 signal_id = g_signal_lookup (signal_name, type);
223 g_print ("[atk-util] detail string == %s\n", detail_string);
224 detail_quark = g_quark_from_string (detail_string);
228 AtkUtilListenerInfo *listener_info;
232 listener_info = g_new (AtkUtilListenerInfo, 1);
233 listener_info->key = listener_idx;
234 listener_info->hook_id =
235 g_signal_add_emission_hook (signal_id, detail_quark, listener,
236 g_strdup (hook_data),
237 (GDestroyNotify) g_free);
238 listener_info->signal_id = signal_id;
240 g_hash_table_insert(listener_list, &(listener_info->key), listener_info);
245 g_debug ("Signal type %s not supported\n", signal_name);
250 g_warning("Invalid object type %s\n", object_type);
256 atk_util_real_add_global_event_listener (GSignalEmissionHook listener,
257 const gchar *event_type)
260 gchar **split_string;
263 split_string = g_strsplit (event_type, ":", 0);
264 length = g_strv_length (split_string);
266 if ((length == 3) || (length == 4))
267 rc = add_listener (listener, split_string[1], split_string[2],
268 split_string[3], event_type);
270 g_strfreev (split_string);
276 atk_util_real_remove_global_event_listener (guint remove_listener)
278 if (remove_listener > 0)
280 AtkUtilListenerInfo *listener_info;
281 gint tmp_idx = remove_listener;
283 listener_info = (AtkUtilListenerInfo *)
284 g_hash_table_lookup(listener_list, &tmp_idx);
286 if (listener_info != NULL)
288 /* Hook id of 0 and signal id of 0 are invalid */
289 if (listener_info->hook_id != 0 && listener_info->signal_id != 0)
291 /* Remove the emission hook */
292 g_signal_remove_emission_hook(listener_info->signal_id,
293 listener_info->hook_id);
295 /* Remove the element from the hash */
296 g_hash_table_remove(listener_list, &tmp_idx);
300 g_warning("Invalid listener hook_id %ld or signal_id %d\n",
301 listener_info->hook_id, listener_info->signal_id);
306 g_warning("No listener with the specified listener id %d",
312 g_warning("Invalid listener_id %d", remove_listener);
318 * atk_add_global_event_listener:
319 * @listener: the listener to notify
320 * @event_type: the type of event for which notification is requested
322 * Adds the specified function to the list of functions to be called
323 * when an ATK event of type event_type occurs.
325 * The format of event_type is the following:
326 * "ATK:<atk_type>:<atk_event>:<atk_event_detail>
328 * Where "ATK" works as the namespace, <atk_interface> is the name of
329 * the ATK type (interface or object), <atk_event> is the name of the
330 * signal defined on that interface and <atk_event_detail> is the
331 * gsignal detail of that signal. You can find more info about gsignal
333 * http://developer.gnome.org/gobject/stable/gobject-Signals.html
335 * The first three parameters are mandatory. The last one is optional.
338 * ATK:AtkObject:state-change
339 * ATK:AtkText:text-selection-changed
340 * ATK:AtkText:text-insert:system
342 * Toolkit implementor note: Atk provides a default implementation for
343 * this virtual method, and that implementation should be enough for
344 * most of the cases. You should have a really good reason to
345 * reimplement this method.
347 * Returns: added event listener id, or 0 on failure.
350 atk_add_global_event_listener (GSignalEmissionHook listener,
351 const gchar *event_type)
354 AtkUtilClass *klass = g_type_class_ref (ATK_TYPE_UTIL);
356 if (klass->add_global_event_listener)
358 retval = klass->add_global_event_listener (listener, event_type);
364 g_type_class_unref (klass);
370 * atk_remove_global_event_listener:
371 * @listener_id: the id of the event listener to remove
373 * @listener_id is the value returned by #atk_add_global_event_listener
374 * when you registered that event listener.
376 * Toolkit implementor note: Atk provides a default implementation for
377 * this virtual method, and that implementation should be enough for
378 * most of the cases. You should have a really good reason to
379 * reimplement this method.
381 * Removes the specified event listener
384 atk_remove_global_event_listener (guint listener_id)
386 AtkUtilClass *klass = g_type_class_peek (ATK_TYPE_UTIL);
388 if (klass && klass->remove_global_event_listener)
389 klass->remove_global_event_listener (listener_id);
393 * atk_add_key_event_listener:
394 * @listener: the listener to notify
395 * @data: a #gpointer that points to a block of data that should be sent to the registered listeners,
396 * along with the event notification, when it occurs.
398 * Adds the specified function to the list of functions to be called
399 * when a key event occurs. The @data element will be passed to the
400 * #AtkKeySnoopFunc (@listener) as the @func_data param, on notification.
402 * Returns: added event listener id, or 0 on failure.
405 atk_add_key_event_listener (AtkKeySnoopFunc listener, gpointer data)
408 AtkUtilClass *klass = g_type_class_peek (ATK_TYPE_UTIL);
409 if (klass && klass->add_key_event_listener)
411 retval = klass->add_key_event_listener (listener, data);
422 * atk_remove_key_event_listener:
423 * @listener_id: the id of the event listener to remove
425 * @listener_id is the value returned by #atk_add_key_event_listener
426 * when you registered that event listener.
428 * Removes the specified event listener.
431 atk_remove_key_event_listener (guint listener_id)
433 AtkUtilClass *klass = g_type_class_peek (ATK_TYPE_UTIL);
435 if (klass->remove_key_event_listener)
436 klass->remove_key_event_listener (listener_id);
442 * Gets the root accessible container for the current application.
444 * Returns: (transfer none): the root accessible container for the current
450 AtkUtilClass *klass = g_type_class_ref (ATK_TYPE_UTIL);
454 retval = klass->get_root ();
460 g_type_class_unref (klass);
466 * atk_get_focus_object:
468 * Gets the currently focused object.
472 * Returns: (transfer none): the currently focused object for the current
476 atk_get_focus_object (void)
478 return previous_focus_object;
482 * atk_get_toolkit_name:
484 * Gets name string for the GUI toolkit implementing ATK for this application.
486 * Returns: name string for the GUI toolkit implementing ATK for this application
489 atk_get_toolkit_name (void)
492 AtkUtilClass *klass = g_type_class_ref (ATK_TYPE_UTIL);
493 if (klass->get_toolkit_name)
495 retval = klass->get_toolkit_name ();
501 g_type_class_unref (klass);
507 * atk_get_toolkit_version:
509 * Gets version string for the GUI toolkit implementing ATK for this application.
511 * Returns: version string for the GUI toolkit implementing ATK for this application
514 atk_get_toolkit_version (void)
517 AtkUtilClass *klass = g_type_class_ref (ATK_TYPE_UTIL);
518 if (klass->get_toolkit_version)
520 retval = klass->get_toolkit_version ();
526 g_type_class_unref (klass);
534 * Gets the current version for ATK.
536 * Returns: version string for ATK
541 atk_get_version (void)
547 atk_util_class_init (AtkUtilClass *klass)
549 klass->add_global_event_listener = atk_util_real_add_global_event_listener;
550 klass->remove_global_event_listener = atk_util_real_remove_global_event_listener;
551 klass->get_root = NULL;
552 klass->get_toolkit_name = NULL;
553 klass->get_toolkit_version = NULL;
555 listener_list = g_hash_table_new_full (g_int_hash, g_int_equal, NULL,