Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ev-mapping-list.c
Go to the documentation of this file.
1 /* ev-mapping.c
2  * this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "ev-mapping-list.h"
22 
30  guint page;
31  GList *list;
32  GDestroyNotify data_destroy_func;
33  volatile gint ref_count;
34 };
35 
36 G_DEFINE_BOXED_TYPE (EvMappingList, ev_mapping_list, ev_mapping_list_ref, ev_mapping_list_unref)
37 
38 
45 EvMapping *
47  gconstpointer data)
48 {
49  GList *list;
50 
51  for (list = mapping_list->list; list; list = list->next) {
52  EvMapping *mapping = list->data;
53 
54  if (mapping->data == data)
55  return mapping;
56  }
57 
58  return NULL;
59 }
60 
69 EvMapping *
71  gconstpointer data,
72  GCompareFunc func)
73 {
74  GList *list;
75 
76  for (list = mapping_list->list; list; list = list->next) {
77  EvMapping *mapping = list->data;
78 
79  if (!func (mapping->data, data))
80  return mapping;
81  }
82 
83  return NULL;
84 }
85 
93 EvMapping *
95  guint n)
96 {
97  g_return_val_if_fail (mapping_list != NULL, NULL);
98 
99  return (EvMapping *)g_list_nth_data (mapping_list->list, n);
100 }
101 
112 EvMapping *
114  gdouble x,
115  gdouble y)
116 {
117  GList *list;
118 
119  g_return_val_if_fail (mapping_list != NULL, NULL);
120 
121  for (list = mapping_list->list; list; list = list->next) {
122  EvMapping *mapping = list->data;
123 
124  if ((x >= mapping->area.x1) &&
125  (y >= mapping->area.y1) &&
126  (x <= mapping->area.x2) &&
127  (y <= mapping->area.y2)) {
128  return mapping;
129  }
130  }
131 
132  return NULL;
133 }
134 
143 gpointer
145  gdouble x,
146  gdouble y)
147 {
148  EvMapping *mapping;
149 
150  mapping = ev_mapping_list_get (mapping_list, x, y);
151  if (mapping)
152  return mapping->data;
153 
154  return NULL;
155 }
156 
163 GList *
165 {
166  return mapping_list ? mapping_list->list : NULL;
167 }
168 
176 void
178  EvMapping *mapping)
179 {
180  mapping_list->list = g_list_remove (mapping_list->list, mapping);
181  mapping_list->data_destroy_func (mapping->data);
182  g_free (mapping);
183 }
184 
185 guint
187 {
188  return mapping_list->page;
189 }
190 
191 guint
193 {
194  g_return_val_if_fail (mapping_list != NULL, 0);
195 
196  return g_list_length (mapping_list->list);
197 }
198 
209  GList *list,
210  GDestroyNotify data_destroy_func)
211 {
212  EvMappingList *mapping_list;
213 
214  g_return_val_if_fail (data_destroy_func != NULL, NULL);
215 
216  mapping_list = g_slice_new (EvMappingList);
217  mapping_list->page = page;
218  mapping_list->list = list;
219  mapping_list->data_destroy_func = data_destroy_func;
220  mapping_list->ref_count = 1;
221 
222  return mapping_list;
223 }
224 
227 {
228  g_return_val_if_fail (mapping_list != NULL, NULL);
229  g_return_val_if_fail (mapping_list->ref_count > 0, mapping_list);
230 
231  g_atomic_int_add (&mapping_list->ref_count, 1);
232 
233  return mapping_list;
234 }
235 
236 static void
238  GDestroyNotify destroy_func)
239 {
240  destroy_func (mapping->data);
241  g_free (mapping);
242 }
243 
244 void
246 {
247  g_return_if_fail (mapping_list != NULL);
248  g_return_if_fail (mapping_list->ref_count > 0);
249 
250  if (g_atomic_int_add (&mapping_list->ref_count, -1) - 1 == 0) {
251  g_list_foreach (mapping_list->list,
253  mapping_list->data_destroy_func);
254  g_list_free (mapping_list->list);
255  g_slice_free (EvMappingList, mapping_list);
256  }
257 }