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