]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtkaccessibilityutil.c
filechooserbutton: Emit 'selection-changed' when changing the selection programmatically
[~andy/gtk] / gtk / a11y / gtkaccessibilityutil.c
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   atk_class->add_key_event_listener = add_key_event_listener;
105   atk_class->remove_key_event_listener = remove_key_event_listener;
106   atk_class->get_root = get_root;
107   atk_class->get_toolkit_name = get_toolkit_name;
108   atk_class->get_toolkit_version = get_toolkit_version;
109 }
110
111 static void
112 atk_key_event_from_gdk_event_key (GdkEventKey       *key,
113                                   AtkKeyEventStruct *event)
114 {
115   if (key->type == GDK_KEY_PRESS)
116     event->type = ATK_KEY_EVENT_PRESS;
117   else if (key->type == GDK_KEY_RELEASE)
118     event->type = ATK_KEY_EVENT_RELEASE;
119   else
120     g_assert_not_reached ();
121
122   event->state = key->state;
123   event->keyval = key->keyval;
124   event->length = key->length;
125   if (key->string && key->string[0] &&
126       (key->state & GDK_CONTROL_MASK ||
127        g_unichar_isgraph (g_utf8_get_char (key->string))))
128     event->string = key->string;
129   else
130     event->string = gdk_keyval_name (key->keyval);
131
132   event->keycode = key->hardware_keycode;
133   event->timestamp = key->time;
134 }
135
136 gboolean
137 _gtk_accessibility_key_snooper (GtkWidget   *widget,
138                                 GdkEventKey *event)
139 {
140   GSList *l;
141   AtkKeyEventStruct atk_event;
142   gboolean result;
143
144   result = FALSE;
145
146   atk_key_event_from_gdk_event_key (event, &atk_event);
147
148   for (l = key_listener_list; l; l = l->next)
149     {
150       KeyEventListener *listener = l->data;
151
152       result |= listener->func (&atk_event, listener->data);
153     }
154
155   return result;
156 }