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