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