]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-mapping-list.c
[dualscreen] fix crash on ctrl+w and fix control window closing
[evince.git] / libdocument / ev-mapping-list.c
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
23 struct _EvMappingList {
24         guint          page;
25         GList         *list;
26         GDestroyNotify data_destroy_func;
27         volatile gint  ref_count;
28 };
29
30 EvMapping *
31 ev_mapping_list_find (EvMappingList *mapping_list,
32                       gconstpointer  data)
33 {
34         GList *list;
35
36         for (list = mapping_list->list; list; list = list->next) {
37                 EvMapping *mapping = list->data;
38
39                 if (mapping->data == data)
40                         return mapping;
41         }
42
43         return NULL;
44 }
45
46 EvMapping *
47 ev_mapping_list_find_custom (EvMappingList *mapping_list,
48                              gconstpointer  data,
49                              GCompareFunc   func)
50 {
51         GList *list;
52
53         for (list = mapping_list->list; list; list = list->next) {
54                 EvMapping *mapping = list->data;
55
56                 if (!func (mapping->data, data))
57                         return mapping;
58         }
59
60         return NULL;
61 }
62
63 gpointer
64 ev_mapping_list_get_data (EvMappingList *mapping_list,
65                           gdouble        x,
66                           gdouble        y)
67 {
68         GList *list;
69
70         for (list = mapping_list->list; list; list = list->next) {
71                 EvMapping *mapping = list->data;
72
73                 if ((x >= mapping->area.x1) &&
74                     (y >= mapping->area.y1) &&
75                     (x <= mapping->area.x2) &&
76                     (y <= mapping->area.y2)) {
77                         return mapping->data;
78                 }
79         }
80
81         return NULL;
82 }
83
84 GList *
85 ev_mapping_list_get_list (EvMappingList *mapping_list)
86 {
87         return mapping_list ? mapping_list->list : NULL;
88 }
89
90 guint
91 ev_mapping_list_get_page (EvMappingList *mapping_list)
92 {
93         return mapping_list->page;
94 }
95
96 EvMappingList *
97 ev_mapping_list_new (guint          page,
98                      GList         *list,
99                      GDestroyNotify data_destroy_func)
100 {
101         EvMappingList *mapping_list;
102
103         g_return_val_if_fail (data_destroy_func != NULL, NULL);
104
105         mapping_list = g_slice_new (EvMappingList);
106         mapping_list->page = page;
107         mapping_list->list = list;
108         mapping_list->data_destroy_func = data_destroy_func;
109         mapping_list->ref_count = 1;
110
111         return mapping_list;
112 }
113
114 EvMappingList *
115 ev_mapping_list_ref (EvMappingList *mapping_list)
116 {
117         g_return_val_if_fail (mapping_list != NULL, NULL);
118         g_return_val_if_fail (mapping_list->ref_count > 0, mapping_list);
119
120         g_atomic_int_add (&mapping_list->ref_count, 1);
121
122         return mapping_list;
123 }
124
125 static void
126 mapping_list_free_foreach (EvMapping     *mapping,
127                            GDestroyNotify destroy_func)
128 {
129         destroy_func (mapping->data);
130         g_free (mapping);
131 }
132
133 void
134 ev_mapping_list_unref (EvMappingList *mapping_list)
135 {
136         g_return_if_fail (mapping_list != NULL);
137         g_return_if_fail (mapping_list->ref_count > 0);
138
139         if (g_atomic_int_exchange_and_add (&mapping_list->ref_count, -1) - 1 == 0) {
140                 g_list_foreach (mapping_list->list,
141                                 (GFunc)mapping_list_free_foreach,
142                                 mapping_list->data_destroy_func);
143                 g_list_free (mapping_list->list);
144                 g_slice_free (EvMappingList, mapping_list);
145         }
146 }