nautilus-3.6.3/eel/eel-accessibility.c

No issues found

  1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
  2 /* eel-accessibility.h - Utility functions for accessibility
  3 
  4    Copyright (C) 2002 Anders Carlsson, Sun Microsystems, Inc.
  5 
  6    The Eel Library is free software; you can redistribute it and/or
  7    modify it under the terms of the GNU Library General Public License as
  8    published by the Free Software Foundation; either version 2 of the
  9    License, or (at your option) any later version.
 10 
 11    The Eel Library is distributed in the hope that it will be useful,
 12    but WITHOUT ANY WARRANTY; without even the implied warranty of
 13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14    Library General Public License for more details.
 15 
 16    You should have received a copy of the GNU Library General Public
 17    License along with the Eel Library; see the file COPYING.LIB.  If not,
 18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 19    Boston, MA 02111-1307, USA.
 20 
 21    Authors:
 22 	Anders Carlsson <andersca@gnu.org>
 23 	Michael Meeks   <michael@ximian.com>
 24 */
 25 #include <config.h>
 26 #include <gtk/gtk.h>
 27 #include <atk/atkrelationset.h>
 28 #include <eel/eel-accessibility.h>
 29 
 30 void
 31 eel_accessibility_set_up_label_widget_relation (GtkWidget *label, GtkWidget *widget)
 32 {
 33 	AtkObject *atk_widget, *atk_label;
 34 
 35 	atk_label = gtk_widget_get_accessible (label);
 36 	atk_widget = gtk_widget_get_accessible (widget);
 37 
 38 	/* Create the label -> widget relation */
 39 	atk_object_add_relationship (atk_label, ATK_RELATION_LABEL_FOR, atk_widget);
 40 
 41 	/* Create the widget -> label relation */
 42 	atk_object_add_relationship (atk_widget, ATK_RELATION_LABELLED_BY, atk_label);
 43 }
 44 
 45 GType
 46 eel_accessibility_create_accessible_gtype (const char *type_name,
 47 					   GtkWidget *widget,
 48 					   GClassInitFunc class_init)
 49 {
 50 	GType atk_type, parent_atk_type;
 51 	GTypeQuery query;
 52 	AtkObject *parent_atk;
 53 	GtkWidgetClass *parent_class, *klass;
 54 
 55 	if ((atk_type = g_type_from_name (type_name))) {
 56 		return atk_type;
 57 	}
 58 
 59 	klass = GTK_WIDGET_CLASS (G_OBJECT_GET_CLASS (widget));
 60 	parent_class = klass;
 61 
 62 	while (klass->get_accessible == parent_class->get_accessible) {
 63 		parent_class = g_type_class_peek_parent (parent_class);
 64 	}
 65 
 66 	parent_atk = parent_class->get_accessible (widget);
 67 	parent_atk_type = G_TYPE_FROM_INSTANCE (parent_atk);
 68 
 69 	if (!parent_atk_type) {
 70 		return G_TYPE_INVALID;
 71 	}
 72 
 73 	/* Figure out the size of the class and instance 
 74 	 * we are deriving from
 75 	 */
 76 	g_type_query (parent_atk_type, &query);
 77 
 78 	/* Register the type */
 79 	return g_type_register_static_simple (parent_atk_type, type_name,
 80 					      query.class_size, class_init,
 81 					      query.instance_size, NULL, 0);
 82 }
 83 
 84 
 85 static GQuark
 86 get_quark_accessible (void)
 87 {
 88 	static GQuark quark_accessible_object = 0;
 89 
 90 	if (!quark_accessible_object) {
 91 		quark_accessible_object = g_quark_from_static_string
 92 			("accessible-object");
 93 	}
 94 
 95 	return quark_accessible_object;
 96 }
 97 
 98 static GQuark
 99 get_quark_gobject (void)
100 {
101 	static GQuark quark_accessible_gobject = 0;
102 
103 	if (!quark_accessible_gobject) {
104 		quark_accessible_gobject = g_quark_from_static_string
105 			("object-for-accessible");
106 	}
107 
108 	return quark_accessible_gobject;
109 }
110 
111 /**
112  * eel_accessibility_get_atk_object:
113  * @object: a GObject of some sort
114  * 
115  * gets an AtkObject associated with a GObject
116  * 
117  * Return value: the associated accessible if one exists or NULL
118  **/
119 AtkObject *
120 eel_accessibility_get_atk_object (gpointer object)
121 {
122 	return g_object_get_qdata (object, get_quark_accessible ());
123 }
124 
125 /**
126  * eel_accessibility_get_gobject:
127  * @object: an AtkObject
128  * 
129  * gets the GObject associated with the AtkObject, for which
130  * @object provides accessibility support.
131  * 
132  * Return value: the accessible's associated GObject
133  **/
134 gpointer
135 eel_accessibility_get_gobject (AtkObject *object)
136 {
137 	return g_object_get_qdata (G_OBJECT (object), get_quark_gobject ());
138 }
139 
140 static void
141 eel_accessibility_destroy (gpointer data,
142 			   GObject *where_the_object_was)
143 {
144 	g_object_set_qdata
145 		(G_OBJECT (data), get_quark_gobject (), NULL);
146 	atk_object_notify_state_change
147 		(ATK_OBJECT (data), ATK_STATE_DEFUNCT, TRUE);
148 	g_object_unref (data);
149 }
150 
151 /**
152  * eel_accessibility_set_atk_object_return:
153  * @object: a GObject
154  * @atk_object: it's AtkObject
155  * 
156  * used to register and return a new accessible object for something
157  * 
158  * Return value: @atk_object.
159  **/
160 AtkObject *
161 eel_accessibility_set_atk_object_return (gpointer   object,
162 					 AtkObject *atk_object)
163 {
164 	atk_object_initialize (atk_object, object);
165 
166 	if (!ATK_IS_GOBJECT_ACCESSIBLE (atk_object)) {
167 		g_object_set_qdata_full
168 			(object, get_quark_accessible (), atk_object,
169 			 (GDestroyNotify)eel_accessibility_destroy);
170 		g_object_set_qdata
171 			(G_OBJECT (atk_object), get_quark_gobject (), object);
172 	}
173 
174 	return atk_object;
175 }
176 
177 static GailTextUtil *
178 get_simple_text (gpointer object)
179 {
180 	GObject *gobject;
181 	EelAccessibleTextIface *aif;
182 
183 	if (GTK_IS_ACCESSIBLE (object)) {
184 		gobject = G_OBJECT (gtk_accessible_get_widget (GTK_ACCESSIBLE (object)));
185 	} else {
186 		gobject = eel_accessibility_get_gobject (object);
187 	}
188 
189 	if (!gobject) {
190 		return NULL;
191 	}
192 
193 	aif = EEL_ACCESSIBLE_TEXT_GET_IFACE (gobject);
194 	if (!aif) {
195 		g_warning ("No accessible text inferface on '%s'",
196 			   g_type_name_from_instance ((gpointer) gobject));
197 
198 	} else if (aif->get_text) {
199 		return aif->get_text (gobject);
200 	}
201 
202 	return NULL;
203 }
204 
205 char *
206 eel_accessibility_text_get_text (AtkText *text,
207 				 gint     start_pos,
208 				 gint     end_pos)
209 {
210 	GailTextUtil *util = get_simple_text (text);
211 	g_return_val_if_fail (util != NULL, NULL);
212 
213 	return gail_text_util_get_substring (util, start_pos, end_pos);
214 }
215 
216 gunichar 
217 eel_accessibility_text_get_character_at_offset (AtkText *text,
218 						gint     offset)
219 {
220 	char *txt, *index;
221 	gint sucks1 = 0, sucks2 = -1;
222 	gunichar c;
223 	GailTextUtil *util = get_simple_text (text);
224 	g_return_val_if_fail (util != NULL, 0);
225 
226 	txt = gail_text_util_get_substring (util, sucks1, sucks2);
227 
228 	index = g_utf8_offset_to_pointer (txt, offset);
229 	c = g_utf8_get_char (index);
230 	g_free (txt);
231 
232 	return c;
233 }
234 
235 char *
236 eel_accessibility_text_get_text_before_offset (AtkText	      *text,
237 					       gint            offset,
238 					       AtkTextBoundary boundary_type,
239 					       gint           *start_offset,
240 					       gint           *end_offset)
241 {
242 	GailTextUtil *util = get_simple_text (text);
243 	g_return_val_if_fail (util != NULL, NULL);
244 
245 	return gail_text_util_get_text (
246 		util, NULL, GAIL_BEFORE_OFFSET, 
247 		boundary_type, offset, start_offset, end_offset);
248 }
249 
250 char *
251 eel_accessibility_text_get_text_at_offset (AtkText        *text,
252 					   gint            offset,
253 					   AtkTextBoundary boundary_type,
254 					   gint           *start_offset,
255 					   gint           *end_offset)
256 {
257 	GailTextUtil *util = get_simple_text (text);
258 	g_return_val_if_fail (util != NULL, NULL);
259 
260 	return gail_text_util_get_text (
261 		util, NULL, GAIL_AT_OFFSET,
262 		boundary_type, offset, start_offset, end_offset);
263 }
264 
265 gchar*
266 eel_accessibility_text_get_text_after_offset  (AtkText	      *text,
267 					       gint            offset,
268 					       AtkTextBoundary boundary_type,
269 					       gint           *start_offset,
270 					       gint           *end_offset)
271 {
272 	GailTextUtil *util = get_simple_text (text);
273 	g_return_val_if_fail (util != NULL, NULL);
274 
275 	return gail_text_util_get_text (
276 		util, NULL, GAIL_AFTER_OFFSET, 
277 		boundary_type, offset, start_offset, end_offset);
278 }
279 
280 gint
281 eel_accessibility_text_get_character_count (AtkText *text)
282 {
283 	GailTextUtil *util = get_simple_text (text);
284 	g_return_val_if_fail (util != NULL, -1);
285 
286 	return gtk_text_buffer_get_char_count (util->buffer);
287 }
288 
289 GType
290 eel_accessible_text_get_type (void)
291 {
292 	static GType type = 0;
293 
294 	if (!type) {
295 		const GTypeInfo tinfo = {
296 			sizeof (AtkTextIface),
297 			(GBaseInitFunc) NULL,
298 			(GBaseFinalizeFunc) NULL,
299 			(GClassInitFunc) NULL,
300 			(GClassFinalizeFunc) NULL
301 		};
302 
303 		type = g_type_register_static (
304 			G_TYPE_INTERFACE, "EelAccessibleText", &tinfo, 0);
305 	}
306 
307 	return type;
308 }