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