]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gailutil.c
a11y: fix private strict regression
[~andy/gtk] / gtk / a11y / gailutil.c
1 /* GAIL - The GNOME Accessibility Implementation Library
2  * Copyright 2011, F123 Consulting & Mais Diferenças
3  * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "config.h"
20
21 #include <stdlib.h>
22 #include <gtk/gtk.h>
23 #include "gailutil.h"
24 #include "gtktoplevelaccessible.h"
25 #include "gtkwindowaccessible.h"
26
27 static GSList *key_listener_list = NULL;
28
29 typedef struct _GailKeyEventInfo GailKeyEventInfo;
30
31 struct _GailKeyEventInfo
32 {
33   AtkKeyEventStruct *key_event;
34   gpointer func_data;
35 };
36
37 static gboolean
38 state_event_watcher (GSignalInvocationHint *hint,
39                      guint                  n_param_values,
40                      const GValue          *param_values,
41                      gpointer               data)
42 {
43   GObject *object;
44   GtkWidget *widget;
45   AtkObject *atk_obj;
46   AtkObject *parent;
47   GdkEventWindowState *event;
48   gchar *signal_name;
49
50   object = g_value_get_object (param_values + 0);
51   if (!GTK_IS_WINDOW (object))
52     return FALSE;
53
54   event = g_value_get_boxed (param_values + 1);
55   if (event->type == GDK_WINDOW_STATE)
56     return FALSE;
57   widget = GTK_WIDGET (object);
58
59   if (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED)
60     signal_name = "maximize";
61   else if (event->new_window_state & GDK_WINDOW_STATE_ICONIFIED)
62     signal_name = "minimize";
63   else if (event->new_window_state == 0)
64     signal_name = "restore";
65   else
66     return TRUE;
67
68   atk_obj = gtk_widget_get_accessible (widget);
69   if (GTK_IS_WINDOW_ACCESSIBLE (atk_obj))
70     {
71       parent = atk_object_get_parent (atk_obj);
72       if (parent == atk_get_root ())
73         g_signal_emit_by_name (atk_obj, signal_name);
74
75       return TRUE;
76     }
77
78   return FALSE;
79 }
80
81 static gboolean
82 configure_event_watcher (GSignalInvocationHint *hint,
83                          guint                  n_param_values,
84                          const GValue          *param_values,
85                          gpointer               data)
86 {
87   GtkAllocation allocation;
88   GObject *object;
89   GtkWidget *widget;
90   AtkObject *atk_obj;
91   AtkObject *parent;
92   GdkEvent *event;
93   gchar *signal_name;
94
95   object = g_value_get_object (param_values + 0);
96   if (!GTK_IS_WINDOW (object))
97     return FALSE;
98
99   event = g_value_get_boxed (param_values + 1);
100   if (event->type != GDK_CONFIGURE)
101     return FALSE;
102   widget = GTK_WIDGET (object);
103   gtk_widget_get_allocation (widget, &allocation);
104   if (allocation.x == ((GdkEventConfigure *)event)->x &&
105       allocation.y == ((GdkEventConfigure *)event)->y &&
106       allocation.width == ((GdkEventConfigure *)event)->width &&
107       allocation.height == ((GdkEventConfigure *)event)->height)
108     return TRUE;
109
110   if (allocation.width != ((GdkEventConfigure *)event)->width ||
111       allocation.height != ((GdkEventConfigure *)event)->height)
112     signal_name = "resize";
113   else
114     signal_name = "move";
115
116   atk_obj = gtk_widget_get_accessible (widget);
117   if (GTK_IS_WINDOW_ACCESSIBLE (atk_obj))
118     {
119       parent = atk_object_get_parent (atk_obj);
120       if (parent == atk_get_root ())
121         g_signal_emit_by_name (atk_obj, signal_name);
122
123       return TRUE;
124     }
125
126   return FALSE;
127 }
128
129 static gboolean
130 window_focus (GtkWidget     *widget,
131               GdkEventFocus *event)
132 {
133   AtkObject *atk_obj;
134
135   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
136
137   atk_obj = gtk_widget_get_accessible (widget);
138   g_signal_emit_by_name (atk_obj, event->in ? "activate" : "deactivate");
139
140   return FALSE;
141 }
142
143 static void
144 window_added (AtkObject *atk_obj,
145               guint      index,
146               AtkObject *child)
147 {
148   GtkWidget *widget;
149
150   if (!GTK_IS_WINDOW_ACCESSIBLE (child))
151     return;
152
153   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (child));
154   if (!widget)
155     return;
156
157   g_signal_connect (widget, "focus-in-event", (GCallback) window_focus, NULL);
158   g_signal_connect (widget, "focus-out-event", (GCallback) window_focus, NULL);
159   g_signal_emit_by_name (child, "create");
160 }
161
162
163 static void
164 window_removed (AtkObject *atk_obj,
165                 guint      index,
166                 AtkObject *child)
167 {
168   GtkWidget *widget;
169   GtkWindow *window;
170
171   if (!GTK_IS_WINDOW_ACCESSIBLE (child))
172     return;
173
174   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (child));
175   if (!widget)
176     return;
177
178   window = GTK_WINDOW (widget);
179   /*
180    * Deactivate window if it is still focused and we are removing it. This
181    * can happen when a dialog displayed by gok is removed.
182    */
183   if (gtk_window_is_active (window) &&
184       gtk_window_has_toplevel_focus (window))
185     g_signal_emit_by_name (child, "deactivate");
186
187   g_signal_handlers_disconnect_by_func (widget, (gpointer) window_focus, NULL);
188   g_signal_emit_by_name (child, "destroy");
189 }
190
191 static void
192 do_window_event_initialization (void)
193 {
194   AtkObject *root;
195
196   g_type_class_ref (GTK_TYPE_WINDOW_ACCESSIBLE);
197   g_signal_add_emission_hook (g_signal_lookup ("window-state-event", GTK_TYPE_WIDGET),
198                               0, state_event_watcher, NULL, (GDestroyNotify) NULL);
199   g_signal_add_emission_hook (g_signal_lookup ("configure-event", GTK_TYPE_WIDGET),
200                               0, configure_event_watcher, NULL, (GDestroyNotify) NULL);
201
202   root = atk_get_root ();
203   g_signal_connect (root, "children-changed::add",
204                     (GCallback) window_added, NULL);
205   g_signal_connect (root, "children-changed::remove",
206                     (GCallback) window_removed, NULL);
207 }
208
209 static void
210 undo_window_event_initialization (void)
211 {
212   AtkObject *root;
213
214   root = atk_get_root ();
215
216   g_signal_handlers_disconnect_by_func (root, (GCallback) window_added, NULL);
217   g_signal_handlers_disconnect_by_func (root, (GCallback) window_removed, NULL);
218 }
219
220 static AtkKeyEventStruct *
221 atk_key_event_from_gdk_event_key (GdkEventKey *key)
222 {
223   AtkKeyEventStruct *event = g_new0 (AtkKeyEventStruct, 1);
224   switch (key->type)
225     {
226     case GDK_KEY_PRESS:
227             event->type = ATK_KEY_EVENT_PRESS;
228             break;
229     case GDK_KEY_RELEASE:
230             event->type = ATK_KEY_EVENT_RELEASE;
231             break;
232     default:
233             g_assert_not_reached ();
234             return NULL;
235     }
236   event->state = key->state;
237   event->keyval = key->keyval;
238   event->length = key->length;
239   if (key->string && key->string [0] &&
240       (key->state & GDK_CONTROL_MASK ||
241        g_unichar_isgraph (g_utf8_get_char (key->string))))
242     {
243       event->string = key->string;
244     }
245   else if (key->type == GDK_KEY_PRESS ||
246            key->type == GDK_KEY_RELEASE)
247     {
248       event->string = gdk_keyval_name (key->keyval);
249     }
250   event->keycode = key->hardware_keycode;
251   event->timestamp = key->time;
252 #ifdef GAIL_DEBUG
253   g_print ("GailKey:\tsym %u\n\tmods %x\n\tcode %u\n\ttime %lx\n",
254            (unsigned int) event->keyval,
255            (unsigned int) event->state,
256            (unsigned int) event->keycode,
257            (unsigned long int) event->timestamp);
258 #endif
259   return event;
260 }
261
262 typedef struct {
263   AtkKeySnoopFunc func;
264   gpointer        data;
265   guint           key;
266 } KeyEventListener;
267
268 gboolean
269 _gail_util_key_snooper (GtkWidget   *the_widget,
270                         GdkEventKey *event)
271 {
272   GSList *l;
273   AtkKeyEventStruct *atk_event;
274   gboolean result;
275
276   atk_event = atk_key_event_from_gdk_event_key (event);
277
278   result = FALSE;
279
280   for (l = key_listener_list; l; l = l->next)
281     {
282       KeyEventListener *listener = l->data;
283
284       result |= listener->func (atk_event, listener->data);
285     }
286   g_free (atk_event);
287
288   return result;
289 }
290
291 static guint
292 gail_util_add_key_event_listener (AtkKeySnoopFunc  listener_func,
293                                   gpointer         listener_data)
294 {
295   static guint key = 0;
296   KeyEventListener *listener;
297
298   key++;
299
300   listener = g_slice_new0 (KeyEventListener);
301   listener->func = listener_func;
302   listener->data = listener_data;
303   listener->key = key;
304
305   key_listener_list = g_slist_append (key_listener_list, listener);
306
307   return key;
308 }
309
310 static void
311 gail_util_remove_key_event_listener (guint listener_key)
312 {
313   GSList *l;
314
315   for (l = key_listener_list; l; l = l->next)
316     {
317       KeyEventListener *listener = l->data;
318
319       if (listener->key == listener_key)
320         {
321           g_slice_free (KeyEventListener, listener);
322           key_listener_list = g_slist_delete_link (key_listener_list, l);
323
324           break;
325         }
326     }
327 }
328
329 static AtkObject *
330 gail_util_get_root (void)
331 {
332   static AtkObject *root = NULL;
333
334   if (!root)
335     {
336       root = g_object_new (GTK_TYPE_TOPLEVEL_ACCESSIBLE, NULL);
337       atk_object_initialize (root, NULL);
338     }
339
340   return root;
341 }
342
343 static const gchar *
344 gail_util_get_toolkit_name (void)
345 {
346   return "gtk";
347 }
348
349 static const gchar *
350 gail_util_get_toolkit_version (void)
351 {
352   return GTK_VERSION;
353 }
354
355 void
356 _gail_util_uninstall (void)
357 {
358   undo_window_event_initialization ();
359 }
360
361 void
362 _gail_util_install (void)
363 {
364   AtkUtilClass *atk_class = ATK_UTIL_CLASS (g_type_class_ref (ATK_TYPE_UTIL));
365
366   atk_class->add_key_event_listener = gail_util_add_key_event_listener;
367   atk_class->remove_key_event_listener = gail_util_remove_key_event_listener;
368   atk_class->get_root = gail_util_get_root;
369   atk_class->get_toolkit_name = gail_util_get_toolkit_name;
370   atk_class->get_toolkit_version = gail_util_get_toolkit_version;
371
372   do_window_event_initialization ();
373 }