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