]> Pileus Git - ~andy/gtk/blob - modules/other/gail/gailutil.c
Remove a bunch of crazy code from gail
[~andy/gtk] / modules / other / gail / 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 <string.h>
24 #include <gtk/gtk.h>
25 #include "gailutil.h"
26 #include "gailtoplevel.h"
27 #include "gailwindow.h"
28 #include "gail-private-macros.h"
29
30 static void             gail_util_class_init                    (GailUtilClass          *klass);
31 static void             gail_util_init                          (GailUtil               *utils);
32 /* atkutil.h */
33
34 static guint            gail_util_add_global_event_listener     (GSignalEmissionHook    listener,
35                                                                  const gchar*           event_type);
36 static void             gail_util_remove_global_event_listener  (guint                  remove_listener);
37 static guint            gail_util_add_key_event_listener        (AtkKeySnoopFunc        listener,
38                                                                  gpointer               data);
39 static void             gail_util_remove_key_event_listener     (guint                  remove_listener);
40 static AtkObject*       gail_util_get_root                      (void);
41 static G_CONST_RETURN gchar *gail_util_get_toolkit_name         (void);
42 static G_CONST_RETURN gchar *gail_util_get_toolkit_version      (void);
43
44 /* gailmisc/AtkMisc */
45 static void             gail_misc_class_init                    (GailMiscClass          *klass);
46 static void             gail_misc_init                          (GailMisc               *misc);
47
48 static void gail_misc_threads_enter (AtkMisc *misc);
49 static void gail_misc_threads_leave (AtkMisc *misc);
50
51 /* Misc */
52
53 static void             _listener_info_destroy                  (gpointer               data);
54 static guint            add_listener                            (GSignalEmissionHook    listener,
55                                                                  const gchar            *object_type,
56                                                                  const gchar            *signal,
57                                                                  const gchar            *hook_data);
58 static void             do_window_event_initialization          (void);
59 static gboolean         state_event_watcher                     (GSignalInvocationHint  *hint,
60                                                                  guint                  n_param_values,
61                                                                  const GValue           *param_values,
62                                                                  gpointer               data);
63 static void             window_added                             (AtkObject             *atk_obj,
64                                                                   guint                 index,
65                                                                   AtkObject             *child);
66 static void             window_removed                           (AtkObject             *atk_obj,
67                                                                   guint                 index,
68                                                                   AtkObject             *child);
69 static gboolean        window_focus                              (GtkWidget             *widget,
70                                                                   GdkEventFocus         *event);
71 static gboolean         configure_event_watcher                 (GSignalInvocationHint  *hint,
72                                                                  guint                  n_param_values,
73                                                                  const GValue           *param_values,
74                                                                  gpointer               data);
75                                                                   
76
77 static AtkObject* root = NULL;
78 static GHashTable *listener_list = NULL;
79 static gint listener_idx = 1;
80 static GSList *key_listener_list = NULL;
81 static guint key_snooper_id = 0;
82
83 typedef struct _GailUtilListenerInfo GailUtilListenerInfo;
84 typedef struct _GailKeyEventInfo GailKeyEventInfo;
85
86 struct _GailUtilListenerInfo
87 {
88    gint key;
89    guint signal_id;
90    gulong hook_id;
91 };
92
93 struct _GailKeyEventInfo
94 {
95   AtkKeyEventStruct *key_event;
96   gpointer func_data;
97 };
98
99 G_DEFINE_TYPE (GailUtil, gail_util, ATK_TYPE_UTIL)
100
101 static void      
102 gail_util_class_init (GailUtilClass *klass)
103 {
104   AtkUtilClass *atk_class;
105   gpointer data;
106
107   data = g_type_class_peek (ATK_TYPE_UTIL);
108   atk_class = ATK_UTIL_CLASS (data);
109
110   atk_class->add_global_event_listener =
111     gail_util_add_global_event_listener;
112   atk_class->remove_global_event_listener =
113     gail_util_remove_global_event_listener;
114   atk_class->add_key_event_listener =
115     gail_util_add_key_event_listener;
116   atk_class->remove_key_event_listener =
117     gail_util_remove_key_event_listener;
118   atk_class->get_root = gail_util_get_root;
119   atk_class->get_toolkit_name = gail_util_get_toolkit_name;
120   atk_class->get_toolkit_version = gail_util_get_toolkit_version;
121
122   listener_list = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, 
123      _listener_info_destroy);
124 }
125
126 static void
127 gail_util_init (GailUtil *utils)
128 {
129 }
130
131 static guint
132 gail_util_add_global_event_listener (GSignalEmissionHook listener,
133                                      const gchar *event_type)
134 {
135   guint rc = 0;
136   gchar **split_string;
137
138   split_string = g_strsplit (event_type, ":", 3);
139
140   if (split_string)
141     {
142       if (!strcmp ("window", split_string[0]))
143         {
144           static gboolean initialized = FALSE;
145
146           if (!initialized)
147             {
148               do_window_event_initialization ();
149               initialized = TRUE;
150             }
151           rc = add_listener (listener, "GailWindow", split_string[1], event_type);
152         }
153       else
154         {
155           rc = add_listener (listener, split_string[1], split_string[2], event_type);
156         }
157
158       g_strfreev (split_string);
159     }
160
161   return rc;
162 }
163
164 static void
165 gail_util_remove_global_event_listener (guint remove_listener)
166 {
167   if (remove_listener > 0)
168   {
169     GailUtilListenerInfo *listener_info;
170     gint tmp_idx = remove_listener;
171
172     listener_info = (GailUtilListenerInfo *)
173       g_hash_table_lookup(listener_list, &tmp_idx);
174
175     if (listener_info != NULL)
176       {
177         /* Hook id of 0 and signal id of 0 are invalid */
178         if (listener_info->hook_id != 0 && listener_info->signal_id != 0)
179           {
180             /* Remove the emission hook */
181             g_signal_remove_emission_hook(listener_info->signal_id,
182               listener_info->hook_id);
183
184             /* Remove the element from the hash */
185             g_hash_table_remove(listener_list, &tmp_idx);
186           }
187         else
188           {
189             g_warning("Invalid listener hook_id %ld or signal_id %d\n",
190               listener_info->hook_id, listener_info->signal_id);
191           }
192       }
193     else
194       {
195         g_warning("No listener with the specified listener id %d", 
196           remove_listener);
197       }
198   }
199   else
200   {
201     g_warning("Invalid listener_id %d", remove_listener);
202   }
203 }
204
205
206 static
207 AtkKeyEventStruct *
208 atk_key_event_from_gdk_event_key (GdkEventKey *key)
209 {
210   AtkKeyEventStruct *event = g_new0 (AtkKeyEventStruct, 1);
211   switch (key->type)
212     {
213     case GDK_KEY_PRESS:
214             event->type = ATK_KEY_EVENT_PRESS;
215             break;
216     case GDK_KEY_RELEASE:
217             event->type = ATK_KEY_EVENT_RELEASE;
218             break;
219     default:
220             g_assert_not_reached ();
221             return NULL;
222     }
223   event->state = key->state;
224   event->keyval = key->keyval;
225   event->length = key->length;
226   if (key->string && key->string [0] && 
227       (key->state & GDK_CONTROL_MASK ||
228        g_unichar_isgraph (g_utf8_get_char (key->string))))
229     {
230       event->string = key->string;
231     }
232   else if (key->type == GDK_KEY_PRESS ||
233            key->type == GDK_KEY_RELEASE)
234     {
235       event->string = gdk_keyval_name (key->keyval);        
236     }
237   event->keycode = key->hardware_keycode;
238   event->timestamp = key->time;
239 #ifdef GAIL_DEBUG  
240   g_print ("GailKey:\tsym %u\n\tmods %x\n\tcode %u\n\ttime %lx\n",
241            (unsigned int) event->keyval,
242            (unsigned int) event->state,
243            (unsigned int) event->keycode,
244            (unsigned long int) event->timestamp);
245 #endif
246   return event;
247 }
248
249 typedef struct {
250   AtkKeySnoopFunc func;
251   gpointer        data;
252   guint           key;
253 } KeyEventListener;
254
255 static gint
256 gail_key_snooper (GtkWidget *the_widget, GdkEventKey *event, gpointer data)
257 {
258   GSList *l;
259   AtkKeyEventStruct *atk_event;
260   gboolean result;
261
262   atk_event = atk_key_event_from_gdk_event_key (event);
263
264   result = FALSE;
265
266   for (l = key_listener_list; l; l = l->next)
267     {
268       KeyEventListener *listener = l->data;
269
270       result |= listener->func (atk_event, listener->data);
271     }
272   g_free (atk_event);
273
274   return result;
275 }
276
277 static guint
278 gail_util_add_key_event_listener (AtkKeySnoopFunc  listener_func,
279                                   gpointer         listener_data)
280 {
281   static guint key = 0;
282   KeyEventListener *listener;
283
284   if (key_snooper_id == 0)
285     key_snooper_id = gtk_key_snooper_install (gail_key_snooper, NULL);
286
287   key++;
288
289   listener = g_slice_new0 (KeyEventListener);
290   listener->func = listener_func;
291   listener->data = listener_data;
292   listener->key = key;
293
294   key_listener_list = g_slist_append (key_listener_list, listener);
295
296   return key;
297 }
298
299 static void
300 gail_util_remove_key_event_listener (guint listener_key)
301 {
302   GSList *l;
303
304   for (l = key_listener_list; l; l = l->next)
305     {
306       KeyEventListener *listener = l->data;
307
308       if (listener->key == listener_key)
309         {
310           g_slice_free (KeyEventListener, listener);
311           key_listener_list = g_slist_delete_link (key_listener_list, l);
312
313           break;
314         }
315     }
316
317   if (key_listener_list == NULL)
318     {
319       gtk_key_snooper_remove (key_snooper_id);
320       key_snooper_id = 0;
321     }
322 }
323
324 static AtkObject*
325 gail_util_get_root (void)
326 {
327   if (!root)
328     {
329       root = g_object_new (GAIL_TYPE_TOPLEVEL, NULL);
330       atk_object_initialize (root, NULL);
331     }
332
333   return root;
334 }
335
336 static G_CONST_RETURN gchar *
337 gail_util_get_toolkit_name (void)
338 {
339   return "GAIL";
340 }
341
342 static G_CONST_RETURN gchar *
343 gail_util_get_toolkit_version (void)
344 {
345  /*
346   * Version is passed in as a -D flag when this file is
347   * compiled.
348   */
349   return GTK_VERSION;
350 }
351
352 static void
353 _listener_info_destroy (gpointer data)
354 {
355    g_free(data);
356 }
357
358 static guint
359 add_listener (GSignalEmissionHook listener,
360               const gchar         *object_type,
361               const gchar         *signal,
362               const gchar         *hook_data)
363 {
364   GType type;
365   guint signal_id;
366   gint  rc = 0;
367
368   type = g_type_from_name (object_type);
369   if (type)
370     {
371       signal_id  = g_signal_lookup (signal, type);
372       if (signal_id > 0)
373         {
374           GailUtilListenerInfo *listener_info;
375
376           rc = listener_idx;
377
378           listener_info = g_malloc(sizeof(GailUtilListenerInfo));
379           listener_info->key = listener_idx;
380           listener_info->hook_id =
381                           g_signal_add_emission_hook (signal_id, 0, listener,
382                                                       g_strdup (hook_data),
383                                                       (GDestroyNotify) g_free);
384           listener_info->signal_id = signal_id;
385
386           g_hash_table_insert(listener_list, &(listener_info->key), listener_info);
387           listener_idx++;
388         }
389       else
390         {
391           g_warning("Invalid signal type %s\n", signal);
392         }
393     }
394   else
395     {
396       g_warning("Invalid object type %s\n", object_type);
397     }
398   return rc;
399 }
400
401 static void
402 do_window_event_initialization (void)
403 {
404   AtkObject *root;
405
406   /*
407    * Ensure that GailWindowClass exists.
408    */
409   g_type_class_ref (GAIL_TYPE_WINDOW);
410   g_signal_add_emission_hook (g_signal_lookup ("window-state-event", GTK_TYPE_WIDGET),
411                               0, state_event_watcher, NULL, (GDestroyNotify) NULL);
412   g_signal_add_emission_hook (g_signal_lookup ("configure-event", GTK_TYPE_WIDGET),
413                               0, configure_event_watcher, NULL, (GDestroyNotify) NULL);
414
415   root = atk_get_root ();
416   g_signal_connect (root, "children-changed::add",
417                     (GCallback) window_added, NULL);
418   g_signal_connect (root, "children-changed::remove",
419                     (GCallback) window_removed, NULL);
420 }
421
422 static gboolean
423 state_event_watcher (GSignalInvocationHint  *hint,
424                      guint                  n_param_values,
425                      const GValue           *param_values,
426                      gpointer               data)
427 {
428   GObject *object;
429   GtkWidget *widget;
430   AtkObject *atk_obj;
431   AtkObject *parent;
432   GdkEventWindowState *event;
433   gchar *signal_name;
434   guint signal_id;
435
436   object = g_value_get_object (param_values + 0);
437   /*
438    * The object can be a GtkMenu when it is popped up; we ignore this
439    */
440   if (!GTK_IS_WINDOW (object))
441     return FALSE;
442
443   event = g_value_get_boxed (param_values + 1);
444   gail_return_val_if_fail (event->type == GDK_WINDOW_STATE, FALSE);
445   widget = GTK_WIDGET (object);
446
447   if (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED)
448     {
449       signal_name = "maximize";
450     }
451   else if (event->new_window_state & GDK_WINDOW_STATE_ICONIFIED)
452     {
453       signal_name = "minimize";
454     }
455   else if (event->new_window_state == 0)
456     {
457       signal_name = "restore";
458     }
459   else
460     return TRUE;
461   
462   atk_obj = gtk_widget_get_accessible (widget);
463
464   if (GAIL_IS_WINDOW (atk_obj))
465     {
466       parent = atk_object_get_parent (atk_obj);
467       if (parent == atk_get_root ())
468         {
469           signal_id = g_signal_lookup (signal_name, GAIL_TYPE_WINDOW); 
470           g_signal_emit (atk_obj, signal_id, 0);
471         }
472       
473       return TRUE;
474     }
475   else
476     {
477       return FALSE;
478     }
479 }
480
481 static void
482 window_added (AtkObject *atk_obj,
483               guint     index,
484               AtkObject *child)
485 {
486   GtkWidget *widget;
487
488   if (!GAIL_IS_WINDOW (child)) return;
489
490   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (child));
491   gail_return_if_fail (widget);
492
493   g_signal_connect (widget, "focus-in-event",  
494                     (GCallback) window_focus, NULL);
495   g_signal_connect (widget, "focus-out-event",  
496                     (GCallback) window_focus, NULL);
497   g_signal_emit (child, g_signal_lookup ("create", GAIL_TYPE_WINDOW), 0); 
498 }
499
500
501 static void
502 window_removed (AtkObject *atk_obj,
503                  guint     index,
504                  AtkObject *child)
505 {
506   GtkWidget *widget;
507   GtkWindow *window;
508
509   if (!GAIL_IS_WINDOW (child)) return;
510
511   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (child));
512   gail_return_if_fail (widget);
513
514   window = GTK_WINDOW (widget);
515   /*
516    * Deactivate window if it is still focused and we are removing it. This
517    * can happen when a dialog displayed by gok is removed.
518    */
519   if (gtk_window_is_active (window) &&
520       gtk_window_has_toplevel_focus (window))
521     {
522       gchar *signal_name;
523       AtkObject *atk_obj;
524
525       atk_obj = gtk_widget_get_accessible (widget);
526       signal_name =  "deactivate";
527       g_signal_emit (atk_obj, g_signal_lookup (signal_name, GAIL_TYPE_WINDOW), 0); 
528     }
529
530   g_signal_handlers_disconnect_by_func (widget, (gpointer) window_focus, NULL);
531   g_signal_emit (child, g_signal_lookup ("destroy", GAIL_TYPE_WINDOW), 0); 
532 }
533
534 static gboolean
535 window_focus (GtkWidget     *widget,
536               GdkEventFocus *event)
537 {
538   gchar *signal_name;
539   AtkObject *atk_obj;
540
541   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
542
543   atk_obj = gtk_widget_get_accessible (widget);
544   signal_name =  (event->in) ? "activate" : "deactivate";
545   g_signal_emit (atk_obj, g_signal_lookup (signal_name, GAIL_TYPE_WINDOW), 0); 
546
547   return FALSE;
548 }
549
550 static gboolean 
551 configure_event_watcher (GSignalInvocationHint  *hint,
552                          guint                  n_param_values,
553                          const GValue           *param_values,
554                          gpointer               data)
555 {
556   GtkAllocation allocation;
557   GObject *object;
558   GtkWidget *widget;
559   AtkObject *atk_obj;
560   AtkObject *parent;
561   GdkEvent *event;
562   gchar *signal_name;
563   guint signal_id;
564
565   object = g_value_get_object (param_values + 0);
566   if (!GTK_IS_WINDOW (object))
567     /*
568      * GtkDrawingArea can send a GDK_CONFIGURE event but we ignore here
569      */
570     return FALSE;
571
572   event = g_value_get_boxed (param_values + 1);
573   if (event->type != GDK_CONFIGURE)
574     return FALSE;
575   widget = GTK_WIDGET (object);
576   gtk_widget_get_allocation (widget, &allocation);
577   if (allocation.x == ((GdkEventConfigure *)event)->x &&
578       allocation.y == ((GdkEventConfigure *)event)->y &&
579       allocation.width == ((GdkEventConfigure *)event)->width &&
580       allocation.height == ((GdkEventConfigure *)event)->height)
581     return TRUE;
582
583   if (allocation.width != ((GdkEventConfigure *)event)->width ||
584       allocation.height != ((GdkEventConfigure *)event)->height)
585     {
586       signal_name = "resize";
587     }
588   else
589     {
590       signal_name = "move";
591     }
592
593   atk_obj = gtk_widget_get_accessible (widget);
594   if (GAIL_IS_WINDOW (atk_obj))
595     {
596       parent = atk_object_get_parent (atk_obj);
597       if (parent == atk_get_root ())
598         {
599           signal_id = g_signal_lookup (signal_name, GAIL_TYPE_WINDOW); 
600           g_signal_emit (atk_obj, signal_id, 0);
601         }
602       
603       return TRUE;
604     }
605   else
606     {
607       return FALSE;
608     }
609 }
610
611 G_DEFINE_TYPE (GailMisc, gail_misc, ATK_TYPE_MISC)
612
613 static void      
614 gail_misc_class_init (GailMiscClass *klass)
615 {
616   AtkMiscClass *miscclass = ATK_MISC_CLASS (klass);
617   miscclass->threads_enter =
618     gail_misc_threads_enter;
619   miscclass->threads_leave =
620     gail_misc_threads_leave;
621   atk_misc_instance = g_object_new (GAIL_TYPE_MISC, NULL);
622 }
623
624 static void
625 gail_misc_init (GailMisc *misc)
626 {
627 }
628
629 static void gail_misc_threads_enter (AtkMisc *misc)
630 {
631   GDK_THREADS_ENTER ();
632 }
633
634 static void gail_misc_threads_leave (AtkMisc *misc)
635 {
636   GDK_THREADS_LEAVE ();
637 }