File: htmlviewaccessible.c

package info (click to toggle)
libgtkhtml2 2.11.0-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,740 kB
  • ctags: 3,830
  • sloc: ansic: 33,179; sh: 8,414; makefile: 597
file content (426 lines) | stat: -rw-r--r-- 11,260 bytes parent folder | download | duplicates (3)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <string.h>

#include "htmlviewaccessible.h"
#include "htmlviewaccessiblefactory.h"
#include <libgtkhtml/layout/htmlboxinline.h>
#include <libgtkhtml/layout/htmlboxtext.h>

#undef DUMP_BOXES

static void       html_view_accessible_class_init          (HtmlViewAccessibleClass *klass);
static void       html_view_accessible_finalize            (GObject            *obj);
static void       html_view_accessible_initialize          (AtkObject          *obj,
                                                            gpointer           data);
static gint       html_view_accessible_get_n_children      (AtkObject          *obj);
static AtkObject* html_view_accessible_ref_child           (AtkObject          *obj,
                                                            gint               i);
static AtkStateSet* html_view_accessible_ref_state_set     (AtkObject          *obj);

static void       html_view_accessible_grab_focus_cb       (GtkWidget          *widget);
static AtkObject* html_view_accessible_get_focus_object    (GtkWidget          *widget,
                                                            gint               *link_index);

static void       focus_object_destroyed                   (gpointer           data);       
static void       root_object_destroyed                    (gpointer           data);
static void       set_root_object                          (GObject            *obj,
                                                            HtmlBox            *root);

static gpointer parent_class = NULL;

static const gchar* gail_focus_object = "gail-focus-object";
static const gchar* html_root = "html_root";

#ifdef DUMP_BOXES
static void
debug_dump_boxes (HtmlBox *root, gint indent, gboolean has_node, xmlNode *n)
{
	HtmlBox *box;
        gint i;

	if (!root)
		return;

	if (has_node) {
		if (root->dom_node != NULL && root->dom_node->xmlnode != n)
			return;
        }

	 box = root->children;


	 for (i = 0; i < indent; i++)
		g_print (" ");

	g_print ("Type %d: %s %s (%p, %p, %p) (%d %d %d %d)",
                 indent,
		 G_OBJECT_TYPE_NAME (root), G_OBJECT_TYPE_NAME (root->dom_node), root, root->dom_node, HTML_BOX_GET_STYLE (root), root->x, root->y, root->width, root->height);
	if (root->dom_node)
		g_print ("%s ", root->dom_node->xmlnode->name);
	g_print ("\n");
	if (HTML_IS_BOX_TEXT (root)) {
		HtmlBoxText *box_text;
		gint len;
		gchar *text;

		box_text = HTML_BOX_TEXT (root);
		text = html_box_text_get_text (box_text, &len);
		g_print ("Master: %p forced_newline: %d\n", box_text->master, box_text->forced_newline);
  		if (len) {
                        gchar *buffer;

			for (i = 0; i < indent; i++)
				g_print (" ");
			buffer = g_malloc (len + 4);
			buffer[0] = '|';
			strncpy (buffer + 1, text, len);
			buffer[len + 1] = '|';
			buffer[len + 2] = '\n';
			buffer[len + 3] = 0;
			g_print (buffer);
			g_free (buffer);
		}
	}

        while (box) {
	  debug_dump_boxes (box, indent + 1, has_node, n);
	  box = box->next;
	}
}
#endif

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

	if (!type) {
		static GTypeInfo tinfo = {
			0, /* class size */
			(GBaseInitFunc) NULL, /* base init */
			(GBaseFinalizeFunc) NULL, /* base finalize */
			(GClassInitFunc) html_view_accessible_class_init,
			(GClassFinalizeFunc) NULL, /* class finalize */
			NULL, /* class data */
			0, /* instance size */
			0, /* nb preallocs */
			(GInstanceInitFunc) NULL, /* instance init */
			NULL /* value table */
		};

		/*
		 * Figure out the size of the class and instance 
		 * we are deriving from
		 */
		AtkObjectFactory *factory;
		GType derived_type;
		GTypeQuery query;
		GType derived_atk_type;

		derived_type = g_type_parent (HTML_TYPE_VIEW);
		factory = atk_registry_get_factory (atk_get_default_registry (), derived_type);
		derived_atk_type = atk_object_factory_get_accessible_type (factory);
		g_type_query (derived_atk_type, &query);
		tinfo.class_size = query.class_size;
		tinfo.instance_size = query.instance_size;

		type = g_type_register_static (derived_atk_type, "HtmlViewAccessible", &tinfo, 0);
	}

	return type;
}

static void
html_view_accessible_class_init (HtmlViewAccessibleClass *klass)
{
	AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

	parent_class = g_type_class_peek_parent (klass);

	gobject_class->finalize = html_view_accessible_finalize;

	class->get_n_children = html_view_accessible_get_n_children;
	class->initialize = html_view_accessible_initialize;
	class->ref_child = html_view_accessible_ref_child;
	class->ref_state_set = html_view_accessible_ref_state_set;
}

AtkObject* 
html_view_accessible_new (GtkWidget *widget)
{
	GObject *object;
	AtkObject *accessible;

	object = g_object_new (HTML_TYPE_VIEW_ACCESSIBLE, NULL);

	accessible = ATK_OBJECT (object);
	atk_object_initialize (accessible, widget);
	accessible->role =  ATK_ROLE_HTML_CONTAINER;
	return accessible;
}

static void
html_view_accessible_initialize (AtkObject *obj,
                                 gpointer  data)
{
	GtkWidget *widget;
	HtmlView *view;

	ATK_OBJECT_CLASS (parent_class)->initialize (obj, data);

	widget = GTK_WIDGET (data);
 	view = HTML_VIEW (data);
	set_root_object (G_OBJECT (obj), view->root);
	g_signal_connect_after (widget, "grab_focus",
			        G_CALLBACK (html_view_accessible_grab_focus_cb),
                                NULL);
}

static void
html_view_accessible_finalize (GObject *obj)
{
	gpointer focus_obj;

	focus_obj = g_object_get_data (obj, gail_focus_object);
	if (focus_obj) {
		g_object_weak_unref (focus_obj, 
				     (GWeakNotify) focus_object_destroyed,
				     obj);
	}
	G_OBJECT_CLASS (parent_class)->finalize (obj);
}

static gint 
html_view_accessible_get_n_children (AtkObject* obj)
{
	GtkAccessible *accessible;
	GtkWidget *widget;
	HtmlView *html_view;
	gint n_children = 0;

	g_return_val_if_fail (HTML_IS_VIEW_ACCESSIBLE (obj), 0);
	accessible = GTK_ACCESSIBLE (obj);
	widget = accessible->widget;
	if (widget == NULL)
		/* State is defunct */
		return 0;

	g_return_val_if_fail (HTML_IS_VIEW (widget), 0);

	html_view = HTML_VIEW (widget);

	if (html_view->root)
		n_children = 1;

	return n_children;
}

static AtkObject* 
html_view_accessible_ref_child (AtkObject *obj, gint i)
{
	GtkAccessible *accessible;
	GtkWidget *widget;
	HtmlView *html_view;
	HtmlBox *html_box;
	AtkObject *atk_child = NULL;

	if (i != 0)
		return NULL;

	g_return_val_if_fail (HTML_IS_VIEW_ACCESSIBLE (obj), NULL);
	accessible = GTK_ACCESSIBLE (obj);
	widget = accessible->widget;
	if (widget == NULL)
		/* State is defunct */
		return NULL;

	g_return_val_if_fail (HTML_IS_VIEW (widget), NULL);

	html_view = HTML_VIEW (widget);

	html_box = html_view->root;

	if (html_box) {
		gpointer old_root;

		atk_child = atk_gobject_accessible_for_object (G_OBJECT (html_box));
		g_object_set_data (G_OBJECT (html_box), "view", widget);
		g_object_ref (atk_child);
		/* We check whether the root node has changed */	
		old_root = g_object_get_data (G_OBJECT (obj), html_root);
		if (!old_root) {
			set_root_object (G_OBJECT (obj), html_box);
			g_signal_emit_by_name (obj, "children_changed::add", 0, NULL, NULL);
		}
	}
	return atk_child;
}

static void
html_view_accessible_grab_focus_cb (GtkWidget *widget)
{
	AtkObject *focus_object;
	AtkObject *obj;
	gint link_index;

	focus_object = html_view_accessible_get_focus_object (widget, &link_index);

	obj = gtk_widget_get_accessible (widget);
	if (GTK_WIDGET_HAS_FOCUS (widget)) {
		if (focus_object) {
			atk_focus_tracker_notify (focus_object);
			g_signal_emit_by_name (focus_object, "link-selected", link_index);
		} else {
			atk_focus_tracker_notify (obj);
		}
	}
}

static gboolean
get_link_index (HtmlBox *root, HtmlBox *link_box, gint *link_index)
{
	HtmlBox *box;
	gboolean ret;

	if (!root)
		return FALSE;

	if (HTML_IS_BOX_INLINE (root)) {
		if (root == link_box)
			return TRUE;
		*link_index++;
	}
	
	box = root->children;
	while (box) {
		ret = get_link_index (box, link_box, link_index);
		if (ret)
			return TRUE;
		box = box->next;
	}
	return FALSE;
}

static AtkObject*
html_view_accessible_get_focus_object (GtkWidget *widget, gint *link_index)
{
	HtmlView *view;
	HtmlBox *box;
	HtmlBox *focus_box;
	HtmlBox *parent;
	DomElement *focus_element;
	AtkObject *atk_obj;
	gint index;

	view = HTML_VIEW (widget);

	focus_element = view->document->focus_element;

	if (focus_element) {
		focus_box = box = html_view_find_layout_box (view, DOM_NODE (focus_element), FALSE);	
		parent = box->parent;
		while (parent) {
			if (!HTML_IS_BOX_BLOCK (parent)) {
				parent = parent->parent;
			} else {
				box = parent;
				break;
			}
		}
		g_assert (HTML_IS_BOX_BLOCK (box));
		if (box->dom_node && strcmp ((char *)box->dom_node->xmlnode->name, "p") == 0) {
			if (link_index) {
				index = 0;
				if (get_link_index (box, focus_box, &index))
					*link_index = index;
			}
		} else {
			box = focus_box->children;
			if (link_index) {
				*link_index = 0;
			}
		}
		g_object_set_data (G_OBJECT (box), "view", widget);
		atk_obj = atk_gobject_accessible_for_object (G_OBJECT (box));
	} else {
		atk_obj = NULL;
	}
	return atk_obj;
}

static AtkStateSet*
html_view_accessible_ref_state_set (AtkObject *obj)
{
	GtkWidget *widget = GTK_ACCESSIBLE (obj)->widget;
	HtmlView *view;
 	AtkStateSet *state_set; 

	state_set = ATK_OBJECT_CLASS (parent_class)->ref_state_set (obj);

	if (widget == NULL)
    		return state_set;

	view = HTML_VIEW (widget);

#ifdef DUMP_BOXES 
	debug_dump_boxes (view->root, 0, FALSE, NULL);
#endif

	if (view->document->focus_element && GTK_WIDGET_HAS_FOCUS (widget))
		atk_state_set_remove_state (state_set, ATK_STATE_FOCUSED);

	return state_set;	
}

static void
focus_object_destroyed (gpointer data)
{
	g_object_set_data (G_OBJECT (data), gail_focus_object, NULL);
}       

static void
root_object_destroyed (gpointer data)
{
	set_root_object (G_OBJECT (data), NULL);
	g_signal_emit_by_name (data, "children_changed::remove", 0, NULL, NULL);
}

static void
set_root_object (GObject *obj,
                 HtmlBox *root) 
{
	gpointer old_root;

	old_root = g_object_get_data (obj, html_root);
	if (old_root && root) {
		g_object_weak_unref (old_root, 
				     (GWeakNotify) root_object_destroyed,
				     obj);
	}
	if (root) {
		g_object_weak_ref (G_OBJECT (root), 
				   (GWeakNotify) root_object_destroyed,
				   obj);
	}
	g_object_set_data (obj, html_root, root);
}