]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gailutil.c
GtkEntryAccessible: Remove unused variable
[~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 AtkKeyEventStruct *
210 atk_key_event_from_gdk_event_key (GdkEventKey *key)
211 {
212   AtkKeyEventStruct *event = g_new0 (AtkKeyEventStruct, 1);
213   switch (key->type)
214     {
215     case GDK_KEY_PRESS:
216             event->type = ATK_KEY_EVENT_PRESS;
217             break;
218     case GDK_KEY_RELEASE:
219             event->type = ATK_KEY_EVENT_RELEASE;
220             break;
221     default:
222             g_assert_not_reached ();
223             return NULL;
224     }
225   event->state = key->state;
226   event->keyval = key->keyval;
227   event->length = key->length;
228   if (key->string && key->string [0] &&
229       (key->state & GDK_CONTROL_MASK ||
230        g_unichar_isgraph (g_utf8_get_char (key->string))))
231     {
232       event->string = key->string;
233     }
234   else if (key->type == GDK_KEY_PRESS ||
235            key->type == GDK_KEY_RELEASE)
236     {
237       event->string = gdk_keyval_name (key->keyval);
238     }
239   event->keycode = key->hardware_keycode;
240   event->timestamp = key->time;
241 #ifdef GAIL_DEBUG
242   g_print ("GailKey:\tsym %u\n\tmods %x\n\tcode %u\n\ttime %lx\n",
243            (unsigned int) event->keyval,
244            (unsigned int) event->state,
245            (unsigned int) event->keycode,
246            (unsigned long int) event->timestamp);
247 #endif
248   return event;
249 }
250
251 typedef struct {
252   AtkKeySnoopFunc func;
253   gpointer        data;
254   guint           key;
255 } KeyEventListener;
256
257 gboolean
258 _gail_util_key_snooper (GtkWidget   *the_widget,
259                         GdkEventKey *event)
260 {
261   GSList *l;
262   AtkKeyEventStruct *atk_event;
263   gboolean result;
264
265   atk_event = atk_key_event_from_gdk_event_key (event);
266
267   result = FALSE;
268
269   for (l = key_listener_list; l; l = l->next)
270     {
271       KeyEventListener *listener = l->data;
272
273       result |= listener->func (atk_event, listener->data);
274     }
275   g_free (atk_event);
276
277   return result;
278 }
279
280 static guint
281 gail_util_add_key_event_listener (AtkKeySnoopFunc  listener_func,
282                                   gpointer         listener_data)
283 {
284   static guint key = 0;
285   KeyEventListener *listener;
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
318 static AtkObject *
319 gail_util_get_root (void)
320 {
321   static AtkObject *root = NULL;
322
323   if (!root)
324     {
325       root = g_object_new (GTK_TYPE_TOPLEVEL_ACCESSIBLE, NULL);
326       atk_object_initialize (root, NULL);
327     }
328
329   return root;
330 }
331
332 static const gchar *
333 gail_util_get_toolkit_name (void)
334 {
335   return "gtk";
336 }
337
338 static const gchar *
339 gail_util_get_toolkit_version (void)
340 {
341   return GTK_VERSION;
342 }
343
344 void
345 _gail_util_install (void)
346 {
347   AtkUtilClass *atk_class = ATK_UTIL_CLASS (g_type_class_ref (ATK_TYPE_UTIL));
348
349   atk_class->add_key_event_listener = gail_util_add_key_event_listener;
350   atk_class->remove_key_event_listener = gail_util_remove_key_event_listener;
351   atk_class->get_root = gail_util_get_root;
352   atk_class->get_toolkit_name = gail_util_get_toolkit_name;
353   atk_class->get_toolkit_version = gail_util_get_toolkit_version;
354
355   do_window_event_initialization ();
356 }