Sat May 14 13:04:23 2005 John Ellis <johne@verizon.net>
[geeqie.git] / src / pixbuf-renderer.c
1 /*
2  * GQview
3  * (C) 2005 John Ellis
4  *
5  * Author: John Ellis
6  *
7  * This software is released under the GNU General Public License (GNU GPL).
8  * Please read the included file COPYING for more information.
9  * This software comes with no warranty of any kind, use at your own risk!
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h>
15 #include "pixbuf-renderer.h"
16 #include "intl.h"
17
18 #include <gtk/gtk.h>
19
20
21 /* comment this out if not using this from within GQview
22  * defining GQVIEW_BUILD does these things:
23  *   - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
24  */
25 #define GQVIEW_BUILD 1
26
27
28 #ifdef GQVIEW_BUILD
29         #include "pixbuf_util.h"
30 #endif
31
32
33 /* size to use when breaking up image pane for rendering */
34 #define PR_TILE_SIZE 128
35
36 /* default size of tile cache (mb) */
37 #define PR_CACHE_SIZE_DEFAULT 8
38
39 /* default min and max zoom */
40 #define PR_ZOOM_MIN -32.0
41 #define PR_ZOOM_MAX 32.0
42
43 /* distance to drag mouse to disable image flip */
44 #define PR_DRAG_SCROLL_THRESHHOLD 4
45
46 /* increase pan rate when holding down shift */
47 #define PR_PAN_SHIFT_MULTIPLIER 6
48
49 /* scroller config */
50 #define PR_SCROLLER_UPDATES_PER_SEC 30
51 #define PR_SCROLLER_DEAD_ZONE 6
52
53 /* alpha channel checkerboard background (same as gimp) */
54 #define PR_ALPHA_CHECK1 0x00999999
55 #define PR_ALPHA_CHECK2 0x00666666
56 #define PR_ALPHA_CHECK_SIZE 16
57
58 /* when scaling image to below this size, use nearest pixel for scaling
59  * (below about 4, the other scale types become slow generating their conversion tables)
60  */
61 #define PR_MIN_SCALE_SIZE 8
62
63
64 typedef enum {
65         TILE_RENDER_NONE = 0,   /* do nothing */
66         TILE_RENDER_AREA,       /* render an area of the tile */
67         TILE_RENDER_ALL         /* render the whole tile */
68 } ImageTileRenderType;
69
70 typedef struct _ImageTile ImageTile;
71 typedef struct _QueueData QueueData;
72
73 struct _ImageTile
74 {
75         GdkPixmap *pixmap;      /* off screen buffer */
76         GdkPixbuf *pixbuf;      /* pixbuf area for zooming */
77         gint x;                 /* x offset into image */
78         gint y;                 /* y offset into image */
79         gint w;                 /* width that is visible (may be less if at edge of image) */
80         gint h;                 /* height '' */
81
82         gboolean blank;
83
84 /* render_todo: (explanation)
85         NONE    do nothing
86         AREA    render area of tile, usually only used when loading an image
87                 note: will jump to an ALL if render_done is not ALL.
88         ALL     render entire tile, if never done before w/ ALL, for expose events *only*
89 */
90
91         ImageTileRenderType render_todo;        /* what to do (see above) */
92         ImageTileRenderType render_done;        /* highest that has been done before on tile */
93
94         QueueData *qd;
95         QueueData *qd2;
96
97         guint size;             /* est. memory used by pixmap and pixbuf */
98 };
99
100 struct _QueueData
101 {
102         ImageTile *it;
103         gint x;
104         gint y;
105         gint w;
106         gint h;
107         gboolean new_data;
108 };
109
110 typedef struct _SourceTile SourceTile;
111 struct _SourceTile
112 {
113         gint x;
114         gint y;
115         GdkPixbuf *pixbuf;
116         gboolean blank;
117 };
118
119 typedef struct _OverlayData OverlayData;
120 struct _OverlayData
121 {
122         gint id;
123
124         GdkPixbuf *pixbuf;
125
126         gint x;
127         gint y;
128         gint relative;  /* x,y coordinates are relative, negative values start bottom right */
129
130         gint visible;
131         gint always;    /* hide temporarily when scrolling */
132 };
133
134 enum {
135         SIGNAL_ZOOM = 0,
136         SIGNAL_CLICKED,
137         SIGNAL_SCROLL_NOTIFY,
138         SIGNAL_RENDER_COMPLETE,
139         SIGNAL_COUNT
140 };
141
142 enum {
143         PROP_0,
144         PROP_ZOOM_MIN,
145         PROP_ZOOM_MAX,
146         PROP_ZOOM_QUALITY,
147         PROP_ZOOM_2PASS,
148         PROP_ZOOM_EXPAND,
149         PROP_DITHER_QUALITY,
150         PROP_SCROLL_RESET,
151         PROP_DELAY_FLIP,
152         PROP_LOADING,
153         PROP_COMPLETE,
154         PROP_CACHE_SIZE_DISPLAY,
155         PROP_CACHE_SIZE_TILES,
156         PROP_WINDOW_FIT,
157         PROP_WINDOW_LIMIT,
158         PROP_WINDOW_LIMIT_VALUE
159 };
160
161
162
163 static guint signals[SIGNAL_COUNT] = { 0 };
164 static GtkEventBoxClass *parent_class = NULL;
165
166
167
168 static void pixbuf_renderer_class_init(PixbufRendererClass *class);
169 static void pixbuf_renderer_init(PixbufRenderer *pr);
170 static void pixbuf_renderer_finalize(GObject *object);
171 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
172                                          const GValue *value, GParamSpec *pspec);
173 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
174                                          GValue *value, GParamSpec *pspec);
175 static gint pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event);
176
177 static void pr_render_complete_signal(PixbufRenderer *pr);
178
179 static void pr_overlay_list_clear(PixbufRenderer *pr);
180 static void pr_scroller_timer_set(PixbufRenderer *pr, gint start);
181 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
182
183
184 static void pr_source_tile_free_all(PixbufRenderer *pr);
185 static void pr_tile_free_all(PixbufRenderer *pr);
186 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
187 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it);
188 static void pr_queue_clear(PixbufRenderer *pr);
189 static void pr_queue_merge(QueueData *parent, QueueData *qd);
190 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
191                      gint clamp, ImageTileRenderType render, gint new_data, 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 gint pixbuf_renderer_get_tiles(PixbufRenderer *pr)
1581 {
1582         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1583
1584         return pr->source_tiles_enabled;
1585 }
1586
1587 static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment,
1588                                 gint center_point, gint x, gint y)
1589 {
1590         gdouble zoom = pr->zoom;
1591
1592         if (increment == 0.0) return;
1593
1594         if (zoom == 0.0)
1595                 {
1596                 if (pr->scale < 1.0)
1597                         {
1598                         zoom = 0.0 - 1.0 / pr->scale;
1599                         }
1600                 else
1601                         {
1602                         zoom = pr->scale;
1603                         }
1604                 }
1605
1606         if (increment < 0.0)
1607                 {
1608                 if (zoom >= 1.0 && zoom + increment < 1.0)
1609                         {
1610                         zoom = zoom + increment - 2.0;
1611                         }
1612                 else
1613                         {
1614                         zoom = zoom + increment;
1615                         }
1616                 }
1617         else
1618                 {
1619                 if (zoom <= -1.0 && zoom + increment > -1.0)
1620                         {
1621                         zoom = zoom + increment + 2.0;
1622                         }
1623                 else
1624                         {
1625                         zoom = zoom + increment;
1626                         }
1627                 }
1628
1629         pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, center_point, x, y);
1630 }
1631
1632 /*
1633  *-------------------------------------------------------------------
1634  * display tiles
1635  *-------------------------------------------------------------------
1636  */
1637
1638 static ImageTile *pr_tile_new(gint x, gint y, gint width, gint height)
1639 {
1640         ImageTile *it;
1641
1642         it = g_new0(ImageTile, 1);
1643
1644         it->x = x;
1645         it->y = y;
1646         it->w = width;
1647         it->h = height;
1648
1649         it->render_done = TILE_RENDER_NONE;
1650
1651         return it;
1652 }
1653
1654 static void pr_tile_free(ImageTile *it)
1655 {
1656         if (!it) return;
1657
1658         if (it->pixbuf) gdk_pixbuf_unref(it->pixbuf);
1659         if (it->pixmap) g_object_unref(it->pixmap);
1660
1661         g_free(it);
1662 }
1663
1664 static void pr_tile_free_all(PixbufRenderer *pr)
1665 {
1666         GList *work;
1667
1668         work = pr->tiles;
1669         while (work)
1670                 {
1671                 ImageTile *it;
1672
1673                 it = work->data;
1674                 work = work->next;
1675
1676                 pr_tile_free(it);
1677                 }
1678
1679         g_list_free(pr->tiles);
1680         pr->tiles = NULL;
1681         pr->tile_cache_size = 0;
1682 }
1683
1684 static ImageTile *pr_tile_add(PixbufRenderer *pr, gint x, gint y)
1685 {
1686         ImageTile *it;
1687
1688         it = pr_tile_new(x, y, pr->tile_width, pr->tile_height);
1689
1690         if (it->x + it->w > pr->width) it->w = pr->width - it->x;
1691         if (it->y + it->h > pr->height) it->h = pr->height - it->y;
1692
1693         pr->tiles = g_list_prepend(pr->tiles, it);
1694         pr->tile_cache_size += it->size;
1695
1696         return it;
1697 }
1698
1699 static void pr_tile_remove(PixbufRenderer *pr, ImageTile *it)
1700 {
1701         if (it->qd)
1702                 {
1703                 QueueData *qd = it->qd;
1704
1705                 it->qd = NULL;
1706                 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
1707                 g_free(qd);
1708                 }
1709
1710         if (it->qd2)
1711                 {
1712                 QueueData *qd = it->qd2;
1713
1714                 it->qd2 = NULL;
1715                 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
1716                 g_free(qd);
1717                 }
1718
1719         pr->tiles = g_list_remove(pr->tiles, it);
1720         pr->tile_cache_size -= it->size;
1721
1722         pr_tile_free(it);
1723 }
1724
1725 static void pr_tile_free_space(PixbufRenderer *pr, guint space, ImageTile *it)
1726 {
1727         GList *work;
1728         gint tile_max;
1729
1730         work = g_list_last(pr->tiles);
1731
1732         if (pr->source_tiles_enabled && pr->scale < 1.0)
1733                 {
1734                 gint tiles;
1735
1736                 tiles = (pr->vis_width / pr->tile_width + 1) * (pr->vis_height / pr->tile_height + 1);
1737                 tile_max = MAX(tiles * pr->tile_width * pr->tile_height * 3,
1738                                (gint)((double)pr->tile_cache_max * 1048576.0 * pr->scale));
1739                 }
1740         else
1741                 {
1742                 tile_max = pr->tile_cache_max * 1048576;
1743                 }
1744
1745         while (work && pr->tile_cache_size + space > tile_max)
1746                 {
1747                 ImageTile *needle;
1748
1749                 needle = work->data;
1750                 work = work->prev;
1751                 if (needle != it &&
1752                     ((!needle->qd && !needle->qd2) || !pr_tile_is_visible(pr, needle))) pr_tile_remove(pr, needle);
1753                 }
1754 }
1755
1756 static void pr_tile_invalidate_all(PixbufRenderer *pr)
1757 {
1758         GList *work;
1759
1760         work = pr->tiles;
1761         while (work)
1762                 {
1763                 ImageTile *it;
1764
1765                 it = work->data;
1766                 work = work->next;
1767
1768                 it->render_done = TILE_RENDER_NONE;
1769                 it->render_todo = TILE_RENDER_ALL;
1770                 it->blank = FALSE;
1771
1772                 it->w = MIN(pr->tile_width, pr->width - it->x);
1773                 it->h = MIN(pr->tile_height, pr->height - it->y);
1774                 }
1775 }
1776
1777 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1778 {
1779         gint x1, x2;
1780         gint y1, y2;
1781         GList *work;
1782
1783         x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
1784         x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
1785
1786         y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
1787         y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
1788
1789         work = pr->tiles;
1790         while (work)
1791                 {
1792                 ImageTile *it;
1793
1794                 it = work->data;
1795                 work = work->next;
1796
1797                 if (it->x < x2 && it->x + it->w > x1 &&
1798                     it->y < y2 && it->y + it->h > y1)
1799                         {
1800                         it->render_done = TILE_RENDER_NONE;
1801                         it->render_todo = TILE_RENDER_ALL;
1802                         }
1803                 }
1804 }
1805
1806 static ImageTile *pr_tile_get(PixbufRenderer *pr, gint x, gint y, gint only_existing)
1807 {
1808         GList *work;
1809
1810         work = pr->tiles;
1811         while (work)
1812                 {
1813                 ImageTile *it;
1814
1815                 it = work->data;
1816                 if (it->x == x && it->y == y)
1817                         {
1818                         pr->tiles = g_list_delete_link(pr->tiles, work);
1819                         pr->tiles = g_list_prepend(pr->tiles, it);
1820                         return it;
1821                         }
1822
1823                 work = work->next;
1824                 }
1825
1826         if (only_existing) return NULL;
1827
1828         return pr_tile_add(pr, x, y);
1829 }
1830
1831 static void pr_tile_prepare(PixbufRenderer *pr, ImageTile *it)
1832 {
1833         if (!it->pixmap)
1834                 {
1835                 GdkPixmap *pixmap;
1836                 guint size;
1837
1838                 pixmap = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
1839
1840                 size = pixmap_calc_size(pixmap);
1841                 pr_tile_free_space(pr, size, it);
1842
1843                 it->pixmap = pixmap;
1844                 it->size += size;
1845                 pr->tile_cache_size += size;
1846                 }
1847         
1848         if ((pr->zoom != 1.0 || pr->source_tiles_enabled || (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf)) ) &&
1849             !it->pixbuf)
1850                 {
1851                 GdkPixbuf *pixbuf;
1852                 guint size;
1853
1854                 if (pr->pixbuf)
1855                         {
1856                         pixbuf = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(pr->pixbuf),
1857                                                 gdk_pixbuf_get_has_alpha(pr->pixbuf),
1858                                                 gdk_pixbuf_get_bits_per_sample(pr->pixbuf),
1859                                                 pr->tile_width, pr->tile_height);
1860                         }
1861                 else
1862                         {
1863                         pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
1864                         }
1865
1866                 size = gdk_pixbuf_get_rowstride(pixbuf) * pr->tile_height;
1867                 pr_tile_free_space(pr, size, it);
1868
1869                 it->pixbuf = pixbuf;
1870                 it->size += size;
1871                 pr->tile_cache_size += size;
1872                 }
1873 }
1874
1875 /*
1876  *-------------------------------------------------------------------
1877  * drawing
1878  *-------------------------------------------------------------------
1879  */
1880
1881 static void pr_tile_render(PixbufRenderer *pr, ImageTile *it,
1882                            gint x, gint y, gint w, gint h,
1883                            gint new_data, gint fast)
1884 {
1885         GtkWidget *box;
1886         gint has_alpha;
1887         gint draw = FALSE;
1888
1889         if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return;
1890
1891         if (it->render_done != TILE_RENDER_ALL)
1892                 {
1893                 x = 0;
1894                 y = 0;
1895                 w = it->w;
1896                 h = it->h;
1897                 if (!fast) it->render_done = TILE_RENDER_ALL;
1898                 }
1899         else if (it->render_todo != TILE_RENDER_AREA)
1900                 {
1901                 if (!fast) it->render_todo = TILE_RENDER_NONE;
1902                 return;
1903                 }
1904
1905         if (!fast) it->render_todo = TILE_RENDER_NONE;
1906
1907         if (new_data) it->blank = FALSE;
1908
1909         pr_tile_prepare(pr, it);
1910         has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1911
1912         box = GTK_WIDGET(pr);
1913
1914         /* FIXME checker colors for alpha should be configurable,
1915          * also should be drawn for blank = TRUE
1916          */
1917
1918         if (it->blank)
1919                 {
1920                 /* no data, do fast rect fill */
1921                 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1922                                    0, 0, it->w, it->h);
1923                 }
1924         else if (pr->source_tiles_enabled)
1925                 {
1926                 draw = pr_source_tile_render(pr, it, x, y, w, h, new_data, fast);
1927                 }
1928         else if (pr->zoom == 1.0 || pr->scale == 1.0)
1929                 {
1930                 if (has_alpha)
1931                         {
1932                         gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, x, y, w, h,
1933                                          (double) 0.0 - it->x,
1934                                          (double) 0.0 - it->y,
1935                                          1.0, 1.0, GDK_INTERP_NEAREST,
1936                                          255, it->x + x, it->y + y,
1937                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1938                         draw = TRUE;
1939                         }
1940                 else
1941                         {
1942                         /* faster, simple */
1943                         gdk_draw_pixbuf(it->pixmap,
1944                                         box->style->fg_gc[GTK_WIDGET_STATE(box)],
1945                                         pr->pixbuf,
1946                                         it->x + x, it->y + y,
1947                                         x, y,
1948                                         w, h,
1949                                         pr->dither_quality, it->x + x, it->y + y);
1950                         }
1951                 }
1952         else
1953                 {
1954                 double scale_x, scale_y;
1955
1956                 if (pr->image_width == 0 || pr->image_height == 0) return;
1957                 scale_x = (double)pr->width / pr->image_width;
1958                 scale_y = (double)pr->height / pr->image_height;
1959
1960                 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1961                  * small sizes for anything but GDK_INTERP_NEAREST
1962                  */
1963                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1964
1965                 if (!has_alpha)
1966                         {
1967                         gdk_pixbuf_scale(pr->pixbuf, it->pixbuf, x, y, w, h,
1968                                          (double) 0.0 - it->x,
1969                                          (double) 0.0 - it->y,
1970                                          scale_x, scale_y,
1971                                          (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1972                         }
1973                 else
1974                         {
1975                         gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, x, y, w, h,
1976                                          (double) 0.0 - it->x,
1977                                          (double) 0.0 - it->y,
1978                                          scale_x, scale_y,
1979                                          (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1980                                          255, it->x + x, it->y + y,
1981                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1982                         }
1983                 draw = TRUE;
1984                 }
1985
1986         if (draw && it->pixbuf && !it->blank)
1987                 {
1988                 gdk_draw_pixbuf(it->pixmap,
1989                                 box->style->fg_gc[GTK_WIDGET_STATE(box)],
1990                                 it->pixbuf,
1991                                 x, y,
1992                                 x, y,
1993                                 w, h,
1994                                 pr->dither_quality, it->x + x, it->y + y);
1995                 }
1996 }
1997
1998
1999 static void pr_tile_expose(PixbufRenderer *pr, ImageTile *it,
2000                            gint x, gint y, gint w, gint h,
2001                            gint new_data, gint fast)
2002 {
2003         GtkWidget *box;
2004
2005         pr_tile_render(pr, it, x, y, w, h, new_data, fast);
2006
2007         box = GTK_WIDGET(pr);
2008
2009         gdk_draw_drawable(box->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
2010                           it->pixmap, x, y,
2011                           pr->x_offset + (it->x - pr->x_scroll) + x, pr->y_offset + (it->y - pr->y_scroll) + y, w, h);
2012
2013         if (pr->overlay_list)
2014                 {
2015                 pr_overlay_draw(pr, pr->x_offset + (it->x - pr->x_scroll) + x,
2016                                 pr->y_offset + (it->y - pr->y_scroll) + y,
2017                                 w, h);
2018                 }
2019 }
2020
2021
2022 static gint pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it)
2023 {
2024         return (it->x + it->w >= pr->x_scroll && it->x < pr->x_scroll + pr->vis_width &&
2025                 it->y + it->h >= pr->y_scroll && it->y < pr->y_scroll + pr->vis_height);
2026 }
2027
2028 /*
2029  *-------------------------------------------------------------------
2030  * draw queue
2031  *-------------------------------------------------------------------
2032  */
2033
2034 static gint pr_queue_draw_idle_cb(gpointer data)
2035 {
2036         PixbufRenderer *pr = data;
2037         QueueData *qd;
2038         gint fast;
2039
2040         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2041             (!pr->draw_queue && !pr->draw_queue_2pass) ||
2042             pr->draw_idle_id == -1)
2043                 {
2044                 pr_render_complete_signal(pr);
2045
2046                 pr->draw_idle_id = -1;
2047                 return FALSE;
2048                 }
2049
2050         if (pr->draw_queue)
2051                 {
2052                 qd = pr->draw_queue->data;
2053                 fast = (pr->zoom_2pass && pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0);
2054                 }
2055         else
2056                 {
2057                 if (pr->loading)
2058                         {
2059                         /* still loading, wait till done (also drops the higher priority) */
2060
2061                         pr->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
2062                                                            pr_queue_draw_idle_cb, pr, NULL);
2063                         pr->draw_idle_high = FALSE;
2064                         return FALSE;
2065                         }
2066
2067                 qd = pr->draw_queue_2pass->data;
2068                 fast = FALSE;
2069                 }
2070
2071         if (GTK_WIDGET_REALIZED(pr))
2072                 {
2073                 if (pr_tile_is_visible(pr, qd->it))
2074                         {
2075                         pr_tile_expose(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2076                         }
2077                 else if (qd->new_data)
2078                         {
2079                         /* if new pixel data, and we already have a pixmap, update the tile */
2080                         qd->it->blank = FALSE;
2081                         if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL)
2082                                 {
2083                                 pr_tile_render(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2084                                 }
2085                         }
2086                 }
2087
2088         if (pr->draw_queue)
2089                 {
2090                 qd->it->qd = NULL;
2091                 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
2092                 if (fast)
2093                         {
2094                         if (qd->it->qd2)
2095                                 {
2096                                 pr_queue_merge(qd->it->qd2, qd);
2097                                 g_free(qd);
2098                                 }
2099                         else
2100                                 {
2101                                 qd->it->qd2 = qd;
2102                                 pr->draw_queue_2pass = g_list_append(pr->draw_queue_2pass, qd);
2103                                 }
2104                         }
2105                 else
2106                         {
2107                         g_free(qd);
2108                         }
2109                 }
2110         else
2111                 {
2112                 qd->it->qd2 = NULL;
2113                 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
2114                 g_free(qd);
2115                 }
2116
2117         if (!pr->draw_queue && !pr->draw_queue_2pass)
2118                 {
2119                 pr_render_complete_signal(pr);
2120
2121                 pr->draw_idle_id = -1;
2122                 return FALSE;
2123                 }
2124
2125         return TRUE;
2126 }
2127
2128 static void pr_queue_list_free(GList *list)
2129 {
2130         GList *work;
2131
2132         work = list;
2133         while (work)
2134                 {
2135                 QueueData *qd;
2136
2137                 qd = work->data;
2138                 work = work->next;
2139
2140                 qd->it->qd = NULL;
2141                 qd->it->qd2 = NULL;
2142                 g_free(qd);
2143                 }
2144
2145         g_list_free(list);
2146 }
2147
2148 static void pr_queue_clear(PixbufRenderer *pr)
2149 {
2150         pr_queue_list_free(pr->draw_queue);
2151         pr->draw_queue = NULL;
2152
2153         pr_queue_list_free(pr->draw_queue_2pass);
2154         pr->draw_queue_2pass = NULL;
2155
2156         if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2157         pr->draw_idle_id = -1;
2158 }
2159
2160 static void pr_queue_merge(QueueData *parent, QueueData *qd)
2161 {
2162         if (parent->x + parent->w < qd->x + qd->w)
2163                 {
2164                 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
2165                 }
2166         if (parent->x > qd->x)
2167                 {
2168                 parent->w += parent->x - qd->x;
2169                 parent->x = qd->x;
2170                 }
2171
2172         if (parent->y + parent->h < qd->y + qd->h)
2173                 {
2174                 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
2175                 }
2176         if (parent->y > qd->y)
2177                 {
2178                 parent->h += parent->y - qd->y;
2179                 parent->y = qd->y;
2180                 }
2181
2182         parent->new_data |= qd->new_data;
2183 }
2184
2185 static gint pr_clamp_to_visible(PixbufRenderer *pr, gint *x, gint *y, gint *w, gint *h)
2186 {
2187         gint nx, ny;
2188         gint nw, nh;
2189         gint vx, vy;
2190         gint vw, vh;
2191
2192         vw = pr->vis_width;
2193         vh = pr->vis_height;
2194
2195         vx = pr->x_scroll;
2196         vy = pr->y_scroll;
2197
2198         if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
2199
2200         /* now clamp it */
2201         nx = CLAMP(*x, vx, vx + vw);
2202         nw = CLAMP(*w - (nx - *x), 1, vw);
2203
2204         ny = CLAMP(*y, vy, vy + vh);
2205         nh = CLAMP(*h - (ny - *y), 1, vh);
2206
2207         *x = nx;
2208         *y = ny;
2209         *w = nw;
2210         *h = nh;
2211
2212         return TRUE;
2213 }
2214
2215 static gint pr_queue_to_tiles(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2216                               gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2217 {
2218         gint i, j;
2219         gint x1, x2;
2220         gint y1, y2;
2221
2222         if (clamp && !pr_clamp_to_visible(pr, &x, &y, &w, &h)) return FALSE;
2223
2224         x1 = (gint)floor(x / pr->tile_width) * pr->tile_width;
2225         x2 = (gint)ceil((x + w) / pr->tile_width) * pr->tile_width;
2226
2227         y1 = (gint)floor(y / pr->tile_height) * pr->tile_height;
2228         y2 = (gint)ceil((y + h) / pr->tile_height) * pr->tile_height;
2229
2230         for (j = y1; j <= y2; j += pr->tile_height)
2231                 {
2232                 for (i = x1; i <= x2; i += pr->tile_width)
2233                         {
2234                         ImageTile *it;
2235
2236                         it = pr_tile_get(pr, i, j,
2237                                          (only_existing &&
2238                                           (i + pr->tile_width < pr->x_scroll ||
2239                                            i > pr->x_scroll + pr->vis_width ||
2240                                            j + pr->tile_height < pr->y_scroll ||
2241                                            j > pr->y_scroll + pr->vis_height)));
2242                         if (it)
2243                                 {
2244                                 QueueData *qd;
2245
2246                                 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
2247                                     (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
2248                                         {
2249                                         it->render_todo = render;
2250                                         }
2251
2252                                 qd = g_new(QueueData, 1);
2253                                 qd->it = it;
2254                                 qd->new_data = new_data;
2255
2256                                 if (i < x)
2257                                         {
2258                                         qd->x = x - i;
2259                                         }
2260                                 else
2261                                         {
2262                                         qd->x = 0;
2263                                         }
2264                                 qd->w = x + w - i - qd->x;
2265                                 if (qd->x + qd->w > pr->tile_width) qd->w = pr->tile_width - qd->x;
2266
2267                                 if (j < y)
2268                                         {
2269                                         qd->y = y - j;
2270                                         }
2271                                 else
2272                                         {
2273                                         qd->y = 0;
2274                                         }
2275                                 qd->h = y + h - j - qd->y;
2276                                 if (qd->y + qd->h > pr->tile_height) qd->h = pr->tile_height - qd->y;
2277
2278                                 if (qd->w < 1 || qd->h < 1)
2279                                         {
2280                                         g_free(qd);
2281                                         }
2282                                 else if (it->qd)
2283                                         {
2284                                         pr_queue_merge(it->qd, qd);
2285                                         g_free(qd);
2286                                         }
2287                                 else
2288                                         {
2289                                         it->qd = qd;
2290                                         pr->draw_queue = g_list_append(pr->draw_queue, qd);
2291                                         }
2292                                 }
2293                         }
2294                 }
2295
2296         return TRUE;
2297 }
2298
2299 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
2300                      gint clamp, ImageTileRenderType render, gint new_data, gint only_existing)
2301 {
2302         gint nx, ny;
2303
2304         nx = CLAMP(x, 0, pr->width - 1);
2305         ny = CLAMP(y, 0, pr->height - 1);
2306         w -= (nx - x);
2307         h -= (ny - y);
2308         w = CLAMP(w, 0, pr->width - nx);
2309         h = CLAMP(h, 0, pr->height - ny);
2310         if (w < 1 || h < 1) return;
2311
2312         if (pr_queue_to_tiles(pr, nx, ny, w, h, clamp, render, new_data, only_existing) &&
2313             ((!pr->draw_queue && !pr->draw_queue_2pass) || pr->draw_idle_id == -1 || !pr->draw_idle_high))
2314                 {
2315                 if (pr->draw_idle_id != -1) g_source_remove(pr->draw_idle_id);
2316                 pr->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW,
2317                                                    pr_queue_draw_idle_cb, pr, NULL);
2318                 pr->draw_idle_high = TRUE;
2319                 }
2320 }
2321
2322 static void pr_redraw(PixbufRenderer *pr, gint new_data)
2323 {
2324         pr_queue_clear(pr);
2325         pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data, FALSE);
2326 }
2327
2328 /*
2329  *-------------------------------------------------------------------
2330  * signal emission
2331  *-------------------------------------------------------------------
2332  */
2333
2334 static void pr_update_signal(PixbufRenderer *pr)
2335 {
2336 #if 0
2337         printf("FIXME: send updated signal\n");
2338 #endif
2339 }
2340
2341 static void pr_zoom_signal(PixbufRenderer *pr)
2342 {
2343         g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
2344 }
2345
2346 static void pr_clicked_signal(PixbufRenderer *pr, GdkEventButton *bevent)
2347 {
2348         g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
2349 }
2350
2351 static void pr_scroll_notify_signal(PixbufRenderer *pr)
2352 {
2353         g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
2354 }
2355
2356 static void pr_render_complete_signal(PixbufRenderer *pr)
2357 {
2358         if (!pr->complete)
2359                 {
2360                 g_signal_emit(pr, signals[SIGNAL_RENDER_COMPLETE], 0);
2361                 g_object_set(G_OBJECT(pr), "complete", TRUE, NULL);
2362                 }
2363 }
2364
2365 /*
2366  *-------------------------------------------------------------------
2367  * sync and clamp
2368  *-------------------------------------------------------------------
2369  */
2370
2371 static gint pr_scroll_clamp(PixbufRenderer *pr)
2372 {
2373         gint old_xs;
2374         gint old_ys;
2375
2376         if (pr->zoom == 0.0)
2377                 {
2378                 pr->x_scroll = 0;
2379                 pr->y_scroll = 0;
2380
2381                 return FALSE;
2382                 }
2383
2384         old_xs = pr->x_scroll;
2385         old_ys = pr->y_scroll;
2386
2387         if (pr->x_offset > 0)
2388                 {
2389                 pr->x_scroll = 0;
2390                 }
2391         else
2392                 {
2393                 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
2394                 }
2395
2396         if (pr->y_offset > 0)
2397                 {
2398                 pr->y_scroll = 0;
2399                 }
2400         else
2401                 {
2402                 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
2403                 }
2404
2405         return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
2406 }
2407
2408 static gint pr_size_clamp(PixbufRenderer *pr)
2409 {
2410         gint old_vw, old_vh;
2411
2412         old_vw = pr->vis_width;
2413         old_vh = pr->vis_height;
2414
2415         if (pr->width < pr->window_width)
2416                 {
2417                 pr->vis_width = pr->width;
2418                 pr->x_offset = (pr->window_width - pr->width) / 2;
2419                 }
2420         else
2421                 {
2422                 pr->vis_width = pr->window_width;
2423                 pr->x_offset = 0;
2424                 }
2425
2426         if (pr->height < pr->window_height)
2427                 {
2428                 pr->vis_height = pr->height;
2429                 pr->y_offset = (pr->window_height - pr->height) / 2;
2430                 }
2431         else
2432                 {
2433                 pr->vis_height = pr->window_height;
2434                 pr->y_offset = 0;
2435                 }
2436
2437         return (old_vw != pr->vis_width || old_vh != pr->vis_height);
2438 }
2439
2440 static gint pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom,
2441                           gint force, gint new, gint invalidate,
2442                           gint *redrawn)
2443 {
2444         gint w, h;
2445         gdouble scale;
2446         gint invalid;
2447
2448         zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
2449
2450         if (pr->zoom == zoom && !force) return FALSE;
2451
2452         w = pr->image_width;
2453         h = pr->image_height;
2454
2455         if (zoom == 0.0 && !pr->pixbuf)
2456                 {
2457                 scale = 1.0;
2458                 }
2459         else if (zoom == 0.0)
2460                 {
2461                 gint max_w;
2462                 gint max_h;
2463                 gint sizeable;
2464
2465                 sizeable = (new && pr_parent_window_sizable(pr));
2466
2467                 if (sizeable)
2468                         {
2469                         max_w = gdk_screen_width();
2470                         max_h = gdk_screen_height();
2471
2472                         if (pr->window_limit)
2473                                 {
2474                                 max_w = max_w * pr->window_limit_size / 100;
2475                                 max_h = max_h * pr->window_limit_size / 100;
2476                                 }
2477                         }
2478                 else
2479                         {
2480                         max_w = pr->window_width;
2481                         max_h = pr->window_height;
2482                         }
2483
2484                 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
2485                         {
2486                         if ((gdouble)max_w / w > (gdouble)max_h / h)
2487                                 {
2488                                 scale = (gdouble)max_h / h;
2489                                 h = max_h;
2490                                 w = w * scale + 0.5;
2491                                 if (w > max_w) w = max_w;
2492                                 }
2493                         else
2494                                 {
2495                                 scale = (gdouble)max_w / w;
2496                                 w = max_w;
2497                                 h = h * scale + 0.5;
2498                                 if (h > max_h) h = max_h;
2499                                 }
2500                         if (w < 1) w = 1;
2501                         if (h < 1) h = 1;
2502                         }
2503                 else
2504                         {
2505                         scale = 1.0;
2506                         }
2507                 }
2508         else if (zoom > 0.0) /* zoom orig, in */
2509                 {
2510                 scale = zoom;
2511                 w = w * scale;
2512                 h = h * scale;
2513                 }
2514         else /* zoom out */
2515                 {
2516                 scale = 1.0 / (0.0 - zoom);
2517                 w = w * scale;
2518                 h = h * scale;
2519                 }
2520
2521         invalid = (pr->width != w || pr->height != h);
2522
2523         pr->zoom = zoom;
2524         pr->width = w;
2525         pr->height = h;
2526         pr->scale = scale;
2527
2528         if (invalidate || invalid)
2529                 {
2530                 pr_tile_invalidate_all(pr);
2531                 pr_redraw(pr, TRUE);
2532                 }
2533         if (redrawn) *redrawn = (invalidate || invalid);
2534
2535         return TRUE;
2536 }
2537
2538 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
2539                          gint force, gint blank, gint new,
2540                          gint center_point, gint px, gint py)
2541 {
2542         gdouble old_scale;
2543         gint old_cx, old_cy;
2544         gint clamped;
2545         gint sized;
2546         gint redrawn = FALSE;
2547
2548         old_scale = pr->scale;
2549         if (center_point)
2550                 {
2551                 px = CLAMP(px, 0, pr->width);
2552                 py = CLAMP(py, 0, pr->height);
2553                 old_cx = pr->x_scroll + (px - pr->x_offset);
2554                 old_cy = pr->y_scroll + (py - pr->y_offset);
2555                 }
2556         else
2557                 {
2558                 px = py = 0;
2559                 old_cx = pr->x_scroll + pr->vis_width / 2;
2560                 old_cy = pr->y_scroll + pr->vis_height / 2;
2561                 }
2562
2563         if (!pr_zoom_clamp(pr, zoom, force, new, force, &redrawn)) return;
2564
2565         clamped = pr_size_clamp(pr);
2566         sized = pr_parent_window_resize(pr, pr->width, pr->height);
2567
2568         if (force)
2569                 {
2570                 switch (pr->scroll_reset)
2571                         {
2572                         case PR_SCROLL_RESET_NOCHANGE:
2573                                 /* maintain old scroll position, do nothing */
2574                                 break;
2575                         case PR_SCROLL_RESET_CENTER:
2576                                 /* center new image */
2577                                 pr->x_scroll = ((double)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2;
2578                                 pr->y_scroll = ((double)pr->image_height / 2.0 * pr->scale) - pr->vis_height / 2;
2579                                 break;
2580                         case PR_SCROLL_RESET_TOPLEFT:
2581                         default:
2582                                 /* reset to upper left */
2583                                 pr->x_scroll = 0;
2584                                 pr->y_scroll = 0;
2585                                 break;
2586                         }
2587                 }
2588         else
2589                 {
2590                 /* user zoom does not force, so keep visible center point */
2591                 if (center_point)
2592                         {
2593                         pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
2594                         pr->y_scroll = old_cy / old_scale * pr->scale - (py - pr->y_offset);
2595                         }
2596                 else
2597                         {
2598                         pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2);
2599                         pr->y_scroll = old_cy / old_scale * pr->scale - (pr->vis_height / 2);
2600                         }
2601                 }
2602
2603         pr_scroll_clamp(pr);
2604
2605 #if 0
2606         pr_tile_sync(pr, blank);
2607 #endif
2608
2609         /* If the window was not sized, redraw the image - we know there will be no size/expose signal.
2610          * But even if a size is claimed, there is no guarantee that the window manager will allow it,
2611          * so redraw the window anyway :/
2612          */
2613         if (sized || clamped) pr_border_clear(pr);
2614         pr_redraw(pr, redrawn);
2615
2616         pr_scroll_notify_signal(pr);
2617         pr_zoom_signal(pr);
2618         pr_update_signal(pr);
2619 }
2620
2621 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
2622 {
2623         if (pr->window_width == new_width && pr->window_height == new_height) return;
2624
2625         pr->window_width = new_width;
2626         pr->window_height = new_height;
2627
2628         if (pr->zoom == 0.0) pr_zoom_clamp(pr, 0.0, TRUE, FALSE, FALSE, NULL);
2629
2630         pr_size_clamp(pr);
2631         pr_scroll_clamp(pr);
2632
2633         /* ensure scroller remains visible */
2634         if (pr->scroller_overlay != -1)
2635                 {
2636                 gint update = FALSE;
2637
2638                 if (pr->scroller_x > new_width)
2639                         {
2640                         pr->scroller_x = new_width;
2641                         pr->scroller_xpos = new_width;
2642                         update = TRUE;
2643                         }
2644                 if (pr->scroller_y > new_height)
2645                         {
2646                         pr->scroller_y = new_height;
2647                         pr->scroller_ypos = new_height;
2648                         update = TRUE;
2649                         }
2650
2651                 if (update)
2652                         {
2653                         GdkPixbuf *pixbuf;
2654
2655                         if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
2656                                 {
2657                                 gint w, h;
2658
2659                                 w = gdk_pixbuf_get_width(pixbuf);
2660                                 h = gdk_pixbuf_get_height(pixbuf);
2661                                 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
2662                                                             pr->scroller_x - w / 2, pr->scroller_y - h / 2);
2663                                 }
2664                         }
2665                 }
2666
2667         pr_border_clear(pr);
2668
2669 #if 0
2670         pr_tile_sync(pr, pr->width, pr->height, FALSE);
2671 #endif
2672
2673         pr_scroll_notify_signal(pr);
2674         pr_update_signal(pr);
2675 }
2676
2677 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2678 {
2679         PixbufRenderer *pr = data;
2680
2681         pr_size_sync(pr, allocation->width, allocation->height);
2682 }
2683
2684 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
2685 {
2686         gint x, y;
2687
2688         pr_border_draw(pr, area->x, area->y, area->width, area->height);
2689
2690         x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
2691         y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
2692
2693         pr_queue(pr, x, y,
2694                  MIN((gint)area->width, pr->width - x),
2695                  MIN((gint)area->height, pr->height - y),
2696                  FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2697 }
2698
2699 /*
2700  *-------------------------------------------------------------------
2701  * scrolling
2702  *-------------------------------------------------------------------
2703  */
2704
2705 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
2706 {
2707         gint old_x, old_y;
2708         gint x_off, y_off;
2709         gint w, h;
2710
2711         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2712
2713         if (!pr->pixbuf && !pr->source_tiles_enabled) return;
2714
2715         old_x = pr->x_scroll;
2716         old_y = pr->y_scroll;
2717
2718         pr->x_scroll += x;
2719         pr->y_scroll += y;
2720
2721         pr_scroll_clamp(pr);
2722         if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
2723
2724         pr_scroll_notify_signal(pr);
2725
2726         if (pr->overlay_list)
2727                 {
2728                 gint new_x, new_y;
2729
2730                 new_x = pr->x_scroll;
2731                 new_y = pr->y_scroll;
2732                 pr->x_scroll = old_x;
2733                 pr->y_scroll = old_y;
2734
2735                 pr_overlay_queue_all(pr);
2736
2737                 pr->x_scroll = new_x;
2738                 pr->y_scroll = new_y;
2739                 }
2740
2741         x_off = pr->x_scroll - old_x;
2742         y_off = pr->y_scroll - old_y;
2743
2744         w = pr->vis_width - abs(x_off);
2745         h = pr->vis_height - abs(y_off);
2746
2747         if (w < 1 || h < 1)
2748                 {
2749                 /* scrolled completely to new material */
2750                 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2751                 return;
2752                 }
2753         else
2754                 {
2755                 gint x1, y1;
2756                 gint x2, y2;
2757                 GtkWidget *box;
2758                 GdkGC *gc;
2759
2760                 if (x_off < 0)
2761                         {
2762                         x1 = abs(x_off);
2763                         x2 = 0;
2764                         }
2765                 else
2766                         {
2767                         x1 = 0;
2768                         x2 = abs(x_off);
2769                         }
2770
2771                 if (y_off < 0)
2772                         {
2773                         y1 = abs(y_off);
2774                         y2 = 0;
2775                         }
2776                 else
2777                         {
2778                         y1 = 0;
2779                         y2 = abs(y_off);
2780                         }
2781
2782                 box = GTK_WIDGET(pr);
2783
2784                 gc = gdk_gc_new(box->window);
2785                 gdk_gc_set_exposures(gc, TRUE);
2786                 gdk_draw_drawable(box->window, gc,
2787                                   box->window,
2788                                   x2 + pr->x_offset, y2 + pr->y_offset,
2789                                   x1 + pr->x_offset, y1 + pr->y_offset, w, h);
2790                 g_object_unref(gc);
2791
2792                 if (pr->overlay_list)
2793                         {
2794                         pr_overlay_queue_all(pr);
2795                         }
2796
2797                 w = pr->vis_width - w;
2798                 h = pr->vis_height - h;
2799
2800                 if (w > 0)
2801                         {
2802                         pr_queue(pr,
2803                                  x_off > 0 ? pr->x_scroll + (pr->vis_width - w) : pr->x_scroll, pr->y_scroll,
2804                                  w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2805                         }
2806                 if (h > 0)
2807                         {
2808                         /* FIXME, to optimize this, remove overlap */
2809                         pr_queue(pr,
2810                                  pr->x_scroll, y_off > 0 ? pr->y_scroll + (pr->vis_height - h) : pr->y_scroll,
2811                                  pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2812                         }
2813                 }
2814 }
2815
2816 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
2817                                      gdouble x_align, gdouble y_align)
2818 {
2819         gint px, py;
2820         gint ax, ay;
2821
2822         x_align = CLAMP(x_align, 0.0, 1.0);
2823         y_align = CLAMP(y_align, 0.0, 1.0);
2824
2825         ax = (gdouble)pr->vis_width * x_align;
2826         ay = (gdouble)pr->vis_height * y_align;
2827
2828         px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
2829         py = (gdouble)y * pr->scale - (pr->y_scroll + ay);
2830
2831         pixbuf_renderer_scroll(pr, px, py);
2832 }
2833
2834 /*
2835  *-------------------------------------------------------------------
2836  * mouse
2837  *-------------------------------------------------------------------
2838  */
2839
2840 static gint pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2841 {
2842         PixbufRenderer *pr;
2843         gint accel;
2844
2845         pr = PIXBUF_RENDERER(widget);
2846
2847         if (pr->scroller_id != -1)
2848                 {
2849                 pr->scroller_xpos = bevent->x;
2850                 pr->scroller_ypos = bevent->y;
2851                 }
2852
2853         if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
2854
2855         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2856                 {
2857                 pr->drag_moved++;
2858                 }
2859         else
2860                 {
2861                 widget_set_cursor(widget, GDK_FLEUR);
2862                 }
2863
2864         if (bevent->state & GDK_SHIFT_MASK)
2865                 {
2866                 accel = PR_PAN_SHIFT_MULTIPLIER;
2867                 }
2868         else
2869                 {
2870                 accel = 1;
2871                 }
2872
2873         /* do the scroll */
2874         pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
2875                                (pr->drag_last_y - bevent->y) * accel);
2876
2877         pr->drag_last_x = bevent->x;
2878         pr->drag_last_y = bevent->y;
2879
2880         return FALSE;
2881 }
2882
2883 static gint pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2884 {
2885         PixbufRenderer *pr;
2886         GtkWidget *parent;
2887
2888         pr = PIXBUF_RENDERER(widget);
2889
2890         if (pr->scroller_id != -1) return TRUE;
2891
2892         switch (bevent->button)
2893                 {
2894                 case 1:
2895                         pr->in_drag = TRUE;
2896                         pr->drag_last_x = bevent->x;
2897                         pr->drag_last_y = bevent->y;
2898                         pr->drag_moved = 0;
2899                         gdk_pointer_grab(widget->window, FALSE,
2900                                          GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
2901                                          NULL, NULL, bevent->time);
2902                         gtk_grab_add(widget);
2903                         break;
2904                 case 2:
2905                         pr->drag_moved = 0;
2906                         break;
2907                 case 3:
2908                         pr_clicked_signal(pr, bevent);
2909                         break;
2910                 default:
2911                         break;
2912                 }
2913
2914         parent = gtk_widget_get_parent(widget);
2915         if (widget && GTK_WIDGET_CAN_FOCUS(parent))
2916                 {
2917                 gtk_widget_grab_focus(parent);
2918                 }
2919
2920         return FALSE;
2921 }
2922
2923 static gint pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2924 {
2925         PixbufRenderer *pr;
2926
2927         pr = PIXBUF_RENDERER(widget);
2928
2929         if (pr->scroller_id != -1)
2930                 {
2931                 pr_scroller_stop(pr);
2932                 return TRUE;
2933                 }
2934
2935         if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
2936                 {
2937                 gtk_grab_remove(widget);
2938                 gdk_pointer_ungrab(bevent->time);
2939                 widget_set_cursor(widget, -1);
2940                 }
2941
2942         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2943                 {
2944                 if (bevent->button == 1 && (bevent->state & GDK_SHIFT_MASK))
2945                         {
2946                         pr_scroller_start(pr, bevent->x, bevent->y);
2947                         }
2948                 else if (bevent->button == 1 || bevent->button == 2)
2949                         {
2950                         pr_clicked_signal(pr, bevent);
2951                         }
2952                 }
2953
2954         pr->in_drag = FALSE;
2955
2956         return FALSE;
2957 }
2958
2959 static gint pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
2960 {
2961         PixbufRenderer *pr;
2962
2963         pr = PIXBUF_RENDERER(widget);
2964
2965         if (pr->scroller_id != -1)
2966                 {
2967                 pr->scroller_xpos = pr->scroller_x;
2968                 pr->scroller_ypos = pr->scroller_y;
2969                 pr->scroller_xinc = 0;
2970                 pr->scroller_yinc = 0;
2971                 }
2972
2973         return FALSE;
2974 }
2975
2976 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
2977 {
2978         PixbufRenderer *pr;
2979
2980         pr = PIXBUF_RENDERER(widget);
2981
2982         pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
2983 }
2984
2985 static void pr_signals_connect(PixbufRenderer *pr)
2986 {
2987         g_signal_connect(G_OBJECT(pr), "motion_notify_event",
2988                          G_CALLBACK(pr_mouse_motion_cb), pr);
2989         g_signal_connect(G_OBJECT(pr), "button_press_event",
2990                          G_CALLBACK(pr_mouse_press_cb), pr);
2991         g_signal_connect(G_OBJECT(pr), "button_release_event",
2992                          G_CALLBACK(pr_mouse_release_cb), pr);
2993         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
2994                          G_CALLBACK(pr_mouse_leave_cb), pr);
2995
2996         gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK |
2997                                               GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
2998                                               GDK_LEAVE_NOTIFY_MASK);
2999
3000         g_signal_connect(G_OBJECT(pr), "drag_begin",
3001                          G_CALLBACK(pr_mouse_drag_cb), pr);
3002
3003 }
3004
3005 /*
3006  *-------------------------------------------------------------------
3007  * public
3008  *-------------------------------------------------------------------
3009  */
3010
3011 static void pr_pixbuf_sync(PixbufRenderer *pr, gdouble zoom, gint blank, gint new)
3012 {
3013         if (!pr->pixbuf)
3014                 {
3015                 GtkWidget *box;
3016
3017                 /* no pixbuf so just clear the window */
3018                 pr->image_width = 0;
3019                 pr->image_height = 0;
3020                 pr->scale = 1.0;
3021
3022                 box = GTK_WIDGET(pr);
3023
3024                 if (GTK_WIDGET_REALIZED(box))
3025                         {
3026                         gdk_window_clear(box->window);
3027                         pr_overlay_draw(pr, 0, 0, pr->window_width, pr->window_height);
3028                         }
3029
3030                 pr_update_signal(pr);
3031
3032                 return;
3033                 }
3034
3035         pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
3036         pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
3037
3038         pr_zoom_sync(pr, zoom, TRUE, blank, new, FALSE, 0, 0);
3039 }
3040
3041 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, gint new)
3042 {
3043         if (pixbuf) g_object_ref(pixbuf);
3044         if (pr->pixbuf) g_object_unref(pr->pixbuf);
3045         pr->pixbuf = pixbuf;
3046
3047         pr_pixbuf_sync(pr, zoom, FALSE, new);
3048 }
3049
3050 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
3051 {
3052         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3053
3054         pr_source_tile_unset(pr);
3055
3056         pr_set_pixbuf(pr, pixbuf, zoom, TRUE);
3057
3058         pr_update_signal(pr);
3059 }
3060
3061 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
3062 {
3063         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
3064
3065         return pr->pixbuf;
3066 }
3067
3068 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
3069 {
3070         GObject *object;
3071         PixbufRendererScrollResetType scroll_reset;
3072
3073         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3074         g_return_if_fail(IS_PIXBUF_RENDERER(source));
3075
3076         if (pr == source) return;
3077
3078         object = G_OBJECT(pr);
3079
3080         g_object_set(object, "zoom_min", source->zoom_min, NULL);
3081         g_object_set(object, "zoom_max", source->zoom_max, NULL);
3082         g_object_set(object, "loading", source->loading, NULL);
3083
3084         pr->complete = source->complete;
3085
3086         pr->x_scroll = source->x_scroll;
3087         pr->y_scroll = source->y_scroll;
3088
3089         scroll_reset = pr->scroll_reset;
3090         pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
3091
3092         if (source->source_tiles_enabled)
3093                 {
3094                 pr_source_tile_unset(pr);
3095
3096                 pr->source_tiles_enabled = source->source_tiles_enabled;
3097                 pr->source_tiles_cache_size = source->source_tiles_cache_size;
3098                 pr->source_tile_width = source->source_tile_width;
3099                 pr->source_tile_height = source->source_tile_height;
3100                 pr->image_width = source->image_width;
3101                 pr->image_height = source->image_height;
3102
3103                 pr->func_tile_request = source->func_tile_request;
3104                 pr->func_tile_dispose = source->func_tile_dispose;
3105                 pr->func_tile_data = source->func_tile_data;
3106
3107                 pr->source_tiles = source->source_tiles;
3108                 source->source_tiles = NULL;
3109
3110                 pr_zoom_sync(pr, source->zoom, TRUE, FALSE, TRUE, FALSE, 0, 0);
3111                 pr_redraw(pr, TRUE);
3112                 }
3113         else
3114                 {
3115                 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
3116                 }
3117
3118         pr->scroll_reset = scroll_reset;
3119
3120         pixbuf_renderer_set_pixbuf(source, NULL, source->zoom);
3121         pr_queue_clear(source);
3122         pr_tile_free_all(source);
3123 }
3124
3125 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
3126 {
3127         gint x1, y1, x2, y2;
3128
3129         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3130
3131         if (pr->source_tiles_enabled)
3132                 {
3133                 pr_source_tile_changed(pr, x, y, width, height);
3134                 }
3135
3136         if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
3137                 {
3138                 /* increase region when using a zoom quality that may access surrounding pixels */
3139                 y -= 1;
3140                 height += 2;
3141                 }
3142
3143         x1 = (gint)floor((double)x * pr->scale);
3144         y1 = (gint)floor((double)y * pr->scale);
3145         x2 = (gint)ceil((double)(x + width) * pr->scale);
3146         y2 = (gint)ceil((double)(y + height) * pr->scale);
3147
3148         pr_queue(pr, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
3149 }
3150
3151 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
3152 {
3153         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3154
3155         pr_zoom_adjust_real(pr, increment, FALSE, 0, 0);
3156 }
3157
3158 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
3159 {
3160         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3161
3162         pr_zoom_adjust_real(pr, increment, TRUE, x, y);
3163 }
3164
3165 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
3166 {
3167         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3168
3169         pr_zoom_sync(pr, zoom, FALSE, FALSE, FALSE, FALSE, 0, 0);
3170 }
3171
3172 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
3173 {
3174         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3175
3176         return pr->zoom;
3177 }
3178
3179 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
3180 {
3181         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
3182
3183         return pr->scale;
3184 }
3185
3186 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
3187 {
3188         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3189
3190         if (min > 1.0 || max < 1.0) return;
3191         if (min < 1.0 && min > -1.0) return;
3192         if (min < -200.0 || max > 200.0) return;
3193
3194         if (pr->zoom_min != min)
3195                 {
3196                 pr->zoom_min = min;
3197                 g_object_notify(G_OBJECT(pr), "zoom_min");
3198                 }
3199         if (pr->zoom_max != max)
3200                 {
3201                 pr->zoom_max = max;
3202                 g_object_notify(G_OBJECT(pr), "zoom_max");
3203                 }
3204 }
3205
3206 gint pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
3207 {
3208         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3209         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3210
3211         if (!pr->pixbuf && !pr->source_tiles_enabled)
3212                 {
3213                 *width = 0;
3214                 *height = 0;
3215                 return FALSE;
3216                 }
3217
3218         *width = pr->image_width;
3219         *height = pr->image_height;
3220         return TRUE;
3221 }
3222
3223 gint pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
3224 {
3225         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3226         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
3227
3228         if (!pr->pixbuf && !pr->source_tiles_enabled)
3229                 {
3230                 *width = 0;
3231                 *height = 0;
3232                 return FALSE;
3233                 }
3234
3235         *width = pr->width;
3236         *height = pr->height;
3237         return TRUE;
3238 }
3239
3240 gint pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
3241 {
3242         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3243         g_return_val_if_fail(rect != NULL, FALSE);
3244
3245         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
3246             !pr->scale)
3247                 {
3248                 rect->x = 0;
3249                 rect->y = 0;
3250                 rect->width = 0;
3251                 rect->height = 0;
3252                 return FALSE;
3253                 }
3254
3255         rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
3256         rect->y = (gint)((gdouble)pr->y_scroll / pr->scale);
3257         rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
3258         rect->height = (gint)((gdouble)pr->vis_height / pr->scale);
3259         return TRUE;
3260 }
3261
3262 gint pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
3263 {
3264         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
3265         g_return_val_if_fail(rect != NULL, FALSE);
3266
3267         if ((!pr->pixbuf && !pr->source_tiles_enabled))
3268                 {
3269                 rect->x = 0;
3270                 rect->y = 0;
3271                 rect->width = 0;
3272                 rect->height = 0;
3273                 return FALSE;
3274                 }
3275
3276         rect->x = pr->x_scroll;
3277         rect->y = pr->y_scroll;
3278         rect->width = pr->vis_width;
3279         rect->height = pr->vis_height;
3280         return TRUE;
3281 }
3282
3283