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