rename GQview -> Geeqie over the code
[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         style = gtk_style_copy(gtk_widget_get_style(widget));
1289         g_object_ref(G_OBJECT(style));
1290
1291         if (color)
1292                 {
1293                 GdkColor *slot;
1294
1295                 slot = &style->bg[GTK_STATE_NORMAL];
1296
1297                 slot->red = color->red;
1298                 slot->green = color->green;
1299                 slot->blue = color->blue;
1300                 }
1301
1302         gtk_widget_set_style(widget, style);
1303         g_object_unref(G_OBJECT(style));
1304
1305         if (GTK_WIDGET_VISIBLE(widget)) pr_border_clear(pr);
1306 }
1307
1308 void pixbuf_renderer_set_black(PixbufRenderer *pr, gint black)
1309 {
1310         GdkColor color = { 0, 0, 0, 0};
1311
1312         pixbuf_renderer_set_color(pr, &color);
1313 }
1314
1315
1316 /*
1317  *-------------------------------------------------------------------
1318  * source tiles
1319  *-------------------------------------------------------------------
1320  */
1321
1322 static void pr_source_tile_free(SourceTile *st)
1323 {
1324         if (!st) return;
1325
1326         if (st->pixbuf) g_object_unref(st->pixbuf);
1327         g_free(st);
1328 }
1329
1330 static void pr_source_tile_free_all(PixbufRenderer *pr)
1331 {
1332         GList *work;
1333
1334         work = pr->source_tiles;
1335         while (work)
1336                 {
1337                 SourceTile *st;
1338
1339                 st = work->data;
1340                 work = work->next;
1341
1342                 pr_source_tile_free(st);
1343                 }
1344
1345         g_list_free(pr->source_tiles);
1346         pr->source_tiles = NULL;
1347 }
1348
1349 static void pr_source_tile_unset(PixbufRenderer *pr)
1350 {
1351         pr_source_tile_free_all(pr);
1352         pr->source_tiles_enabled = FALSE;
1353 }
1354
1355 static gint pr_source_tile_visible(PixbufRenderer *pr, SourceTile *st)
1356 {
1357         gint x1, y1, x2, y2;
1358
1359         if (!st) return FALSE;
1360
1361         x1 = (pr->x_scroll / pr->tile_width) * pr->tile_width;
1362         y1 = (pr->y_scroll / pr->tile_height) * pr->tile_height;
1363         x2 = ((pr->x_scroll + pr->vis_width) / pr->tile_width) * pr->tile_width + pr->tile_width;
1364         y2 = ((pr->y_scroll + pr->vis_height) / pr->tile_height) * pr->tile_height + pr->tile_height;
1365
1366         return !((double)st->x * pr->scale > (double)x2 ||
1367                  (double)(st->x + pr->source_tile_width) * pr->scale < (double)x1 ||
1368                  (double)st->y * pr->scale > (double)y2 ||
1369                  (double)(st->y + pr->source_tile_height) * pr->scale < (double)y1);
1370 }
1371
1372 static SourceTile *pr_source_tile_new(PixbufRenderer *pr, gint x, gint y)
1373 {
1374         SourceTile *st = NULL;
1375         gint count;
1376
1377         g_return_val_if_fail(pr->source_tile_width >= 1 && pr->source_tile_height >= 1, NULL);
1378
1379         if (pr->source_tiles_cache_size < 4) pr->source_tiles_cache_size = 4;
1380
1381         count = g_list_length(pr->source_tiles);
1382         if (count >= pr->source_tiles_cache_size)
1383                 {
1384                 GList *work;
1385
1386                 work = g_list_last(pr->source_tiles);
1387                 while (work && count >= pr->source_tiles_cache_size)
1388                         {
1389                         SourceTile *needle;
1390
1391                         needle = work->data;
1392                         work = work->prev;
1393
1394                         if (!pr_source_tile_visible(pr, needle))
1395                                 {
1396                                 pr->source_tiles = g_list_remove(pr->source_tiles, needle);
1397
1398                                 if (pr->func_tile_dispose)
1399                                         {
1400                                         pr->func_tile_dispose(pr, needle->x, needle->y,
1401                                                               pr->source_tile_width, pr->source_tile_height,
1402                                                               needle->pixbuf, pr->func_tile_data);
1403                                         }
1404
1405                                 if (!st)
1406                                         {
1407                                         st = needle;
1408                                         }
1409                                 else
1410                                         {
1411                                         pr_source_tile_free(needle);
1412                                         }
1413
1414                                 count--;
1415                                 }
1416                         }
1417                 }
1418
1419         if (!st)
1420                 {
1421                 st = g_new0(SourceTile, 1);
1422                 st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
1423                                             pr->source_tile_width, pr->source_tile_height);
1424                 }
1425
1426         st->x = (x / pr->source_tile_width) * pr->source_tile_width;
1427         st->y = (y / pr->source_tile_height) * pr->source_tile_height;
1428         st->blank = TRUE;
1429
1430         pr->source_tiles = g_list_prepend(pr->source_tiles, st);
1431
1432         return st;
1433 }
1434
1435 static SourceTile *pr_source_tile_request(PixbufRenderer *pr, gint x, gint y)
1436 {
1437         SourceTile *st;
1438
1439         st = pr_source_tile_new(pr, x, y);
1440         if (!st) return NULL;
1441
1442         if (pr->func_tile_request &&
1443             pr->func_tile_request(pr, st->x, st->y,
1444                                    pr->source_tile_width, pr->source_tile_height, st->pixbuf, pr->func_tile_data))
1445                 {
1446                 st->blank = FALSE;
1447                 }
1448
1449         pr_tile_invalidate_region(pr, st->x * pr->scale, st->y * pr->scale,
1450                                   pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1451
1452         return st;
1453 }
1454
1455 static SourceTile *pr_source_tile_find(PixbufRenderer *pr, gint x, gint y)
1456 {
1457         GList *work;
1458
1459         work = pr->source_tiles;
1460         while (work)
1461                 {
1462                 SourceTile *st = work->data;
1463
1464                 if (x >= st->x && x < st->x + pr->source_tile_width &&
1465                     y >= st->y && y < st->y + pr->source_tile_height)
1466                         {
1467                         if (work != pr->source_tiles)
1468                                 {
1469                                 pr->source_tiles = g_list_remove_link(pr->source_tiles, work);
1470                                 pr->source_tiles = g_list_concat(work, pr->source_tiles);
1471                                 }
1472                         return st;
1473                         }
1474
1475                 work = work->next;
1476                 }
1477
1478         return NULL;
1479 }
1480
1481 static GList *pr_source_tile_compute_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h, gint request)
1482 {
1483         gint x1, y1;
1484         GList *list = NULL;
1485         gint sx, sy;
1486
1487         if (x < 0) x = 0;
1488         if (y < 0) y = 0;
1489         if (w > pr->image_width) w = pr->image_width;
1490         if (h > pr->image_height) h = pr->image_height;
1491
1492         sx = (x / pr->source_tile_width) * pr->source_tile_width;
1493         sy = (y / pr->source_tile_height) * pr->source_tile_height;
1494
1495         for (x1 = sx; x1 < x + w; x1+= pr->source_tile_width)
1496                 {
1497                 for (y1 = sy; y1 < y + h; y1 += pr->source_tile_height)
1498                         {
1499                         SourceTile *st;
1500
1501                         st = pr_source_tile_find(pr, x1, y1);
1502                         if (!st && request) st = pr_source_tile_request(pr, x1, y1);
1503
1504                         if (st) list = g_list_prepend(list, st);
1505                         }
1506                 }
1507
1508         return g_list_reverse(list);
1509 }
1510
1511 static void pr_source_tile_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
1512 {
1513         GList *work;
1514
1515         if (width < 1 || height < 1) return;
1516
1517         work = pr->source_tiles;
1518         while (work)
1519                 {
1520                 SourceTile *st;
1521                 gint rx, ry, rw, rh;
1522
1523                 st = work->data;
1524                 work = work->next;
1525
1526                 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1527                                    x, y, width, height,
1528                                    &rx, &ry, &rw, &rh))
1529                         {
1530                         GdkPixbuf *pixbuf;
1531
1532                         pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh);
1533                         if (pr->func_tile_request &&
1534                             pr->func_tile_request(pr, rx, ry, rw, rh, pixbuf, pr->func_tile_data))
1535                                 {
1536                                 pr_tile_invalidate_region(pr, rx * pr->scale, ry * pr->scale,
1537                                                               rw * pr->scale, rh * pr->scale);
1538                                 }
1539                         g_object_unref(pixbuf);
1540                         }
1541                 }
1542 }
1543
1544 static gint pr_source_tile_render(PixbufRenderer *pr, ImageTile *it,
1545                                   gint x, gint y, gint w, gint h,
1546                                   gint new_data, gint fast)
1547 {
1548         GtkWidget *box;
1549         GList *list;
1550         GList *work;
1551         gint draw = FALSE;
1552
1553         box = GTK_WIDGET(pr);
1554
1555         if (pr->zoom == 1.0 || pr->scale == 1.0)
1556                 {
1557                 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1558                 work = list;
1559                 while (work)
1560                         {
1561                         SourceTile *st;
1562                         gint rx, ry, rw, rh;
1563
1564                         st = work->data;
1565                         work = work->next;
1566
1567                         if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1568                                            it->x + x, it->y + y, w, h,
1569                                            &rx, &ry, &rw, &rh))
1570                                 {
1571                                 if (st->blank)
1572                                         {
1573                                         gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1574                                                            rx - st->x, ry - st->y, rw, rh);
1575                                         }
1576                                 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1577                                         {
1578                                         gdk_draw_pixbuf(it->pixmap,
1579                                                         box->style->fg_gc[GTK_WIDGET_STATE(box)],
1580                                                         st->pixbuf,
1581                                                         rx - st->x, ry - st->y,
1582                                                         rx - it->x, ry - it->y,
1583                                                         rw, rh,
1584                                                         pr->dither_quality, rx, ry);
1585                                         }
1586                                 }
1587                         }
1588                 }
1589         else
1590                 {
1591                 double scale_x, scale_y;
1592                 gint sx, sy, sw, sh;
1593
1594                 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1595                 scale_x = (double)pr->width / pr->image_width;
1596                 scale_y = (double)pr->height / pr->image_height;
1597
1598                 sx = (double)(it->x + x) / scale_x;
1599                 sy = (double)(it->y + y) / scale_y;
1600                 sw = (double)w / scale_x;
1601                 sh = (double)h / scale_y;
1602
1603                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1604
1605 #if 0
1606                 /* draws red over draw region, to check for leaks (regions not filled) */
1607                 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1608 #endif
1609
1610                 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1611                 work = list;
1612                 while (work)
1613                         {
1614                         SourceTile *st;
1615                         gint rx, ry, rw, rh;
1616                         gint stx, sty, stw, sth;
1617
1618                         st = work->data;
1619                         work = work->next;
1620
1621                         stx = floor((double)st->x * scale_x);
1622                         sty = floor((double)st->y * scale_y);
1623                         stw = ceil ((double)(st->x + pr->source_tile_width) * scale_x) - stx;
1624                         sth = ceil ((double)(st->y + pr->source_tile_height) * scale_y) - sty;
1625
1626                         if (pr_clip_region(stx, sty, stw, sth,
1627                                            it->x + x, it->y + y, w, h,
1628                                            &rx, &ry, &rw, &rh))
1629                                 {
1630                                 if (st->blank)
1631                                         {
1632                                         gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1633                                                            rx - st->x, ry - st->y, rw, rh);
1634                                         }
1635                                 else
1636                                         {
1637                                         double offset_x;
1638                                         double offset_y;
1639
1640                                         /* may need to use unfloored stx,sty values here */
1641                                         offset_x = (double)(stx - it->x);
1642                                         offset_y = (double)(sty - it->y);
1643
1644                                         gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1645                                                  (double) 0.0 + offset_x,
1646                                                  (double) 0.0 + offset_y,
1647                                                  scale_x, scale_y,
1648                                                  (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1649                                         draw = TRUE;
1650                                         }
1651                                 }
1652                         }
1653                 }
1654
1655         g_list_free(list);
1656
1657         return draw;
1658 }
1659
1660 void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height,
1661                                gint tile_width, gint tile_height, gint cache_size,
1662                                PixbufRendererTileRequestFunc func_request,
1663                                PixbufRendererTileDisposeFunc func_dispose,
1664                                gpointer user_data,
1665                                gdouble zoom)
1666 {
1667         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1668         g_return_if_fail(tile_width >= 32 && tile_width >= 32);
1669         g_return_if_fail(width >= 32 && height > 32);
1670         g_return_if_fail(func_request != NULL);
1671
1672         if (pr->pixbuf) g_object_unref(pr->pixbuf);
1673         pr->pixbuf = NULL;
1674
1675         pr_source_tile_unset(pr);
1676
1677         if (cache_size < 4) cache_size = 4;
1678
1679         pr->source_tiles_enabled = TRUE;
1680         pr->source_tiles_cache_size = cache_size;
1681         pr->source_tile_width = tile_width;
1682         pr->source_tile_height = tile_height;
1683
1684         pr->image_width = width;
1685         pr->image_height = height;
1686
1687         pr->func_tile_request = func_request;
1688         pr->func_tile_dispose = func_dispose;
1689         pr->func_tile_data = user_data;
1690
1691         pr_zoom_sync(pr, zoom, TRUE, TRUE, FALSE, 0, 0);
1692         pr_redraw(pr, TRUE);
1693 }
1694
1695 void pixbuf_renderer_set_tiles_size(PixbufRenderer *pr, gint width, gint height)
1696 {
1697         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1698         g_return_if_fail(width >= 32 && height > 32);
1699
1700         if (!pr->source_tiles_enabled) return;
1701         if (pr->image_width == width && pr->image_height == height) return;
1702
1703         pr->image_width = width;
1704         pr->image_height = height;
1705
1706         pr_zoom_sync(pr, pr->zoom, TRUE, FALSE, FALSE, 0, 0);
1707 }
1708
1709 gint pixbuf_renderer_get_tiles(PixbufRenderer *pr)
1710 {
1711         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1712
1713         return pr->source_tiles_enabled;
1714 }
1715
1716 static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment,
1717                                 gint center_point, gint x, gint y)
1718 {
1719         gdouble zoom = pr->zoom;
1720
1721         if (increment == 0.0) return;
1722
1723         if (zoom == 0.0)
1724                 {
1725                 if (pr->scale < 1.0)
1726                         {
1727                         zoom = 0.0 - 1.0 / pr->scale;
1728                         }
1729                 else
1730                         {
1731                         zoom = pr->scale;
1732                         }
1733                 }
1734
1735         if (increment < 0.0)
1736                 {
1737                 if (zoom >= 1.0 && zoom + increment < 1.0)
1738                         {
1739                         zoom = zoom + increment - 2.0;
1740                         }
1741                 else
1742                         {
1743                         zoom = zoom + increment;
1744                         }
1745                 }
1746         else
1747                 {
1748                 if (zoom <= -1.0 && zoom + increment > -1.0)
1749                         {
1750                         zoom = zoom + increment + 2.0;
1751                         }
1752                 else
1753                         {
1754                         zoom = zoom + increment;
1755                         }
1756                 }
1757
1758         pr_zoom_sync(pr, zoom, FALSE, FALSE, center_point, x, y);
1759 }
1760
1761 /*
1762  *-------------------------------------------------------------------
1763  * display tiles
1764  *-------------------------------------------------------------------
1765  */
1766
1767 static ImageTile *pr_tile_new(gint x, gint y, gint width, gint height)
1768 {
1769         ImageTile *it;
1770
1771         it = g_new0(ImageTile, 1);
1772
1773         it->x = x;
1774         it->y = y;
1775         it->w = width;
1776         it->h = height;
1777
1778         it->render_done = TILE_RENDER_NONE;
1779
1780         return it;
1781 }
1782
1783 static void pr_tile_free(ImageTile *it)
1784 {
1785         if (!it) return;
1786
1787         if (it->pixbuf) gdk_pixbuf_unref(it->pixbuf);
1788         if (it->pixmap) g_object_unref(it->pixmap);
1789
1790         g_free(it);
1791 }
1792
1793 static void pr_tile_free_all(PixbufRenderer *pr)
1794 {
1795         GList *work;
1796
1797         work = pr->tiles;
1798         while (work)
1799                 {
1800                 ImageTile *it;
1801
1802                 it = work->data;
1803                 work = work->next;
1804
1805                 pr_tile_free(it);
1806                 }
1807
1808         g_list_free(pr->tiles);
1809         pr->tiles = NULL;
1810         pr->tile_cache_size = 0;
1811 }
1812
1813 static ImageTile *pr_tile_add(PixbufRenderer *pr, gint x, gint y)
1814 {
1815         ImageTile *it;
1816
1817         it = pr_tile_new(x, y, pr->tile_width, pr->tile_height);
1818
1819         if (it->x + it->w > pr->width) it->w = pr->width - it->x;
1820         if (it->y + it->h > pr->height) it->h = pr->height - it->y;
1821
1822         pr->tiles = g_list_prepend(pr->tiles, it);
1823         pr->tile_cache_size += it->size;
1824
1825         return it;
1826 }
1827
1828 static void pr_tile_remove(PixbufRenderer *pr, ImageTile *it)
1829 {
1830         if (it->qd)
1831                 {
1832                 QueueData *qd = it->qd;
1833
1834                 it->qd = NULL;
1835                 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
1836                 g_free(qd);
1837                 }
1838
1839         if (it->qd2)
1840                 {
1841                 QueueData *qd = it->qd2;
1842
1843                 it->qd2 = NULL;
1844                 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
1845                 g_free(qd);
1846                 }
1847
1848         pr->tiles = g_list_remove(pr->tiles, it);
1849         pr->tile_cache_size -= it->size;
1850
1851         pr_tile_free(it);
1852 }
1853
1854 static void pr_tile_free_space(PixbufRenderer *pr, guint space, ImageTile *it)
1855 {
1856         GList *work;
1857         gint tile_max;
1858
1859         work = g_list_last(pr->tiles);
1860
1861         if (pr->source_tiles_enabled && pr->scale < 1.0)
1862                 {
1863                 gint tiles;
1864
1865                 tiles = (pr->vis_width / pr->tile_width + 1) * (pr->vis_height / pr->tile_height + 1);
1866                 tile_max = MAX(tiles * pr->tile_width * pr->tile_height * 3,
1867                                (gint)((double)pr->tile_cache_max * 1048576.0 * pr->scale));
1868                 }
1869         else
1870                 {
1871                 tile_max = pr->tile_cache_max * 1048576;
1872                 }
1873
1874         while (work && pr->tile_cache_size + space > tile_max)
1875                 {
1876                 ImageTile *needle;
1877
1878                 needle = work->data;
1879                 work = work->prev;
1880                 if (needle != it &&
1881                     ((!needle->qd && !needle->qd2) || !pr_tile_is_visible(pr, needle))) pr_tile_remove(pr, needle);
1882                 }
1883 }
1884
1885 static void pr_tile_invalidate_all(PixbufRenderer *pr)
1886 {
1887         GList *work;
1888
1889         work = pr->tiles;
1890         while (work)
1891                 {
1892                 ImageTile *it;
1893
1894                 it = work->data;
1895                 work = work->next;
1896
1897                 it->render_done = TILE_RENDER_NONE;
1898                 it->render_todo = TILE_RENDER_ALL;
1899                 it->blank = FALSE;
1900
1901                 it->w = MIN(pr->tile_width, pr->width - it->x);
1902                 it->h = MIN(pr->tile_height, pr->height - it->y);
1903                 }
1904 }
1905
1906 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1907 {
1908         gint x1, x2;
1909         gint y1, y2;
1910         GList *work;
1911
1912         x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
1913         x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
1914
1915         y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
1916         y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
1917
1918         work = pr->tiles;
1919         while (work)
1920                 {
1921                 ImageTile *it;
1922
1923                 it = work->data;
1924                 work = work->next;
1925
1926                 if (it->x < x2 && it->x + it->w > x1 &&
1927                     it->y < y2 && it->y + it->h > y1)
1928                         {
1929                         it->render_done = TILE_RENDER_NONE;
1930                         it->render_todo = TILE_RENDER_ALL;
1931                         }
1932                 }
1933 }
1934
1935 static ImageTile *pr_tile_get(PixbufRenderer *pr, gint x, gint y, gint only_existing)
1936 {
1937         GList *work;
1938
1939         work = pr->tiles;
1940         while (work)
1941                 {
1942                 ImageTile *it;
1943
1944                 it = work->data;
1945                 if (it->x == x && it->y == y)
1946                         {
1947                         pr->tiles = g_list_delete_link(pr->tiles, work);
1948                         pr->tiles = g_list_prepend(pr->tiles, it);
1949                         return it;
1950                         }
1951
1952                 work = work->next;
1953                 }
1954
1955         if (only_existing) return NULL;
1956
1957         return pr_tile_add(pr, x, y);
1958 }
1959
1960 static void pr_tile_prepare(PixbufRenderer *pr, ImageTile *it)
1961 {
1962         if (!it->pixmap)
1963                 {
1964                 GdkPixmap *pixmap;
1965                 guint size;
1966
1967                 pixmap = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
1968
1969                 size = pixmap_calc_size(pixmap);
1970                 pr_tile_free_space(pr, size, it);
1971
1972                 it->pixmap = pixmap;
1973                 it->size += size;
1974                 pr->tile_cache_size += size;
1975                 }
1976         
1977         if ((pr->zoom != 1.0 || pr->source_tiles_enabled || (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf)) ) &&
1978             !it->pixbuf)
1979                 {
1980                 GdkPixbuf *pixbuf;
1981                 guint size;
1982
1983                 if (pr->pixbuf)
1984                         {
1985                         pixbuf = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(pr->pixbuf),
1986                                                 gdk_pixbuf_get_has_alpha(pr->pixbuf),
1987                                                 gdk_pixbuf_get_bits_per_sample(pr->pixbuf),
1988                                                 pr->tile_width, pr->tile_height);
1989                         }
1990                 else
1991                         {
1992                         pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
1993                         }
1994
1995                 size = gdk_pixbuf_get_rowstride(pixbuf) * pr->tile_height;
1996                 pr_tile_free_space(pr, size, it);
1997
1998                 it->pixbuf = pixbuf;
1999                 it->size += size;
2000                 pr->tile_cache_size += size;
2001                 }
2002 }
2003
2004 /*
2005  *-------------------------------------------------------------------
2006  * drawing
2007  *-------------------------------------------------------------------
2008  */
2009
2010 static void pr_tile_render(PixbufRenderer *pr, ImageTile *it,
2011                            gint x, gint y, gint w, gint h,
2012                            gint new_data, gint fast)
2013 {
2014         GtkWidget *box;
2015         gint has_alpha;
2016         gint draw = FALSE;
2017
2018         if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return;
2019
2020         if (it->render_done != TILE_RENDER_ALL)
2021                 {
2022                 x = 0;
2023                 y = 0;
2024                 w = it->w;
2025                 h = it->h;
2026                 if (!fast) it->render_done = TILE_RENDER_ALL;
2027                 }
2028         else if (it->render_todo != TILE_RENDER_AREA)
2029                 {
2030                 if (!fast) it->render_todo = TILE_RENDER_NONE;
2031                 return;
2032                 }
2033
2034         if (!fast) it->render_todo = TILE_RENDER_NONE;
2035
2036         if (new_data) it->blank = FALSE;
2037
2038         pr_tile_prepare(pr, it);
2039         has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
2040
2041         box = GTK_WIDGET(pr);
2042
2043         /* FIXME checker colors for alpha should be configurable,
2044          * also should be drawn for blank = TRUE
2045          */
2046
2047         if (it->blank)
2048                 {
2049                 /* no data, do fast rect fill */
2050                 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
2051                                    0, 0, it->w, it->h);
2052                 }
2053         else if (pr->source_tiles_enabled)
2054                 {
2055                 draw = pr_source_tile_render(pr, it, x, y, w, h, new_data, fast);
2056                 }
2057         else if (pr->zoom == 1.0 || pr->scale == 1.0)
2058                 {
2059                 if (has_alpha)
2060                         {
2061                         gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, x, y, w, h,
2062                                          (double) 0.0 - it->x,
2063                                          (double) 0.0 - it->y,
2064                                          1.0, 1.0, GDK_INTERP_NEAREST,
2065                                          255, it->x + x, it->y + y,
2066                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2067                         draw = TRUE;
2068                         }
2069                 else
2070                         {
2071                         /* faster, simple */
2072                         gdk_draw_pixbuf(it->pixmap,
2073                                         box->style->fg_gc[GTK_WIDGET_STATE(box)],
2074                                         pr->pixbuf,
2075                                         it->x + x, it->y + y,
2076                                         x, y,
2077                                         w, h,
2078                                         pr->dither_quality, it->x + x, it->y + y);
2079                         }
2080                 }
2081         else
2082                 {
2083                 double scale_x, scale_y;
2084
2085                 if (pr->image_width == 0 || pr->image_height == 0) return;
2086                 scale_x = (double)pr->width / pr->image_width;
2087                 scale_y = (double)pr->height / pr->image_height;
2088
2089                 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
2090                  * small sizes for anything but GDK_INTERP_NEAREST
2091                  */
2092                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
2093
2094                 if (!has_alpha)
2095                         {
2096                         gdk_pixbuf_scale(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                         }
2102                 else
2103                         {
2104                         gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, x, y, w, h,
2105                                          (double) 0.0 - it->x,
2106                                          (double) 0.0 - it->y,
2107                                          scale_x, scale_y,
2108                                          (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
2109                                          255, it->x + x, it->y + y,
2110                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2111                         }
2112                 draw = TRUE;
2113                 }
2114
2115         if (draw && it->pixbuf && !it->blank)
2116                 {
2117                 gdk_draw_pixbuf(it->pixmap,
2118                                 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2119                                 it->pixbuf,
2120                                 x, y,
2121                                 x, y,
2122                                 w, h,
2123                                 pr->dither_quality, it->x + x, it->y + y);
2124                 }
2125
2126 #if 0
2127         /* enable this line for debugging the edges of tiles */
2128         gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2129                            FALSE, 0, 0, it->w, it->h);
2130 #endif
2131 }
2132
2133
2134 static void pr_tile_expose(PixbufRenderer *pr, ImageTile *it,
2135                            gint x, gint y, gint w, gint h,
2136                            gint new_data, gint fast)
2137 {
2138         GtkWidget *box;
2139
2140         pr_tile_render(pr, it, x, y, w, h, new_data, fast);
2141
2142         box = GTK_WIDGET(pr);
2143
2144         gdk_draw_drawable(box->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
2145                           it->pixmap, x, y,
2146                           pr->x_offset + (it->x - pr->x_scroll) + x, pr->y_offset + (it->y - pr->y_scroll) + y, w, h);
2147
2148         if (pr->overlay_list)
2149                 {
2150                 pr_overlay_draw(pr, pr->x_offset + (it->x - pr->x_scroll) + x,
2151                                 pr->y_offset + (it->y - pr->y_scroll) + y,
2152                                 w, h,
2153                                 it);
2154                 }
2155 }
2156
2157
2158 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it)
2159 {
2160         return (it->x + it->w >= pr->x_scroll && it->x < pr->x_scroll + pr->vis_width &&
2161                 it->y + it->h >= pr->y_scroll && it->y < pr->y_scroll + pr->vis_height);
2162 }
2163
2164 /*
2165  *-------------------------------------------------------------------
2166  * draw queue
2167  *-------------------------------------------------------------------
2168  */
2169
2170 static gint pr_queue_draw_idle_cb(gpointer data)
2171 {
2172         PixbufRenderer *pr = data;
2173         QueueData *qd;
2174         gint fast;
2175
2176         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2177             (!pr->draw_queue && !pr->draw_queue_2pass) ||
2178             pr->draw_idle_id == -1)
2179                 {
2180                 pr_render_complete_signal(pr);
2181
2182                 pr->draw_idle_id = -1;
2183                 return FALSE;
2184                 }
2185
2186         if (pr->draw_queue)
2187                 {
2188                 qd = pr->draw_queue->data;
2189                 fast = (pr->zoom_2pass && pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0);
2190                 }
2191         else
2192                 {
2193                 if (pr->loading)
2194                         {
2195                         /* still loading, wait till done (also drops the higher priority) */
2196
2197                         pr->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
2198                                                            pr_queue_draw_idle_cb, pr, NULL);
2199                         pr->draw_idle_high = FALSE;
2200                         return FALSE;
2201                         }
2202
2203                 qd = pr->draw_queue_2pass->data;
2204                 fast = FALSE;
2205                 }
2206
2207         if (GTK_WIDGET_REALIZED(pr))
2208                 {
2209                 if (pr_tile_is_visible(pr, qd->it))
2210                         {
2211                         pr_tile_expose(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2212                         }
2213                 else if (qd->new_data)
2214                         {
2215                         /* if new pixel data, and we already have a pixmap, update the tile */
2216                         qd->it->blank = FALSE;
2217                         if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL)
2218                                 {
2219                                 pr_tile_render(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2220                                 }
2221                         }
2222                 }
2223
2224         if (pr->draw_queue)
2225                 {
2226                 qd->it->qd = NULL;
2227                 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
2228                 if (fast)
2229                         {
2230                         if (qd->it->qd2)
2231                                 {
2232                                 pr_queue_merge(qd->it->qd2, qd);
2233                                 g_free(qd);
2234                                 }
2235                         else
2236                                 {
2237                                 qd->it->qd2 = qd;
2238                                 pr->draw_queue_2pass = g_list_append(pr->draw_queue_2pass, qd);
2239                                 }
2240                         }
2241                 else
2242                         {
2243                         g_free(qd);
2244                         }
2245                 }
2246         else
2247                 {
2248                 qd->it->qd2 = NULL;
2249                 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
2250                 g_free(qd);
2251                 }
2252
2253         if (!pr->draw_queue && !pr->draw_queue_2pass)
2254                 {
2255                 pr_render_complete_signal(pr);
2256
2257                 pr->draw_idle_id = -1;
2258                 return FALSE;
2259                 }
2260
2261         return TRUE;
2262 }
2263
2264 static void pr_queue_list_free(GList *list)
2265 {
2266         GList *work;
2267
2268         work = list;
2269         while (work)
2270                 {
2271                 QueueData *qd;
2272
2273                 qd = work->data;
2274                 work = work->next;
2275
2276                 qd->it->qd = NULL;
2277                 qd->it->qd2 = NULL;
2278                 g_free(qd);
2279                 }
2280
2281         g_list_free(list);
2282 }
2283
2284 static void pr_queue_clear(PixbufRenderer *pr)
2285 {
2286         pr_queue_list_free(pr->draw_queue);
2287         pr->draw_queue = NULL;
2288
2289         pr_queue_list_free(pr->draw_queue_2pass);
2290         pr->draw_queue_2pass = NULL;
2291
2292         if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2293         pr->draw_idle_id = -1;
2294 }
2295
2296 static void pr_queue_merge(QueueData *parent, QueueData *qd)
2297 {
2298         if (parent->x + parent->w < qd->x + qd->w)
2299                 {
2300                 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
2301                 }
2302         if (parent->x > qd->x)
2303                 {
2304                 parent->w += parent->x - qd->x;
2305                 parent->x = qd->x;
2306                 }
2307
2308         if (parent->y + parent->h < qd->y + qd->h)
2309                 {
2310                 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
2311                 }
2312         if (parent->y > qd->y)
2313                 {
2314                 parent->h += parent->y - qd->y;
2315                 parent->y = qd->y;
2316                 }
2317
2318         parent->new_data |= qd->new_data;
2319 }
2320
2321 static gint pr_clamp_to_visible(PixbufRenderer *pr, gint *x, gint *y, gint *w, gint *h)
2322 {
2323         gint nx, ny;
2324         gint nw, nh;
2325         gint vx, vy;
2326         gint vw, vh;
2327
2328         vw = pr->vis_width;
2329         vh = pr->vis_height;
2330
2331         vx = pr->x_scroll;
2332         vy = pr->y_scroll;
2333
2334         if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
2335
2336         /* now clamp it */
2337         nx = CLAMP(*x, vx, vx + vw);
2338         nw = CLAMP(*w - (nx - *x), 1, vw);
2339
2340         ny = CLAMP(*y, vy, vy + vh);
2341         nh = CLAMP(*h - (ny - *y), 1, vh);
2342
2343         *x = nx;
2344         *y = ny;
2345         *w = nw;
2346         *h = nh;
2347
2348         return TRUE;
2349 }
2350
2351 static gint pr_queue_to_tiles(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2352                               gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2353 {
2354         gint i, j;
2355         gint x1, x2;
2356         gint y1, y2;
2357
2358         if (clamp && !pr_clamp_to_visible(pr, &x, &y, &w, &h)) return FALSE;
2359
2360         x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
2361         x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
2362
2363         y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
2364         y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
2365
2366         for (j = y1; j <= y2; j += pr->tile_height)
2367                 {
2368                 for (i = x1; i <= x2; i += pr->tile_width)
2369                         {
2370                         ImageTile *it;
2371
2372                         it = pr_tile_get(pr, i, j,
2373                                          (only_existing &&
2374                                           (i + pr->tile_width < pr->x_scroll ||
2375                                            i > pr->x_scroll + pr->vis_width ||
2376                                            j + pr->tile_height < pr->y_scroll ||
2377                                            j > pr->y_scroll + pr->vis_height)));
2378                         if (it)
2379                                 {
2380                                 QueueData *qd;
2381
2382                                 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
2383                                     (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
2384                                         {
2385                                         it->render_todo = render;
2386                                         }
2387
2388                                 qd = g_new(QueueData, 1);
2389                                 qd->it = it;
2390                                 qd->new_data = new_data;
2391
2392                                 if (i < x)
2393                                         {
2394                                         qd->x = x - i;
2395                                         }
2396                                 else
2397                                         {
2398                                         qd->x = 0;
2399                                         }
2400                                 qd->w = x + w - i - qd->x;
2401                                 if (qd->x + qd->w > pr->tile_width) qd->w = pr->tile_width - qd->x;
2402
2403                                 if (j < y)
2404                                         {
2405                                         qd->y = y - j;
2406                                         }
2407                                 else
2408                                         {
2409                                         qd->y = 0;
2410                                         }
2411                                 qd->h = y + h - j - qd->y;
2412                                 if (qd->y + qd->h > pr->tile_height) qd->h = pr->tile_height - qd->y;
2413
2414                                 if (qd->w < 1 || qd->h < 1)
2415                                         {
2416                                         g_free(qd);
2417                                         }
2418                                 else if (it->qd)
2419                                         {
2420                                         pr_queue_merge(it->qd, qd);
2421                                         g_free(qd);
2422                                         }
2423                                 else
2424                                         {
2425                                         it->qd = qd;
2426                                         pr->draw_queue = g_list_append(pr->draw_queue, qd);
2427                                         }
2428                                 }
2429                         }
2430                 }
2431
2432         return TRUE;
2433 }
2434
2435 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2436                      gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2437 {
2438         gint nx, ny;
2439
2440         nx = CLAMP(x, 0, pr->width - 1);
2441         ny = CLAMP(y, 0, pr->height - 1);
2442         w -= (nx - x);
2443         h -= (ny - y);
2444         w = CLAMP(w, 0, pr->width - nx);
2445         h = CLAMP(h, 0, pr->height - ny);
2446         if (w < 1 || h < 1) return;
2447
2448         if (pr_queue_to_tiles(pr, nx, ny, w, h, clamp, render, new_data, only_existing) &&
2449             ((!pr->draw_queue && !pr->draw_queue_2pass) || pr->draw_idle_id == -1 || !pr->draw_idle_high))
2450                 {
2451                 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2452                 pr->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW,
2453                                                    pr_queue_draw_idle_cb, pr, NULL);
2454                 pr->draw_idle_high = TRUE;
2455                 }
2456 }
2457
2458 static void pr_redraw(PixbufRenderer *pr, gint new_data)
2459 {
2460         pr_queue_clear(pr);
2461         pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data, FALSE);
2462 }
2463
2464 /*
2465  *-------------------------------------------------------------------
2466  * signal emission
2467  *-------------------------------------------------------------------
2468  */
2469
2470 static void pr_update_signal(PixbufRenderer *pr)
2471 {
2472 #if 0
2473         printf("FIXME: send updated signal\n");
2474 #endif
2475 }
2476
2477 static void pr_zoom_signal(PixbufRenderer *pr)
2478 {
2479         g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
2480 }
2481
2482 static void pr_clicked_signal(PixbufRenderer *pr, GdkEventButton *bevent)
2483 {
2484         g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
2485 }
2486
2487 static void pr_scroll_notify_signal(PixbufRenderer *pr)
2488 {
2489         g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
2490 }
2491
2492 static void pr_render_complete_signal(PixbufRenderer *pr)
2493 {
2494         if (!pr->complete)
2495                 {
2496                 g_signal_emit(pr, signals[SIGNAL_RENDER_COMPLETE], 0);
2497                 g_object_set(G_OBJECT(pr), "complete", TRUE, NULL);
2498                 }
2499 }
2500
2501 static void pr_drag_signal(PixbufRenderer *pr, GdkEventButton *bevent)
2502 {
2503         g_signal_emit(pr, signals[SIGNAL_DRAG], 0, bevent);
2504 }
2505
2506 /*
2507  *-------------------------------------------------------------------
2508  * sync and clamp
2509  *-------------------------------------------------------------------
2510  */
2511
2512 static gint pr_scroll_clamp(PixbufRenderer *pr)
2513 {
2514         gint old_xs;
2515         gint old_ys;
2516
2517         if (pr->zoom == 0.0)
2518                 {
2519                 pr->x_scroll = 0;
2520                 pr->y_scroll = 0;
2521
2522                 return FALSE;
2523                 }
2524
2525         old_xs = pr->x_scroll;
2526         old_ys = pr->y_scroll;
2527
2528         if (pr->x_offset > 0)
2529                 {
2530                 pr->x_scroll = 0;
2531                 }
2532         else
2533                 {
2534                 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
2535                 }
2536
2537         if (pr->y_offset > 0)
2538                 {
2539                 pr->y_scroll = 0;
2540                 }
2541         else
2542                 {
2543                 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
2544                 }
2545
2546         return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
2547 }
2548
2549 static gint pr_size_clamp(PixbufRenderer *pr)
2550 {
2551         gint old_vw, old_vh;
2552
2553         old_vw = pr->vis_width;
2554         old_vh = pr->vis_height;
2555
2556         if (pr->width < pr->window_width)
2557                 {
2558                 pr->vis_width = pr->width;
2559                 pr->x_offset = (pr->window_width - pr->width) / 2;
2560                 }
2561         else
2562                 {
2563                 pr->vis_width = pr->window_width;
2564                 pr->x_offset = 0;
2565                 }
2566
2567         if (pr->height < pr->window_height)
2568                 {
2569                 pr->vis_height = pr->height;
2570                 pr->y_offset = (pr->window_height - pr->height) / 2;
2571                 }
2572         else
2573                 {
2574                 pr->vis_height = pr->window_height;
2575                 pr->y_offset = 0;
2576                 }
2577
2578         return (old_vw != pr->vis_width || old_vh != pr->vis_height);
2579 }
2580
2581 static gint pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom,
2582                           gint force, gint new, gint invalidate,
2583                           gint *redrawn)
2584 {
2585         gint w, h;
2586         gdouble scale;
2587         gint invalid;
2588
2589         zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
2590
2591         if (pr->zoom == zoom && !force) return FALSE;
2592
2593         w = pr->image_width;
2594         h = pr->image_height;
2595
2596         if (zoom == 0.0 && !pr->pixbuf)
2597                 {
2598                 scale = 1.0;
2599                 }
2600         else if (zoom == 0.0)
2601                 {
2602                 gint max_w;
2603                 gint max_h;
2604                 gint sizeable;
2605
2606                 sizeable = (new && pr_parent_window_sizable(pr));
2607
2608                 if (sizeable)
2609                         {
2610                         max_w = gdk_screen_width();
2611                         max_h = gdk_screen_height();
2612
2613                         if (pr->window_limit)
2614                                 {
2615                                 max_w = max_w * pr->window_limit_size / 100;
2616                                 max_h = max_h * pr->window_limit_size / 100;
2617                                 }
2618                         }
2619                 else
2620                         {
2621                         max_w = pr->window_width;
2622                         max_h = pr->window_height;
2623                         }
2624
2625                 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
2626                         {
2627                         if ((gdouble)max_w / w > (gdouble)max_h / h)
2628                                 {
2629                                 scale = (gdouble)max_h / h;
2630                                 h = max_h;
2631                                 w = w * scale + 0.5;
2632                                 if (w > max_w) w = max_w;
2633                                 }
2634                         else
2635                                 {
2636                                 scale = (gdouble)max_w / w;
2637                                 w = max_w;
2638                                 h = h * scale + 0.5;
2639                                 if (h > max_h) h = max_h;
2640                                 }
2641                         if (w < 1) w = 1;
2642                         if (h < 1) h = 1;
2643                         }
2644                 else
2645                         {
2646                         scale = 1.0;
2647                         }
2648                 }
2649         else if (zoom > 0.0) /* zoom orig, in */
2650                 {
2651                 scale = zoom;
2652                 w = w * scale;
2653                 h = h * scale;
2654                 }
2655         else /* zoom out */
2656                 {
2657                 scale = 1.0 / (0.0 - zoom);
2658                 w = w * scale;
2659                 h = h * scale;
2660                 }
2661
2662         invalid = (pr->width != w || pr->height != h);
2663
2664         pr->zoom = zoom;
2665         pr->width = w;
2666         pr->height = h;
2667         pr->scale = scale;
2668
2669         if (invalidate || invalid)
2670                 {
2671                 pr_tile_invalidate_all(pr);
2672                 pr_redraw(pr, TRUE);
2673                 }
2674         if (redrawn) *redrawn = (invalidate || invalid);
2675
2676         return TRUE;
2677 }
2678
2679 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
2680                          gint force, gint new,
2681                          gint center_point, gint px, gint py)
2682 {
2683         gdouble old_scale;
2684         gint old_cx, old_cy;
2685         gint clamped;
2686         gint sized;
2687         gint redrawn = FALSE;
2688
2689         old_scale = pr->scale;
2690         if (center_point)
2691                 {
2692                 px = CLAMP(px, 0, pr->width);
2693                 py = CLAMP(py, 0, pr->height);
2694                 old_cx = pr->x_scroll + (px - pr->x_offset);
2695                 old_cy = pr->y_scroll + (py - pr->y_offset);
2696                 }
2697         else
2698                 {
2699                 px = py = 0;
2700                 old_cx = pr->x_scroll + pr->vis_width / 2;
2701                 old_cy = pr->y_scroll + pr->vis_height / 2;
2702                 }
2703
2704         if (!pr_zoom_clamp(pr, zoom, force, new, force, &redrawn)) return;
2705
2706         clamped = pr_size_clamp(pr);
2707         sized = pr_parent_window_resize(pr, pr->width, pr->height);
2708
2709         if (force && new)
2710                 {
2711                 switch (pr->scroll_reset)
2712                         {
2713                         case PR_SCROLL_RESET_NOCHANGE:
2714                                 /* maintain old scroll position, do nothing */
2715                                 break;
2716                         case PR_SCROLL_RESET_CENTER:
2717                                 /* center new image */
2718                                 pr->x_scroll = ((double)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2;
2719                                 pr->y_scroll = ((double)pr->image_height / 2.0 * pr->scale) - pr->vis_height / 2;
2720                                 break;
2721                         case PR_SCROLL_RESET_TOPLEFT:
2722                         default:
2723                                 /* reset to upper left */
2724                                 pr->x_scroll = 0;
2725                                 pr->y_scroll = 0;
2726                                 break;
2727                         }
2728                 }
2729         else
2730                 {
2731                 /* user zoom does not force, so keep visible center point */
2732                 if (center_point)
2733                         {
2734                         pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
2735                         pr->y_scroll = old_cy / old_scale * pr->scale - (py - pr->y_offset);
2736                         }
2737                 else
2738                         {
2739                         pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2);
2740                         pr->y_scroll = old_cy / old_scale * pr->scale - (pr->vis_height / 2);
2741                         }
2742                 }
2743
2744         pr_scroll_clamp(pr);
2745
2746         /* If the window was not sized, redraw the image - we know there will be no size/expose signal.
2747          * But even if a size is claimed, there is no guarantee that the window manager will allow it,
2748          * so redraw the window anyway :/
2749          */
2750         if (sized || clamped) pr_border_clear(pr);
2751         pr_redraw(pr, redrawn);
2752
2753         pr_scroll_notify_signal(pr);
2754         pr_zoom_signal(pr);
2755         pr_update_signal(pr);
2756 }
2757
2758 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
2759 {
2760         gint zoom_changed = FALSE;
2761
2762         if (pr->window_width == new_width && pr->window_height == new_height) return;
2763
2764         pr->window_width = new_width;
2765         pr->window_height = new_height;
2766
2767         if (pr->zoom == 0.0)
2768                 {
2769                 gdouble old_scale = pr->scale;
2770                 pr_zoom_clamp(pr, 0.0, TRUE, FALSE, FALSE, NULL);
2771                 zoom_changed = (old_scale != pr->scale);
2772                 }
2773
2774         pr_size_clamp(pr);
2775         pr_scroll_clamp(pr);
2776
2777         pr_overlay_update_sizes(pr);
2778
2779         /* ensure scroller remains visible */
2780         if (pr->scroller_overlay != -1)
2781                 {
2782                 gint update = FALSE;
2783
2784                 if (pr->scroller_x > new_width)
2785                         {
2786                         pr->scroller_x = new_width;
2787                         pr->scroller_xpos = new_width;
2788                         update = TRUE;
2789                         }
2790                 if (pr->scroller_y > new_height)
2791                         {
2792                         pr->scroller_y = new_height;
2793                         pr->scroller_ypos = new_height;
2794                         update = TRUE;
2795                         }
2796
2797                 if (update)
2798                         {
2799                         GdkPixbuf *pixbuf;
2800
2801                         if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
2802                                 {
2803                                 gint w, h;
2804
2805                                 w = gdk_pixbuf_get_width(pixbuf);
2806                                 h = gdk_pixbuf_get_height(pixbuf);
2807                                 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
2808                                                             pr->scroller_x - w / 2, pr->scroller_y - h / 2);
2809                                 }
2810                         }
2811                 }
2812
2813         pr_border_clear(pr);
2814
2815         pr_scroll_notify_signal(pr);
2816         if (zoom_changed) pr_zoom_signal(pr);
2817         pr_update_signal(pr);
2818 }
2819
2820 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2821 {
2822         PixbufRenderer *pr = data;
2823
2824         pr_size_sync(pr, allocation->width, allocation->height);
2825 }
2826
2827 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
2828 {
2829         gint x, y;
2830
2831         pr_border_draw(pr, area->x, area->y, area->width, area->height);
2832
2833         x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
2834         y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
2835
2836         pr_queue(pr, x, y,
2837                  MIN((gint)area->width, pr->width - x),
2838                  MIN((gint)area->height, pr->height - y),
2839                  FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2840 }
2841
2842 /*
2843  *-------------------------------------------------------------------
2844  * scrolling
2845  *-------------------------------------------------------------------
2846  */
2847
2848 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
2849 {
2850         gint old_x, old_y;
2851         gint x_off, y_off;
2852         gint w, h;
2853
2854         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2855
2856         if (!pr->pixbuf && !pr->source_tiles_enabled) return;
2857
2858         old_x = pr->x_scroll;
2859         old_y = pr->y_scroll;
2860
2861         pr->x_scroll += x;
2862         pr->y_scroll += y;
2863
2864         pr_scroll_clamp(pr);
2865         if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
2866
2867         pr_scroll_notify_signal(pr);
2868
2869         x_off = pr->x_scroll - old_x;
2870         y_off = pr->y_scroll - old_y;
2871
2872         w = pr->vis_width - abs(x_off);
2873         h = pr->vis_height - abs(y_off);
2874
2875         if (w < 1 || h < 1)
2876                 {
2877                 /* scrolled completely to new material */
2878                 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2879                 return;
2880                 }
2881         else
2882                 {
2883                 gint x1, y1;
2884                 gint x2, y2;
2885                 GtkWidget *box;
2886                 GdkGC *gc;
2887                 GdkEvent *event;
2888
2889                 if (x_off < 0)
2890                         {
2891                         x1 = abs(x_off);
2892                         x2 = 0;
2893                         }
2894                 else
2895                         {
2896                         x1 = 0;
2897                         x2 = abs(x_off);
2898                         }
2899
2900                 if (y_off < 0)
2901                         {
2902                         y1 = abs(y_off);
2903                         y2 = 0;
2904                         }
2905                 else
2906                         {
2907                         y1 = 0;
2908                         y2 = abs(y_off);
2909                         }
2910
2911                 box = GTK_WIDGET(pr);
2912
2913                 gc = gdk_gc_new(box->window);
2914                 gdk_gc_set_exposures(gc, TRUE);
2915                 gdk_draw_drawable(box->window, gc,
2916                                   box->window,
2917                                   x2 + pr->x_offset, y2 + pr->y_offset,
2918                                   x1 + pr->x_offset, y1 + pr->y_offset, w, h);
2919                 g_object_unref(gc);
2920
2921                 if (pr->overlay_list)
2922                         {
2923                         pr_overlay_queue_all(pr);
2924                         }
2925
2926                 w = pr->vis_width - w;
2927                 h = pr->vis_height - h;
2928
2929                 if (w > 0)
2930                         {
2931                         pr_queue(pr,
2932                                  x_off > 0 ? pr->x_scroll + (pr->vis_width - w) : pr->x_scroll, pr->y_scroll,
2933                                  w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2934                         }
2935                 if (h > 0)
2936                         {
2937                         /* FIXME, to optimize this, remove overlap */
2938                         pr_queue(pr,
2939                                  pr->x_scroll, y_off > 0 ? pr->y_scroll + (pr->vis_height - h) : pr->y_scroll,
2940                                  pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2941                         }
2942
2943                 /* process exposures here, "expose_event" seems to miss a few with obstructed windows */
2944                 while ((event = gdk_event_get_graphics_expose(box->window)) != NULL)
2945                         {
2946                         pixbuf_renderer_paint(pr, &event->expose.area);
2947
2948                         if (event->expose.count == 0)
2949                                 {
2950                                 gdk_event_free(event);
2951                                 break;
2952                                 }
2953                         gdk_event_free(event);
2954                         }
2955                 }
2956 }
2957
2958 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
2959                                      gdouble x_align, gdouble y_align)
2960 {
2961         gint px, py;
2962         gint ax, ay;
2963
2964         x_align = CLAMP(x_align, 0.0, 1.0);
2965         y_align = CLAMP(y_align, 0.0, 1.0);
2966
2967         ax = (gdouble)pr->vis_width * x_align;
2968         ay = (gdouble)pr->vis_height * y_align;
2969
2970         px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
2971         py = (gdouble)y * pr->scale - (pr->y_scroll + ay);
2972
2973         pixbuf_renderer_scroll(pr, px, py);
2974 }
2975
2976 /* get or set coordinates of viewport center in the image, in range 0.0 - 1.0 */
2977
2978 void pixbuf_renderer_get_scroll_center(PixbufRenderer *pr, gdouble *x, gdouble *y)
2979 {
2980         gint src_x, src_y;
2981         
2982         src_x = pr->x_scroll + pr->vis_width / 2;
2983         src_y = pr->y_scroll + pr->vis_height / 2;
2984
2985         *x = (gdouble)src_x / pr->width;
2986         *y = (gdouble)src_y / pr->height;
2987 }
2988
2989 void pixbuf_renderer_set_scroll_center(PixbufRenderer *pr, gdouble x, gdouble y)
2990 {
2991         gdouble dst_x, dst_y;
2992
2993         dst_x = x * pr->width  - pr->vis_width  / 2 - pr->x_scroll + CLAMP(pr->subpixel_x_scroll, -1.0, 1.0);
2994         dst_y = y * pr->height - pr->vis_height / 2 - pr->y_scroll + CLAMP(pr->subpixel_y_scroll, -1.0, 1.0);
2995
2996         pr->subpixel_x_scroll = dst_x - (int)dst_x;
2997         pr->subpixel_y_scroll = dst_y - (int)dst_y;
2998         
2999         pixbuf_renderer_scroll(pr, (int)dst_x, (int)dst_y);
3000 }
3001
3002
3003 /*
3004  *-------------------------------------------------------------------
3005  * mouse
3006  *-------------------------------------------------------------------
3007  */
3008
3009 static gint pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3010 {
3011         PixbufRenderer *pr;
3012         gint accel;
3013
3014         pr = PIXBUF_RENDERER(widget);
3015
3016         if (pr->scroller_id != -1)
3017                 {
3018                 pr->scroller_xpos = bevent->x;
3019                 pr->scroller_ypos = bevent->y;
3020                 }
3021
3022         if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
3023
3024         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3025                 {
3026                 pr->drag_moved++;
3027                 }
3028         else
3029                 {
3030                 widget_set_cursor(widget, GDK_FLEUR);
3031                 }
3032
3033         if (bevent->state & GDK_SHIFT_MASK)
3034                 {
3035                 accel = PR_PAN_SHIFT_MULTIPLIER;
3036                 }
3037         else
3038                 {
3039                 accel = 1;
3040                 }
3041
3042         /* do the scroll */
3043         pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
3044                                (pr->drag_last_y - bevent->y) * accel);
3045
3046         pr_drag_signal(pr, bevent);
3047
3048         pr->drag_last_x = bevent->x;
3049         pr->drag_last_y = bevent->y;
3050
3051         return FALSE;
3052 }
3053
3054 static gint pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3055 {
3056         PixbufRenderer *pr;
3057         GtkWidget *parent;
3058
3059         pr = PIXBUF_RENDERER(widget);
3060
3061         if (pr->scroller_id != -1) return TRUE;
3062
3063         switch (bevent->button)
3064                 {
3065                 case 1:
3066                         pr->in_drag = TRUE;
3067                         pr->drag_last_x = bevent->x;
3068                         pr->drag_last_y = bevent->y;
3069                         pr->drag_moved = 0;
3070                         gdk_pointer_grab(widget->window, FALSE,
3071                                          GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3072                                          NULL, NULL, bevent->time);
3073                         gtk_grab_add(widget);
3074                         break;
3075                 case 2:
3076                         pr->drag_moved = 0;
3077                         break;
3078                 case 3:
3079                         pr_clicked_signal(pr, bevent);
3080                         break;
3081                 default:
3082                         break;
3083                 }
3084
3085         parent = gtk_widget_get_parent(widget);
3086         if (widget && GTK_WIDGET_CAN_FOCUS(parent))
3087                 {
3088                 gtk_widget_grab_focus(parent);
3089                 }
3090
3091         return FALSE;
3092 }
3093
3094 static gint pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3095 {
3096         PixbufRenderer *pr;
3097
3098         pr = PIXBUF_RENDERER(widget);
3099
3100         if (pr->scroller_id != -1)
3101                 {
3102                 pr_scroller_stop(pr);
3103                 return TRUE;
3104                 }
3105
3106         if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
3107                 {
3108                 gtk_grab_remove(widget);
3109                 gdk_pointer_ungrab(bevent->time);
3110                 widget_set_cursor(widget, -1);
3111                 }
3112
3113         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3114                 {
3115                 if (bevent->button == 1 && (bevent->state & GDK_SHIFT_MASK))
3116                         {
3117                         pr_scroller_start(pr, bevent->x, bevent->y);
3118                         }
3119                 else if (bevent->button == 1 || bevent->button == 2)
3120                         {
3121                         pr_clicked_signal(pr, bevent);
3122                         }
3123                 }
3124
3125         pr->in_drag = FALSE;
3126
3127         return FALSE;
3128 }
3129
3130 static gint pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
3131 {
3132         PixbufRenderer *pr;
3133
3134         pr = PIXBUF_RENDERER(widget);
3135
3136         if (pr->scroller_id != -1)
3137                 {
3138                 pr->scroller_xpos = pr->scroller_x;
3139                 pr->scroller_ypos = pr->scroller_y;
3140                 pr->scroller_xinc = 0;
3141                 pr->scroller_yinc = 0;
3142                 }
3143
3144         return FALSE;
3145 }
3146
3147 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
3148 {
3149         PixbufRenderer *pr;
3150
3151         pr = PIXBUF_RENDERER(widget);
3152
3153         pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
3154 }
3155
3156 static void pr_signals_connect(PixbufRenderer *pr)
3157 {
3158         g_signal_connect(G_OBJECT(pr), "motion_notify_event",
3159                          G_CALLBACK(pr_mouse_motion_cb), pr);
3160         g_signal_connect(G_OBJECT(pr), "button_press_event",
3161                          G_CALLBACK(pr_mouse_press_cb), pr);
3162         g_signal_connect(G_OBJECT(pr), "button_release_event",
3163                          G_CALLBACK(pr_mouse_release_cb), pr);
3164         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
3165                          G_CALLBACK(pr_mouse_leave_cb), pr);
3166
3167         gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK |
3168                                               GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
3169                                               GDK_LEAVE_NOTIFY_MASK);
3170
3171         g_signal_connect(G_OBJECT(pr), "drag_begin",
3172                          G_CALLBACK(pr_mouse_drag_cb), pr);
3173
3174 }
3175
3176 /*
3177  *-------------------------------------------------------------------
3178  * public
3179  *-------------------------------------------------------------------
3180  */
3181
3182 static void pr_pixbuf_sync(PixbufRenderer *pr, gdouble zoom)
3183 {
3184         if (!pr->pixbuf)
3185                 {
3186                 GtkWidget *box;
3187
3188                 /* no pixbuf so just clear the window */
3189                 pr->image_width = 0;
3190                 pr->image_height = 0;
3191                 pr->scale = 1.0;
3192
3193                 box = GTK_WIDGET(pr);
3194
3195                 if (GTK_WIDGET_REALIZED(box))
3196                         {
3197                         gdk_window_clear(box->window);
3198                         pr_overlay_draw(pr, 0, 0, pr->window_width, pr->window_height, NULL);
3199                         }
3200
3201                 pr_update_signal(pr);
3202
3203                 return;
3204                 }
3205
3206         pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
3207         pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
3208
3209         pr_zoom_sync(pr, zoom, TRUE, TRUE, FALSE, 0, 0);
3210 }
3211
3212 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3213 {
3214         if (pixbuf) g_object_ref(pixbuf);
3215         if (pr->pixbuf) g_object_unref(pr->pixbuf);
3216         pr->pixbuf = pixbuf;
3217
3218         pr_pixbuf_sync(pr, zoom);
3219 }
3220
3221 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3222 {
3223         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3224
3225         pr_source_tile_unset(pr);
3226
3227         pr_set_pixbuf(pr, pixbuf, zoom);
3228
3229         pr_update_signal(pr);
3230 }
3231
3232 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
3233 {
3234         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
3235
3236         return pr->pixbuf;
3237 }
3238
3239 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
3240 {
3241         GObject *object;
3242         PixbufRendererScrollResetType scroll_reset;
3243
3244         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3245         g_return_if_fail(IS_PIXBUF_RENDERER(source));
3246
3247         if (pr == source) return;
3248
3249         object = G_OBJECT(pr);
3250
3251         g_object_set(object, "zoom_min", source->zoom_min, NULL);
3252         g_object_set(object, "zoom_max", source->zoom_max, NULL);
3253         g_object_set(object, "loading", source->loading, NULL);
3254
3255         pr->complete = source->complete;
3256
3257         pr->x_scroll = source->x_scroll;
3258         pr->y_scroll = source->y_scroll;
3259
3260         scroll_reset = pr->scroll_reset;
3261         pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
3262
3263         if (source->source_tiles_enabled)
3264                 {
3265                 pr_source_tile_unset(pr);
3266
3267                 pr->source_tiles_enabled = source->source_tiles_enabled;
3268                 pr->source_tiles_cache_size = source->source_tiles_cache_size;
3269                 pr->source_tile_width = source->source_tile_width;
3270                 pr->source_tile_height = source->source_tile_height;
3271                 pr->image_width = source->image_width;
3272                 pr->image_height = source->image_height;
3273
3274                 pr->func_tile_request = source->func_tile_request;
3275                 pr->func_tile_dispose = source->func_tile_dispose;
3276                 pr->func_tile_data = source->func_tile_data;
3277
3278                 pr->source_tiles = source->source_tiles;
3279                 source->source_tiles = NULL;
3280
3281                 pr_zoom_sync(pr, source->zoom, TRUE, TRUE, FALSE, 0, 0);
3282                 pr_redraw(pr, TRUE);
3283                 }
3284         else
3285                 {
3286                 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
3287                 }
3288
3289         pr->scroll_reset = scroll_reset;
3290
3291         pixbuf_renderer_set_pixbuf(source, NULL, source->zoom);
3292         pr_queue_clear(source);
3293         pr_tile_free_all(source);
3294 }
3295
3296 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
3297 {
3298         gint x1, y1, x2, y2;
3299
3300         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3301
3302         if (pr->source_tiles_enabled)
3303                 {
3304                 pr_source_tile_changed(pr, x, y, width, height);
3305                 }
3306
3307         if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
3308                 {
3309                 /* increase region when using a zoom quality that may access surrounding pixels */
3310                 y -= 1;
3311                 height += 2;
3312                 }
3313
3314         x1 = (gint)floor((double)x * pr->scale);
3315         y1 = (gint)floor((double)y * pr->scale);
3316         x2 = (gint)ceil((double)(x + width) * pr->scale);
3317         y2 = (gint)ceil((double)(y + height) * pr->scale);
3318
3319         pr_queue(pr, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
3320 }
3321
3322 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
3323 {
3324         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3325
3326         pr_zoom_adjust_real(pr, increment, FALSE, 0, 0);
3327 }
3328
3329 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
3330 {
3331         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3332
3333         pr_zoom_adjust_real(pr, increment, TRUE, x, y);
3334 }
3335
3336 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
3337 {
3338         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3339
3340         pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, 0, 0);
3341 }
3342
3343 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
3344 {
3345         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3346
3347         return pr->zoom;
3348 }
3349
3350 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
3351 {
3352         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3353
3354         return pr->scale;
3355 }
3356
3357 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
3358 {
3359         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3360
3361         if (min > 1.0 || max < 1.0) return;
3362         if (min < 1.0 && min > -1.0) return;
3363         if (min < -200.0 || max > 200.0) return;
3364
3365         if (pr->zoom_min != min)
3366                 {
3367                 pr->zoom_min = min;
3368                 g_object_notify(G_OBJECT(pr), "zoom_min");
3369                 }
3370         if (pr->zoom_max != max)
3371                 {
3372                 pr->zoom_max = max;
3373                 g_object_notify(G_OBJECT(pr), "zoom_max");
3374                 }
3375 }
3376
3377 gint pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
3378 {
3379         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3380         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3381
3382         if (!pr->pixbuf && !pr->source_tiles_enabled)
3383                 {
3384                 *width = 0;
3385                 *height = 0;
3386                 return FALSE;
3387                 }
3388
3389         *width = pr->image_width;
3390         *height = pr->image_height;
3391         return TRUE;
3392 }
3393
3394 gint pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
3395 {
3396         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3397         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3398
3399         if (!pr->pixbuf && !pr->source_tiles_enabled)
3400                 {
3401                 *width = 0;
3402                 *height = 0;
3403                 return FALSE;
3404                 }
3405
3406         *width = pr->width;
3407         *height = pr->height;
3408         return TRUE;
3409 }
3410
3411 gint pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
3412 {
3413         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3414         g_return_val_if_fail(rect != NULL, FALSE);
3415
3416         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
3417             !pr->scale)
3418                 {
3419                 rect->x = 0;
3420                 rect->y = 0;
3421                 rect->width = 0;
3422                 rect->height = 0;
3423                 return FALSE;
3424                 }
3425
3426         rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
3427         rect->y = (gint)((gdouble)pr->y_scroll / pr->scale);
3428         rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
3429         rect->height = (gint)((gdouble)pr->vis_height / pr->scale);
3430         return TRUE;
3431 }
3432
3433 gint pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
3434 {
3435         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3436         g_return_val_if_fail(rect != NULL, FALSE);
3437
3438         if ((!pr->pixbuf && !pr->source_tiles_enabled))
3439                 {
3440                 rect->x = 0;
3441                 rect->y = 0;
3442                 rect->width = 0;
3443                 rect->height = 0;
3444                 return FALSE;
3445                 }
3446
3447         rect->x = pr->x_scroll;
3448         rect->y = pr->y_scroll;
3449         rect->width = pr->vis_width;
3450         rect->height = pr->vis_height;
3451         return TRUE;
3452 }
3453
3454