overlay now survives image split
[geeqie.git] / src / pixbuf-renderer.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2009 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <math.h>
17
18 #include "pixbuf-renderer.h"
19 #include "intl.h"
20
21 #include <gtk/gtk.h>
22
23
24 /* comment this out if not using this from within Geeqie
25  * defining GQ_BUILD does these things:
26  *   - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
27  */
28 #define GQ_BUILD 1
29
30 #ifdef GQ_BUILD
31 #include "main.h"
32 #include "pixbuf_util.h"
33 #include "exif.h"
34 #else
35 typedef enum {
36         EXIF_ORIENTATION_UNKNOWN        = 0,
37         EXIF_ORIENTATION_TOP_LEFT       = 1,
38         EXIF_ORIENTATION_TOP_RIGHT      = 2,
39         EXIF_ORIENTATION_BOTTOM_RIGHT   = 3,
40         EXIF_ORIENTATION_BOTTOM_LEFT    = 4,
41         EXIF_ORIENTATION_LEFT_TOP       = 5,
42         EXIF_ORIENTATION_RIGHT_TOP      = 6,
43         EXIF_ORIENTATION_RIGHT_BOTTOM   = 7,
44         EXIF_ORIENTATION_LEFT_BOTTOM    = 8
45 } ExifOrientationType;
46 #endif
47
48
49 /* size to use when breaking up image pane for rendering */
50 #define PR_TILE_SIZE 128
51
52 /* default size of tile cache (mb) */
53 #define PR_CACHE_SIZE_DEFAULT 8
54
55 /* default min and max zoom */
56 #define PR_ZOOM_MIN -32.0
57 #define PR_ZOOM_MAX 32.0
58
59 /* distance to drag mouse to disable image flip */
60 #define PR_DRAG_SCROLL_THRESHHOLD 4
61
62 /* increase pan rate when holding down shift */
63 #define PR_PAN_SHIFT_MULTIPLIER 6
64
65 /* scroller config */
66 #define PR_SCROLLER_UPDATES_PER_SEC 30
67 #define PR_SCROLLER_DEAD_ZONE 6
68
69 /* alpha channel checkerboard background (same as gimp) */
70 #define PR_ALPHA_CHECK1 0x00999999
71 #define PR_ALPHA_CHECK2 0x00666666
72 #define PR_ALPHA_CHECK_SIZE 16
73
74 /* when scaling image to below this size, use nearest pixel for scaling
75  * (below about 4, the other scale types become slow generating their conversion tables)
76  */
77 #define PR_MIN_SCALE_SIZE 8
78
79 typedef enum {
80         TILE_RENDER_NONE = 0,   /* do nothing */
81         TILE_RENDER_AREA,       /* render an area of the tile */
82         TILE_RENDER_ALL         /* render the whole tile */
83 } ImageTileRenderType;
84
85 typedef struct _ImageTile ImageTile;
86 typedef struct _QueueData QueueData;
87
88 struct _ImageTile
89 {
90         GdkPixmap *pixmap;      /* off screen buffer */
91         GdkPixbuf *pixbuf;      /* pixbuf area for zooming */
92         gint x;                 /* x offset into image */
93         gint y;                 /* y offset into image */
94         gint w;                 /* width that is visible (may be less if at edge of image) */
95         gint h;                 /* height '' */
96
97         gboolean blank;
98
99 /* render_todo: (explanation)
100         NONE    do nothing
101         AREA    render area of tile, usually only used when loading an image
102                 note: will jump to an ALL if render_done is not ALL.
103         ALL     render entire tile, if never done before w/ ALL, for expose events *only*
104 */
105
106         ImageTileRenderType render_todo;        /* what to do (see above) */
107         ImageTileRenderType render_done;        /* highest that has been done before on tile */
108
109         QueueData *qd;
110         QueueData *qd2;
111
112         guint size;             /* est. memory used by pixmap and pixbuf */
113 };
114
115 struct _QueueData
116 {
117         ImageTile *it;
118         gint x;
119         gint y;
120         gint w;
121         gint h;
122         gboolean new_data;
123 };
124
125 typedef struct _SourceTile SourceTile;
126 struct _SourceTile
127 {
128         gint x;
129         gint y;
130         GdkPixbuf *pixbuf;
131         gboolean blank;
132 };
133
134 typedef struct _OverlayData OverlayData;
135 struct _OverlayData
136 {
137         gint id;
138
139         GdkPixbuf *pixbuf;
140         GdkWindow *window;
141
142         gint x;
143         gint y;
144
145         OverlayRendererFlags flags;
146 };
147
148 enum {
149         SIGNAL_ZOOM = 0,
150         SIGNAL_CLICKED,
151         SIGNAL_SCROLL_NOTIFY,
152         SIGNAL_RENDER_COMPLETE,
153         SIGNAL_DRAG,
154         SIGNAL_COUNT
155 };
156
157 enum {
158         PROP_0,
159         PROP_ZOOM_MIN,
160         PROP_ZOOM_MAX,
161         PROP_ZOOM_QUALITY,
162         PROP_ZOOM_2PASS,
163         PROP_ZOOM_EXPAND,
164         PROP_DITHER_QUALITY,
165         PROP_SCROLL_RESET,
166         PROP_DELAY_FLIP,
167         PROP_LOADING,
168         PROP_COMPLETE,
169         PROP_CACHE_SIZE_DISPLAY,
170         PROP_CACHE_SIZE_TILES,
171         PROP_WINDOW_FIT,
172         PROP_WINDOW_LIMIT,
173         PROP_WINDOW_LIMIT_VALUE,
174         PROP_AUTOFIT_LIMIT,
175         PROP_AUTOFIT_LIMIT_VALUE
176 };
177
178 typedef enum {
179         PR_ZOOM_NONE            = 0,
180         PR_ZOOM_FORCE           = 1 << 0,
181         PR_ZOOM_NEW             = 1 << 1,
182         PR_ZOOM_CENTER          = 1 << 2,
183         PR_ZOOM_INVALIDATE      = 1 << 3,
184         PR_ZOOM_LAZY            = 1 << 4  /* wait with redraw for pixbuf_renderer_area_changed */
185 } PrZoomFlags;
186
187 static guint signals[SIGNAL_COUNT] = { 0 };
188 static GtkEventBoxClass *parent_class = NULL;
189
190
191
192 static void pixbuf_renderer_class_init(PixbufRendererClass *class);
193 static void pixbuf_renderer_init(PixbufRenderer *pr);
194 static void pixbuf_renderer_finalize(GObject *object);
195 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
196                                          const GValue *value, GParamSpec *pspec);
197 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
198                                          GValue *value, GParamSpec *pspec);
199 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event);
200
201 static void pr_render_complete_signal(PixbufRenderer *pr);
202
203 static void pr_overlay_list_clear(PixbufRenderer *pr);
204 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start);
205 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
206
207
208 static void pr_source_tile_free_all(PixbufRenderer *pr);
209 static void pr_tile_free_all(PixbufRenderer *pr);
210 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
211 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it);
212 static void pr_queue_clear(PixbufRenderer *pr);
213 static void pr_queue_merge(QueueData *parent, QueueData *qd);
214 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
215                      gint clamp, ImageTileRenderType render, gint new_data, gint only_existing);
216
217 static void pr_redraw(PixbufRenderer *pr, gint new_data);
218
219 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
220                          PrZoomFlags flags, gint px, gint py);
221
222 static void pr_signals_connect(PixbufRenderer *pr);
223 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data);
224 static void pr_unmap_cb(GtkWidget *widget, gpointer data);
225 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area);
226 static gint pr_queue_draw_idle_cb(gpointer data);
227
228
229 /*
230  *-------------------------------------------------------------------
231  * Pixbuf Renderer object
232  *-------------------------------------------------------------------
233  */
234
235 GType pixbuf_renderer_get_type(void)
236 {
237         static GType pixbuf_renderer_type = 0;
238
239         if (!pixbuf_renderer_type)
240                 {
241                 static const GTypeInfo pixbuf_renderer_info =
242                         {
243                         sizeof(PixbufRendererClass), /* class_size */
244                         NULL,           /* base_init */
245                         NULL,           /* base_finalize */
246                         (GClassInitFunc)pixbuf_renderer_class_init,
247                         NULL,           /* class_finalize */
248                         NULL,           /* class_data */
249                         sizeof(PixbufRenderer), /* instance_size */
250                         0,              /* n_preallocs */
251                         (GInstanceInitFunc)pixbuf_renderer_init, /* instance_init */
252                         NULL,           /* value_table */
253                         };
254
255                 pixbuf_renderer_type = g_type_register_static(GTK_TYPE_EVENT_BOX, "PixbufRenderer",
256                                                               &pixbuf_renderer_info, 0);
257                 }
258
259         return pixbuf_renderer_type;
260 }
261
262 static void pixbuf_renderer_class_init(PixbufRendererClass *class)
263 {
264         GObjectClass *gobject_class = G_OBJECT_CLASS(class);
265         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
266
267         parent_class = g_type_class_peek_parent(class);
268
269         gobject_class->set_property = pixbuf_renderer_set_property;
270         gobject_class->get_property = pixbuf_renderer_get_property;
271
272         gobject_class->finalize = pixbuf_renderer_finalize;
273
274         widget_class->expose_event = pixbuf_renderer_expose;
275
276         g_object_class_install_property(gobject_class,
277                                         PROP_ZOOM_MIN,
278                                         g_param_spec_double("zoom_min",
279                                                             "Zoom minimum",
280                                                             NULL,
281                                                             -1000.0,
282                                                             1000.0,
283                                                             PR_ZOOM_MIN,
284                                                             G_PARAM_READABLE | G_PARAM_WRITABLE));
285
286         g_object_class_install_property(gobject_class,
287                                         PROP_ZOOM_MAX,
288                                         g_param_spec_double("zoom_max",
289                                                             "Zoom maximum",
290                                                             NULL,
291                                                             -1000.0,
292                                                             1000.0,
293                                                             PR_ZOOM_MIN,
294                                                             G_PARAM_READABLE | G_PARAM_WRITABLE));
295
296         g_object_class_install_property(gobject_class,
297                                         PROP_ZOOM_QUALITY,
298                                         g_param_spec_uint("zoom_quality",
299                                                           "Zoom quality",
300                                                           NULL,
301                                                           GDK_INTERP_NEAREST,
302                                                           GDK_INTERP_HYPER,
303                                                           GDK_INTERP_BILINEAR,
304                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
305
306         g_object_class_install_property(gobject_class,
307                                         PROP_ZOOM_2PASS,
308                                         g_param_spec_boolean("zoom_2pass",
309                                                              "2 pass zoom",
310                                                              NULL,
311                                                              TRUE,
312                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
313
314         g_object_class_install_property(gobject_class,
315                                         PROP_ZOOM_EXPAND,
316                                         g_param_spec_boolean("zoom_expand",
317                                                              "Expand image in autozoom.",
318                                                              NULL,
319                                                              FALSE,
320                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
321
322         g_object_class_install_property(gobject_class,
323                                         PROP_DITHER_QUALITY,
324                                         g_param_spec_uint("dither_quality",
325                                                           "Dither quality",
326                                                           NULL,
327                                                           GDK_RGB_DITHER_NONE,
328                                                           GDK_RGB_DITHER_MAX,
329                                                           GDK_RGB_DITHER_NORMAL,
330                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
331
332         g_object_class_install_property(gobject_class,
333                                         PROP_SCROLL_RESET,
334                                         g_param_spec_uint("scroll_reset",
335                                                           "New image scroll reset",
336                                                           NULL,
337                                                           PR_SCROLL_RESET_TOPLEFT,
338                                                           PR_SCROLL_RESET_NOCHANGE,
339                                                           PR_SCROLL_RESET_TOPLEFT,
340                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
341
342         g_object_class_install_property(gobject_class,
343                                         PROP_DELAY_FLIP,
344                                         g_param_spec_boolean("delay_flip",
345                                                              "Delay image update",
346                                                              NULL,
347                                                              FALSE,
348                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
349
350         g_object_class_install_property(gobject_class,
351                                         PROP_LOADING,
352                                         g_param_spec_boolean("loading",
353                                                              "Image actively loading",
354                                                              NULL,
355                                                              FALSE,
356                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
357
358         g_object_class_install_property(gobject_class,
359                                         PROP_COMPLETE,
360                                         g_param_spec_boolean("complete",
361                                                              "Image rendering complete",
362                                                              NULL,
363                                                              FALSE,
364                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
365
366         g_object_class_install_property(gobject_class,
367                                         PROP_CACHE_SIZE_DISPLAY,
368                                         g_param_spec_uint("cache_display",
369                                                           "Display cache size MB",
370                                                           NULL,
371                                                           0,
372                                                           128,
373                                                           PR_CACHE_SIZE_DEFAULT,
374                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
375
376         g_object_class_install_property(gobject_class,
377                                         PROP_CACHE_SIZE_TILES,
378                                         g_param_spec_uint("cache_tiles",
379                                                           "Tile cache count",
380                                                           "Number of tiles to retain in memory at any one time.",
381                                                           0,
382                                                           256,
383                                                           PR_CACHE_SIZE_DEFAULT,
384                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
385
386         g_object_class_install_property(gobject_class,
387                                         PROP_WINDOW_FIT,
388                                         g_param_spec_boolean("window_fit",
389                                                              "Fit window to image size",
390                                                              NULL,
391                                                              FALSE,
392                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
393
394         g_object_class_install_property(gobject_class,
395                                         PROP_WINDOW_LIMIT,
396                                         g_param_spec_boolean("window_limit",
397                                                              "Limit size of parent window",
398                                                              NULL,
399                                                              FALSE,
400                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
401
402         g_object_class_install_property(gobject_class,
403                                         PROP_WINDOW_LIMIT_VALUE,
404                                         g_param_spec_uint("window_limit_value",
405                                                           "Size limit of parent window",
406                                                           NULL,
407                                                           10,
408                                                           150,
409                                                           100,
410                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
411
412         g_object_class_install_property(gobject_class,
413                                         PROP_AUTOFIT_LIMIT,
414                                         g_param_spec_boolean("autofit_limit",
415                                                              "Limit size of image when autofitting",
416                                                              NULL,
417                                                              FALSE,
418                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
419
420         g_object_class_install_property(gobject_class,
421                                         PROP_AUTOFIT_LIMIT_VALUE,
422                                         g_param_spec_uint("autofit_limit_value",
423                                                           "Size limit of image when autofitting",
424                                                           NULL,
425                                                           10,
426                                                           150,
427                                                           100,
428                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
429
430
431         signals[SIGNAL_ZOOM] =
432                 g_signal_new("zoom",
433                              G_OBJECT_CLASS_TYPE(gobject_class),
434                              G_SIGNAL_RUN_LAST,
435                              G_STRUCT_OFFSET(PixbufRendererClass, zoom),
436                              NULL, NULL,
437                              g_cclosure_marshal_VOID__DOUBLE,
438                              G_TYPE_NONE, 1,
439                              G_TYPE_DOUBLE);
440
441         signals[SIGNAL_CLICKED] =
442                 g_signal_new("clicked",
443                              G_OBJECT_CLASS_TYPE(gobject_class),
444                              G_SIGNAL_RUN_LAST,
445                              G_STRUCT_OFFSET(PixbufRendererClass, clicked),
446                              NULL, NULL,
447                              g_cclosure_marshal_VOID__BOXED,
448                              G_TYPE_NONE, 1,
449                              GDK_TYPE_EVENT);
450
451         signals[SIGNAL_SCROLL_NOTIFY] =
452                 g_signal_new("scroll-notify",
453                              G_OBJECT_CLASS_TYPE(gobject_class),
454                              G_SIGNAL_RUN_LAST,
455                              G_STRUCT_OFFSET(PixbufRendererClass, scroll_notify),
456                              NULL, NULL,
457                              g_cclosure_marshal_VOID__VOID,
458                              G_TYPE_NONE, 0);
459
460         signals[SIGNAL_RENDER_COMPLETE] =
461                 g_signal_new("render-complete",
462                              G_OBJECT_CLASS_TYPE(gobject_class),
463                              G_SIGNAL_RUN_LAST,
464                              G_STRUCT_OFFSET(PixbufRendererClass, render_complete),
465                              NULL, NULL,
466                              g_cclosure_marshal_VOID__VOID,
467                              G_TYPE_NONE, 0);
468
469         signals[SIGNAL_DRAG] =
470                 g_signal_new("drag",
471                              G_OBJECT_CLASS_TYPE(gobject_class),
472                              G_SIGNAL_RUN_LAST,
473                              G_STRUCT_OFFSET(PixbufRendererClass, drag),
474                              NULL, NULL,
475                              g_cclosure_marshal_VOID__BOXED,
476                              G_TYPE_NONE, 1,
477                              GDK_TYPE_EVENT);
478 }
479
480 static void pixbuf_renderer_init(PixbufRenderer *pr)
481 {
482         GtkWidget *box;
483
484         box = GTK_WIDGET(pr);
485
486         pr->zoom_min = PR_ZOOM_MIN;
487         pr->zoom_max = PR_ZOOM_MAX;
488         pr->zoom_quality = GDK_INTERP_BILINEAR;
489         pr->zoom_2pass = FALSE;
490
491         pr->zoom = 1.0;
492         pr->scale = 1.0;
493
494         pr->dither_quality = GDK_RGB_DITHER_NORMAL;
495
496         pr->scroll_reset = PR_SCROLL_RESET_TOPLEFT;
497
498         pr->draw_idle_id = -1;
499
500         pr->tile_width = PR_TILE_SIZE;
501         pr->tile_height = PR_TILE_SIZE;
502
503         pr->tiles = NULL;
504         pr->tile_cache_size = 0;
505
506         pr->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
507
508         pr->scroller_id = -1;
509         pr->scroller_overlay = -1;
510
511         pr->source_tiles_enabled = FALSE;
512         pr->source_tiles = NULL;
513
514         pr->orientation = 1;
515
516         pr->norm_center_x = 0.5;
517         pr->norm_center_y = 0.5;
518
519         gtk_widget_set_double_buffered(box, FALSE);
520         g_signal_connect_after(G_OBJECT(box), "size_allocate",
521                                G_CALLBACK(pr_size_cb), pr);
522
523         g_signal_connect(G_OBJECT(pr), "unmap",
524                          G_CALLBACK(pr_unmap_cb), pr);
525
526         pr_signals_connect(pr);
527 }
528
529 static void pixbuf_renderer_finalize(GObject *object)
530 {
531         PixbufRenderer *pr;
532
533         pr = PIXBUF_RENDERER(object);
534
535         pr_queue_clear(pr);
536         pr_tile_free_all(pr);
537
538
539         if (pr->pixbuf) g_object_unref(pr->pixbuf);
540         if (pr->spare_tile) g_object_unref(pr->spare_tile);
541
542         pr_scroller_timer_set(pr, FALSE);
543         pr_overlay_list_clear(pr);
544
545         pr_source_tile_free_all(pr);
546 }
547
548 PixbufRenderer *pixbuf_renderer_new(void)
549 {
550         return g_object_new(TYPE_PIXBUF_RENDERER, NULL);
551 }
552
553 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
554                                          const GValue *value, GParamSpec *pspec)
555 {
556         PixbufRenderer *pr;
557
558         pr = PIXBUF_RENDERER(object);
559
560         switch (prop_id)
561                 {
562                 case PROP_ZOOM_MIN:
563                         pr->zoom_min = g_value_get_double(value);
564                         break;
565                 case PROP_ZOOM_MAX:
566                         pr->zoom_max = g_value_get_double(value);
567                         break;
568                 case PROP_ZOOM_QUALITY:
569                         pr->zoom_quality = g_value_get_uint(value);
570                         break;
571                 case PROP_ZOOM_2PASS:
572                         pr->zoom_2pass = g_value_get_boolean(value);
573                         break;
574                 case PROP_ZOOM_EXPAND:
575                         pr->zoom_expand = g_value_get_boolean(value);
576                         break;
577                 case PROP_DITHER_QUALITY:
578                         pr->dither_quality = g_value_get_uint(value);
579                         break;
580                 case PROP_SCROLL_RESET:
581                         pr->scroll_reset = g_value_get_uint(value);
582                         break;
583                 case PROP_DELAY_FLIP:
584                         pr->delay_flip = g_value_get_boolean(value);
585                         break;
586                 case PROP_LOADING:
587                         pr->loading = g_value_get_boolean(value);
588                         break;
589                 case PROP_COMPLETE:
590                         pr->complete = g_value_get_boolean(value);
591                         break;
592                 case PROP_CACHE_SIZE_DISPLAY:
593                         pr->tile_cache_max = g_value_get_uint(value);
594                         break;
595                 case PROP_CACHE_SIZE_TILES:
596                         pr->source_tiles_cache_size = g_value_get_uint(value);
597                         break;
598                 case PROP_WINDOW_FIT:
599                         pr->window_fit = g_value_get_boolean(value);
600                         break;
601                 case PROP_WINDOW_LIMIT:
602                         pr->window_limit = g_value_get_boolean(value);
603                         break;
604                 case PROP_WINDOW_LIMIT_VALUE:
605                         pr->window_limit_size = g_value_get_uint(value);
606                         break;
607                 case PROP_AUTOFIT_LIMIT:
608                         pr->autofit_limit = g_value_get_boolean(value);
609                         break;
610                 case PROP_AUTOFIT_LIMIT_VALUE:
611                         pr->autofit_limit_size = g_value_get_uint(value);
612                         break;
613                 default:
614                         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
615                         break;
616                 }
617 }
618
619 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
620                                          GValue *value, GParamSpec *pspec)
621 {
622         PixbufRenderer *pr;
623
624         pr = PIXBUF_RENDERER(object);
625
626         switch (prop_id)
627                 {
628                 case PROP_ZOOM_MIN:
629                         g_value_set_double(value, pr->zoom_min);
630                         break;
631                 case PROP_ZOOM_MAX:
632                         g_value_set_double(value, pr->zoom_max);
633                         break;
634                 case PROP_ZOOM_QUALITY:
635                         g_value_set_uint(value, pr->zoom_quality);
636                         break;
637                 case PROP_ZOOM_2PASS:
638                         g_value_set_boolean(value, pr->zoom_2pass);
639                         break;
640                 case PROP_ZOOM_EXPAND:
641                         g_value_set_boolean(value, pr->zoom_expand);
642                         break;
643                 case PROP_DITHER_QUALITY:
644                         g_value_set_uint(value, pr->dither_quality);
645                         break;
646                 case PROP_SCROLL_RESET:
647                         g_value_set_uint(value, pr->scroll_reset);
648                         break;
649                 case PROP_DELAY_FLIP:
650                         g_value_set_boolean(value, pr->delay_flip);
651                         break;
652                 case PROP_LOADING:
653                         g_value_set_boolean(value, pr->loading);
654                         break;
655                 case PROP_COMPLETE:
656                         g_value_set_boolean(value, pr->complete);
657                         break;
658                 case PROP_CACHE_SIZE_DISPLAY:
659                         g_value_set_uint(value, pr->tile_cache_max);
660                         break;
661                 case PROP_CACHE_SIZE_TILES:
662                         g_value_set_uint(value, pr->source_tiles_cache_size);
663                         break;
664                 case PROP_WINDOW_FIT:
665                         g_value_set_boolean(value, pr->window_fit);
666                         break;
667                 case PROP_WINDOW_LIMIT:
668                         g_value_set_boolean(value, pr->window_limit);
669                         break;
670                 case PROP_WINDOW_LIMIT_VALUE:
671                         g_value_set_uint(value, pr->window_limit_size);
672                         break;
673                 case PROP_AUTOFIT_LIMIT:
674                         g_value_set_boolean(value, pr->autofit_limit);
675                         break;
676                 case PROP_AUTOFIT_LIMIT_VALUE:
677                         g_value_set_uint(value, pr->autofit_limit_size);
678                         break;
679                 default:
680                         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
681                         break;
682                 }
683 }
684
685 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event)
686 {
687         if (GTK_WIDGET_DRAWABLE(widget))
688                 {
689                 if (!GTK_WIDGET_NO_WINDOW(widget))
690                         {
691                         if (event->window != widget->window)
692                                 {
693                                 GdkRectangle area;
694
695                                 gdk_window_get_position(event->window, &area.x, &area.y);
696
697                                 area.x += event->area.x;
698                                 area.y += event->area.y;
699                                 area.width = event->area.width;
700                                 area.height = event->area.height;
701                                 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &area);
702                                 }
703                         else
704                                 {
705                                 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &event->area);
706                                 }
707                         }
708                 }
709
710         return FALSE;
711 }
712
713 /*
714  *-------------------------------------------------------------------
715  * misc utilities
716  *-------------------------------------------------------------------
717  */
718
719 static void widget_set_cursor(GtkWidget *widget, gint icon)
720 {
721         GdkCursor *cursor;
722
723         if (!widget->window) return;
724
725         if (icon == -1)
726                 {
727                 cursor = NULL;
728                 }
729         else
730                 {
731                 cursor = gdk_cursor_new(icon);
732                 }
733
734         gdk_window_set_cursor(widget->window, cursor);
735
736         if (cursor) gdk_cursor_unref(cursor);
737 }
738
739 static gint pixmap_calc_size(GdkPixmap *pixmap)
740 {
741         gint w, h, d;
742
743         d = gdk_drawable_get_depth(pixmap);
744         gdk_drawable_get_size(pixmap, &w, &h);
745         return w * h * (d / 8);
746 }
747
748 static gint pr_clip_region(gint x, gint y, gint w, gint h,
749                            gint clip_x, gint clip_y, gint clip_w, gint clip_h,
750                            gint *rx, gint *ry, gint *rw, gint *rh)
751 {
752         if (clip_x + clip_w <= x ||
753             clip_x >= x + w ||
754             clip_y + clip_h <= y ||
755             clip_y >= y + h)
756                 {
757                 return FALSE;
758                 }
759
760         *rx = MAX(x, clip_x);
761         *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
762
763         *ry = MAX(y, clip_y);
764         *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
765
766         return TRUE;
767 }
768
769 static gint pr_parent_window_sizable(PixbufRenderer *pr)
770 {
771         GdkWindowState state;
772
773         if (!pr->parent_window) return FALSE;
774         if (!pr->window_fit) return FALSE;
775         if (!GTK_WIDGET(pr)->window) return FALSE;
776
777         if (!pr->parent_window->window) return FALSE;
778         state = gdk_window_get_state(pr->parent_window->window);
779         if (state & GDK_WINDOW_STATE_MAXIMIZED) return FALSE;
780
781         return TRUE;
782 }
783
784 static gint pr_parent_window_resize(PixbufRenderer *pr, gint w, gint h)
785 {
786         GtkWidget *widget;
787         GtkWidget *parent;
788         gint ww, wh;
789
790         if (!pr_parent_window_sizable(pr)) return FALSE;
791
792         if (pr->window_limit)
793                 {
794                 gint sw = gdk_screen_width() * pr->window_limit_size / 100;
795                 gint sh = gdk_screen_height() * pr->window_limit_size / 100;
796
797                 if (w > sw) w = sw;
798                 if (h > sh) h = sh;
799                 }
800
801         widget = GTK_WIDGET(pr);
802         parent = GTK_WIDGET(pr->parent_window);
803
804         w += (parent->allocation.width - widget->allocation.width);
805         h += (parent->allocation.height - widget->allocation.height);
806
807         gdk_drawable_get_size(parent->window, &ww, &wh);
808         if (w == ww && h == wh) return FALSE;
809
810         gdk_window_resize(parent->window, w, h);
811
812         return TRUE;
813 }
814
815 void pixbuf_renderer_set_parent(PixbufRenderer *pr, GtkWindow *window)
816 {
817         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
818         g_return_if_fail(window == NULL || GTK_IS_WINDOW(window));
819
820         pr->parent_window = GTK_WIDGET(window);
821 }
822
823 GtkWindow *pixbuf_renderer_get_parent(PixbufRenderer *pr)
824 {
825         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
826
827         return GTK_WINDOW(pr->parent_window);
828 }
829
830
831 /*
832  *-------------------------------------------------------------------
833  * overlays
834  *-------------------------------------------------------------------
835  */
836
837 static void pr_overlay_get_position(PixbufRenderer *pr, OverlayData *od,
838                                     gint *x, gint *y, gint *w, gint *h)
839 {
840         gint px, py, pw, ph;
841
842         pw = gdk_pixbuf_get_width(od->pixbuf);
843         ph = gdk_pixbuf_get_height(od->pixbuf);
844         px = od->x;
845         py = od->y;
846
847         if (od->flags & OVL_RELATIVE)
848                 {
849                 if (px < 0) px = pr->window_width - pw + px;
850                 if (py < 0) py = pr->window_height - ph + py;
851                 }
852
853         if (x) *x = px;
854         if (y) *y = py;
855         if (w) *w = pw;
856         if (h) *h = ph;
857 }
858
859 static void pr_overlay_init_window(PixbufRenderer *pr, OverlayData *od)
860 {
861         gint px, py, pw, ph;
862         GdkWindowAttr attributes;
863         gint attributes_mask;
864
865         pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
866
867         attributes.window_type = GDK_WINDOW_CHILD;
868         attributes.wclass = GDK_INPUT_OUTPUT;
869         attributes.width = pw;
870         attributes.height = ph;
871         attributes.event_mask = GDK_EXPOSURE_MASK;
872         attributes_mask = 0;
873
874         od->window = gdk_window_new(GTK_WIDGET(pr)->window, &attributes, attributes_mask);
875         gdk_window_set_user_data(od->window, pr);
876         gdk_window_move(od->window, px, py);
877         gdk_window_show(od->window);
878 }
879
880 static void pr_overlay_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
881                             ImageTile *it)
882 {
883         GtkWidget *box;
884         GList *work;
885
886         box = GTK_WIDGET(pr);
887
888         work = pr->overlay_list;
889         while (work)
890                 {
891                 OverlayData *od;
892                 gint px, py, pw, ph;
893                 gint rx, ry, rw, rh;
894
895                 od = work->data;
896                 work = work->next;
897
898                 if (!od->window) pr_overlay_init_window(pr, od);
899                 
900                 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
901                 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
902                         {
903                         if (!pr->overlay_buffer)
904                                 {
905                                 pr->overlay_buffer = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
906                                 }
907
908                         if (it)
909                                 {
910                                 gdk_draw_drawable(pr->overlay_buffer, box->style->fg_gc[GTK_WIDGET_STATE(box)],
911                                                   it->pixmap,
912                                                   rx - (pr->x_offset + (it->x - pr->x_scroll)),
913                                                   ry - (pr->y_offset + (it->y - pr->y_scroll)),
914                                                   0, 0, rw, rh);
915                                 gdk_draw_pixbuf(pr->overlay_buffer,
916                                                 box->style->fg_gc[GTK_WIDGET_STATE(box)],
917                                                 od->pixbuf,
918                                                 rx - px, ry - py,
919                                                 0, 0, rw, rh,
920                                                 pr->dither_quality, rx, ry);
921                                 gdk_draw_drawable(od->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
922                                                   pr->overlay_buffer,
923                                                   0, 0,
924                                                   rx - px, ry - py, rw, rh);
925                                 }
926                         else
927                                 {
928                                 /* no ImageTile means region may be larger than our scratch buffer */
929                                 gint sx, sy;
930
931                                 for (sx = rx; sx < rx + rw; sx += pr->tile_width)
932                                     for (sy = ry; sy < ry + rh; sy += pr->tile_height)
933                                         {
934                                         gint sw, sh;
935
936                                         sw = MIN(rx + rw - sx, pr->tile_width);
937                                         sh = MIN(ry + rh - sy, pr->tile_height);
938
939                                         gdk_draw_rectangle(pr->overlay_buffer,
940                                                            box->style->bg_gc[GTK_WIDGET_STATE(box)], TRUE,
941                                                            0, 0, sw, sh);
942                                         gdk_draw_pixbuf(pr->overlay_buffer,
943                                                         box->style->fg_gc[GTK_WIDGET_STATE(box)],
944                                                         od->pixbuf,
945                                                         sx - px, sy - py,
946                                                         0, 0, sw, sh,
947                                                         pr->dither_quality, sx, sy);
948                                         gdk_draw_drawable(od->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
949                                                           pr->overlay_buffer,
950                                                           0, 0,
951                                                           sx - px, sy - py, sw, sh);
952                                         }
953                                 }
954                         }
955                 }
956 }
957
958 static void pr_overlay_queue_draw(PixbufRenderer *pr, OverlayData *od)
959 {
960         gint x, y, w, h;
961
962         pr_overlay_get_position(pr, od, &x, &y, &w, &h);
963         pr_queue(pr, pr->x_scroll - pr->x_offset + x,
964                  pr->y_scroll - pr->y_offset + y,
965                  w, h,
966                  FALSE, TILE_RENDER_ALL, FALSE, FALSE);
967
968         pr_border_draw(pr, x, y, w, h);
969 }
970
971 static void pr_overlay_queue_all(PixbufRenderer *pr)
972 {
973         GList *work;
974
975         work = pr->overlay_list;
976         while (work)
977                 {
978                 OverlayData *od = work->data;
979                 work = work->next;
980
981                 pr_overlay_queue_draw(pr, od);
982                 }
983 }
984
985 static void pr_overlay_update_sizes(PixbufRenderer *pr)
986 {
987         GList *work;
988
989         work = pr->overlay_list;
990         while (work)
991                 {
992                 OverlayData *od = work->data;
993                 work = work->next;
994                 
995                 if (!od->window) pr_overlay_init_window(pr, od);
996                 
997                 if (od->flags & OVL_RELATIVE)
998                         {
999                         gint x, y, w, h;
1000
1001                         pr_overlay_get_position(pr, od, &x, &y, &w, &h);
1002                         gdk_window_move_resize(od->window, x, y, w, h);
1003                         }
1004                 }
1005 }
1006
1007 static OverlayData *pr_overlay_find(PixbufRenderer *pr, gint id)
1008 {
1009         GList *work;
1010
1011         work = pr->overlay_list;
1012         while (work)
1013                 {
1014                 OverlayData *od = work->data;
1015                 work = work->next;
1016
1017                 if (od->id == id) return od;
1018                 }
1019
1020         return NULL;
1021 }
1022
1023
1024 gint pixbuf_renderer_overlay_add(PixbufRenderer *pr, GdkPixbuf *pixbuf, gint x, gint y,
1025                                  OverlayRendererFlags flags)
1026 {
1027         OverlayData *od;
1028         gint id;
1029
1030         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
1031         g_return_val_if_fail(pixbuf != NULL, -1);
1032
1033         id = 1;
1034         while (pr_overlay_find(pr, id)) id++;
1035
1036         od = g_new0(OverlayData, 1);
1037         od->id = id;
1038         od->pixbuf = pixbuf;
1039         g_object_ref(G_OBJECT(od->pixbuf));
1040         od->x = x;
1041         od->y = y;
1042         od->flags = flags;
1043
1044         pr_overlay_init_window(pr, od);
1045         
1046         pr->overlay_list = g_list_append(pr->overlay_list, od);
1047
1048         pr_overlay_queue_draw(pr, od);
1049
1050         return od->id;
1051 }
1052
1053 static void pr_overlay_free(PixbufRenderer *pr, OverlayData *od)
1054 {
1055         pr->overlay_list = g_list_remove(pr->overlay_list, od);
1056
1057         if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
1058         if (od->window) gdk_window_destroy(od->window);
1059         g_free(od);
1060
1061         if (!pr->overlay_list && pr->overlay_buffer)
1062                 {
1063                 g_object_unref(pr->overlay_buffer);
1064                 pr->overlay_buffer = NULL;
1065                 }
1066 }
1067
1068 static void pr_overlay_list_clear(PixbufRenderer *pr)
1069 {
1070         while (pr->overlay_list)
1071                 {
1072                 OverlayData *od;
1073
1074                 od = pr->overlay_list->data;
1075                 pr_overlay_free(pr, od);
1076                 }
1077 }
1078
1079 static void pr_overlay_list_reset_window(PixbufRenderer *pr)
1080 {
1081         GList *work;
1082
1083         if (pr->overlay_buffer) g_object_unref(pr->overlay_buffer);
1084         pr->overlay_buffer = NULL;
1085
1086         work = pr->overlay_list;
1087         while (work)
1088                 {
1089                 OverlayData *od = work->data;
1090                 work = work->next;
1091                 if (od->window) gdk_window_destroy(od->window);
1092                 od->window = NULL;
1093                 }
1094 }
1095
1096 void pixbuf_renderer_overlay_set(PixbufRenderer *pr, gint id, GdkPixbuf *pixbuf, gint x, gint y)
1097 {
1098         OverlayData *od;
1099
1100         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1101
1102         od = pr_overlay_find(pr, id);
1103         if (!od) return;
1104
1105         if (pixbuf)
1106                 {
1107                 gint px, py, pw, ph;
1108
1109                 g_object_ref(G_OBJECT(pixbuf));
1110                 g_object_unref(G_OBJECT(od->pixbuf));
1111                 od->pixbuf = pixbuf;
1112
1113                 od->x = x;
1114                 od->y = y;
1115
1116                 pr_overlay_queue_draw(pr, od);
1117                 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
1118                 gdk_window_move_resize(od->window, px, py, pw, ph);
1119                 }
1120         else
1121                 {
1122                 pr_overlay_queue_draw(pr, od);
1123                 pr_overlay_free(pr, od);
1124                 }
1125 }
1126
1127 gint pixbuf_renderer_overlay_get(PixbufRenderer *pr, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
1128 {
1129         OverlayData *od;
1130
1131         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1132
1133         od = pr_overlay_find(pr, id);
1134         if (!od) return FALSE;
1135
1136         if (pixbuf) *pixbuf = od->pixbuf;
1137         if (x) *x = od->x;
1138         if (y) *y = od->y;
1139
1140         return TRUE;
1141 }
1142
1143 void pixbuf_renderer_overlay_remove(PixbufRenderer *pr, gint id)
1144 {
1145         pixbuf_renderer_overlay_set(pr, id, NULL, 0, 0);
1146 }
1147
1148 static void pr_unmap_cb(GtkWidget *widget, gpointer data)
1149 {
1150         PixbufRenderer *pr = data;
1151         pr_overlay_list_reset_window(pr);
1152 }
1153
1154
1155 /*
1156  *-------------------------------------------------------------------
1157  * scroller overlay
1158  *-------------------------------------------------------------------
1159  */
1160
1161
1162 static gboolean pr_scroller_update_cb(gpointer data)
1163 {
1164         PixbufRenderer *pr = data;
1165         gint x, y;
1166         gint xinc, yinc;
1167
1168         /* this was a simple scroll by difference between scroller and mouse position,
1169          * but all this math results in a smoother result and accounts for a dead zone.
1170          */
1171
1172         if (abs(pr->scroller_xpos - pr->scroller_x) < PR_SCROLLER_DEAD_ZONE)
1173                 {
1174                 x = 0;
1175                 }
1176         else
1177                 {
1178                 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1179                 x = (pr->scroller_xpos - pr->scroller_x) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1180                 x += (x > 0) ? -shift : shift;
1181                 }
1182
1183         if (abs(pr->scroller_ypos - pr->scroller_y) < PR_SCROLLER_DEAD_ZONE)
1184                 {
1185                 y = 0;
1186                 }
1187         else
1188                 {
1189                 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1190                 y = (pr->scroller_ypos - pr->scroller_y) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1191                 y += (y > 0) ? -shift : shift;
1192                 }
1193
1194         if (abs(x) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
1195                 {
1196                 xinc = x;
1197                 }
1198         else
1199                 {
1200                 xinc = pr->scroller_xinc;
1201
1202                 if (x >= 0)
1203                         {
1204                         if (xinc < 0) xinc = 0;
1205                         if (x < xinc) xinc = x;
1206                         if (x > xinc) xinc = MIN(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
1207                         }
1208                 else
1209                         {
1210                         if (xinc > 0) xinc = 0;
1211                         if (x > xinc) xinc = x;
1212                         if (x < xinc) xinc = MAX(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
1213                         }
1214                 }
1215
1216         if (abs(y) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
1217                 {
1218                 yinc = y;
1219                 }
1220         else
1221                 {
1222                 yinc = pr->scroller_yinc;
1223
1224                 if (y >= 0)
1225                         {
1226                         if (yinc < 0) yinc = 0;
1227                         if (y < yinc) yinc = y;
1228                         if (y > yinc) yinc = MIN(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1229                         }
1230                 else
1231                         {
1232                         if (yinc > 0) yinc = 0;
1233                         if (y > yinc) yinc = y;
1234                         if (y < yinc) yinc = MAX(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1235                         }
1236                 }
1237
1238         pr->scroller_xinc = xinc;
1239         pr->scroller_yinc = yinc;
1240
1241         xinc = xinc / PR_SCROLLER_UPDATES_PER_SEC;
1242         yinc = yinc / PR_SCROLLER_UPDATES_PER_SEC;
1243
1244         pixbuf_renderer_scroll(pr, xinc, yinc);
1245
1246         return TRUE;
1247 }
1248
1249 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start)
1250 {
1251         if (pr->scroller_id != -1)
1252                 {
1253                 g_source_remove(pr->scroller_id);
1254                 pr->scroller_id = -1;
1255                 }
1256
1257         if (start)
1258                 {
1259                 pr->scroller_id = g_timeout_add(1000 / PR_SCROLLER_UPDATES_PER_SEC,
1260                                                 pr_scroller_update_cb, pr);
1261                 }
1262 }
1263
1264 static void pr_scroller_start(PixbufRenderer *pr, gint x, gint y)
1265 {
1266         if (pr->scroller_overlay == -1)
1267                 {
1268                 GdkPixbuf *pixbuf;
1269                 gint w, h;
1270
1271 #ifdef GQ_BUILD
1272                 pixbuf = pixbuf_inline(PIXBUF_INLINE_SCROLLER);
1273 #else
1274                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
1275                 gdk_pixbuf_fill(pixbuf, 0x000000ff);
1276 #endif
1277                 w = gdk_pixbuf_get_width(pixbuf);
1278                 h = gdk_pixbuf_get_height(pixbuf);
1279
1280                 pr->scroller_overlay = pixbuf_renderer_overlay_add(pr, pixbuf, x - w / 2, y - h / 2, OVL_NORMAL);
1281                 g_object_unref(pixbuf);
1282                 }
1283
1284         pr->scroller_x = x;
1285         pr->scroller_y = y;
1286         pr->scroller_xpos = x;
1287         pr->scroller_ypos = y;
1288
1289         pr_scroller_timer_set(pr, TRUE);
1290 }
1291
1292 static void pr_scroller_stop(PixbufRenderer *pr)
1293 {
1294         if (pr->scroller_id == -1) return;
1295
1296         pixbuf_renderer_overlay_remove(pr, pr->scroller_overlay);
1297         pr->scroller_overlay = -1;
1298
1299         pr_scroller_timer_set(pr, FALSE);
1300 }
1301
1302 /*
1303  *-------------------------------------------------------------------
1304  * borders
1305  *-------------------------------------------------------------------
1306  */
1307
1308 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1309 {
1310         GtkWidget *box;
1311         gint rx, ry, rw, rh;
1312
1313         box = GTK_WIDGET(pr);
1314
1315         if (!box->window) return;
1316
1317         if (!pr->pixbuf && !pr->source_tiles_enabled)
1318                 {
1319                 if (pr_clip_region(x, y, w, h,
1320                                    0, 0,
1321                                    pr->window_width, pr->window_height,
1322                                    &rx, &ry, &rw, &rh))
1323                         {
1324                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1325                         pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1326                         }
1327                 return;
1328                 }
1329
1330         if (pr->vis_width < pr->window_width)
1331                 {
1332                 if (pr->x_offset > 0 &&
1333                     pr_clip_region(x, y, w, h,
1334                                    0, 0,
1335                                    pr->x_offset, pr->window_height,
1336                                    &rx, &ry, &rw, &rh))
1337                         {
1338                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1339                         pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1340                         }
1341                 if (pr->window_width - pr->vis_width - pr->x_offset > 0 &&
1342                     pr_clip_region(x, y, w, h,
1343                                    pr->x_offset + pr->vis_width, 0,
1344                                    pr->window_width - pr->vis_width - pr->x_offset, pr->window_height,
1345                                    &rx, &ry, &rw, &rh))
1346                         {
1347                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1348                         pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1349                         }
1350                 }
1351         if (pr->vis_height < pr->window_height)
1352                 {
1353                 if (pr->y_offset > 0 &&
1354                     pr_clip_region(x, y, w, h,
1355                                    pr->x_offset, 0,
1356                                    pr->vis_width, pr->y_offset,
1357                                    &rx, &ry, &rw, &rh))
1358                         {
1359                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1360                         pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1361                         }
1362                 if (pr->window_height - pr->vis_height - pr->y_offset > 0 &&
1363                     pr_clip_region(x, y, w, h,
1364                                    pr->x_offset, pr->y_offset + pr->vis_height,
1365                                    pr->vis_width, pr->window_height - pr->vis_height - pr->y_offset,
1366                                    &rx, &ry, &rw, &rh))
1367                         {
1368                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1369                         pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1370                         }
1371                 }
1372 }
1373
1374 static void pr_border_clear(PixbufRenderer *pr)
1375 {
1376         pr_border_draw(pr, 0, 0, pr->window_width, pr->window_height);
1377 }
1378
1379 void pixbuf_renderer_set_color(PixbufRenderer *pr, GdkColor *color)
1380 {
1381         GtkStyle *style;
1382         GtkWidget *widget;
1383
1384         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1385
1386         widget = GTK_WIDGET(pr);
1387
1388         if (color) {
1389                 GdkColor *slot;
1390
1391                 style = gtk_style_copy(gtk_widget_get_style(widget));
1392                 slot = &style->bg[GTK_STATE_NORMAL];
1393
1394                 slot->red = color->red;
1395                 slot->green = color->green;
1396                 slot->blue = color->blue;
1397                 }
1398         else {
1399                 style = gtk_style_copy(gtk_widget_get_default_style());
1400         }
1401
1402         gtk_widget_set_style(widget, style);
1403
1404         if (GTK_WIDGET_VISIBLE(widget)) pr_border_clear(pr);
1405 }
1406
1407
1408 /*
1409  *-------------------------------------------------------------------
1410  * source tiles
1411  *-------------------------------------------------------------------
1412  */
1413
1414 static void pr_source_tile_free(SourceTile *st)
1415 {
1416         if (!st) return;
1417
1418         if (st->pixbuf) g_object_unref(st->pixbuf);
1419         g_free(st);
1420 }
1421
1422 static void pr_source_tile_free_all(PixbufRenderer *pr)
1423 {
1424         GList *work;
1425
1426         work = pr->source_tiles;
1427         while (work)
1428                 {
1429                 SourceTile *st;
1430
1431                 st = work->data;
1432                 work = work->next;
1433
1434                 pr_source_tile_free(st);
1435                 }
1436
1437         g_list_free(pr->source_tiles);
1438         pr->source_tiles = NULL;
1439 }
1440
1441 static void pr_source_tile_unset(PixbufRenderer *pr)
1442 {
1443         pr_source_tile_free_all(pr);
1444         pr->source_tiles_enabled = FALSE;
1445 }
1446
1447 static gint pr_source_tile_visible(PixbufRenderer *pr, SourceTile *st)
1448 {
1449         gint x1, y1, x2, y2;
1450
1451         if (!st) return FALSE;
1452
1453         x1 = (pr->x_scroll / pr->tile_width) * pr->tile_width;
1454         y1 = (pr->y_scroll / pr->tile_height) * pr->tile_height;
1455         x2 = ((pr->x_scroll + pr->vis_width) / pr->tile_width) * pr->tile_width + pr->tile_width;
1456         y2 = ((pr->y_scroll + pr->vis_height) / pr->tile_height) * pr->tile_height + pr->tile_height;
1457
1458         return !((gdouble)st->x * pr->scale > (gdouble)x2 ||
1459                  (gdouble)(st->x + pr->source_tile_width) * pr->scale < (gdouble)x1 ||
1460                  (gdouble)st->y * pr->scale > (gdouble)y2 ||
1461                  (gdouble)(st->y + pr->source_tile_height) * pr->scale < (gdouble)y1);
1462 }
1463
1464 static SourceTile *pr_source_tile_new(PixbufRenderer *pr, gint x, gint y)
1465 {
1466         SourceTile *st = NULL;
1467         gint count;
1468
1469         g_return_val_if_fail(pr->source_tile_width >= 1 && pr->source_tile_height >= 1, NULL);
1470
1471         if (pr->source_tiles_cache_size < 4) pr->source_tiles_cache_size = 4;
1472
1473         count = g_list_length(pr->source_tiles);
1474         if (count >= pr->source_tiles_cache_size)
1475                 {
1476                 GList *work;
1477
1478                 work = g_list_last(pr->source_tiles);
1479                 while (work && count >= pr->source_tiles_cache_size)
1480                         {
1481                         SourceTile *needle;
1482
1483                         needle = work->data;
1484                         work = work->prev;
1485
1486                         if (!pr_source_tile_visible(pr, needle))
1487                                 {
1488                                 pr->source_tiles = g_list_remove(pr->source_tiles, needle);
1489
1490                                 if (pr->func_tile_dispose)
1491                                         {
1492                                         pr->func_tile_dispose(pr, needle->x, needle->y,
1493                                                               pr->source_tile_width, pr->source_tile_height,
1494                                                               needle->pixbuf, pr->func_tile_data);
1495                                         }
1496
1497                                 if (!st)
1498                                         {
1499                                         st = needle;
1500                                         }
1501                                 else
1502                                         {
1503                                         pr_source_tile_free(needle);
1504                                         }
1505
1506                                 count--;
1507                                 }
1508                         }
1509                 }
1510
1511         if (!st)
1512                 {
1513                 st = g_new0(SourceTile, 1);
1514                 st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
1515                                             pr->source_tile_width, pr->source_tile_height);
1516                 }
1517
1518         st->x = (x / pr->source_tile_width) * pr->source_tile_width;
1519         st->y = (y / pr->source_tile_height) * pr->source_tile_height;
1520         st->blank = TRUE;
1521
1522         pr->source_tiles = g_list_prepend(pr->source_tiles, st);
1523
1524         return st;
1525 }
1526
1527 static SourceTile *pr_source_tile_request(PixbufRenderer *pr, gint x, gint y)
1528 {
1529         SourceTile *st;
1530
1531         st = pr_source_tile_new(pr, x, y);
1532         if (!st) return NULL;
1533
1534         if (pr->func_tile_request &&
1535             pr->func_tile_request(pr, st->x, st->y,
1536                                    pr->source_tile_width, pr->source_tile_height, st->pixbuf, pr->func_tile_data))
1537                 {
1538                 st->blank = FALSE;
1539                 }
1540
1541         pr_tile_invalidate_region(pr, st->x * pr->scale, st->y * pr->scale,
1542                                   pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1543
1544         return st;
1545 }
1546
1547 static SourceTile *pr_source_tile_find(PixbufRenderer *pr, gint x, gint y)
1548 {
1549         GList *work;
1550
1551         work = pr->source_tiles;
1552         while (work)
1553                 {
1554                 SourceTile *st = work->data;
1555
1556                 if (x >= st->x && x < st->x + pr->source_tile_width &&
1557                     y >= st->y && y < st->y + pr->source_tile_height)
1558                         {
1559                         if (work != pr->source_tiles)
1560                                 {
1561                                 pr->source_tiles = g_list_remove_link(pr->source_tiles, work);
1562                                 pr->source_tiles = g_list_concat(work, pr->source_tiles);
1563                                 }
1564                         return st;
1565                         }
1566
1567                 work = work->next;
1568                 }
1569
1570         return NULL;
1571 }
1572
1573 static GList *pr_source_tile_compute_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h, gint request)
1574 {
1575         gint x1, y1;
1576         GList *list = NULL;
1577         gint sx, sy;
1578
1579         if (x < 0) x = 0;
1580         if (y < 0) y = 0;
1581         if (w > pr->image_width) w = pr->image_width;
1582         if (h > pr->image_height) h = pr->image_height;
1583
1584         sx = (x / pr->source_tile_width) * pr->source_tile_width;
1585         sy = (y / pr->source_tile_height) * pr->source_tile_height;
1586
1587         for (x1 = sx; x1 < x + w; x1+= pr->source_tile_width)
1588                 {
1589                 for (y1 = sy; y1 < y + h; y1 += pr->source_tile_height)
1590                         {
1591                         SourceTile *st;
1592
1593                         st = pr_source_tile_find(pr, x1, y1);
1594                         if (!st && request) st = pr_source_tile_request(pr, x1, y1);
1595
1596                         if (st) list = g_list_prepend(list, st);
1597                         }
1598                 }
1599
1600         return g_list_reverse(list);
1601 }
1602
1603 static void pr_source_tile_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
1604 {
1605         GList *work;
1606
1607         if (width < 1 || height < 1) return;
1608
1609         work = pr->source_tiles;
1610         while (work)
1611                 {
1612                 SourceTile *st;
1613                 gint rx, ry, rw, rh;
1614
1615                 st = work->data;
1616                 work = work->next;
1617
1618                 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1619                                    x, y, width, height,
1620                                    &rx, &ry, &rw, &rh))
1621                         {
1622                         GdkPixbuf *pixbuf;
1623
1624                         pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh);
1625                         if (pr->func_tile_request &&
1626                             pr->func_tile_request(pr, rx, ry, rw, rh, pixbuf, pr->func_tile_data))
1627                                 {
1628                                 pr_tile_invalidate_region(pr, rx * pr->scale, ry * pr->scale,
1629                                                               rw * pr->scale, rh * pr->scale);
1630                                 }
1631                         g_object_unref(pixbuf);
1632                         }
1633                 }
1634 }
1635
1636 static gint pr_source_tile_render(PixbufRenderer *pr, ImageTile *it,
1637                                   gint x, gint y, gint w, gint h,
1638                                   gint new_data, gint fast)
1639 {
1640         GtkWidget *box;
1641         GList *list;
1642         GList *work;
1643         gint draw = FALSE;
1644
1645         box = GTK_WIDGET(pr);
1646
1647         if (pr->zoom == 1.0 || pr->scale == 1.0)
1648                 {
1649                 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1650                 work = list;
1651                 while (work)
1652                         {
1653                         SourceTile *st;
1654                         gint rx, ry, rw, rh;
1655
1656                         st = work->data;
1657                         work = work->next;
1658
1659                         if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1660                                            it->x + x, it->y + y, w, h,
1661                                            &rx, &ry, &rw, &rh))
1662                                 {
1663                                 if (st->blank)
1664                                         {
1665                                         gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1666                                                            rx - st->x, ry - st->y, rw, rh);
1667                                         }
1668                                 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1669                                         {
1670                                         gdk_draw_pixbuf(it->pixmap,
1671                                                         box->style->fg_gc[GTK_WIDGET_STATE(box)],
1672                                                         st->pixbuf,
1673                                                         rx - st->x, ry - st->y,
1674                                                         rx - it->x, ry - it->y,
1675                                                         rw, rh,
1676                                                         pr->dither_quality, rx, ry);
1677                                         }
1678                                 }
1679                         }
1680                 }
1681         else
1682                 {
1683                 gdouble scale_x, scale_y;
1684                 gint sx, sy, sw, sh;
1685
1686                 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1687                 scale_x = (gdouble)pr->width / pr->image_width;
1688                 scale_y = (gdouble)pr->height / pr->image_height;
1689
1690                 sx = (gdouble)(it->x + x) / scale_x;
1691                 sy = (gdouble)(it->y + y) / scale_y;
1692                 sw = (gdouble)w / scale_x;
1693                 sh = (gdouble)h / scale_y;
1694
1695                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1696
1697 #if 0
1698                 /* draws red over draw region, to check for leaks (regions not filled) */
1699                 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1700 #endif
1701
1702                 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1703                 work = list;
1704                 while (work)
1705                         {
1706                         SourceTile *st;
1707                         gint rx, ry, rw, rh;
1708                         gint stx, sty, stw, sth;
1709
1710                         st = work->data;
1711                         work = work->next;
1712
1713                         stx = floor((gdouble)st->x * scale_x);
1714                         sty = floor((gdouble)st->y * scale_y);
1715                         stw = ceil((gdouble)(st->x + pr->source_tile_width) * scale_x) - stx;
1716                         sth = ceil((gdouble)(st->y + pr->source_tile_height) * scale_y) - sty;
1717
1718                         if (pr_clip_region(stx, sty, stw, sth,
1719                                            it->x + x, it->y + y, w, h,
1720                                            &rx, &ry, &rw, &rh))
1721                                 {
1722                                 if (st->blank)
1723                                         {
1724                                         gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1725                                                            rx - st->x, ry - st->y, rw, rh);
1726                                         }
1727                                 else
1728                                         {
1729                                         gdouble offset_x;
1730                                         gdouble offset_y;
1731
1732                                         /* may need to use unfloored stx,sty values here */
1733                                         offset_x = (gdouble)(stx - it->x);
1734                                         offset_y = (gdouble)(sty - it->y);
1735
1736                                         gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1737                                                  (gdouble) 0.0 + offset_x,
1738                                                  (gdouble) 0.0 + offset_y,
1739                                                  scale_x, scale_y,
1740                                                  (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1741                                         draw = TRUE;
1742                                         }
1743                                 }
1744                         }
1745                 }
1746
1747         g_list_free(list);
1748
1749         return draw;
1750 }
1751
1752 void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height,
1753                                gint tile_width, gint tile_height, gint cache_size,
1754                                PixbufRendererTileRequestFunc func_request,
1755                                PixbufRendererTileDisposeFunc func_dispose,
1756                                gpointer user_data,
1757                                gdouble zoom)
1758 {
1759         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1760         g_return_if_fail(tile_width >= 32 && tile_width >= 32);
1761         g_return_if_fail(width >= 32 && height > 32);
1762         g_return_if_fail(func_request != NULL);
1763
1764         if (pr->pixbuf) g_object_unref(pr->pixbuf);
1765         pr->pixbuf = NULL;
1766
1767         pr_source_tile_unset(pr);
1768
1769         if (cache_size < 4) cache_size = 4;
1770
1771         pr->source_tiles_enabled = TRUE;
1772         pr->source_tiles_cache_size = cache_size;
1773         pr->source_tile_width = tile_width;
1774         pr->source_tile_height = tile_height;
1775
1776         pr->image_width = width;
1777         pr->image_height = height;
1778
1779         pr->func_tile_request = func_request;
1780         pr->func_tile_dispose = func_dispose;
1781         pr->func_tile_data = user_data;
1782
1783         pr_zoom_sync(pr, zoom, PR_ZOOM_FORCE | PR_ZOOM_NEW, 0, 0);
1784         pr_redraw(pr, TRUE);
1785 }
1786
1787 void pixbuf_renderer_set_tiles_size(PixbufRenderer *pr, gint width, gint height)
1788 {
1789         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1790         g_return_if_fail(width >= 32 && height > 32);
1791
1792         if (!pr->source_tiles_enabled) return;
1793         if (pr->image_width == width && pr->image_height == height) return;
1794
1795         pr->image_width = width;
1796         pr->image_height = height;
1797
1798         pr_zoom_sync(pr, pr->zoom, PR_ZOOM_FORCE, 0, 0);
1799 }
1800
1801 gint pixbuf_renderer_get_tiles(PixbufRenderer *pr)
1802 {
1803         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1804
1805         return pr->source_tiles_enabled;
1806 }
1807
1808 static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment,
1809                                 PrZoomFlags flags, gint x, gint y)
1810 {
1811         gdouble zoom = pr->zoom;
1812
1813         if (increment == 0.0) return;
1814
1815         if (zoom == 0.0)
1816                 {
1817                 if (pr->scale < 1.0)
1818                         {
1819                         zoom = 0.0 - 1.0 / pr->scale;
1820                         }
1821                 else
1822                         {
1823                         zoom = pr->scale;
1824                         }
1825                 }
1826
1827         if (increment < 0.0)
1828                 {
1829                 if (zoom >= 1.0 && zoom + increment < 1.0)
1830                         {
1831                         zoom = zoom + increment - 2.0;
1832                         }
1833                 else
1834                         {
1835                         zoom = zoom + increment;
1836                         }
1837                 }
1838         else
1839                 {
1840                 if (zoom <= -1.0 && zoom + increment > -1.0)
1841                         {
1842                         zoom = zoom + increment + 2.0;
1843                         }
1844                 else
1845                         {
1846                         zoom = zoom + increment;
1847                         }
1848                 }
1849
1850         pr_zoom_sync(pr, zoom, flags, x, y);
1851 }
1852
1853 /*
1854  *-------------------------------------------------------------------
1855  * display tiles
1856  *-------------------------------------------------------------------
1857  */
1858
1859 static ImageTile *pr_tile_new(gint x, gint y, gint width, gint height)
1860 {
1861         ImageTile *it;
1862
1863         it = g_new0(ImageTile, 1);
1864
1865         it->x = x;
1866         it->y = y;
1867         it->w = width;
1868         it->h = height;
1869
1870         it->render_done = TILE_RENDER_NONE;
1871
1872         return it;
1873 }
1874
1875 static void pr_tile_free(ImageTile *it)
1876 {
1877         if (!it) return;
1878
1879         if (it->pixbuf) g_object_unref(it->pixbuf);
1880         if (it->pixmap) g_object_unref(it->pixmap);
1881
1882         g_free(it);
1883 }
1884
1885 static void pr_tile_free_all(PixbufRenderer *pr)
1886 {
1887         GList *work;
1888
1889         work = pr->tiles;
1890         while (work)
1891                 {
1892                 ImageTile *it;
1893
1894                 it = work->data;
1895                 work = work->next;
1896
1897                 pr_tile_free(it);
1898                 }
1899
1900         g_list_free(pr->tiles);
1901         pr->tiles = NULL;
1902         pr->tile_cache_size = 0;
1903 }
1904
1905 static ImageTile *pr_tile_add(PixbufRenderer *pr, gint x, gint y)
1906 {
1907         ImageTile *it;
1908
1909         it = pr_tile_new(x, y, pr->tile_width, pr->tile_height);
1910
1911         if (it->x + it->w > pr->width) it->w = pr->width - it->x;
1912         if (it->y + it->h > pr->height) it->h = pr->height - it->y;
1913
1914         pr->tiles = g_list_prepend(pr->tiles, it);
1915         pr->tile_cache_size += it->size;
1916
1917         return it;
1918 }
1919
1920 static void pr_tile_remove(PixbufRenderer *pr, ImageTile *it)
1921 {
1922         if (it->qd)
1923                 {
1924                 QueueData *qd = it->qd;
1925
1926                 it->qd = NULL;
1927                 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
1928                 g_free(qd);
1929                 }
1930
1931         if (it->qd2)
1932                 {
1933                 QueueData *qd = it->qd2;
1934
1935                 it->qd2 = NULL;
1936                 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
1937                 g_free(qd);
1938                 }
1939
1940         pr->tiles = g_list_remove(pr->tiles, it);
1941         pr->tile_cache_size -= it->size;
1942
1943         pr_tile_free(it);
1944 }
1945
1946 static void pr_tile_free_space(PixbufRenderer *pr, guint space, ImageTile *it)
1947 {
1948         GList *work;
1949         guint tile_max;
1950
1951         work = g_list_last(pr->tiles);
1952
1953         if (pr->source_tiles_enabled && pr->scale < 1.0)
1954                 {
1955                 gint tiles;
1956
1957                 tiles = (pr->vis_width / pr->tile_width + 1) * (pr->vis_height / pr->tile_height + 1);
1958                 tile_max = MAX(tiles * pr->tile_width * pr->tile_height * 3,
1959                                (gint)((gdouble)pr->tile_cache_max * 1048576.0 * pr->scale));
1960                 }
1961         else
1962                 {
1963                 tile_max = pr->tile_cache_max * 1048576;
1964                 }
1965
1966         while (work && pr->tile_cache_size + space > tile_max)
1967                 {
1968                 ImageTile *needle;
1969
1970                 needle = work->data;
1971                 work = work->prev;
1972                 if (needle != it &&
1973                     ((!needle->qd && !needle->qd2) || !pr_tile_is_visible(pr, needle))) pr_tile_remove(pr, needle);
1974                 }
1975 }
1976
1977 static void pr_tile_invalidate_all(PixbufRenderer *pr)
1978 {
1979         GList *work;
1980
1981         work = pr->tiles;
1982         while (work)
1983                 {
1984                 ImageTile *it;
1985
1986                 it = work->data;
1987                 work = work->next;
1988
1989                 it->render_done = TILE_RENDER_NONE;
1990                 it->render_todo = TILE_RENDER_ALL;
1991                 it->blank = FALSE;
1992
1993                 it->w = MIN(pr->tile_width, pr->width - it->x);
1994                 it->h = MIN(pr->tile_height, pr->height - it->y);
1995                 }
1996 }
1997
1998 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1999 {
2000         gint x1, x2;
2001         gint y1, y2;
2002         GList *work;
2003
2004         x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
2005         x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
2006
2007         y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
2008         y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
2009
2010         work = pr->tiles;
2011         while (work)
2012                 {
2013                 ImageTile *it;
2014
2015                 it = work->data;
2016                 work = work->next;
2017
2018                 if (it->x < x2 && it->x + it->w > x1 &&
2019                     it->y < y2 && it->y + it->h > y1)
2020                         {
2021                         it->render_done = TILE_RENDER_NONE;
2022                         it->render_todo = TILE_RENDER_ALL;
2023                         }
2024                 }
2025 }
2026
2027 static ImageTile *pr_tile_get(PixbufRenderer *pr, gint x, gint y, gint only_existing)
2028 {
2029         GList *work;
2030
2031         work = pr->tiles;
2032         while (work)
2033                 {
2034                 ImageTile *it;
2035
2036                 it = work->data;
2037                 if (it->x == x && it->y == y)
2038                         {
2039                         pr->tiles = g_list_delete_link(pr->tiles, work);
2040                         pr->tiles = g_list_prepend(pr->tiles, it);
2041                         return it;
2042                         }
2043
2044                 work = work->next;
2045                 }
2046
2047         if (only_existing) return NULL;
2048
2049         return pr_tile_add(pr, x, y);
2050 }
2051
2052 static void pr_tile_prepare(PixbufRenderer *pr, ImageTile *it)
2053 {
2054         if (!it->pixmap)
2055                 {
2056                 GdkPixmap *pixmap;
2057                 guint size;
2058
2059                 pixmap = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
2060
2061                 size = pixmap_calc_size(pixmap);
2062                 pr_tile_free_space(pr, size, it);
2063
2064                 it->pixmap = pixmap;
2065                 it->size += size;
2066                 pr->tile_cache_size += size;
2067                 }
2068
2069         if ((pr->zoom != 1.0 || pr->source_tiles_enabled || (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf)) ||
2070              pr->orientation != EXIF_ORIENTATION_TOP_LEFT || pr->func_post_process) && !it->pixbuf)
2071                 {
2072                 GdkPixbuf *pixbuf;
2073                 guint size;
2074 #if 0
2075 /* I don't think that we need a pixbuf with alpha channel here */
2076                 if (pr->pixbuf)
2077                         {
2078                         pixbuf = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(pr->pixbuf),
2079                                                 gdk_pixbuf_get_has_alpha(pr->pixbuf),
2080                                                 gdk_pixbuf_get_bits_per_sample(pr->pixbuf),
2081                                                 pr->tile_width, pr->tile_height);
2082                         }
2083                 else
2084 #endif
2085                         {
2086                         pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
2087                         }
2088
2089                 size = gdk_pixbuf_get_rowstride(pixbuf) * pr->tile_height;
2090                 pr_tile_free_space(pr, size, it);
2091
2092                 it->pixbuf = pixbuf;
2093                 it->size += size;
2094                 pr->tile_cache_size += size;
2095                 }
2096 }
2097
2098 /*
2099  *-------------------------------------------------------------------
2100  * drawing
2101  *-------------------------------------------------------------------
2102  */
2103
2104
2105 static void pr_tile_coords_map_orientation(PixbufRenderer *pr,
2106                                      gdouble tile_x, gdouble tile_y, /* coordinates of the tile */
2107                                      gint image_w, gint image_h,
2108                                      gdouble tile_w, gdouble tile_h,
2109                                      gdouble *res_x, gdouble *res_y)
2110 {
2111         *res_x = tile_x;
2112         *res_y = tile_y;
2113         switch (pr->orientation)
2114                 {
2115                 case EXIF_ORIENTATION_TOP_LEFT:
2116                         /* normal -- nothing to do */
2117                         break;
2118                 case EXIF_ORIENTATION_TOP_RIGHT:
2119                         /* mirrored */
2120                         *res_x = image_w - tile_x - tile_w;
2121                         break;
2122                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2123                         /* upside down */
2124                         *res_x = image_w - tile_x - tile_w;
2125                         *res_y = image_h - tile_y - tile_h;
2126                         break;
2127                 case EXIF_ORIENTATION_BOTTOM_LEFT:
2128                         /* flipped */
2129                         *res_y = image_h - tile_y - tile_h;
2130                         break;
2131                 case EXIF_ORIENTATION_LEFT_TOP:
2132                         *res_x = tile_y;
2133                         *res_y = tile_x;
2134                         break;
2135                 case EXIF_ORIENTATION_RIGHT_TOP:
2136                         /* rotated -90 (270) */
2137                         *res_x = tile_y;
2138                         *res_y = image_w - tile_x - tile_w;
2139                         break;
2140                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2141                         *res_x = image_h - tile_y - tile_h;
2142                         *res_y = image_w - tile_x - tile_w;
2143                         break;
2144                 case EXIF_ORIENTATION_LEFT_BOTTOM:
2145                         /* rotated 90 */
2146                         *res_x = image_h - tile_y - tile_h;
2147                         *res_y = tile_x;
2148                         break;
2149                 default:
2150                         /* The other values are out of range */
2151                         break;
2152                 }
2153 //      log_printf("tile coord y:%f, ih:%d, th:%f ry:%f\n", tile_y, image_h, tile_h, *res_x);
2154 }
2155
2156 static void pr_tile_region_map_orientation(PixbufRenderer *pr,
2157                                      gint area_x, gint area_y, /* coordinates of the area inside tile */
2158                                      gint tile_w, gint tile_h,
2159                                      gint area_w, gint area_h,
2160                                      gint *res_x, gint *res_y,
2161                                      gint *res_w, gint *res_h)
2162 {
2163         *res_x = area_x;
2164         *res_y = area_y;
2165         *res_w = area_w;
2166         *res_h = area_h;
2167
2168         switch (pr->orientation)
2169                 {
2170                 case EXIF_ORIENTATION_TOP_LEFT:
2171                         /* normal -- nothing to do */
2172                         break;
2173                 case EXIF_ORIENTATION_TOP_RIGHT:
2174                         /* mirrored */
2175                         *res_x = tile_w - area_x - area_w;
2176                         break;
2177                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2178                         /* upside down */
2179                         *res_x = tile_w - area_x - area_w;
2180                         *res_y = tile_h - area_y - area_h;
2181                         break;
2182                 case EXIF_ORIENTATION_BOTTOM_LEFT:
2183                         /* flipped */
2184                         *res_y = tile_h - area_y - area_h;
2185                         break;
2186                 case EXIF_ORIENTATION_LEFT_TOP:
2187                         *res_x = area_y;
2188                         *res_y = area_x;
2189                         *res_w = area_h;
2190                         *res_h = area_w;
2191                         break;
2192                 case EXIF_ORIENTATION_RIGHT_TOP:
2193                         /* rotated -90 (270) */
2194                         *res_x = area_y;
2195                         *res_y = tile_w - area_x - area_w;
2196                         *res_w = area_h;
2197                         *res_h = area_w;
2198                         break;
2199                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2200                         *res_x = tile_h - area_y - area_h;
2201                         *res_y = tile_w - area_x - area_w;
2202                         *res_w = area_h;
2203                         *res_h = area_w;
2204                         break;
2205                 case EXIF_ORIENTATION_LEFT_BOTTOM:
2206                         /* rotated 90 */
2207                         *res_x = tile_h - area_y - area_h;
2208                         *res_y = area_x;
2209                         *res_w = area_h;
2210                         *res_h = area_w;
2211                         break;
2212                 default:
2213                         /* The other values are out of range */
2214                         break;
2215                 }
2216 //      log_printf("inside y:%d, th:%d, ah:%d ry:%d\n", area_y, tile_h, area_h, *res_x);
2217 }
2218
2219 static void pr_coords_map_orientation_reverse(PixbufRenderer *pr,
2220                                      gint area_x, gint area_y,
2221                                      gint tile_w, gint tile_h,
2222                                      gint area_w, gint area_h,
2223                                      gint *res_x, gint *res_y,
2224                                      gint *res_w, gint *res_h)
2225 {
2226         *res_x = area_x;
2227         *res_y = area_y;
2228         *res_w = area_w;
2229         *res_h = area_h;
2230
2231         switch (pr->orientation)
2232                 {
2233                 case EXIF_ORIENTATION_TOP_LEFT:
2234                         /* normal -- nothing to do */
2235                         break;
2236                 case EXIF_ORIENTATION_TOP_RIGHT:
2237                         /* mirrored */
2238                         *res_x = tile_w - area_x - area_w;
2239                         break;
2240                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2241                         /* upside down */
2242                         *res_x = tile_w - area_x - area_w;
2243                         *res_y = tile_h - area_y - area_h;
2244                         break;
2245                 case EXIF_ORIENTATION_BOTTOM_LEFT:
2246                         /* flipped */
2247                         *res_y = tile_h - area_y - area_h;
2248                         break;
2249                 case EXIF_ORIENTATION_LEFT_TOP:
2250                         *res_x = area_y;
2251                         *res_y = area_x;
2252                         *res_w = area_h;
2253                         *res_h = area_w;
2254                         break;
2255                 case EXIF_ORIENTATION_RIGHT_TOP:
2256                         /* rotated -90 (270) */
2257                         *res_x = tile_w - area_y - area_h;
2258                         *res_y = area_x;
2259                         *res_w = area_h;
2260                         *res_h = area_w;
2261                         break;
2262                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2263                         *res_x = tile_w - area_y - area_h;
2264                         *res_y = tile_h - area_x - area_w;
2265                         *res_w = area_h;
2266                         *res_h = area_w;
2267                         break;
2268                 case EXIF_ORIENTATION_LEFT_BOTTOM:
2269                         /* rotated 90 */
2270                         *res_x = area_y;
2271                         *res_y = tile_h - area_x - area_w;
2272                         *res_w = area_h;
2273                         *res_h = area_w;
2274                         break;
2275                 default:
2276                         /* The other values are out of range */
2277                         break;
2278                 }
2279 }
2280
2281
2282 static GdkPixbuf *pr_get_spare_tile(PixbufRenderer *pr)
2283 {
2284         if (!pr->spare_tile) pr->spare_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
2285         return pr->spare_tile;
2286 }
2287
2288 #define COLOR_BYTES 3   /* rgb */
2289
2290 static void pr_tile_rotate_90_clockwise(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2291 {
2292         GdkPixbuf *src = *tile;
2293         GdkPixbuf *dest;
2294         gint srs, drs;
2295         guchar *s_pix, *d_pix;
2296         guchar *sp, *dp;
2297         guchar *ip, *spi, *dpi;
2298         gint i, j;
2299         gint tw = pr->tile_width;
2300
2301         srs = gdk_pixbuf_get_rowstride(src);
2302         s_pix = gdk_pixbuf_get_pixels(src);
2303         spi = s_pix + (x * COLOR_BYTES);
2304
2305         dest = pr_get_spare_tile(pr);
2306         drs = gdk_pixbuf_get_rowstride(dest);
2307         d_pix = gdk_pixbuf_get_pixels(dest);
2308         dpi = d_pix + (tw - 1) * COLOR_BYTES;
2309
2310         for (i = y; i < y + h; i++)
2311                 {
2312                 sp = spi + (i * srs);
2313                 ip = dpi - (i * COLOR_BYTES);
2314                 for (j = x; j < x + w; j++)
2315                         {
2316                         dp = ip + (j * drs);
2317                         memcpy(dp, sp, COLOR_BYTES);
2318                         sp += COLOR_BYTES;
2319                         }
2320                 }
2321
2322         pr->spare_tile = src;
2323         *tile = dest;
2324 }
2325
2326 static void pr_tile_rotate_90_counter_clockwise(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2327 {
2328         GdkPixbuf *src = *tile;
2329         GdkPixbuf *dest;
2330         gint srs, drs;
2331         guchar *s_pix, *d_pix;
2332         guchar *sp, *dp;
2333         guchar *ip, *spi, *dpi;
2334         gint i, j;
2335         gint th = pr->tile_height;
2336
2337         srs = gdk_pixbuf_get_rowstride(src);
2338         s_pix = gdk_pixbuf_get_pixels(src);
2339         spi = s_pix + (x * COLOR_BYTES);
2340
2341         dest = pr_get_spare_tile(pr);
2342         drs = gdk_pixbuf_get_rowstride(dest);
2343         d_pix = gdk_pixbuf_get_pixels(dest);
2344         dpi = d_pix + (th - 1) * drs;
2345
2346         for (i = y; i < y + h; i++)
2347                 {
2348                 sp = spi + (i * srs);
2349                 ip = dpi + (i * COLOR_BYTES);
2350                 for (j = x; j < x + w; j++)
2351                         {
2352                         dp = ip - (j * drs);
2353                         memcpy(dp, sp, COLOR_BYTES);
2354                         sp += COLOR_BYTES;
2355                         }
2356                 }
2357
2358         pr->spare_tile = src;
2359         *tile = dest;
2360 }
2361
2362 static void pr_tile_mirror_only(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2363 {
2364         GdkPixbuf *src = *tile;
2365         GdkPixbuf *dest;
2366         gint srs, drs;
2367         guchar *s_pix, *d_pix;
2368         guchar *sp, *dp;
2369         guchar *spi, *dpi;
2370         gint i, j;
2371
2372         gint tw = pr->tile_width;
2373
2374         srs = gdk_pixbuf_get_rowstride(src);
2375         s_pix = gdk_pixbuf_get_pixels(src);
2376         spi = s_pix + (x * COLOR_BYTES);
2377
2378         dest = pr_get_spare_tile(pr);
2379         drs = gdk_pixbuf_get_rowstride(dest);
2380         d_pix = gdk_pixbuf_get_pixels(dest);
2381         dpi =  d_pix + (tw - x - 1) * COLOR_BYTES;
2382
2383         for (i = y; i < y + h; i++)
2384                 {
2385                 sp = spi + (i * srs);
2386                 dp = dpi + (i * drs);
2387                 for (j = 0; j < w; j++)
2388                         {
2389                         memcpy(dp, sp, COLOR_BYTES);
2390                         sp += COLOR_BYTES;
2391                         dp -= COLOR_BYTES;
2392                         }
2393                 }
2394
2395         pr->spare_tile = src;
2396         *tile = dest;
2397 }
2398
2399 static void pr_tile_mirror_and_flip(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2400 {
2401         GdkPixbuf *src = *tile;
2402         GdkPixbuf *dest;
2403         gint srs, drs;
2404         guchar *s_pix, *d_pix;
2405         guchar *sp, *dp;
2406         guchar *spi, *dpi;
2407         gint i, j;
2408         gint tw = pr->tile_width;
2409         gint th = pr->tile_height;
2410
2411         srs = gdk_pixbuf_get_rowstride(src);
2412         s_pix = gdk_pixbuf_get_pixels(src);
2413         spi = s_pix + (x * COLOR_BYTES);
2414
2415         dest = pr_get_spare_tile(pr);
2416         drs = gdk_pixbuf_get_rowstride(dest);
2417         d_pix = gdk_pixbuf_get_pixels(dest);
2418         dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
2419
2420         for (i = y; i < y + h; i++)
2421                 {
2422                 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
2423                 dp = dpi - (i * drs) - (x * COLOR_BYTES);
2424                 for (j = 0; j < w; j++)
2425                         {
2426                         memcpy(dp, sp, COLOR_BYTES);
2427                         sp += COLOR_BYTES;
2428                         dp -= COLOR_BYTES;
2429                         }
2430                 }
2431
2432         pr->spare_tile = src;
2433         *tile = dest;
2434 }
2435
2436 static void pr_tile_flip_only(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2437 {
2438         GdkPixbuf *src = *tile;
2439         GdkPixbuf *dest;
2440         gint srs, drs;
2441         guchar *s_pix, *d_pix;
2442         guchar *sp, *dp;
2443         guchar *spi, *dpi;
2444         gint i;
2445         gint th = pr->tile_height;
2446
2447         srs = gdk_pixbuf_get_rowstride(src);
2448         s_pix = gdk_pixbuf_get_pixels(src);
2449         spi = s_pix + (x * COLOR_BYTES);
2450
2451         dest = pr_get_spare_tile(pr);
2452         drs = gdk_pixbuf_get_rowstride(dest);
2453         d_pix = gdk_pixbuf_get_pixels(dest);
2454         dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
2455
2456         for (i = y; i < y + h; i++)
2457                 {
2458                 sp = spi + (i * srs);
2459                 dp = dpi - (i * drs);
2460                 memcpy(dp, sp, w * COLOR_BYTES);
2461                 }
2462
2463         pr->spare_tile = src;
2464         *tile = dest;
2465 }
2466
2467 static void pr_tile_apply_orientation(PixbufRenderer *pr, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
2468 {
2469         switch (pr->orientation)
2470                 {
2471                 case EXIF_ORIENTATION_TOP_LEFT:
2472                         /* normal -- nothing to do */
2473                         break;
2474                 case EXIF_ORIENTATION_TOP_RIGHT:
2475                         /* mirrored */
2476                         {
2477                                 pr_tile_mirror_only(pr, pixbuf, x, y, w, h);
2478                         }
2479                         break;
2480                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2481                         /* upside down */
2482                         {
2483                                 pr_tile_mirror_and_flip(pr, pixbuf, x, y, w, h);
2484                         }
2485                         break;
2486                 case EXIF_ORIENTATION_BOTTOM_LEFT:
2487                         /* flipped */
2488                         {
2489                                 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2490                         }
2491                         break;
2492                 case EXIF_ORIENTATION_LEFT_TOP:
2493                         {
2494                                 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2495                                 pr_tile_rotate_90_clockwise(pr, pixbuf, x, pr->tile_height - y - h, w, h);
2496                         }
2497                         break;
2498                 case EXIF_ORIENTATION_RIGHT_TOP:
2499                         /* rotated -90 (270) */
2500                         {
2501                                 pr_tile_rotate_90_clockwise(pr, pixbuf, x, y, w, h);
2502                         }
2503                         break;
2504                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2505                         {
2506                                 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2507                                 pr_tile_rotate_90_counter_clockwise(pr, pixbuf, x, pr->tile_height - y - h, w, h);
2508                         }
2509                         break;
2510                 case EXIF_ORIENTATION_LEFT_BOTTOM:
2511                         /* rotated 90 */
2512                         {
2513                                 pr_tile_rotate_90_counter_clockwise(pr, pixbuf, x, y, w, h);
2514                         }
2515                         break;
2516                 default:
2517                         /* The other values are out of range */
2518                         break;
2519                 }
2520 }
2521
2522
2523 static void pr_tile_render(PixbufRenderer *pr, ImageTile *it,
2524                            gint x, gint y, gint w, gint h,
2525                            gint new_data, gint fast)
2526 {
2527         GtkWidget *box;
2528         gint has_alpha;
2529         gint draw = FALSE;
2530
2531         if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return;
2532
2533         if (it->render_done != TILE_RENDER_ALL)
2534                 {
2535                 x = 0;
2536                 y = 0;
2537                 w = it->w;
2538                 h = it->h;
2539                 if (!fast) it->render_done = TILE_RENDER_ALL;
2540                 }
2541         else if (it->render_todo != TILE_RENDER_AREA)
2542                 {
2543                 if (!fast) it->render_todo = TILE_RENDER_NONE;
2544                 return;
2545                 }
2546
2547         if (!fast) it->render_todo = TILE_RENDER_NONE;
2548
2549         if (new_data) it->blank = FALSE;
2550
2551         pr_tile_prepare(pr, it);
2552         has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
2553
2554         box = GTK_WIDGET(pr);
2555
2556         /* FIXME checker colors for alpha should be configurable,
2557          * also should be drawn for blank = TRUE
2558          */
2559
2560         if (it->blank)
2561                 {
2562                 /* no data, do fast rect fill */
2563                 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
2564                                    0, 0, it->w, it->h);
2565                 }
2566         else if (pr->source_tiles_enabled)
2567                 {
2568                 draw = pr_source_tile_render(pr, it, x, y, w, h, new_data, fast);
2569                 }
2570         else if (pr->zoom == 1.0 || pr->scale == 1.0)
2571                 {
2572
2573                 gdouble src_x, src_y;
2574                 gint pb_x, pb_y;
2575                 gint pb_w, pb_h;
2576                 pr_tile_coords_map_orientation(pr, it->x, it->y,
2577                                             pr->image_width, pr->image_height,
2578                                             pr->tile_width, pr->tile_height,
2579                                             &src_x, &src_y);
2580                 pr_tile_region_map_orientation(pr, x, y,
2581                                             pr->tile_width, pr->tile_height,
2582                                             w, h,
2583                                             &pb_x, &pb_y,
2584                                             &pb_w, &pb_h);
2585
2586                 if (has_alpha)
2587                         {
2588                         gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2589                                          (gdouble) 0.0 - src_x,
2590                                          (gdouble) 0.0 - src_y,
2591                                          1.0, 1.0, GDK_INTERP_NEAREST,
2592                                          255, it->x + pb_x, it->y + pb_y,
2593                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2594                         pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2595                         draw = TRUE;
2596                         }
2597                 else
2598                         {
2599
2600
2601                         if (pr->orientation == EXIF_ORIENTATION_TOP_LEFT && !(pr->func_post_process && !(pr->post_process_slow && fast)))
2602                                 {
2603                                 /* faster, simple, base orientation, no postprocessing */
2604                                 gdk_draw_pixbuf(it->pixmap,
2605                                                 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2606                                                 pr->pixbuf,
2607                                                 it->x + x, it->y + y,
2608                                                 x, y,
2609                                                 w, h,
2610                                                 pr->dither_quality, it->x + x, it->y + y);
2611                                 }
2612                         else
2613                                 {
2614                                 gdk_pixbuf_copy_area(pr->pixbuf,
2615                                                      src_x + pb_x, src_y + pb_y,
2616                                                      pb_w, pb_h,
2617                                                      it->pixbuf,
2618                                                      pb_x, pb_y);
2619                                 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2620                                 draw = TRUE;
2621                                 }
2622                         }
2623                 }
2624         else
2625                 {
2626                 gdouble scale_x, scale_y;
2627                 gdouble src_x, src_y;
2628                 gint pb_x, pb_y;
2629                 gint pb_w, pb_h;
2630
2631                 if (pr->image_width == 0 || pr->image_height == 0) return;
2632
2633                 scale_x = (gdouble)pr->width / pr->image_width;
2634                 scale_y = (gdouble)pr->height / pr->image_height;
2635
2636                 pr_tile_coords_map_orientation(pr, it->x / scale_x, it->y /scale_y ,
2637                                             pr->image_width, pr->image_height,
2638                                             pr->tile_width / scale_x , pr->tile_height / scale_y,
2639                                             &src_x, &src_y);
2640                 pr_tile_region_map_orientation(pr, x, y,
2641                                             pr->tile_width, pr->tile_height,
2642                                             w, h,
2643                                             &pb_x, &pb_y,
2644                                             &pb_w, &pb_h);
2645
2646                 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
2647                  * small sizes for anything but GDK_INTERP_NEAREST
2648                  */
2649                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
2650
2651                 if (!has_alpha)
2652                         {
2653                         gdk_pixbuf_scale(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2654                                          (gdouble) 0.0 - src_x * scale_x,
2655                                          (gdouble) 0.0 - src_y * scale_y,
2656                                          scale_x, scale_y,
2657                                          (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
2658                         }
2659                 else
2660                         {
2661                         gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2662                                          (gdouble) 0.0 - src_x * scale_x,
2663                                          (gdouble) 0.0 - src_y * scale_y,
2664                                          scale_x, scale_y,
2665                                          (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
2666                                          255, it->x + pb_x, it->y + pb_y,
2667                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2668                         }
2669                 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2670                 draw = TRUE;
2671                 }
2672
2673         if (draw && it->pixbuf && !it->blank)
2674                 {
2675
2676                 if (pr->func_post_process && !(pr->post_process_slow && fast))
2677                         pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
2678
2679                 gdk_draw_pixbuf(it->pixmap,
2680                                 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2681                                 it->pixbuf,
2682                                 x, y,
2683                                 x, y,
2684                                 w, h,
2685                                 pr->dither_quality, it->x + x, it->y + y);
2686                 }
2687
2688 #if 0
2689         /* enable this line for debugging the edges of tiles */
2690         gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2691                            FALSE, 0, 0, it->w, it->h);
2692         gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2693                            FALSE, x, y, w, h);
2694 #endif
2695 }
2696
2697
2698 static void pr_tile_expose(PixbufRenderer *pr, ImageTile *it,
2699                            gint x, gint y, gint w, gint h,
2700                            gint new_data, gint fast)
2701 {
2702         GtkWidget *box;
2703
2704         pr_tile_render(pr, it, x, y, w, h, new_data, fast);
2705
2706         box = GTK_WIDGET(pr);
2707
2708         gdk_draw_drawable(box->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
2709                           it->pixmap, x, y,
2710                           pr->x_offset + (it->x - pr->x_scroll) + x, pr->y_offset + (it->y - pr->y_scroll) + y, w, h);
2711
2712         if (pr->overlay_list)
2713                 {
2714                 pr_overlay_draw(pr, pr->x_offset + (it->x - pr->x_scroll) + x,
2715                                 pr->y_offset + (it->y - pr->y_scroll) + y,
2716                                 w, h,
2717                                 it);
2718                 }
2719 }
2720
2721
2722 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it)
2723 {
2724         return (it->x + it->w >= pr->x_scroll && it->x < pr->x_scroll + pr->vis_width &&
2725                 it->y + it->h >= pr->y_scroll && it->y < pr->y_scroll + pr->vis_height);
2726 }
2727
2728 /*
2729  *-------------------------------------------------------------------
2730  * draw queue
2731  *-------------------------------------------------------------------
2732  */
2733
2734 static gint pr_get_queued_area(GList *work)
2735 {
2736         gint area = 0;
2737         
2738         while (work) 
2739                 {
2740                 QueueData *qd = work->data;
2741                 area += qd->w * qd->h;
2742                 work = work->next;
2743                 }
2744         return area;
2745 }
2746
2747
2748 static gint pr_queue_schedule_next_draw(PixbufRenderer *pr, gboolean force_set)
2749 {
2750         gfloat percent;
2751         gint visible_area = pr->vis_width * pr->vis_height;
2752         
2753         if (!pr->loading)
2754                 {
2755                 /* 2pass prio */ 
2756                 DEBUG_2("redraw priority: 2pass");
2757                 pr->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, pr_queue_draw_idle_cb, pr, NULL);
2758                 return FALSE;
2759                 }
2760         
2761         if (visible_area == 0)
2762                 {
2763                 /* not known yet */
2764                 percent = 100.0;
2765                 }
2766         else
2767                 {
2768                 percent = 100.0 * pr_get_queued_area(pr->draw_queue) / visible_area;
2769                 }
2770         
2771         if (percent > 10.0)
2772                 {
2773                 /* we have enough data for starting intensive redrawing */
2774                 DEBUG_2("redraw priority: high %.2f %%", percent);
2775                 pr->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, pr_queue_draw_idle_cb, pr, NULL);
2776                 return FALSE;
2777                 }
2778         
2779         if (percent < 1.0 || force_set)
2780                 {
2781                 /* queue is (almost) empty, wait  50 ms*/
2782                 DEBUG_2("redraw priority: wait %.2f %%", percent);
2783                 pr->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, pr_queue_draw_idle_cb, pr, NULL);
2784                 return FALSE;
2785                 }
2786         
2787         /* keep the same priority as before */
2788         DEBUG_2("redraw priority: no change %.2f %%", percent);
2789         return TRUE;
2790 }
2791                 
2792
2793 static gint pr_queue_draw_idle_cb(gpointer data)
2794 {
2795         PixbufRenderer *pr = data;
2796         QueueData *qd;
2797         gint fast;
2798
2799
2800         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2801             (!pr->draw_queue && !pr->draw_queue_2pass) ||
2802             pr->draw_idle_id == -1)
2803                 {
2804                 pr_render_complete_signal(pr);
2805
2806                 pr->draw_idle_id = -1;
2807                 return FALSE;
2808                 }
2809
2810         if (pr->draw_queue)
2811                 {
2812                 qd = pr->draw_queue->data;
2813                 fast = ((pr->zoom_2pass && pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow);
2814                 }
2815         else
2816                 {
2817                 if (pr->loading)
2818                         {
2819                         /* still loading, wait till done (also drops the higher priority) */
2820
2821                         return pr_queue_schedule_next_draw(pr, FALSE);
2822                         }
2823
2824                 qd = pr->draw_queue_2pass->data;
2825                 fast = FALSE;
2826                 }
2827
2828         if (GTK_WIDGET_REALIZED(pr))
2829                 {
2830                 if (pr_tile_is_visible(pr, qd->it))
2831                         {
2832                         pr_tile_expose(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2833                         }
2834                 else if (qd->new_data)
2835                         {
2836                         /* if new pixel data, and we already have a pixmap, update the tile */
2837                         qd->it->blank = FALSE;
2838                         if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL)
2839                                 {
2840                                 pr_tile_render(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2841                                 }
2842                         }
2843                 }
2844
2845         if (pr->draw_queue)
2846                 {
2847                 qd->it->qd = NULL;
2848                 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
2849                 if (fast)
2850                         {
2851                         if (qd->it->qd2)
2852                                 {
2853                                 pr_queue_merge(qd->it->qd2, qd);
2854                                 g_free(qd);
2855                                 }
2856                         else
2857                                 {
2858                                 qd->it->qd2 = qd;
2859                                 pr->draw_queue_2pass = g_list_append(pr->draw_queue_2pass, qd);
2860                                 }
2861                         }
2862                 else
2863                         {
2864                         g_free(qd);
2865                         }
2866                 }
2867         else
2868                 {
2869                 qd->it->qd2 = NULL;
2870                 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
2871                 g_free(qd);
2872                 }
2873
2874         if (!pr->draw_queue && !pr->draw_queue_2pass)
2875                 {
2876                 pr_render_complete_signal(pr);
2877
2878                 pr->draw_idle_id = -1;
2879                 return FALSE;
2880                 }
2881
2882                 return pr_queue_schedule_next_draw(pr, FALSE);
2883 }
2884
2885 static void pr_queue_list_free(GList *list)
2886 {
2887         GList *work;
2888
2889         work = list;
2890         while (work)
2891                 {
2892                 QueueData *qd;
2893
2894                 qd = work->data;
2895                 work = work->next;
2896
2897                 qd->it->qd = NULL;
2898                 qd->it->qd2 = NULL;
2899                 g_free(qd);
2900                 }
2901
2902         g_list_free(list);
2903 }
2904
2905 static void pr_queue_clear(PixbufRenderer *pr)
2906 {
2907         pr_queue_list_free(pr->draw_queue);
2908         pr->draw_queue = NULL;
2909
2910         pr_queue_list_free(pr->draw_queue_2pass);
2911         pr->draw_queue_2pass = NULL;
2912
2913         if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2914         pr->draw_idle_id = -1;
2915 }
2916
2917 static void pr_queue_merge(QueueData *parent, QueueData *qd)
2918 {
2919         if (parent->x + parent->w < qd->x + qd->w)
2920                 {
2921                 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
2922                 }
2923         if (parent->x > qd->x)
2924                 {
2925                 parent->w += parent->x - qd->x;
2926                 parent->x = qd->x;
2927                 }
2928
2929         if (parent->y + parent->h < qd->y + qd->h)
2930                 {
2931                 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
2932                 }
2933         if (parent->y > qd->y)
2934                 {
2935                 parent->h += parent->y - qd->y;
2936                 parent->y = qd->y;
2937                 }
2938
2939         parent->new_data |= qd->new_data;
2940 }
2941
2942 static gint pr_clamp_to_visible(PixbufRenderer *pr, gint *x, gint *y, gint *w, gint *h)
2943 {
2944         gint nx, ny;
2945         gint nw, nh;
2946         gint vx, vy;
2947         gint vw, vh;
2948
2949         vw = pr->vis_width;
2950         vh = pr->vis_height;
2951
2952         vx = pr->x_scroll;
2953         vy = pr->y_scroll;
2954
2955         if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
2956
2957         /* now clamp it */
2958         nx = CLAMP(*x, vx, vx + vw);
2959         nw = CLAMP(*w - (nx - *x), 1, vw);
2960
2961         ny = CLAMP(*y, vy, vy + vh);
2962         nh = CLAMP(*h - (ny - *y), 1, vh);
2963
2964         *x = nx;
2965         *y = ny;
2966         *w = nw;
2967         *h = nh;
2968
2969         return TRUE;
2970 }
2971
2972 static gint pr_queue_to_tiles(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2973                               gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2974 {
2975         gint i, j;
2976         gint x1, x2;
2977         gint y1, y2;
2978
2979         if (clamp && !pr_clamp_to_visible(pr, &x, &y, &w, &h)) return FALSE;
2980
2981         x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
2982         x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
2983
2984         y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
2985         y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
2986
2987         for (j = y1; j <= y2; j += pr->tile_height)
2988                 {
2989                 for (i = x1; i <= x2; i += pr->tile_width)
2990                         {
2991                         ImageTile *it;
2992
2993                         it = pr_tile_get(pr, i, j,
2994                                          (only_existing &&
2995                                           (i + pr->tile_width < pr->x_scroll ||
2996                                            i > pr->x_scroll + pr->vis_width ||
2997                                            j + pr->tile_height < pr->y_scroll ||
2998                                            j > pr->y_scroll + pr->vis_height)));
2999                         if (it)
3000                                 {
3001                                 QueueData *qd;
3002
3003                                 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
3004                                     (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
3005                                         {
3006                                         it->render_todo = render;
3007                                         }
3008
3009                                 qd = g_new(QueueData, 1);
3010                                 qd->it = it;
3011                                 qd->new_data = new_data;
3012
3013                                 if (i < x)
3014                                         {
3015                                         qd->x = x - i;
3016                                         }
3017                                 else
3018                                         {
3019                                         qd->x = 0;
3020                                         }
3021                                 qd->w = x + w - i - qd->x;
3022                                 if (qd->x + qd->w > pr->tile_width) qd->w = pr->tile_width - qd->x;
3023
3024                                 if (j < y)
3025                                         {
3026                                         qd->y = y - j;
3027                                         }
3028                                 else
3029                                         {
3030                                         qd->y = 0;
3031                                         }
3032                                 qd->h = y + h - j - qd->y;
3033                                 if (qd->y + qd->h > pr->tile_height) qd->h = pr->tile_height - qd->y;
3034
3035                                 if (qd->w < 1 || qd->h < 1)
3036                                         {
3037                                         g_free(qd);
3038                                         }
3039                                 else if (it->qd)
3040                                         {
3041                                         pr_queue_merge(it->qd, qd);
3042                                         g_free(qd);
3043                                         }
3044                                 else
3045                                         {
3046                                         it->qd = qd;
3047                                         pr->draw_queue = g_list_append(pr->draw_queue, qd);
3048                                         }
3049                                 }
3050                         }
3051                 }
3052
3053         return TRUE;
3054 }
3055
3056 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
3057                      gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
3058 {
3059         gint nx, ny;
3060
3061         nx = CLAMP(x, 0, pr->width - 1);
3062         ny = CLAMP(y, 0, pr->height - 1);
3063         w -= (nx - x);
3064         h -= (ny - y);
3065         w = CLAMP(w, 0, pr->width - nx);
3066         h = CLAMP(h, 0, pr->height - ny);
3067         if (w < 1 || h < 1) return;
3068
3069         if (pr_queue_to_tiles(pr, nx, ny, w, h, clamp, render, new_data, only_existing) &&
3070             ((!pr->draw_queue && !pr->draw_queue_2pass) || pr->draw_idle_id == -1))
3071                 {
3072                 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
3073                 pr_queue_schedule_next_draw(pr, TRUE);
3074                 }
3075 }
3076
3077 static void pr_redraw(PixbufRenderer *pr, gint new_data)
3078 {
3079         pr_queue_clear(pr);
3080         pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data, FALSE);
3081 }
3082
3083 /*
3084  *-------------------------------------------------------------------
3085  * signal emission
3086  *-------------------------------------------------------------------
3087  */
3088
3089 static void pr_update_signal(PixbufRenderer *pr)
3090 {
3091 #if 0
3092         log_printf("FIXME: send updated signal\n");
3093 #endif
3094         DEBUG_1("%s pixbuf renderer updated - started drawing %p", get_exec_time(), pr);
3095         pr->debug_updated = TRUE;
3096 }
3097
3098 static void pr_zoom_signal(PixbufRenderer *pr)
3099 {
3100         g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
3101 }
3102
3103 static void pr_clicked_signal(PixbufRenderer *pr, GdkEventButton *bevent)
3104 {
3105         g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
3106 }
3107
3108 static void pr_scroll_notify_signal(PixbufRenderer *pr)
3109 {
3110         g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
3111 }
3112
3113 static void pr_render_complete_signal(PixbufRenderer *pr)
3114 {
3115         if (!pr->complete)
3116                 {
3117                 g_signal_emit(pr, signals[SIGNAL_RENDER_COMPLETE], 0);
3118                 g_object_set(G_OBJECT(pr), "complete", TRUE, NULL);
3119                 }
3120         if (pr->debug_updated)
3121                 {
3122                 DEBUG_1("%s pixbuf renderer done %p", get_exec_time(), pr);
3123                 pr->debug_updated = FALSE;
3124                 }
3125 }
3126
3127 static void pr_drag_signal(PixbufRenderer *pr, GdkEventButton *bevent)
3128 {
3129         g_signal_emit(pr, signals[SIGNAL_DRAG], 0, bevent);
3130 }
3131
3132 /*
3133  *-------------------------------------------------------------------
3134  * sync and clamp
3135  *-------------------------------------------------------------------
3136  */
3137
3138 static void pixbuf_renderer_sync_scroll_center(PixbufRenderer *pr)
3139 {
3140         gint src_x, src_y;
3141         if (!pr->width || !pr->height) return;
3142
3143         src_x = pr->x_scroll + pr->vis_width / 2;
3144         src_y = pr->y_scroll + pr->vis_height / 2;
3145
3146         pr->norm_center_x = (gdouble)src_x / pr->width;
3147         pr->norm_center_y = (gdouble)src_y / pr->height;
3148 }
3149
3150
3151 static gint pr_scroll_clamp(PixbufRenderer *pr)
3152 {
3153         gint old_xs;
3154         gint old_ys;
3155
3156         if (pr->zoom == 0.0)
3157                 {
3158                 pr->x_scroll = 0;
3159                 pr->y_scroll = 0;
3160
3161                 return FALSE;
3162                 }
3163
3164         old_xs = pr->x_scroll;
3165         old_ys = pr->y_scroll;
3166
3167         if (pr->x_offset > 0)
3168                 {
3169                 pr->x_scroll = 0;
3170                 }
3171         else
3172                 {
3173                 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
3174                 }
3175
3176         if (pr->y_offset > 0)
3177                 {
3178                 pr->y_scroll = 0;
3179                 }
3180         else
3181                 {
3182                 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
3183                 }
3184
3185         pixbuf_renderer_sync_scroll_center(pr);
3186
3187         return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
3188 }
3189
3190 static gint pr_size_clamp(PixbufRenderer *pr)
3191 {
3192         gint old_vw, old_vh;
3193
3194         old_vw = pr->vis_width;
3195         old_vh = pr->vis_height;
3196
3197         if (pr->width < pr->window_width)
3198                 {
3199                 pr->vis_width = pr->width;
3200                 pr->x_offset = (pr->window_width - pr->width) / 2;
3201                 }
3202         else
3203                 {
3204                 pr->vis_width = pr->window_width;
3205                 pr->x_offset = 0;
3206                 }
3207
3208         if (pr->height < pr->window_height)
3209                 {
3210                 pr->vis_height = pr->height;
3211                 pr->y_offset = (pr->window_height - pr->height) / 2;
3212                 }
3213         else
3214                 {
3215                 pr->vis_height = pr->window_height;
3216                 pr->y_offset = 0;
3217                 }
3218
3219         pixbuf_renderer_sync_scroll_center(pr);
3220
3221         return (old_vw != pr->vis_width || old_vh != pr->vis_height);
3222 }
3223
3224 static gint pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom,
3225                           PrZoomFlags flags, gboolean *redrawn)
3226 {
3227         gint w, h;
3228         gdouble scale;
3229         gboolean invalid;
3230         gboolean force = !!(flags & PR_ZOOM_FORCE);
3231         gboolean new = !!(flags & PR_ZOOM_NEW);
3232         gboolean invalidate = !!(flags & PR_ZOOM_INVALIDATE);
3233         gboolean lazy = !!(flags & PR_ZOOM_LAZY);
3234
3235         zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
3236
3237         if (pr->zoom == zoom && !force) return FALSE;
3238
3239         w = pr->image_width;
3240         h = pr->image_height;
3241
3242         if (zoom == 0.0 && !pr->pixbuf)
3243                 {
3244                 scale = 1.0;
3245                 }
3246         else if (zoom == 0.0)
3247                 {
3248                 gint max_w;
3249                 gint max_h;
3250                 gboolean sizeable;
3251
3252                 sizeable = (new && pr_parent_window_sizable(pr));
3253
3254                 if (sizeable)
3255                         {
3256                         max_w = gdk_screen_width();
3257                         max_h = gdk_screen_height();
3258
3259                         if (pr->window_limit)
3260                                 {
3261                                 max_w = max_w * pr->window_limit_size / 100;
3262                                 max_h = max_h * pr->window_limit_size / 100;
3263                                 }
3264                         }
3265                 else
3266                         {
3267                         max_w = pr->window_width;
3268                         max_h = pr->window_height;
3269                         }
3270
3271                 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
3272                         {
3273                         if ((gdouble)max_w / w > (gdouble)max_h / h)
3274                                 {
3275                                 scale = (gdouble)max_h / h;
3276                                 h = max_h;
3277                                 w = w * scale + 0.5;
3278                                 if (w > max_w) w = max_w;
3279                                 }
3280                         else
3281                                 {
3282                                 scale = (gdouble)max_w / w;
3283                                 w = max_w;
3284                                 h = h * scale + 0.5;
3285                                 if (h > max_h) h = max_h;
3286                                 }
3287
3288                         if (pr->autofit_limit)
3289                                 {
3290                                 gdouble factor = (gdouble)pr->autofit_limit_size / 100;
3291                                 w = w * factor + 0.5;
3292                                 h = h * factor + 0.5;
3293                                 scale = scale * factor;
3294                                 }
3295
3296                         if (w < 1) w = 1;
3297                         if (h < 1) h = 1;
3298                         }
3299                 else
3300                         {
3301                         scale = 1.0;
3302                         }
3303                 }
3304         else if (zoom > 0.0) /* zoom orig, in */
3305                 {
3306                 scale = zoom;
3307                 w = w * scale;
3308                 h = h * scale;
3309                 }
3310         else /* zoom out */
3311                 {
3312                 scale = 1.0 / (0.0 - zoom);
3313                 w = w * scale;
3314                 h = h * scale;
3315                 }
3316
3317         invalid = (pr->width != w || pr->height != h);
3318
3319         pr->zoom = zoom;
3320         pr->width = w;
3321         pr->height = h;
3322         pr->scale = scale;
3323
3324         if (invalidate || invalid)
3325                 {
3326                 pr_tile_invalidate_all(pr);
3327                 if (!lazy) pr_redraw(pr, TRUE);
3328                 }
3329         if (redrawn) *redrawn = (invalidate || invalid);
3330
3331         pixbuf_renderer_sync_scroll_center(pr);
3332
3333         return TRUE;
3334 }
3335
3336 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
3337                          PrZoomFlags flags, gint px, gint py)
3338 {
3339         gdouble old_scale;
3340         gint old_cx, old_cy;
3341         gint clamped;
3342         gint sized;
3343         gboolean redrawn = FALSE;
3344         gboolean center_point = !!(flags & PR_ZOOM_CENTER);
3345         gboolean force = !!(flags & PR_ZOOM_FORCE);
3346         gboolean new = !!(flags & PR_ZOOM_NEW);
3347         gboolean lazy = !!(flags & PR_ZOOM_LAZY);
3348         PrZoomFlags clamp_flags = flags;
3349         gdouble old_center_x = pr->norm_center_x;
3350         gdouble old_center_y = pr->norm_center_y;
3351         
3352         old_scale = pr->scale;
3353         if (center_point)
3354                 {
3355                 px = CLAMP(px, 0, pr->width);
3356                 py = CLAMP(py, 0, pr->height);
3357                 old_cx = pr->x_scroll + (px - pr->x_offset);
3358                 old_cy = pr->y_scroll + (py - pr->y_offset);
3359                 }
3360         else
3361                 {
3362                 px = py = 0;
3363                 old_cx = pr->x_scroll + pr->vis_width / 2;
3364                 old_cy = pr->y_scroll + pr->vis_height / 2;
3365                 }
3366
3367         if (force) clamp_flags |= PR_ZOOM_INVALIDATE;
3368         if (lazy) clamp_flags |= PR_ZOOM_LAZY;
3369         if (!pr_zoom_clamp(pr, zoom, clamp_flags, &redrawn)) return;
3370
3371         clamped = pr_size_clamp(pr);
3372         sized = pr_parent_window_resize(pr, pr->width, pr->height);
3373
3374         if (force && new)
3375                 {
3376                 switch (pr->scroll_reset)
3377                         {
3378                         case PR_SCROLL_RESET_NOCHANGE:
3379                                 /* maintain old scroll position */
3380                                 pr->x_scroll = ((gdouble)pr->image_width * old_center_x * pr->scale) - pr->vis_width / 2;
3381                                 pr->y_scroll = ((gdouble)pr->image_height * old_center_y * pr->scale) - pr->vis_height / 2;
3382                                 break;
3383                         case PR_SCROLL_RESET_CENTER:
3384                                 /* center new image */
3385                                 pr->x_scroll = ((gdouble)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2;
3386                                 pr->y_scroll = ((gdouble)pr->image_height / 2.0 * pr->scale) - pr->vis_height / 2;
3387                                 break;
3388                         case PR_SCROLL_RESET_TOPLEFT:
3389                         default:
3390                                 /* reset to upper left */
3391                                 pr->x_scroll = 0;
3392                                 pr->y_scroll = 0;
3393                                 break;
3394                         }
3395                 }
3396         else
3397                 {
3398                 /* user zoom does not force, so keep visible center point */
3399                 if (center_point)
3400                         {
3401                         pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
3402                         pr->y_scroll = old_cy / old_scale * pr->scale - (py - pr->y_offset);
3403                         }
3404                 else
3405                         {
3406                         pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2);
3407                         pr->y_scroll = old_cy / old_scale * pr->scale - (pr->vis_height / 2);
3408                         }
3409                 }
3410
3411         pr_scroll_clamp(pr);
3412
3413         /* If the window was not sized, redraw the image - we know there will be no size/expose signal.
3414          * But even if a size is claimed, there is no guarantee that the window manager will allow it,
3415          * so redraw the window anyway :/
3416          */
3417         if (sized || clamped) pr_border_clear(pr);
3418         
3419         if (lazy)
3420                 {
3421                 pr_queue_clear(pr);
3422                 }
3423         else
3424                 {
3425                 pr_redraw(pr, redrawn);
3426                 }
3427
3428         pr_scroll_notify_signal(pr);
3429         pr_zoom_signal(pr);
3430         pr_update_signal(pr);
3431 }
3432
3433 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
3434 {
3435         gint zoom_changed = FALSE;
3436
3437         if (pr->window_width == new_width && pr->window_height == new_height) return;
3438
3439         pr->window_width = new_width;
3440         pr->window_height = new_height;
3441
3442         if (pr->zoom == 0.0)
3443                 {
3444                 gdouble old_scale = pr->scale;
3445                 pr_zoom_clamp(pr, 0.0, PR_ZOOM_FORCE, NULL);
3446                 zoom_changed = (old_scale != pr->scale);
3447                 }
3448
3449         pr_size_clamp(pr);
3450         pr_scroll_clamp(pr);
3451
3452         pr_overlay_update_sizes(pr);
3453
3454         /* ensure scroller remains visible */
3455         if (pr->scroller_overlay != -1)
3456                 {
3457                 gint update = FALSE;
3458
3459                 if (pr->scroller_x > new_width)
3460                         {
3461                         pr->scroller_x = new_width;
3462                         pr->scroller_xpos = new_width;
3463                         update = TRUE;
3464                         }
3465                 if (pr->scroller_y > new_height)
3466                         {
3467                         pr->scroller_y = new_height;
3468                         pr->scroller_ypos = new_height;
3469                         update = TRUE;
3470                         }
3471
3472                 if (update)
3473                         {
3474                         GdkPixbuf *pixbuf;
3475
3476                         if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
3477                                 {
3478                                 gint w, h;
3479
3480                                 w = gdk_pixbuf_get_width(pixbuf);
3481                                 h = gdk_pixbuf_get_height(pixbuf);
3482                                 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
3483                                                             pr->scroller_x - w / 2, pr->scroller_y - h / 2);
3484                                 }
3485                         }
3486                 }
3487
3488         pr_border_clear(pr);
3489
3490         pr_scroll_notify_signal(pr);
3491         if (zoom_changed) pr_zoom_signal(pr);
3492         pr_update_signal(pr);
3493 }
3494
3495 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
3496 {
3497         PixbufRenderer *pr = data;
3498
3499         pr_size_sync(pr, allocation->width, allocation->height);
3500 }
3501
3502 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
3503 {
3504         gint x, y;
3505
3506         pr_border_draw(pr, area->x, area->y, area->width, area->height);
3507
3508         x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
3509         y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
3510
3511         pr_queue(pr, x, y,
3512                  MIN((gint)area->width, pr->width - x),
3513                  MIN((gint)area->height, pr->height - y),
3514                  FALSE, TILE_RENDER_ALL, FALSE, FALSE);
3515 }
3516
3517 /*
3518  *-------------------------------------------------------------------
3519  * scrolling
3520  *-------------------------------------------------------------------
3521  */
3522
3523 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
3524 {
3525         gint old_x, old_y;
3526         gint x_off, y_off;
3527         gint w, h;
3528
3529         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3530
3531         if (!pr->pixbuf && !pr->source_tiles_enabled) return;
3532
3533         old_x = pr->x_scroll;
3534         old_y = pr->y_scroll;
3535
3536         pr->x_scroll += x;
3537         pr->y_scroll += y;
3538
3539         pr_scroll_clamp(pr);
3540         
3541         pixbuf_renderer_sync_scroll_center(pr);
3542         
3543         if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
3544
3545         pr_scroll_notify_signal(pr);
3546
3547         x_off = pr->x_scroll - old_x;
3548         y_off = pr->y_scroll - old_y;
3549
3550         w = pr->vis_width - abs(x_off);
3551         h = pr->vis_height - abs(y_off);
3552
3553         if (w < 1 || h < 1)
3554                 {
3555                 /* scrolled completely to new material */
3556                 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3557                 return;
3558                 }
3559         else
3560                 {
3561                 gint x1, y1;
3562                 gint x2, y2;
3563                 GtkWidget *box;
3564                 GdkGC *gc;
3565                 GdkEvent *event;
3566
3567                 if (x_off < 0)
3568                         {
3569                         x1 = abs(x_off);
3570                         x2 = 0;
3571                         }
3572                 else
3573                         {
3574                         x1 = 0;
3575                         x2 = abs(x_off);
3576                         }
3577
3578                 if (y_off < 0)
3579                         {
3580                         y1 = abs(y_off);
3581                         y2 = 0;
3582                         }
3583                 else
3584                         {
3585                         y1 = 0;
3586                         y2 = abs(y_off);
3587                         }
3588
3589                 box = GTK_WIDGET(pr);
3590
3591                 gc = gdk_gc_new(box->window);
3592                 gdk_gc_set_exposures(gc, TRUE);
3593                 gdk_draw_drawable(box->window, gc,
3594                                   box->window,
3595                                   x2 + pr->x_offset, y2 + pr->y_offset,
3596                                   x1 + pr->x_offset, y1 + pr->y_offset, w, h);
3597                 g_object_unref(gc);
3598
3599                 if (pr->overlay_list)
3600                         {
3601                         pr_overlay_queue_all(pr);
3602                         }
3603
3604                 w = pr->vis_width - w;
3605                 h = pr->vis_height - h;
3606
3607                 if (w > 0)
3608                         {
3609                         pr_queue(pr,
3610                                  x_off > 0 ? pr->x_scroll + (pr->vis_width - w) : pr->x_scroll, pr->y_scroll,
3611                                  w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3612                         }
3613                 if (h > 0)
3614                         {
3615                         /* FIXME, to optimize this, remove overlap */
3616                         pr_queue(pr,
3617                                  pr->x_scroll, y_off > 0 ? pr->y_scroll + (pr->vis_height - h) : pr->y_scroll,
3618                                  pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3619                         }
3620
3621                 /* process exposures here, "expose_event" seems to miss a few with obstructed windows */
3622                 while ((event = gdk_event_get_graphics_expose(box->window)) != NULL)
3623                         {
3624                         pixbuf_renderer_paint(pr, &event->expose.area);
3625
3626                         if (event->expose.count == 0)
3627                                 {
3628                                 gdk_event_free(event);
3629                                 break;
3630                                 }
3631                         gdk_event_free(event);
3632                         }
3633                 }
3634 }
3635
3636 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
3637                                      gdouble x_align, gdouble y_align)
3638 {
3639         gint px, py;
3640         gint ax, ay;
3641
3642         x_align = CLAMP(x_align, 0.0, 1.0);
3643         y_align = CLAMP(y_align, 0.0, 1.0);
3644
3645         ax = (gdouble)pr->vis_width * x_align;
3646         ay = (gdouble)pr->vis_height * y_align;
3647
3648         px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
3649         py = (gdouble)y * pr->scale - (pr->y_scroll + ay);
3650
3651         pixbuf_renderer_scroll(pr, px, py);
3652 }
3653
3654 /* get or set coordinates of viewport center in the image, in range 0.0 - 1.0 */
3655
3656 void pixbuf_renderer_get_scroll_center(PixbufRenderer *pr, gdouble *x, gdouble *y)
3657 {
3658         *x = pr->norm_center_x;
3659         *y = pr->norm_center_y;
3660 }
3661
3662 void pixbuf_renderer_set_scroll_center(PixbufRenderer *pr, gdouble x, gdouble y)
3663 {
3664         gdouble dst_x, dst_y;
3665
3666         dst_x = x * pr->width  - pr->vis_width  / 2 - pr->x_scroll + CLAMP(pr->subpixel_x_scroll, -1.0, 1.0);
3667         dst_y = y * pr->height - pr->vis_height / 2 - pr->y_scroll + CLAMP(pr->subpixel_y_scroll, -1.0, 1.0);
3668
3669         pr->subpixel_x_scroll = dst_x - (gint)dst_x;
3670         pr->subpixel_y_scroll = dst_y - (gint)dst_y;
3671
3672         pixbuf_renderer_scroll(pr, (gint)dst_x, (gint)dst_y);
3673 }
3674
3675
3676 /*
3677  *-------------------------------------------------------------------
3678  * mouse
3679  *-------------------------------------------------------------------
3680  */
3681
3682 static gint pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3683 {
3684         PixbufRenderer *pr;
3685         gint accel;
3686
3687         pr = PIXBUF_RENDERER(widget);
3688
3689         if (pr->scroller_id != -1)
3690                 {
3691                 pr->scroller_xpos = bevent->x;
3692                 pr->scroller_ypos = bevent->y;
3693                 }
3694
3695         if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
3696
3697         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3698                 {
3699                 pr->drag_moved++;
3700                 }
3701         else
3702                 {
3703                 widget_set_cursor(widget, GDK_FLEUR);
3704                 }
3705
3706         if (bevent->state & GDK_CONTROL_MASK)
3707                 {
3708                 accel = PR_PAN_SHIFT_MULTIPLIER;
3709                 }
3710         else
3711                 {
3712                 accel = 1;
3713                 }
3714
3715         /* do the scroll */
3716         pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
3717                                (pr->drag_last_y - bevent->y) * accel);
3718
3719         pr_drag_signal(pr, bevent);
3720
3721         pr->drag_last_x = bevent->x;
3722         pr->drag_last_y = bevent->y;
3723
3724         return FALSE;
3725 }
3726
3727 static gint pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3728 {
3729         PixbufRenderer *pr;
3730         GtkWidget *parent;
3731
3732         pr = PIXBUF_RENDERER(widget);
3733
3734         if (pr->scroller_id != -1) return TRUE;
3735
3736         switch (bevent->button)
3737                 {
3738                 case MOUSE_BUTTON_LEFT:
3739                         pr->in_drag = TRUE;
3740                         pr->drag_last_x = bevent->x;
3741                         pr->drag_last_y = bevent->y;
3742                         pr->drag_moved = 0;
3743                         gdk_pointer_grab(widget->window, FALSE,
3744                                          GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3745                                          NULL, NULL, bevent->time);
3746                         gtk_grab_add(widget);
3747                         break;
3748                 case MOUSE_BUTTON_MIDDLE:
3749                         pr->drag_moved = 0;
3750                         break;
3751                 case MOUSE_BUTTON_RIGHT:
3752                         pr_clicked_signal(pr, bevent);
3753                         break;
3754                 default:
3755                         break;
3756                 }
3757
3758         parent = gtk_widget_get_parent(widget);
3759         if (widget && GTK_WIDGET_CAN_FOCUS(parent))
3760                 {
3761                 gtk_widget_grab_focus(parent);
3762                 }
3763
3764         return FALSE;
3765 }
3766
3767 static gint pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3768 {
3769         PixbufRenderer *pr;
3770
3771         pr = PIXBUF_RENDERER(widget);
3772
3773         if (pr->scroller_id != -1)
3774                 {
3775                 pr_scroller_stop(pr);
3776                 return TRUE;
3777                 }
3778
3779         if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
3780                 {
3781                 gtk_grab_remove(widget);
3782                 gdk_pointer_ungrab(bevent->time);
3783                 widget_set_cursor(widget, -1);
3784                 }
3785
3786         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3787                 {
3788                 if (bevent->button == MOUSE_BUTTON_LEFT && (bevent->state & GDK_CONTROL_MASK))
3789                         {
3790                         pr_scroller_start(pr, bevent->x, bevent->y);
3791                         }
3792                 else if (bevent->button == MOUSE_BUTTON_LEFT || bevent->button == MOUSE_BUTTON_MIDDLE)
3793                         {
3794                         pr_clicked_signal(pr, bevent);
3795                         }
3796                 }
3797
3798         pr->in_drag = FALSE;
3799
3800         return FALSE;
3801 }
3802
3803 static gint pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
3804 {
3805         PixbufRenderer *pr;
3806
3807         pr = PIXBUF_RENDERER(widget);
3808
3809         if (pr->scroller_id != -1)
3810                 {
3811                 pr->scroller_xpos = pr->scroller_x;
3812                 pr->scroller_ypos = pr->scroller_y;
3813                 pr->scroller_xinc = 0;
3814                 pr->scroller_yinc = 0;
3815                 }
3816
3817         return FALSE;
3818 }
3819
3820 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
3821 {
3822         PixbufRenderer *pr;
3823
3824         pr = PIXBUF_RENDERER(widget);
3825
3826         pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
3827 }
3828
3829 static void pr_signals_connect(PixbufRenderer *pr)
3830 {
3831         g_signal_connect(G_OBJECT(pr), "motion_notify_event",
3832                          G_CALLBACK(pr_mouse_motion_cb), pr);
3833         g_signal_connect(G_OBJECT(pr), "button_press_event",
3834                          G_CALLBACK(pr_mouse_press_cb), pr);
3835         g_signal_connect(G_OBJECT(pr), "button_release_event",
3836                          G_CALLBACK(pr_mouse_release_cb), pr);
3837         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
3838                          G_CALLBACK(pr_mouse_leave_cb), pr);
3839         g_signal_connect(G_OBJECT(pr), "unmap",
3840                          G_CALLBACK(pr_unmap_cb), pr);
3841
3842         gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK |
3843                                               GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
3844                                               GDK_LEAVE_NOTIFY_MASK);
3845
3846         g_signal_connect(G_OBJECT(pr), "drag_begin",
3847                          G_CALLBACK(pr_mouse_drag_cb), pr);
3848
3849 }
3850
3851 /*
3852  *-------------------------------------------------------------------
3853  * public
3854  *-------------------------------------------------------------------
3855  */
3856 static void pr_pixbuf_size_sync(PixbufRenderer *pr)
3857 {
3858         if (!pr->pixbuf) return;
3859         switch (pr->orientation)
3860                 {
3861                 case EXIF_ORIENTATION_LEFT_TOP:
3862                 case EXIF_ORIENTATION_RIGHT_TOP:
3863                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
3864                 case EXIF_ORIENTATION_LEFT_BOTTOM:
3865                         pr->image_width = gdk_pixbuf_get_height(pr->pixbuf);
3866                         pr->image_height = gdk_pixbuf_get_width(pr->pixbuf);
3867                         break;
3868                 default:
3869                         pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
3870                         pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
3871                 }
3872 }
3873
3874 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, PrZoomFlags flags)
3875 {
3876         if (pixbuf) g_object_ref(pixbuf);
3877         if (pr->pixbuf) g_object_unref(pr->pixbuf);
3878         pr->pixbuf = pixbuf;
3879
3880         if (!pr->pixbuf)
3881                 {
3882                 GtkWidget *box;
3883
3884                 /* no pixbuf so just clear the window */
3885                 pr->image_width = 0;
3886                 pr->image_height = 0;
3887                 pr->scale = 1.0;
3888
3889                 box = GTK_WIDGET(pr);
3890
3891                 if (GTK_WIDGET_REALIZED(box))
3892                         {
3893                         gdk_window_clear(box->window);
3894                         pr_overlay_draw(pr, 0, 0, pr->window_width, pr->window_height, NULL);
3895                         }
3896
3897                 pr_update_signal(pr);
3898
3899                 return;
3900                 }
3901
3902         pr_pixbuf_size_sync(pr);
3903         pr_zoom_sync(pr, zoom, flags | PR_ZOOM_FORCE | PR_ZOOM_NEW, 0, 0);
3904 }
3905
3906 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3907 {
3908         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3909
3910         pr_source_tile_unset(pr);
3911
3912         pr_set_pixbuf(pr, pixbuf, zoom, 0);
3913
3914         pr_update_signal(pr);
3915 }
3916
3917 void pixbuf_renderer_set_pixbuf_lazy(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, gint orientation)
3918 {
3919         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3920
3921         pr_source_tile_unset(pr);
3922
3923         pr->orientation = orientation;
3924         pr_set_pixbuf(pr, pixbuf, zoom, PR_ZOOM_LAZY);
3925
3926         pr_update_signal(pr);
3927 }
3928
3929 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
3930 {
3931         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
3932
3933         return pr->pixbuf;
3934 }
3935
3936 void pixbuf_renderer_set_orientation(PixbufRenderer *pr, gint orientation)
3937 {
3938         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3939
3940         pr->orientation = orientation;
3941
3942         pr_pixbuf_size_sync(pr);
3943         pr_zoom_sync(pr, pr->zoom, PR_ZOOM_FORCE, 0, 0);
3944 }
3945
3946 gint pixbuf_renderer_get_orientation(PixbufRenderer *pr)
3947 {
3948         if (!pr) return 1;
3949         return pr->orientation;
3950 }
3951
3952 void pixbuf_renderer_set_post_process_func(PixbufRenderer *pr, PixbufRendererPostProcessFunc func, gpointer user_data, gint slow)
3953 {
3954         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3955
3956         pr->func_post_process = func;
3957         pr->post_process_user_data = user_data;
3958         pr->post_process_slow = func && slow;
3959
3960 }
3961
3962
3963 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
3964 {
3965         GObject *object;
3966         PixbufRendererScrollResetType scroll_reset;
3967
3968         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3969         g_return_if_fail(IS_PIXBUF_RENDERER(source));
3970
3971         if (pr == source) return;
3972
3973         object = G_OBJECT(pr);
3974
3975         g_object_set(object, "zoom_min", source->zoom_min, NULL);
3976         g_object_set(object, "zoom_max", source->zoom_max, NULL);
3977         g_object_set(object, "loading", source->loading, NULL);
3978
3979         pr->complete = source->complete;
3980
3981         pr->x_scroll = source->x_scroll;
3982         pr->y_scroll = source->y_scroll;
3983
3984         scroll_reset = pr->scroll_reset;
3985         pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
3986
3987         pr->func_post_process = source->func_post_process;
3988         pr->post_process_user_data = source->post_process_user_data;
3989         pr->post_process_slow = source->post_process_slow;
3990         pr->orientation = source->orientation;
3991
3992         if (source->source_tiles_enabled)
3993                 {
3994                 pr_source_tile_unset(pr);
3995
3996                 pr->source_tiles_enabled = source->source_tiles_enabled;
3997                 pr->source_tiles_cache_size = source->source_tiles_cache_size;
3998                 pr->source_tile_width = source->source_tile_width;
3999                 pr->source_tile_height = source->source_tile_height;
4000                 pr->image_width = source->image_width;
4001                 pr->image_height = source->image_height;
4002
4003                 pr->func_tile_request = source->func_tile_request;
4004                 pr->func_tile_dispose = source->func_tile_dispose;
4005                 pr->func_tile_data = source->func_tile_data;
4006
4007                 pr->source_tiles = source->source_tiles;
4008                 source->source_tiles = NULL;
4009
4010                 pr_zoom_sync(pr, source->zoom, PR_ZOOM_FORCE | PR_ZOOM_NEW, 0, 0);
4011                 pr_redraw(pr, TRUE);
4012                 }
4013         else
4014                 {
4015                 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
4016                 }
4017
4018         pr->scroll_reset = scroll_reset;
4019
4020         pixbuf_renderer_set_pixbuf(source, NULL, source->zoom);
4021         pr_queue_clear(source);
4022         pr_tile_free_all(source);
4023 }
4024
4025 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint src_x, gint src_y, gint src_w, gint src_h)
4026 {
4027         gint x, y, width, height,  x1, y1, x2, y2;
4028
4029         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4030
4031         pr_coords_map_orientation_reverse(pr,
4032                                      src_x, src_y,
4033                                      pr->image_width, pr->image_height,
4034                                      src_w, src_h,
4035                                      &x, &y,
4036                                      &width, &height);
4037
4038         if (pr->source_tiles_enabled)
4039                 {
4040                 pr_source_tile_changed(pr, x, y, width, height);
4041                 }
4042
4043         if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
4044                 {
4045                 /* increase region when using a zoom quality that may access surrounding pixels */
4046                 y -= 1;
4047                 height += 2;
4048                 }
4049
4050         x1 = (gint)floor((gdouble)x * pr->scale);
4051         y1 = (gint)floor((gdouble)y * pr->scale);
4052         x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
4053         y2 = (gint)ceil((gdouble)(y + height) * pr->scale);
4054
4055         pr_queue(pr, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
4056 }
4057
4058 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
4059 {
4060         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4061
4062         pr_zoom_adjust_real(pr, increment, PR_ZOOM_NONE, 0, 0);
4063 }
4064
4065 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
4066 {
4067         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4068
4069         pr_zoom_adjust_real(pr, increment, PR_ZOOM_CENTER, x, y);
4070 }
4071
4072 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
4073 {
4074         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4075
4076         pr_zoom_sync(pr, zoom, PR_ZOOM_NONE, 0, 0);
4077 }
4078
4079 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
4080 {
4081         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
4082
4083         return pr->zoom;
4084 }
4085
4086 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
4087 {
4088         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
4089
4090         return pr->scale;
4091 }
4092
4093 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
4094 {
4095         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4096
4097         if (min > 1.0 || max < 1.0) return;
4098         if (min < 1.0 && min > -1.0) return;
4099         if (min < -200.0 || max > 200.0) return;
4100
4101         if (pr->zoom_min != min)
4102                 {
4103                 pr->zoom_min = min;
4104                 g_object_notify(G_OBJECT(pr), "zoom_min");
4105                 }
4106         if (pr->zoom_max != max)
4107                 {
4108                 pr->zoom_max = max;
4109                 g_object_notify(G_OBJECT(pr), "zoom_max");
4110                 }
4111 }
4112
4113 gint pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
4114 {
4115         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4116         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
4117
4118         if (!pr->pixbuf && !pr->source_tiles_enabled)
4119                 {
4120                 *width = 0;
4121                 *height = 0;
4122                 return FALSE;
4123                 }
4124
4125         *width = pr->image_width;
4126         *height = pr->image_height;
4127         return TRUE;
4128 }
4129
4130 gint pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
4131 {
4132         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4133         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
4134
4135         if (!pr->pixbuf && !pr->source_tiles_enabled)
4136                 {
4137                 *width = 0;
4138                 *height = 0;
4139                 return FALSE;
4140                 }
4141
4142         *width = pr->width;
4143         *height = pr->height;
4144         return TRUE;
4145 }
4146
4147 gint pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
4148 {
4149         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4150         g_return_val_if_fail(rect != NULL, FALSE);
4151
4152         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
4153             !pr->scale)
4154                 {
4155                 rect->x = 0;
4156                 rect->y = 0;
4157                 rect->width = 0;
4158                 rect->height = 0;
4159                 return FALSE;
4160                 }
4161
4162         rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
4163         rect->y = (gint)((gdouble)pr->y_scroll / pr->scale);
4164         rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
4165         rect->height = (gint)((gdouble)pr->vis_height / pr->scale);
4166         return TRUE;
4167 }
4168
4169 gint pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
4170 {
4171         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4172         g_return_val_if_fail(rect != NULL, FALSE);
4173
4174         if ((!pr->pixbuf && !pr->source_tiles_enabled))
4175                 {
4176                 rect->x = 0;
4177                 rect->y = 0;
4178                 rect->width = 0;
4179                 rect->height = 0;
4180                 return FALSE;
4181                 }
4182
4183         rect->x = pr->x_scroll;
4184         rect->y = pr->y_scroll;
4185         rect->width = pr->vis_width;
4186         rect->height = pr->vis_height;
4187         return TRUE;
4188 }
4189 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */