]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-link.c
Implements another history variant
[evince.git] / libdocument / 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 #include "ev-link.h"
22
23 enum {
24         PROP_0,
25         PROP_TITLE,
26         PROP_ACTION
27 };
28
29 struct _EvLink {
30         GObject base_instance;
31         EvLinkPrivate *priv;
32 };
33
34 struct _EvLinkClass {
35         GObjectClass base_class;
36 };
37
38 struct _EvLinkPrivate {
39         gchar        *title;
40         EvLinkAction *action;
41 };
42
43 G_DEFINE_TYPE (EvLink, ev_link, G_TYPE_OBJECT)
44
45 #define EV_LINK_GET_PRIVATE(object) \
46         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_LINK, EvLinkPrivate))
47
48 const gchar *
49 ev_link_get_title (EvLink *self)
50 {
51         g_return_val_if_fail (EV_IS_LINK (self), NULL);
52         
53         return self->priv->title;
54 }
55
56 EvLinkAction *
57 ev_link_get_action (EvLink *self)
58 {
59         g_return_val_if_fail (EV_IS_LINK (self), NULL);
60         
61         return self->priv->action;
62 }
63
64 static void
65 ev_link_get_property (GObject    *object,
66                       guint       prop_id,
67                       GValue     *value,
68                       GParamSpec *param_spec)
69 {
70         EvLink *self;
71
72         self = EV_LINK (object);
73
74         switch (prop_id) {
75                 case PROP_TITLE:
76                         g_value_set_string (value, self->priv->title);
77                         break;
78                 case PROP_ACTION:
79                         g_value_set_pointer (value, self->priv->action);
80                         break;
81                 default:
82                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
83                                                            prop_id,
84                                                            param_spec);
85                         break;
86         }
87 }
88
89 static void
90 ev_link_set_property (GObject      *object,
91                       guint         prop_id,
92                       const GValue *value,
93                       GParamSpec   *param_spec)
94 {
95         EvLink *self = EV_LINK (object);
96         
97         switch (prop_id) {
98                 case PROP_TITLE:
99                         self->priv->title = g_value_dup_string (value); 
100                         break;
101                 case PROP_ACTION:
102                         self->priv->action = g_value_get_pointer (value);
103                         break;
104                 default:
105                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
106                                                            prop_id,
107                                                            param_spec);
108                         break;
109         }
110 }
111
112 static void
113 ev_link_finalize (GObject *object)
114 {
115         EvLinkPrivate *priv;
116
117         priv = EV_LINK (object)->priv;
118
119         if (priv->title) {
120                 g_free (priv->title);
121                 priv->title = NULL;
122         }
123
124         if (priv->action) {
125                 g_object_unref (priv->action);
126                 priv->action = NULL;
127         }
128
129         G_OBJECT_CLASS (ev_link_parent_class)->finalize (object);
130 }
131
132 static void
133 ev_link_init (EvLink *ev_link)
134 {
135         ev_link->priv = EV_LINK_GET_PRIVATE (ev_link);
136
137         ev_link->priv->title = NULL;
138         ev_link->priv->action = NULL;
139 }
140
141 static void
142 ev_link_class_init (EvLinkClass *ev_window_class)
143 {
144         GObjectClass *g_object_class;
145
146         g_object_class = G_OBJECT_CLASS (ev_window_class);
147
148         g_object_class->set_property = ev_link_set_property;
149         g_object_class->get_property = ev_link_get_property;
150
151         g_object_class->finalize = ev_link_finalize;
152
153         g_type_class_add_private (g_object_class, sizeof (EvLinkPrivate));
154
155         g_object_class_install_property (g_object_class,
156                                          PROP_TITLE,
157                                          g_param_spec_string ("title",
158                                                               "Link Title",
159                                                               "The link title",
160                                                               NULL,
161                                                               G_PARAM_READWRITE |
162                                                               G_PARAM_CONSTRUCT_ONLY));
163         g_object_class_install_property (g_object_class,
164                                          PROP_ACTION,
165                                          g_param_spec_pointer ("action",
166                                                                "Link Action",
167                                                                "The link action",
168                                                                G_PARAM_READWRITE |
169                                                                G_PARAM_CONSTRUCT_ONLY));
170 }
171
172 EvLink *
173 ev_link_new (const char   *title,
174              EvLinkAction *action)
175 {
176         return EV_LINK (g_object_new (EV_TYPE_LINK,
177                                       "title", title,
178                                       "action", action,
179                                       NULL));
180 }
181
182 /* Link Mapping stuff */
183 static void
184 ev_link_mapping_free_foreach (EvLinkMapping *mapping)
185 {
186         g_object_unref (G_OBJECT (mapping->link));
187         g_free (mapping);
188 }
189
190 void
191 ev_link_mapping_free (GList *link_mapping)
192 {
193         if (link_mapping == NULL)
194                 return;
195
196         g_list_foreach (link_mapping, (GFunc) (ev_link_mapping_free_foreach), NULL);
197         g_list_free (link_mapping);
198 }
199
200 EvLink *
201 ev_link_mapping_find (GList   *link_mapping,
202                       gdouble  x,
203                       gdouble  y)
204 {
205         GList *list;
206         EvLink *link = NULL;
207         int i;
208         
209         i = 0;
210
211         for (list = link_mapping; list; list = list->next) {
212                 EvLinkMapping *mapping = list->data;
213
214                 i++;
215                 if ((x >= mapping->x1) &&
216                     (y >= mapping->y1) &&
217                     (x <= mapping->x2) &&
218                     (y <= mapping->y2)) {
219                         link = mapping->link;
220                         break;
221                 }
222         }
223
224         return link;
225 }
226
227 gint
228 ev_link_get_page (EvLink *link)
229 {
230         EvLinkAction *action;
231         EvLinkDest *dest;
232
233         action = ev_link_get_action (link);
234         if (!action)
235                 return -1;
236
237         if (ev_link_action_get_action_type (action) !=
238             EV_LINK_ACTION_TYPE_GOTO_DEST)
239                 return -1;
240
241         dest = ev_link_action_get_dest (action);
242         if (dest)
243                 return ev_link_dest_get_page (dest);
244                 
245         return -1;
246 }