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