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