]> www.fi.muni.cz Git - evince.git/blob - shell/ev-pixbuf-cache.c
Set points_set to TRUE so that selections don't disappear after a zoom
[evince.git] / shell / ev-pixbuf-cache.c
1 #include "ev-pixbuf-cache.h"
2 #include "ev-job-queue.h"
3 #include "ev-page-cache.h"
4 #include "ev-selection.h"
5 #include "ev-document-images.h"
6 #include "ev-image.h"
7
8 typedef struct _CacheJobInfo
9 {
10         EvJob *job;
11         EvRenderContext *rc;
12
13         /* Data we get from rendering */
14         GdkPixbuf *pixbuf;
15         GList *link_mapping;
16         GList *image_mapping;
17         GdkRegion *text_mapping;
18         
19         /* Selection data. 
20          * Selection_points are the coordinates encapsulated in selection.
21          * target_points is the target selection size. */
22         EvRectangle selection_points;
23         EvRectangle target_points;
24         gboolean    points_set;
25         
26         GdkPixbuf *selection;
27         GdkRegion *selection_region;
28 } CacheJobInfo;
29
30 struct _EvPixbufCache
31 {
32         GObject parent;
33
34         /* We keep a link to our containing view just for style information. */
35         GtkWidget *view;
36         EvDocument *document;
37         int start_page;
38         int end_page;
39
40         /* preload_cache_size is the number of pages prior to the current
41          * visible area that we cache.  It's normally 1, but could be 2 in the
42          * case of twin pages.
43          */
44         int preload_cache_size;
45         CacheJobInfo *prev_job;
46         CacheJobInfo *job_list;
47         CacheJobInfo *next_job;
48 };
49
50 struct _EvPixbufCacheClass
51 {
52         GObjectClass parent_class;
53
54         void (* job_finished) (EvPixbufCache *pixbuf_cache);
55 };
56
57
58 enum
59 {
60         JOB_FINISHED,
61         N_SIGNALS,
62 };
63
64 static guint signals[N_SIGNALS] = {0, };
65
66 static void          ev_pixbuf_cache_init       (EvPixbufCache      *pixbuf_cache);
67 static void          ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache);
68 static void          ev_pixbuf_cache_finalize   (GObject            *object);
69 static void          ev_pixbuf_cache_dispose    (GObject            *object);
70 static void          job_finished_cb            (EvJob              *job,
71                                                  EvPixbufCache      *pixbuf_cache);
72 static CacheJobInfo *find_job_cache             (EvPixbufCache      *pixbuf_cache,
73                                                  int                 page);
74 static void          copy_job_to_job_info       (EvJobRender        *job_render,
75                                                  CacheJobInfo       *job_info,
76                                                  EvPixbufCache      *pixbuf_cache);
77 static gboolean      new_selection_pixbuf_needed(EvPixbufCache      *pixbuf_cache,
78                                                  CacheJobInfo       *job_info,
79                                                  gint                page,
80                                                  gfloat              scale);
81
82
83 /* These are used for iterating through the prev and next arrays */
84 #define FIRST_VISABLE_PREV(pixbuf_cache) \
85         (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
86 #define VISIBLE_NEXT_LEN(pixbuf_cache, page_cache) \
87         (MIN(pixbuf_cache->preload_cache_size, ev_page_cache_get_n_pages (page_cache) - (1 + pixbuf_cache->end_page)))
88 #define PAGE_CACHE_LEN(pixbuf_cache) \
89         ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1)
90
91 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
92
93 static void
94 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
95 {
96         pixbuf_cache->start_page = 0;
97         pixbuf_cache->end_page = 0;
98         pixbuf_cache->job_list = g_new0 (CacheJobInfo, PAGE_CACHE_LEN (pixbuf_cache));
99
100         pixbuf_cache->preload_cache_size = 2;
101         pixbuf_cache->prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
102         pixbuf_cache->next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
103 }
104
105 static void
106 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
107 {
108         GObjectClass *object_class;
109
110         object_class = G_OBJECT_CLASS (class);
111
112         object_class->finalize = ev_pixbuf_cache_finalize;
113         object_class->dispose = ev_pixbuf_cache_dispose;
114
115         signals[JOB_FINISHED] = g_signal_new ("job-finished",
116                                             G_OBJECT_CLASS_TYPE (object_class),
117                                             G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
118                                             G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished),
119                                             NULL, NULL,
120                                             g_cclosure_marshal_VOID__VOID,
121                                             G_TYPE_NONE, 0);
122 }
123
124 static void
125 ev_pixbuf_cache_finalize (GObject *object)
126 {
127         EvPixbufCache *pixbuf_cache;
128
129         pixbuf_cache = EV_PIXBUF_CACHE (object);
130
131         g_free (pixbuf_cache->prev_job);
132         g_free (pixbuf_cache->job_list);
133         g_free (pixbuf_cache->next_job);
134 }
135
136 static void
137 dispose_cache_job_info (CacheJobInfo *job_info,
138                         gpointer      data)
139 {
140         if (job_info == NULL)
141                 return;
142         if (job_info->job) {
143                 g_signal_handlers_disconnect_by_func (job_info->job,
144                                                       G_CALLBACK (job_finished_cb),
145                                                       data);
146                 ev_job_queue_remove_job (job_info->job);
147                 g_object_unref (G_OBJECT (job_info->job));
148                 job_info->job = NULL;
149         }
150         if (job_info->pixbuf) {
151                 g_object_unref (G_OBJECT (job_info->pixbuf));
152                 job_info->pixbuf = NULL;
153         }
154         if (job_info->link_mapping) {
155                 ev_link_mapping_free (job_info->link_mapping);
156                 job_info->link_mapping = NULL;
157         }
158         if (job_info->image_mapping) {
159                 ev_image_mapping_free (job_info->image_mapping);
160                 job_info->image_mapping = NULL;
161         }
162         if (job_info->text_mapping) {
163                 gdk_region_destroy (job_info->text_mapping);
164                 job_info->text_mapping = NULL;
165         }
166         if (job_info->selection) {
167                 g_object_unref (G_OBJECT (job_info->selection));
168                 job_info->selection = NULL;
169         }
170         if (job_info->selection_region) {
171                 gdk_region_destroy (job_info->selection_region);
172                 job_info->selection_region = NULL;
173         }
174         if (job_info->rc) {
175                 g_object_unref (G_OBJECT (job_info->rc));
176                 job_info->rc = NULL;
177         }
178
179         job_info->points_set = FALSE;
180 }
181
182 static void
183 ev_pixbuf_cache_dispose (GObject *object)
184 {
185         EvPixbufCache *pixbuf_cache;
186         int i;
187
188         pixbuf_cache = EV_PIXBUF_CACHE (object);
189
190         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
191                 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
192                 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
193         }
194
195         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
196                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
197         }
198 }
199
200
201 EvPixbufCache *
202 ev_pixbuf_cache_new (GtkWidget  *view,
203                      EvDocument *document)
204 {
205         EvPixbufCache *pixbuf_cache;
206
207         pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
208         /* This is a backlink, so we don't ref this */ 
209         pixbuf_cache->view = view;
210         pixbuf_cache->document = document;
211
212         return pixbuf_cache;
213 }
214
215 static void
216 job_finished_cb (EvJob         *job,
217                  EvPixbufCache *pixbuf_cache)
218 {
219         CacheJobInfo *job_info;
220         EvJobRender *job_render = EV_JOB_RENDER (job);
221
222         /* If the job is outside of our interest, we silently discard it */
223         if ((job_render->rc->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
224             (job_render->rc->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
225                 g_object_unref (job);
226                 return;
227         }
228         
229         job_info = find_job_cache (pixbuf_cache, job_render->rc->page);
230
231         copy_job_to_job_info (job_render, job_info, pixbuf_cache);
232
233         g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0);
234 }
235
236 /* This checks a job to see if the job would generate the right sized pixbuf
237  * given a scale.  If it won't, it removes the job and clears it to NULL.
238  */
239 static void
240 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
241                           CacheJobInfo *job_info,
242                           EvPageCache  *page_cache,
243                           gfloat        scale)
244 {
245         gint width;
246         gint height;
247
248         g_assert (job_info);
249
250         if (job_info->job == NULL)
251                 return;
252
253         ev_page_cache_get_size (page_cache,
254                                 EV_JOB_RENDER (job_info->job)->rc->page,
255                                 EV_JOB_RENDER (job_info->job)->rc->rotation,
256                                 scale,
257                                 &width, &height);
258                                 
259         if (width == EV_JOB_RENDER (job_info->job)->target_width &&
260             height == EV_JOB_RENDER (job_info->job)->target_height)
261                 return;
262
263         g_signal_handlers_disconnect_by_func (job_info->job,
264                                               G_CALLBACK (job_finished_cb),
265                                               pixbuf_cache);
266         ev_job_queue_remove_job (job_info->job);
267         g_object_unref (job_info->job);
268         job_info->job = NULL;
269 }
270
271 /* Do all function that copies a job from an older cache to it's position in the
272  * new cache.  It clears the old job if it doesn't have a place.
273  */
274 static void
275 move_one_job (CacheJobInfo  *job_info,
276               EvPixbufCache *pixbuf_cache,
277               int            page,
278               CacheJobInfo  *new_job_list,
279               CacheJobInfo  *new_prev_job,
280               CacheJobInfo  *new_next_job,
281               int            start_page,
282               int            end_page,
283               EvJobPriority  priority)
284 {
285         CacheJobInfo *target_page = NULL;
286         int page_offset;
287         EvJobPriority new_priority;
288
289         if (page < (start_page - pixbuf_cache->preload_cache_size) ||
290             page > (end_page + pixbuf_cache->preload_cache_size)) {
291                 dispose_cache_job_info (job_info, pixbuf_cache);
292                 return;
293         }
294
295         /* find the target page to copy it over to. */
296         if (page < start_page) {
297                 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
298
299                 g_assert (page_offset >= 0 &&
300                           page_offset < pixbuf_cache->preload_cache_size);
301                 target_page = new_prev_job + page_offset;
302                 new_priority = EV_JOB_PRIORITY_LOW;
303         } else if (page > end_page) {
304                 page_offset = (page - (end_page + 1));
305
306                 g_assert (page_offset >= 0 &&
307                           page_offset < pixbuf_cache->preload_cache_size);
308                 target_page = new_next_job + page_offset;
309                 new_priority = EV_JOB_PRIORITY_LOW;
310         } else {
311                 page_offset = page - start_page;
312                 g_assert (page_offset >= 0 &&
313                           page_offset <= ((end_page - start_page) + 1));
314                 new_priority = EV_JOB_PRIORITY_HIGH;
315                 target_page = new_job_list + page_offset;
316         }
317
318         *target_page = *job_info;
319         job_info->job = NULL;
320         job_info->pixbuf = NULL;
321         job_info->link_mapping = NULL;
322         job_info->image_mapping = NULL;
323
324         if (new_priority != priority && target_page->job) {
325                 ev_job_queue_update_job (target_page->job, new_priority);
326         }
327 }
328
329
330
331 static void
332 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
333                               gint           start_page,
334                               gint           end_page)
335 {
336         CacheJobInfo *new_job_list;
337         CacheJobInfo *new_prev_job;
338         CacheJobInfo *new_next_job;
339         EvPageCache *page_cache;
340         int i, page;
341
342         if (pixbuf_cache->start_page == start_page &&
343             pixbuf_cache->end_page == end_page)
344                 return;
345
346         page_cache = ev_page_cache_get (pixbuf_cache->document);
347
348         new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
349         new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
350         new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
351
352         /* We go through each job in the old cache and either clear it or move
353          * it to a new location. */
354
355         /* Start with the prev cache. */
356         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
357         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
358                 if (page < 0) {
359                         dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
360                 } else {
361                         move_one_job (pixbuf_cache->prev_job + i,
362                                       pixbuf_cache, page,
363                                       new_job_list, new_prev_job, new_next_job,
364                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
365                 }
366                 page ++;
367         }
368
369         page = pixbuf_cache->start_page;
370         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
371                 move_one_job (pixbuf_cache->job_list + i,
372                               pixbuf_cache, page,
373                               new_job_list, new_prev_job, new_next_job,
374                               start_page, end_page, EV_JOB_PRIORITY_HIGH);
375                 page ++;
376         }
377
378         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
379                 if (page >= ev_page_cache_get_n_pages (page_cache)) {
380                         dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
381                 } else {
382                         move_one_job (pixbuf_cache->next_job + i,
383                                       pixbuf_cache, page,
384                                       new_job_list, new_prev_job, new_next_job,
385                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
386                 }
387                 page ++;
388         }
389
390         g_free (pixbuf_cache->job_list);
391         g_free (pixbuf_cache->prev_job);
392         g_free (pixbuf_cache->next_job);
393
394         pixbuf_cache->job_list = new_job_list;
395         pixbuf_cache->prev_job = new_prev_job;
396         pixbuf_cache->next_job = new_next_job;
397
398         pixbuf_cache->start_page = start_page;
399         pixbuf_cache->end_page = end_page;
400 }
401
402 static void
403 copy_job_to_job_info (EvJobRender   *job_render,
404                       CacheJobInfo  *job_info,
405                       EvPixbufCache *pixbuf_cache)
406 {
407
408         job_info->points_set = FALSE;
409
410         if (job_info->pixbuf) {
411                 g_object_unref (G_OBJECT (job_info->pixbuf));
412         }
413         job_info->pixbuf = g_object_ref (job_render->pixbuf);
414
415         if (job_info->rc) {
416                 g_object_unref (G_OBJECT (job_info->rc));
417         }
418         job_info->rc = g_object_ref (job_render->rc);
419
420         if (job_render->include_links) {
421                 if (job_info->link_mapping)
422                         ev_link_mapping_free (job_info->link_mapping);
423                 job_info->link_mapping = job_render->link_mapping;
424         }
425
426         if (job_render->include_images) {
427                 if (job_info->image_mapping)
428                         ev_image_mapping_free (job_info->image_mapping);
429                 job_info->image_mapping = job_render->image_mapping;
430         }
431
432         if (job_render->include_text) {
433                 if (job_info->text_mapping)
434                         gdk_region_destroy (job_info->text_mapping);
435                 job_info->text_mapping = job_render->text_mapping;
436         }
437
438         if (job_render->include_selection) {
439                 if (job_info->selection) {
440                         g_object_unref (G_OBJECT (job_info->selection));
441                         job_info->selection = NULL;
442                 }
443                 if (job_info->selection_region) {
444                         gdk_region_destroy (job_info->selection_region);
445                         job_info->selection_region = NULL;
446                 }
447                 
448                 job_info->selection_points = job_render->selection_points;
449                 job_info->selection_region = gdk_region_copy (job_render->selection_region);
450                 job_info->selection = g_object_ref (job_render->selection);
451                 g_assert (job_info->selection_points.x1 >= 0);
452                 job_info->points_set = TRUE;
453         }
454
455         if (job_info->job) {
456                 g_signal_handlers_disconnect_by_func (job_info->job,
457                                                       G_CALLBACK (job_finished_cb),
458                                                       pixbuf_cache);
459                 ev_job_queue_remove_job (job_info->job);
460                 g_object_unref (G_OBJECT (job_info->job));
461                 job_info->job = NULL;
462         }
463 }
464
465 static CacheJobInfo *
466 find_job_cache (EvPixbufCache *pixbuf_cache,
467                 int            page)
468 {
469         int page_offset;
470
471         if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
472             page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
473                 return NULL;
474
475         if (page < pixbuf_cache->start_page) {
476                 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
477
478                 g_assert (page_offset >= 0 &&
479                           page_offset < pixbuf_cache->preload_cache_size);
480                 return pixbuf_cache->prev_job + page_offset;
481         }
482
483         if (page > pixbuf_cache->end_page) {
484                 page_offset = (page - (pixbuf_cache->end_page + 1));
485
486                 g_assert (page_offset >= 0 &&
487                           page_offset < pixbuf_cache->preload_cache_size);
488                 return pixbuf_cache->next_job + page_offset;
489         }
490
491         page_offset = page - pixbuf_cache->start_page;
492         g_assert (page_offset >= 0 &&
493                   page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
494         return pixbuf_cache->job_list + page_offset;
495 }
496
497 static void
498 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
499                                  gfloat         scale)
500 {
501         EvPageCache *page_cache;
502         int i;
503
504         page_cache = ev_page_cache_get (pixbuf_cache->document);
505
506         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
507                 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, page_cache, scale);
508         }
509
510         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
511                 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, page_cache, scale);
512                 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, page_cache, scale);
513         }
514 }
515
516 #define FIRST_VISABLE_PREV(pixbuf_cache) \
517         (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
518
519 static void
520 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
521 {
522     if (GTK_WIDGET_HAS_FOCUS (widget)) {
523         *text = &widget->style->text [GTK_STATE_SELECTED];
524         *base = &widget->style->base [GTK_STATE_SELECTED];
525     } else {
526         *text = &widget->style->text [GTK_STATE_ACTIVE];
527         *base = &widget->style->base [GTK_STATE_ACTIVE];
528     }
529 }
530
531 static void
532 add_job_if_needed (EvPixbufCache *pixbuf_cache,
533                    CacheJobInfo  *job_info,
534                    EvPageCache   *page_cache,
535                    gint           page,
536                    gint           rotation,
537                    gfloat         scale,
538                    EvJobPriority  priority)
539 {
540         gboolean include_links = FALSE;
541         gboolean include_text = FALSE;
542         gboolean include_selection = FALSE;
543         gboolean include_images = FALSE;
544         int width, height;
545         GdkColor *text, *base;
546
547         if (job_info->job)
548                 return;
549
550         ev_page_cache_get_size (page_cache, page, rotation,
551                                 scale, &width, &height);
552
553         if (job_info->pixbuf &&
554             gdk_pixbuf_get_width (job_info->pixbuf) == width &&
555             gdk_pixbuf_get_height (job_info->pixbuf) == height)
556                 return;
557
558         /* make a new job now */
559         if (job_info->rc == NULL) {
560                 job_info->rc = ev_render_context_new (rotation, page, scale);
561         } else {
562                 ev_render_context_set_rotation (job_info->rc, rotation);
563                 ev_render_context_set_page (job_info->rc, page);
564                 ev_render_context_set_scale (job_info->rc, scale);
565         }
566
567         /* Figure out what else we need for this job */
568         if (job_info->link_mapping == NULL)
569                 include_links = TRUE;
570         if (job_info->image_mapping == NULL)
571                 include_images = TRUE;
572         if (job_info->text_mapping == NULL)
573                 include_text = TRUE;
574         if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
575                 include_selection = TRUE;
576         }
577
578         gtk_widget_ensure_style (pixbuf_cache->view);
579
580         get_selection_colors (pixbuf_cache->view, &text, &base);
581
582         job_info->job = ev_job_render_new (pixbuf_cache->document,
583                                            job_info->rc,
584                                            width, height,
585                                            &(job_info->target_points),
586                                            text, base,
587                                            include_links,
588                                            include_images,
589                                            include_text,
590                                            include_selection);
591         ev_job_queue_add_job (job_info->job, priority);
592         g_signal_connect (job_info->job, "finished", G_CALLBACK (job_finished_cb), pixbuf_cache);
593 }
594
595
596 static void
597 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
598                                     gint           rotation,
599                                     gfloat         scale)
600 {
601         EvPageCache *page_cache;
602         CacheJobInfo *job_info;
603         int page;
604         int i;
605
606         page_cache = ev_page_cache_get (pixbuf_cache->document);
607
608         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
609                 job_info = (pixbuf_cache->job_list + i);
610                 page = pixbuf_cache->start_page + i;
611
612                 add_job_if_needed (pixbuf_cache, job_info,
613                                    page_cache, page, rotation, scale,
614                                    EV_JOB_PRIORITY_HIGH);
615         }
616
617         for (i = FIRST_VISABLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
618                 job_info = (pixbuf_cache->prev_job + i);
619                 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
620
621                 add_job_if_needed (pixbuf_cache, job_info,
622                                    page_cache, page, rotation, scale,
623                                    EV_JOB_PRIORITY_LOW);
624         }
625
626         for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
627                 job_info = (pixbuf_cache->next_job + i);
628                 page = pixbuf_cache->end_page + 1 + i;
629
630                 add_job_if_needed (pixbuf_cache, job_info,
631                                    page_cache, page, rotation, scale,
632                                    EV_JOB_PRIORITY_LOW);
633         }
634
635 }
636
637 void
638 ev_pixbuf_cache_set_page_range (EvPixbufCache  *pixbuf_cache,
639                                 gint            start_page,
640                                 gint            end_page,
641                                 gint            rotation,
642                                 gfloat          scale,
643                                 GList          *selection_list)
644 {
645         EvPageCache *page_cache;
646
647         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
648
649         page_cache = ev_page_cache_get (pixbuf_cache->document);
650
651         g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
652         g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
653         g_return_if_fail (end_page >= start_page);
654
655         /* First, resize the page_range as needed.  We cull old pages
656          * mercilessly. */
657         ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
658
659         /* Then, we update the current jobs to see if any of them are the wrong
660          * size, we remove them if we need to. */
661         ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
662
663         /* Next, we update the target selection for our pages */
664         ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
665
666         /* Finally, we add the new jobs for all the sizes that don't have a
667          * pixbuf */
668         ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
669 }
670
671 GdkPixbuf *
672 ev_pixbuf_cache_get_pixbuf (EvPixbufCache *pixbuf_cache,
673                             gint           page)
674 {
675         CacheJobInfo *job_info;
676
677         job_info = find_job_cache (pixbuf_cache, page);
678         if (job_info == NULL)
679                 return NULL;
680
681         /* We don't need to wait for the idle to handle the callback */
682         if (job_info->job &&
683             EV_JOB (job_info->job)->finished) {
684                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
685         }
686
687         return job_info->pixbuf;
688 }
689
690 GList *
691 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
692                                   gint           page)
693 {
694         CacheJobInfo *job_info;
695
696         job_info = find_job_cache (pixbuf_cache, page);
697         if (job_info == NULL)
698                 return NULL;
699
700         /* We don't need to wait for the idle to handle the callback */
701         if (job_info->job &&
702             EV_JOB (job_info->job)->finished) {
703                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
704         }
705
706         return job_info->link_mapping;
707 }
708
709 GList *
710 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
711                                    gint           page)
712 {
713         CacheJobInfo *job_info;
714
715         if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
716                 return NULL;
717         
718         job_info = find_job_cache (pixbuf_cache, page);
719         if (job_info == NULL)
720                 return NULL;
721
722         /* We don't need to wait for the idle to handle the callback */
723         if (job_info->job &&
724             EV_JOB (job_info->job)->finished) {
725                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
726         }
727
728         return job_info->image_mapping;
729 }
730
731 static gboolean
732 new_selection_pixbuf_needed (EvPixbufCache *pixbuf_cache,
733                              CacheJobInfo  *job_info,
734                              gint           page,
735                              gfloat         scale)
736 {
737         EvPageCache *page_cache;
738         gint width, height;
739
740         if (job_info->selection) {
741                 page_cache = ev_page_cache_get (pixbuf_cache->document);
742                 ev_page_cache_get_size (page_cache, page, job_info->rc->rotation,
743                                         scale, &width, &height);
744                 
745                 if (width != gdk_pixbuf_get_width (job_info->selection) ||
746                     height != gdk_pixbuf_get_height (job_info->selection))
747                         return TRUE;
748         } else {
749                 if (job_info->points_set)
750                         return TRUE;
751         }
752         return FALSE;
753 }
754
755 static void
756 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
757                            CacheJobInfo  *job_info,
758                            gint           page,
759                            gfloat         scale)
760 {
761         if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
762                 if (job_info->selection)
763                         g_object_unref (job_info->selection);
764                 job_info->selection = NULL;
765                 job_info->selection_points.x1 = -1;
766         }
767 }
768
769 GdkRegion *
770 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
771                                   gint           page)
772 {
773         CacheJobInfo *job_info;
774
775         job_info = find_job_cache (pixbuf_cache, page);
776         if (job_info == NULL)
777                 return NULL;
778
779         /* We don't need to wait for the idle to handle the callback */
780         if (job_info->job &&
781             EV_JOB (job_info->job)->finished) {
782                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
783         }
784         
785         return job_info->text_mapping;
786 }
787
788 /* Clears the cache of jobs and pixbufs.
789  */
790 void
791 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
792 {
793         int i;
794
795         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
796                 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
797                 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
798         }
799
800         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
801                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
802         }
803 }
804
805
806 void
807 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
808 {
809         gint i;
810
811         /* FIXME: doesn't update running jobs. */
812         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
813                 CacheJobInfo *job_info;
814
815                 job_info = pixbuf_cache->prev_job + i;
816                 if (job_info->selection) {
817                         g_object_unref (G_OBJECT (job_info->selection));
818                         job_info->selection = NULL;
819                 }
820
821                 job_info = pixbuf_cache->next_job + i;
822                 if (job_info->selection) {
823                         g_object_unref (G_OBJECT (job_info->selection));
824                         job_info->selection = NULL;
825                 }
826         }
827
828         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
829                 CacheJobInfo *job_info;
830
831                 job_info = pixbuf_cache->job_list + i;
832                 if (job_info->selection) {
833                         g_object_unref (G_OBJECT (job_info->selection));
834                         job_info->selection = NULL;
835                 }
836         }
837 }
838
839 GdkPixbuf *
840 ev_pixbuf_cache_get_selection_pixbuf (EvPixbufCache  *pixbuf_cache,
841                                       gint            page,
842                                       gfloat          scale,
843                                       GdkRegion     **region)
844 {
845         CacheJobInfo *job_info;
846
847         /* the document does not implement the selection interface */
848         if (!EV_IS_SELECTION (pixbuf_cache->document))
849                 return NULL;
850
851         job_info = find_job_cache (pixbuf_cache, page);
852         if (job_info == NULL)
853                 return NULL;
854
855         /* No selection on this page */
856         if (!job_info->points_set)
857                 return NULL;
858
859         /* Update the rc */
860         g_assert (job_info->rc);
861         ev_render_context_set_scale (job_info->rc, scale);
862
863         /* If we have a running job, we just return what we have under the
864          * assumption that it'll be updated later and we can scale it as need
865          * be */
866         if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
867                 return job_info->selection;
868
869         /* Now, lets see if we need to resize the image.  If we do, we clear the
870          * old one. */
871         clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
872
873         /* Finally, we see if the two scales are the same, and get a new pixbuf
874          * if needed.  We do this synchronously for now.  At some point, we
875          * _should_ be able to get rid of the doc_mutex, so the synchronicity
876          * doesn't kill us.  Rendering a few glyphs should really be fast.
877          */
878         if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
879                 EvRectangle *old_points;
880                 GdkColor *text, *base;
881
882                 /* we need to get a new selection pixbuf */
883                 ev_document_doc_mutex_lock ();
884                 if (job_info->selection_points.x1 < 0) {
885                         g_assert (job_info->selection == NULL);
886                         old_points = NULL;
887                 } else {
888                         g_assert (job_info->selection != NULL);
889                         old_points = &(job_info->selection_points);
890                 }
891
892                 if (job_info->selection_region)
893                         gdk_region_destroy (job_info->selection_region);
894                 job_info->selection_region =
895                         ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
896                                                            job_info->rc,
897                                                            &(job_info->target_points));
898
899                 gtk_widget_ensure_style (pixbuf_cache->view);
900
901                 get_selection_colors (pixbuf_cache->view, &text, &base);
902
903                 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
904                                                job_info->rc, &(job_info->selection),
905                                                &(job_info->target_points),
906                                                old_points,
907                                                text, base);
908                 job_info->selection_points = job_info->target_points;
909                 ev_document_doc_mutex_unlock ();
910         }
911         if (region)
912                 *region = job_info->selection_region;
913         return job_info->selection;
914 }
915
916 static void
917 update_job_selection (CacheJobInfo    *job_info,
918                       EvViewSelection *selection)
919 {
920         job_info->points_set = TRUE;            
921         job_info->target_points = selection->rect;
922 }
923
924 static void
925 clear_job_selection (CacheJobInfo *job_info)
926 {
927         job_info->points_set = FALSE;
928         job_info->selection_points.x1 = -1;
929
930         if (job_info->selection) {
931                 g_object_unref (job_info->selection);
932                 job_info->selection = NULL;
933         }
934 }
935
936 /* This function will reset the selection on pages that no longer have them, and
937  * will update the target_selection on those that need it.  It will _not_ free
938  * the previous selection_list -- that's up to caller to do.
939  */
940 void
941 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
942                                     GList         *selection_list)
943 {
944         EvPageCache *page_cache;
945         EvViewSelection *selection;
946         GList *list = selection_list;
947         int page;
948         int i;
949
950         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
951
952         if (!EV_IS_SELECTION (pixbuf_cache->document))
953                 return;
954
955         page_cache = ev_page_cache_get (pixbuf_cache->document);
956
957         /* We check each area to see what needs updating, and what needs freeing; */
958         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
959         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
960                 if (page < 0) {
961                         page ++;
962                         continue;
963                 }
964
965                 selection = NULL;
966                 while (list) {
967                         if (((EvViewSelection *)list->data)->page == page) {
968                                 selection = list->data;
969                                 break;
970                         } else if (((EvViewSelection *)list->data)->page > page) 
971                                 break;
972                         list = list->next;
973                 }
974
975                 if (selection)
976                         update_job_selection (pixbuf_cache->prev_job + i, selection);
977                 else
978                         clear_job_selection (pixbuf_cache->prev_job + i);
979                 page ++;
980         }
981
982         page = pixbuf_cache->start_page;
983         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
984                 selection = NULL;
985                 while (list) {
986                         if (((EvViewSelection *)list->data)->page == page) {
987                                 selection = list->data;
988                                 break;
989                         } else if (((EvViewSelection *)list->data)->page > page) 
990                                 break;
991                         list = list->next;
992                 }
993
994                 if (selection)
995                         update_job_selection (pixbuf_cache->job_list + i, selection);
996                 else
997                         clear_job_selection (pixbuf_cache->job_list + i);
998                 page ++;
999         }
1000
1001         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1002                 if (page >= ev_page_cache_get_n_pages (page_cache))
1003                         break;
1004
1005                 selection = NULL;
1006                 while (list) {
1007                         if (((EvViewSelection *)list->data)->page == page) {
1008                                 selection = list->data;
1009                                 break;
1010                         } else if (((EvViewSelection *)list->data)->page > page) 
1011                                 break;
1012                         list = list->next;
1013                 }
1014
1015                 if (selection)
1016                         update_job_selection (pixbuf_cache->next_job + i, selection);
1017                 else
1018                         clear_job_selection (pixbuf_cache->next_job + i);
1019                 page ++;
1020         }
1021 }
1022
1023
1024 /* Returns what the pixbuf cache thinks is */
1025
1026 GList *
1027 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1028 {
1029         EvPageCache *page_cache;
1030         EvViewSelection *selection;
1031         GList *retval = NULL;
1032         int page;
1033         int i;
1034
1035         g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1036
1037         page_cache = ev_page_cache_get (pixbuf_cache->document);
1038
1039         /* We check each area to see what needs updating, and what needs freeing; */
1040         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1041         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1042                 if (page < 0) {
1043                         page ++;
1044                         continue;
1045                 }
1046
1047                 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1048                         selection = g_new0 (EvViewSelection, 1);
1049                         selection->page = page;
1050                         selection->rect = pixbuf_cache->prev_job[i].selection_points;
1051                         if (pixbuf_cache->prev_job[i].selection_region)
1052                                 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1053                         retval = g_list_append (retval, selection);
1054                 }
1055                 
1056                 page ++;
1057         }
1058
1059         page = pixbuf_cache->start_page;
1060         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1061                 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1062                         selection = g_new0 (EvViewSelection, 1);
1063                         selection->page = page;
1064                         selection->rect = pixbuf_cache->job_list[i].selection_points;
1065                         if (pixbuf_cache->job_list[i].selection_region)
1066                                 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1067                         retval = g_list_append (retval, selection);
1068                 }
1069                 
1070                 page ++;
1071         }
1072
1073         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1074                 if (page >= ev_page_cache_get_n_pages (page_cache))
1075                         break;
1076
1077                 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1078                         selection = g_new0 (EvViewSelection, 1);
1079                         selection->page = page;
1080                         selection->rect = pixbuf_cache->next_job[i].selection_points;
1081                         if (pixbuf_cache->next_job[i].selection_region)
1082                                 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1083                         retval = g_list_append (retval, selection);
1084                 }
1085                 
1086                 page ++;
1087         }
1088
1089         return retval;
1090 }
1091