]> www.fi.muni.cz Git - evince.git/blob - backend/ev-link.c
071d6af5edbcceaf83afdbd603dc586ce75d6315
[evince.git] / backend / ev-link.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* this file is part of evince, a gnome document viewer
3  *
4  *  Copyright (C) 2005 Red Hat, Inc.
5  *
6  * Evince is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Evince is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "ev-link.h"
26
27 enum {
28         PROP_0,
29         PROP_TITLE,
30         PROP_TYPE,
31         PROP_PAGE,
32         PROP_URI,
33         PROP_LEFT,
34         PROP_TOP,
35         PROP_ZOOM
36 };
37
38
39 struct _EvLink {
40         GObject base_instance;
41         EvLinkPrivate *priv;
42 };
43
44 struct _EvLinkClass {
45         GObjectClass base_class;
46 };
47
48 struct _EvLinkPrivate {
49         char *title;
50         char *uri;
51         EvLinkType type;
52         int page;
53         double top;
54         double left;
55         double zoom;
56 };
57
58 G_DEFINE_TYPE (EvLink, ev_link, G_TYPE_OBJECT)
59
60 #define EV_LINK_GET_PRIVATE(object) \
61         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_LINK, EvLinkPrivate))
62
63 GType
64 ev_link_type_get_type (void)
65 {
66         static GType type = 0;
67
68         if (G_UNLIKELY (type == 0)) {
69                 static const GEnumValue values[] = {
70                         { EV_LINK_TYPE_TITLE, "EV_LINK_TYPE_TITLE", "title" },
71                         { EV_LINK_TYPE_PAGE, "EV_LINK_TYPE_PAGE", "page" },
72                         { EV_LINK_TYPE_PAGE_XYZ, "EV_LINK_TYPE_PAGE_XYZ", "page-xyz" },
73                         { EV_LINK_TYPE_PAGE_FIT, "EV_LINK_TYPE_PAGE_FIT", "page-fit" },
74                         { EV_LINK_TYPE_PAGE_FITH, "EV_LINK_TYPE_PAGE_FITH", "page-fith" },
75                         { EV_LINK_TYPE_EXTERNAL_URI, "EV_LINK_TYPE_EXTERNAL_URI", "external" },
76                         { 0, NULL, NULL }
77                 };
78
79                 type = g_enum_register_static ("EvLinkType", values);
80         }
81
82         return type;
83 }
84
85 const char *
86 ev_link_get_title (EvLink *self)
87 {
88         g_return_val_if_fail (EV_IS_LINK (self), NULL);
89         
90         return self->priv->title;
91 }
92
93 const char *
94 ev_link_get_uri (EvLink *self)
95 {
96         g_return_val_if_fail (EV_IS_LINK (self), NULL);
97         
98         return self->priv->uri;
99 }
100
101 EvLinkType
102 ev_link_get_link_type (EvLink *self)
103 {
104         g_return_val_if_fail (EV_IS_LINK (self), 0);
105         
106         return self->priv->type;
107 }
108
109 int
110 ev_link_get_page (EvLink *self)
111 {
112         g_return_val_if_fail (EV_IS_LINK (self), 0);
113         
114         return self->priv->page;
115 }
116
117 double
118 ev_link_get_top (EvLink *self)
119 {
120         g_return_val_if_fail (EV_IS_LINK (self), 0);
121         
122         return self->priv->top;
123 }
124
125 double
126 ev_link_get_left (EvLink *self)
127 {
128         g_return_val_if_fail (EV_IS_LINK (self), 0);
129         
130         return self->priv->left;
131 }
132
133 double
134 ev_link_get_zoom (EvLink *self)
135 {
136         g_return_val_if_fail (EV_IS_LINK (self), 0);
137         
138         return self->priv->zoom;
139 }
140
141 static void
142 ev_link_get_property (GObject *object, guint prop_id, GValue *value,
143                       GParamSpec *param_spec)
144 {
145         EvLink *self;
146
147         self = EV_LINK (object);
148
149         switch (prop_id) {
150         case PROP_TITLE:
151                 g_value_set_string (value, self->priv->title);
152                 break;
153         case PROP_URI:
154                 g_value_set_string (value, self->priv->uri);
155                 break;
156         case PROP_TYPE:
157                 g_value_set_enum (value, self->priv->type);
158                 break;
159         case PROP_PAGE:
160                 g_value_set_int (value, self->priv->page);
161                 break;
162         case PROP_TOP:
163                 g_value_set_double (value, self->priv->top);
164                 break;
165         case PROP_LEFT:
166                 g_value_set_double (value, self->priv->left);
167                 break;
168         case PROP_ZOOM:
169                 g_value_set_double (value, self->priv->zoom);
170                 break;
171         default:
172                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
173                                                    prop_id,
174                                                    param_spec);
175                 break;
176         }
177 }
178
179 static void
180 ev_link_set_property (GObject *object, guint prop_id, const GValue *value,
181                       GParamSpec *param_spec)
182 {
183         EvLink *link = EV_LINK (object);
184         
185         switch (prop_id) {
186         case PROP_TITLE:
187                 link->priv->title = g_strdup (g_value_get_string (value));      
188                 break;
189         case PROP_URI:
190                 link->priv->uri = g_strdup (g_value_get_string (value));
191                 break;
192         case PROP_TYPE:
193                 link->priv->type = g_value_get_enum (value);
194                 break;
195         case PROP_PAGE:
196                 link->priv->page = g_value_get_int (value);
197                 break;
198         case PROP_TOP:
199                 link->priv->top = g_value_get_double (value);
200                 break;
201         case PROP_LEFT:
202                 link->priv->left = g_value_get_double (value);
203                 break;
204         case PROP_ZOOM:
205                 link->priv->zoom = g_value_get_double (value);
206                 break;
207
208         default:
209                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
210                                                    prop_id,
211                                                    param_spec);
212                 break;
213         }
214 }
215
216 static void
217 ev_window_dispose (GObject *object)
218 {
219         EvLinkPrivate *priv;
220
221         g_return_if_fail (EV_IS_LINK (object));
222
223         priv = EV_LINK (object)->priv;
224
225         if (priv->title) {
226                 g_free (priv->title);
227                 priv->title = NULL;
228         }
229
230         if (priv->uri) {
231                 g_free (priv->uri);
232                 priv->uri = NULL;
233         }
234
235         G_OBJECT_CLASS (ev_link_parent_class)->dispose (object);
236 }
237
238 static void
239 ev_link_init (EvLink *ev_link)
240 {
241         ev_link->priv = EV_LINK_GET_PRIVATE (ev_link);
242
243         ev_link->priv->type = EV_LINK_TYPE_TITLE;
244 }
245
246 static void
247 ev_link_class_init (EvLinkClass *ev_window_class)
248 {
249         GObjectClass *g_object_class;
250
251         g_object_class = G_OBJECT_CLASS (ev_window_class);
252         g_object_class->dispose = ev_window_dispose;
253         g_object_class->set_property = ev_link_set_property;
254         g_object_class->get_property = ev_link_get_property;
255
256         g_type_class_add_private (g_object_class, sizeof (EvLinkPrivate));
257
258         g_object_class_install_property (g_object_class,
259                                          PROP_TITLE,
260                                          g_param_spec_string ("title",
261                                                               "Link Title",
262                                                               "The link title",
263                                                               NULL,
264                                                               G_PARAM_READWRITE |
265                                                               G_PARAM_CONSTRUCT_ONLY));
266         g_object_class_install_property (g_object_class,
267                                          PROP_URI,
268                                          g_param_spec_string ("uri",
269                                                               "Link URI",
270                                                               "The link URI",
271                                                               NULL,
272                                                               G_PARAM_READWRITE |
273                                                               G_PARAM_CONSTRUCT_ONLY));
274         g_object_class_install_property (g_object_class,
275                                          PROP_TYPE,
276                                          g_param_spec_enum  ("type",
277                                                              "Link Type",
278                                                              "The link type",
279                                                              EV_TYPE_LINK_TYPE,
280                                                              EV_LINK_TYPE_TITLE,
281                                                              G_PARAM_READWRITE |
282                                                              G_PARAM_CONSTRUCT_ONLY));
283         g_object_class_install_property (g_object_class,
284                                          PROP_PAGE,
285                                          g_param_spec_int ("page",
286                                                            "Link Page",
287                                                            "The link page",
288                                                             -1,
289                                                             G_MAXINT,
290                                                             0,
291                                                             G_PARAM_READWRITE |
292                                                             G_PARAM_CONSTRUCT_ONLY));
293         g_object_class_install_property (g_object_class,
294                                          PROP_LEFT,
295                                          g_param_spec_double ("left",
296                                                               "Left coordinate",
297                                                               "The left coordinate",
298                                                               -G_MAXDOUBLE,
299                                                               G_MAXDOUBLE,
300                                                               0,
301                                                               G_PARAM_READWRITE |
302                                                               G_PARAM_CONSTRUCT_ONLY));
303         g_object_class_install_property (g_object_class,
304                                          PROP_TOP,
305                                          g_param_spec_double ("top",
306                                                               "Top coordinate",
307                                                               "The top coordinate",
308                                                               -G_MAXDOUBLE,
309                                                               G_MAXDOUBLE,
310                                                               0,
311                                                               G_PARAM_READWRITE |
312                                                               G_PARAM_CONSTRUCT_ONLY));
313         g_object_class_install_property (g_object_class,
314                                          PROP_ZOOM,
315                                          g_param_spec_double ("zoom",
316                                                               "Zoom",
317                                                               "Zoom",
318                                                               0,
319                                                               G_MAXDOUBLE,
320                                                               0,
321                                                               G_PARAM_READWRITE |
322                                                               G_PARAM_CONSTRUCT_ONLY));
323 }
324
325 EvLink *
326 ev_link_new_title (const char *title)
327 {
328         return EV_LINK (g_object_new (EV_TYPE_LINK,
329                                       "title", title,
330                                       "type", EV_LINK_TYPE_TITLE,
331                                       NULL));
332 }
333
334 EvLink *
335 ev_link_new_page (const char *title, int page)
336 {
337         return EV_LINK (g_object_new (EV_TYPE_LINK,
338                                       "title", title,
339                                       "page", page,
340                                       "type", EV_LINK_TYPE_PAGE,
341                                       NULL));
342 }
343
344 EvLink *
345 ev_link_new_page_xyz (const char *title,
346                       int         page,
347                       double      left,
348                       double      top,
349                       double      zoom)
350 {
351         return EV_LINK (g_object_new (EV_TYPE_LINK,
352                                       "title", title,
353                                       "page", page,
354                                       "type", EV_LINK_TYPE_PAGE_XYZ,
355                                       "left", left,
356                                       "top", top,
357                                       "zoom", zoom,
358                                       NULL));
359 }
360
361 EvLink *
362 ev_link_new_page_fit (const char *title,
363                       int         page)
364 {
365         return EV_LINK (g_object_new (EV_TYPE_LINK,
366                                       "title", title,
367                                       "page", page,
368                                       "type", EV_LINK_TYPE_PAGE_FIT,
369                                       NULL));
370 }
371
372 EvLink *
373 ev_link_new_page_fith (const char *title,
374                        int         page,
375                        double      top)
376 {
377         return EV_LINK (g_object_new (EV_TYPE_LINK,
378                                       "title", title,
379                                       "page", page,
380                                       "type", EV_LINK_TYPE_PAGE_FITH,
381                                       "top", top,
382                                       NULL));
383 }
384
385 EvLink *
386 ev_link_new_external (const char *title, const char *uri)
387 {
388         return EV_LINK (g_object_new (EV_TYPE_LINK,
389                                       "title", title,
390                                       "uri", uri,
391                                       "type", EV_LINK_TYPE_EXTERNAL_URI,
392                                       NULL));
393 }
394
395
396
397 static void
398 ev_link_mapping_free_foreach (EvLinkMapping *mapping)
399 {
400         g_object_unref (G_OBJECT (mapping->link));
401         g_free (mapping);
402 }
403
404 void
405 ev_link_mapping_free (GList *link_mapping)
406 {
407         if (link_mapping == NULL)
408                 return;
409
410         g_list_foreach (link_mapping, (GFunc) (ev_link_mapping_free_foreach), NULL);
411         g_list_free (link_mapping);
412 }
413
414
415 EvLink *
416 ev_link_mapping_find (GList   *link_mapping,
417                       gdouble  x,
418                       gdouble  y)
419 {
420         GList *list;
421         EvLink *link = NULL;
422         int i;
423         
424         i = 0;
425
426         for (list = link_mapping; list; list = list->next) {
427                 EvLinkMapping *mapping = list->data;
428
429                 i++;
430                 if ((x >= mapping->x1) &&
431                     (y >= mapping->y1) &&
432                     (x <= mapping->x2) &&
433                     (y <= mapping->y2)) {
434                         link = mapping->link;
435                         break;
436                 }
437         }
438
439         return link;
440 }
441