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