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