Sat Apr 2 17:28:16 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         GdkWindowState state;
680
681         if (!pr->parent_window) return FALSE;
682         if (!pr->window_fit) return FALSE;
683         if (!GTK_WIDGET(pr)->window) return FALSE;
684
685         if (!pr->parent_window->window) return FALSE;
686         state = gdk_window_get_state(pr->parent_window->window);
687         if (state & GDK_WINDOW_STATE_MAXIMIZED) return FALSE;
688
689         return TRUE;
690 }
691
692 static gint pr_parent_window_resize(PixbufRenderer *pr, gint w, gint h)
693 {
694         GtkWidget *widget;
695         GtkWidget *parent;
696         gint ww, wh;
697
698         if (!pr_parent_window_sizable(pr)) return FALSE;
699
700         if (pr->window_limit)
701                 {
702                 gint sw = gdk_screen_width() * pr->window_limit_size / 100;
703                 gint sh = gdk_screen_height() * pr->window_limit_size / 100;
704
705                 if (w > sw) w = sw;
706                 if (h > sh) h = sh;
707                 }
708
709         widget = GTK_WIDGET(pr);
710         parent = GTK_WIDGET(pr->parent_window);
711
712         w += (parent->allocation.width - widget->allocation.width);
713         h += (parent->allocation.height - widget->allocation.height);
714
715         gdk_drawable_get_size(parent->window, &ww, &wh);
716         if (w == ww && h == wh) return FALSE;
717
718         gdk_window_resize(parent->window, w, h);
719
720         return TRUE;
721 }
722
723 void pixbuf_renderer_set_parent(PixbufRenderer *pr, GtkWindow *window)
724 {
725         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
726         g_return_if_fail(window == NULL || GTK_IS_WINDOW(window));
727
728         pr->parent_window = GTK_WIDGET(window);
729 }
730
731 GtkWindow *pixbuf_renderer_get_parent(PixbufRenderer *pr)
732 {
733         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
734
735         return GTK_WINDOW(pr->parent_window);
736 }
737
738
739 /*
740  *-------------------------------------------------------------------
741  * overlays
742  *-------------------------------------------------------------------
743  */
744
745 static void pr_overlay_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
746 {
747         GtkWidget *box;
748         GList *work;
749
750         box = GTK_WIDGET(pr);
751
752         work = pr->overlay_list;
753         while (work)
754                 {
755                 OverlayData *od;
756                 gint px, py, pw, ph;
757                 gint rx, ry, rw, rh;
758
759                 od = work->data;
760                 work = work->next;
761
762                 if (!od->visible) continue;
763
764                 pw = gdk_pixbuf_get_width(od->pixbuf);
765                 ph = gdk_pixbuf_get_height(od->pixbuf);
766                 px = od->x;
767                 py = od->y;
768
769                 if (od->relative)
770                         {
771                         if (px < 0) px = pr->window_width - pw + px;
772                         if (py < 0) py = pr->window_height - ph + py;
773                         }
774
775                 if (util_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
776                         {
777                         gdk_draw_pixbuf(box->window,
778                                         box->style->fg_gc[GTK_WIDGET_STATE(box)],
779                                         od->pixbuf,
780                                         rx - px, ry - py,
781                                         rx, ry, rw, rh,
782                                         pr->dither_quality, rx, ry);
783                         }
784                 }
785 }
786
787 static void pr_overlay_queue_draw(PixbufRenderer *pr, OverlayData *od, gint hidden)
788 {
789         gint x, y, w, h;
790         gint old_vis;
791
792         w = gdk_pixbuf_get_width(od->pixbuf);
793         h = gdk_pixbuf_get_height(od->pixbuf);
794         x = od->x;
795         y = od->y;
796
797         if (od->relative)
798                 {
799                 if (x < 0) x = pr->window_width - w + x;
800                 if (y < 0) y = pr->window_height - h + y;
801                 }
802
803         pr_queue(pr, pr->x_scroll - pr->x_offset + x,
804                  pr->y_scroll - pr->y_offset + y,
805                  w, h,
806                  FALSE, TILE_RENDER_ALL, FALSE);
807
808         old_vis = od->visible;
809         if (hidden) od->visible = FALSE;
810         pr_border_draw(pr, x, y, w, h);
811         od->visible = old_vis;
812 }
813
814 static void pr_overlay_queue_all(PixbufRenderer *pr)
815 {
816         GList *work;
817
818         work = pr->overlay_list;
819         while (work)
820                 {
821                 OverlayData *od = work->data;
822                 work = work->next;
823
824                 pr_overlay_queue_draw(pr, od, FALSE);
825                 }
826 }
827
828 static OverlayData *pr_overlay_find(PixbufRenderer *pr, gint id)
829 {
830         GList *work;
831
832         work = pr->overlay_list;
833         while (work)
834                 {
835                 OverlayData *od = work->data;
836                 work = work->next;
837
838                 if (od->id == id) return od;
839                 }
840
841         return NULL;
842 }
843
844 gint pixbuf_renderer_overlay_add(PixbufRenderer *pr, GdkPixbuf *pixbuf, gint x, gint y,
845                                  gint relative, gint always)
846 {
847         OverlayData *od;
848         gint id;
849
850         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
851         g_return_val_if_fail(pixbuf != NULL, -1);
852
853         id = 1;
854         while (pr_overlay_find(pr, id)) id++;
855
856         od = g_new0(OverlayData, 1);
857         od->id = id;
858         od->pixbuf = pixbuf;
859         g_object_ref(G_OBJECT(od->pixbuf));
860         od->x = x;
861         od->y = y;
862         od->relative = relative;
863         od->visible = TRUE;
864         od->always = always;
865
866         pr->overlay_list = g_list_append(pr->overlay_list, od);
867
868         pr_overlay_queue_draw(pr, od, FALSE);
869
870         return od->id;
871 }
872
873 static void pr_overlay_free(PixbufRenderer *pr, OverlayData *od)
874 {
875         pr->overlay_list = g_list_remove(pr->overlay_list, od);
876
877         if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
878         g_free(od);
879 }
880
881 static void pr_overlay_list_clear(PixbufRenderer *pr)
882 {
883         while (pr->overlay_list)
884                 {
885                 OverlayData *od;
886
887                 od = pr->overlay_list->data;
888                 pr_overlay_free(pr, od);
889                 }
890 }
891
892 void pixbuf_renderer_overlay_set(PixbufRenderer *pr, gint id, GdkPixbuf *pixbuf, gint x, gint y)
893 {
894         OverlayData *od;
895
896         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
897
898         od = pr_overlay_find(pr, id);
899         if (!od) return;
900
901         if (pixbuf)
902                 {
903                 pr_overlay_queue_draw(pr, od, TRUE);
904
905                 g_object_ref(G_OBJECT(pixbuf));
906                 g_object_unref(G_OBJECT(od->pixbuf));
907                 od->pixbuf = pixbuf;
908
909                 od->x = x;
910                 od->y = y;
911
912                 pr_overlay_queue_draw(pr, od, FALSE);
913                 }
914         else
915                 {
916                 pr_overlay_queue_draw(pr, od, TRUE);
917                 pr_overlay_free(pr, od);
918                 }
919 }
920
921 gint pixbuf_renderer_overlay_get(PixbufRenderer *pr, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
922 {
923         OverlayData *od;
924
925         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
926
927         od = pr_overlay_find(pr, id);
928         if (!od) return FALSE;
929
930         if (pixbuf) *pixbuf = od->pixbuf;
931         if (x) *x = od->x;
932         if (y) *y = od->y;
933
934         return TRUE;
935 }
936
937 void pixbuf_renderer_overlay_remove(PixbufRenderer *pr, gint id)
938 {
939         pixbuf_renderer_overlay_set(pr, id, NULL, 0, 0);
940 }
941
942 /*
943  *-------------------------------------------------------------------
944  * scroller overlay
945  *-------------------------------------------------------------------
946  */
947
948
949 static gboolean pr_scroller_update_cb(gpointer data)
950 {
951         PixbufRenderer *pr = data;
952         gint x, y;
953         gint xinc, yinc;
954
955         /* this was a simple scroll by difference between scroller and mouse position,
956          * but all this math results in a smoother result and accounts for a dead zone.
957          */
958
959         if (abs(pr->scroller_xpos - pr->scroller_x) < PR_SCROLLER_DEAD_ZONE)
960                 {
961                 x = 0;
962                 }
963         else
964                 {
965                 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
966                 x = (pr->scroller_xpos - pr->scroller_x) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
967                 x += (x > 0) ? -shift : shift;
968                 }
969
970         if (abs(pr->scroller_ypos - pr->scroller_y) < PR_SCROLLER_DEAD_ZONE)
971                 {
972                 y = 0;
973                 }
974         else
975                 {
976                 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
977                 y = (pr->scroller_ypos - pr->scroller_y) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
978                 y += (y > 0) ? -shift : shift;
979                 }
980
981         if (abs(x) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
982                 {
983                 xinc = x;
984                 }
985         else
986                 {
987                 xinc = pr->scroller_xinc;
988
989                 if (x >= 0)
990                         {
991                         if (xinc < 0) xinc = 0;
992                         if (x < xinc) xinc = x;
993                         if (x > xinc) xinc = MIN(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
994                         }
995                 else
996                         {
997                         if (xinc > 0) xinc = 0;
998                         if (x > xinc) xinc = x;
999                         if (x < xinc) xinc = MAX(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
1000                         }
1001                 }
1002
1003         if (abs(y) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
1004                 {
1005                 yinc = y;
1006                 }
1007         else
1008                 {
1009                 yinc = pr->scroller_yinc;
1010
1011                 if (y >= 0)
1012                         {
1013                         if (yinc < 0) yinc = 0;
1014                         if (y < yinc) yinc = y;
1015                         if (y > yinc) yinc = MIN(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1016                         }
1017                 else
1018                         {
1019                         if (yinc > 0) yinc = 0;
1020                         if (y > yinc) yinc = y;
1021                         if (y < yinc) yinc = MAX(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1022                         }
1023                 }
1024
1025         pr->scroller_xinc = xinc;
1026         pr->scroller_yinc = yinc;
1027
1028         xinc = xinc / PR_SCROLLER_UPDATES_PER_SEC;
1029         yinc = yinc / PR_SCROLLER_UPDATES_PER_SEC;
1030
1031         pixbuf_renderer_scroll(pr, xinc, yinc);
1032
1033         return TRUE;
1034 }
1035
1036 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start)
1037 {
1038         if (pr->scroller_id != -1)
1039                 {
1040                 g_source_remove(pr->scroller_id);
1041                 pr->scroller_id = -1;
1042                 }
1043
1044         if (start)
1045                 {
1046                 pr->scroller_id = g_timeout_add(1000 / PR_SCROLLER_UPDATES_PER_SEC,
1047                                                 pr_scroller_update_cb, pr);
1048                 }
1049 }
1050
1051 static void pr_scroller_start(PixbufRenderer *pr, gint x, gint y)
1052 {
1053         if (pr->scroller_overlay == -1)
1054                 {
1055                 GdkPixbuf *pixbuf;
1056                 gint w, h;
1057
1058 #ifdef GQVIEW_BUILD
1059                 pixbuf = pixbuf_inline(PIXBUF_INLINE_SCROLLER);
1060 #else
1061                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
1062                 gdk_pixbuf_fill(pixbuf, 0x000000ff);
1063 #endif
1064                 w = gdk_pixbuf_get_width(pixbuf);
1065                 h = gdk_pixbuf_get_height(pixbuf);
1066
1067                 pr->scroller_overlay = pixbuf_renderer_overlay_add(pr, pixbuf, x - w / 2, y - h / 2, FALSE, TRUE);
1068                 g_object_unref(pixbuf);
1069                 }
1070
1071         pr->scroller_x = x;
1072         pr->scroller_y = y;
1073         pr->scroller_xpos = x;
1074         pr->scroller_ypos = y;
1075
1076         pr_scroller_timer_set(pr, TRUE);
1077 }
1078
1079 static void pr_scroller_stop(PixbufRenderer *pr)
1080 {
1081         if (pr->scroller_id == -1) return;
1082
1083         pixbuf_renderer_overlay_remove(pr, pr->scroller_overlay);
1084         pr->scroller_overlay = -1;
1085
1086         pr_scroller_timer_set(pr, FALSE);
1087 }
1088
1089 /*
1090  *-------------------------------------------------------------------
1091  * borders
1092  *-------------------------------------------------------------------
1093  */
1094
1095 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1096 {
1097         GtkWidget *box;
1098         gint rx, ry, rw, rh;
1099
1100         box = GTK_WIDGET(pr);
1101
1102         if (!box->window) return;
1103
1104         if (!pr->pixbuf && !pr->source_tiles_enabled)
1105                 {
1106                 if (util_clip_region(x, y, w, h,
1107                         0, 0,
1108                         pr->window_width, pr->window_height,
1109                         &rx, &ry, &rw, &rh))
1110                         {
1111                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1112                         pr_overlay_draw(pr, rx, ry, rw, rh);
1113                         }
1114                 return;
1115                 }
1116
1117         if (pr->vis_width < pr->window_width)
1118                 {
1119                 if (pr->x_offset > 0 &&
1120                     util_clip_region(x, y, w, h,
1121                         0, 0,
1122                         pr->x_offset, pr->window_height,
1123                         &rx, &ry, &rw, &rh))
1124                         {
1125                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1126                         pr_overlay_draw(pr, rx, ry, rw, rh);
1127                         }
1128                 if (pr->window_width - pr->vis_width - pr->x_offset > 0 &&
1129                     util_clip_region(x, y, w, h,
1130                         pr->x_offset + pr->vis_width, 0,
1131                         pr->window_width - pr->vis_width - pr->x_offset, pr->window_height,
1132                         &rx, &ry, &rw, &rh))
1133                         {
1134                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1135                         pr_overlay_draw(pr, rx, ry, rw, rh);
1136                         }
1137                 }
1138         if (pr->vis_height < pr->window_height)
1139                 {
1140                 if (pr->y_offset > 0 &&
1141                     util_clip_region(x, y, w, h,
1142                         pr->x_offset, 0,
1143                         pr->vis_width, pr->y_offset,
1144                         &rx, &ry, &rw, &rh))
1145                         {
1146                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1147                         pr_overlay_draw(pr, rx, ry, rw, rh);
1148                         }
1149                 if (pr->window_height - pr->vis_height - pr->y_offset > 0 &&
1150                     util_clip_region(x, y, w, h,
1151                         pr->x_offset, pr->y_offset + pr->vis_height,
1152                         pr->vis_width, pr->window_height - pr->vis_height - pr->y_offset,
1153                         &rx, &ry, &rw, &rh))
1154                         {
1155                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1156                         pr_overlay_draw(pr, rx, ry, rw, rh);
1157                         }
1158                 }
1159 }
1160
1161 static void pr_border_clear(PixbufRenderer *pr)
1162 {
1163         pr_border_draw(pr, 0, 0, pr->window_width, pr->window_height);
1164 }
1165
1166 void pixbuf_renderer_set_color(PixbufRenderer *pr, GdkColor *color)
1167 {
1168         GtkStyle *style;
1169         GtkWidget *widget;
1170
1171         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1172
1173         widget = GTK_WIDGET(pr);
1174
1175         style = gtk_style_copy(gtk_widget_get_style(widget));
1176         g_object_ref(G_OBJECT(style));
1177
1178         if (color)
1179                 {
1180                 GdkColor *slot;
1181
1182                 slot = &style->bg[GTK_STATE_NORMAL];
1183
1184                 slot->red = color->red;
1185                 slot->green = color->green;
1186                 slot->blue = color->blue;
1187                 }
1188
1189         gtk_widget_set_style(widget, style);
1190         g_object_unref(G_OBJECT(style));
1191
1192         if (GTK_WIDGET_VISIBLE(widget)) pr_border_clear(pr);
1193 }
1194
1195 void pixbuf_renderer_set_black(PixbufRenderer *pr, gint black)
1196 {
1197         GdkColor color = { 0, 0, 0, 0};
1198
1199         pixbuf_renderer_set_color(pr, &color);
1200 }
1201
1202
1203 /*
1204  *-------------------------------------------------------------------
1205  * source tiles
1206  *-------------------------------------------------------------------
1207  */
1208
1209 static void pr_source_tile_free(SourceTile *st)
1210 {
1211         if (!st) return;
1212
1213         if (st->pixbuf) g_object_unref(st->pixbuf);
1214         g_free(st);
1215 }
1216
1217 static void pr_source_tile_free_all(PixbufRenderer *pr)
1218 {
1219         GList *work;
1220
1221         work = pr->source_tiles;
1222         while (work)
1223                 {
1224                 SourceTile *st;
1225
1226                 st = work->data;
1227                 work = work->next;
1228
1229                 pr_source_tile_free(st);
1230                 }
1231
1232         g_list_free(pr->source_tiles);
1233         pr->source_tiles = NULL;
1234 }
1235
1236 static void pr_source_tile_unset(PixbufRenderer *pr)
1237 {
1238         pr_source_tile_free_all(pr);
1239         pr->source_tiles_enabled = FALSE;
1240 }
1241
1242 static gint pr_source_tile_visible(PixbufRenderer *pr, SourceTile *st)
1243 {
1244         gint x1, y1, x2, y2;
1245
1246         if (!st) return FALSE;
1247
1248         x1 = (pr->x_scroll / PR_TILE_SIZE) * PR_TILE_SIZE;
1249         y1 = (pr->y_scroll / PR_TILE_SIZE) * PR_TILE_SIZE;
1250         x2 = ((pr->x_scroll + pr->vis_width) / PR_TILE_SIZE) * PR_TILE_SIZE + PR_TILE_SIZE;
1251         y2 = ((pr->y_scroll + pr->vis_height) / PR_TILE_SIZE) * PR_TILE_SIZE + PR_TILE_SIZE;
1252
1253         return !((double)st->x * pr->scale > (double)x2 ||
1254                  (double)(st->x + pr->source_tile_width) * pr->scale < (double)x1 ||
1255                  (double)st->y * pr->scale > (double)y2 ||
1256                  (double)(st->y + pr->source_tile_height) * pr->scale < (double)y1);
1257 }
1258
1259 static SourceTile *pr_source_tile_new(PixbufRenderer *pr, gint x, gint y)
1260 {
1261         SourceTile *st = NULL;
1262         gint count;
1263
1264         g_return_val_if_fail(pr->source_tile_width >= 1 && pr->source_tile_height >= 1, NULL);
1265
1266         if (pr->source_tiles_cache_size < 4) pr->source_tiles_cache_size = 4;
1267
1268         count = g_list_length(pr->source_tiles);
1269         if (count >= pr->source_tiles_cache_size)
1270                 {
1271                 GList *work;
1272
1273                 work = g_list_last(pr->source_tiles);
1274                 while (work && count >= pr->source_tiles_cache_size)
1275                         {
1276                         SourceTile *needle;
1277
1278                         needle = work->data;
1279                         work = work->prev;
1280
1281                         if (!pr_source_tile_visible(pr, needle))
1282                                 {
1283                                 pr->source_tiles = g_list_remove(pr->source_tiles, needle);
1284
1285                                 if (pr->func_tile_dispose)
1286                                         {
1287                                         pr->func_tile_dispose(pr, needle->x, needle->y,
1288                                                               pr->source_tile_width, pr->source_tile_height,
1289                                                               needle->pixbuf, pr->func_tile_data);
1290                                         }
1291
1292                                 if (!st)
1293                                         {
1294                                         st = needle;
1295                                         }
1296                                 else
1297                                         {
1298                                         pr_source_tile_free(needle);
1299                                         }
1300
1301                                 count--;
1302                                 }
1303                         }
1304                 }
1305
1306         if (!st)
1307                 {
1308                 st = g_new0(SourceTile, 1);
1309                 st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
1310                                             pr->source_tile_width, pr->source_tile_height);
1311                 }
1312
1313         st->x = (x / pr->source_tile_width) * pr->source_tile_width;
1314         st->y = (y / pr->source_tile_height) * pr->source_tile_height;
1315         st->blank = TRUE;
1316
1317         pr->source_tiles = g_list_prepend(pr->source_tiles, st);
1318
1319         return st;
1320 }
1321
1322 static SourceTile *pr_source_tile_request(PixbufRenderer *pr, gint x, gint y)
1323 {
1324         SourceTile *st;
1325
1326         st = pr_source_tile_new(pr, x, y);
1327         if (!st) return NULL;
1328
1329         if (pr->func_tile_request &&
1330             pr->func_tile_request(pr, st->x, st->y,
1331                                    pr->source_tile_width, pr->source_tile_height, st->pixbuf, pr->func_tile_data))
1332                 {
1333                 st->blank = FALSE;
1334                 }
1335
1336         pr_tile_invalidate_region(pr, st->x * pr->scale, st->y * pr->scale,
1337                                   pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1338
1339         return st;
1340 }
1341
1342 static SourceTile *pr_source_tile_find(PixbufRenderer *pr, gint x, gint y)
1343 {
1344         GList *work;
1345
1346         work = pr->source_tiles;
1347         while (work)
1348                 {
1349                 SourceTile *st = work->data;
1350
1351                 if (x >= st->x && x < st->x + pr->source_tile_width &&
1352                     y >= st->y && y < st->y + pr->source_tile_height)
1353                         {
1354                         if (work != pr->source_tiles)
1355                                 {
1356                                 pr->source_tiles = g_list_remove_link(pr->source_tiles, work);
1357                                 pr->source_tiles = g_list_concat(work, pr->source_tiles);
1358                                 }
1359                         return st;
1360                         }
1361
1362                 work = work->next;
1363                 }
1364
1365         return NULL;
1366 }
1367
1368 static GList *pr_source_tile_compute_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h, gint request)
1369 {
1370         gint x1, y1;
1371         GList *list = NULL;
1372         gint sx, sy;
1373
1374         if (x < 0) x = 0;
1375         if (y < 0) y = 0;
1376         if (w > pr->image_width) w = pr->image_width;
1377         if (h > pr->image_height) h = pr->image_height;
1378
1379         sx = (x / pr->source_tile_width) * pr->source_tile_width;
1380         sy = (y / pr->source_tile_height) * pr->source_tile_height;
1381
1382         for (x1 = sx; x1 < x + w; x1+= pr->source_tile_width)
1383                 {
1384                 for (y1 = sy; y1 < y + h; y1 += pr->source_tile_height)
1385                         {
1386                         SourceTile *st;
1387
1388                         st = pr_source_tile_find(pr, x1, y1);
1389                         if (!st && request) st = pr_source_tile_request(pr, x1, y1);
1390
1391                         if (st) list = g_list_prepend(list, st);
1392                         }
1393                 }
1394
1395         return g_list_reverse(list);
1396 }
1397
1398 static void pr_source_tile_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
1399 {
1400         GList *work;
1401
1402         work = pr->source_tiles;
1403         while (work)
1404                 {
1405                 SourceTile *st;
1406                 gint rx, ry, rw, rh;
1407
1408                 st = work->data;
1409                 work = work->next;
1410
1411                 if (util_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1412                                      x, y, width, height,
1413                                      &rx, &ry, &rw, &rh))
1414                         {
1415                         GdkPixbuf *pixbuf;
1416
1417                         pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh);
1418                         if (pr->func_tile_request &&
1419                             pr->func_tile_request(pr, rx, ry, rw, rh, pixbuf, pr->func_tile_data))
1420                                 {
1421                                 pr_tile_invalidate_region(pr, rx * pr->scale, ry * pr->scale,
1422                                                               rw * pr->scale, rh * pr->scale);
1423                                 }
1424                         g_object_unref(pixbuf);
1425                         }
1426                 }
1427 }
1428
1429 static gint pr_source_tile_render(PixbufRenderer *pr, ImageTile *it,
1430                                   gint x, gint y, gint w, gint h,
1431                                   gint new_data, gint fast)
1432 {
1433         GtkWidget *box;
1434         GList *list;
1435         GList *work;
1436         gint draw = FALSE;
1437
1438         box = GTK_WIDGET(pr);
1439
1440         if (pr->zoom == 1.0 || pr->scale == 1.0)
1441                 {
1442                 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1443                 work = list;
1444                 while (work)
1445                         {
1446                         SourceTile *st;
1447                         gint rx, ry, rw, rh;
1448
1449                         st = work->data;
1450                         work = work->next;
1451
1452                         if (util_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1453                                              it->x + x, it->y + y, w, h,
1454                                              &rx, &ry, &rw, &rh))
1455                                 {
1456                                 if (st->blank)
1457                                         {
1458                                         gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1459                                                            rx - st->x, ry - st->y, rw, rh);
1460                                         }
1461                                 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1462                                         {
1463                                         gdk_draw_pixbuf(it->pixmap,
1464                                                         box->style->fg_gc[GTK_WIDGET_STATE(box)],
1465                                                         st->pixbuf,
1466                                                         rx - st->x, ry - st->y,
1467                                                         rx - it->x, ry - it->y,
1468                                                         rw, rh,
1469                                                         pr->dither_quality, rx, ry);
1470                                         }
1471                                 }
1472                         }
1473                 }
1474         else
1475                 {
1476                 double scale_x, scale_y;
1477                 gint sx, sy, sw, sh;
1478
1479                 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1480                 scale_x = (double)pr->width / pr->image_width;
1481                 scale_y = (double)pr->height / pr->image_height;
1482
1483                 sx = (double)(it->x + x) / scale_x;
1484                 sy = (double)(it->y + y) / scale_y;
1485                 sw = (double)w / scale_x;
1486                 sh = (double)h / scale_y;
1487
1488                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1489
1490 #if 0
1491                 /* draws red over draw region, to check for leaks (regions not filled) */
1492                 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1493 #endif
1494
1495                 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1496                 work = list;
1497                 while (work)
1498                         {
1499                         SourceTile *st;
1500                         gint rx, ry, rw, rh;
1501                         gint stx, sty, stw, sth;
1502
1503                         st = work->data;
1504                         work = work->next;
1505
1506                         stx = floor((double)st->x * scale_x);
1507                         sty = floor((double)st->y * scale_y);
1508                         stw = ceil ((double)(st->x + pr->source_tile_width) * scale_x) - stx;
1509                         sth = ceil ((double)(st->y + pr->source_tile_height) * scale_y) - sty;
1510
1511                         if (util_clip_region(stx, sty, stw, sth,
1512                                              it->x + x, it->y + y, w, h,
1513                                              &rx, &ry, &rw, &rh))
1514                                 {
1515                                 if (st->blank)
1516                                         {
1517                                         gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1518                                                            rx - st->x, ry - st->y, rw, rh);
1519                                         }
1520                                 else
1521                                         {
1522                                         double offset_x;
1523                                         double offset_y;
1524
1525                                         /* may need to use unfloored stx,sty values here */
1526                                         offset_x = (double)(stx - it->x);
1527                                         offset_y = (double)(sty - it->y);
1528
1529                                         gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1530                                                  (double) 0.0 + offset_x,
1531                                                  (double) 0.0 + offset_y,
1532                                                  scale_x, scale_y,
1533                                                  (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1534                                         draw = TRUE;
1535                                         }
1536                                 }
1537                         }
1538                 }
1539
1540         g_list_free(list);
1541
1542         return draw;
1543 }
1544
1545 void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height,
1546                                gint tile_width, gint tile_height, gint cache_size,
1547                                PixbufRendererTileRequestFunc func_request,
1548                                PixbufRendererTileDisposeFunc func_dispose,
1549                                gpointer user_data,
1550                                gdouble zoom)
1551 {
1552         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1553         g_return_if_fail(tile_width >= 32 && tile_width >= 32);
1554         g_return_if_fail(width >= 32 && height > 32);
1555         g_return_if_fail(func_request != NULL);
1556
1557         if (pr->pixbuf) g_object_unref(pr->pixbuf);
1558         pr->pixbuf = NULL;
1559
1560         pr_source_tile_unset(pr);
1561
1562         if (cache_size < 4) cache_size = 4;
1563
1564         pr->source_tiles_enabled = TRUE;
1565         pr->source_tiles_cache_size = cache_size;
1566         pr->source_tile_width = tile_width;
1567         pr->source_tile_height = tile_height;
1568
1569         pr->image_width = width;
1570         pr->image_height = height;
1571
1572         pr->func_tile_request = func_request;
1573         pr->func_tile_dispose = func_dispose;
1574         pr->func_tile_data = user_data;
1575
1576         pr_zoom_sync(pr, zoom, TRUE, FALSE, TRUE, FALSE, 0, 0);
1577         pr_redraw(pr, TRUE);
1578 }
1579
1580 gint pixbuf_renderer_get_tiles(PixbufRenderer *pr)
1581 {
1582         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1583
1584         return pr->source_tiles_enabled;
1585 }
1586
1587 static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment,
1588                                 gint center_point, gint x, gint y)
1589 {
1590         gdouble zoom = pr->zoom;
1591
1592         if (increment == 0.0) return;
1593
1594         if (zoom == 0.0)
1595                 {
1596                 if (pr->scale < 1.0)
1597                         {
1598                         zoom = 0.0 - 1.0 / pr->scale;
1599                         }
1600                 else
1601                         {
1602                         zoom = pr->scale;
1603                         }
1604                 }
1605
1606         if (increment < 0.0)
1607                 {
1608                 if (zoom >= 1.0 && zoom + increment < 1.0)
1609                         {
1610                         zoom = zoom + increment - 2.0;
1611                         }
1612                 else
1613                         {
1614                         zoom = zoom + increment;
1615                         }
1616                 }
1617         else
1618                 {
1619                 if (zoom <= -1.0 && zoom + increment > -1.0)
1620                         {
1621                         zoom = zoom + increment + 2.0;
1622                         }
1623                 else
1624                         {
1625                         zoom = zoom + increment;
1626                         }
1627                 }
1628
1629         pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, center_point, x, y);
1630 }
1631
1632 /*
1633  *-------------------------------------------------------------------
1634  * display tiles
1635  *-------------------------------------------------------------------
1636  */
1637
1638 static ImageTile *pr_tile_new(gint x, gint y, gint width, gint height)
1639 {
1640         ImageTile *it;
1641
1642         it = g_new0(ImageTile, 1);
1643
1644         it->x = x;
1645         it->y = y;
1646         it->w = width;
1647         it->h = height;
1648
1649         it->render_done = TILE_RENDER_NONE;
1650
1651         return it;
1652 }
1653
1654 static void pr_tile_free(ImageTile *it)
1655 {
1656         if (!it) return;
1657
1658         if (it->pixbuf) gdk_pixbuf_unref(it->pixbuf);
1659         if (it->pixmap) g_object_unref(it->pixmap);
1660
1661         g_free(it);
1662 }
1663
1664 static void pr_tile_free_all(PixbufRenderer *pr)
1665 {
1666         GList *work;
1667
1668         work = pr->tiles;
1669         while (work)
1670                 {
1671                 ImageTile *it;
1672
1673                 it = work->data;
1674                 work = work->next;
1675
1676                 pr_tile_free(it);
1677                 }
1678
1679         g_list_free(pr->tiles);
1680         pr->tiles = NULL;
1681         pr->tile_cache_size = 0;
1682 }
1683
1684 static ImageTile *pr_tile_add(PixbufRenderer *pr, gint x, gint y)
1685 {
1686         ImageTile *it;
1687
1688         it = pr_tile_new(x, y, pr->tile_width, pr->tile_height);
1689
1690         if (it->x + it->w > pr->width) it->w = pr->width - it->x;
1691         if (it->y + it->h > pr->height) it->h = pr->height - it->y;
1692
1693         pr->tiles = g_list_prepend(pr->tiles, it);
1694         pr->tile_cache_size += it->size;
1695
1696         return it;
1697 }
1698
1699 static void pr_tile_remove(PixbufRenderer *pr, ImageTile *it)
1700 {
1701         if (it->qd)
1702                 {
1703                 QueueData *qd = it->qd;
1704
1705                 it->qd = NULL;
1706                 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
1707                 g_free(qd);
1708                 }
1709
1710         if (it->qd2)
1711                 {
1712                 QueueData *qd = it->qd2;
1713
1714                 it->qd2 = NULL;
1715                 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
1716                 g_free(qd);
1717                 }
1718
1719         pr->tiles = g_list_remove(pr->tiles, it);
1720         pr->tile_cache_size -= it->size;
1721
1722         pr_tile_free(it);
1723 }
1724
1725 static void pr_tile_free_space(PixbufRenderer *pr, guint space, ImageTile *it)
1726 {
1727         GList *work;
1728         gint tile_max;
1729
1730         work = g_list_last(pr->tiles);
1731
1732         if (pr->source_tiles_enabled && pr->scale < 1.0)
1733                 {
1734                 gint tiles;
1735
1736                 tiles = (pr->vis_width / PR_TILE_SIZE + 1) * (pr->vis_width / PR_TILE_SIZE + 1);
1737                 tile_max = MAX(tiles * PR_TILE_SIZE * PR_TILE_SIZE * 3,
1738                                (gint)((double)pr->tile_cache_max * 1048576.0 * pr->scale));
1739                 }
1740         else
1741                 {
1742                 tile_max = pr->tile_cache_max * 1048576;
1743                 }
1744
1745         while (work && pr->tile_cache_size + space > tile_max)
1746                 {
1747                 ImageTile *needle;
1748
1749                 needle = work->data;
1750                 work = work->prev;
1751                 if (needle != it &&
1752                     ((!needle->qd && !needle->qd2) || !pr_tile_is_visible(pr, needle))) pr_tile_remove(pr, needle);
1753                 }
1754 }
1755
1756 static void pr_tile_invalidate_all(PixbufRenderer *pr)
1757 {
1758         GList *work;
1759
1760         work = pr->tiles;
1761         while (work)
1762                 {
1763                 ImageTile *it;
1764
1765                 it = work->data;
1766                 work = work->next;
1767
1768                 it->render_done = TILE_RENDER_NONE;
1769                 it->render_todo = TILE_RENDER_NONE;
1770                 it->blank = FALSE;
1771
1772                 it->w = MIN(pr->tile_width, pr->width - it->x);
1773                 it->h = MIN(pr->tile_height, pr->height - it->y);
1774                 }
1775 }
1776
1777 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1778 {
1779         gint x1, x2;
1780         gint y1, y2;
1781         GList *work;
1782
1783         x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
1784         x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
1785
1786         y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
1787         y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
1788
1789         work = pr->tiles;
1790         while (work)
1791                 {
1792                 ImageTile *it;
1793
1794                 it = work->data;
1795                 work = work->next;
1796
1797                 if (it->x < x2 && it->x + it->w > x1 &&
1798                     it->y < y2 && it->y + it->h > y1)
1799                         {
1800                         it->render_done = TILE_RENDER_NONE;
1801                         it->render_todo = TILE_RENDER_NONE;
1802                         }
1803                 }
1804 }
1805
1806 static ImageTile *pr_tile_get(PixbufRenderer *pr, gint x, gint y)
1807 {
1808         GList *work;
1809
1810         work = pr->tiles;
1811         while (work)
1812                 {
1813                 ImageTile *it;
1814
1815                 it = work->data;
1816                 if (it->x == x && it->y == y)
1817                         {
1818                         pr->tiles = g_list_delete_link(pr->tiles, work);
1819                         pr->tiles = g_list_prepend(pr->tiles, it);
1820                         return it;
1821                         }
1822
1823                 work = work->next;
1824                 }
1825
1826         return pr_tile_add(pr, x, y);
1827 }
1828
1829 static void pr_tile_prepare(PixbufRenderer *pr, ImageTile *it)
1830 {
1831         if (!it->pixmap)
1832                 {
1833                 GdkPixmap *pixmap;
1834                 guint size;
1835
1836                 pixmap = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
1837
1838                 size = pixmap_calc_size(pixmap);
1839                 pr_tile_free_space(pr, size, it);
1840
1841                 it->pixmap = pixmap;
1842                 it->size += size;
1843                 pr->tile_cache_size += size;
1844                 }
1845         
1846         if ((pr->zoom != 1.0 || pr->source_tiles_enabled || (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf)) ) &&
1847             !it->pixbuf)
1848                 {
1849                 GdkPixbuf *pixbuf;
1850                 guint size;
1851
1852                 if (pr->pixbuf)
1853                         {
1854                         pixbuf = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(pr->pixbuf),
1855                                                 gdk_pixbuf_get_has_alpha(pr->pixbuf),
1856                                                 gdk_pixbuf_get_bits_per_sample(pr->pixbuf),
1857                                                 pr->tile_width, pr->tile_height);
1858                         }
1859                 else
1860                         {
1861                         pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
1862                         }
1863
1864                 size = gdk_pixbuf_get_rowstride(pixbuf) * pr->tile_height;
1865                 pr_tile_free_space(pr, size, it);
1866
1867                 it->pixbuf = pixbuf;
1868                 it->size += size;
1869                 pr->tile_cache_size += size;
1870                 }
1871 }
1872
1873 /*
1874  *-------------------------------------------------------------------
1875  * drawing
1876  *-------------------------------------------------------------------
1877  */
1878
1879 static void pr_tile_render(PixbufRenderer *pr, ImageTile *it,
1880                            gint x, gint y, gint w, gint h,
1881                            gint new_data, gint fast)
1882 {
1883         GtkWidget *box;
1884         gint has_alpha;
1885         gint draw = FALSE;
1886
1887         if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return;
1888
1889         if (it->render_done != TILE_RENDER_ALL)
1890                 {
1891                 x = 0;
1892                 y = 0;
1893                 w = it->w;
1894                 h = it->h;
1895                 if (!fast) it->render_done = TILE_RENDER_ALL;
1896                 }
1897         else if (it->render_todo != TILE_RENDER_AREA)
1898                 {
1899                 if (!fast) it->render_todo = TILE_RENDER_NONE;
1900                 return;
1901                 }
1902
1903         if (!fast) it->render_todo = TILE_RENDER_NONE;
1904
1905         if (new_data) it->blank = FALSE;
1906
1907         pr_tile_prepare(pr, it);
1908         has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1909
1910         box = GTK_WIDGET(pr);
1911
1912         /* FIXME checker colors for alpha should be configurable,
1913          * also should be drawn for blank = TRUE
1914          */
1915
1916         if (it->blank)
1917                 {
1918                 /* no data, do fast rect fill */
1919                 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1920                                    0, 0, it->w, it->h);
1921                 }
1922         else if (pr->source_tiles_enabled)
1923                 {
1924                 draw = pr_source_tile_render(pr, it, x, y, w, h, new_data, fast);
1925                 }
1926         else if (pr->zoom == 1.0 || pr->scale == 1.0)
1927                 {
1928                 if (has_alpha)
1929                         {
1930                         gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, x, y, w, h,
1931                                          (double) 0.0 - it->x,
1932                                          (double) 0.0 - it->y,
1933                                          1.0, 1.0, GDK_INTERP_NEAREST,
1934                                          255, it->x + x, it->y + y,
1935                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1936                         draw = TRUE;
1937                         }
1938                 else
1939                         {
1940                         /* faster, simple */
1941                         gdk_draw_pixbuf(it->pixmap,
1942                                         box->style->fg_gc[GTK_WIDGET_STATE(box)],
1943                                         pr->pixbuf,
1944                                         it->x + x, it->y + y,
1945                                         x, y,
1946                                         w, h,
1947                                         pr->dither_quality, it->x + x, it->y + y);
1948                         }
1949                 }
1950         else
1951                 {
1952                 double scale_x, scale_y;
1953
1954                 if (pr->image_width == 0 || pr->image_height == 0) return;
1955                 scale_x = (double)pr->width / pr->image_width;
1956                 scale_y = (double)pr->height / pr->image_height;
1957
1958                 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1959                  * small sizes for anything but GDK_INTERP_NEAREST
1960                  */
1961                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1962
1963                 if (!has_alpha)
1964                         {
1965                         gdk_pixbuf_scale(pr->pixbuf, it->pixbuf, x, y, w, h,
1966                                          (double) 0.0 - it->x,
1967                                          (double) 0.0 - it->y,
1968                                          scale_x, scale_y,
1969                                          (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1970                         }
1971                 else
1972                         {
1973                         gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, x, y, w, h,
1974                                          (double) 0.0 - it->x,
1975                                          (double) 0.0 - it->y,
1976                                          scale_x, scale_y,
1977                                          (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1978                                          255, it->x + x, it->y + y,
1979                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1980                         }
1981                 draw = TRUE;
1982                 }
1983
1984         if (draw && it->pixbuf && !it->blank)
1985                 {
1986                 gdk_draw_pixbuf(it->pixmap,
1987                                 box->style->fg_gc[GTK_WIDGET_STATE(box)],
1988                                 it->pixbuf,
1989                                 x, y,
1990                                 x, y,
1991                                 w, h,
1992                                 pr->dither_quality, it->x + x, it->y + y);
1993                 }
1994 }
1995
1996
1997 static void pr_tile_expose(PixbufRenderer *pr, ImageTile *it,
1998                            gint x, gint y, gint w, gint h,
1999                            gint new_data, gint fast)
2000 {
2001         GtkWidget *box;
2002
2003         pr_tile_render(pr, it, x, y, w, h, new_data, fast);
2004
2005         box = GTK_WIDGET(pr);
2006
2007         gdk_draw_drawable(box->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
2008                           it->pixmap, x, y,
2009                           pr->x_offset + (it->x - pr->x_scroll) + x, pr->y_offset + (it->y - pr->y_scroll) + y, w, h);
2010
2011         if (pr->overlay_list)
2012                 {
2013                 pr_overlay_draw(pr, pr->x_offset + (it->x - pr->x_scroll) + x,
2014                                 pr->y_offset + (it->y - pr->y_scroll) + y,
2015                                 w, h);
2016                 }
2017 }
2018
2019
2020 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it)
2021 {
2022         return (it->x + it->w >= pr->x_scroll && it->x <= pr->x_scroll + pr->window_width - pr->x_offset * 2 &&
2023                 it->y + it->h >= pr->y_scroll && it->y <= pr->y_scroll + pr->window_height - pr->y_offset * 2);
2024 }
2025
2026 /*
2027  *-------------------------------------------------------------------
2028  * draw queue
2029  *-------------------------------------------------------------------
2030  */
2031
2032 static gint pr_queue_draw_idle_cb(gpointer data)
2033 {
2034         PixbufRenderer *pr = data;
2035         QueueData *qd;
2036         gint fast;
2037
2038         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2039             (!pr->draw_queue && !pr->draw_queue_2pass) ||
2040             pr->draw_idle_id == -1)
2041                 {
2042                 pr_render_complete_signal(pr);
2043
2044                 pr->draw_idle_id = -1;
2045                 return FALSE;
2046                 }
2047
2048         if (pr->draw_queue)
2049                 {
2050                 qd = pr->draw_queue->data;
2051                 fast = (pr->zoom_2pass && pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0);
2052                 }
2053         else
2054                 {
2055                 if (pr->loading)
2056                         {
2057                         /* still loading, wait till done (also drops the higher priority) */
2058
2059                         pr->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
2060                                                            pr_queue_draw_idle_cb, pr, NULL);
2061                         pr->draw_idle_high = FALSE;
2062                         return FALSE;
2063                         }
2064
2065                 qd = pr->draw_queue_2pass->data;
2066                 fast = FALSE;
2067                 }
2068
2069         if (GTK_WIDGET_REALIZED(pr))
2070                 {
2071                 if (pr_tile_is_visible(pr, qd->it))
2072                         {
2073                         pr_tile_expose(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2074                         }
2075                 else if (qd->new_data)
2076                         {
2077                         /* if new pixel data, and we already have a pixmap, update the tile */
2078                         qd->it->blank = FALSE;
2079                         if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL)
2080                                 {
2081                                 pr_tile_render(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2082                                 }
2083                         }
2084                 }
2085
2086         if (pr->draw_queue)
2087                 {
2088                 qd->it->qd = NULL;
2089                 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
2090                 if (fast)
2091                         {
2092                         if (qd->it->qd2)
2093                                 {
2094                                 pr_queue_merge(qd->it->qd2, qd);
2095                                 g_free(qd);
2096                                 }
2097                         else
2098                                 {
2099                                 qd->it->qd2 = qd;
2100                                 pr->draw_queue_2pass = g_list_append(pr->draw_queue_2pass, qd);
2101                                 }
2102                         }
2103                 else
2104                         {
2105                         g_free(qd);
2106                         }
2107                 }
2108         else
2109                 {
2110                 qd->it->qd2 = NULL;
2111                 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
2112                 g_free(qd);
2113                 }
2114
2115         if (!pr->draw_queue && !pr->draw_queue_2pass)
2116                 {
2117                 pr_render_complete_signal(pr);
2118
2119                 pr->draw_idle_id = -1;
2120                 return FALSE;
2121                 }
2122
2123         return TRUE;
2124 }
2125
2126 static void pr_queue_list_free(GList *list)
2127 {
2128         GList *work;
2129
2130         work = list;
2131         while (work)
2132                 {
2133                 QueueData *qd;
2134
2135                 qd = work->data;
2136                 work = work->next;
2137
2138                 qd->it->qd = NULL;
2139                 qd->it->qd2 = NULL;
2140                 g_free(qd);
2141                 }
2142
2143         g_list_free(list);
2144 }
2145
2146 static void pr_queue_clear(PixbufRenderer *pr)
2147 {
2148         pr_queue_list_free(pr->draw_queue);
2149         pr->draw_queue = NULL;
2150
2151         pr_queue_list_free(pr->draw_queue_2pass);
2152         pr->draw_queue_2pass = NULL;
2153
2154         if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2155         pr->draw_idle_id = -1;
2156 }
2157
2158 static void pr_queue_merge(QueueData *parent, QueueData *qd)
2159 {
2160         if (parent->x + parent->w < qd->x + qd->w)
2161                 {
2162                 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
2163                 }
2164         if (parent->x > qd->x)
2165                 {
2166                 parent->w += parent->x - qd->x;
2167                 parent->x = qd->x;
2168                 }
2169
2170         if (parent->y + parent->h < qd->y + qd->h)
2171                 {
2172                 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
2173                 }
2174         if (parent->y > qd->y)
2175                 {
2176                 parent->h += parent->y - qd->y;
2177                 parent->y = qd->y;
2178                 }
2179
2180         parent->new_data |= qd->new_data;
2181 }
2182
2183 static gint pr_clamp_to_visible(PixbufRenderer *pr, gint *x, gint *y, gint *w, gint *h)
2184 {
2185         gint nx, ny;
2186         gint nw, nh;
2187         gint vx, vy;
2188         gint vw, vh;
2189
2190         vw = pr->vis_width;
2191         vh = pr->vis_height;
2192
2193         vx = pr->x_scroll;
2194         vy = pr->y_scroll;
2195
2196         if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
2197
2198         /* now clamp it */
2199         nx = CLAMP(*x, vx, vx + vw);
2200         nw = CLAMP(*w - (nx - *x), 1, vw);
2201
2202         ny = CLAMP(*y, vy, vy + vh);
2203         nh = CLAMP(*h - (ny - *y), 1, vh);
2204
2205         *x = nx;
2206         *y = ny;
2207         *w = nw;
2208         *h = nh;
2209
2210         return TRUE;
2211 }
2212
2213 static gint pr_queue_to_tiles(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2214                               gint clamp, ImageTileRenderType render, gint new_data)
2215 {
2216         gint i, j;
2217         gint x1, x2;
2218         gint y1, y2;
2219
2220         if (clamp && !pr_clamp_to_visible(pr, &x, &y, &w, &h)) return FALSE;
2221
2222         x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
2223         x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
2224
2225         y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
2226         y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
2227
2228         for (j = y1; j <= y2; j += pr->tile_height)
2229                 {
2230                 for (i = x1; i <= x2; i += pr->tile_width)
2231                         {
2232                         ImageTile *it;
2233
2234                         it = pr_tile_get(pr, i, j);
2235                         if (it)
2236                                 {
2237                                 QueueData *qd;
2238
2239                                 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
2240                                     (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
2241                                         {
2242                                         it->render_todo = render;
2243                                         }
2244
2245                                 qd = g_new(QueueData, 1);
2246                                 qd->it = it;
2247                                 qd->new_data = new_data;
2248
2249                                 if (i < x)
2250                                         {
2251                                         qd->x = x - i;
2252                                         }
2253                                 else
2254                                         {
2255                                         qd->x = 0;
2256                                         }
2257                                 qd->w = x + w - i - qd->x;
2258                                 if (qd->x + qd->w > pr->tile_width) qd->w = pr->tile_width - qd->x;
2259
2260                                 if (j < y)
2261                                         {
2262                                         qd->y = y - j;
2263                                         }
2264                                 else
2265                                         {
2266                                         qd->y = 0;
2267                                         }
2268                                 qd->h = y + h - j - qd->y;
2269                                 if (qd->y + qd->h > pr->tile_height) qd->h = pr->tile_height - qd->y;
2270
2271                                 if (qd->w < 1 || qd->h < 1)
2272                                         {
2273                                         g_free(qd);
2274                                         }
2275                                 else if (it->qd)
2276                                         {
2277                                         pr_queue_merge(it->qd, qd);
2278                                         g_free(qd);
2279                                         }
2280                                 else
2281                                         {
2282                                         it->qd = qd;
2283                                         pr->draw_queue = g_list_append(pr->draw_queue, qd);
2284                                         }
2285                                 }
2286                         }
2287                 }
2288
2289         return TRUE;
2290 }
2291
2292 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2293                      gint clamp, ImageTileRenderType render, gint new_data)
2294 {
2295         gint nx, ny;
2296
2297         nx = CLAMP(x, 0, pr->width - 1);
2298         ny = CLAMP(y, 0, pr->height - 1);
2299         w -= (nx - x);
2300         h -= (ny - y);
2301         w = CLAMP(w, 0, pr->width - nx);
2302         h = CLAMP(h, 0, pr->height - ny);
2303         if (w < 1 || h < 1) return;
2304
2305         if (pr_queue_to_tiles(pr, nx, ny, w, h, clamp, render, new_data) &&
2306             ((!pr->draw_queue && !pr->draw_queue_2pass) || pr->draw_idle_id == -1 || !pr->draw_idle_high))
2307                 {
2308                 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2309                 pr->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW,
2310                                                    pr_queue_draw_idle_cb, pr, NULL);
2311                 pr->draw_idle_high = TRUE;
2312                 }
2313 }
2314
2315 static void pr_redraw(PixbufRenderer *pr, gint new_data)
2316 {
2317         pr_queue_clear(pr);
2318         pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data);
2319 }
2320
2321 /*
2322  *-------------------------------------------------------------------
2323  * signal emission
2324  *-------------------------------------------------------------------
2325  */
2326
2327 static void pr_update_signal(PixbufRenderer *pr)
2328 {
2329 #if 0
2330         printf("FIXME: send updated signal\n");
2331 #endif
2332 }
2333
2334 static void pr_zoom_signal(PixbufRenderer *pr)
2335 {
2336         g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
2337 }
2338
2339 static void pr_clicked_signal(PixbufRenderer *pr, GdkEventButton *bevent)
2340 {
2341         g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
2342 }
2343
2344 static void pr_scroll_notify_signal(PixbufRenderer *pr)
2345 {
2346         g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
2347 }
2348
2349 static void pr_render_complete_signal(PixbufRenderer *pr)
2350 {
2351         if (!pr->complete)
2352                 {
2353                 g_signal_emit(pr, signals[SIGNAL_RENDER_COMPLETE], 0);
2354                 g_object_set(G_OBJECT(pr), "complete", TRUE, NULL);
2355                 }
2356 }
2357
2358 /*
2359  *-------------------------------------------------------------------
2360  * sync and clamp
2361  *-------------------------------------------------------------------
2362  */
2363
2364 static gint pr_scroll_clamp(PixbufRenderer *pr)
2365 {
2366         gint old_xs;
2367         gint old_ys;
2368
2369         if (pr->zoom == 0.0)
2370                 {
2371                 pr->x_scroll = 0;
2372                 pr->y_scroll = 0;
2373
2374                 pr_scroll_notify_signal(pr);
2375                 return FALSE;
2376                 }
2377
2378         old_xs = pr->x_scroll;
2379         old_ys = pr->y_scroll;
2380
2381         if (pr->x_offset > 0)
2382                 {
2383                 pr->x_scroll = 0;
2384                 }
2385         else
2386                 {
2387                 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
2388                 }
2389
2390         if (pr->y_offset > 0)
2391                 {
2392                 pr->y_scroll = 0;
2393                 }
2394         else
2395                 {
2396                 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
2397                 }
2398
2399         pr_scroll_notify_signal(pr);
2400
2401         return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
2402 }
2403
2404 static gint pr_size_clamp(PixbufRenderer *pr)
2405 {
2406         gint old_vw, old_vh;
2407
2408         old_vw = pr->vis_width;
2409         old_vh = pr->vis_height;
2410
2411         if (pr->width < pr->window_width)
2412                 {
2413                 pr->vis_width = pr->width;
2414                 pr->x_offset = (pr->window_width - pr->width) / 2;
2415                 }
2416         else
2417                 {
2418                 pr->vis_width = pr->window_width;
2419                 pr->x_offset = 0;
2420                 }
2421
2422         if (pr->height < pr->window_height)
2423                 {
2424                 pr->vis_height = pr->height;
2425                 pr->y_offset = (pr->window_height - pr->height) / 2;
2426                 }
2427         else
2428                 {
2429                 pr->vis_height = pr->window_height;
2430                 pr->y_offset = 0;
2431                 }
2432
2433         return (old_vw != pr->vis_width || old_vh != pr->vis_height);
2434 }
2435
2436 static gint pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom,
2437                           gint force, gint new, gint invalidate,
2438                           gint *redrawn)
2439 {
2440         gint w, h;
2441         gdouble scale;
2442         gint invalid;
2443
2444         zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
2445
2446         if (pr->zoom == zoom && !force) return FALSE;
2447
2448         w = pr->image_width;
2449         h = pr->image_height;
2450
2451         if (zoom == 0.0 && !pr->pixbuf)
2452                 {
2453                 scale = 1.0;
2454                 }
2455         else if (zoom == 0.0)
2456                 {
2457                 gint max_w;
2458                 gint max_h;
2459                 gint sizeable;
2460
2461                 sizeable = (new && pr_parent_window_sizable(pr));
2462
2463                 if (sizeable)
2464                         {
2465                         max_w = gdk_screen_width();
2466                         max_h = gdk_screen_height();
2467
2468                         if (pr->window_limit)
2469                                 {
2470                                 max_w = max_w * pr->window_limit_size / 100;
2471                                 max_h = max_h * pr->window_limit_size / 100;
2472                                 }
2473                         }
2474                 else
2475                         {
2476                         max_w = pr->window_width;
2477                         max_h = pr->window_height;
2478                         }
2479
2480                 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
2481                         {
2482                         if ((gdouble)max_w / w > (gdouble)max_h / h)
2483                                 {
2484                                 scale = (gdouble)max_h / h;
2485                                 h = max_h;
2486                                 w = w * scale + 0.5;
2487                                 if (w > max_w) w = max_w;
2488                                 }
2489                         else
2490                                 {
2491                                 scale = (gdouble)max_w / w;
2492                                 w = max_w;
2493                                 h = h * scale + 0.5;
2494                                 if (h > max_h) h = max_h;
2495                                 }
2496                         if (w < 1) w = 1;
2497                         if (h < 1) h = 1;
2498                         }
2499                 else
2500                         {
2501                         scale = 1.0;
2502                         }
2503                 }
2504         else if (zoom > 0.0) /* zoom orig, in */
2505                 {
2506                 scale = zoom;
2507                 w = w * scale;
2508                 h = h * scale;
2509                 }
2510         else /* zoom out */
2511                 {
2512                 scale = 1.0 / (0.0 - zoom);
2513                 w = w * scale;
2514                 h = h * scale;
2515                 }
2516
2517         invalid = (pr->width != w || pr->height != h);
2518
2519         pr->zoom = zoom;
2520         pr->width = w;
2521         pr->height = h;
2522         pr->scale = scale;
2523
2524         if (invalidate || invalid)
2525                 {
2526                 pr_tile_invalidate_all(pr);
2527                 pr_redraw(pr, TRUE);
2528                 }
2529         if (redrawn) *redrawn = (invalidate || invalid);
2530
2531         return TRUE;
2532 }
2533
2534 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
2535                          gint force, gint blank, gint new,
2536                          gint center_point, gint px, gint py)
2537 {
2538         gdouble old_scale;
2539         gint old_cx, old_cy;
2540         gint clamped;
2541         gint sized;
2542         gint redrawn = FALSE;
2543
2544         old_scale = pr->scale;
2545         if (center_point)
2546                 {
2547                 px = CLAMP(px, 0, pr->width);
2548                 py = CLAMP(py, 0, pr->height);
2549                 old_cx = pr->x_scroll + (px - pr->x_offset);
2550                 old_cy = pr->y_scroll + (py - pr->y_offset);
2551                 }
2552         else
2553                 {
2554                 px = py = 0;
2555                 old_cx = pr->x_scroll + pr->vis_width / 2;
2556                 old_cy = pr->y_scroll + pr->vis_height / 2;
2557                 }
2558
2559         if (!pr_zoom_clamp(pr, zoom, force, new, force, &redrawn)) return;
2560
2561         clamped = pr_size_clamp(pr);
2562         sized = pr_parent_window_resize(pr, pr->width, pr->height);
2563
2564         if (force)
2565                 {
2566                 switch (pr->scroll_reset)
2567                         {
2568                         case PR_SCROLL_RESET_NOCHANGE:
2569                                 /* maintain old scroll position, do nothing */
2570                                 break;
2571                         case PR_SCROLL_RESET_CENTER:
2572                                 /* center new image */
2573                                 pr->x_scroll = ((double)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2;
2574                                 pr->y_scroll = ((double)pr->image_height / 2.0 * pr->scale) - pr->vis_height / 2;
2575                                 break;
2576                         case PR_SCROLL_RESET_TOPLEFT:
2577                         default:
2578                                 /* reset to upper left */
2579                                 pr->x_scroll = 0;
2580                                 pr->y_scroll = 0;
2581                                 break;
2582                         }
2583                 }
2584         else
2585                 {
2586                 /* user zoom does not force, so keep visible center point */
2587                 if (center_point)
2588                         {
2589                         pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
2590                         pr->y_scroll = old_cy / old_scale * pr->scale - (py - pr->y_offset);
2591                         }
2592                 else
2593                         {
2594                         pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2);
2595                         pr->y_scroll = old_cy / old_scale * pr->scale - (pr->vis_height / 2);
2596                         }
2597                 }
2598
2599         pr_scroll_clamp(pr);
2600
2601 #if 0
2602         pr_tile_sync(pr, blank);
2603 #endif
2604
2605         /* If the window was not sized, redraw the image - we know there will be no size/expose signal.
2606          * But even if a size is claimed, there is no guarantee that the window manager will allow it,
2607          * so redraw the window anyway :/
2608          */
2609         if (sized || clamped) pr_border_clear(pr);
2610         pr_redraw(pr, redrawn);
2611
2612         pr_zoom_signal(pr);
2613         pr_update_signal(pr);
2614 }
2615
2616 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
2617 {
2618         if (pr->window_width == new_width && pr->window_height == new_height) return;
2619
2620         pr->window_width = new_width;
2621         pr->window_height = new_height;
2622
2623         if (pr->zoom == 0.0) pr_zoom_clamp(pr, 0.0, TRUE, FALSE, FALSE, NULL);
2624
2625         pr_size_clamp(pr);
2626         pr_scroll_clamp(pr);
2627
2628         /* ensure scroller remains visible */
2629         if (pr->scroller_overlay != -1)
2630                 {
2631                 gint update = FALSE;
2632
2633                 if (pr->scroller_x > new_width)
2634                         {
2635                         pr->scroller_x = new_width;
2636                         pr->scroller_xpos = new_width;
2637                         update = TRUE;
2638                         }
2639                 if (pr->scroller_y > new_height)
2640                         {
2641                         pr->scroller_y = new_height;
2642                         pr->scroller_ypos = new_height;
2643                         update = TRUE;
2644                         }
2645
2646                 if (update)
2647                         {
2648                         GdkPixbuf *pixbuf;
2649
2650                         if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
2651                                 {
2652                                 gint w, h;
2653
2654                                 w = gdk_pixbuf_get_width(pixbuf);
2655                                 h = gdk_pixbuf_get_height(pixbuf);
2656                                 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
2657                                                             pr->scroller_x - w / 2, pr->scroller_y - h / 2);
2658                                 }
2659                         }
2660                 }
2661
2662         pr_border_clear(pr);
2663
2664 #if 0
2665         pr_tile_sync(pr, pr->width, pr->height, FALSE);
2666 #endif
2667
2668         pr_update_signal(pr);
2669 }
2670
2671 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2672 {
2673         PixbufRenderer *pr = data;
2674
2675         pr_size_sync(pr, allocation->width, allocation->height);
2676 }
2677
2678 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
2679 {
2680         gint x, y;
2681
2682         pr_border_draw(pr, area->x, area->y, area->width, area->height);
2683
2684         x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
2685         y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
2686
2687         pr_queue(pr, x, y,
2688                  MIN((gint)area->width, pr->width - x),
2689                  MIN((gint)area->height, pr->height - y),
2690                  FALSE, TILE_RENDER_ALL, FALSE);
2691 }
2692
2693 /*
2694  *-------------------------------------------------------------------
2695  * scrolling
2696  *-------------------------------------------------------------------
2697  */
2698
2699 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
2700 {
2701         gint old_x, old_y;
2702         gint x_off, y_off;
2703         gint w, h;
2704
2705         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2706
2707         if (!pr->pixbuf && !pr->source_tiles_enabled) return;
2708
2709         old_x = pr->x_scroll;
2710         old_y = pr->y_scroll;
2711
2712         pr->x_scroll += x;
2713         pr->y_scroll += y;
2714
2715         pr_scroll_clamp(pr);
2716         if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
2717
2718         if (pr->overlay_list)
2719                 {
2720                 gint new_x, new_y;
2721
2722                 new_x = pr->x_scroll;
2723                 new_y = pr->y_scroll;
2724                 pr->x_scroll = old_x;
2725                 pr->y_scroll = old_y;
2726
2727                 pr_overlay_queue_all(pr);
2728
2729                 pr->x_scroll = new_x;
2730                 pr->y_scroll = new_y;
2731                 }
2732
2733         x_off = pr->x_scroll - old_x;
2734         y_off = pr->y_scroll - old_y;
2735
2736         w = pr->vis_width - abs(x_off);
2737         h = pr->vis_height - abs(y_off);
2738
2739         if (w < 1 || h < 1)
2740                 {
2741                 /* scrolled completely to new material */
2742                 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE);
2743                 return;
2744                 }
2745         else
2746                 {
2747                 gint x1, y1;
2748                 gint x2, y2;
2749                 GtkWidget *box;
2750                 GdkGC *gc;
2751
2752                 if (x_off < 0)
2753                         {
2754                         x1 = abs(x_off);
2755                         x2 = 0;
2756                         }
2757                 else
2758                         {
2759                         x1 = 0;
2760                         x2 = abs(x_off);
2761                         }
2762
2763                 if (y_off < 0)
2764                         {
2765                         y1 = abs(y_off);
2766                         y2 = 0;
2767                         }
2768                 else
2769                         {
2770                         y1 = 0;
2771                         y2 = abs(y_off);
2772                         }
2773
2774                 box = GTK_WIDGET(pr);
2775
2776                 gc = gdk_gc_new(box->window);
2777                 gdk_gc_set_exposures(gc, TRUE);
2778                 gdk_draw_drawable(box->window, gc,
2779                                   box->window,
2780                                   x2 + pr->x_offset, y2 + pr->y_offset,
2781                                   x1 + pr->x_offset, y1 + pr->y_offset, w, h);
2782                 g_object_unref(gc);
2783
2784                 if (pr->overlay_list)
2785                         {
2786                         pr_overlay_queue_all(pr);
2787                         }
2788
2789                 w = pr->vis_width - w;
2790                 h = pr->vis_height - h;
2791
2792                 if (w > 0)
2793                         {
2794                         pr_queue(pr,
2795                                  x_off > 0 ? pr->x_scroll + (pr->vis_width - w) : pr->x_scroll, pr->y_scroll,
2796                                  w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE);
2797                         }
2798                 if (h > 0)
2799                         {
2800                         /* FIXME, to optimize this, remove overlap */
2801                         pr_queue(pr,
2802                                  pr->x_scroll, y_off > 0 ? pr->y_scroll + (pr->vis_height - h) : pr->y_scroll,
2803                                  pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE);
2804                         }
2805                 }
2806 }
2807
2808 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
2809                                      gdouble x_align, gdouble y_align)
2810 {
2811         gint px, py;
2812         gint ax, ay;
2813
2814         x_align = CLAMP(x_align, 0.0, 1.0);
2815         y_align = CLAMP(y_align, 0.0, 1.0);
2816
2817         ax = (gdouble)pr->vis_width * x_align;
2818         ay = (gdouble)pr->vis_height * y_align;
2819
2820         px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
2821         py = (gdouble)y * pr->scale - (pr->y_scroll + ay);
2822
2823         pixbuf_renderer_scroll(pr, px, py);
2824 }
2825
2826 /*
2827  *-------------------------------------------------------------------
2828  * mouse
2829  *-------------------------------------------------------------------
2830  */
2831
2832 static gint pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2833 {
2834         PixbufRenderer *pr;
2835         gint accel;
2836
2837         pr = PIXBUF_RENDERER(widget);
2838
2839         if (pr->scroller_id != -1)
2840                 {
2841                 pr->scroller_xpos = bevent->x;
2842                 pr->scroller_ypos = bevent->y;
2843                 }
2844
2845         if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
2846
2847         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2848                 {
2849                 pr->drag_moved++;
2850                 }
2851         else
2852                 {
2853                 widget_set_cursor(widget, GDK_FLEUR);
2854                 }
2855
2856         if (bevent->state & GDK_SHIFT_MASK)
2857                 {
2858                 accel = PR_PAN_SHIFT_MULTIPLIER;
2859                 }
2860         else
2861                 {
2862                 accel = 1;
2863                 }
2864
2865         /* do the scroll */
2866         pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
2867                                (pr->drag_last_y - bevent->y) * accel);
2868
2869         pr->drag_last_x = bevent->x;
2870         pr->drag_last_y = bevent->y;
2871
2872         return FALSE;
2873 }
2874
2875 static gint pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2876 {
2877         PixbufRenderer *pr;
2878         GtkWidget *parent;
2879
2880         pr = PIXBUF_RENDERER(widget);
2881
2882         if (pr->scroller_id != -1) return TRUE;
2883
2884         switch (bevent->button)
2885                 {
2886                 case 1:
2887                         pr->in_drag = TRUE;
2888                         pr->drag_last_x = bevent->x;
2889                         pr->drag_last_y = bevent->y;
2890                         pr->drag_moved = 0;
2891                         gdk_pointer_grab(widget->window, FALSE,
2892                                          GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
2893                                          NULL, NULL, bevent->time);
2894                         gtk_grab_add(widget);
2895                         break;
2896                 case 2:
2897                         pr->drag_moved = 0;
2898                         break;
2899                 case 3:
2900                         pr_clicked_signal(pr, bevent);
2901                         break;
2902                 default:
2903                         break;
2904                 }
2905
2906         parent = gtk_widget_get_parent(widget);
2907         if (widget && GTK_WIDGET_CAN_FOCUS(parent))
2908                 {
2909                 gtk_widget_grab_focus(parent);
2910                 }
2911
2912         return FALSE;
2913 }
2914
2915 static gint pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2916 {
2917         PixbufRenderer *pr;
2918
2919         pr = PIXBUF_RENDERER(widget);
2920
2921         if (pr->scroller_id != -1)
2922                 {
2923                 pr_scroller_stop(pr);
2924                 return TRUE;
2925                 }
2926
2927         if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
2928                 {
2929                 gtk_grab_remove(widget);
2930                 gdk_pointer_ungrab(bevent->time);
2931                 widget_set_cursor(widget, -1);
2932                 }
2933
2934         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2935                 {
2936                 if (bevent->button == 1 && (bevent->state & GDK_SHIFT_MASK))
2937                         {
2938                         pr_scroller_start(pr, bevent->x, bevent->y);
2939                         }
2940                 else if (bevent->button == 1 || bevent->button == 2)
2941                         {
2942                         pr_clicked_signal(pr, bevent);
2943                         }
2944                 }
2945
2946         pr->in_drag = FALSE;
2947
2948         return FALSE;
2949 }
2950
2951 static gint pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
2952 {
2953         PixbufRenderer *pr;
2954
2955         pr = PIXBUF_RENDERER(widget);
2956
2957         if (pr->scroller_id != -1)
2958                 {
2959                 pr->scroller_xpos = pr->scroller_x;
2960                 pr->scroller_ypos = pr->scroller_y;
2961                 pr->scroller_xinc = 0;
2962                 pr->scroller_yinc = 0;
2963                 }
2964
2965         return FALSE;
2966 }
2967
2968 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
2969 {
2970         PixbufRenderer *pr;
2971
2972         pr = PIXBUF_RENDERER(widget);
2973
2974         pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
2975 }
2976
2977 static void pr_signals_connect(PixbufRenderer *pr)
2978 {
2979         g_signal_connect(G_OBJECT(pr), "motion_notify_event",
2980                          G_CALLBACK(pr_mouse_motion_cb), pr);
2981         g_signal_connect(G_OBJECT(pr), "button_press_event",
2982                          G_CALLBACK(pr_mouse_press_cb), pr);
2983         g_signal_connect(G_OBJECT(pr), "button_release_event",
2984                          G_CALLBACK(pr_mouse_release_cb), pr);
2985         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
2986                          G_CALLBACK(pr_mouse_leave_cb), pr);
2987
2988         gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK |
2989                                               GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
2990                                               GDK_LEAVE_NOTIFY_MASK);
2991
2992         g_signal_connect(G_OBJECT(pr), "drag_begin",
2993                          G_CALLBACK(pr_mouse_drag_cb), pr);
2994
2995 }
2996
2997 /*
2998  *-------------------------------------------------------------------
2999  * public
3000  *-------------------------------------------------------------------
3001  */
3002
3003 static void pr_pixbuf_sync(PixbufRenderer *pr, gdouble zoom, gint blank, gint new)
3004 {
3005         if (!pr->pixbuf)
3006                 {
3007                 GtkWidget *box;
3008
3009                 /* no pixbuf so just clear the window */
3010                 pr->image_width = 0;
3011                 pr->image_height = 0;
3012                 pr->scale = 1.0;
3013
3014                 box = GTK_WIDGET(pr);
3015
3016                 if (GTK_WIDGET_REALIZED(box))
3017                         {
3018                         gdk_window_clear(box->window);
3019                         pr_overlay_draw(pr, 0, 0, pr->window_width, pr->window_height);
3020                         }
3021
3022                 pr_update_signal(pr);
3023
3024                 return;
3025                 }
3026
3027         pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
3028         pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
3029
3030         pr_zoom_sync(pr, zoom, TRUE, blank, new, FALSE, 0, 0);
3031 }
3032
3033 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, gint new)
3034 {
3035         if (pixbuf) g_object_ref(pixbuf);
3036         if (pr->pixbuf) g_object_unref(pr->pixbuf);
3037         pr->pixbuf = pixbuf;
3038
3039         pr_pixbuf_sync(pr, zoom, FALSE, new);
3040 }
3041
3042 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3043 {
3044         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3045
3046         pr_source_tile_unset(pr);
3047
3048         pr_set_pixbuf(pr, pixbuf, zoom, TRUE);
3049
3050         pr_update_signal(pr);
3051 }
3052
3053 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
3054 {
3055         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
3056
3057         return pr->pixbuf;
3058 }
3059
3060 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
3061 {
3062         GObject *object;
3063         PixbufRendererScrollResetType scroll_reset;
3064
3065         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3066         g_return_if_fail(IS_PIXBUF_RENDERER(source));
3067
3068         if (pr == source) return;
3069
3070         object = G_OBJECT(pr);
3071
3072         g_object_set(object, "zoom_min", source->zoom_min, NULL);
3073         g_object_set(object, "zoom_max", source->zoom_max, NULL);
3074         g_object_set(object, "loading", source->loading, NULL);
3075
3076         pr->x_scroll = source->x_scroll;
3077         pr->y_scroll = source->y_scroll;
3078
3079         scroll_reset = pr->scroll_reset;
3080         pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
3081
3082         if (source->source_tiles_enabled)
3083                 {
3084                 pr_source_tile_unset(pr);
3085
3086                 pr->source_tiles_enabled = source->source_tiles_enabled;
3087                 pr->source_tiles_cache_size = source->source_tiles_cache_size;
3088                 pr->source_tile_width = source->source_tile_width;
3089                 pr->source_tile_height = source->source_tile_height;
3090                 pr->image_width = source->image_width;
3091                 pr->image_height = source->image_height;
3092
3093                 pr->func_tile_request = source->func_tile_request;
3094                 pr->func_tile_dispose = source->func_tile_dispose;
3095                 pr->func_tile_data = source->func_tile_data;
3096
3097                 pr->source_tiles = source->source_tiles;
3098                 source->source_tiles = NULL;
3099
3100                 pr_zoom_sync(pr, pr->zoom, TRUE, FALSE, TRUE, FALSE, 0, 0);
3101                 pr_redraw(pr, TRUE);
3102                 }
3103         else
3104                 {
3105                 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
3106                 }
3107
3108         pr->scroll_reset = scroll_reset;
3109
3110         pixbuf_renderer_set_pixbuf(source, NULL, source->zoom);
3111 }
3112
3113 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
3114 {
3115         gint x1, y1, x2, y2;
3116
3117         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3118
3119         if (pr->source_tiles_enabled)
3120                 {
3121                 pr_source_tile_changed(pr, x, y, width, height);
3122                 }
3123
3124         if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
3125                 {
3126                 /* increase region when using a zoom quality that may access surrounding pixels */
3127                 y -= 1;
3128                 height += 2;
3129                 }
3130
3131         x1 = (gint)floor((double)x * pr->scale);
3132         y1 = (gint)floor((double)y * pr->scale);
3133         x2 = (gint)ceil((double)(x + width) * pr->scale);
3134         y2 = (gint)ceil((double)(y + height) * pr->scale);
3135
3136         pr_queue(pr, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE);
3137 }
3138
3139 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
3140 {
3141         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3142
3143         pr_zoom_adjust_real(pr, increment, FALSE, 0, 0);
3144 }
3145
3146 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
3147 {
3148         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3149
3150         pr_zoom_adjust_real(pr, increment, TRUE, x, y);
3151 }
3152
3153 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
3154 {
3155         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3156
3157         pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, FALSE, 0, 0);
3158 }
3159
3160 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
3161 {
3162         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3163
3164         return pr->zoom;
3165 }
3166
3167 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
3168 {
3169         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3170
3171         return pr->scale;
3172 }
3173
3174 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
3175 {
3176         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3177
3178         if (min > 1.0 || max < 1.0) return;
3179         if (min < 1.0 && min > -1.0) return;
3180         if (min < -200.0 || max > 200.0) return;
3181
3182         if (pr->zoom_min != min)
3183                 {
3184                 pr->zoom_min = min;
3185                 g_object_notify(G_OBJECT(pr), "zoom_min");
3186                 }
3187         if (pr->zoom_max != max)
3188                 {
3189                 pr->zoom_max = max;
3190                 g_object_notify(G_OBJECT(pr), "zoom_max");
3191                 }
3192 }
3193
3194 gint pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
3195 {
3196         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3197         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3198
3199         if (!pr->pixbuf && !pr->source_tiles_enabled)
3200                 {
3201                 *width = 0;
3202                 *height = 0;
3203                 return FALSE;
3204                 }
3205
3206         *width = pr->image_width;
3207         *height = pr->image_height;
3208         return TRUE;
3209 }
3210
3211 gint pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
3212 {
3213         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3214         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3215
3216         if (!pr->pixbuf && !pr->source_tiles_enabled)
3217                 {
3218                 *width = 0;
3219                 *height = 0;
3220                 return FALSE;
3221                 }
3222
3223         *width = pr->width;
3224         *height = pr->height;
3225         return TRUE;
3226 }
3227
3228 gint pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
3229 {
3230         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3231         g_return_val_if_fail(rect != NULL, FALSE);
3232
3233         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
3234             !pr->scale)
3235                 {
3236                 rect->x = 0;
3237                 rect->y = 0;
3238                 rect->width = 0;
3239                 rect->height = 0;
3240                 return FALSE;
3241                 }
3242
3243         rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
3244         rect->y = (gint)((gdouble)pr->y_scroll / pr->scale);
3245         rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
3246         rect->height = (gint)((gdouble)pr->vis_height / pr->scale);
3247         return TRUE;
3248 }
3249
3250 gint pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
3251 {
3252         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3253         g_return_val_if_fail(rect != NULL, FALSE);
3254
3255         if ((!pr->pixbuf && !pr->source_tiles_enabled))
3256                 {
3257                 rect->x = 0;
3258                 rect->y = 0;
3259                 rect->width = 0;
3260                 rect->height = 0;
3261                 return FALSE;
3262                 }
3263
3264         rect->x = pr->x_scroll;
3265         rect->y = pr->y_scroll;
3266         rect->width = pr->vis_width;
3267         rect->height = pr->vis_height;
3268         return TRUE;
3269 }
3270
3271