]> www.fi.muni.cz Git - evince.git/blob - backend/ev-page-cache.c
modify the expose handling to get the shadows.
[evince.git] / backend / ev-page-cache.c
1 #include "ev-page-cache.h"
2 #include "ev-job-queue.h"
3 #include <stdlib.h>
4 #include <string.h>
5
6 typedef struct _EvPageCacheInfo
7 {
8         double width;
9         double height;
10 }
11 EvPageCacheInfo;
12
13
14 struct _EvPageCache
15 {
16         GObject parent;
17
18         gint current_page;
19         int n_pages;
20         char *title;
21         char **page_labels;
22         
23         gboolean has_labels;
24         gboolean uniform;
25         
26         double uniform_width;
27         double uniform_height;
28
29         double max_width_page_width;
30         double max_width_page_height;
31         double max_height_page_width;
32         double max_height_page_height;
33
34         EvPageCacheInfo *size_cache;
35         EvDocumentInfo *page_info;
36 };
37
38 struct _EvPageCacheClass
39 {
40         GObjectClass parent_class;
41
42         void (* page_changed) (EvPageCache *page_cache, gint page);
43 };
44
45 enum
46 {
47         PAGE_CHANGED,
48         N_SIGNALS,
49 };
50
51 static guint signals[N_SIGNALS] = {0, };
52
53 static void ev_page_cache_init       (EvPageCache      *page_cache);
54 static void ev_page_cache_class_init (EvPageCacheClass *page_cache);
55 static void ev_page_cache_finalize   (GObject *object);
56
57 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
58
59 static void
60 ev_page_cache_init (EvPageCache *page_cache)
61 {
62         page_cache->current_page = -1;
63 }
64
65 static void
66 ev_page_cache_class_init (EvPageCacheClass *class)
67 {
68         GObjectClass *object_class;
69
70         object_class = G_OBJECT_CLASS (class);
71
72         object_class->finalize = ev_page_cache_finalize;
73
74         signals [PAGE_CHANGED] =
75                 g_signal_new ("page-changed",
76                               EV_TYPE_PAGE_CACHE,
77                               G_SIGNAL_RUN_LAST,
78                               G_STRUCT_OFFSET (EvPageCacheClass, page_changed),
79                               NULL, NULL,
80                               g_cclosure_marshal_VOID__INT,
81                               G_TYPE_NONE, 1,
82                               G_TYPE_INT);
83
84 }
85
86 static void
87 ev_page_cache_finalize (GObject *object)
88 {
89         EvPageCache *page_cache;
90
91         page_cache = EV_PAGE_CACHE (object);
92
93         g_free (page_cache->title);
94         g_free (page_cache->size_cache);
95         ev_document_info_free (page_cache->page_info);
96 }
97
98 EvPageCache *
99 _ev_page_cache_new (EvDocument *document)
100 {
101         EvDocumentInfo *doc_info;
102         EvPageCache *page_cache;
103         EvPageCacheInfo *info;
104         gint i;
105
106         page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
107
108         ev_document_doc_mutex_lock ();
109
110         /* We read page information out of the document */
111
112         /* Assume all pages are the same size until proven otherwise */
113         page_cache->uniform = TRUE;
114         page_cache->has_labels = FALSE;
115         page_cache->n_pages = ev_document_get_n_pages (document);
116         page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
117         page_cache->max_width_page_width = 0;
118         page_cache->max_width_page_height = 0;
119         page_cache->max_height_page_width = 0;
120         page_cache->max_height_page_height = 0;
121
122         doc_info = ev_document_get_info (document);
123         if (doc_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
124                 page_cache->title = g_strdup (doc_info->title);
125         } else {
126                 page_cache->title = NULL;
127         }
128         g_free (doc_info);
129
130         for (i = 0; i < page_cache->n_pages; i++) {
131                 double page_width = 0;
132                 double page_height = 0;
133                 
134                 ev_document_get_page_size (document, i, &page_width, &page_height);
135
136                 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
137
138                 if (!page_cache->has_labels && page_cache->page_labels[i] != NULL) {
139                         gchar *expected_label;
140                         
141                         expected_label = g_strdup_printf ("%d", i + 1);
142                         if (strcmp (expected_label, page_cache->page_labels[i]))  
143                                 page_cache->has_labels = TRUE;
144                         g_free (expected_label);
145                 }
146
147                 if (page_width > page_cache->max_width_page_width) {
148                         page_cache->max_width_page_width = page_width;
149                         page_cache->max_width_page_height = page_height;
150                 }
151
152                 if (page_height > page_cache->max_height_page_height) {
153                         page_cache->max_height_page_width = page_width;
154                         page_cache->max_height_page_height = page_height;
155                 }
156
157                 if (i == 0) {
158                         page_cache->uniform_width = page_width;
159                         page_cache->uniform_height = page_height;
160                 } else if (page_cache->uniform &&
161                            (page_cache->uniform_width != page_width ||
162                             page_cache->uniform_height != page_height)) {
163                         /* It's a different page size.  Backfill the array. */
164                         int j;
165
166                         page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
167
168                         for (j = 0; j < i; j++) {
169                                 info = &(page_cache->size_cache [j]);
170                                 info->width = page_cache->uniform_width;
171                                 info->height = page_cache->uniform_height;
172                         }
173                         page_cache->uniform = FALSE;
174
175                 }
176
177                 if (! page_cache->uniform) {
178                         info = &(page_cache->size_cache [i]);
179
180                         info->width = page_width;
181                         info->height = page_height;
182                 }
183         }
184
185         page_cache->page_info = ev_document_get_info (document);
186
187         /* make some sanity check assertions */
188         if (! page_cache->uniform)
189                 g_assert (page_cache->size_cache != NULL);
190         if (page_cache->uniform)
191                 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
192
193         ev_document_doc_mutex_unlock ();
194
195         if (page_cache->n_pages > 0)
196                 ev_page_cache_set_current_page (page_cache, 0);
197
198         return page_cache;
199 }
200
201 gint
202 ev_page_cache_get_n_pages (EvPageCache *page_cache)
203 {
204         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
205
206         return page_cache->n_pages;
207 }
208
209 gint
210 ev_page_cache_get_current_page (EvPageCache *page_cache)
211 {
212         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
213
214         return page_cache->current_page;
215 }
216
217 void
218 ev_page_cache_set_current_page (EvPageCache *page_cache,
219                                 int          page)
220 {
221         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
222         g_return_if_fail (page >= 0 || page < page_cache->n_pages);
223
224         if (page == page_cache->current_page)
225                 return;
226
227         page_cache->current_page = page;
228         g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
229 }
230
231 gboolean
232 ev_page_cache_set_page_label (EvPageCache *page_cache,
233                               const char  *page_label)
234 {
235         gint i, page;
236         long value;
237         char *endptr = NULL;
238         
239         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
240         g_return_val_if_fail (page_label != NULL, FALSE);
241
242         /* First, look for a literal label match */
243         for (i = 0; i < page_cache->n_pages; i ++) {
244                 if (page_cache->page_labels[i] != NULL &&
245                     ! strcmp (page_label, page_cache->page_labels[i])) {
246                         ev_page_cache_set_current_page (page_cache, i);
247                         return TRUE;
248                 }
249         }
250
251         /* Next, parse the label, and see if the number fits */
252         value = strtol (page_label, &endptr, 10);
253         if (endptr[0] == '\0') {
254                 /* Page number is an integer */
255                 page = MIN (G_MAXINT, value);
256
257                 /* convert from a page label to a page offset */
258                 page --;
259                 if (page >= 0 &&
260                     page < page_cache->n_pages &&
261                     page_cache->page_labels[page] == NULL) {
262                         ev_page_cache_set_current_page (page_cache, page);
263                         return TRUE;
264                 }
265         }
266
267         return FALSE;
268 }
269
270 void
271 ev_page_cache_set_link (EvPageCache *page_cache,
272                         EvLink      *link)
273 {
274         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
275         g_return_if_fail (EV_IS_LINK (link));
276
277         ev_page_cache_set_current_page (page_cache, ev_link_get_page (link));
278 }
279
280 char *
281 ev_page_cache_get_title (EvPageCache *page_cache)
282 {
283         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
284
285         return page_cache->title;
286 }
287
288 void
289 ev_page_cache_get_size (EvPageCache *page_cache,
290                         gint         page,
291                         gfloat       scale,
292                         gint        *width,
293                         gint        *height)
294 {
295         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
296         g_return_if_fail (page >= 0 && page < page_cache->n_pages);
297
298         if (page_cache->uniform) {
299                 if (width)
300                         *width = page_cache->uniform_width;
301                 if (height)
302                         *height = page_cache->uniform_height;
303         } else {
304                 EvPageCacheInfo *info;
305
306                 info = &(page_cache->size_cache [page]);
307                 
308                 if (width)
309                         *width = info->width;
310                 if (height)
311                         *height = info->height;
312         }
313
314         if (width)
315                 *width = (int) ((*width) * scale + 0.5);
316         if (width)
317                 *height = (int) ((*height) * scale + 0.5);
318
319 }
320
321
322 /* Note that these aren't necessarily from the same page.
323  */
324 void
325 ev_page_cache_get_max_width_size (EvPageCache *page_cache,
326                                   gfloat       scale,
327                                   gint        *width,
328                                   gint        *height)
329 {
330         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
331
332         if (width)
333                 *width = page_cache->max_width_page_width * scale;
334         if (height)
335                 *height = page_cache->max_width_page_height * scale;
336 }
337
338 void
339 ev_page_cache_get_max_height_size (EvPageCache *page_cache,
340                                    gfloat       scale,
341                                    gint        *width,
342                                    gint        *height)
343 {
344         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
345
346         if (width)
347                 *width = page_cache->max_height_page_width * scale;
348         if (height)
349                 *height = page_cache->max_height_page_height * scale;
350 }
351
352
353 gchar *
354 ev_page_cache_get_page_label (EvPageCache *page_cache,
355                               gint         page)
356 {
357         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
358         g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
359
360         if (page_cache->page_labels[page] == NULL)
361                 return g_strdup_printf ("%d", page + 1);
362
363         return g_strdup (page_cache->page_labels[page]);
364 }
365
366 gboolean
367 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
368 {
369         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
370         return page_cache->has_labels;
371 }
372
373 const EvDocumentInfo *
374 ev_page_cache_get_info (EvPageCache *page_cache)
375 {
376         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
377
378         return page_cache->page_info;
379 }
380
381
382 gboolean
383 ev_page_cache_next_page (EvPageCache *page_cache)
384 {
385         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
386
387         if (page_cache->current_page >= page_cache->n_pages - 1)
388                 return FALSE;
389
390         ev_page_cache_set_current_page (page_cache, page_cache->current_page + 1);
391         return TRUE;
392
393 }
394
395 gboolean
396 ev_page_cache_prev_page (EvPageCache *page_cache)
397 {
398         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
399
400         if (page_cache->current_page <= 0)
401                 return FALSE;
402
403         ev_page_cache_set_current_page (page_cache, page_cache->current_page - 1);
404         return TRUE;
405 }
406