Effectively drop empty newlines at end of files (missing from rev 535)
[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 #define COLOR_BYTES 3   /* rgb */
2240
2241 static void pr_tile_rotate_90_clockwise(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2242 {
2243         GdkPixbuf *src = *tile;
2244         GdkPixbuf *dest;
2245         gint srs, drs;
2246         guchar *s_pix, *d_pix;
2247         guchar *sp, *dp;
2248         guchar *ip, *spi, *dpi;
2249         gint i, j;
2250         gint tw = pr->tile_width;
2251
2252         srs = gdk_pixbuf_get_rowstride(src);
2253         s_pix = gdk_pixbuf_get_pixels(src);
2254         spi = s_pix + (x * COLOR_BYTES);
2255
2256         dest = pr_get_spare_tile(pr);
2257         drs = gdk_pixbuf_get_rowstride(dest);
2258         d_pix = gdk_pixbuf_get_pixels(dest);
2259         dpi = d_pix + (tw - 1) * COLOR_BYTES;
2260
2261         for (i = y; i < y + h; i++)
2262                 {
2263                 sp = spi + (i * srs);
2264                 ip = dpi - (i * COLOR_BYTES);
2265                 for (j = x; j < x + w; j++)
2266                         {
2267                         dp = ip + (j * drs);
2268                         memcpy(dp, sp, COLOR_BYTES);
2269                         sp += COLOR_BYTES;
2270                         }
2271                 }
2272
2273         pr->spare_tile = src;
2274         *tile = dest;
2275 }
2276
2277 static void pr_tile_rotate_90_counter_clockwise(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2278 {
2279         GdkPixbuf *src = *tile;
2280         GdkPixbuf *dest;
2281         gint srs, drs;
2282         guchar *s_pix, *d_pix;
2283         guchar *sp, *dp;
2284         guchar *ip, *spi, *dpi;
2285         gint i, j;
2286         gint th = pr->tile_height;
2287
2288         srs = gdk_pixbuf_get_rowstride(src);
2289         s_pix = gdk_pixbuf_get_pixels(src);
2290         spi = s_pix + (x * COLOR_BYTES);
2291
2292         dest = pr_get_spare_tile(pr);
2293         drs = gdk_pixbuf_get_rowstride(dest);
2294         d_pix = gdk_pixbuf_get_pixels(dest);
2295         dpi = d_pix + (th - 1) * drs;
2296
2297         for (i = y; i < y + h; i++)
2298                 {
2299                 sp = spi + (i * srs);
2300                 ip = dpi + (i * COLOR_BYTES);
2301                 for (j = x; j < x + w; j++)
2302                         {
2303                         dp = ip - (j * drs);
2304                         memcpy(dp, sp, COLOR_BYTES);
2305                         sp += COLOR_BYTES;
2306                         }
2307                 }
2308
2309         pr->spare_tile = src;
2310         *tile = dest;
2311 }
2312
2313 static void pr_tile_mirror_only(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2314 {
2315         GdkPixbuf *src = *tile;
2316         GdkPixbuf *dest;
2317         gint srs, drs;
2318         guchar *s_pix, *d_pix;
2319         guchar *sp, *dp;
2320         guchar *spi, *dpi;
2321         gint i, j;
2322
2323         gint tw = pr->tile_width;
2324
2325         srs = gdk_pixbuf_get_rowstride(src);
2326         s_pix = gdk_pixbuf_get_pixels(src);
2327         spi = s_pix + (x * COLOR_BYTES);
2328
2329         dest = pr_get_spare_tile(pr);
2330         drs = gdk_pixbuf_get_rowstride(dest);
2331         d_pix = gdk_pixbuf_get_pixels(dest);
2332         dpi =  d_pix + (tw - x - 1) * COLOR_BYTES;
2333
2334         for (i = y; i < y + h; i++)
2335                 {
2336                 sp = spi + (i * srs);
2337                 dp = dpi + (i * drs);
2338                 for (j = 0; j < w; j++)
2339                         {
2340                         memcpy(dp, sp, COLOR_BYTES);
2341                         sp += COLOR_BYTES;
2342                         dp -= COLOR_BYTES;
2343                         }
2344                 }
2345
2346         pr->spare_tile = src;
2347         *tile = dest;
2348 }
2349
2350 static void pr_tile_mirror_and_flip(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2351 {
2352         GdkPixbuf *src = *tile;
2353         GdkPixbuf *dest;
2354         gint srs, drs;
2355         guchar *s_pix, *d_pix;
2356         guchar *sp, *dp;
2357         guchar *spi, *dpi;
2358         gint i, j;
2359         gint tw = pr->tile_width;
2360         gint th = pr->tile_height;
2361
2362         srs = gdk_pixbuf_get_rowstride(src);
2363         s_pix = gdk_pixbuf_get_pixels(src);
2364         spi = s_pix + (x * COLOR_BYTES);
2365
2366         dest = pr_get_spare_tile(pr);
2367         drs = gdk_pixbuf_get_rowstride(dest);
2368         d_pix = gdk_pixbuf_get_pixels(dest);
2369         dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
2370
2371         for (i = y; i < y + h; i++)
2372                 {
2373                 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
2374                 dp = dpi - (i * drs) - (x * COLOR_BYTES);
2375                 for (j = 0; j < w; j++)
2376                         {
2377                         memcpy(dp, sp, COLOR_BYTES);
2378                         sp += COLOR_BYTES;
2379                         dp -= COLOR_BYTES;
2380                         }
2381                 }
2382
2383         pr->spare_tile = src;
2384         *tile = dest;
2385 }
2386
2387 static void pr_tile_flip_only(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2388 {
2389         GdkPixbuf *src = *tile;
2390         GdkPixbuf *dest;
2391         gint srs, drs;
2392         guchar *s_pix, *d_pix;
2393         guchar *sp, *dp;
2394         guchar *spi, *dpi;
2395         gint i;
2396         gint th = pr->tile_height;
2397
2398         srs = gdk_pixbuf_get_rowstride(src);
2399         s_pix = gdk_pixbuf_get_pixels(src);
2400         spi = s_pix + (x * COLOR_BYTES);
2401
2402         dest = pr_get_spare_tile(pr);
2403         drs = gdk_pixbuf_get_rowstride(dest);
2404         d_pix = gdk_pixbuf_get_pixels(dest);
2405         dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
2406
2407         for (i = y; i < y + h; i++)
2408                 {
2409                 sp = spi + (i * srs);
2410                 dp = dpi - (i * drs);
2411                 memcpy(dp, sp, w * COLOR_BYTES);
2412                 }
2413
2414         pr->spare_tile = src;
2415         *tile = dest;
2416 }
2417
2418 static void pr_tile_apply_orientation(PixbufRenderer *pr, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
2419 {
2420         switch (pr->orientation)
2421                 {
2422                 case EXIF_ORIENTATION_TOP_LEFT:
2423                         /* normal -- nothing to do */
2424                         break;
2425                 case EXIF_ORIENTATION_TOP_RIGHT:
2426                         /* mirrored */
2427                         {
2428                                 pr_tile_mirror_only(pr, pixbuf, x, y, w, h);
2429                         }
2430                         break;
2431                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2432                         /* upside down */
2433                         {
2434                                 pr_tile_mirror_and_flip(pr, pixbuf, x, y, w, h);
2435                         }
2436                         break;
2437                 case EXIF_ORIENTATION_BOTTOM_LEFT:
2438                         /* flipped */
2439                         {
2440                                 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2441                         }
2442                         break;
2443                 case EXIF_ORIENTATION_LEFT_TOP:
2444                         {
2445                                 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2446                                 pr_tile_rotate_90_clockwise(pr, pixbuf, x, pr->tile_height - y - h, w, h);
2447                         }
2448                         break;
2449                 case EXIF_ORIENTATION_RIGHT_TOP:
2450                         /* rotated -90 (270) */
2451                         {
2452                                 pr_tile_rotate_90_clockwise(pr, pixbuf, x, y, w, h);
2453                         }
2454                         break;
2455                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2456                         {
2457                                 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2458                                 pr_tile_rotate_90_counter_clockwise(pr, pixbuf, x, pr->tile_height - y - h, w, h);
2459                         }
2460                         break;
2461                 case EXIF_ORIENTATION_LEFT_BOTTOM:
2462                         /* rotated 90 */
2463                         {
2464                                 pr_tile_rotate_90_counter_clockwise(pr, pixbuf, x, y, w, h);
2465                         }
2466                         break;
2467                 default:
2468                         /* The other values are out of range */
2469                         break;
2470                 }
2471 }
2472
2473
2474 static void pr_tile_render(PixbufRenderer *pr, ImageTile *it,
2475                            gint x, gint y, gint w, gint h,
2476                            gint new_data, gint fast)
2477 {
2478         GtkWidget *box;
2479         gint has_alpha;
2480         gint draw = FALSE;
2481
2482         if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return;
2483
2484         if (it->render_done != TILE_RENDER_ALL)
2485                 {
2486                 x = 0;
2487                 y = 0;
2488                 w = it->w;
2489                 h = it->h;
2490                 if (!fast) it->render_done = TILE_RENDER_ALL;
2491                 }
2492         else if (it->render_todo != TILE_RENDER_AREA)
2493                 {
2494                 if (!fast) it->render_todo = TILE_RENDER_NONE;
2495                 return;
2496                 }
2497
2498         if (!fast) it->render_todo = TILE_RENDER_NONE;
2499
2500         if (new_data) it->blank = FALSE;
2501
2502         pr_tile_prepare(pr, it);
2503         has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
2504
2505         box = GTK_WIDGET(pr);
2506
2507         /* FIXME checker colors for alpha should be configurable,
2508          * also should be drawn for blank = TRUE
2509          */
2510
2511         if (it->blank)
2512                 {
2513                 /* no data, do fast rect fill */
2514                 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
2515                                    0, 0, it->w, it->h);
2516                 }
2517         else if (pr->source_tiles_enabled)
2518                 {
2519                 draw = pr_source_tile_render(pr, it, x, y, w, h, new_data, fast);
2520                 }
2521         else if (pr->zoom == 1.0 || pr->scale == 1.0)
2522                 {
2523
2524                 double src_x, src_y;
2525                 gint pb_x, pb_y;
2526                 gint pb_w, pb_h;
2527                 pr_tile_coords_map_orientation(pr, it->x, it->y,
2528                                             pr->image_width, pr->image_height,
2529                                             pr->tile_width, pr->tile_height,
2530                                             &src_x, &src_y);
2531                 pr_tile_region_map_orientation(pr, x, y,
2532                                             pr->tile_width, pr->tile_height,
2533                                             w, h,
2534                                             &pb_x, &pb_y,
2535                                             &pb_w, &pb_h);
2536
2537                 if (has_alpha)
2538                         {
2539                         gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2540                                          (double) 0.0 - src_x,
2541                                          (double) 0.0 - src_y,
2542                                          1.0, 1.0, GDK_INTERP_NEAREST,
2543                                          255, it->x + pb_x, it->y + pb_y,
2544                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2545                         pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2546                         draw = TRUE;
2547                         }
2548                 else
2549                         {
2550
2551
2552                         if (pr->orientation == EXIF_ORIENTATION_TOP_LEFT && !(pr->func_post_process && !(pr->post_process_slow && fast)))
2553                                 {
2554                                 /* faster, simple, base orientation, no postprocessing */
2555                                 gdk_draw_pixbuf(it->pixmap,
2556                                                 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2557                                                 pr->pixbuf,
2558                                                 it->x + x, it->y + y,
2559                                                 x, y,
2560                                                 w, h,
2561                                                 pr->dither_quality, it->x + x, it->y + y);
2562                                 }
2563                         else
2564                                 {
2565                                 gdk_pixbuf_copy_area(pr->pixbuf,
2566                                                      src_x + pb_x, src_y + pb_y,
2567                                                      pb_w, pb_h,
2568                                                      it->pixbuf,
2569                                                      pb_x, pb_y);
2570                                 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2571                                 draw = TRUE;
2572                                 }
2573                         }
2574                 }
2575         else
2576                 {
2577                 double scale_x, scale_y;
2578                 double src_x, src_y;
2579                 gint pb_x, pb_y;
2580                 gint pb_w, pb_h;
2581
2582                 if (pr->image_width == 0 || pr->image_height == 0) return;
2583
2584                 scale_x = (double)pr->width / pr->image_width;
2585                 scale_y = (double)pr->height / pr->image_height;
2586
2587                 pr_tile_coords_map_orientation(pr, it->x / scale_x, it->y /scale_y ,
2588                                             pr->image_width, pr->image_height,
2589                                             pr->tile_width / scale_x , pr->tile_height / scale_y,
2590                                             &src_x, &src_y);
2591                 pr_tile_region_map_orientation(pr, x, y,
2592                                             pr->tile_width, pr->tile_height,
2593                                             w, h,
2594                                             &pb_x, &pb_y,
2595                                             &pb_w, &pb_h);
2596
2597                 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
2598                  * small sizes for anything but GDK_INTERP_NEAREST
2599                  */
2600                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
2601
2602                 if (!has_alpha)
2603                         {
2604                         gdk_pixbuf_scale(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2605                                          (double) 0.0 - src_x * scale_x,
2606                                          (double) 0.0 - src_y * scale_y,
2607                                          scale_x, scale_y,
2608                                          (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
2609                         }
2610                 else
2611                         {
2612                         gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2613                                          (double) 0.0 - src_x * scale_x,
2614                                          (double) 0.0 - src_y * scale_y,
2615                                          scale_x, scale_y,
2616                                          (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
2617                                          255, it->x + pb_x, it->y + pb_y,
2618                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2619                         }
2620                 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2621                 draw = TRUE;
2622                 }
2623
2624         if (draw && it->pixbuf && !it->blank)
2625                 {
2626
2627                 if (pr->func_post_process && !(pr->post_process_slow && fast))
2628                         pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
2629
2630                 gdk_draw_pixbuf(it->pixmap,
2631                                 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2632                                 it->pixbuf,
2633                                 x, y,
2634                                 x, y,
2635                                 w, h,
2636                                 pr->dither_quality, it->x + x, it->y + y);
2637                 }
2638
2639 #if 0
2640         /* enable this line for debugging the edges of tiles */
2641         gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2642                            FALSE, 0, 0, it->w, it->h);
2643         gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2644                            FALSE, x, y, w, h);
2645 #endif
2646 }
2647
2648
2649 static void pr_tile_expose(PixbufRenderer *pr, ImageTile *it,
2650                            gint x, gint y, gint w, gint h,
2651                            gint new_data, gint fast)
2652 {
2653         GtkWidget *box;
2654
2655         pr_tile_render(pr, it, x, y, w, h, new_data, fast);
2656
2657         box = GTK_WIDGET(pr);
2658
2659         gdk_draw_drawable(box->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
2660                           it->pixmap, x, y,
2661                           pr->x_offset + (it->x - pr->x_scroll) + x, pr->y_offset + (it->y - pr->y_scroll) + y, w, h);
2662
2663         if (pr->overlay_list)
2664                 {
2665                 pr_overlay_draw(pr, pr->x_offset + (it->x - pr->x_scroll) + x,
2666                                 pr->y_offset + (it->y - pr->y_scroll) + y,
2667                                 w, h,
2668                                 it);
2669                 }
2670 }
2671
2672
2673 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it)
2674 {
2675         return (it->x + it->w >= pr->x_scroll && it->x < pr->x_scroll + pr->vis_width &&
2676                 it->y + it->h >= pr->y_scroll && it->y < pr->y_scroll + pr->vis_height);
2677 }
2678
2679 /*
2680  *-------------------------------------------------------------------
2681  * draw queue
2682  *-------------------------------------------------------------------
2683  */
2684
2685 static gint pr_queue_draw_idle_cb(gpointer data)
2686 {
2687         PixbufRenderer *pr = data;
2688         QueueData *qd;
2689         gint fast;
2690
2691         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2692             (!pr->draw_queue && !pr->draw_queue_2pass) ||
2693             pr->draw_idle_id == -1)
2694                 {
2695                 pr_render_complete_signal(pr);
2696
2697                 pr->draw_idle_id = -1;
2698                 return FALSE;
2699                 }
2700
2701         if (pr->draw_queue)
2702                 {
2703                 qd = pr->draw_queue->data;
2704                 fast = ((pr->zoom_2pass && pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow);
2705                 }
2706         else
2707                 {
2708                 if (pr->loading)
2709                         {
2710                         /* still loading, wait till done (also drops the higher priority) */
2711
2712                         pr->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
2713                                                            pr_queue_draw_idle_cb, pr, NULL);
2714                         pr->draw_idle_high = FALSE;
2715                         return FALSE;
2716                         }
2717
2718                 qd = pr->draw_queue_2pass->data;
2719                 fast = FALSE;
2720                 }
2721
2722         if (GTK_WIDGET_REALIZED(pr))
2723                 {
2724                 if (pr_tile_is_visible(pr, qd->it))
2725                         {
2726                         pr_tile_expose(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2727                         }
2728                 else if (qd->new_data)
2729                         {
2730                         /* if new pixel data, and we already have a pixmap, update the tile */
2731                         qd->it->blank = FALSE;
2732                         if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL)
2733                                 {
2734                                 pr_tile_render(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2735                                 }
2736                         }
2737                 }
2738
2739         if (pr->draw_queue)
2740                 {
2741                 qd->it->qd = NULL;
2742                 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
2743                 if (fast)
2744                         {
2745                         if (qd->it->qd2)
2746                                 {
2747                                 pr_queue_merge(qd->it->qd2, qd);
2748                                 g_free(qd);
2749                                 }
2750                         else
2751                                 {
2752                                 qd->it->qd2 = qd;
2753                                 pr->draw_queue_2pass = g_list_append(pr->draw_queue_2pass, qd);
2754                                 }
2755                         }
2756                 else
2757                         {
2758                         g_free(qd);
2759                         }
2760                 }
2761         else
2762                 {
2763                 qd->it->qd2 = NULL;
2764                 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
2765                 g_free(qd);
2766                 }
2767
2768         if (!pr->draw_queue && !pr->draw_queue_2pass)
2769                 {
2770                 pr_render_complete_signal(pr);
2771
2772                 pr->draw_idle_id = -1;
2773                 return FALSE;
2774                 }
2775
2776         return TRUE;
2777 }
2778
2779 static void pr_queue_list_free(GList *list)
2780 {
2781         GList *work;
2782
2783         work = list;
2784         while (work)
2785                 {
2786                 QueueData *qd;
2787
2788                 qd = work->data;
2789                 work = work->next;
2790
2791                 qd->it->qd = NULL;
2792                 qd->it->qd2 = NULL;
2793                 g_free(qd);
2794                 }
2795
2796         g_list_free(list);
2797 }
2798
2799 static void pr_queue_clear(PixbufRenderer *pr)
2800 {
2801         pr_queue_list_free(pr->draw_queue);
2802         pr->draw_queue = NULL;
2803
2804         pr_queue_list_free(pr->draw_queue_2pass);
2805         pr->draw_queue_2pass = NULL;
2806
2807         if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2808         pr->draw_idle_id = -1;
2809 }
2810
2811 static void pr_queue_merge(QueueData *parent, QueueData *qd)
2812 {
2813         if (parent->x + parent->w < qd->x + qd->w)
2814                 {
2815                 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
2816                 }
2817         if (parent->x > qd->x)
2818                 {
2819                 parent->w += parent->x - qd->x;
2820                 parent->x = qd->x;
2821                 }
2822
2823         if (parent->y + parent->h < qd->y + qd->h)
2824                 {
2825                 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
2826                 }
2827         if (parent->y > qd->y)
2828                 {
2829                 parent->h += parent->y - qd->y;
2830                 parent->y = qd->y;
2831                 }
2832
2833         parent->new_data |= qd->new_data;
2834 }
2835
2836 static gint pr_clamp_to_visible(PixbufRenderer *pr, gint *x, gint *y, gint *w, gint *h)
2837 {
2838         gint nx, ny;
2839         gint nw, nh;
2840         gint vx, vy;
2841         gint vw, vh;
2842
2843         vw = pr->vis_width;
2844         vh = pr->vis_height;
2845
2846         vx = pr->x_scroll;
2847         vy = pr->y_scroll;
2848
2849         if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
2850
2851         /* now clamp it */
2852         nx = CLAMP(*x, vx, vx + vw);
2853         nw = CLAMP(*w - (nx - *x), 1, vw);
2854
2855         ny = CLAMP(*y, vy, vy + vh);
2856         nh = CLAMP(*h - (ny - *y), 1, vh);
2857
2858         *x = nx;
2859         *y = ny;
2860         *w = nw;
2861         *h = nh;
2862
2863         return TRUE;
2864 }
2865
2866 static gint pr_queue_to_tiles(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2867                               gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2868 {
2869         gint i, j;
2870         gint x1, x2;
2871         gint y1, y2;
2872
2873         if (clamp && !pr_clamp_to_visible(pr, &x, &y, &w, &h)) return FALSE;
2874
2875         x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
2876         x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
2877
2878         y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
2879         y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
2880
2881         for (j = y1; j <= y2; j += pr->tile_height)
2882                 {
2883                 for (i = x1; i <= x2; i += pr->tile_width)
2884                         {
2885                         ImageTile *it;
2886
2887                         it = pr_tile_get(pr, i, j,
2888                                          (only_existing &&
2889                                           (i + pr->tile_width < pr->x_scroll ||
2890                                            i > pr->x_scroll + pr->vis_width ||
2891                                            j + pr->tile_height < pr->y_scroll ||
2892                                            j > pr->y_scroll + pr->vis_height)));
2893                         if (it)
2894                                 {
2895                                 QueueData *qd;
2896
2897                                 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
2898                                     (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
2899                                         {
2900                                         it->render_todo = render;
2901                                         }
2902
2903                                 qd = g_new(QueueData, 1);
2904                                 qd->it = it;
2905                                 qd->new_data = new_data;
2906
2907                                 if (i < x)
2908                                         {
2909                                         qd->x = x - i;
2910                                         }
2911                                 else
2912                                         {
2913                                         qd->x = 0;
2914                                         }
2915                                 qd->w = x + w - i - qd->x;
2916                                 if (qd->x + qd->w > pr->tile_width) qd->w = pr->tile_width - qd->x;
2917
2918                                 if (j < y)
2919                                         {
2920                                         qd->y = y - j;
2921                                         }
2922                                 else
2923                                         {
2924                                         qd->y = 0;
2925                                         }
2926                                 qd->h = y + h - j - qd->y;
2927                                 if (qd->y + qd->h > pr->tile_height) qd->h = pr->tile_height - qd->y;
2928
2929                                 if (qd->w < 1 || qd->h < 1)
2930                                         {
2931                                         g_free(qd);
2932                                         }
2933                                 else if (it->qd)
2934                                         {
2935                                         pr_queue_merge(it->qd, qd);
2936                                         g_free(qd);
2937                                         }
2938                                 else
2939                                         {
2940                                         it->qd = qd;
2941                                         pr->draw_queue = g_list_append(pr->draw_queue, qd);
2942                                         }
2943                                 }
2944                         }
2945                 }
2946
2947         return TRUE;
2948 }
2949
2950 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2951                      gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2952 {
2953         gint nx, ny;
2954
2955         nx = CLAMP(x, 0, pr->width - 1);
2956         ny = CLAMP(y, 0, pr->height - 1);
2957         w -= (nx - x);
2958         h -= (ny - y);
2959         w = CLAMP(w, 0, pr->width - nx);
2960         h = CLAMP(h, 0, pr->height - ny);
2961         if (w < 1 || h < 1) return;
2962
2963         if (pr_queue_to_tiles(pr, nx, ny, w, h, clamp, render, new_data, only_existing) &&
2964             ((!pr->draw_queue && !pr->draw_queue_2pass) || pr->draw_idle_id == -1 || !pr->draw_idle_high))
2965                 {
2966                 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2967                 pr->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW,
2968                                                    pr_queue_draw_idle_cb, pr, NULL);
2969                 pr->draw_idle_high = TRUE;
2970                 }
2971 }
2972
2973 static void pr_redraw(PixbufRenderer *pr, gint new_data)
2974 {
2975         pr_queue_clear(pr);
2976         pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data, FALSE);
2977 }
2978
2979 /*
2980  *-------------------------------------------------------------------
2981  * signal emission
2982  *-------------------------------------------------------------------
2983  */
2984
2985 static void pr_update_signal(PixbufRenderer *pr)
2986 {
2987 #if 0
2988         printf("FIXME: send updated signal\n");
2989 #endif
2990         if (debug) printf("%s pixbuf renderer updated - started drawing %p\n", get_exec_time(), pr);
2991         pr->debug_updated = TRUE;
2992 }
2993
2994 static void pr_zoom_signal(PixbufRenderer *pr)
2995 {
2996         g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
2997 }
2998
2999 static void pr_clicked_signal(PixbufRenderer *pr, GdkEventButton *bevent)
3000 {
3001         g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
3002 }
3003
3004 static void pr_scroll_notify_signal(PixbufRenderer *pr)
3005 {
3006         g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
3007 }
3008
3009 static void pr_render_complete_signal(PixbufRenderer *pr)
3010 {
3011         if (!pr->complete)
3012                 {
3013                 g_signal_emit(pr, signals[SIGNAL_RENDER_COMPLETE], 0);
3014                 g_object_set(G_OBJECT(pr), "complete", TRUE, NULL);
3015                 }
3016         if (pr->debug_updated)
3017                 {
3018                 if (debug) printf("%s pixbuf renderer done %p\n", get_exec_time(), pr);
3019                 pr->debug_updated = FALSE;
3020                 }
3021 }
3022
3023 static void pr_drag_signal(PixbufRenderer *pr, GdkEventButton *bevent)
3024 {
3025         g_signal_emit(pr, signals[SIGNAL_DRAG], 0, bevent);
3026 }
3027
3028 /*
3029  *-------------------------------------------------------------------
3030  * sync and clamp
3031  *-------------------------------------------------------------------
3032  */
3033
3034 static gint pr_scroll_clamp(PixbufRenderer *pr)
3035 {
3036         gint old_xs;
3037         gint old_ys;
3038
3039         if (pr->zoom == 0.0)
3040                 {
3041                 pr->x_scroll = 0;
3042                 pr->y_scroll = 0;
3043
3044                 return FALSE;
3045                 }
3046
3047         old_xs = pr->x_scroll;
3048         old_ys = pr->y_scroll;
3049
3050         if (pr->x_offset > 0)
3051                 {
3052                 pr->x_scroll = 0;
3053                 }
3054         else
3055                 {
3056                 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
3057                 }
3058
3059         if (pr->y_offset > 0)
3060                 {
3061                 pr->y_scroll = 0;
3062                 }
3063         else
3064                 {
3065                 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
3066                 }
3067
3068         return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
3069 }
3070
3071 static gint pr_size_clamp(PixbufRenderer *pr)
3072 {
3073         gint old_vw, old_vh;
3074
3075         old_vw = pr->vis_width;
3076         old_vh = pr->vis_height;
3077
3078         if (pr->width < pr->window_width)
3079                 {
3080                 pr->vis_width = pr->width;
3081                 pr->x_offset = (pr->window_width - pr->width) / 2;
3082                 }
3083         else
3084                 {
3085                 pr->vis_width = pr->window_width;
3086                 pr->x_offset = 0;
3087                 }
3088
3089         if (pr->height < pr->window_height)
3090                 {
3091                 pr->vis_height = pr->height;
3092                 pr->y_offset = (pr->window_height - pr->height) / 2;
3093                 }
3094         else
3095                 {
3096                 pr->vis_height = pr->window_height;
3097                 pr->y_offset = 0;
3098                 }
3099
3100         return (old_vw != pr->vis_width || old_vh != pr->vis_height);
3101 }
3102
3103 static gint pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom,
3104                           gint force, gint new, gint invalidate,
3105                           gint *redrawn)
3106 {
3107         gint w, h;
3108         gdouble scale;
3109         gint invalid;
3110
3111         zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
3112
3113         if (pr->zoom == zoom && !force) return FALSE;
3114
3115         w = pr->image_width;
3116         h = pr->image_height;
3117
3118         if (zoom == 0.0 && !pr->pixbuf)
3119                 {
3120                 scale = 1.0;
3121                 }
3122         else if (zoom == 0.0)
3123                 {
3124                 gint max_w;
3125                 gint max_h;
3126                 gint sizeable;
3127
3128                 sizeable = (new && pr_parent_window_sizable(pr));
3129
3130                 if (sizeable)
3131                         {
3132                         max_w = gdk_screen_width();
3133                         max_h = gdk_screen_height();
3134
3135                         if (pr->window_limit)
3136                                 {
3137                                 max_w = max_w * pr->window_limit_size / 100;
3138                                 max_h = max_h * pr->window_limit_size / 100;
3139                                 }
3140                         }
3141                 else
3142                         {
3143                         max_w = pr->window_width;
3144                         max_h = pr->window_height;
3145                         }
3146
3147                 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
3148                         {
3149                         if ((gdouble)max_w / w > (gdouble)max_h / h)
3150                                 {
3151                                 scale = (gdouble)max_h / h;
3152                                 h = max_h;
3153                                 w = w * scale + 0.5;
3154                                 if (w > max_w) w = max_w;
3155                                 }
3156                         else
3157                                 {
3158                                 scale = (gdouble)max_w / w;
3159                                 w = max_w;
3160                                 h = h * scale + 0.5;
3161                                 if (h > max_h) h = max_h;
3162                                 }
3163
3164                         if (pr->autofit_limit)
3165                                 {
3166                                 gdouble factor = (gdouble)pr->autofit_limit_size / 100;
3167                                 w = w * factor + 0.5;
3168                                 h = h * factor + 0.5;
3169                                 scale = scale * factor;
3170                                 }
3171
3172                         if (w < 1) w = 1;
3173                         if (h < 1) h = 1;
3174                         }
3175                 else
3176                         {
3177                         scale = 1.0;
3178                         }
3179                 }
3180         else if (zoom > 0.0) /* zoom orig, in */
3181                 {
3182                 scale = zoom;
3183                 w = w * scale;
3184                 h = h * scale;
3185                 }
3186         else /* zoom out */
3187                 {
3188                 scale = 1.0 / (0.0 - zoom);
3189                 w = w * scale;
3190                 h = h * scale;
3191                 }
3192
3193         invalid = (pr->width != w || pr->height != h);
3194
3195         pr->zoom = zoom;
3196         pr->width = w;
3197         pr->height = h;
3198         pr->scale = scale;
3199
3200         if (invalidate || invalid)
3201                 {
3202                 pr_tile_invalidate_all(pr);
3203                 pr_redraw(pr, TRUE);
3204                 }
3205         if (redrawn) *redrawn = (invalidate || invalid);
3206
3207         return TRUE;
3208 }
3209
3210 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
3211                          gint force, gint new,
3212                          gint center_point, gint px, gint py)
3213 {
3214         gdouble old_scale;
3215         gint old_cx, old_cy;
3216         gint clamped;
3217         gint sized;
3218         gint redrawn = FALSE;
3219
3220         old_scale = pr->scale;
3221         if (center_point)
3222                 {
3223                 px = CLAMP(px, 0, pr->width);
3224                 py = CLAMP(py, 0, pr->height);
3225                 old_cx = pr->x_scroll + (px - pr->x_offset);
3226                 old_cy = pr->y_scroll + (py - pr->y_offset);
3227                 }
3228         else
3229                 {
3230                 px = py = 0;
3231                 old_cx = pr->x_scroll + pr->vis_width / 2;
3232                 old_cy = pr->y_scroll + pr->vis_height / 2;
3233                 }
3234
3235         if (!pr_zoom_clamp(pr, zoom, force, new, force, &redrawn)) return;
3236
3237         clamped = pr_size_clamp(pr);
3238         sized = pr_parent_window_resize(pr, pr->width, pr->height);
3239
3240         if (force && new)
3241                 {
3242                 switch (pr->scroll_reset)
3243                         {
3244                         case PR_SCROLL_RESET_NOCHANGE:
3245                                 /* maintain old scroll position, do nothing */
3246                                 break;
3247                         case PR_SCROLL_RESET_CENTER:
3248                                 /* center new image */
3249                                 pr->x_scroll = ((double)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2;
3250                                 pr->y_scroll = ((double)pr->image_height / 2.0 * pr->scale) - pr->vis_height / 2;
3251                                 break;
3252                         case PR_SCROLL_RESET_TOPLEFT:
3253                         default:
3254                                 /* reset to upper left */
3255                                 pr->x_scroll = 0;
3256                                 pr->y_scroll = 0;
3257                                 break;
3258                         }
3259                 }
3260         else
3261                 {
3262                 /* user zoom does not force, so keep visible center point */
3263                 if (center_point)
3264                         {
3265                         pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
3266                         pr->y_scroll = old_cy / old_scale * pr->scale - (py - pr->y_offset);
3267                         }
3268                 else
3269                         {
3270                         pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2);
3271                         pr->y_scroll = old_cy / old_scale * pr->scale - (pr->vis_height / 2);
3272                         }
3273                 }
3274
3275         pr_scroll_clamp(pr);
3276
3277         /* If the window was not sized, redraw the image - we know there will be no size/expose signal.
3278          * But even if a size is claimed, there is no guarantee that the window manager will allow it,
3279          * so redraw the window anyway :/
3280          */
3281         if (sized || clamped) pr_border_clear(pr);
3282         pr_redraw(pr, redrawn);
3283
3284         pr_scroll_notify_signal(pr);
3285         pr_zoom_signal(pr);
3286         pr_update_signal(pr);
3287 }
3288
3289 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
3290 {
3291         gint zoom_changed = FALSE;
3292
3293         if (pr->window_width == new_width && pr->window_height == new_height) return;
3294
3295         pr->window_width = new_width;
3296         pr->window_height = new_height;
3297
3298         if (pr->zoom == 0.0)
3299                 {
3300                 gdouble old_scale = pr->scale;
3301                 pr_zoom_clamp(pr, 0.0, TRUE, FALSE, FALSE, NULL);
3302                 zoom_changed = (old_scale != pr->scale);
3303                 }
3304
3305         pr_size_clamp(pr);
3306         pr_scroll_clamp(pr);
3307
3308         pr_overlay_update_sizes(pr);
3309
3310         /* ensure scroller remains visible */
3311         if (pr->scroller_overlay != -1)
3312                 {
3313                 gint update = FALSE;
3314
3315                 if (pr->scroller_x > new_width)
3316                         {
3317                         pr->scroller_x = new_width;
3318                         pr->scroller_xpos = new_width;
3319                         update = TRUE;
3320                         }
3321                 if (pr->scroller_y > new_height)
3322                         {
3323                         pr->scroller_y = new_height;
3324                         pr->scroller_ypos = new_height;
3325                         update = TRUE;
3326                         }
3327
3328                 if (update)
3329                         {
3330                         GdkPixbuf *pixbuf;
3331
3332                         if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
3333                                 {
3334                                 gint w, h;
3335
3336                                 w = gdk_pixbuf_get_width(pixbuf);
3337                                 h = gdk_pixbuf_get_height(pixbuf);
3338                                 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
3339                                                             pr->scroller_x - w / 2, pr->scroller_y - h / 2);
3340                                 }
3341                         }
3342                 }
3343
3344         pr_border_clear(pr);
3345
3346         pr_scroll_notify_signal(pr);
3347         if (zoom_changed) pr_zoom_signal(pr);
3348         pr_update_signal(pr);
3349 }
3350
3351 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
3352 {
3353         PixbufRenderer *pr = data;
3354
3355         pr_size_sync(pr, allocation->width, allocation->height);
3356 }
3357
3358 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
3359 {
3360         gint x, y;
3361
3362         pr_border_draw(pr, area->x, area->y, area->width, area->height);
3363
3364         x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
3365         y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
3366
3367         pr_queue(pr, x, y,
3368                  MIN((gint)area->width, pr->width - x),
3369                  MIN((gint)area->height, pr->height - y),
3370                  FALSE, TILE_RENDER_ALL, FALSE, FALSE);
3371 }
3372
3373 /*
3374  *-------------------------------------------------------------------
3375  * scrolling
3376  *-------------------------------------------------------------------
3377  */
3378
3379 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
3380 {
3381         gint old_x, old_y;
3382         gint x_off, y_off;
3383         gint w, h;
3384
3385         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3386
3387         if (!pr->pixbuf && !pr->source_tiles_enabled) return;
3388
3389         old_x = pr->x_scroll;
3390         old_y = pr->y_scroll;
3391
3392         pr->x_scroll += x;
3393         pr->y_scroll += y;
3394
3395         pr_scroll_clamp(pr);
3396         if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
3397
3398         pr_scroll_notify_signal(pr);
3399
3400         x_off = pr->x_scroll - old_x;
3401         y_off = pr->y_scroll - old_y;
3402
3403         w = pr->vis_width - abs(x_off);
3404         h = pr->vis_height - abs(y_off);
3405
3406         if (w < 1 || h < 1)
3407                 {
3408                 /* scrolled completely to new material */
3409                 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3410                 return;
3411                 }
3412         else
3413                 {
3414                 gint x1, y1;
3415                 gint x2, y2;
3416                 GtkWidget *box;
3417                 GdkGC *gc;
3418                 GdkEvent *event;
3419
3420                 if (x_off < 0)
3421                         {
3422                         x1 = abs(x_off);
3423                         x2 = 0;
3424                         }
3425                 else
3426                         {
3427                         x1 = 0;
3428                         x2 = abs(x_off);
3429                         }
3430
3431                 if (y_off < 0)
3432                         {
3433                         y1 = abs(y_off);
3434                         y2 = 0;
3435                         }
3436                 else
3437                         {
3438                         y1 = 0;
3439                         y2 = abs(y_off);
3440                         }
3441
3442                 box = GTK_WIDGET(pr);
3443
3444                 gc = gdk_gc_new(box->window);
3445                 gdk_gc_set_exposures(gc, TRUE);
3446                 gdk_draw_drawable(box->window, gc,
3447                                   box->window,
3448                                   x2 + pr->x_offset, y2 + pr->y_offset,
3449                                   x1 + pr->x_offset, y1 + pr->y_offset, w, h);
3450                 g_object_unref(gc);
3451
3452                 if (pr->overlay_list)
3453                         {
3454                         pr_overlay_queue_all(pr);
3455                         }
3456
3457                 w = pr->vis_width - w;
3458                 h = pr->vis_height - h;
3459
3460                 if (w > 0)
3461                         {
3462                         pr_queue(pr,
3463                                  x_off > 0 ? pr->x_scroll + (pr->vis_width - w) : pr->x_scroll, pr->y_scroll,
3464                                  w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3465                         }
3466                 if (h > 0)
3467                         {
3468                         /* FIXME, to optimize this, remove overlap */
3469                         pr_queue(pr,
3470                                  pr->x_scroll, y_off > 0 ? pr->y_scroll + (pr->vis_height - h) : pr->y_scroll,
3471                                  pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3472                         }
3473
3474                 /* process exposures here, "expose_event" seems to miss a few with obstructed windows */
3475                 while ((event = gdk_event_get_graphics_expose(box->window)) != NULL)
3476                         {
3477                         pixbuf_renderer_paint(pr, &event->expose.area);
3478
3479                         if (event->expose.count == 0)
3480                                 {
3481                                 gdk_event_free(event);
3482                                 break;
3483                                 }
3484                         gdk_event_free(event);
3485                         }
3486                 }
3487 }
3488
3489 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
3490                                      gdouble x_align, gdouble y_align)
3491 {
3492         gint px, py;
3493         gint ax, ay;
3494
3495         x_align = CLAMP(x_align, 0.0, 1.0);
3496         y_align = CLAMP(y_align, 0.0, 1.0);
3497
3498         ax = (gdouble)pr->vis_width * x_align;
3499         ay = (gdouble)pr->vis_height * y_align;
3500
3501         px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
3502         py = (gdouble)y * pr->scale - (pr->y_scroll + ay);
3503
3504         pixbuf_renderer_scroll(pr, px, py);
3505 }
3506
3507 /* get or set coordinates of viewport center in the image, in range 0.0 - 1.0 */
3508
3509 void pixbuf_renderer_get_scroll_center(PixbufRenderer *pr, gdouble *x, gdouble *y)
3510 {
3511         gint src_x, src_y;
3512
3513         src_x = pr->x_scroll + pr->vis_width / 2;
3514         src_y = pr->y_scroll + pr->vis_height / 2;
3515
3516         *x = (gdouble)src_x / pr->width;
3517         *y = (gdouble)src_y / pr->height;
3518 }
3519
3520 void pixbuf_renderer_set_scroll_center(PixbufRenderer *pr, gdouble x, gdouble y)
3521 {
3522         gdouble dst_x, dst_y;
3523
3524         dst_x = x * pr->width  - pr->vis_width  / 2 - pr->x_scroll + CLAMP(pr->subpixel_x_scroll, -1.0, 1.0);
3525         dst_y = y * pr->height - pr->vis_height / 2 - pr->y_scroll + CLAMP(pr->subpixel_y_scroll, -1.0, 1.0);
3526
3527         pr->subpixel_x_scroll = dst_x - (int)dst_x;
3528         pr->subpixel_y_scroll = dst_y - (int)dst_y;
3529
3530         pixbuf_renderer_scroll(pr, (int)dst_x, (int)dst_y);
3531 }
3532
3533
3534 /*
3535  *-------------------------------------------------------------------
3536  * mouse
3537  *-------------------------------------------------------------------
3538  */
3539
3540 static gint pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3541 {
3542         PixbufRenderer *pr;
3543         gint accel;
3544
3545         pr = PIXBUF_RENDERER(widget);
3546
3547         if (pr->scroller_id != -1)
3548                 {
3549                 pr->scroller_xpos = bevent->x;
3550                 pr->scroller_ypos = bevent->y;
3551                 }
3552
3553         if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
3554
3555         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3556                 {
3557                 pr->drag_moved++;
3558                 }
3559         else
3560                 {
3561                 widget_set_cursor(widget, GDK_FLEUR);
3562                 }
3563
3564         if (bevent->state & GDK_SHIFT_MASK)
3565                 {
3566                 accel = PR_PAN_SHIFT_MULTIPLIER;
3567                 }
3568         else
3569                 {
3570                 accel = 1;
3571                 }
3572
3573         /* do the scroll */
3574         pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
3575                                (pr->drag_last_y - bevent->y) * accel);
3576
3577         pr_drag_signal(pr, bevent);
3578
3579         pr->drag_last_x = bevent->x;
3580         pr->drag_last_y = bevent->y;
3581
3582         return FALSE;
3583 }
3584
3585 static gint pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3586 {
3587         PixbufRenderer *pr;
3588         GtkWidget *parent;
3589
3590         pr = PIXBUF_RENDERER(widget);
3591
3592         if (pr->scroller_id != -1) return TRUE;
3593
3594         switch (bevent->button)
3595                 {
3596                 case 1:
3597                         pr->in_drag = TRUE;
3598                         pr->drag_last_x = bevent->x;
3599                         pr->drag_last_y = bevent->y;
3600                         pr->drag_moved = 0;
3601                         gdk_pointer_grab(widget->window, FALSE,
3602                                          GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3603                                          NULL, NULL, bevent->time);
3604                         gtk_grab_add(widget);
3605                         break;
3606                 case 2:
3607                         pr->drag_moved = 0;
3608                         break;
3609                 case 3:
3610                         pr_clicked_signal(pr, bevent);
3611                         break;
3612                 default:
3613                         break;
3614                 }
3615
3616         parent = gtk_widget_get_parent(widget);
3617         if (widget && GTK_WIDGET_CAN_FOCUS(parent))
3618                 {
3619                 gtk_widget_grab_focus(parent);
3620                 }
3621
3622         return FALSE;
3623 }
3624
3625 static gint pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3626 {
3627         PixbufRenderer *pr;
3628
3629         pr = PIXBUF_RENDERER(widget);
3630
3631         if (pr->scroller_id != -1)
3632                 {
3633                 pr_scroller_stop(pr);
3634                 return TRUE;
3635                 }
3636
3637         if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
3638                 {
3639                 gtk_grab_remove(widget);
3640                 gdk_pointer_ungrab(bevent->time);
3641                 widget_set_cursor(widget, -1);
3642                 }
3643
3644         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3645                 {
3646                 if (bevent->button == 1 && (bevent->state & GDK_SHIFT_MASK))
3647                         {
3648                         pr_scroller_start(pr, bevent->x, bevent->y);
3649                         }
3650                 else if (bevent->button == 1 || bevent->button == 2)
3651                         {
3652                         pr_clicked_signal(pr, bevent);
3653                         }
3654                 }
3655
3656         pr->in_drag = FALSE;
3657
3658         return FALSE;
3659 }
3660
3661 static gint pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
3662 {
3663         PixbufRenderer *pr;
3664
3665         pr = PIXBUF_RENDERER(widget);
3666
3667         if (pr->scroller_id != -1)
3668                 {
3669                 pr->scroller_xpos = pr->scroller_x;
3670                 pr->scroller_ypos = pr->scroller_y;
3671                 pr->scroller_xinc = 0;
3672                 pr->scroller_yinc = 0;
3673                 }
3674
3675         return FALSE;
3676 }
3677
3678 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
3679 {
3680         PixbufRenderer *pr;
3681
3682         pr = PIXBUF_RENDERER(widget);
3683
3684         pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
3685 }
3686
3687 static void pr_signals_connect(PixbufRenderer *pr)
3688 {
3689         g_signal_connect(G_OBJECT(pr), "motion_notify_event",
3690                          G_CALLBACK(pr_mouse_motion_cb), pr);
3691         g_signal_connect(G_OBJECT(pr), "button_press_event",
3692                          G_CALLBACK(pr_mouse_press_cb), pr);
3693         g_signal_connect(G_OBJECT(pr), "button_release_event",
3694                          G_CALLBACK(pr_mouse_release_cb), pr);
3695         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
3696                          G_CALLBACK(pr_mouse_leave_cb), pr);
3697
3698         gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK |
3699                                               GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
3700                                               GDK_LEAVE_NOTIFY_MASK);
3701
3702         g_signal_connect(G_OBJECT(pr), "drag_begin",
3703                          G_CALLBACK(pr_mouse_drag_cb), pr);
3704
3705 }
3706
3707 /*
3708  *-------------------------------------------------------------------
3709  * public
3710  *-------------------------------------------------------------------
3711  */
3712 static void pr_pixbuf_size_sync(PixbufRenderer *pr)
3713 {
3714         if (!pr->pixbuf) return;
3715         switch (pr->orientation)
3716                 {
3717                 case EXIF_ORIENTATION_LEFT_TOP:
3718                 case EXIF_ORIENTATION_RIGHT_TOP:
3719                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
3720                 case EXIF_ORIENTATION_LEFT_BOTTOM:
3721                         pr->image_width = gdk_pixbuf_get_height(pr->pixbuf);
3722                         pr->image_height = gdk_pixbuf_get_width(pr->pixbuf);
3723                         break;
3724                 default:
3725                         pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
3726                         pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
3727                 }
3728 }
3729
3730 static void pr_pixbuf_sync(PixbufRenderer *pr, gdouble zoom)
3731 {
3732         if (!pr->pixbuf)
3733                 {
3734                 GtkWidget *box;
3735
3736                 /* no pixbuf so just clear the window */
3737                 pr->image_width = 0;
3738                 pr->image_height = 0;
3739                 pr->scale = 1.0;
3740
3741                 box = GTK_WIDGET(pr);
3742
3743                 if (GTK_WIDGET_REALIZED(box))
3744                         {
3745                         gdk_window_clear(box->window);
3746                         pr_overlay_draw(pr, 0, 0, pr->window_width, pr->window_height, NULL);
3747                         }
3748
3749                 pr_update_signal(pr);
3750
3751                 return;
3752                 }
3753
3754         pr_pixbuf_size_sync(pr);
3755         pr_zoom_sync(pr, zoom, TRUE, TRUE, FALSE, 0, 0);
3756 }
3757
3758 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3759 {
3760         if (pixbuf) g_object_ref(pixbuf);
3761         if (pr->pixbuf) g_object_unref(pr->pixbuf);
3762         pr->pixbuf = pixbuf;
3763
3764         pr_pixbuf_sync(pr, zoom);
3765 }
3766
3767 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3768 {
3769         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3770
3771         pr_source_tile_unset(pr);
3772
3773         pr_set_pixbuf(pr, pixbuf, zoom);
3774
3775         pr_update_signal(pr);
3776 }
3777
3778 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
3779 {
3780         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
3781
3782         return pr->pixbuf;
3783 }
3784
3785 void pixbuf_renderer_set_orientation(PixbufRenderer *pr, gint orientation)
3786 {
3787         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3788
3789         pr->orientation = orientation;
3790
3791         pr_pixbuf_size_sync(pr);
3792         pr_zoom_sync(pr, pr->zoom, TRUE, FALSE, FALSE, 0, 0);
3793 }
3794
3795 gint pixbuf_renderer_get_orientation(PixbufRenderer *pr)
3796 {
3797         if (!pr) return 1;
3798         return pr->orientation;
3799 }
3800
3801 void pixbuf_renderer_set_post_process_func(PixbufRenderer *pr, PixbufRendererPostProcessFunc func, gpointer user_data, gint slow)
3802 {
3803         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3804
3805         pr->func_post_process = func;
3806         pr->post_process_user_data = user_data;
3807         pr->post_process_slow = func && slow;
3808
3809 }
3810
3811
3812 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
3813 {
3814         GObject *object;
3815         PixbufRendererScrollResetType scroll_reset;
3816
3817         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3818         g_return_if_fail(IS_PIXBUF_RENDERER(source));
3819
3820         if (pr == source) return;
3821
3822         object = G_OBJECT(pr);
3823
3824         g_object_set(object, "zoom_min", source->zoom_min, NULL);
3825         g_object_set(object, "zoom_max", source->zoom_max, NULL);
3826         g_object_set(object, "loading", source->loading, NULL);
3827
3828         pr->complete = source->complete;
3829
3830         pr->x_scroll = source->x_scroll;
3831         pr->y_scroll = source->y_scroll;
3832
3833         scroll_reset = pr->scroll_reset;
3834         pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
3835
3836         pr->func_post_process = source->func_post_process;
3837         pr->post_process_user_data = source->post_process_user_data;
3838         pr->post_process_slow = source->post_process_slow;
3839         pr->orientation = source->orientation;
3840
3841         if (source->source_tiles_enabled)
3842                 {
3843                 pr_source_tile_unset(pr);
3844
3845                 pr->source_tiles_enabled = source->source_tiles_enabled;
3846                 pr->source_tiles_cache_size = source->source_tiles_cache_size;
3847                 pr->source_tile_width = source->source_tile_width;
3848                 pr->source_tile_height = source->source_tile_height;
3849                 pr->image_width = source->image_width;
3850                 pr->image_height = source->image_height;
3851
3852                 pr->func_tile_request = source->func_tile_request;
3853                 pr->func_tile_dispose = source->func_tile_dispose;
3854                 pr->func_tile_data = source->func_tile_data;
3855
3856                 pr->source_tiles = source->source_tiles;
3857                 source->source_tiles = NULL;
3858
3859                 pr_zoom_sync(pr, source->zoom, TRUE, TRUE, FALSE, 0, 0);
3860                 pr_redraw(pr, TRUE);
3861                 }
3862         else
3863                 {
3864                 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
3865                 }
3866
3867         pr->scroll_reset = scroll_reset;
3868
3869         pixbuf_renderer_set_pixbuf(source, NULL, source->zoom);
3870         pr_queue_clear(source);
3871         pr_tile_free_all(source);
3872 }
3873
3874 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint src_x, gint src_y, gint src_w, gint src_h)
3875 {
3876         gint x, y, width, height,  x1, y1, x2, y2;
3877
3878         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3879
3880         pr_coords_map_orientation_reverse(pr,
3881                                      src_x, src_y,
3882                                      pr->image_width, pr->image_height,
3883                                      src_w, src_h,
3884                                      &x, &y,
3885                                      &width, &height);
3886
3887         if (pr->source_tiles_enabled)
3888                 {
3889                 pr_source_tile_changed(pr, x, y, width, height);
3890                 }
3891
3892         if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
3893                 {
3894                 /* increase region when using a zoom quality that may access surrounding pixels */
3895                 y -= 1;
3896                 height += 2;
3897                 }
3898
3899         x1 = (gint)floor((double)x * pr->scale);
3900         y1 = (gint)floor((double)y * pr->scale);
3901         x2 = (gint)ceil((double)(x + width) * pr->scale);
3902         y2 = (gint)ceil((double)(y + height) * pr->scale);
3903
3904         pr_queue(pr, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
3905 }
3906
3907 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
3908 {
3909         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3910
3911         pr_zoom_adjust_real(pr, increment, FALSE, 0, 0);
3912 }
3913
3914 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
3915 {
3916         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3917
3918         pr_zoom_adjust_real(pr, increment, TRUE, x, y);
3919 }
3920
3921 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
3922 {
3923         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3924
3925         pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, 0, 0);
3926 }
3927
3928 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
3929 {
3930         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3931
3932         return pr->zoom;
3933 }
3934
3935 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
3936 {
3937         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3938
3939         return pr->scale;
3940 }
3941
3942 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
3943 {
3944         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3945
3946         if (min > 1.0 || max < 1.0) return;
3947         if (min < 1.0 && min > -1.0) return;
3948         if (min < -200.0 || max > 200.0) return;
3949
3950         if (pr->zoom_min != min)
3951                 {
3952                 pr->zoom_min = min;
3953                 g_object_notify(G_OBJECT(pr), "zoom_min");
3954                 }
3955         if (pr->zoom_max != max)
3956                 {
3957                 pr->zoom_max = max;
3958                 g_object_notify(G_OBJECT(pr), "zoom_max");
3959                 }
3960 }
3961
3962 gint pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
3963 {
3964         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3965         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3966
3967         if (!pr->pixbuf && !pr->source_tiles_enabled)
3968                 {
3969                 *width = 0;
3970                 *height = 0;
3971                 return FALSE;
3972                 }
3973
3974         *width = pr->image_width;
3975         *height = pr->image_height;
3976         return TRUE;
3977 }
3978
3979 gint pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
3980 {
3981         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3982         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3983
3984         if (!pr->pixbuf && !pr->source_tiles_enabled)
3985                 {
3986                 *width = 0;
3987                 *height = 0;
3988                 return FALSE;
3989                 }
3990
3991         *width = pr->width;
3992         *height = pr->height;
3993         return TRUE;
3994 }
3995
3996 gint pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
3997 {
3998         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3999         g_return_val_if_fail(rect != NULL, FALSE);
4000
4001         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
4002             !pr->scale)
4003                 {
4004                 rect->x = 0;
4005                 rect->y = 0;
4006                 rect->width = 0;
4007                 rect->height = 0;
4008                 return FALSE;
4009                 }
4010
4011         rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
4012         rect->y = (gint)((gdouble)pr->y_scroll / pr->scale);
4013         rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
4014         rect->height = (gint)((gdouble)pr->vis_height / pr->scale);
4015         return TRUE;
4016 }
4017
4018 gint pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
4019 {
4020         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4021         g_return_val_if_fail(rect != NULL, FALSE);
4022
4023         if ((!pr->pixbuf && !pr->source_tiles_enabled))
4024                 {
4025                 rect->x = 0;
4026                 rect->y = 0;
4027                 rect->width = 0;
4028                 rect->height = 0;
4029                 return FALSE;
4030                 }
4031
4032         rect->x = pr->x_scroll;
4033         rect->y = pr->y_scroll;
4034         rect->width = pr->vis_width;
4035         rect->height = pr->vis_height;
4036         return TRUE;
4037 }