summaryrefslogtreecommitdiff
path: root/libview/ev-link-accessible.c
blob: a9ee85dc18e32490867318ca8b0ecd8788476066 (plain)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
/* this file is part of atril, a mate document viewer
 *
 *  Copyright (C) 2013 Carlos Garcia Campos <carlosgc@gnome.org>
 *
 * Evince is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Evince 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>

#include "ev-link-accessible.h"
#include "ev-view-private.h"

typedef struct _EvHyperlink      EvHyperlink;
typedef struct _EvHyperlinkClass EvHyperlinkClass;

struct _EvLinkAccessiblePrivate {
        EvPageAccessible *page;
        EvLink           *link;
        EvRectangle       area;

        EvHyperlink      *hyperlink;

        gchar      *name;
};

struct _EvHyperlink {
        AtkHyperlink parent;

        EvLinkAccessible *link_impl;
};

struct _EvHyperlinkClass {
        AtkHyperlinkClass parent_class;
};

#define EV_TYPE_HYPERLINK (ev_hyperlink_get_type ())
#define EV_HYPERLINK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_HYPERLINK, EvHyperlink))

static GType ev_hyperlink_get_type              (void);

G_DEFINE_TYPE (EvHyperlink, ev_hyperlink, ATK_TYPE_HYPERLINK)

static gchar *
ev_hyperlink_get_uri (AtkHyperlink *atk_hyperlink,
                      gint          i)
{
        EvHyperlink             *hyperlink = EV_HYPERLINK (atk_hyperlink);
        EvLinkAccessiblePrivate *impl_priv;
        EvLinkAction            *action;

        if (!hyperlink->link_impl)
                return NULL;

        impl_priv = hyperlink->link_impl->priv;
        action = ev_link_get_action (impl_priv->link);

        return action ? g_strdup (ev_link_action_get_uri (action)) : NULL;
}

static gint
ev_hyperlink_get_n_anchors (AtkHyperlink *atk_hyperlink)
{
        return 1;
}

static gboolean
ev_hyperlink_is_valid (AtkHyperlink *atk_hyperlink)
{
        return TRUE;
}

static AtkObject *
ev_hyperlink_get_object (AtkHyperlink *atk_hyperlink,
                         gint          i)
{
        EvHyperlink *hyperlink = EV_HYPERLINK (atk_hyperlink);

        return hyperlink->link_impl ? ATK_OBJECT (hyperlink->link_impl) : NULL;
}

static gint
ev_hyperlink_get_start_index (AtkHyperlink *atk_hyperlink)
{
        EvHyperlink             *hyperlink = EV_HYPERLINK (atk_hyperlink);
        EvLinkAccessiblePrivate *impl_priv;
        EvView                  *view;
        EvRectangle             *areas = NULL;
        guint                    n_areas = 0;
        guint                    i;

        if (!hyperlink->link_impl)
                return -1;

        impl_priv = hyperlink->link_impl->priv;

	view = ev_page_accessible_get_view (impl_priv->page);
        if (!view->page_cache)
                return -1;

        ev_page_cache_get_text_layout (view->page_cache,
				       ev_page_accessible_get_page (impl_priv->page),
				       &areas, &n_areas);
        if (!areas)
                return -1;

        for (i = 0; i < n_areas; i++) {
                EvRectangle *rect = areas + i;
                gdouble      c_x, c_y;

                c_x = rect->x1 + (rect->x2 - rect->x1) / 2.;
                c_y = rect->y1 + (rect->y2 - rect->y1) / 2.;
                if (c_x >= impl_priv->area.x1 && c_x <= impl_priv->area.x2 &&
                    c_y >= impl_priv->area.y1 && c_y <= impl_priv->area.y2)
                        return i;
        }

        return -1;
}

static gint
ev_hyperlink_get_end_index (AtkHyperlink *atk_hyperlink)
{
        EvHyperlink             *hyperlink = EV_HYPERLINK (atk_hyperlink);
        EvLinkAccessiblePrivate *impl_priv;
        EvView                  *view;
        EvRectangle             *areas = NULL;
        guint                    n_areas = 0;
        guint                    i;

        if (!hyperlink->link_impl)
                return -1;

        impl_priv = hyperlink->link_impl->priv;

	view = ev_page_accessible_get_view (impl_priv->page);
        if (!view->page_cache)
                return -1;

        ev_page_cache_get_text_layout (view->page_cache,
				       ev_page_accessible_get_page (impl_priv->page),
				       &areas, &n_areas);
        if (!areas)
                return -1;

        for (i = n_areas - 1; i >= 0; i--) {
                EvRectangle *rect = areas + i;
                gdouble      c_x, c_y;

                c_x = rect->x1 + (rect->x2 - rect->x1) / 2.;
                c_y = rect->y1 + (rect->y2 - rect->y1) / 2.;
                if (c_x >= impl_priv->area.x1 && c_x <= impl_priv->area.x2 &&
                    c_y >= impl_priv->area.y1 && c_y <= impl_priv->area.y2)
                        return i + 1;
        }

        return -1;
}

static void
ev_hyperlink_class_init (EvHyperlinkClass *klass)
{
        AtkHyperlinkClass *atk_link_class = ATK_HYPERLINK_CLASS (klass);

        atk_link_class->get_uri = ev_hyperlink_get_uri;
        atk_link_class->get_n_anchors = ev_hyperlink_get_n_anchors;
        atk_link_class->is_valid = ev_hyperlink_is_valid;
        atk_link_class->get_object = ev_hyperlink_get_object;
        atk_link_class->get_start_index = ev_hyperlink_get_start_index;
        atk_link_class->get_end_index = ev_hyperlink_get_end_index;
}

static void
ev_hyperlink_init (EvHyperlink *link)
{
}

static void ev_link_accessible_hyperlink_impl_iface_init (AtkHyperlinkImplIface *iface);
static void ev_link_accessible_action_interface_init     (AtkActionIface        *iface);
static void ev_link_accessible_component_iface_init      (AtkComponentIface     *iface);

G_DEFINE_TYPE_WITH_CODE (EvLinkAccessible, ev_link_accessible, ATK_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (ATK_TYPE_HYPERLINK_IMPL, ev_link_accessible_hyperlink_impl_iface_init)
			 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, ev_link_accessible_action_interface_init)
			 G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, ev_link_accessible_component_iface_init))

static const gchar *
ev_link_accessible_get_name (AtkObject *atk_object)
{
	EvLinkAccessiblePrivate *priv;
	gint start_index;
	gint end_index;

	priv = EV_LINK_ACCESSIBLE (atk_object)->priv;
	if (priv->name)
		return priv->name;

	start_index = ev_hyperlink_get_start_index (ATK_HYPERLINK (priv->hyperlink));
	end_index = ev_hyperlink_get_end_index (ATK_HYPERLINK (priv->hyperlink));
	priv->name = atk_text_get_text (ATK_TEXT (atk_object_get_parent (atk_object)), start_index, end_index);

	return priv->name;
}

static AtkObject *
ev_link_accessible_get_parent (AtkObject *atk_object)
{
	EvLinkAccessiblePrivate *priv = EV_LINK_ACCESSIBLE (atk_object)->priv;

	return ATK_OBJECT (priv->page);
}

static AtkStateSet *
ev_link_accessible_ref_state_set (AtkObject *atk_object)
{
	AtkStateSet *state_set;
	AtkStateSet *copy_set;
	AtkStateSet *page_accessible_state_set;
	EvLinkAccessible *self;
	EvViewAccessible *view_accessible;
	EvView *view;
	gint page;

	self = EV_LINK_ACCESSIBLE (atk_object);
	state_set = ATK_OBJECT_CLASS (ev_link_accessible_parent_class)->ref_state_set (atk_object);
	atk_state_set_clear_states (state_set);

	page_accessible_state_set = atk_object_ref_state_set (ATK_OBJECT (self->priv->page));
	copy_set = atk_state_set_or_sets (state_set, page_accessible_state_set);

	view_accessible = ev_page_accessible_get_view_accessible (self->priv->page);
	page = ev_page_accessible_get_page (self->priv->page);
	if (!ev_view_accessible_is_doc_rect_showing (view_accessible, page, &self->priv->area))
		atk_state_set_remove_state (copy_set, ATK_STATE_SHOWING);

	view = ev_page_accessible_get_view (self->priv->page);
	if (!view->focused_element || view->focused_element->data != self->priv->link)
		atk_state_set_remove_state (copy_set, ATK_STATE_FOCUSED);

	g_object_unref (state_set);
	g_object_unref (page_accessible_state_set);

	return copy_set;
}

static void
ev_link_accessible_finalize (GObject *object)
{
        EvLinkAccessible *link = EV_LINK_ACCESSIBLE (object);

        g_clear_object (&link->priv->hyperlink);
        g_free (link->priv->name);

        G_OBJECT_CLASS (ev_link_accessible_parent_class)->finalize (object);
}

static void
ev_link_accessible_class_init (EvLinkAccessibleClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
        AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);

        object_class->finalize = ev_link_accessible_finalize;

        g_type_class_add_private (klass, sizeof (EvLinkAccessiblePrivate));

        atk_class->get_parent = ev_link_accessible_get_parent;
        atk_class->get_name = ev_link_accessible_get_name;
        atk_class->ref_state_set = ev_link_accessible_ref_state_set;
}

static void
ev_link_accessible_init (EvLinkAccessible *link)
{
        atk_object_set_role (ATK_OBJECT (link), ATK_ROLE_LINK);
        link->priv = G_TYPE_INSTANCE_GET_PRIVATE (link, EV_TYPE_LINK_ACCESSIBLE, EvLinkAccessiblePrivate);
}

static AtkHyperlink *
ev_link_accessible_get_hyperlink (AtkHyperlinkImpl *hyperlink_impl)
{
        EvLinkAccessible *link = EV_LINK_ACCESSIBLE (hyperlink_impl);

        if (link->priv->hyperlink)
                return ATK_HYPERLINK (link->priv->hyperlink);

        link->priv->hyperlink = g_object_new (EV_TYPE_HYPERLINK, NULL);

        link->priv->hyperlink->link_impl = link;
        g_object_add_weak_pointer (G_OBJECT (link), (gpointer *)&link->priv->hyperlink->link_impl);

        return ATK_HYPERLINK (link->priv->hyperlink);
}

static void
ev_link_accessible_hyperlink_impl_iface_init (AtkHyperlinkImplIface *iface)
{
        iface->get_hyperlink = ev_link_accessible_get_hyperlink;
}

static gboolean
ev_link_accessible_action_do_action (AtkAction *atk_action,
				     gint      i)
{
	EvLinkAccessiblePrivate *priv = EV_LINK_ACCESSIBLE (atk_action)->priv;
	EvView *view;

	view = ev_page_accessible_get_view (priv->page);
	if (!ev_link_get_action (priv->link))
		return FALSE;

	ev_view_handle_link (view, priv->link);

	return TRUE;
}

static gint
ev_link_accessible_action_get_n_actions (AtkAction *atk_action)
{
	return 1;
}

static const gchar *
ev_link_accessible_action_get_description (AtkAction *atk_action,
					   gint      i)
{
	/* TODO */
	return NULL;
}

static const gchar *
ev_link_accessible_action_get_name (AtkAction *atk_action,
				    gint      i)
{
	return i == 0 ? "activate" : NULL;
}

static void
ev_link_accessible_action_interface_init (AtkActionIface *iface)
{
	iface->do_action = ev_link_accessible_action_do_action;
	iface->get_n_actions = ev_link_accessible_action_get_n_actions;
	iface->get_description = ev_link_accessible_action_get_description;
	iface->get_name = ev_link_accessible_action_get_name;
}

static void
ev_link_accessible_get_extents (AtkComponent *atk_component,
				gint         *x,
				gint         *y,
				gint         *width,
				gint         *height,
				AtkCoordType coord_type)
{
	EvLinkAccessible *self;
	EvViewAccessible *view_accessible;
	gint page;
	EvRectangle atk_rect;

	self = EV_LINK_ACCESSIBLE (atk_component);
	view_accessible = ev_page_accessible_get_view_accessible (self->priv->page);
	page = ev_page_accessible_get_page (self->priv->page);
	_transform_doc_rect_to_atk_rect (view_accessible, page, &self->priv->area, &atk_rect, coord_type);
	*x = atk_rect.x1;
	*y = atk_rect.y1;
	*width = atk_rect.x2 - atk_rect.x1;
	*height = atk_rect.y2 - atk_rect.y1;
}

static gboolean
ev_link_accessible_grab_focus (AtkComponent *atk_component)
{
	EvLinkAccessible *self;
	EvView *view;
	EvMappingList *link_mapping;
	EvMapping *mapping;
	gint page;

	self = EV_LINK_ACCESSIBLE (atk_component);
	view = ev_page_accessible_get_view (self->priv->page);
	page = ev_page_accessible_get_page (self->priv->page);
	link_mapping = ev_page_cache_get_link_mapping (view->page_cache, page);
	mapping = ev_mapping_list_find (link_mapping, self->priv->link);
	_ev_view_set_focused_element (view, mapping, page);

	return TRUE;
}

static void
ev_link_accessible_component_iface_init (AtkComponentIface *iface)
{
	iface->get_extents = ev_link_accessible_get_extents;
	iface->grab_focus = ev_link_accessible_grab_focus;
}

EvLinkAccessible *
ev_link_accessible_new (EvPageAccessible *page,
                        EvLink           *link,
                        EvRectangle      *area)
{
        EvLinkAccessible *atk_link;

        atk_link = g_object_new (EV_TYPE_LINK_ACCESSIBLE, NULL);
        atk_link->priv->page = page;
        atk_link->priv->link = g_object_ref (link);
        atk_link->priv->area = *area;

        return EV_LINK_ACCESSIBLE (atk_link);
}