]> www.fi.muni.cz Git - evince.git/blob - backend/ev-link.c
Hungarian translation updated.
[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 };
34
35
36 struct _EvLink {
37         GObject base_instance;
38         EvLinkPrivate *priv;
39 };
40
41 struct _EvLinkClass {
42         GObjectClass base_class;
43 };
44
45 struct _EvLinkPrivate {
46         char *title;
47         char *uri;
48         EvLinkType type;
49         int page;
50 };
51
52 G_DEFINE_TYPE (EvLink, ev_link, G_TYPE_OBJECT)
53
54 #define EV_LINK_GET_PRIVATE(object) \
55         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_LINK, EvLinkPrivate))
56
57 GType
58 ev_link_type_get_type (void)
59 {
60         static GType type = 0;
61
62         if (G_UNLIKELY (type == 0)) {
63                 static const GEnumValue values[] = {
64                         { EV_LINK_TYPE_TITLE, "EV_LINK_TYPE_TITLE", "title" },
65                         { EV_LINK_TYPE_PAGE, "EV_LINK_TYPE_PAGE", "page" },
66                         { EV_LINK_TYPE_EXTERNAL_URI, "EV_LINK_TYPE_EXTERNAL_URI", "external" },
67                         { 0, NULL, NULL }
68                 };
69
70                 type = g_enum_register_static ("EvLinkType", values);
71         }
72
73         return type;
74 }
75
76 const char *
77 ev_link_get_title (EvLink *self)
78 {
79         g_return_val_if_fail (EV_IS_LINK (self), NULL);
80         
81         return self->priv->title;
82 }
83
84 void
85 ev_link_set_title (EvLink* self, const char *title)
86 {
87         g_assert (EV_IS_LINK (self));
88
89         if (self->priv->title != NULL) {
90                 g_free (self->priv->title);
91         }
92         if (title)
93                 self->priv->title = g_strdup (title);
94         else
95                 self->priv->title = NULL;
96
97         g_object_notify (G_OBJECT (self), "title");
98 }
99
100 const char *
101 ev_link_get_uri (EvLink *self)
102 {
103         g_return_val_if_fail (EV_IS_LINK (self), NULL);
104         
105         return self->priv->uri;
106 }
107
108 void
109 ev_link_set_uri (EvLink* self, const char *uri)
110 {
111         g_assert (EV_IS_LINK (self));
112         g_assert (uri != NULL);
113
114         if (self->priv->uri != NULL) {
115                 g_free (self->priv->uri);
116         }
117
118         self->priv->uri = g_strdup (uri);
119
120         g_object_notify (G_OBJECT (self), "uri");
121 }
122
123 EvLinkType
124 ev_link_get_link_type (EvLink *self)
125 {
126         g_return_val_if_fail (EV_IS_LINK (self), 0);
127         
128         return self->priv->type;
129 }
130
131 void
132 ev_link_set_link_type (EvLink* self, EvLinkType type)
133 {
134         g_assert (EV_IS_LINK (self));
135
136         self->priv->type = type;
137
138         g_object_notify (G_OBJECT (self), "type");
139 }
140
141 int
142 ev_link_get_page (EvLink *self)
143 {
144         g_return_val_if_fail (EV_IS_LINK (self), 0);
145         
146         return self->priv->page;
147 }
148
149 void
150 ev_link_set_page (EvLink* self, int page)
151 {
152         g_assert (EV_IS_LINK (self));
153
154         self->priv->page = page;
155
156         g_object_notify (G_OBJECT (self), "page");
157 }
158
159 static void
160 ev_link_get_property (GObject *object, guint prop_id, GValue *value,
161                       GParamSpec *param_spec)
162 {
163         EvLink *self;
164
165         self = EV_LINK (object);
166
167         switch (prop_id) {
168         case PROP_TITLE:
169                 g_value_set_string (value, self->priv->title);
170                 break;
171         case PROP_URI:
172                 g_value_set_string (value, self->priv->uri);
173                 break;
174         case PROP_TYPE:
175                 g_value_set_enum (value, self->priv->type);
176                 break;
177         case PROP_PAGE:
178                 g_value_set_int (value, self->priv->page);
179                 break;
180         default:
181                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
182                                                    prop_id,
183                                                    param_spec);
184                 break;
185         }
186 }
187
188 static void
189 ev_link_set_property (GObject *object, guint prop_id, const GValue *value,
190                       GParamSpec *param_spec)
191 {
192         EvLink *self;
193         
194         self = EV_LINK (object);
195         
196         switch (prop_id) {
197         case PROP_TITLE:
198                 ev_link_set_title (self, g_value_get_string (value));
199                 break;
200         case PROP_URI:
201                 ev_link_set_uri (self, g_value_get_string (value));
202                 break;
203         case PROP_TYPE:
204                 ev_link_set_link_type (self, g_value_get_enum (value));
205                 break;
206         case PROP_PAGE:
207                 ev_link_set_page (self, g_value_get_int (value));
208                 break;
209         default:
210                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
211                                                    prop_id,
212                                                    param_spec);
213                 break;
214         }
215 }
216
217 static void
218 ev_window_dispose (GObject *object)
219 {
220         EvLinkPrivate *priv;
221
222         g_return_if_fail (EV_IS_LINK (object));
223
224         priv = EV_LINK (object)->priv;
225
226         if (priv->title) {
227                 g_free (priv->title);
228                 priv->title = NULL;
229         }
230
231         G_OBJECT_CLASS (ev_link_parent_class)->dispose (object);
232 }
233
234 static void
235 ev_link_init (EvLink *ev_link)
236 {
237         ev_link->priv = EV_LINK_GET_PRIVATE (ev_link);
238
239         ev_link->priv->type = EV_LINK_TYPE_TITLE;
240 }
241
242 static void
243 ev_link_class_init (EvLinkClass *ev_window_class)
244 {
245         GObjectClass *g_object_class;
246
247         g_object_class = G_OBJECT_CLASS (ev_window_class);
248         g_object_class->dispose = ev_window_dispose;
249         g_object_class->set_property = ev_link_set_property;
250         g_object_class->get_property = ev_link_get_property;
251
252         g_type_class_add_private (g_object_class, sizeof (EvLinkPrivate));
253
254         g_object_class_install_property (g_object_class,
255                                          PROP_TITLE,
256                                          g_param_spec_string ("title",
257                                                               "Link Title",
258                                                               "The link title",
259                                                               NULL,
260                                                               G_PARAM_READWRITE));
261
262         g_object_class_install_property (g_object_class,
263                                          PROP_URI,
264                                          g_param_spec_string ("uri",
265                                                               "Link URI",
266                                                               "The link URI",
267                                                               NULL,
268                                                               G_PARAM_READWRITE));
269
270         g_object_class_install_property (g_object_class,
271                                          PROP_TYPE,
272                                          g_param_spec_enum  ("type",
273                                                              "Link Type",
274                                                              "The link type",
275                                                              EV_TYPE_LINK_TYPE,
276                                                              EV_LINK_TYPE_TITLE,
277                                                              G_PARAM_READWRITE));
278
279         g_object_class_install_property (g_object_class,
280                                          PROP_PAGE,
281                                          g_param_spec_int ("page",
282                                                            "Link Page",
283                                                            "The link page",
284                                                             -1,
285                                                             G_MAXINT,
286                                                             0,
287                                                             G_PARAM_READWRITE));
288 }
289
290 EvLink *
291 ev_link_new_title (const char *title)
292 {
293         return EV_LINK (g_object_new (EV_TYPE_LINK,
294                                       "title", title,
295                                       "type", EV_LINK_TYPE_TITLE,
296                                       NULL));
297 }
298
299 EvLink *
300 ev_link_new_page (const char *title, int page)
301 {
302         return EV_LINK (g_object_new (EV_TYPE_LINK,
303                                       "title", title,
304                                       "page", page,
305                                       "type", EV_LINK_TYPE_PAGE,
306                                       NULL));
307 }
308
309 EvLink *
310 ev_link_new_external (const char *title, const char *uri)
311 {
312         return EV_LINK (g_object_new (EV_TYPE_LINK,
313                                       "title", title,
314                                       "uri", uri,
315                                       "type", EV_LINK_TYPE_EXTERNAL_URI,
316                                       NULL));
317 }
318
319
320
321 static void
322 ev_link_mapping_free_foreach (EvLinkMapping *mapping)
323 {
324         g_object_unref (G_OBJECT (mapping->link));
325         g_free (mapping);
326 }
327
328 void
329 ev_link_mapping_free (GList *link_mapping)
330 {
331         if (link_mapping == NULL)
332                 return;
333
334         g_list_foreach (link_mapping, (GFunc) (ev_link_mapping_free_foreach), NULL);
335         g_list_free (link_mapping);
336 }
337
338
339 EvLink *
340 ev_link_mapping_find (GList   *link_mapping,
341                       gdouble  x,
342                       gdouble  y)
343 {
344         GList *list;
345         EvLink *link = NULL;
346         int i;
347         
348         i = 0;
349
350         for (list = link_mapping; list; list = list->next) {
351                 EvLinkMapping *mapping = list->data;
352
353                 i++;
354                 if ((x >= mapping->x1) &&
355                     (y >= mapping->y1) &&
356                     (x <= mapping->x2) &&
357                     (y <= mapping->y2)) {
358                         link = mapping->link;
359                         break;
360                 }
361         }
362
363         return link;
364 }
365