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