Filter:   InfoImg
download bonobo-a11y.c
Language: C
Copyright: Copyright 2002 Sun Microsystems, Inc.
LOC: 272
Project Info
libbonoboui
Server: Gnome
Type: cvs
...onoboui\libbonoboui\bonobo\
   .cvsignore
   bonobo-a11y.c
   bonobo-a11y.h
   bonobo-canvas-component.c
   bonobo-canvas-component.h
   bonobo-canvas-item.c
   bonobo-canvas-item.h
   bonobo-control-frame.c
   bonobo-control-frame.h
   bonobo-control-internal.h
   bonobo-control.c
   bonobo-control.h
   bonobo-dock-band.c
   bonobo-dock-band.h
   bonobo-dock-item-grip.c
   bonobo-dock-item-grip.h
   bonobo-dock-item.c
   bonobo-dock-item.h
   bonobo-dock-layout.c
   bonobo-dock-layout.h
   bonobo-dock.c
   bonobo-dock.h
   ...bo-embeddable-factory.h
   ...bo-file-selector-util.c
   ...bo-file-selector-util.h
   ...bo-insert-component.xpm
   bonobo-plug.c
   bonobo-plug.h
   bonobo-property-control.c
   bonobo-property-control.h
   bonobo-selector-widget.c
   bonobo-selector-widget.h
   bonobo-selector.c
   bonobo-selector.h
   bonobo-socket.c
   bonobo-socket.h
   bonobo-ui-component.c
   bonobo-ui-component.h
   bonobo-ui-config-widget.c
   bonobo-ui-config-widget.h
   bonobo-ui-container.c
   bonobo-ui-container.h
   bonobo-ui-engine-config.c
   bonobo-ui-engine-config.h
   bonobo-ui-engine-private.h
   bonobo-ui-engine.c
   bonobo-ui-engine.h
   bonobo-ui-init-gtk.c
   ...o-ui-internal-toolbar.c
   bonobo-ui-main.c
   bonobo-ui-main.h
   bonobo-ui-marshal-main.c
   bonobo-ui-marshal.list
   bonobo-ui-node-private.h
   bonobo-ui-node.c
   bonobo-ui-node.h
   bonobo-ui-preferences.c
   bonobo-ui-preferences.h
   bonobo-ui-private.h
   bonobo-ui-sync-keys.c
   bonobo-ui-sync-keys.h
   bonobo-ui-sync-menu.c
   bonobo-ui-sync-menu.h
   bonobo-ui-sync-status.c
   bonobo-ui-sync-status.h
   bonobo-ui-sync-toolbar.c
   bonobo-ui-sync-toolbar.h
   bonobo-ui-sync.c
   bonobo-ui-sync.h
   ...i-toolbar-button-item.c
   ...i-toolbar-button-item.h
   ...-toolbar-control-item.c
   ...-toolbar-control-item.h
   bonobo-ui-toolbar-item.c
   bonobo-ui-toolbar-item.h
   ...ui-toolbar-popup-item.c
   ...ui-toolbar-popup-item.h
   ...ar-toggle-button-item.c
   ...ar-toggle-button-item.h
   bonobo-ui-toolbar.c
   bonobo-ui-toolbar.h
   bonobo-ui-util.c
   bonobo-ui-util.h
   bonobo-ui-win32.c
   bonobo-ui-xml.c
   bonobo-ui-xml.h
   bonobo-widget.c
   bonobo-widget.h
   bonobo-window.c
   bonobo-window.h
   bonobo-wrapper.h
   bonobo-zoomable-frame.c
   bonobo-zoomable-frame.h
   bonobo-zoomable.c
   bonobo-zoomable.h
   ...i-2.0-uninstalled.pc.in
   libbonoboui-2.0.pc.in
   Makefile.am

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/**
 * Bonobo accessibility helpers
 *
 * Author:
 *   Michael Meeks (michael@ximian.com)
 *
 * Copyright 2002 Sun Microsystems, Inc.
 */
#include "config.h"
#include <glib/garray.h>
#include <atk/atkregistry.h>
#include <atk/atkobjectfactory.h>
#include <gtk/gtkwidget.h>
#include <bonobo-a11y.h>

GType
bonobo_a11y_get_derived_type_for (GType                 widget_type,
				  const char           *gail_parent_class,
				  BonoboA11YClassInitFn class_init)
{
	GType type;
	GType parent_atk_type;
	GTypeInfo tinfo = { 0 };
	GTypeQuery query;
	char *type_name;
		
	parent_atk_type = g_type_from_name (
		gail_parent_class ? gail_parent_class : "GailWidget");

	g_return_val_if_fail (parent_atk_type != G_TYPE_INVALID, G_TYPE_INVALID);

	/*
	 * Figure out the size of the class and instance 
	 * we are deriving from
	 */
	g_type_query (parent_atk_type, &query);

	tinfo.class_init    = (GClassInitFunc) class_init;
	tinfo.class_size    = query.class_size;
	tinfo.instance_size = query.instance_size;

	/* Make up a name */
	type_name = g_strconcat (g_type_name (widget_type),
				 "Accessible", NULL);

	/* Register the type */
	type = g_type_register_static (
		parent_atk_type, type_name, &tinfo, 0);

	g_free (type_name);
		
	return type;
}

AtkObject *
bonobo_a11y_create_accessible_for (GtkWidget            *widget,
				   const char           *gail_parent_class,
				   BonoboA11YClassInitFn class_init,
				   GType                 first_interface_type,
				   ...)
{
	va_list args;
	AtkObject *accessible;
	GType type, widget_type;
	static GHashTable *type_hash = NULL;

	va_start (args, first_interface_type);

	accessible = bonobo_a11y_get_atk_object (widget);

	if (accessible)
		goto out;

	if (!type_hash)
		type_hash = g_hash_table_new (NULL, NULL);

	widget_type = G_TYPE_FROM_INSTANCE (widget);
	type = (GType) g_hash_table_lookup (type_hash, (gpointer) widget_type);

	if (!type) {
		GType it;

		type = bonobo_a11y_get_derived_type_for (
			widget_type, gail_parent_class, class_init);

		g_return_val_if_fail (type != G_TYPE_INVALID, NULL);

		for (it = first_interface_type; it; it = va_arg (args, GType)) {
			const GInterfaceInfo *if_info = va_arg (args, gpointer);
			
			g_type_add_interface_static (type, it, if_info);
		}

		g_hash_table_insert (type_hash,
				     (gpointer) widget_type,
				     (gpointer) type);
	}

	g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
					    
	accessible = g_object_new (type, NULL);

	bonobo_a11y_set_atk_object_ret (widget, accessible);

 out:
	va_end (args);

	return accessible;
}

static GQuark
get_quark_accessible (void)
{
	static GQuark quark_accessible_object = 0;

	if (!quark_accessible_object)
		quark_accessible_object = g_quark_from_static_string (
			"gtk-accessible-object");

	return quark_accessible_object;
}

AtkObject *
bonobo_a11y_get_atk_object (gpointer widget)
{
	return g_object_get_qdata (widget, get_quark_accessible ());
}

AtkObject *
bonobo_a11y_set_atk_object_ret (GtkWidget *widget,
				AtkObject *object)
{
	atk_object_initialize (object, widget);
	
	g_object_set_qdata (
		G_OBJECT (widget),
		get_quark_accessible (),
		object);

	return object;
}

typedef struct {
	AtkActionIface  chain;
	GArray         *actions;
} BonoboActionInterface;

typedef struct {
	AtkActionIface  chain;
	GArray         *actions;
} BonoboActionIfData;

typedef struct {
	char *name;
	char *description;
	char *keybinding;
} BonoboAction;

#define BONOBO_TYPE_ACTION bonobo_a11y_action_get_type ()

static GType
bonobo_a11y_action_get_type (void)
{
	static GType type = 0;

	if (!type) {
		GTypeInfo tinfo = {
			sizeof (BonoboActionInterface),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,

		};

		type = g_type_register_static (
			G_TYPE_INTERFACE, "BonoboAction", &tinfo, 0);
	}

	return type;
}

static gboolean
bonobo_a11y_action_do (AtkAction *action,
		       gint       i)
{
	BonoboActionInterface *aif = 
		G_TYPE_INSTANCE_GET_INTERFACE (
			action, BONOBO_TYPE_ACTION, BonoboActionInterface);

	if (aif->chain.do_action)
		return aif->chain.do_action (action, i);

	else
		g_warning ("bonobo a11y action %d unimplemented on %p",
			   i, action);

	return FALSE;
}

static gint
bonobo_a11y_action_get_n (AtkAction *action)
{
	int i, count;
	BonoboActionInterface *aif = 
		G_TYPE_INSTANCE_GET_INTERFACE (
			action, BONOBO_TYPE_ACTION, BonoboActionInterface);

	if (aif->chain.get_n_actions)
		return aif->chain.get_n_actions (action);

	for (i = count = 0; i < aif->actions->len; i++) {
		if (g_array_index (aif->actions, BonoboAction, i).name)
			count++;
	}

	return count;
}

static G_CONST_RETURN gchar *
bonobo_a11y_action_get_description (AtkAction *action,
				    gint       i)
{
	BonoboActionInterface *aif = 
		G_TYPE_INSTANCE_GET_INTERFACE (
			action, BONOBO_TYPE_ACTION, BonoboActionInterface);

	if (aif->chain.get_description)
		return aif->chain.get_description (action, i);

	/* I refuse to handle set_description it seems uber ugly */
	if (i >= 0 && i < aif->actions->len &&
	    g_array_index (aif->actions, BonoboAction, i).description)
		return g_array_index (aif->actions, BonoboAction, i).description;
	else
		return NULL;
}

static G_CONST_RETURN gchar *
bonobo_a11y_action_get_name (AtkAction *action,
			     gint       i)
{
	BonoboActionInterface *aif = 
		G_TYPE_INSTANCE_GET_INTERFACE (
			action, BONOBO_TYPE_ACTION, BonoboActionInterface);

	if (aif->chain.get_name)
		return aif->chain.get_name (action, i);

	if (i >= 0 && i < aif->actions->len &&
	    g_array_index (aif->actions, BonoboAction, i).name)
		return g_array_index (aif->actions, BonoboAction, i).name;
	else
		return NULL;
}

static G_CONST_RETURN gchar *
bonobo_a11y_action_get_keybinding (AtkAction *action,
				   gint       i)
{
	BonoboActionInterface *aif = 
		G_TYPE_INSTANCE_GET_INTERFACE (
			action, BONOBO_TYPE_ACTION, BonoboActionInterface);

	if (aif->chain.get_keybinding)
		return aif->chain.get_keybinding (action, i);

	if (i >= 0 && i < aif->actions->len &&
	    g_array_index (aif->actions, BonoboAction, i).keybinding)
		return g_array_index (aif->actions, BonoboAction, i).keybinding;
	else
		return NULL;
}

static gboolean
bonobo_a11y_action_set_description (AtkAction   *action,
				    gint         i,
				    const gchar *desc)
{
	BonoboActionInterface *aif = 
		G_TYPE_INSTANCE_GET_INTERFACE (
			action, BONOBO_TYPE_ACTION, BonoboActionInterface);

	if (aif->chain.set_description)
		return aif->chain.set_description (action, i, desc);

	/* I refuse to handle set_description it seems uber ugly */
	return FALSE;
}

static void
bonobo_a11y_action_if_init (gpointer g_iface,
			    gpointer iface_data)
{
	BonoboActionIfData *aifd = iface_data;
	BonoboActionInterface *aif = g_iface;

	aif->chain   = aifd->chain;
	aif->actions = aifd->actions;
}

static void
bonobo_a11y_atk_action_if_init (gpointer g_iface,
				gpointer iface_data)
{
	AtkActionIface *aif = g_iface;

	aif->do_action       = bonobo_a11y_action_do;
	aif->get_n_actions   = bonobo_a11y_action_get_n;
	aif->get_description = bonobo_a11y_action_get_description;
	aif->get_name        = bonobo_a11y_action_get_name;
	aif->get_keybinding  = bonobo_a11y_action_get_keybinding;
	aif->set_description = bonobo_a11y_action_set_description;
}

static void
bonobo_a11y_action_if_finalize (gpointer g_iface,
				gpointer iface_data)
{
	/* FIXME: should we free aifd->actions etc. here ? */
}

void
bonobo_a11y_add_actions_interface (GType           a11y_object_type,
				   AtkActionIface *chain,
				   /* triplets of: */
				   int             first_id,
				   /* char * action name */
				   /* char * initial action description */
				   /* char * keybinding descr. */
				   ...)
{
	va_list args; 
	int     id;
	GInterfaceInfo iinfo;
	BonoboActionIfData *aifd = g_new0 (BonoboActionIfData, 1);

	va_start (args, first_id);

	aifd->chain = *chain;

	aifd->actions = g_array_new (FALSE, TRUE, sizeof (BonoboAction));

	for (id = first_id; id >= 0; id = va_arg (args, int)) {
		if (id >= aifd->actions->len)
			g_array_set_size (aifd->actions, id + 2);

		g_array_index (aifd->actions, BonoboAction, id).name =
			g_strdup (va_arg (args, char *));
		g_array_index (aifd->actions, BonoboAction, id).description =
			g_strdup (va_arg (args, char *));
		g_array_index (aifd->actions, BonoboAction, id).keybinding =
			g_strdup (va_arg (args, char *));
	}

	iinfo.interface_init     = bonobo_a11y_action_if_init;
	iinfo.interface_finalize = bonobo_a11y_action_if_finalize;
	iinfo.interface_data     = aifd;

	g_type_add_interface_static (
		a11y_object_type,
		bonobo_a11y_action_get_type (),
		&iinfo);

	iinfo.interface_init     = bonobo_a11y_atk_action_if_init;
	iinfo.interface_finalize = NULL;
	iinfo.interface_data     = NULL;

	g_type_add_interface_static (
		a11y_object_type, ATK_TYPE_ACTION, &iinfo);

	va_end (args);
}