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