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