]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gailutil.c
Merge branch 'bgo593793-filechooser-recent-folders-master'
[~andy/gtk] / gtk / a11y / gailutil.c
1 /* GAIL - The GNOME Accessibility Implementation Library
2  * Copyright 2001, 2002, 2003 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 "config.h"
21
22 #include <stdlib.h>
23 #include <gtk/gtk.h>
24 #include "gailutil.h"
25 #include "gtktoplevelaccessible.h"
26 #include "gtkwindowaccessible.h"
27
28
29 static GHashTable *listener_list = NULL;
30 static gint listener_idx = 1;
31 static GSList *key_listener_list = NULL;
32 static guint key_snooper_id = 0;
33
34 typedef struct _GailUtilListenerInfo GailUtilListenerInfo;
35 typedef struct _GailKeyEventInfo GailKeyEventInfo;
36
37 struct _GailUtilListenerInfo
38 {
39    gint key;
40    guint signal_id;
41    gulong hook_id;
42 };
43
44 struct _GailKeyEventInfo
45 {
46   AtkKeyEventStruct *key_event;
47   gpointer func_data;
48 };
49
50 static guint
51 add_listener (GSignalEmissionHook  listener,
52               const gchar         *object_type,
53               const gchar         *signal_name,
54               const gchar         *hook_data)
55 {
56   GType type;
57   guint signal_id;
58   gint  rc = 0;
59
60   type = g_type_from_name (object_type);
61   if (type)
62     {
63       signal_id  = g_signal_lookup (signal_name, type);
64       if (signal_id > 0)
65         {
66           GailUtilListenerInfo *listener_info;
67
68           rc = listener_idx;
69
70           listener_info = g_new (GailUtilListenerInfo, 1);
71           listener_info->key = listener_idx;
72           listener_info->hook_id =
73                           g_signal_add_emission_hook (signal_id, 0, listener,
74                                                       g_strdup (hook_data),
75                                                       (GDestroyNotify) g_free);
76           listener_info->signal_id = signal_id;
77
78           g_hash_table_insert (listener_list, &(listener_info->key), listener_info);
79           listener_idx++;
80         }
81       else
82         {
83           g_warning("Invalid signal type %s\n", signal_name);
84         }
85     }
86   else
87     {
88       g_warning("Invalid object type %s\n", object_type);
89     }
90   return rc;
91 }
92
93 static gboolean
94 state_event_watcher (GSignalInvocationHint *hint,
95                      guint                  n_param_values,
96                      const GValue          *param_values,
97                      gpointer               data)
98 {
99   GObject *object;
100   GtkWidget *widget;
101   AtkObject *atk_obj;
102   AtkObject *parent;
103   GdkEventWindowState *event;
104   gchar *signal_name;
105
106   object = g_value_get_object (param_values + 0);
107   if (!GTK_IS_WINDOW (object))
108     return FALSE;
109
110   event = g_value_get_boxed (param_values + 1);
111   if (event->type == GDK_WINDOW_STATE)
112     return FALSE;
113   widget = GTK_WIDGET (object);
114
115   if (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED)
116     signal_name = "maximize";
117   else if (event->new_window_state & GDK_WINDOW_STATE_ICONIFIED)
118     signal_name = "minimize";
119   else if (event->new_window_state == 0)
120     signal_name = "restore";
121   else
122     return TRUE;
123
124   atk_obj = gtk_widget_get_accessible (widget);
125   if (GTK_IS_WINDOW_ACCESSIBLE (atk_obj))
126     {
127       parent = atk_object_get_parent (atk_obj);
128       if (parent == atk_get_root ())
129         g_signal_emit_by_name (atk_obj, signal_name);
130
131       return TRUE;
132     }
133
134   return FALSE;
135 }
136
137 static gboolean
138 configure_event_watcher (GSignalInvocationHint *hint,
139                          guint                  n_param_values,
140                          const GValue          *param_values,
141                          gpointer               data)
142 {
143   GtkAllocation allocation;
144   GObject *object;
145   GtkWidget *widget;
146   AtkObject *atk_obj;
147   AtkObject *parent;
148   GdkEvent *event;
149   gchar *signal_name;
150
151   object = g_value_get_object (param_values + 0);
152   if (!GTK_IS_WINDOW (object))
153     return FALSE;
154
155   event = g_value_get_boxed (param_values + 1);
156   if (event->type != GDK_CONFIGURE)
157     return FALSE;
158   widget = GTK_WIDGET (object);
159   gtk_widget_get_allocation (widget, &allocation);
160   if (allocation.x == ((GdkEventConfigure *)event)->x &&
161       allocation.y == ((GdkEventConfigure *)event)->y &&
162       allocation.width == ((GdkEventConfigure *)event)->width &&
163       allocation.height == ((GdkEventConfigure *)event)->height)
164     return TRUE;
165
166   if (allocation.width != ((GdkEventConfigure *)event)->width ||
167       allocation.height != ((GdkEventConfigure *)event)->height)
168     signal_name = "resize";
169   else
170     signal_name = "move";
171
172   atk_obj = gtk_widget_get_accessible (widget);
173   if (GTK_IS_WINDOW_ACCESSIBLE (atk_obj))
174     {
175       parent = atk_object_get_parent (atk_obj);
176       if (parent == atk_get_root ())
177         g_signal_emit_by_name (atk_obj, signal_name);
178
179       return TRUE;
180     }
181
182   return FALSE;
183 }
184
185 static gboolean
186 window_focus (GtkWidget     *widget,
187               GdkEventFocus *event)
188 {
189   AtkObject *atk_obj;
190
191   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
192
193   atk_obj = gtk_widget_get_accessible (widget);
194   g_signal_emit_by_name (atk_obj, event->in ? "activate" : "deactivate");
195
196   return FALSE;
197 }
198
199 static void
200 window_added (AtkObject *atk_obj,
201               guint      index,
202               AtkObject *child)
203 {
204   GtkWidget *widget;
205
206   if (!GTK_IS_WINDOW_ACCESSIBLE (child))
207     return;
208
209   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (child));
210   if (!widget)
211     return;
212
213   g_signal_connect (widget, "focus-in-event", (GCallback) window_focus, NULL);
214   g_signal_connect (widget, "focus-out-event", (GCallback) window_focus, NULL);
215   g_signal_emit_by_name (child, "create");
216 }
217
218
219 static void
220 window_removed (AtkObject *atk_obj,
221                 guint      index,
222                 AtkObject *child)
223 {
224   GtkWidget *widget;
225   GtkWindow *window;
226
227   if (!GTK_IS_WINDOW_ACCESSIBLE (child))
228     return;
229
230   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (child));
231   if (!widget)
232     return;
233
234   window = GTK_WINDOW (widget);
235   /*
236    * Deactivate window if it is still focused and we are removing it. This
237    * can happen when a dialog displayed by gok is removed.
238    */
239   if (gtk_window_is_active (window) &&
240       gtk_window_has_toplevel_focus (window))
241     g_signal_emit_by_name (child, "deactivate");
242
243   g_signal_handlers_disconnect_by_func (widget, (gpointer) window_focus, NULL);
244   g_signal_emit_by_name (child, "destroy");
245 }
246
247 static void
248 do_window_event_initialization (void)
249 {
250   AtkObject *root;
251
252   g_type_class_ref (GTK_TYPE_WINDOW_ACCESSIBLE);
253   g_signal_add_emission_hook (g_signal_lookup ("window-state-event", GTK_TYPE_WIDGET),
254                               0, state_event_watcher, NULL, (GDestroyNotify) NULL);
255   g_signal_add_emission_hook (g_signal_lookup ("configure-event", GTK_TYPE_WIDGET),
256                               0, configure_event_watcher, NULL, (GDestroyNotify) NULL);
257
258   root = atk_get_root ();
259   g_signal_connect (root, "children-changed::add",
260                     (GCallback) window_added, NULL);
261   g_signal_connect (root, "children-changed::remove",
262                     (GCallback) window_removed, NULL);
263 }
264 static guint
265 gail_util_add_global_event_listener (GSignalEmissionHook  listener,
266                                      const gchar         *event_type)
267 {
268   guint rc = 0;
269   gchar **split_string;
270
271   split_string = g_strsplit (event_type, ":", 3);
272
273   if (split_string)
274     {
275       if (!g_strcmp0 ("window", split_string[0]))
276         {
277           static gboolean initialized = FALSE;
278
279           if (!initialized)
280             {
281               do_window_event_initialization ();
282               initialized = TRUE;
283             }
284           rc = add_listener (listener, "GtkWindowAccessible", split_string[1], event_type);
285         }
286       else
287         {
288           rc = add_listener (listener, split_string[1], split_string[2], event_type);
289         }
290
291       g_strfreev (split_string);
292     }
293
294   return rc;
295 }
296
297 static void
298 gail_util_remove_global_event_listener (guint remove_listener)
299 {
300   if (remove_listener > 0)
301   {
302     GailUtilListenerInfo *listener_info;
303     gint tmp_idx = remove_listener;
304
305     listener_info = (GailUtilListenerInfo *)
306       g_hash_table_lookup(listener_list, &tmp_idx);
307
308     if (listener_info != NULL)
309       {
310         /* Hook id of 0 and signal id of 0 are invalid */
311         if (listener_info->hook_id != 0 && listener_info->signal_id != 0)
312           {
313             /* Remove the emission hook */
314             g_signal_remove_emission_hook(listener_info->signal_id,
315               listener_info->hook_id);
316
317             /* Remove the element from the hash */
318             g_hash_table_remove(listener_list, &tmp_idx);
319           }
320         else
321           {
322             g_warning("Invalid listener hook_id %ld or signal_id %d\n",
323               listener_info->hook_id, listener_info->signal_id);
324           }
325       }
326     else
327       {
328         g_warning("No listener with the specified listener id %d",
329           remove_listener);
330       }
331   }
332   else
333   {
334     g_warning("Invalid listener_id %d", remove_listener);
335   }
336 }
337
338 static AtkKeyEventStruct *
339 atk_key_event_from_gdk_event_key (GdkEventKey *key)
340 {
341   AtkKeyEventStruct *event = g_new0 (AtkKeyEventStruct, 1);
342   switch (key->type)
343     {
344     case GDK_KEY_PRESS:
345             event->type = ATK_KEY_EVENT_PRESS;
346             break;
347     case GDK_KEY_RELEASE:
348             event->type = ATK_KEY_EVENT_RELEASE;
349             break;
350     default:
351             g_assert_not_reached ();
352             return NULL;
353     }
354   event->state = key->state;
355   event->keyval = key->keyval;
356   event->length = key->length;
357   if (key->string && key->string [0] &&
358       (key->state & GDK_CONTROL_MASK ||
359        g_unichar_isgraph (g_utf8_get_char (key->string))))
360     {
361       event->string = key->string;
362     }
363   else if (key->type == GDK_KEY_PRESS ||
364            key->type == GDK_KEY_RELEASE)
365     {
366       event->string = gdk_keyval_name (key->keyval);
367     }
368   event->keycode = key->hardware_keycode;
369   event->timestamp = key->time;
370 #ifdef GAIL_DEBUG
371   g_print ("GailKey:\tsym %u\n\tmods %x\n\tcode %u\n\ttime %lx\n",
372            (unsigned int) event->keyval,
373            (unsigned int) event->state,
374            (unsigned int) event->keycode,
375            (unsigned long int) event->timestamp);
376 #endif
377   return event;
378 }
379
380 typedef struct {
381   AtkKeySnoopFunc func;
382   gpointer        data;
383   guint           key;
384 } KeyEventListener;
385
386 static gint
387 gail_key_snooper (GtkWidget   *the_widget,
388                   GdkEventKey *event,
389                   gpointer     data)
390 {
391   GSList *l;
392   AtkKeyEventStruct *atk_event;
393   gboolean result;
394
395   atk_event = atk_key_event_from_gdk_event_key (event);
396
397   result = FALSE;
398
399   for (l = key_listener_list; l; l = l->next)
400     {
401       KeyEventListener *listener = l->data;
402
403       result |= listener->func (atk_event, listener->data);
404     }
405   g_free (atk_event);
406
407   return result;
408 }
409
410 static guint
411 gail_util_add_key_event_listener (AtkKeySnoopFunc  listener_func,
412                                   gpointer         listener_data)
413 {
414   static guint key = 0;
415   KeyEventListener *listener;
416
417   if (key_snooper_id == 0)
418     key_snooper_id = gtk_key_snooper_install (gail_key_snooper, NULL);
419
420   key++;
421
422   listener = g_slice_new0 (KeyEventListener);
423   listener->func = listener_func;
424   listener->data = listener_data;
425   listener->key = key;
426
427   key_listener_list = g_slist_append (key_listener_list, listener);
428
429   return key;
430 }
431
432 static void
433 gail_util_remove_key_event_listener (guint listener_key)
434 {
435   GSList *l;
436
437   for (l = key_listener_list; l; l = l->next)
438     {
439       KeyEventListener *listener = l->data;
440
441       if (listener->key == listener_key)
442         {
443           g_slice_free (KeyEventListener, listener);
444           key_listener_list = g_slist_delete_link (key_listener_list, l);
445
446           break;
447         }
448     }
449
450   if (key_listener_list == NULL)
451     {
452       gtk_key_snooper_remove (key_snooper_id);
453       key_snooper_id = 0;
454     }
455 }
456
457 static AtkObject *
458 gail_util_get_root (void)
459 {
460   static AtkObject *root = NULL;
461
462   if (!root)
463     {
464       root = g_object_new (GTK_TYPE_TOPLEVEL_ACCESSIBLE, NULL);
465       atk_object_initialize (root, NULL);
466     }
467
468   return root;
469 }
470
471 static const gchar *
472 gail_util_get_toolkit_name (void)
473 {
474   return "gtk";
475 }
476
477 static const gchar *
478 gail_util_get_toolkit_version (void)
479 {
480   return GTK_VERSION;
481 }
482
483 void
484 _gail_util_install (void)
485 {
486   AtkUtilClass *atk_class = ATK_UTIL_CLASS (g_type_class_ref (ATK_TYPE_UTIL));
487
488   atk_class->add_global_event_listener = gail_util_add_global_event_listener;
489   atk_class->remove_global_event_listener = gail_util_remove_global_event_listener;
490   atk_class->add_key_event_listener = gail_util_add_key_event_listener;
491   atk_class->remove_key_event_listener = gail_util_remove_key_event_listener;
492   atk_class->get_root = gail_util_get_root;
493   atk_class->get_toolkit_name = gail_util_get_toolkit_name;
494   atk_class->get_toolkit_version = gail_util_get_toolkit_version;
495
496   listener_list = g_hash_table_new_full (g_int_hash, g_int_equal, NULL, g_free);
497 }