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