Debian Codegraph @

gtk/a11y/gtkbuttonaccessible.c

raw
1 /* GTK+ - accessibility implementations
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, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include <string.h>
21 #include <gtk/gtk.h>
22 #include <glib/gi18n-lib.h>
23 #include "gtkbuttonaccessible.h"
24
25
26 static void atk_action_interface_init (AtkActionIface *iface);
27 static void atk_image_interface_init (AtkImageIface *iface);
28
29 G_DEFINE_TYPE_WITH_CODE (GtkButtonAccessible, gtk_button_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
30 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)
31 G_IMPLEMENT_INTERFACE (ATK_TYPE_IMAGE, atk_image_interface_init))
32
33 static void
34 gtk_button_accessible_initialize (AtkObject *obj,
35 gpointer data)
36 {
37 GtkWidget *parent;
38
39 ATK_OBJECT_CLASS (gtk_button_accessible_parent_class)->initialize (obj, data);
40
41 parent = gtk_widget_get_parent (gtk_accessible_get_widget (GTK_ACCESSIBLE (obj)));
42 if (GTK_IS_TREE_VIEW (parent))
43 {
44 /* Even though the accessible parent of the column header will
45 * be reported as the table because the parent widget of the
46 * GtkTreeViewColumn's button is the GtkTreeView we set
47 * the accessible parent for column header to be the table
48 * to ensure that atk_object_get_index_in_parent() returns
49 * the correct value; see gail_widget_get_index_in_parent().
50 */
51 atk_object_set_parent (obj, gtk_widget_get_accessible (parent));
52 obj->role = ATK_ROLE_TABLE_COLUMN_HEADER;
53 }
54 else
55 obj->role = ATK_ROLE_PUSH_BUTTON;
56 }
57
58 static GtkWidget *
59 get_image_from_button (GtkWidget *button)
60 {
61 GtkWidget *image;
62
63 image = gtk_button_get_image (GTK_BUTTON (button));
64 if (GTK_IS_IMAGE (image))
65 return image;
66
67 return NULL;
68 }
69
70 static GtkWidget *
71 find_label_child (GtkContainer *container)
72 {
73 GList *children, *tmp_list;
74 GtkWidget *child;
75
76 children = gtk_container_get_children (container);
77
78 child = NULL;
79 for (tmp_list = children; tmp_list != NULL; tmp_list = tmp_list->next)
80 {
81 if (GTK_IS_LABEL (tmp_list->data))
82 {
83 child = GTK_WIDGET (tmp_list->data);
84 break;
85 }
86 else if (GTK_IS_CONTAINER (tmp_list->data))
87 {
88 child = find_label_child (GTK_CONTAINER (tmp_list->data));
89 if (child)
90 break;
91 }
92 }
93 g_list_free (children);
94 return child;
95 }
96
97 static GtkWidget *
98 get_label_from_button (GtkWidget *button)
99 {
100 GtkWidget *child;
101
102 child = gtk_bin_get_child (GTK_BIN (button));
103 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
104 if (GTK_IS_ALIGNMENT (child))
105 child = gtk_bin_get_child (GTK_BIN (child));
106 G_GNUC_END_IGNORE_DEPRECATIONS
107
108 if (GTK_IS_CONTAINER (child))
109 child = find_label_child (GTK_CONTAINER (child));
110 else if (!GTK_IS_LABEL (child))
111 child = NULL;
112
113 return child;
114 }
115
116 static const gchar *
117 gtk_button_accessible_get_name (AtkObject *obj)
118 {
119 const gchar *name = NULL;
120 GtkWidget *widget;
121 GtkWidget *child;
122
123 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
124 if (widget == NULL)
125 return NULL;
126
127 name = ATK_OBJECT_CLASS (gtk_button_accessible_parent_class)->get_name (obj);
128 if (name != NULL)
129 return name;
130
131 child = get_label_from_button (widget);
132 if (GTK_IS_LABEL (child))
133 name = gtk_label_get_text (GTK_LABEL (child));
134 else
135 {
136 GtkWidget *image;
137
138 image = get_image_from_button (widget);
139 if (GTK_IS_IMAGE (image))
140 {
141 AtkObject *atk_obj;
142
143 atk_obj = gtk_widget_get_accessible (image);
144 name = atk_object_get_name (atk_obj);
145 }
146 }
147
148 return name;
149 }
150
151 static gint
152 gtk_button_accessible_get_n_children (AtkObject* obj)
153 {
154 return 0;
155 }
156
157 static AtkObject *
158 gtk_button_accessible_ref_child (AtkObject *obj,
159 gint i)
160 {
161 return NULL;
162 }
163
164 static AtkStateSet *
165 gtk_button_accessible_ref_state_set (AtkObject *obj)
166 {
167 AtkStateSet *state_set;
168 GtkWidget *widget;
169
170 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
171 if (widget == NULL)
172 return NULL;
173
174 state_set = ATK_OBJECT_CLASS (gtk_button_accessible_parent_class)->ref_state_set (obj);
175
176 if ((gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_ACTIVE) != 0)
177 atk_state_set_add_state (state_set, ATK_STATE_ARMED);
178
179 if (!gtk_widget_get_can_focus (widget))
180 atk_state_set_remove_state (state_set, ATK_STATE_SELECTABLE);
181
182 return state_set;
183 }
184
185 static void
186 gtk_button_accessible_notify_gtk (GObject *obj,
187 GParamSpec *pspec)
188 {
189 GtkWidget *widget = GTK_WIDGET (obj);
190 AtkObject *atk_obj = gtk_widget_get_accessible (widget);
191
192 if (strcmp (pspec->name, "label") == 0)
193 {
194 if (atk_obj->name == NULL)
195 g_object_notify (G_OBJECT (atk_obj), "accessible-name");
196
197 g_signal_emit_by_name (atk_obj, "visible-data-changed");
198 }
199 else
200 GTK_WIDGET_ACCESSIBLE_CLASS (gtk_button_accessible_parent_class)->notify_gtk (obj, pspec);
201 }
202
203 static void
204 gtk_button_accessible_class_init (GtkButtonAccessibleClass *klass)
205 {
206 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
207 GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
208 GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
209
210 class->get_name = gtk_button_accessible_get_name;
211 class->get_n_children = gtk_button_accessible_get_n_children;
212 class->ref_child = gtk_button_accessible_ref_child;
213 class->ref_state_set = gtk_button_accessible_ref_state_set;
214 class->initialize = gtk_button_accessible_initialize;
215
216 widget_class->notify_gtk = gtk_button_accessible_notify_gtk;
217
218 container_class->add_gtk = NULL;
219 container_class->remove_gtk = NULL;
220 }
221
222 static void
223 gtk_button_accessible_init (GtkButtonAccessible *button)
224 {
225 }
226
227 static gboolean
228 gtk_button_accessible_do_action (AtkAction *action,
229 gint i)
230 {
231 GtkWidget *widget;
232
233 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
234 if (widget == NULL)
235 return FALSE;
236
237 if (i != 0)
238 return FALSE;
239
240 if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
241 return FALSE;
242
243 gtk_button_clicked (GTK_BUTTON (widget));
244 return TRUE;
245 }
246
247 static gint
248 gtk_button_accessible_get_n_actions (AtkAction *action)
249 {
250 return 1;
251 }
252
253 static const gchar *
254 gtk_button_accessible_get_keybinding (AtkAction *action,
255 gint i)
256 {
257 gchar *return_value = NULL;
258 GtkWidget *widget;
259 GtkWidget *label;
260 guint key_val;
261
262 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
263 if (widget == NULL)
264 return NULL;
265
266 if (i != 0)
267 return NULL;
268
269 label = get_label_from_button (widget);
270 if (GTK_IS_LABEL (label))
271 {
272 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
273 if (key_val != GDK_KEY_VoidSymbol)
274 return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
275 }
276 if (return_value == NULL)
277 {
278 /* Find labelled-by relation */
279 AtkRelationSet *set;
280 AtkRelation *relation;
281 GPtrArray *target;
282 gpointer target_object;
283
284 set = atk_object_ref_relation_set (ATK_OBJECT (action));
285 if (set)
286 {
287 relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
288 if (relation)
289 {
290 target = atk_relation_get_target (relation);
291 target_object = g_ptr_array_index (target, 0);
292 label = gtk_accessible_get_widget (GTK_ACCESSIBLE (target_object));
293 }
294 g_object_unref (set);
295 }
296
297 if (GTK_IS_LABEL (label))
298 {
299 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
300 if (key_val != GDK_KEY_VoidSymbol)
301 return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
302 }
303 }
304 return return_value;
305 }
306
307 static const gchar *
308 gtk_button_accessible_action_get_name (AtkAction *action,
309 gint i)
310 {
311 if (i == 0)
312 return "click";
313 return NULL;
314 }
315
316 static const gchar *
317 gtk_button_accessible_action_get_localized_name (AtkAction *action,
318 gint i)
319 {
320 if (i == 0)
321 return C_("Action name", "Click");
322 return NULL;
323 }
324
325 static const gchar *
326 gtk_button_accessible_action_get_description (AtkAction *action,
327 gint i)
328 {
329 if (i == 0)
330 return C_("Action description", "Clicks the button");
331 return NULL;
332 }
333
334 static void
335 atk_action_interface_init (AtkActionIface *iface)
336 {
337 iface->do_action = gtk_button_accessible_do_action;
338 iface->get_n_actions = gtk_button_accessible_get_n_actions;
339 iface->get_keybinding = gtk_button_accessible_get_keybinding;
340 iface->get_name = gtk_button_accessible_action_get_name;
341 iface->get_localized_name = gtk_button_accessible_action_get_localized_name;
342 iface->get_description = gtk_button_accessible_action_get_description;
343 }
344
345 static const gchar *
346 gtk_button_accessible_get_image_description (AtkImage *image)
347 {
348 GtkWidget *widget;
349 GtkWidget *button_image;
350 AtkObject *obj;
351
352 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
353 if (widget == NULL)
354 return NULL;
355
356 button_image = get_image_from_button (widget);
357 if (GTK_IS_IMAGE (button_image))
358 {
359 obj = gtk_widget_get_accessible (button_image);
360 return atk_image_get_image_description (ATK_IMAGE (obj));
361 }
362
363 return NULL;
364 }
365
366 static void
367 gtk_button_accessible_get_image_position (AtkImage *image,
368 gint *x,
369 gint *y,
370 AtkCoordType coord_type)
371 {
372 GtkWidget *widget;
373 GtkWidget *button_image;
374 AtkObject *obj;
375
376 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
377 if (widget == NULL)
378 {
379 *x = G_MININT;
380 *y = G_MININT;
381 return;
382 }
383
384 button_image = get_image_from_button (widget);
385 if (button_image != NULL)
386 {
387 obj = gtk_widget_get_accessible (button_image);
388 atk_component_get_extents (ATK_COMPONENT (obj), x, y, NULL, NULL,
389 coord_type);
390 }
391 else
392 {
393 *x = G_MININT;
394 *y = G_MININT;
395 }
396 }
397
398 static void
399 gtk_button_accessible_get_image_size (AtkImage *image,
400 gint *width,
401 gint *height)
402 {
403 GtkWidget *widget;
404 GtkWidget *button_image;
405 AtkObject *obj;
406
407 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
408 if (widget == NULL)
409 {
410 *width = -1;
411 *height = -1;
412 return;
413 }
414
415 button_image = get_image_from_button (widget);
416 if (GTK_IS_IMAGE (button_image))
417 {
418 obj = gtk_widget_get_accessible (GTK_WIDGET (button_image));
419 atk_image_get_image_size (ATK_IMAGE (obj), width, height);
420 }
421 else
422 {
423 *width = -1;
424 *height = -1;
425 }
426 }
427
428 static gboolean
429 gtk_button_accessible_set_image_description (AtkImage *image,
430 const gchar *description)
431 {
432 GtkWidget *widget;
433 GtkWidget *button_image;
434 AtkObject *obj;
435
436 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
437
438 if (widget == NULL)
439 return FALSE;
440
441 button_image = get_image_from_button (widget);
442 if (GTK_IMAGE (button_image))
443 {
444 obj = gtk_widget_get_accessible (GTK_WIDGET (button_image));
445 return atk_image_set_image_description (ATK_IMAGE (obj), description);
446 }
447
448 return FALSE;
449 }
450
451 static void
452 atk_image_interface_init (AtkImageIface *iface)
453 {
454 iface->get_image_description = gtk_button_accessible_get_image_description;
455 iface->get_image_position = gtk_button_accessible_get_image_position;
456 iface->get_image_size = gtk_button_accessible_get_image_size;
457 iface->set_image_description = gtk_button_accessible_set_image_description;
458 }