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