1 #include "ev-pixbuf-cache.h"
2 #include "ev-job-queue.h"
3 #include "ev-page-cache.h"
4 #include "ev-selection.h"
6 typedef struct _CacheJobInfo
11 /* Data we get from rendering */
14 GdkRegion *text_mapping;
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;
24 GdkRegion *selection_region;
31 /* We keep a link to our containing view just for style information. */
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
41 int preload_cache_size;
42 CacheJobInfo *prev_job;
43 CacheJobInfo *job_list;
44 CacheJobInfo *next_job;
47 struct _EvPixbufCacheClass
49 GObjectClass parent_class;
51 void (* job_finished) (EvPixbufCache *pixbuf_cache);
61 static guint signals[N_SIGNALS] = {0, };
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,
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,
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)
88 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
91 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
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));
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);
103 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
105 GObjectClass *object_class;
107 object_class = G_OBJECT_CLASS (class);
109 object_class->finalize = ev_pixbuf_cache_finalize;
110 object_class->dispose = ev_pixbuf_cache_dispose;
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),
117 g_cclosure_marshal_VOID__VOID,
122 ev_pixbuf_cache_finalize (GObject *object)
124 EvPixbufCache *pixbuf_cache;
126 pixbuf_cache = EV_PIXBUF_CACHE (object);
128 g_free (pixbuf_cache->prev_job);
129 g_free (pixbuf_cache->job_list);
130 g_free (pixbuf_cache->next_job);
134 dispose_cache_job_info (CacheJobInfo *job_info,
137 if (job_info == NULL)
140 g_signal_handlers_disconnect_by_func (job_info->job,
141 G_CALLBACK (job_finished_cb),
143 ev_job_queue_remove_job (job_info->job);
144 g_object_unref (G_OBJECT (job_info->job));
145 job_info->job = NULL;
147 if (job_info->pixbuf) {
148 g_object_unref (G_OBJECT (job_info->pixbuf));
149 job_info->pixbuf = NULL;
151 if (job_info->link_mapping) {
152 ev_link_mapping_free (job_info->link_mapping);
153 job_info->link_mapping = NULL;
155 if (job_info->text_mapping) {
156 gdk_region_destroy (job_info->text_mapping);
157 job_info->text_mapping = NULL;
159 if (job_info->selection) {
160 g_object_unref (G_OBJECT (job_info->selection));
161 job_info->selection = NULL;
163 if (job_info->selection_region) {
164 gdk_region_destroy (job_info->selection_region);
165 job_info->selection_region = NULL;
168 g_object_unref (G_OBJECT (job_info->rc));
172 job_info->points_set = FALSE;
176 ev_pixbuf_cache_dispose (GObject *object)
178 EvPixbufCache *pixbuf_cache;
181 pixbuf_cache = EV_PIXBUF_CACHE (object);
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);
188 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
189 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
195 ev_pixbuf_cache_new (GtkWidget *view,
196 EvDocument *document)
198 EvPixbufCache *pixbuf_cache;
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;
209 job_finished_cb (EvJob *job,
210 EvPixbufCache *pixbuf_cache)
212 CacheJobInfo *job_info;
213 EvJobRender *job_render = EV_JOB_RENDER (job);
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);
222 job_info = find_job_cache (pixbuf_cache, job_render->rc->page);
224 copy_job_to_job_info (job_render, job_info, pixbuf_cache);
226 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0);
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.
233 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
234 CacheJobInfo *job_info,
235 EvPageCache *page_cache,
243 if (job_info->job == NULL)
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,
252 if (width == EV_JOB_RENDER (job_info->job)->target_width &&
253 height == EV_JOB_RENDER (job_info->job)->target_height)
256 g_signal_handlers_disconnect_by_func (job_info->job,
257 G_CALLBACK (job_finished_cb),
259 ev_job_queue_remove_job (job_info->job);
260 g_object_unref (job_info->job);
261 job_info->job = NULL;
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.
268 move_one_job (CacheJobInfo *job_info,
269 EvPixbufCache *pixbuf_cache,
271 CacheJobInfo *new_job_list,
272 CacheJobInfo *new_prev_job,
273 CacheJobInfo *new_next_job,
276 EvJobPriority priority)
278 CacheJobInfo *target_page = NULL;
280 EvJobPriority new_priority;
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);
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));
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));
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;
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;
311 *target_page = *job_info;
312 job_info->job = NULL;
313 job_info->pixbuf = NULL;
314 job_info->link_mapping = NULL;
316 if (new_priority != priority && target_page->job) {
317 ev_job_queue_update_job (target_page->job, new_priority);
324 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
328 CacheJobInfo *new_job_list;
329 CacheJobInfo *new_prev_job;
330 CacheJobInfo *new_next_job;
331 EvPageCache *page_cache;
334 if (pixbuf_cache->start_page == start_page &&
335 pixbuf_cache->end_page == end_page)
338 page_cache = ev_page_cache_get (pixbuf_cache->document);
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);
344 /* We go through each job in the old cache and either clear it or move
345 * it to a new location. */
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++) {
351 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
353 move_one_job (pixbuf_cache->prev_job + i,
355 new_job_list, new_prev_job, new_next_job,
356 start_page, end_page, EV_JOB_PRIORITY_LOW);
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,
365 new_job_list, new_prev_job, new_next_job,
366 start_page, end_page, EV_JOB_PRIORITY_HIGH);
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);
374 move_one_job (pixbuf_cache->next_job + i,
376 new_job_list, new_prev_job, new_next_job,
377 start_page, end_page, EV_JOB_PRIORITY_LOW);
382 g_free (pixbuf_cache->job_list);
383 g_free (pixbuf_cache->prev_job);
384 g_free (pixbuf_cache->next_job);
386 pixbuf_cache->job_list = new_job_list;
387 pixbuf_cache->prev_job = new_prev_job;
388 pixbuf_cache->next_job = new_next_job;
390 pixbuf_cache->start_page = start_page;
391 pixbuf_cache->end_page = end_page;
395 copy_job_to_job_info (EvJobRender *job_render,
396 CacheJobInfo *job_info,
397 EvPixbufCache *pixbuf_cache)
402 pixbuf = g_object_ref (job_render->pixbuf);
403 rc = g_object_ref (job_render->rc);
405 dispose_cache_job_info (job_info, pixbuf_cache);
407 job_info->pixbuf = pixbuf;
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;
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);
425 static CacheJobInfo *
426 find_job_cache (EvPixbufCache *pixbuf_cache,
431 if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
432 page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
435 if (page < pixbuf_cache->start_page) {
436 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
438 g_assert (page_offset >= 0 &&
439 page_offset < pixbuf_cache->preload_cache_size);
440 return pixbuf_cache->prev_job + page_offset;
443 if (page > pixbuf_cache->end_page) {
444 page_offset = (page - (pixbuf_cache->end_page + 1));
446 g_assert (page_offset >= 0 &&
447 page_offset < pixbuf_cache->preload_cache_size);
448 return pixbuf_cache->next_job + page_offset;
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;
458 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
461 EvPageCache *page_cache;
464 page_cache = ev_page_cache_get (pixbuf_cache->document);
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);
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);
476 #define FIRST_VISABLE_PREV(pixbuf_cache) \
477 (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
480 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
482 if (GTK_WIDGET_HAS_FOCUS (widget)) {
483 *text = &widget->style->text [GTK_STATE_SELECTED];
484 *base = &widget->style->base [GTK_STATE_SELECTED];
486 *text = &widget->style->text [GTK_STATE_ACTIVE];
487 *base = &widget->style->base [GTK_STATE_ACTIVE];
492 add_job_if_needed (EvPixbufCache *pixbuf_cache,
493 CacheJobInfo *job_info,
494 EvPageCache *page_cache,
498 EvJobPriority priority)
500 gboolean include_links = FALSE;
501 gboolean include_text = FALSE;
502 gboolean include_selection = FALSE;
504 GdkColor *text, *base;
509 ev_page_cache_get_size (page_cache, page, rotation,
510 scale, &width, &height);
512 if (job_info->pixbuf &&
513 gdk_pixbuf_get_width (job_info->pixbuf) == width &&
514 gdk_pixbuf_get_height (job_info->pixbuf) == height)
517 /* make a new job now */
518 if (job_info->rc == NULL) {
519 job_info->rc = ev_render_context_new (rotation, page, scale);
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);
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)
531 if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
532 include_selection = TRUE;
535 gtk_widget_ensure_style (pixbuf_cache->view);
537 get_selection_colors (pixbuf_cache->view, &text, &base);
539 job_info->job = ev_job_render_new (pixbuf_cache->document,
542 &(job_info->target_points),
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);
553 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
557 EvPageCache *page_cache;
558 CacheJobInfo *job_info;
562 page_cache = ev_page_cache_get (pixbuf_cache->document);
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;
568 add_job_if_needed (pixbuf_cache, job_info,
569 page_cache, page, rotation, scale,
570 EV_JOB_PRIORITY_HIGH);
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;
577 add_job_if_needed (pixbuf_cache, job_info,
578 page_cache, page, rotation, scale,
579 EV_JOB_PRIORITY_LOW);
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;
586 add_job_if_needed (pixbuf_cache, job_info,
587 page_cache, page, rotation, scale,
588 EV_JOB_PRIORITY_LOW);
594 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
599 GList *selection_list)
601 EvPageCache *page_cache;
603 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
605 page_cache = ev_page_cache_get (pixbuf_cache->document);
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);
611 /* First, resize the page_range as needed. We cull old pages
613 ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
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);
619 /* Next, we update the target selection for our pages */
620 ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
622 /* Finally, we add the new jobs for all the sizes that don't have a
624 ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
628 ev_pixbuf_cache_get_pixbuf (EvPixbufCache *pixbuf_cache,
631 CacheJobInfo *job_info;
633 job_info = find_job_cache (pixbuf_cache, page);
634 if (job_info == NULL)
637 /* We don't need to wait for the idle to handle the callback */
639 EV_JOB (job_info->job)->finished) {
640 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
643 return job_info->pixbuf;
647 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
650 CacheJobInfo *job_info;
652 job_info = find_job_cache (pixbuf_cache, page);
653 if (job_info == NULL)
656 /* We don't need to wait for the idle to handle the callback */
658 EV_JOB (job_info->job)->finished) {
659 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
662 return job_info->link_mapping;
666 new_selection_pixbuf_needed (EvPixbufCache *pixbuf_cache,
667 CacheJobInfo *job_info,
671 EvPageCache *page_cache;
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);
679 if (width != gdk_pixbuf_get_width (job_info->selection) ||
680 height != gdk_pixbuf_get_height (job_info->selection))
683 if (job_info->points_set)
690 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
691 CacheJobInfo *job_info,
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;
704 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
707 CacheJobInfo *job_info;
709 job_info = find_job_cache (pixbuf_cache, page);
710 if (job_info == NULL)
713 /* We don't need to wait for the idle to handle the callback */
715 EV_JOB (job_info->job)->finished) {
716 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
719 return job_info->text_mapping;
722 /* Clears the cache of jobs and pixbufs.
725 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
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);
734 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
735 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
741 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
745 /* FIXME: doesn't update running jobs. */
746 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
747 CacheJobInfo *job_info;
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;
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;
762 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
763 CacheJobInfo *job_info;
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;
774 ev_pixbuf_cache_get_selection_pixbuf (EvPixbufCache *pixbuf_cache,
779 CacheJobInfo *job_info;
781 /* the document does not implement the selection interface */
782 if (!EV_IS_SELECTION (pixbuf_cache->document))
785 job_info = find_job_cache (pixbuf_cache, page);
786 if (job_info == NULL)
789 /* No selection on this page */
790 if (!job_info->points_set)
794 g_assert (job_info->rc);
795 ev_render_context_set_scale (job_info->rc, scale);
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
800 if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
801 return job_info->selection;
803 /* Now, lets see if we need to resize the image. If we do, we clear the
805 clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
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.
812 if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
813 EvRectangle *old_points;
814 GdkColor *text, *base;
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);
822 g_assert (job_info->selection != NULL);
823 old_points = &(job_info->selection_points);
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),
831 &(job_info->target_points));
833 gtk_widget_ensure_style (pixbuf_cache->view);
835 get_selection_colors (pixbuf_cache->view, &text, &base);
837 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
838 job_info->rc, &(job_info->selection),
839 &(job_info->target_points),
842 job_info->selection_points = job_info->target_points;
843 ev_document_doc_mutex_unlock ();
846 *region = job_info->selection_region;
847 return job_info->selection;
851 update_job_selection (CacheJobInfo *job_info,
852 EvViewSelection *selection)
854 job_info->points_set = TRUE;
855 job_info->target_points = selection->rect;
859 clear_job_selection (CacheJobInfo *job_info)
861 job_info->points_set = FALSE;
863 if (job_info->selection) {
864 g_object_unref (job_info->selection);
865 job_info->selection = NULL;
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.
874 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
875 GList *selection_list)
877 EvPageCache *page_cache;
878 EvViewSelection *selection;
879 GList *list = selection_list;
883 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
885 if (!EV_IS_SELECTION (pixbuf_cache->document))
888 page_cache = ev_page_cache_get (pixbuf_cache->document);
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++) {
900 if (((EvViewSelection *)list->data)->page == page) {
901 selection = list->data;
903 } else if (((EvViewSelection *)list->data)->page > page)
909 update_job_selection (pixbuf_cache->prev_job + i, selection);
911 clear_job_selection (pixbuf_cache->prev_job + i);
915 page = pixbuf_cache->start_page;
916 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
919 if (((EvViewSelection *)list->data)->page == page) {
920 selection = list->data;
922 } else if (((EvViewSelection *)list->data)->page > page)
928 update_job_selection (pixbuf_cache->job_list + i, selection);
930 clear_job_selection (pixbuf_cache->job_list + i);
934 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
935 if (page >= ev_page_cache_get_n_pages (page_cache))
940 if (((EvViewSelection *)list->data)->page == page) {
941 selection = list->data;
943 } else if (((EvViewSelection *)list->data)->page > page)
949 update_job_selection (pixbuf_cache->next_job + i, selection);
951 clear_job_selection (pixbuf_cache->next_job + i);
957 /* Returns what the pixbuf cache thinks is */
960 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
962 EvPageCache *page_cache;
963 EvViewSelection *selection;
964 GList *retval = NULL;
968 g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
970 page_cache = ev_page_cache_get (pixbuf_cache->document);
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++) {
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);
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);
1006 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1007 if (page >= ev_page_cache_get_n_pages (page_cache))
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);