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