Debian Codegraph @

gtk/a11y/gtkaccessibilityutil.c

raw
1 /* GTK+ - accessibility implementations
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 "gtkaccessibility.h"
24 #include "gtkaccessibilityutil.h"
25 #include "gtktoplevelaccessible.h"
26
27 static GSList *key_listener_list = NULL;
28
29 typedef struct {
30 AtkKeySnoopFunc func;
31 gpointer data;
32 guint key;
33 } KeyEventListener;
34
35 static guint
36 add_key_event_listener (AtkKeySnoopFunc listener_func,
37 gpointer listener_data)
38 {
39 static guint key = 0;
40 KeyEventListener *listener;
41
42 key++;
43
44 listener = g_slice_new0 (KeyEventListener);
45 listener->func = listener_func;
46 listener->data = listener_data;
47 listener->key = key;
48
49 key_listener_list = g_slist_append (key_listener_list, listener);
50
51 return key;
52 }
53
54 static void
55 remove_key_event_listener (guint listener_key)
56 {
57 GSList *l;
58
59 for (l = key_listener_list; l; l = l->next)
60 {
61 KeyEventListener *listener = l->data;
62
63 if (listener->key == listener_key)
64 {
65 g_slice_free (KeyEventListener, listener);
66 key_listener_list = g_slist_delete_link (key_listener_list, l);
67
68 break;
69 }
70 }
71 }
72
73 static AtkObject *
74 get_root (void)
75 {
76 static AtkObject *root = NULL;
77
78 if (!root)
79 {
80 root = g_object_new (GTK_TYPE_TOPLEVEL_ACCESSIBLE, NULL);
81 atk_object_initialize (root, NULL);
82 }
83
84 return root;
85 }
86
87 static const gchar *
88 get_toolkit_name (void)
89 {
90 return "gtk";
91 }
92
93 static const gchar *
94 get_toolkit_version (void)
95 {
96 return GTK_VERSION;
97 }
98
99 void
100 _gtk_accessibility_override_atk_util (void)
101 {
102 AtkUtilClass *atk_class = ATK_UTIL_CLASS (g_type_class_ref (ATK_TYPE_UTIL));
103
104 if (atk_class->get_root)
105 return;
106
107 atk_class->add_key_event_listener = add_key_event_listener;
108 atk_class->remove_key_event_listener = remove_key_event_listener;
109 atk_class->get_root = get_root;
110 atk_class->get_toolkit_name = get_toolkit_name;
111 atk_class->get_toolkit_version = get_toolkit_version;
112 }
113
114 static void
115 atk_key_event_from_gdk_event_key (GdkEventKey *key,
116 AtkKeyEventStruct *event)
117 {
118 if (key->type == GDK_KEY_PRESS)
119 event->type = ATK_KEY_EVENT_PRESS;
120 else if (key->type == GDK_KEY_RELEASE)
121 event->type = ATK_KEY_EVENT_RELEASE;
122 else
123 g_assert_not_reached ();
124
125 event->state = key->state;
126 event->keyval = key->keyval;
127 event->length = key->length;
128 if (key->string && key->string[0] &&
129 g_unichar_isgraph (g_utf8_get_char (key->string)))
130 event->string = key->string;
131 else
132 event->string = gdk_keyval_name (key->keyval);
133
134 event->keycode = key->hardware_keycode;
135 event->timestamp = key->time;
136 }
137
138 gboolean
139 _gtk_accessibility_key_snooper (GtkWidget *widget,
140 GdkEventKey *event)
141 {
142 GSList *l;
143 AtkKeyEventStruct atk_event;
144 gboolean result;
145
146 result = FALSE;
147
148 atk_key_event_from_gdk_event_key (event, &atk_event);
149
150 for (l = key_listener_list; l; l = l->next)
151 {
152 KeyEventListener *listener = l->data;
153
154 result |= listener->func (&atk_event, listener->data);
155 }
156
157 return result;
158 }