added 2010 to copyright text
[geeqie.git] / src / pixbuf-renderer.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2010 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <math.h>
17
18 #include "main.h"
19 #include "pixbuf-renderer.h"
20
21 #include "intl.h"
22 #include "layout.h"
23
24 #include <gtk/gtk.h>
25
26
27 /* comment this out if not using this from within Geeqie
28  * defining GQ_BUILD does these things:
29  *   - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
30  */
31 #define GQ_BUILD 1
32
33 #ifdef GQ_BUILD
34 #include "main.h"
35 #include "pixbuf_util.h"
36 #include "exif.h"
37 #else
38 typedef enum {
39         EXIF_ORIENTATION_UNKNOWN        = 0,
40         EXIF_ORIENTATION_TOP_LEFT       = 1,
41         EXIF_ORIENTATION_TOP_RIGHT      = 2,
42         EXIF_ORIENTATION_BOTTOM_RIGHT   = 3,
43         EXIF_ORIENTATION_BOTTOM_LEFT    = 4,
44         EXIF_ORIENTATION_LEFT_TOP       = 5,
45         EXIF_ORIENTATION_RIGHT_TOP      = 6,
46         EXIF_ORIENTATION_RIGHT_BOTTOM   = 7,
47         EXIF_ORIENTATION_LEFT_BOTTOM    = 8
48 } ExifOrientationType;
49 #endif
50
51
52 /* size to use when breaking up image pane for rendering */
53 #define PR_TILE_SIZE 128
54
55 /* default size of tile cache (mb) */
56 #define PR_CACHE_SIZE_DEFAULT 8
57
58 /* default min and max zoom */
59 #define PR_ZOOM_MIN -32.0
60 #define PR_ZOOM_MAX 32.0
61
62 /* distance to drag mouse to disable image flip */
63 #define PR_DRAG_SCROLL_THRESHHOLD 4
64
65 /* increase pan rate when holding down shift */
66 #define PR_PAN_SHIFT_MULTIPLIER 6
67
68 /* scroller config */
69 #define PR_SCROLLER_UPDATES_PER_SEC 30
70 #define PR_SCROLLER_DEAD_ZONE 6
71
72 /* alpha channel checkerboard background (same as gimp) */
73 #define PR_ALPHA_CHECK1 0x00999999
74 #define PR_ALPHA_CHECK2 0x00666666
75 #define PR_ALPHA_CHECK_SIZE 16
76
77 /* when scaling image to below this size, use nearest pixel for scaling
78  * (below about 4, the other scale types become slow generating their conversion tables)
79  */
80 #define PR_MIN_SCALE_SIZE 8
81
82 /* round A up/down to integer count of B */
83 #define ROUND_UP(A,B)   ((gint)(((A)+(B)-1)/(B))*(B))
84 #define ROUND_DOWN(A,B) ((gint)(((A))/(B))*(B))
85
86 typedef enum {
87         TILE_RENDER_NONE = 0,   /* do nothing */
88         TILE_RENDER_AREA,       /* render an area of the tile */
89         TILE_RENDER_ALL         /* render the whole tile */
90 } ImageTileRenderType;
91
92 typedef struct _ImageTile ImageTile;
93 typedef struct _QueueData QueueData;
94
95 struct _ImageTile
96 {
97         GdkPixmap *pixmap;      /* off screen buffer */
98         GdkPixbuf *pixbuf;      /* pixbuf area for zooming */
99         gint x;                 /* x offset into image */
100         gint y;                 /* y offset into image */
101         gint w;                 /* width that is visible (may be less if at edge of image) */
102         gint h;                 /* height '' */
103
104         gboolean blank;
105
106 /* render_todo: (explanation)
107         NONE    do nothing
108         AREA    render area of tile, usually only used when loading an image
109                 note: will jump to an ALL if render_done is not ALL.
110         ALL     render entire tile, if never done before w/ ALL, for expose events *only*
111 */
112
113         ImageTileRenderType render_todo;        /* what to do (see above) */
114         ImageTileRenderType render_done;        /* highest that has been done before on tile */
115
116         QueueData *qd;
117         QueueData *qd2;
118
119         guint size;             /* est. memory used by pixmap and pixbuf */
120 };
121
122 struct _QueueData
123 {
124         ImageTile *it;
125         gint x;
126         gint y;
127         gint w;
128         gint h;
129         gboolean new_data;
130 };
131
132 typedef struct _SourceTile SourceTile;
133 struct _SourceTile
134 {
135         gint x;
136         gint y;
137         GdkPixbuf *pixbuf;
138         gboolean blank;
139 };
140
141 typedef struct _OverlayData OverlayData;
142 struct _OverlayData
143 {
144         gint id;
145
146         GdkPixbuf *pixbuf;
147         GdkWindow *window;
148
149         gint x;
150         gint y;
151
152         OverlayRendererFlags flags;
153 };
154
155 enum {
156         SIGNAL_ZOOM = 0,
157         SIGNAL_CLICKED,
158         SIGNAL_SCROLL_NOTIFY,
159         SIGNAL_RENDER_COMPLETE,
160         SIGNAL_DRAG,
161         SIGNAL_UPDATE_PIXEL,
162         SIGNAL_COUNT
163 };
164
165 enum {
166         PROP_0,
167         PROP_ZOOM_MIN,
168         PROP_ZOOM_MAX,
169         PROP_ZOOM_QUALITY,
170         PROP_ZOOM_2PASS,
171         PROP_ZOOM_EXPAND,
172         PROP_DITHER_QUALITY,
173         PROP_SCROLL_RESET,
174         PROP_DELAY_FLIP,
175         PROP_LOADING,
176         PROP_COMPLETE,
177         PROP_CACHE_SIZE_DISPLAY,
178         PROP_CACHE_SIZE_TILES,
179         PROP_WINDOW_FIT,
180         PROP_WINDOW_LIMIT,
181         PROP_WINDOW_LIMIT_VALUE,
182         PROP_AUTOFIT_LIMIT,
183         PROP_AUTOFIT_LIMIT_VALUE
184 };
185
186 typedef enum {
187         PR_ZOOM_NONE            = 0,
188         PR_ZOOM_FORCE           = 1 << 0,
189         PR_ZOOM_NEW             = 1 << 1,
190         PR_ZOOM_CENTER          = 1 << 2,
191         PR_ZOOM_INVALIDATE      = 1 << 3,
192         PR_ZOOM_LAZY            = 1 << 4  /* wait with redraw for pixbuf_renderer_area_changed */
193 } PrZoomFlags;
194
195 static guint signals[SIGNAL_COUNT] = { 0 };
196 static GtkEventBoxClass *parent_class = NULL;
197
198
199
200 static void pixbuf_renderer_class_init(PixbufRendererClass *class);
201 static void pixbuf_renderer_init(PixbufRenderer *pr);
202 static void pixbuf_renderer_finalize(GObject *object);
203 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
204                                          const GValue *value, GParamSpec *pspec);
205 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
206                                          GValue *value, GParamSpec *pspec);
207 static gboolean pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event);
208
209 static void pr_render_complete_signal(PixbufRenderer *pr);
210
211 static void pr_overlay_list_clear(PixbufRenderer *pr);
212 static void pr_scroller_timer_set(PixbufRenderer *pr, gboolean start);
213 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
214
215
216 static void pr_source_tile_free_all(PixbufRenderer *pr);
217 static void pr_tile_free_all(PixbufRenderer *pr);
218 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h);
219 static gboolean pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it);
220 static void pr_queue_clear(PixbufRenderer *pr);
221 static void pr_queue_merge(QueueData *parent, QueueData *qd);
222 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
223                      gint clamp, ImageTileRenderType render, gboolean new_data, gboolean only_existing);
224
225 static void pr_redraw(PixbufRenderer *pr, gboolean new_data);
226
227 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
228                          PrZoomFlags flags, gint px, gint py);
229
230 static void pr_signals_connect(PixbufRenderer *pr);
231 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data);
232 static void pr_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data);
233 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area);
234 static gint pr_queue_draw_idle_cb(gpointer data);
235
236
237 /*
238  *-------------------------------------------------------------------
239  * Pixbuf Renderer object
240  *-------------------------------------------------------------------
241  */
242
243 GType pixbuf_renderer_get_type(void)
244 {
245         static GType pixbuf_renderer_type = 0;
246
247         if (!pixbuf_renderer_type)
248                 {
249                 static const GTypeInfo pixbuf_renderer_info =
250                         {
251                         sizeof(PixbufRendererClass), /* class_size */
252                         NULL,           /* base_init */
253                         NULL,           /* base_finalize */
254                         (GClassInitFunc)pixbuf_renderer_class_init,
255                         NULL,           /* class_finalize */
256                         NULL,           /* class_data */
257                         sizeof(PixbufRenderer), /* instance_size */
258                         0,              /* n_preallocs */
259                         (GInstanceInitFunc)pixbuf_renderer_init, /* instance_init */
260                         NULL,           /* value_table */
261                         };
262
263                 pixbuf_renderer_type = g_type_register_static(GTK_TYPE_EVENT_BOX, "PixbufRenderer",
264                                                               &pixbuf_renderer_info, 0);
265                 }
266
267         return pixbuf_renderer_type;
268 }
269
270 static void pixbuf_renderer_class_init(PixbufRendererClass *class)
271 {
272         GObjectClass *gobject_class = G_OBJECT_CLASS(class);
273         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
274
275         parent_class = g_type_class_peek_parent(class);
276
277         gobject_class->set_property = pixbuf_renderer_set_property;
278         gobject_class->get_property = pixbuf_renderer_get_property;
279
280         gobject_class->finalize = pixbuf_renderer_finalize;
281
282         widget_class->expose_event = pixbuf_renderer_expose;
283
284         g_object_class_install_property(gobject_class,
285                                         PROP_ZOOM_MIN,
286                                         g_param_spec_double("zoom_min",
287                                                             "Zoom minimum",
288                                                             NULL,
289                                                             -1000.0,
290                                                             1000.0,
291                                                             PR_ZOOM_MIN,
292                                                             G_PARAM_READABLE | G_PARAM_WRITABLE));
293
294         g_object_class_install_property(gobject_class,
295                                         PROP_ZOOM_MAX,
296                                         g_param_spec_double("zoom_max",
297                                                             "Zoom maximum",
298                                                             NULL,
299                                                             -1000.0,
300                                                             1000.0,
301                                                             PR_ZOOM_MIN,
302                                                             G_PARAM_READABLE | G_PARAM_WRITABLE));
303
304         g_object_class_install_property(gobject_class,
305                                         PROP_ZOOM_QUALITY,
306                                         g_param_spec_uint("zoom_quality",
307                                                           "Zoom quality",
308                                                           NULL,
309                                                           GDK_INTERP_NEAREST,
310                                                           GDK_INTERP_HYPER,
311                                                           GDK_INTERP_BILINEAR,
312                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
313
314         g_object_class_install_property(gobject_class,
315                                         PROP_ZOOM_2PASS,
316                                         g_param_spec_boolean("zoom_2pass",
317                                                              "2 pass zoom",
318                                                              NULL,
319                                                              TRUE,
320                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
321
322         g_object_class_install_property(gobject_class,
323                                         PROP_ZOOM_EXPAND,
324                                         g_param_spec_boolean("zoom_expand",
325                                                              "Expand image in autozoom.",
326                                                              NULL,
327                                                              FALSE,
328                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
329
330         g_object_class_install_property(gobject_class,
331                                         PROP_DITHER_QUALITY,
332                                         g_param_spec_uint("dither_quality",
333                                                           "Dither quality",
334                                                           NULL,
335                                                           GDK_RGB_DITHER_NONE,
336                                                           GDK_RGB_DITHER_MAX,
337                                                           GDK_RGB_DITHER_NORMAL,
338                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
339
340         g_object_class_install_property(gobject_class,
341                                         PROP_SCROLL_RESET,
342                                         g_param_spec_uint("scroll_reset",
343                                                           "New image scroll reset",
344                                                           NULL,
345                                                           PR_SCROLL_RESET_TOPLEFT,
346                                                           PR_SCROLL_RESET_NOCHANGE,
347                                                           PR_SCROLL_RESET_TOPLEFT,
348                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
349
350         g_object_class_install_property(gobject_class,
351                                         PROP_DELAY_FLIP,
352                                         g_param_spec_boolean("delay_flip",
353                                                              "Delay image update",
354                                                              NULL,
355                                                              FALSE,
356                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
357
358         g_object_class_install_property(gobject_class,
359                                         PROP_LOADING,
360                                         g_param_spec_boolean("loading",
361                                                              "Image actively loading",
362                                                              NULL,
363                                                              FALSE,
364                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
365
366         g_object_class_install_property(gobject_class,
367                                         PROP_COMPLETE,
368                                         g_param_spec_boolean("complete",
369                                                              "Image rendering complete",
370                                                              NULL,
371                                                              FALSE,
372                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
373
374         g_object_class_install_property(gobject_class,
375                                         PROP_CACHE_SIZE_DISPLAY,
376                                         g_param_spec_uint("cache_display",
377                                                           "Display cache size MB",
378                                                           NULL,
379                                                           0,
380                                                           128,
381                                                           PR_CACHE_SIZE_DEFAULT,
382                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
383
384         g_object_class_install_property(gobject_class,
385                                         PROP_CACHE_SIZE_TILES,
386                                         g_param_spec_uint("cache_tiles",
387                                                           "Tile cache count",
388                                                           "Number of tiles to retain in memory at any one time.",
389                                                           0,
390                                                           256,
391                                                           PR_CACHE_SIZE_DEFAULT,
392                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
393
394         g_object_class_install_property(gobject_class,
395                                         PROP_WINDOW_FIT,
396                                         g_param_spec_boolean("window_fit",
397                                                              "Fit window to image size",
398                                                              NULL,
399                                                              FALSE,
400                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
401
402         g_object_class_install_property(gobject_class,
403                                         PROP_WINDOW_LIMIT,
404                                         g_param_spec_boolean("window_limit",
405                                                              "Limit size of parent window",
406                                                              NULL,
407                                                              FALSE,
408                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
409
410         g_object_class_install_property(gobject_class,
411                                         PROP_WINDOW_LIMIT_VALUE,
412                                         g_param_spec_uint("window_limit_value",
413                                                           "Size limit of parent window",
414                                                           NULL,
415                                                           10,
416                                                           150,
417                                                           100,
418                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
419
420         g_object_class_install_property(gobject_class,
421                                         PROP_AUTOFIT_LIMIT,
422                                         g_param_spec_boolean("autofit_limit",
423                                                              "Limit size of image when autofitting",
424                                                              NULL,
425                                                              FALSE,
426                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
427
428         g_object_class_install_property(gobject_class,
429                                         PROP_AUTOFIT_LIMIT_VALUE,
430                                         g_param_spec_uint("autofit_limit_value",
431                                                           "Size limit of image when autofitting",
432                                                           NULL,
433                                                           10,
434                                                           150,
435                                                           100,
436                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
437
438
439         signals[SIGNAL_ZOOM] =
440                 g_signal_new("zoom",
441                              G_OBJECT_CLASS_TYPE(gobject_class),
442                              G_SIGNAL_RUN_LAST,
443                              G_STRUCT_OFFSET(PixbufRendererClass, zoom),
444                              NULL, NULL,
445                              g_cclosure_marshal_VOID__DOUBLE,
446                              G_TYPE_NONE, 1,
447                              G_TYPE_DOUBLE);
448
449         signals[SIGNAL_CLICKED] =
450                 g_signal_new("clicked",
451                              G_OBJECT_CLASS_TYPE(gobject_class),
452                              G_SIGNAL_RUN_LAST,
453                              G_STRUCT_OFFSET(PixbufRendererClass, clicked),
454                              NULL, NULL,
455                              g_cclosure_marshal_VOID__BOXED,
456                              G_TYPE_NONE, 1,
457                              GDK_TYPE_EVENT);
458
459         signals[SIGNAL_SCROLL_NOTIFY] =
460                 g_signal_new("scroll-notify",
461                              G_OBJECT_CLASS_TYPE(gobject_class),
462                              G_SIGNAL_RUN_LAST,
463                              G_STRUCT_OFFSET(PixbufRendererClass, scroll_notify),
464                              NULL, NULL,
465                              g_cclosure_marshal_VOID__VOID,
466                              G_TYPE_NONE, 0);
467
468         signals[SIGNAL_RENDER_COMPLETE] =
469                 g_signal_new("render-complete",
470                              G_OBJECT_CLASS_TYPE(gobject_class),
471                              G_SIGNAL_RUN_LAST,
472                              G_STRUCT_OFFSET(PixbufRendererClass, render_complete),
473                              NULL, NULL,
474                              g_cclosure_marshal_VOID__VOID,
475                              G_TYPE_NONE, 0);
476
477         signals[SIGNAL_DRAG] =
478                 g_signal_new("drag",
479                              G_OBJECT_CLASS_TYPE(gobject_class),
480                              G_SIGNAL_RUN_LAST,
481                              G_STRUCT_OFFSET(PixbufRendererClass, drag),
482                              NULL, NULL,
483                              g_cclosure_marshal_VOID__BOXED,
484                              G_TYPE_NONE, 1,
485                              GDK_TYPE_EVENT);
486                              
487         signals[SIGNAL_UPDATE_PIXEL] =
488                 g_signal_new("update-pixel",
489                              G_OBJECT_CLASS_TYPE(gobject_class),
490                              G_SIGNAL_RUN_LAST,
491                              G_STRUCT_OFFSET(PixbufRendererClass, update_pixel),
492                              NULL, NULL,
493                              g_cclosure_marshal_VOID__VOID,
494                              G_TYPE_NONE, 0);
495 }
496
497 static void pixbuf_renderer_init(PixbufRenderer *pr)
498 {
499         GtkWidget *box;
500
501         box = GTK_WIDGET(pr);
502
503         pr->zoom_min = PR_ZOOM_MIN;
504         pr->zoom_max = PR_ZOOM_MAX;
505         pr->zoom_quality = GDK_INTERP_BILINEAR;
506         pr->zoom_2pass = FALSE;
507
508         pr->zoom = 1.0;
509         pr->scale = 1.0;
510
511         pr->dither_quality = GDK_RGB_DITHER_NORMAL;
512
513         pr->scroll_reset = PR_SCROLL_RESET_TOPLEFT;
514
515         pr->draw_idle_id = 0;
516
517         pr->tile_width = PR_TILE_SIZE;
518         pr->tile_height = PR_TILE_SIZE;
519
520         pr->tiles = NULL;
521         pr->tile_cache_size = 0;
522
523         pr->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
524
525         pr->scroller_id = 0;
526         pr->scroller_overlay = -1;
527         
528         pr->x_mouse = -1;
529         pr->y_mouse = -1;
530
531         pr->source_tiles_enabled = FALSE;
532         pr->source_tiles = NULL;
533
534         pr->orientation = 1;
535
536         pr->norm_center_x = 0.5;
537         pr->norm_center_y = 0.5;
538
539         gtk_widget_set_double_buffered(box, FALSE);
540         g_signal_connect_after(G_OBJECT(box), "size_allocate",
541                                G_CALLBACK(pr_size_cb), pr);
542
543         g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
544                          G_CALLBACK(pr_hierarchy_changed_cb), pr);
545
546         pr_signals_connect(pr);
547 }
548
549 static void pixbuf_renderer_finalize(GObject *object)
550 {
551         PixbufRenderer *pr;
552
553         pr = PIXBUF_RENDERER(object);
554
555         pr_queue_clear(pr);
556         pr_tile_free_all(pr);
557
558
559         if (pr->pixbuf) g_object_unref(pr->pixbuf);
560         if (pr->spare_tile) g_object_unref(pr->spare_tile);
561
562         pr_scroller_timer_set(pr, FALSE);
563         pr_overlay_list_clear(pr);
564
565         pr_source_tile_free_all(pr);
566 }
567
568 PixbufRenderer *pixbuf_renderer_new(void)
569 {
570         return g_object_new(TYPE_PIXBUF_RENDERER, NULL);
571 }
572
573 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
574                                          const GValue *value, GParamSpec *pspec)
575 {
576         PixbufRenderer *pr;
577
578         pr = PIXBUF_RENDERER(object);
579
580         switch (prop_id)
581                 {
582                 case PROP_ZOOM_MIN:
583                         pr->zoom_min = g_value_get_double(value);
584                         break;
585                 case PROP_ZOOM_MAX:
586                         pr->zoom_max = g_value_get_double(value);
587                         break;
588                 case PROP_ZOOM_QUALITY:
589                         pr->zoom_quality = g_value_get_uint(value);
590                         break;
591                 case PROP_ZOOM_2PASS:
592                         pr->zoom_2pass = g_value_get_boolean(value);
593                         break;
594                 case PROP_ZOOM_EXPAND:
595                         pr->zoom_expand = g_value_get_boolean(value);
596                         break;
597                 case PROP_DITHER_QUALITY:
598                         pr->dither_quality = g_value_get_uint(value);
599                         break;
600                 case PROP_SCROLL_RESET:
601                         pr->scroll_reset = g_value_get_uint(value);
602                         break;
603                 case PROP_DELAY_FLIP:
604                         pr->delay_flip = g_value_get_boolean(value);
605                         break;
606                 case PROP_LOADING:
607                         pr->loading = g_value_get_boolean(value);
608                         break;
609                 case PROP_COMPLETE:
610                         pr->complete = g_value_get_boolean(value);
611                         break;
612                 case PROP_CACHE_SIZE_DISPLAY:
613                         pr->tile_cache_max = g_value_get_uint(value);
614                         break;
615                 case PROP_CACHE_SIZE_TILES:
616                         pr->source_tiles_cache_size = g_value_get_uint(value);
617                         break;
618                 case PROP_WINDOW_FIT:
619                         pr->window_fit = g_value_get_boolean(value);
620                         break;
621                 case PROP_WINDOW_LIMIT:
622                         pr->window_limit = g_value_get_boolean(value);
623                         break;
624                 case PROP_WINDOW_LIMIT_VALUE:
625                         pr->window_limit_size = g_value_get_uint(value);
626                         break;
627                 case PROP_AUTOFIT_LIMIT:
628                         pr->autofit_limit = g_value_get_boolean(value);
629                         break;
630                 case PROP_AUTOFIT_LIMIT_VALUE:
631                         pr->autofit_limit_size = g_value_get_uint(value);
632                         break;
633                 default:
634                         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
635                         break;
636                 }
637 }
638
639 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
640                                          GValue *value, GParamSpec *pspec)
641 {
642         PixbufRenderer *pr;
643
644         pr = PIXBUF_RENDERER(object);
645
646         switch (prop_id)
647                 {
648                 case PROP_ZOOM_MIN:
649                         g_value_set_double(value, pr->zoom_min);
650                         break;
651                 case PROP_ZOOM_MAX:
652                         g_value_set_double(value, pr->zoom_max);
653                         break;
654                 case PROP_ZOOM_QUALITY:
655                         g_value_set_uint(value, pr->zoom_quality);
656                         break;
657                 case PROP_ZOOM_2PASS:
658                         g_value_set_boolean(value, pr->zoom_2pass);
659                         break;
660                 case PROP_ZOOM_EXPAND:
661                         g_value_set_boolean(value, pr->zoom_expand);
662                         break;
663                 case PROP_DITHER_QUALITY:
664                         g_value_set_uint(value, pr->dither_quality);
665                         break;
666                 case PROP_SCROLL_RESET:
667                         g_value_set_uint(value, pr->scroll_reset);
668                         break;
669                 case PROP_DELAY_FLIP:
670                         g_value_set_boolean(value, pr->delay_flip);
671                         break;
672                 case PROP_LOADING:
673                         g_value_set_boolean(value, pr->loading);
674                         break;
675                 case PROP_COMPLETE:
676                         g_value_set_boolean(value, pr->complete);
677                         break;
678                 case PROP_CACHE_SIZE_DISPLAY:
679                         g_value_set_uint(value, pr->tile_cache_max);
680                         break;
681                 case PROP_CACHE_SIZE_TILES:
682                         g_value_set_uint(value, pr->source_tiles_cache_size);
683                         break;
684                 case PROP_WINDOW_FIT:
685                         g_value_set_boolean(value, pr->window_fit);
686                         break;
687                 case PROP_WINDOW_LIMIT:
688                         g_value_set_boolean(value, pr->window_limit);
689                         break;
690                 case PROP_WINDOW_LIMIT_VALUE:
691                         g_value_set_uint(value, pr->window_limit_size);
692                         break;
693                 case PROP_AUTOFIT_LIMIT:
694                         g_value_set_boolean(value, pr->autofit_limit);
695                         break;
696                 case PROP_AUTOFIT_LIMIT_VALUE:
697                         g_value_set_uint(value, pr->autofit_limit_size);
698                         break;
699                 default:
700                         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
701                         break;
702                 }
703 }
704
705 static gboolean pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event)
706 {
707         if (GTK_WIDGET_DRAWABLE(widget))
708                 {
709                 if (!GTK_WIDGET_NO_WINDOW(widget))
710                         {
711                         if (event->window != widget->window)
712                                 {
713                                 GdkRectangle area;
714
715                                 gdk_window_get_position(event->window, &area.x, &area.y);
716
717                                 area.x += event->area.x;
718                                 area.y += event->area.y;
719                                 area.width = event->area.width;
720                                 area.height = event->area.height;
721                                 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &area);
722                                 }
723                         else
724                                 {
725                                 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &event->area);
726                                 }
727                         }
728                 }
729
730         return FALSE;
731 }
732
733 /*
734  *-------------------------------------------------------------------
735  * misc utilities
736  *-------------------------------------------------------------------
737  */
738
739 static void widget_set_cursor(GtkWidget *widget, gint icon)
740 {
741         GdkCursor *cursor;
742
743         if (!widget->window) return;
744
745         if (icon == -1)
746                 {
747                 cursor = NULL;
748                 }
749         else
750                 {
751                 cursor = gdk_cursor_new(icon);
752                 }
753
754         gdk_window_set_cursor(widget->window, cursor);
755
756         if (cursor) gdk_cursor_unref(cursor);
757 }
758
759 static gint pixmap_calc_size(GdkPixmap *pixmap)
760 {
761         gint w, h, d;
762
763         d = gdk_drawable_get_depth(pixmap);
764         gdk_drawable_get_size(pixmap, &w, &h);
765         return w * h * (d / 8);
766 }
767
768 static gboolean pr_clip_region(gint x, gint y, gint w, gint h,
769                                gint clip_x, gint clip_y, gint clip_w, gint clip_h,
770                                gint *rx, gint *ry, gint *rw, gint *rh)
771 {
772         if (clip_x + clip_w <= x ||
773             clip_x >= x + w ||
774             clip_y + clip_h <= y ||
775             clip_y >= y + h)
776                 {
777                 return FALSE;
778                 }
779
780         *rx = MAX(x, clip_x);
781         *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
782
783         *ry = MAX(y, clip_y);
784         *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
785
786         return TRUE;
787 }
788
789 static gboolean pr_parent_window_sizable(PixbufRenderer *pr)
790 {
791         GdkWindowState state;
792
793         if (!pr->parent_window) return FALSE;
794         if (!pr->window_fit) return FALSE;
795         if (!GTK_WIDGET(pr)->window) return FALSE;
796
797         if (!pr->parent_window->window) return FALSE;
798         state = gdk_window_get_state(pr->parent_window->window);
799         if (state & GDK_WINDOW_STATE_MAXIMIZED) return FALSE;
800
801         return TRUE;
802 }
803
804 static gboolean pr_parent_window_resize(PixbufRenderer *pr, gint w, gint h)
805 {
806         GtkWidget *widget;
807         GtkWidget *parent;
808         gint ww, wh;
809
810         if (!pr_parent_window_sizable(pr)) return FALSE;
811
812         if (pr->window_limit)
813                 {
814                 gint sw = gdk_screen_width() * pr->window_limit_size / 100;
815                 gint sh = gdk_screen_height() * pr->window_limit_size / 100;
816
817                 if (w > sw) w = sw;
818                 if (h > sh) h = sh;
819                 }
820
821         widget = GTK_WIDGET(pr);
822         parent = GTK_WIDGET(pr->parent_window);
823
824         w += (parent->allocation.width - widget->allocation.width);
825         h += (parent->allocation.height - widget->allocation.height);
826
827         gdk_drawable_get_size(parent->window, &ww, &wh);
828         if (w == ww && h == wh) return FALSE;
829
830         gdk_window_resize(parent->window, w, h);
831
832         return TRUE;
833 }
834
835 void pixbuf_renderer_set_parent(PixbufRenderer *pr, GtkWindow *window)
836 {
837         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
838         g_return_if_fail(window == NULL || GTK_IS_WINDOW(window));
839
840         pr->parent_window = GTK_WIDGET(window);
841 }
842
843 GtkWindow *pixbuf_renderer_get_parent(PixbufRenderer *pr)
844 {
845         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
846
847         return GTK_WINDOW(pr->parent_window);
848 }
849
850
851 /*
852  *-------------------------------------------------------------------
853  * overlays
854  *-------------------------------------------------------------------
855  */
856
857 static void pr_overlay_get_position(PixbufRenderer *pr, OverlayData *od,
858                                     gint *x, gint *y, gint *w, gint *h)
859 {
860         gint px, py, pw, ph;
861
862         pw = gdk_pixbuf_get_width(od->pixbuf);
863         ph = gdk_pixbuf_get_height(od->pixbuf);
864         px = od->x;
865         py = od->y;
866
867         if (od->flags & OVL_RELATIVE)
868                 {
869                 if (px < 0) px = pr->window_width - pw + px;
870                 if (py < 0) py = pr->window_height - ph + py;
871                 }
872
873         if (x) *x = px;
874         if (y) *y = py;
875         if (w) *w = pw;
876         if (h) *h = ph;
877 }
878
879 static void pr_overlay_init_window(PixbufRenderer *pr, OverlayData *od)
880 {
881         gint px, py, pw, ph;
882         GdkWindowAttr attributes;
883         gint attributes_mask;
884
885         pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
886
887         attributes.window_type = GDK_WINDOW_CHILD;
888         attributes.wclass = GDK_INPUT_OUTPUT;
889         attributes.width = pw;
890         attributes.height = ph;
891         attributes.event_mask = GDK_EXPOSURE_MASK;
892         attributes_mask = 0;
893
894         od->window = gdk_window_new(GTK_WIDGET(pr)->window, &attributes, attributes_mask);
895         gdk_window_set_user_data(od->window, pr);
896         gdk_window_move(od->window, px, py);
897         gdk_window_show(od->window);
898 }
899
900 static void pr_overlay_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
901                             ImageTile *it)
902 {
903         GtkWidget *box;
904         GList *work;
905
906         box = GTK_WIDGET(pr);
907
908         work = pr->overlay_list;
909         while (work)
910                 {
911                 OverlayData *od;
912                 gint px, py, pw, ph;
913                 gint rx, ry, rw, rh;
914
915                 od = work->data;
916                 work = work->next;
917
918                 if (!od->window) pr_overlay_init_window(pr, od);
919                 
920                 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
921                 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
922                         {
923                         if (!pr->overlay_buffer)
924                                 {
925                                 pr->overlay_buffer = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
926                                 }
927
928                         if (it)
929                                 {
930                                 gdk_draw_drawable(pr->overlay_buffer, box->style->fg_gc[GTK_WIDGET_STATE(box)],
931                                                   it->pixmap,
932                                                   rx - (pr->x_offset + (it->x - pr->x_scroll)),
933                                                   ry - (pr->y_offset + (it->y - pr->y_scroll)),
934                                                   0, 0, rw, rh);
935                                 gdk_draw_pixbuf(pr->overlay_buffer,
936                                                 box->style->fg_gc[GTK_WIDGET_STATE(box)],
937                                                 od->pixbuf,
938                                                 rx - px, ry - py,
939                                                 0, 0, rw, rh,
940                                                 pr->dither_quality, rx, ry);
941                                 gdk_draw_drawable(od->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
942                                                   pr->overlay_buffer,
943                                                   0, 0,
944                                                   rx - px, ry - py, rw, rh);
945                                 }
946                         else
947                                 {
948                                 /* no ImageTile means region may be larger than our scratch buffer */
949                                 gint sx, sy;
950
951                                 for (sx = rx; sx < rx + rw; sx += pr->tile_width)
952                                     for (sy = ry; sy < ry + rh; sy += pr->tile_height)
953                                         {
954                                         gint sw, sh;
955
956                                         sw = MIN(rx + rw - sx, pr->tile_width);
957                                         sh = MIN(ry + rh - sy, pr->tile_height);
958
959                                         gdk_draw_rectangle(pr->overlay_buffer,
960                                                            box->style->bg_gc[GTK_WIDGET_STATE(box)], TRUE,
961                                                            0, 0, sw, sh);
962                                         gdk_draw_pixbuf(pr->overlay_buffer,
963                                                         box->style->fg_gc[GTK_WIDGET_STATE(box)],
964                                                         od->pixbuf,
965                                                         sx - px, sy - py,
966                                                         0, 0, sw, sh,
967                                                         pr->dither_quality, sx, sy);
968                                         gdk_draw_drawable(od->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
969                                                           pr->overlay_buffer,
970                                                           0, 0,
971                                                           sx - px, sy - py, sw, sh);
972                                         }
973                                 }
974                         }
975                 }
976 }
977
978 static void pr_overlay_queue_draw(PixbufRenderer *pr, OverlayData *od)
979 {
980         gint x, y, w, h;
981
982         pr_overlay_get_position(pr, od, &x, &y, &w, &h);
983         pr_queue(pr, pr->x_scroll - pr->x_offset + x,
984                  pr->y_scroll - pr->y_offset + y,
985                  w, h,
986                  FALSE, TILE_RENDER_ALL, FALSE, FALSE);
987
988         pr_border_draw(pr, x, y, w, h);
989 }
990
991 static void pr_overlay_queue_all(PixbufRenderer *pr)
992 {
993         GList *work;
994
995         work = pr->overlay_list;
996         while (work)
997                 {
998                 OverlayData *od = work->data;
999                 work = work->next;
1000
1001                 pr_overlay_queue_draw(pr, od);
1002                 }
1003 }
1004
1005 static void pr_overlay_update_sizes(PixbufRenderer *pr)
1006 {
1007         GList *work;
1008
1009         work = pr->overlay_list;
1010         while (work)
1011                 {
1012                 OverlayData *od = work->data;
1013                 work = work->next;
1014                 
1015                 if (!od->window) pr_overlay_init_window(pr, od);
1016                 
1017                 if (od->flags & OVL_RELATIVE)
1018                         {
1019                         gint x, y, w, h;
1020
1021                         pr_overlay_get_position(pr, od, &x, &y, &w, &h);
1022                         gdk_window_move_resize(od->window, x, y, w, h);
1023                         }
1024                 }
1025 }
1026
1027 static OverlayData *pr_overlay_find(PixbufRenderer *pr, gint id)
1028 {
1029         GList *work;
1030
1031         work = pr->overlay_list;
1032         while (work)
1033                 {
1034                 OverlayData *od = work->data;
1035                 work = work->next;
1036
1037                 if (od->id == id) return od;
1038                 }
1039
1040         return NULL;
1041 }
1042
1043
1044 gint pixbuf_renderer_overlay_add(PixbufRenderer *pr, GdkPixbuf *pixbuf, gint x, gint y,
1045                                  OverlayRendererFlags flags)
1046 {
1047         OverlayData *od;
1048         gint id;
1049
1050         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
1051         g_return_val_if_fail(pixbuf != NULL, -1);
1052
1053         id = 1;
1054         while (pr_overlay_find(pr, id)) id++;
1055
1056         od = g_new0(OverlayData, 1);
1057         od->id = id;
1058         od->pixbuf = pixbuf;
1059         g_object_ref(G_OBJECT(od->pixbuf));
1060         od->x = x;
1061         od->y = y;
1062         od->flags = flags;
1063
1064         pr_overlay_init_window(pr, od);
1065         
1066         pr->overlay_list = g_list_append(pr->overlay_list, od);
1067
1068         pr_overlay_queue_draw(pr, od);
1069
1070         return od->id;
1071 }
1072
1073 static void pr_overlay_free(PixbufRenderer *pr, OverlayData *od)
1074 {
1075         pr->overlay_list = g_list_remove(pr->overlay_list, od);
1076
1077         if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
1078         if (od->window) gdk_window_destroy(od->window);
1079         g_free(od);
1080
1081         if (!pr->overlay_list && pr->overlay_buffer)
1082                 {
1083                 g_object_unref(pr->overlay_buffer);
1084                 pr->overlay_buffer = NULL;
1085                 }
1086 }
1087
1088 static void pr_overlay_list_clear(PixbufRenderer *pr)
1089 {
1090         while (pr->overlay_list)
1091                 {
1092                 OverlayData *od;
1093
1094                 od = pr->overlay_list->data;
1095                 pr_overlay_free(pr, od);
1096                 }
1097 }
1098
1099 static void pr_overlay_list_reset_window(PixbufRenderer *pr)
1100 {
1101         GList *work;
1102
1103         if (pr->overlay_buffer) g_object_unref(pr->overlay_buffer);
1104         pr->overlay_buffer = NULL;
1105
1106         work = pr->overlay_list;
1107         while (work)
1108                 {
1109                 OverlayData *od = work->data;
1110                 work = work->next;
1111                 if (od->window) gdk_window_destroy(od->window);
1112                 od->window = NULL;
1113                 }
1114 }
1115
1116 void pixbuf_renderer_overlay_set(PixbufRenderer *pr, gint id, GdkPixbuf *pixbuf, gint x, gint y)
1117 {
1118         OverlayData *od;
1119
1120         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1121
1122         od = pr_overlay_find(pr, id);
1123         if (!od) return;
1124
1125         if (pixbuf)
1126                 {
1127                 gint px, py, pw, ph;
1128
1129                 g_object_ref(G_OBJECT(pixbuf));
1130                 g_object_unref(G_OBJECT(od->pixbuf));
1131                 od->pixbuf = pixbuf;
1132
1133                 od->x = x;
1134                 od->y = y;
1135
1136                 if (!od->window) pr_overlay_init_window(pr, od);
1137
1138                 pr_overlay_queue_draw(pr, od);
1139                 pr_overlay_get_position(pr, od, &px, &py, &pw, &ph);
1140                 gdk_window_move_resize(od->window, px, py, pw, ph);
1141                 }
1142         else
1143                 {
1144                 pr_overlay_queue_draw(pr, od);
1145                 pr_overlay_free(pr, od);
1146                 }
1147 }
1148
1149 gboolean pixbuf_renderer_overlay_get(PixbufRenderer *pr, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
1150 {
1151         OverlayData *od;
1152
1153         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1154
1155         od = pr_overlay_find(pr, id);
1156         if (!od) return FALSE;
1157
1158         if (pixbuf) *pixbuf = od->pixbuf;
1159         if (x) *x = od->x;
1160         if (y) *y = od->y;
1161
1162         return TRUE;
1163 }
1164
1165 void pixbuf_renderer_overlay_remove(PixbufRenderer *pr, gint id)
1166 {
1167         pixbuf_renderer_overlay_set(pr, id, NULL, 0, 0);
1168 }
1169
1170 static void pr_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data)
1171 {
1172         PixbufRenderer *pr = data;
1173         pr_overlay_list_reset_window(pr);
1174 }
1175
1176
1177 /*
1178  *-------------------------------------------------------------------
1179  * scroller overlay
1180  *-------------------------------------------------------------------
1181  */
1182
1183
1184 static gboolean pr_scroller_update_cb(gpointer data)
1185 {
1186         PixbufRenderer *pr = data;
1187         gint x, y;
1188         gint xinc, yinc;
1189
1190         /* this was a simple scroll by difference between scroller and mouse position,
1191          * but all this math results in a smoother result and accounts for a dead zone.
1192          */
1193
1194         if (abs(pr->scroller_xpos - pr->scroller_x) < PR_SCROLLER_DEAD_ZONE)
1195                 {
1196                 x = 0;
1197                 }
1198         else
1199                 {
1200                 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1201                 x = (pr->scroller_xpos - pr->scroller_x) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1202                 x += (x > 0) ? -shift : shift;
1203                 }
1204
1205         if (abs(pr->scroller_ypos - pr->scroller_y) < PR_SCROLLER_DEAD_ZONE)
1206                 {
1207                 y = 0;
1208                 }
1209         else
1210                 {
1211                 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1212                 y = (pr->scroller_ypos - pr->scroller_y) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
1213                 y += (y > 0) ? -shift : shift;
1214                 }
1215
1216         if (abs(x) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
1217                 {
1218                 xinc = x;
1219                 }
1220         else
1221                 {
1222                 xinc = pr->scroller_xinc;
1223
1224                 if (x >= 0)
1225                         {
1226                         if (xinc < 0) xinc = 0;
1227                         if (x < xinc) xinc = x;
1228                         if (x > xinc) xinc = MIN(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
1229                         }
1230                 else
1231                         {
1232                         if (xinc > 0) xinc = 0;
1233                         if (x > xinc) xinc = x;
1234                         if (x < xinc) xinc = MAX(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
1235                         }
1236                 }
1237
1238         if (abs(y) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
1239                 {
1240                 yinc = y;
1241                 }
1242         else
1243                 {
1244                 yinc = pr->scroller_yinc;
1245
1246                 if (y >= 0)
1247                         {
1248                         if (yinc < 0) yinc = 0;
1249                         if (y < yinc) yinc = y;
1250                         if (y > yinc) yinc = MIN(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1251                         }
1252                 else
1253                         {
1254                         if (yinc > 0) yinc = 0;
1255                         if (y > yinc) yinc = y;
1256                         if (y < yinc) yinc = MAX(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
1257                         }
1258                 }
1259
1260         pr->scroller_xinc = xinc;
1261         pr->scroller_yinc = yinc;
1262
1263         xinc = xinc / PR_SCROLLER_UPDATES_PER_SEC;
1264         yinc = yinc / PR_SCROLLER_UPDATES_PER_SEC;
1265
1266         pixbuf_renderer_scroll(pr, xinc, yinc);
1267
1268         return TRUE;
1269 }
1270
1271 static void pr_scroller_timer_set(PixbufRenderer *pr, gboolean start)
1272 {
1273         if (pr->scroller_id)
1274                 {
1275                 g_source_remove(pr->scroller_id);
1276                 pr->scroller_id = 0;
1277                 }
1278
1279         if (start)
1280                 {
1281                 pr->scroller_id = g_timeout_add(1000 / PR_SCROLLER_UPDATES_PER_SEC,
1282                                                 pr_scroller_update_cb, pr);
1283                 }
1284 }
1285
1286 static void pr_scroller_start(PixbufRenderer *pr, gint x, gint y)
1287 {
1288         if (pr->scroller_overlay == -1)
1289                 {
1290                 GdkPixbuf *pixbuf;
1291                 gint w, h;
1292
1293 #ifdef GQ_BUILD
1294                 pixbuf = pixbuf_inline(PIXBUF_INLINE_SCROLLER);
1295 #else
1296                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
1297                 gdk_pixbuf_fill(pixbuf, 0x000000ff);
1298 #endif
1299                 w = gdk_pixbuf_get_width(pixbuf);
1300                 h = gdk_pixbuf_get_height(pixbuf);
1301
1302                 pr->scroller_overlay = pixbuf_renderer_overlay_add(pr, pixbuf, x - w / 2, y - h / 2, OVL_NORMAL);
1303                 g_object_unref(pixbuf);
1304                 }
1305
1306         pr->scroller_x = x;
1307         pr->scroller_y = y;
1308         pr->scroller_xpos = x;
1309         pr->scroller_ypos = y;
1310
1311         pr_scroller_timer_set(pr, TRUE);
1312 }
1313
1314 static void pr_scroller_stop(PixbufRenderer *pr)
1315 {
1316         if (!pr->scroller_id) return;
1317
1318         pixbuf_renderer_overlay_remove(pr, pr->scroller_overlay);
1319         pr->scroller_overlay = -1;
1320
1321         pr_scroller_timer_set(pr, FALSE);
1322 }
1323
1324 /*
1325  *-------------------------------------------------------------------
1326  * borders
1327  *-------------------------------------------------------------------
1328  */
1329
1330 static void pr_border_draw(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
1331 {
1332         GtkWidget *box;
1333         gint rx, ry, rw, rh;
1334
1335         box = GTK_WIDGET(pr);
1336
1337         if (!box->window) return;
1338
1339         if (!pr->pixbuf && !pr->source_tiles_enabled)
1340                 {
1341                 if (pr_clip_region(x, y, w, h,
1342                                    0, 0,
1343                                    pr->window_width, pr->window_height,
1344                                    &rx, &ry, &rw, &rh))
1345                         {
1346                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1347                         pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1348                         }
1349                 return;
1350                 }
1351
1352         if (pr->vis_width < pr->window_width)
1353                 {
1354                 if (pr->x_offset > 0 &&
1355                     pr_clip_region(x, y, w, h,
1356                                    0, 0,
1357                                    pr->x_offset, pr->window_height,
1358                                    &rx, &ry, &rw, &rh))
1359                         {
1360                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1361                         pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1362                         }
1363                 if (pr->window_width - pr->vis_width - pr->x_offset > 0 &&
1364                     pr_clip_region(x, y, w, h,
1365                                    pr->x_offset + pr->vis_width, 0,
1366                                    pr->window_width - pr->vis_width - pr->x_offset, pr->window_height,
1367                                    &rx, &ry, &rw, &rh))
1368                         {
1369                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1370                         pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1371                         }
1372                 }
1373         if (pr->vis_height < pr->window_height)
1374                 {
1375                 if (pr->y_offset > 0 &&
1376                     pr_clip_region(x, y, w, h,
1377                                    pr->x_offset, 0,
1378                                    pr->vis_width, pr->y_offset,
1379                                    &rx, &ry, &rw, &rh))
1380                         {
1381                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1382                         pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1383                         }
1384                 if (pr->window_height - pr->vis_height - pr->y_offset > 0 &&
1385                     pr_clip_region(x, y, w, h,
1386                                    pr->x_offset, pr->y_offset + pr->vis_height,
1387                                    pr->vis_width, pr->window_height - pr->vis_height - pr->y_offset,
1388                                    &rx, &ry, &rw, &rh))
1389                         {
1390                         gdk_window_clear_area(box->window, rx, ry, rw, rh);
1391                         pr_overlay_draw(pr, rx, ry, rw, rh, NULL);
1392                         }
1393                 }
1394 }
1395
1396 static void pr_border_clear(PixbufRenderer *pr)
1397 {
1398         pr_border_draw(pr, 0, 0, pr->window_width, pr->window_height);
1399 }
1400
1401 void pixbuf_renderer_set_color(PixbufRenderer *pr, GdkColor *color)
1402 {
1403         GtkStyle *style;
1404         GtkWidget *widget;
1405
1406         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1407
1408         widget = GTK_WIDGET(pr);
1409
1410         if (color) {
1411                 GdkColor *slot;
1412
1413                 style = gtk_style_copy(gtk_widget_get_style(widget));
1414                 slot = &style->bg[GTK_STATE_NORMAL];
1415
1416                 slot->red = color->red;
1417                 slot->green = color->green;
1418                 slot->blue = color->blue;
1419                 }
1420         else {
1421                 style = gtk_style_copy(gtk_widget_get_default_style());
1422         }
1423
1424         gtk_widget_set_style(widget, style);
1425
1426         if (GTK_WIDGET_VISIBLE(widget)) pr_border_clear(pr);
1427 }
1428
1429
1430 /*
1431  *-------------------------------------------------------------------
1432  * source tiles
1433  *-------------------------------------------------------------------
1434  */
1435
1436 static void pr_source_tile_free(SourceTile *st)
1437 {
1438         if (!st) return;
1439
1440         if (st->pixbuf) g_object_unref(st->pixbuf);
1441         g_free(st);
1442 }
1443
1444 static void pr_source_tile_free_all(PixbufRenderer *pr)
1445 {
1446         GList *work;
1447
1448         work = pr->source_tiles;
1449         while (work)
1450                 {
1451                 SourceTile *st;
1452
1453                 st = work->data;
1454                 work = work->next;
1455
1456                 pr_source_tile_free(st);
1457                 }
1458
1459         g_list_free(pr->source_tiles);
1460         pr->source_tiles = NULL;
1461 }
1462
1463 static void pr_source_tile_unset(PixbufRenderer *pr)
1464 {
1465         pr_source_tile_free_all(pr);
1466         pr->source_tiles_enabled = FALSE;
1467 }
1468
1469 static gboolean pr_source_tile_visible(PixbufRenderer *pr, SourceTile *st)
1470 {
1471         gint x1, y1, x2, y2;
1472
1473         if (!st) return FALSE;
1474
1475         x1 = ROUND_DOWN(pr->x_scroll, pr->tile_width);
1476         y1 = ROUND_DOWN(pr->y_scroll, pr->tile_height);
1477         x2 = ROUND_UP(pr->x_scroll + pr->vis_width, pr->tile_width);
1478         y2 = ROUND_UP(pr->y_scroll + pr->vis_height, pr->tile_height);
1479
1480         return !((gdouble)st->x * pr->scale > (gdouble)x2 ||
1481                  (gdouble)(st->x + pr->source_tile_width) * pr->scale < (gdouble)x1 ||
1482                  (gdouble)st->y * pr->scale > (gdouble)y2 ||
1483                  (gdouble)(st->y + pr->source_tile_height) * pr->scale < (gdouble)y1);
1484 }
1485
1486 static SourceTile *pr_source_tile_new(PixbufRenderer *pr, gint x, gint y)
1487 {
1488         SourceTile *st = NULL;
1489         gint count;
1490
1491         g_return_val_if_fail(pr->source_tile_width >= 1 && pr->source_tile_height >= 1, NULL);
1492
1493         if (pr->source_tiles_cache_size < 4) pr->source_tiles_cache_size = 4;
1494
1495         count = g_list_length(pr->source_tiles);
1496         if (count >= pr->source_tiles_cache_size)
1497                 {
1498                 GList *work;
1499
1500                 work = g_list_last(pr->source_tiles);
1501                 while (work && count >= pr->source_tiles_cache_size)
1502                         {
1503                         SourceTile *needle;
1504
1505                         needle = work->data;
1506                         work = work->prev;
1507
1508                         if (!pr_source_tile_visible(pr, needle))
1509                                 {
1510                                 pr->source_tiles = g_list_remove(pr->source_tiles, needle);
1511
1512                                 if (pr->func_tile_dispose)
1513                                         {
1514                                         pr->func_tile_dispose(pr, needle->x, needle->y,
1515                                                               pr->source_tile_width, pr->source_tile_height,
1516                                                               needle->pixbuf, pr->func_tile_data);
1517                                         }
1518
1519                                 if (!st)
1520                                         {
1521                                         st = needle;
1522                                         }
1523                                 else
1524                                         {
1525                                         pr_source_tile_free(needle);
1526                                         }
1527
1528                                 count--;
1529                                 }
1530                         }
1531                 }
1532
1533         if (!st)
1534                 {
1535                 st = g_new0(SourceTile, 1);
1536                 st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
1537                                             pr->source_tile_width, pr->source_tile_height);
1538                 }
1539
1540         st->x = ROUND_DOWN(x, pr->source_tile_width);
1541         st->y = ROUND_DOWN(y, pr->source_tile_height);
1542         st->blank = TRUE;
1543
1544         pr->source_tiles = g_list_prepend(pr->source_tiles, st);
1545
1546         return st;
1547 }
1548
1549 static SourceTile *pr_source_tile_request(PixbufRenderer *pr, gint x, gint y)
1550 {
1551         SourceTile *st;
1552
1553         st = pr_source_tile_new(pr, x, y);
1554         if (!st) return NULL;
1555
1556         if (pr->func_tile_request &&
1557             pr->func_tile_request(pr, st->x, st->y,
1558                                    pr->source_tile_width, pr->source_tile_height, st->pixbuf, pr->func_tile_data))
1559                 {
1560                 st->blank = FALSE;
1561                 }
1562
1563         pr_tile_invalidate_region(pr, st->x * pr->scale, st->y * pr->scale,
1564                                   pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1565
1566         return st;
1567 }
1568
1569 static SourceTile *pr_source_tile_find(PixbufRenderer *pr, gint x, gint y)
1570 {
1571         GList *work;
1572
1573         work = pr->source_tiles;
1574         while (work)
1575                 {
1576                 SourceTile *st = work->data;
1577
1578                 if (x >= st->x && x < st->x + pr->source_tile_width &&
1579                     y >= st->y && y < st->y + pr->source_tile_height)
1580                         {
1581                         if (work != pr->source_tiles)
1582                                 {
1583                                 pr->source_tiles = g_list_remove_link(pr->source_tiles, work);
1584                                 pr->source_tiles = g_list_concat(work, pr->source_tiles);
1585                                 }
1586                         return st;
1587                         }
1588
1589                 work = work->next;
1590                 }
1591
1592         return NULL;
1593 }
1594
1595 static GList *pr_source_tile_compute_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h, gboolean request)
1596 {
1597         gint x1, y1;
1598         GList *list = NULL;
1599         gint sx, sy;
1600
1601         if (x < 0) x = 0;
1602         if (y < 0) y = 0;
1603         if (w > pr->image_width) w = pr->image_width;
1604         if (h > pr->image_height) h = pr->image_height;
1605
1606         sx = ROUND_DOWN(x, pr->source_tile_width);
1607         sy = ROUND_DOWN(y, pr->source_tile_height);
1608
1609         for (x1 = sx; x1 < x + w; x1+= pr->source_tile_width)
1610                 {
1611                 for (y1 = sy; y1 < y + h; y1 += pr->source_tile_height)
1612                         {
1613                         SourceTile *st;
1614
1615                         st = pr_source_tile_find(pr, x1, y1);
1616                         if (!st && request) st = pr_source_tile_request(pr, x1, y1);
1617
1618                         if (st) list = g_list_prepend(list, st);
1619                         }
1620                 }
1621
1622         return g_list_reverse(list);
1623 }
1624
1625 static void pr_source_tile_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
1626 {
1627         GList *work;
1628
1629         if (width < 1 || height < 1) return;
1630
1631         work = pr->source_tiles;
1632         while (work)
1633                 {
1634                 SourceTile *st;
1635                 gint rx, ry, rw, rh;
1636
1637                 st = work->data;
1638                 work = work->next;
1639
1640                 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1641                                    x, y, width, height,
1642                                    &rx, &ry, &rw, &rh))
1643                         {
1644                         GdkPixbuf *pixbuf;
1645
1646                         pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh);
1647                         if (pr->func_tile_request &&
1648                             pr->func_tile_request(pr, rx, ry, rw, rh, pixbuf, pr->func_tile_data))
1649                                 {
1650                                 pr_tile_invalidate_region(pr, rx * pr->scale, ry * pr->scale,
1651                                                               rw * pr->scale, rh * pr->scale);
1652                                 }
1653                         g_object_unref(pixbuf);
1654                         }
1655                 }
1656 }
1657
1658 static gboolean pr_source_tile_render(PixbufRenderer *pr, ImageTile *it,
1659                                       gint x, gint y, gint w, gint h,
1660                                       gboolean new_data, gboolean fast)
1661 {
1662         GtkWidget *box;
1663         GList *list;
1664         GList *work;
1665         gboolean draw = FALSE;
1666
1667         box = GTK_WIDGET(pr);
1668
1669         if (pr->zoom == 1.0 || pr->scale == 1.0)
1670                 {
1671                 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1672                 work = list;
1673                 while (work)
1674                         {
1675                         SourceTile *st;
1676                         gint rx, ry, rw, rh;
1677
1678                         st = work->data;
1679                         work = work->next;
1680
1681                         if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1682                                            it->x + x, it->y + y, w, h,
1683                                            &rx, &ry, &rw, &rh))
1684                                 {
1685                                 if (st->blank)
1686                                         {
1687                                         gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1688                                                            rx - st->x, ry - st->y, rw, rh);
1689                                         }
1690                                 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1691                                         {
1692                                         gdk_draw_pixbuf(it->pixmap,
1693                                                         box->style->fg_gc[GTK_WIDGET_STATE(box)],
1694                                                         st->pixbuf,
1695                                                         rx - st->x, ry - st->y,
1696                                                         rx - it->x, ry - it->y,
1697                                                         rw, rh,
1698                                                         pr->dither_quality, rx, ry);
1699                                         }
1700                                 }
1701                         }
1702                 }
1703         else
1704                 {
1705                 gdouble scale_x, scale_y;
1706                 gint sx, sy, sw, sh;
1707
1708                 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1709                 scale_x = (gdouble)pr->width / pr->image_width;
1710                 scale_y = (gdouble)pr->height / pr->image_height;
1711
1712                 sx = (gdouble)(it->x + x) / scale_x;
1713                 sy = (gdouble)(it->y + y) / scale_y;
1714                 sw = (gdouble)w / scale_x;
1715                 sh = (gdouble)h / scale_y;
1716
1717                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1718
1719 #if 0
1720                 /* draws red over draw region, to check for leaks (regions not filled) */
1721                 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1722 #endif
1723
1724                 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1725                 work = list;
1726                 while (work)
1727                         {
1728                         SourceTile *st;
1729                         gint rx, ry, rw, rh;
1730                         gint stx, sty, stw, sth;
1731
1732                         st = work->data;
1733                         work = work->next;
1734
1735                         stx = floor((gdouble)st->x * scale_x);
1736                         sty = floor((gdouble)st->y * scale_y);
1737                         stw = ceil((gdouble)(st->x + pr->source_tile_width) * scale_x) - stx;
1738                         sth = ceil((gdouble)(st->y + pr->source_tile_height) * scale_y) - sty;
1739
1740                         if (pr_clip_region(stx, sty, stw, sth,
1741                                            it->x + x, it->y + y, w, h,
1742                                            &rx, &ry, &rw, &rh))
1743                                 {
1744                                 if (st->blank)
1745                                         {
1746                                         gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1747                                                            rx - st->x, ry - st->y, rw, rh);
1748                                         }
1749                                 else
1750                                         {
1751                                         gdouble offset_x;
1752                                         gdouble offset_y;
1753
1754                                         /* may need to use unfloored stx,sty values here */
1755                                         offset_x = (gdouble)(stx - it->x);
1756                                         offset_y = (gdouble)(sty - it->y);
1757
1758                                         gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1759                                                  (gdouble) 0.0 + offset_x,
1760                                                  (gdouble) 0.0 + offset_y,
1761                                                  scale_x, scale_y,
1762                                                  (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1763                                         draw = TRUE;
1764                                         }
1765                                 }
1766                         }
1767                 }
1768
1769         g_list_free(list);
1770
1771         return draw;
1772 }
1773
1774 void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height,
1775                                gint tile_width, gint tile_height, gint cache_size,
1776                                PixbufRendererTileRequestFunc func_request,
1777                                PixbufRendererTileDisposeFunc func_dispose,
1778                                gpointer user_data,
1779                                gdouble zoom)
1780 {
1781         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1782         g_return_if_fail(tile_width >= 32 && tile_width >= 32);
1783         g_return_if_fail(width >= 32 && height > 32);
1784         g_return_if_fail(func_request != NULL);
1785
1786         if (pr->pixbuf) g_object_unref(pr->pixbuf);
1787         pr->pixbuf = NULL;
1788
1789         pr_source_tile_unset(pr);
1790
1791         if (cache_size < 4) cache_size = 4;
1792
1793         pr->source_tiles_enabled = TRUE;
1794         pr->source_tiles_cache_size = cache_size;
1795         pr->source_tile_width = tile_width;
1796         pr->source_tile_height = tile_height;
1797
1798         pr->image_width = width;
1799         pr->image_height = height;
1800
1801         pr->func_tile_request = func_request;
1802         pr->func_tile_dispose = func_dispose;
1803         pr->func_tile_data = user_data;
1804
1805         pr_zoom_sync(pr, zoom, PR_ZOOM_FORCE | PR_ZOOM_NEW, 0, 0);
1806         pr_redraw(pr, TRUE);
1807 }
1808
1809 void pixbuf_renderer_set_tiles_size(PixbufRenderer *pr, gint width, gint height)
1810 {
1811         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1812         g_return_if_fail(width >= 32 && height > 32);
1813
1814         if (!pr->source_tiles_enabled) return;
1815         if (pr->image_width == width && pr->image_height == height) return;
1816
1817         pr->image_width = width;
1818         pr->image_height = height;
1819
1820         pr_zoom_sync(pr, pr->zoom, PR_ZOOM_FORCE, 0, 0);
1821 }
1822
1823 gint pixbuf_renderer_get_tiles(PixbufRenderer *pr)
1824 {
1825         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1826
1827         return pr->source_tiles_enabled;
1828 }
1829
1830 static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment,
1831                                 PrZoomFlags flags, gint x, gint y)
1832 {
1833         gdouble zoom = pr->zoom;
1834
1835         if (increment == 0.0) return;
1836
1837         if (zoom == 0.0)
1838                 {
1839                 if (pr->scale < 1.0)
1840                         {
1841                         zoom = 0.0 - 1.0 / pr->scale;
1842                         }
1843                 else
1844                         {
1845                         zoom = pr->scale;
1846                         }
1847                 }
1848
1849         if (increment < 0.0)
1850                 {
1851                 if (zoom >= 1.0 && zoom + increment < 1.0)
1852                         {
1853                         zoom = zoom + increment - 2.0;
1854                         }
1855                 else
1856                         {
1857                         zoom = zoom + increment;
1858                         }
1859                 }
1860         else
1861                 {
1862                 if (zoom <= -1.0 && zoom + increment > -1.0)
1863                         {
1864                         zoom = zoom + increment + 2.0;
1865                         }
1866                 else
1867                         {
1868                         zoom = zoom + increment;
1869                         }
1870                 }
1871
1872         pr_zoom_sync(pr, zoom, flags, x, y);
1873 }
1874
1875 /*
1876  *-------------------------------------------------------------------
1877  * display tiles
1878  *-------------------------------------------------------------------
1879  */
1880
1881 static ImageTile *pr_tile_new(gint x, gint y, gint width, gint height)
1882 {
1883         ImageTile *it;
1884
1885         it = g_new0(ImageTile, 1);
1886
1887         it->x = x;
1888         it->y = y;
1889         it->w = width;
1890         it->h = height;
1891
1892         it->render_done = TILE_RENDER_NONE;
1893
1894         return it;
1895 }
1896
1897 static void pr_tile_free(ImageTile *it)
1898 {
1899         if (!it) return;
1900
1901         if (it->pixbuf) g_object_unref(it->pixbuf);
1902         if (it->pixmap) g_object_unref(it->pixmap);
1903
1904         g_free(it);
1905 }
1906
1907 static void pr_tile_free_all(PixbufRenderer *pr)
1908 {
1909         GList *work;
1910
1911         work = pr->tiles;
1912         while (work)
1913                 {
1914                 ImageTile *it;
1915
1916                 it = work->data;
1917                 work = work->next;
1918
1919                 pr_tile_free(it);
1920                 }
1921
1922         g_list_free(pr->tiles);
1923         pr->tiles = NULL;
1924         pr->tile_cache_size = 0;
1925 }
1926
1927 static ImageTile *pr_tile_add(PixbufRenderer *pr, gint x, gint y)
1928 {
1929         ImageTile *it;
1930
1931         it = pr_tile_new(x, y, pr->tile_width, pr->tile_height);
1932
1933         if (it->x + it->w > pr->width) it->w = pr->width - it->x;
1934         if (it->y + it->h > pr->height) it->h = pr->height - it->y;
1935
1936         pr->tiles = g_list_prepend(pr->tiles, it);
1937         pr->tile_cache_size += it->size;
1938
1939         return it;
1940 }
1941
1942 static void pr_tile_remove(PixbufRenderer *pr, ImageTile *it)
1943 {
1944         if (it->qd)
1945                 {
1946                 QueueData *qd = it->qd;
1947
1948                 it->qd = NULL;
1949                 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
1950                 g_free(qd);
1951                 }
1952
1953         if (it->qd2)
1954                 {
1955                 QueueData *qd = it->qd2;
1956
1957                 it->qd2 = NULL;
1958                 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
1959                 g_free(qd);
1960                 }
1961
1962         pr->tiles = g_list_remove(pr->tiles, it);
1963         pr->tile_cache_size -= it->size;
1964
1965         pr_tile_free(it);
1966 }
1967
1968 static void pr_tile_free_space(PixbufRenderer *pr, guint space, ImageTile *it)
1969 {
1970         GList *work;
1971         guint tile_max;
1972
1973         work = g_list_last(pr->tiles);
1974
1975         if (pr->source_tiles_enabled && pr->scale < 1.0)
1976                 {
1977                 gint tiles;
1978
1979                 tiles = (pr->vis_width / pr->tile_width + 1) * (pr->vis_height / pr->tile_height + 1);
1980                 tile_max = MAX(tiles * pr->tile_width * pr->tile_height * 3,
1981                                (gint)((gdouble)pr->tile_cache_max * 1048576.0 * pr->scale));
1982                 }
1983         else
1984                 {
1985                 tile_max = pr->tile_cache_max * 1048576;
1986                 }
1987
1988         while (work && pr->tile_cache_size + space > tile_max)
1989                 {
1990                 ImageTile *needle;
1991
1992                 needle = work->data;
1993                 work = work->prev;
1994                 if (needle != it &&
1995                     ((!needle->qd && !needle->qd2) || !pr_tile_is_visible(pr, needle))) pr_tile_remove(pr, needle);
1996                 }
1997 }
1998
1999 static void pr_tile_invalidate_all(PixbufRenderer *pr)
2000 {
2001         GList *work;
2002
2003         work = pr->tiles;
2004         while (work)
2005                 {
2006                 ImageTile *it;
2007
2008                 it = work->data;
2009                 work = work->next;
2010
2011                 it->render_done = TILE_RENDER_NONE;
2012                 it->render_todo = TILE_RENDER_ALL;
2013                 it->blank = FALSE;
2014
2015                 it->w = MIN(pr->tile_width, pr->width - it->x);
2016                 it->h = MIN(pr->tile_height, pr->height - it->y);
2017                 }
2018 }
2019
2020 static void pr_tile_invalidate_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h)
2021 {
2022         gint x1, x2;
2023         gint y1, y2;
2024         GList *work;
2025
2026         x1 = ROUND_DOWN(x, pr->tile_width);
2027         x2 = ROUND_UP(x + w, pr->tile_width);
2028
2029         y1 = ROUND_DOWN(y, pr->tile_height);
2030         y2 = ROUND_UP(y + h, pr->tile_height);
2031
2032         work = pr->tiles;
2033         while (work)
2034                 {
2035                 ImageTile *it;
2036
2037                 it = work->data;
2038                 work = work->next;
2039
2040                 if (it->x < x2 && it->x + it->w > x1 &&
2041                     it->y < y2 && it->y + it->h > y1)
2042                         {
2043                         it->render_done = TILE_RENDER_NONE;
2044                         it->render_todo = TILE_RENDER_ALL;
2045                         }
2046                 }
2047 }
2048
2049 static ImageTile *pr_tile_get(PixbufRenderer *pr, gint x, gint y, gboolean only_existing)
2050 {
2051         GList *work;
2052
2053         work = pr->tiles;
2054         while (work)
2055                 {
2056                 ImageTile *it;
2057
2058                 it = work->data;
2059                 if (it->x == x && it->y == y)
2060                         {
2061                         pr->tiles = g_list_delete_link(pr->tiles, work);
2062                         pr->tiles = g_list_prepend(pr->tiles, it);
2063                         return it;
2064                         }
2065
2066                 work = work->next;
2067                 }
2068
2069         if (only_existing) return NULL;
2070
2071         return pr_tile_add(pr, x, y);
2072 }
2073
2074 static void pr_tile_prepare(PixbufRenderer *pr, ImageTile *it)
2075 {
2076         if (!it->pixmap)
2077                 {
2078                 GdkPixmap *pixmap;
2079                 guint size;
2080
2081                 pixmap = gdk_pixmap_new(((GtkWidget *)pr)->window, pr->tile_width, pr->tile_height, -1);
2082
2083                 size = pixmap_calc_size(pixmap);
2084                 pr_tile_free_space(pr, size, it);
2085
2086                 it->pixmap = pixmap;
2087                 it->size += size;
2088                 pr->tile_cache_size += size;
2089                 }
2090
2091         if ((pr->zoom != 1.0 || pr->source_tiles_enabled || (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf)) ||
2092              pr->orientation != EXIF_ORIENTATION_TOP_LEFT || pr->func_post_process) && !it->pixbuf)
2093                 {
2094                 GdkPixbuf *pixbuf;
2095                 guint size;
2096 #if 0
2097 /* I don't think that we need a pixbuf with alpha channel here */
2098                 if (pr->pixbuf)
2099                         {
2100                         pixbuf = gdk_pixbuf_new(gdk_pixbuf_get_colorspace(pr->pixbuf),
2101                                                 gdk_pixbuf_get_has_alpha(pr->pixbuf),
2102                                                 gdk_pixbuf_get_bits_per_sample(pr->pixbuf),
2103                                                 pr->tile_width, pr->tile_height);
2104                         }
2105                 else
2106 #endif
2107                         {
2108                         pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
2109                         }
2110
2111                 size = gdk_pixbuf_get_rowstride(pixbuf) * pr->tile_height;
2112                 pr_tile_free_space(pr, size, it);
2113
2114                 it->pixbuf = pixbuf;
2115                 it->size += size;
2116                 pr->tile_cache_size += size;
2117                 }
2118 }
2119
2120 /*
2121  *-------------------------------------------------------------------
2122  * drawing
2123  *-------------------------------------------------------------------
2124  */
2125
2126
2127 static void pr_tile_coords_map_orientation(PixbufRenderer *pr,
2128                                      gdouble tile_x, gdouble tile_y, /* coordinates of the tile */
2129                                      gdouble image_w, gdouble image_h,
2130                                      gdouble tile_w, gdouble tile_h,
2131                                      gdouble *res_x, gdouble *res_y)
2132 {
2133         *res_x = tile_x;
2134         *res_y = tile_y;
2135         switch (pr->orientation)
2136                 {
2137                 case EXIF_ORIENTATION_TOP_LEFT:
2138                         /* normal -- nothing to do */
2139                         break;
2140                 case EXIF_ORIENTATION_TOP_RIGHT:
2141                         /* mirrored */
2142                         *res_x = image_w - tile_x - tile_w;
2143                         break;
2144                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2145                         /* upside down */
2146                         *res_x = image_w - tile_x - tile_w;
2147                         *res_y = image_h - tile_y - tile_h;
2148                         break;
2149                 case EXIF_ORIENTATION_BOTTOM_LEFT:
2150                         /* flipped */
2151                         *res_y = image_h - tile_y - tile_h;
2152                         break;
2153                 case EXIF_ORIENTATION_LEFT_TOP:
2154                         *res_x = tile_y;
2155                         *res_y = tile_x;
2156                         break;
2157                 case EXIF_ORIENTATION_RIGHT_TOP:
2158                         /* rotated -90 (270) */
2159                         *res_x = tile_y;
2160                         *res_y = image_w - tile_x - tile_w;
2161                         break;
2162                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2163                         *res_x = image_h - tile_y - tile_h;
2164                         *res_y = image_w - tile_x - tile_w;
2165                         break;
2166                 case EXIF_ORIENTATION_LEFT_BOTTOM:
2167                         /* rotated 90 */
2168                         *res_x = image_h - tile_y - tile_h;
2169                         *res_y = tile_x;
2170                         break;
2171                 default:
2172                         /* The other values are out of range */
2173                         break;
2174                 }
2175 //      log_printf("tile coord y:%f, ih:%d, th:%f ry:%f\n", tile_y, image_h, tile_h, *res_x);
2176 }
2177
2178 static void pr_tile_region_map_orientation(PixbufRenderer *pr,
2179                                      gint area_x, gint area_y, /* coordinates of the area inside tile */
2180                                      gint tile_w, gint tile_h,
2181                                      gint area_w, gint area_h,
2182                                      gint *res_x, gint *res_y,
2183                                      gint *res_w, gint *res_h)
2184 {
2185         *res_x = area_x;
2186         *res_y = area_y;
2187         *res_w = area_w;
2188         *res_h = area_h;
2189
2190         switch (pr->orientation)
2191                 {
2192                 case EXIF_ORIENTATION_TOP_LEFT:
2193                         /* normal -- nothing to do */
2194                         break;
2195                 case EXIF_ORIENTATION_TOP_RIGHT:
2196                         /* mirrored */
2197                         *res_x = tile_w - area_x - area_w;
2198                         break;
2199                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2200                         /* upside down */
2201                         *res_x = tile_w - area_x - area_w;
2202                         *res_y = tile_h - area_y - area_h;
2203                         break;
2204                 case EXIF_ORIENTATION_BOTTOM_LEFT:
2205                         /* flipped */
2206                         *res_y = tile_h - area_y - area_h;
2207                         break;
2208                 case EXIF_ORIENTATION_LEFT_TOP:
2209                         *res_x = area_y;
2210                         *res_y = area_x;
2211                         *res_w = area_h;
2212                         *res_h = area_w;
2213                         break;
2214                 case EXIF_ORIENTATION_RIGHT_TOP:
2215                         /* rotated -90 (270) */
2216                         *res_x = area_y;
2217                         *res_y = tile_w - area_x - area_w;
2218                         *res_w = area_h;
2219                         *res_h = area_w;
2220                         break;
2221                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2222                         *res_x = tile_h - area_y - area_h;
2223                         *res_y = tile_w - area_x - area_w;
2224                         *res_w = area_h;
2225                         *res_h = area_w;
2226                         break;
2227                 case EXIF_ORIENTATION_LEFT_BOTTOM:
2228                         /* rotated 90 */
2229                         *res_x = tile_h - area_y - area_h;
2230                         *res_y = area_x;
2231                         *res_w = area_h;
2232                         *res_h = area_w;
2233                         break;
2234                 default:
2235                         /* The other values are out of range */
2236                         break;
2237                 }
2238 //      log_printf("inside y:%d, th:%d, ah:%d ry:%d\n", area_y, tile_h, area_h, *res_x);
2239 }
2240
2241 static void pr_coords_map_orientation_reverse(PixbufRenderer *pr,
2242                                      gint area_x, gint area_y,
2243                                      gint tile_w, gint tile_h,
2244                                      gint area_w, gint area_h,
2245                                      gint *res_x, gint *res_y,
2246                                      gint *res_w, gint *res_h)
2247 {
2248         *res_x = area_x;
2249         *res_y = area_y;
2250         *res_w = area_w;
2251         *res_h = area_h;
2252
2253         switch (pr->orientation)
2254                 {
2255                 case EXIF_ORIENTATION_TOP_LEFT:
2256                         /* normal -- nothing to do */
2257                         break;
2258                 case EXIF_ORIENTATION_TOP_RIGHT:
2259                         /* mirrored */
2260                         *res_x = tile_w - area_x - area_w;
2261                         break;
2262                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2263                         /* upside down */
2264                         *res_x = tile_w - area_x - area_w;
2265                         *res_y = tile_h - area_y - area_h;
2266                         break;
2267                 case EXIF_ORIENTATION_BOTTOM_LEFT:
2268                         /* flipped */
2269                         *res_y = tile_h - area_y - area_h;
2270                         break;
2271                 case EXIF_ORIENTATION_LEFT_TOP:
2272                         *res_x = area_y;
2273                         *res_y = area_x;
2274                         *res_w = area_h;
2275                         *res_h = area_w;
2276                         break;
2277                 case EXIF_ORIENTATION_RIGHT_TOP:
2278                         /* rotated -90 (270) */
2279                         *res_x = tile_w - area_y - area_h;
2280                         *res_y = area_x;
2281                         *res_w = area_h;
2282                         *res_h = area_w;
2283                         break;
2284                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2285                         *res_x = tile_w - area_y - area_h;
2286                         *res_y = tile_h - area_x - area_w;
2287                         *res_w = area_h;
2288                         *res_h = area_w;
2289                         break;
2290                 case EXIF_ORIENTATION_LEFT_BOTTOM:
2291                         /* rotated 90 */
2292                         *res_x = area_y;
2293                         *res_y = tile_h - area_x - area_w;
2294                         *res_w = area_h;
2295                         *res_h = area_w;
2296                         break;
2297                 default:
2298                         /* The other values are out of range */
2299                         break;
2300                 }
2301 }
2302
2303
2304 static GdkPixbuf *pr_get_spare_tile(PixbufRenderer *pr)
2305 {
2306         if (!pr->spare_tile) pr->spare_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, pr->tile_width, pr->tile_height);
2307         return pr->spare_tile;
2308 }
2309
2310 #define COLOR_BYTES 3   /* rgb */
2311
2312 static void pr_tile_rotate_90_clockwise(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2313 {
2314         GdkPixbuf *src = *tile;
2315         GdkPixbuf *dest;
2316         gint srs, drs;
2317         guchar *s_pix, *d_pix;
2318         guchar *sp, *dp;
2319         guchar *ip, *spi, *dpi;
2320         gint i, j;
2321         gint tw = pr->tile_width;
2322
2323         srs = gdk_pixbuf_get_rowstride(src);
2324         s_pix = gdk_pixbuf_get_pixels(src);
2325         spi = s_pix + (x * COLOR_BYTES);
2326
2327         dest = pr_get_spare_tile(pr);
2328         drs = gdk_pixbuf_get_rowstride(dest);
2329         d_pix = gdk_pixbuf_get_pixels(dest);
2330         dpi = d_pix + (tw - 1) * COLOR_BYTES;
2331
2332         for (i = y; i < y + h; i++)
2333                 {
2334                 sp = spi + (i * srs);
2335                 ip = dpi - (i * COLOR_BYTES);
2336                 for (j = x; j < x + w; j++)
2337                         {
2338                         dp = ip + (j * drs);
2339                         memcpy(dp, sp, COLOR_BYTES);
2340                         sp += COLOR_BYTES;
2341                         }
2342                 }
2343
2344         pr->spare_tile = src;
2345         *tile = dest;
2346 }
2347
2348 static void pr_tile_rotate_90_counter_clockwise(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2349 {
2350         GdkPixbuf *src = *tile;
2351         GdkPixbuf *dest;
2352         gint srs, drs;
2353         guchar *s_pix, *d_pix;
2354         guchar *sp, *dp;
2355         guchar *ip, *spi, *dpi;
2356         gint i, j;
2357         gint th = pr->tile_height;
2358
2359         srs = gdk_pixbuf_get_rowstride(src);
2360         s_pix = gdk_pixbuf_get_pixels(src);
2361         spi = s_pix + (x * COLOR_BYTES);
2362
2363         dest = pr_get_spare_tile(pr);
2364         drs = gdk_pixbuf_get_rowstride(dest);
2365         d_pix = gdk_pixbuf_get_pixels(dest);
2366         dpi = d_pix + (th - 1) * drs;
2367
2368         for (i = y; i < y + h; i++)
2369                 {
2370                 sp = spi + (i * srs);
2371                 ip = dpi + (i * COLOR_BYTES);
2372                 for (j = x; j < x + w; j++)
2373                         {
2374                         dp = ip - (j * drs);
2375                         memcpy(dp, sp, COLOR_BYTES);
2376                         sp += COLOR_BYTES;
2377                         }
2378                 }
2379
2380         pr->spare_tile = src;
2381         *tile = dest;
2382 }
2383
2384 static void pr_tile_mirror_only(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2385 {
2386         GdkPixbuf *src = *tile;
2387         GdkPixbuf *dest;
2388         gint srs, drs;
2389         guchar *s_pix, *d_pix;
2390         guchar *sp, *dp;
2391         guchar *spi, *dpi;
2392         gint i, j;
2393
2394         gint tw = pr->tile_width;
2395
2396         srs = gdk_pixbuf_get_rowstride(src);
2397         s_pix = gdk_pixbuf_get_pixels(src);
2398         spi = s_pix + (x * COLOR_BYTES);
2399
2400         dest = pr_get_spare_tile(pr);
2401         drs = gdk_pixbuf_get_rowstride(dest);
2402         d_pix = gdk_pixbuf_get_pixels(dest);
2403         dpi =  d_pix + (tw - x - 1) * COLOR_BYTES;
2404
2405         for (i = y; i < y + h; i++)
2406                 {
2407                 sp = spi + (i * srs);
2408                 dp = dpi + (i * drs);
2409                 for (j = 0; j < w; j++)
2410                         {
2411                         memcpy(dp, sp, COLOR_BYTES);
2412                         sp += COLOR_BYTES;
2413                         dp -= COLOR_BYTES;
2414                         }
2415                 }
2416
2417         pr->spare_tile = src;
2418         *tile = dest;
2419 }
2420
2421 static void pr_tile_mirror_and_flip(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2422 {
2423         GdkPixbuf *src = *tile;
2424         GdkPixbuf *dest;
2425         gint srs, drs;
2426         guchar *s_pix, *d_pix;
2427         guchar *sp, *dp;
2428         guchar *spi, *dpi;
2429         gint i, j;
2430         gint tw = pr->tile_width;
2431         gint th = pr->tile_height;
2432
2433         srs = gdk_pixbuf_get_rowstride(src);
2434         s_pix = gdk_pixbuf_get_pixels(src);
2435         spi = s_pix + (x * COLOR_BYTES);
2436
2437         dest = pr_get_spare_tile(pr);
2438         drs = gdk_pixbuf_get_rowstride(dest);
2439         d_pix = gdk_pixbuf_get_pixels(dest);
2440         dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
2441
2442         for (i = y; i < y + h; i++)
2443                 {
2444                 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
2445                 dp = dpi - (i * drs) - (x * COLOR_BYTES);
2446                 for (j = 0; j < w; j++)
2447                         {
2448                         memcpy(dp, sp, COLOR_BYTES);
2449                         sp += COLOR_BYTES;
2450                         dp -= COLOR_BYTES;
2451                         }
2452                 }
2453
2454         pr->spare_tile = src;
2455         *tile = dest;
2456 }
2457
2458 static void pr_tile_flip_only(PixbufRenderer *pr, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
2459 {
2460         GdkPixbuf *src = *tile;
2461         GdkPixbuf *dest;
2462         gint srs, drs;
2463         guchar *s_pix, *d_pix;
2464         guchar *sp, *dp;
2465         guchar *spi, *dpi;
2466         gint i;
2467         gint th = pr->tile_height;
2468
2469         srs = gdk_pixbuf_get_rowstride(src);
2470         s_pix = gdk_pixbuf_get_pixels(src);
2471         spi = s_pix + (x * COLOR_BYTES);
2472
2473         dest = pr_get_spare_tile(pr);
2474         drs = gdk_pixbuf_get_rowstride(dest);
2475         d_pix = gdk_pixbuf_get_pixels(dest);
2476         dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
2477
2478         for (i = y; i < y + h; i++)
2479                 {
2480                 sp = spi + (i * srs);
2481                 dp = dpi - (i * drs);
2482                 memcpy(dp, sp, w * COLOR_BYTES);
2483                 }
2484
2485         pr->spare_tile = src;
2486         *tile = dest;
2487 }
2488
2489 static void pr_tile_apply_orientation(PixbufRenderer *pr, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
2490 {
2491         switch (pr->orientation)
2492                 {
2493                 case EXIF_ORIENTATION_TOP_LEFT:
2494                         /* normal -- nothing to do */
2495                         break;
2496                 case EXIF_ORIENTATION_TOP_RIGHT:
2497                         /* mirrored */
2498                         {
2499                                 pr_tile_mirror_only(pr, pixbuf, x, y, w, h);
2500                         }
2501                         break;
2502                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
2503                         /* upside down */
2504                         {
2505                                 pr_tile_mirror_and_flip(pr, pixbuf, x, y, w, h);
2506                         }
2507                         break;
2508                 case EXIF_ORIENTATION_BOTTOM_LEFT:
2509                         /* flipped */
2510                         {
2511                                 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2512                         }
2513                         break;
2514                 case EXIF_ORIENTATION_LEFT_TOP:
2515                         {
2516                                 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2517                                 pr_tile_rotate_90_clockwise(pr, pixbuf, x, pr->tile_height - y - h, w, h);
2518                         }
2519                         break;
2520                 case EXIF_ORIENTATION_RIGHT_TOP:
2521                         /* rotated -90 (270) */
2522                         {
2523                                 pr_tile_rotate_90_clockwise(pr, pixbuf, x, y, w, h);
2524                         }
2525                         break;
2526                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2527                         {
2528                                 pr_tile_flip_only(pr, pixbuf, x, y, w, h);
2529                                 pr_tile_rotate_90_counter_clockwise(pr, pixbuf, x, pr->tile_height - y - h, w, h);
2530                         }
2531                         break;
2532                 case EXIF_ORIENTATION_LEFT_BOTTOM:
2533                         /* rotated 90 */
2534                         {
2535                                 pr_tile_rotate_90_counter_clockwise(pr, pixbuf, x, y, w, h);
2536                         }
2537                         break;
2538                 default:
2539                         /* The other values are out of range */
2540                         break;
2541                 }
2542 }
2543
2544
2545 static void pr_tile_render(PixbufRenderer *pr, ImageTile *it,
2546                            gint x, gint y, gint w, gint h,
2547                            gboolean new_data, gboolean fast)
2548 {
2549         GtkWidget *box;
2550         gboolean has_alpha;
2551         gboolean draw = FALSE;
2552
2553         if (it->render_todo == TILE_RENDER_NONE && it->pixmap && !new_data) return;
2554
2555         if (it->render_done != TILE_RENDER_ALL)
2556                 {
2557                 x = 0;
2558                 y = 0;
2559                 w = it->w;
2560                 h = it->h;
2561                 if (!fast) it->render_done = TILE_RENDER_ALL;
2562                 }
2563         else if (it->render_todo != TILE_RENDER_AREA)
2564                 {
2565                 if (!fast) it->render_todo = TILE_RENDER_NONE;
2566                 return;
2567                 }
2568
2569         if (!fast) it->render_todo = TILE_RENDER_NONE;
2570
2571         if (new_data) it->blank = FALSE;
2572
2573         pr_tile_prepare(pr, it);
2574         has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
2575
2576         box = GTK_WIDGET(pr);
2577
2578         /* FIXME checker colors for alpha should be configurable,
2579          * also should be drawn for blank = TRUE
2580          */
2581
2582         if (it->blank)
2583                 {
2584                 /* no data, do fast rect fill */
2585                 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
2586                                    0, 0, it->w, it->h);
2587                 }
2588         else if (pr->source_tiles_enabled)
2589                 {
2590                 draw = pr_source_tile_render(pr, it, x, y, w, h, new_data, fast);
2591                 }
2592         else if (pr->zoom == 1.0 || pr->scale == 1.0)
2593                 {
2594
2595                 gdouble src_x, src_y;
2596                 gint pb_x, pb_y;
2597                 gint pb_w, pb_h;
2598                 pr_tile_coords_map_orientation(pr, it->x, it->y,
2599                                             pr->image_width, pr->image_height,
2600                                             pr->tile_width, pr->tile_height,
2601                                             &src_x, &src_y);
2602                 pr_tile_region_map_orientation(pr, x, y,
2603                                             pr->tile_width, pr->tile_height,
2604                                             w, h,
2605                                             &pb_x, &pb_y,
2606                                             &pb_w, &pb_h);
2607
2608                 if (has_alpha)
2609                         {
2610                         gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2611                                          (gdouble) 0.0 - src_x,
2612                                          (gdouble) 0.0 - src_y,
2613                                          1.0, 1.0, GDK_INTERP_NEAREST,
2614                                          255, it->x + pb_x, it->y + pb_y,
2615                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2616                         pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2617                         draw = TRUE;
2618                         }
2619                 else
2620                         {
2621
2622
2623                         if (pr->orientation == EXIF_ORIENTATION_TOP_LEFT && !(pr->func_post_process && !(pr->post_process_slow && fast)))
2624                                 {
2625                                 /* faster, simple, base orientation, no postprocessing */
2626                                 gdk_draw_pixbuf(it->pixmap,
2627                                                 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2628                                                 pr->pixbuf,
2629                                                 it->x + x, it->y + y,
2630                                                 x, y,
2631                                                 w, h,
2632                                                 pr->dither_quality, it->x + x, it->y + y);
2633                                 }
2634                         else
2635                                 {
2636                                 gdk_pixbuf_copy_area(pr->pixbuf,
2637                                                      src_x + pb_x, src_y + pb_y,
2638                                                      pb_w, pb_h,
2639                                                      it->pixbuf,
2640                                                      pb_x, pb_y);
2641                                 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2642                                 draw = TRUE;
2643                                 }
2644                         }
2645                 }
2646         else
2647                 {
2648                 gdouble scale_x, scale_y;
2649                 gdouble src_x, src_y;
2650                 gint pb_x, pb_y;
2651                 gint pb_w, pb_h;
2652
2653                 if (pr->image_width == 0 || pr->image_height == 0) return;
2654
2655                 scale_x = (gdouble)pr->width / pr->image_width;
2656                 scale_y = (gdouble)pr->height / pr->image_height;
2657
2658                 pr_tile_coords_map_orientation(pr, it->x, it->y,
2659                                             pr->image_width * scale_x, pr->image_height * scale_y,
2660                                             pr->tile_width, pr->tile_height,
2661                                             &src_x, &src_y);
2662                 pr_tile_region_map_orientation(pr, x, y,
2663                                             pr->tile_width, pr->tile_height,
2664                                             w, h,
2665                                             &pb_x, &pb_y,
2666                                             &pb_w, &pb_h);
2667                 switch (pr->orientation)
2668                         {
2669                         gdouble tmp;
2670                         case EXIF_ORIENTATION_LEFT_TOP:
2671                         case EXIF_ORIENTATION_RIGHT_TOP:
2672                         case EXIF_ORIENTATION_RIGHT_BOTTOM:
2673                         case EXIF_ORIENTATION_LEFT_BOTTOM:
2674                                 tmp = scale_x;
2675                                 scale_x = scale_y;
2676                                 scale_y = tmp;
2677                                 break;
2678                         default:
2679                                 /* nothing to do */
2680                                 break;
2681                         }
2682
2683                 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
2684                  * small sizes for anything but GDK_INTERP_NEAREST
2685                  */
2686                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
2687
2688                 if (!has_alpha)
2689                         {
2690                         gdk_pixbuf_scale(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2691                                          (gdouble) 0.0 - src_x,
2692                                          (gdouble) 0.0 - src_y,
2693                                          scale_x, scale_y,
2694                                          (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
2695                         }
2696                 else
2697                         {
2698                         gdk_pixbuf_composite_color(pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
2699                                          (gdouble) 0.0 - src_x,
2700                                          (gdouble) 0.0 - src_y,
2701                                          scale_x, scale_y,
2702                                          (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
2703                                          255, it->x + pb_x, it->y + pb_y,
2704                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
2705                         }
2706                 pr_tile_apply_orientation(pr, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
2707                 draw = TRUE;
2708                 }
2709
2710         if (draw && it->pixbuf && !it->blank)
2711                 {
2712
2713                 if (pr->func_post_process && !(pr->post_process_slow && fast))
2714                         pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
2715
2716                 gdk_draw_pixbuf(it->pixmap,
2717                                 box->style->fg_gc[GTK_WIDGET_STATE(box)],
2718                                 it->pixbuf,
2719                                 x, y,
2720                                 x, y,
2721                                 w, h,
2722                                 pr->dither_quality, it->x + x, it->y + y);
2723                 }
2724
2725 #if 0
2726         /* enable this line for debugging the edges of tiles */
2727         gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2728                            FALSE, 0, 0, it->w, it->h);
2729         gdk_draw_rectangle(it->pixmap, box->style->white_gc,
2730                            FALSE, x, y, w, h);
2731 #endif
2732 }
2733
2734
2735 static void pr_tile_expose(PixbufRenderer *pr, ImageTile *it,
2736                            gint x, gint y, gint w, gint h,
2737                            gboolean new_data, gboolean fast)
2738 {
2739         GtkWidget *box;
2740
2741         pr_tile_render(pr, it, x, y, w, h, new_data, fast);
2742
2743         box = GTK_WIDGET(pr);
2744
2745         gdk_draw_drawable(box->window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
2746                           it->pixmap, x, y,
2747                           pr->x_offset + (it->x - pr->x_scroll) + x, pr->y_offset + (it->y - pr->y_scroll) + y, w, h);
2748
2749         if (pr->overlay_list)
2750                 {
2751                 pr_overlay_draw(pr, pr->x_offset + (it->x - pr->x_scroll) + x,
2752                                 pr->y_offset + (it->y - pr->y_scroll) + y,
2753                                 w, h,
2754                                 it);
2755                 }
2756 }
2757
2758
2759 static gboolean pr_tile_is_visible(PixbufRenderer *pr, ImageTile *it)
2760 {
2761         return (it->x + it->w >= pr->x_scroll && it->x < pr->x_scroll + pr->vis_width &&
2762                 it->y + it->h >= pr->y_scroll && it->y < pr->y_scroll + pr->vis_height);
2763 }
2764
2765 /*
2766  *-------------------------------------------------------------------
2767  * draw queue
2768  *-------------------------------------------------------------------
2769  */
2770
2771 static gint pr_get_queued_area(GList *work)
2772 {
2773         gint area = 0;
2774         
2775         while (work) 
2776                 {
2777                 QueueData *qd = work->data;
2778                 area += qd->w * qd->h;
2779                 work = work->next;
2780                 }
2781         return area;
2782 }
2783
2784
2785 static gboolean pr_queue_schedule_next_draw(PixbufRenderer *pr, gboolean force_set)
2786 {
2787         gfloat percent;
2788         gint visible_area = pr->vis_width * pr->vis_height;
2789         
2790         if (!pr->loading)
2791                 {
2792                 /* 2pass prio */ 
2793                 DEBUG_2("redraw priority: 2pass");
2794                 pr->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, pr_queue_draw_idle_cb, pr, NULL);
2795                 return FALSE;
2796                 }
2797         
2798         if (visible_area == 0)
2799                 {
2800                 /* not known yet */
2801                 percent = 100.0;
2802                 }
2803         else
2804                 {
2805                 percent = 100.0 * pr_get_queued_area(pr->draw_queue) / visible_area;
2806                 }
2807         
2808         if (percent > 10.0)
2809                 {
2810                 /* we have enough data for starting intensive redrawing */
2811                 DEBUG_2("redraw priority: high %.2f %%", percent);
2812                 pr->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, pr_queue_draw_idle_cb, pr, NULL);
2813                 return FALSE;
2814                 }
2815         
2816         if (percent < 1.0 || force_set)
2817                 {
2818                 /* queue is (almost) empty, wait  50 ms*/
2819                 DEBUG_2("redraw priority: wait %.2f %%", percent);
2820                 pr->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, pr_queue_draw_idle_cb, pr, NULL);
2821                 return FALSE;
2822                 }
2823         
2824         /* keep the same priority as before */
2825         DEBUG_2("redraw priority: no change %.2f %%", percent);
2826         return TRUE;
2827 }
2828                 
2829
2830 static gboolean pr_queue_draw_idle_cb(gpointer data)
2831 {
2832         PixbufRenderer *pr = data;
2833         QueueData *qd;
2834         gboolean fast;
2835
2836
2837         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2838             (!pr->draw_queue && !pr->draw_queue_2pass) ||
2839             !pr->draw_idle_id)
2840                 {
2841                 pr_render_complete_signal(pr);
2842
2843                 pr->draw_idle_id = 0;
2844                 return FALSE;
2845                 }
2846
2847         if (pr->draw_queue)
2848                 {
2849                 qd = pr->draw_queue->data;
2850                 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
2851                 }
2852         else
2853                 {
2854                 if (pr->loading)
2855                         {
2856                         /* still loading, wait till done (also drops the higher priority) */
2857
2858                         return pr_queue_schedule_next_draw(pr, FALSE);
2859                         }
2860
2861                 qd = pr->draw_queue_2pass->data;
2862                 fast = FALSE;
2863                 }
2864
2865         if (GTK_WIDGET_REALIZED(pr))
2866                 {
2867                 if (pr_tile_is_visible(pr, qd->it))
2868                         {
2869                         pr_tile_expose(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2870                         }
2871                 else if (qd->new_data)
2872                         {
2873                         /* if new pixel data, and we already have a pixmap, update the tile */
2874                         qd->it->blank = FALSE;
2875                         if (qd->it->pixmap && qd->it->render_done == TILE_RENDER_ALL)
2876                                 {
2877                                 pr_tile_render(pr, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
2878                                 }
2879                         }
2880                 }
2881
2882         if (pr->draw_queue)
2883                 {
2884                 qd->it->qd = NULL;
2885                 pr->draw_queue = g_list_remove(pr->draw_queue, qd);
2886                 if (fast)
2887                         {
2888                         if (qd->it->qd2)
2889                                 {
2890                                 pr_queue_merge(qd->it->qd2, qd);
2891                                 g_free(qd);
2892                                 }
2893                         else
2894                                 {
2895                                 qd->it->qd2 = qd;
2896                                 pr->draw_queue_2pass = g_list_append(pr->draw_queue_2pass, qd);
2897                                 }
2898                         }
2899                 else
2900                         {
2901                         g_free(qd);
2902                         }
2903                 }
2904         else
2905                 {
2906                 qd->it->qd2 = NULL;
2907                 pr->draw_queue_2pass = g_list_remove(pr->draw_queue_2pass, qd);
2908                 g_free(qd);
2909                 }
2910
2911         if (!pr->draw_queue && !pr->draw_queue_2pass)
2912                 {
2913                 pr_render_complete_signal(pr);
2914
2915                 pr->draw_idle_id = 0;
2916                 return FALSE;
2917                 }
2918
2919                 return pr_queue_schedule_next_draw(pr, FALSE);
2920 }
2921
2922 static void pr_queue_list_free(GList *list)
2923 {
2924         GList *work;
2925
2926         work = list;
2927         while (work)
2928                 {
2929                 QueueData *qd;
2930
2931                 qd = work->data;
2932                 work = work->next;
2933
2934                 qd->it->qd = NULL;
2935                 qd->it->qd2 = NULL;
2936                 g_free(qd);
2937                 }
2938
2939         g_list_free(list);
2940 }
2941
2942 static void pr_queue_clear(PixbufRenderer *pr)
2943 {
2944         pr_queue_list_free(pr->draw_queue);
2945         pr->draw_queue = NULL;
2946
2947         pr_queue_list_free(pr->draw_queue_2pass);
2948         pr->draw_queue_2pass = NULL;
2949
2950         if (pr->draw_idle_id)
2951                 {
2952                 g_source_remove(pr->draw_idle_id);
2953                 pr->draw_idle_id = 0;
2954                 }
2955 }
2956
2957 static void pr_queue_merge(QueueData *parent, QueueData *qd)
2958 {
2959         if (parent->x + parent->w < qd->x + qd->w)
2960                 {
2961                 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
2962                 }
2963         if (parent->x > qd->x)
2964                 {
2965                 parent->w += parent->x - qd->x;
2966                 parent->x = qd->x;
2967                 }
2968
2969         if (parent->y + parent->h < qd->y + qd->h)
2970                 {
2971                 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
2972                 }
2973         if (parent->y > qd->y)
2974                 {
2975                 parent->h += parent->y - qd->y;
2976                 parent->y = qd->y;
2977                 }
2978
2979         parent->new_data |= qd->new_data;
2980 }
2981
2982 static gboolean pr_clamp_to_visible(PixbufRenderer *pr, gint *x, gint *y, gint *w, gint *h)
2983 {
2984         gint nx, ny;
2985         gint nw, nh;
2986         gint vx, vy;
2987         gint vw, vh;
2988
2989         vw = pr->vis_width;
2990         vh = pr->vis_height;
2991
2992         vx = pr->x_scroll;
2993         vy = pr->y_scroll;
2994
2995         if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
2996
2997         /* now clamp it */
2998         nx = CLAMP(*x, vx, vx + vw);
2999         nw = CLAMP(*w - (nx - *x), 1, vw);
3000
3001         ny = CLAMP(*y, vy, vy + vh);
3002         nh = CLAMP(*h - (ny - *y), 1, vh);
3003
3004         *x = nx;
3005         *y = ny;
3006         *w = nw;
3007         *h = nh;
3008
3009         return TRUE;
3010 }
3011
3012 static gboolean pr_queue_to_tiles(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
3013                                   gboolean clamp, ImageTileRenderType render,
3014                                   gboolean new_data, gboolean only_existing)
3015 {
3016         gint i, j;
3017         gint x1, x2;
3018         gint y1, y2;
3019
3020         if (clamp && !pr_clamp_to_visible(pr, &x, &y, &w, &h)) return FALSE;
3021
3022         x1 = ROUND_DOWN(x, pr->tile_width);
3023         x2 = ROUND_UP(x + w, pr->tile_width);
3024
3025         y1 = ROUND_DOWN(y, pr->tile_height);
3026         y2 = ROUND_UP(y + h, pr->tile_height);
3027
3028         for (j = y1; j <= y2; j += pr->tile_height)
3029                 {
3030                 for (i = x1; i <= x2; i += pr->tile_width)
3031                         {
3032                         ImageTile *it;
3033
3034                         it = pr_tile_get(pr, i, j,
3035                                          (only_existing &&
3036                                           (i + pr->tile_width < pr->x_scroll ||
3037                                            i > pr->x_scroll + pr->vis_width ||
3038                                            j + pr->tile_height < pr->y_scroll ||
3039                                            j > pr->y_scroll + pr->vis_height)));
3040                         if (it)
3041                                 {
3042                                 QueueData *qd;
3043
3044                                 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
3045                                     (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
3046                                         {
3047                                         it->render_todo = render;
3048                                         }
3049
3050                                 qd = g_new(QueueData, 1);
3051                                 qd->it = it;
3052                                 qd->new_data = new_data;
3053
3054                                 if (i < x)
3055                                         {
3056                                         qd->x = x - i;
3057                                         }
3058                                 else
3059                                         {
3060                                         qd->x = 0;
3061                                         }
3062                                 qd->w = x + w - i - qd->x;
3063                                 if (qd->x + qd->w > pr->tile_width) qd->w = pr->tile_width - qd->x;
3064
3065                                 if (j < y)
3066                                         {
3067                                         qd->y = y - j;
3068                                         }
3069                                 else
3070                                         {
3071                                         qd->y = 0;
3072                                         }
3073                                 qd->h = y + h - j - qd->y;
3074                                 if (qd->y + qd->h > pr->tile_height) qd->h = pr->tile_height - qd->y;
3075
3076                                 if (qd->w < 1 || qd->h < 1)
3077                                         {
3078                                         g_free(qd);
3079                                         }
3080                                 else if (it->qd)
3081                                         {
3082                                         pr_queue_merge(it->qd, qd);
3083                                         g_free(qd);
3084                                         }
3085                                 else
3086                                         {
3087                                         it->qd = qd;
3088                                         pr->draw_queue = g_list_append(pr->draw_queue, qd);
3089                                         }
3090                                 }
3091                         }
3092                 }
3093
3094         return TRUE;
3095 }
3096
3097 static void pr_queue(PixbufRenderer *pr, gint x, gint y, gint w, gint h,
3098                      gboolean clamp, ImageTileRenderType render,
3099                      gboolean new_data, gboolean only_existing)
3100 {
3101         gint nx, ny;
3102
3103         nx = CLAMP(x, 0, pr->width - 1);
3104         ny = CLAMP(y, 0, pr->height - 1);
3105         w -= (nx - x);
3106         h -= (ny - y);
3107         w = CLAMP(w, 0, pr->width - nx);
3108         h = CLAMP(h, 0, pr->height - ny);
3109         if (w < 1 || h < 1) return;
3110
3111         if (pr_queue_to_tiles(pr, nx, ny, w, h, clamp, render, new_data, only_existing) &&
3112             ((!pr->draw_queue && !pr->draw_queue_2pass) || !pr->draw_idle_id))
3113                 {
3114                 if (pr->draw_idle_id)
3115                         {
3116                         g_source_remove(pr->draw_idle_id);
3117                         pr->draw_idle_id = 0;
3118                         }
3119                 pr_queue_schedule_next_draw(pr, TRUE);
3120                 }
3121 }
3122
3123 static void pr_redraw(PixbufRenderer *pr, gboolean new_data)
3124 {
3125         pr_queue_clear(pr);
3126         pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data, FALSE);
3127 }
3128
3129 /*
3130  *-------------------------------------------------------------------
3131  * signal emission
3132  *-------------------------------------------------------------------
3133  */
3134
3135 static void pr_update_signal(PixbufRenderer *pr)
3136 {
3137 #if 0
3138         log_printf("FIXME: send updated signal\n");
3139 #endif
3140         DEBUG_1("%s pixbuf renderer updated - started drawing %p", get_exec_time(), pr);
3141         pr->debug_updated = TRUE;
3142 }
3143
3144 static void pr_zoom_signal(PixbufRenderer *pr)
3145 {
3146         g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
3147 }
3148
3149 static void pr_clicked_signal(PixbufRenderer *pr, GdkEventButton *bevent)
3150 {
3151         g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
3152 }
3153
3154 static void pr_scroll_notify_signal(PixbufRenderer *pr)
3155 {
3156         g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
3157 }
3158
3159 static void pr_render_complete_signal(PixbufRenderer *pr)
3160 {
3161         if (!pr->complete)
3162                 {
3163                 g_signal_emit(pr, signals[SIGNAL_RENDER_COMPLETE], 0);
3164                 g_object_set(G_OBJECT(pr), "complete", TRUE, NULL);
3165                 }
3166         if (pr->debug_updated)
3167                 {
3168                 DEBUG_1("%s pixbuf renderer done %p", get_exec_time(), pr);
3169                 pr->debug_updated = FALSE;
3170                 }
3171 }
3172
3173 static void pr_drag_signal(PixbufRenderer *pr, GdkEventButton *bevent)
3174 {
3175         g_signal_emit(pr, signals[SIGNAL_DRAG], 0, bevent);
3176 }
3177
3178 static void pr_update_pixel_signal(PixbufRenderer *pr)
3179 {
3180         g_signal_emit(pr, signals[SIGNAL_UPDATE_PIXEL], 0);
3181 }
3182
3183 /*
3184  *-------------------------------------------------------------------
3185  * sync and clamp
3186  *-------------------------------------------------------------------
3187  */
3188
3189 static void pixbuf_renderer_sync_scroll_center(PixbufRenderer *pr)
3190 {
3191         gint src_x, src_y;
3192         if (!pr->width || !pr->height) return;
3193
3194         /* 
3195          * Update norm_center only if the image is bigger than the window.
3196          * With this condition the stored center survives also a temporary display
3197          * of the "broken image" icon.
3198         */
3199
3200         if (pr->width > pr->window_width)
3201                 {
3202                 src_x = pr->x_scroll + pr->vis_width / 2;
3203                 pr->norm_center_x = (gdouble)src_x / pr->width;
3204                 }
3205         
3206         if (pr->height > pr->window_height)
3207                 {
3208                 src_y = pr->y_scroll + pr->vis_height / 2;
3209                 pr->norm_center_y = (gdouble)src_y / pr->height;
3210                 }
3211 }
3212
3213
3214 static gboolean pr_scroll_clamp(PixbufRenderer *pr)
3215 {
3216         gint old_xs;
3217         gint old_ys;
3218
3219         if (pr->zoom == 0.0)
3220                 {
3221                 pr->x_scroll = 0;
3222                 pr->y_scroll = 0;
3223
3224                 return FALSE;
3225                 }
3226
3227         old_xs = pr->x_scroll;
3228         old_ys = pr->y_scroll;
3229
3230         if (pr->x_offset > 0)
3231                 {
3232                 pr->x_scroll = 0;
3233                 }
3234         else
3235                 {
3236                 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
3237                 }
3238
3239         if (pr->y_offset > 0)
3240                 {
3241                 pr->y_scroll = 0;
3242                 }
3243         else
3244                 {
3245                 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
3246                 }
3247
3248         pixbuf_renderer_sync_scroll_center(pr);
3249
3250         return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
3251 }
3252
3253 static gboolean pr_size_clamp(PixbufRenderer *pr)
3254 {
3255         gint old_vw, old_vh;
3256
3257         old_vw = pr->vis_width;
3258         old_vh = pr->vis_height;
3259
3260         if (pr->width < pr->window_width)
3261                 {
3262                 pr->vis_width = pr->width;
3263                 pr->x_offset = (pr->window_width - pr->width) / 2;
3264                 }
3265         else
3266                 {
3267                 pr->vis_width = pr->window_width;
3268                 pr->x_offset = 0;
3269                 }
3270
3271         if (pr->height < pr->window_height)
3272                 {
3273                 pr->vis_height = pr->height;
3274                 pr->y_offset = (pr->window_height - pr->height) / 2;
3275                 }
3276         else
3277                 {
3278                 pr->vis_height = pr->window_height;
3279                 pr->y_offset = 0;
3280                 }
3281
3282         pixbuf_renderer_sync_scroll_center(pr);
3283
3284         return (old_vw != pr->vis_width || old_vh != pr->vis_height);
3285 }
3286
3287 static gboolean pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom,
3288                               PrZoomFlags flags, gboolean *redrawn)
3289 {
3290         gint w, h;
3291         gdouble scale;
3292         gboolean invalid;
3293         gboolean force = !!(flags & PR_ZOOM_FORCE);
3294         gboolean new = !!(flags & PR_ZOOM_NEW);
3295         gboolean invalidate = !!(flags & PR_ZOOM_INVALIDATE);
3296         gboolean lazy = !!(flags & PR_ZOOM_LAZY);
3297
3298         zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
3299
3300         if (pr->zoom == zoom && !force) return FALSE;
3301
3302         w = pr->image_width;
3303         h = pr->image_height;
3304
3305         if (zoom == 0.0 && !pr->pixbuf)
3306                 {
3307                 scale = 1.0;
3308                 }
3309         else if (zoom == 0.0)
3310                 {
3311                 gint max_w;
3312                 gint max_h;
3313                 gboolean sizeable;
3314
3315                 sizeable = (new && pr_parent_window_sizable(pr));
3316
3317                 if (sizeable)
3318                         {
3319                         max_w = gdk_screen_width();
3320                         max_h = gdk_screen_height();
3321
3322                         if (pr->window_limit)
3323                                 {
3324                                 max_w = max_w * pr->window_limit_size / 100;
3325                                 max_h = max_h * pr->window_limit_size / 100;
3326                                 }
3327                         }
3328                 else
3329                         {
3330                         max_w = pr->window_width;
3331                         max_h = pr->window_height;
3332                         }
3333
3334                 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
3335                         {
3336                         if ((gdouble)max_w / w > (gdouble)max_h / h)
3337                                 {
3338                                 scale = (gdouble)max_h / h;
3339                                 h = max_h;
3340                                 w = w * scale + 0.5;
3341                                 if (w > max_w) w = max_w;
3342                                 }
3343                         else
3344                                 {
3345                                 scale = (gdouble)max_w / w;
3346                                 w = max_w;
3347                                 h = h * scale + 0.5;
3348                                 if (h > max_h) h = max_h;
3349                                 }
3350
3351                         if (pr->autofit_limit)
3352                                 {
3353                                 gdouble factor = (gdouble)pr->autofit_limit_size / 100;
3354                                 w = w * factor + 0.5;
3355                                 h = h * factor + 0.5;
3356                                 scale = scale * factor;
3357                                 }
3358
3359                         if (w < 1) w = 1;
3360                         if (h < 1) h = 1;
3361                         }
3362                 else
3363                         {
3364                         scale = 1.0;
3365                         }
3366                 }
3367         else if (zoom > 0.0) /* zoom orig, in */
3368                 {
3369                 scale = zoom;
3370                 w = w * scale;
3371                 h = h * scale;
3372                 }
3373         else /* zoom out */
3374                 {
3375                 scale = 1.0 / (0.0 - zoom);
3376                 w = w * scale;
3377                 h = h * scale;
3378                 }
3379
3380         invalid = (pr->width != w || pr->height != h);
3381
3382         pr->zoom = zoom;
3383         pr->width = w;
3384         pr->height = h;
3385         pr->scale = scale;
3386
3387         if (invalidate || invalid)
3388                 {
3389                 pr_tile_invalidate_all(pr);
3390                 if (!lazy) pr_redraw(pr, TRUE);
3391                 }
3392         if (redrawn) *redrawn = (invalidate || invalid);
3393
3394         pixbuf_renderer_sync_scroll_center(pr);
3395
3396         return TRUE;
3397 }
3398
3399 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
3400                          PrZoomFlags flags, gint px, gint py)
3401 {
3402         gdouble old_scale;
3403         gint old_cx, old_cy;
3404         gboolean clamped;
3405         gboolean sized;
3406         gboolean redrawn = FALSE;
3407         gboolean center_point = !!(flags & PR_ZOOM_CENTER);
3408         gboolean force = !!(flags & PR_ZOOM_FORCE);
3409         gboolean new = !!(flags & PR_ZOOM_NEW);
3410         gboolean lazy = !!(flags & PR_ZOOM_LAZY);
3411         PrZoomFlags clamp_flags = flags;
3412         gdouble old_center_x = pr->norm_center_x;
3413         gdouble old_center_y = pr->norm_center_y;
3414         
3415         old_scale = pr->scale;
3416         if (center_point)
3417                 {
3418                 px = CLAMP(px, 0, pr->width);
3419                 py = CLAMP(py, 0, pr->height);
3420                 old_cx = pr->x_scroll + (px - pr->x_offset);
3421                 old_cy = pr->y_scroll + (py - pr->y_offset);
3422                 }
3423         else
3424                 {
3425                 px = py = 0;
3426                 old_cx = pr->x_scroll + pr->vis_width / 2;
3427                 old_cy = pr->y_scroll + pr->vis_height / 2;
3428                 }
3429
3430         if (force) clamp_flags |= PR_ZOOM_INVALIDATE;
3431         if (lazy) clamp_flags |= PR_ZOOM_LAZY;
3432         if (!pr_zoom_clamp(pr, zoom, clamp_flags, &redrawn)) return;
3433
3434         clamped = pr_size_clamp(pr);
3435         sized = pr_parent_window_resize(pr, pr->width, pr->height);
3436
3437         if (force && new)
3438                 {
3439                 switch (pr->scroll_reset)
3440                         {
3441                         case PR_SCROLL_RESET_NOCHANGE:
3442                                 /* maintain old scroll position */
3443                                 pr->x_scroll = ((gdouble)pr->image_width * old_center_x * pr->scale) - pr->vis_width / 2;
3444                                 pr->y_scroll = ((gdouble)pr->image_height * old_center_y * pr->scale) - pr->vis_height / 2;
3445                                 break;
3446                         case PR_SCROLL_RESET_CENTER:
3447                                 /* center new image */
3448                                 pr->x_scroll = ((gdouble)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2;
3449                                 pr->y_scroll = ((gdouble)pr->image_height / 2.0 * pr->scale) - pr->vis_height / 2;
3450                                 break;
3451                         case PR_SCROLL_RESET_TOPLEFT:
3452                         default:
3453                                 /* reset to upper left */
3454                                 pr->x_scroll = 0;
3455                                 pr->y_scroll = 0;
3456                                 break;
3457                         }
3458                 }
3459         else
3460                 {
3461                 /* user zoom does not force, so keep visible center point */
3462                 if (center_point)
3463                         {
3464                         pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
3465                         pr->y_scroll = old_cy / old_scale * pr->scale - (py - pr->y_offset);
3466                         }
3467                 else
3468                         {
3469                         pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2);
3470                         pr->y_scroll = old_cy / old_scale * pr->scale - (pr->vis_height / 2);
3471                         }
3472                 }
3473
3474         pr_scroll_clamp(pr);
3475
3476         /* If the window was not sized, redraw the image - we know there will be no size/expose signal.
3477          * But even if a size is claimed, there is no guarantee that the window manager will allow it,
3478          * so redraw the window anyway :/
3479          */
3480         if (sized || clamped) pr_border_clear(pr);
3481         
3482         if (lazy)
3483                 {
3484                 pr_queue_clear(pr);
3485                 }
3486         else
3487                 {
3488                 pr_redraw(pr, redrawn);
3489                 }
3490
3491         pr_scroll_notify_signal(pr);
3492         pr_zoom_signal(pr);
3493         pr_update_signal(pr);
3494 }
3495
3496 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
3497 {
3498         gboolean zoom_changed = FALSE;
3499
3500         if (pr->window_width == new_width && pr->window_height == new_height) return;
3501
3502         pr->window_width = new_width;
3503         pr->window_height = new_height;
3504
3505         if (pr->zoom == 0.0)
3506                 {
3507                 gdouble old_scale = pr->scale;
3508                 pr_zoom_clamp(pr, 0.0, PR_ZOOM_FORCE, NULL);
3509                 zoom_changed = (old_scale != pr->scale);
3510                 }
3511
3512         pr_size_clamp(pr);
3513         pr_scroll_clamp(pr);
3514
3515         pr_overlay_update_sizes(pr);
3516
3517         /* ensure scroller remains visible */
3518         if (pr->scroller_overlay != -1)
3519                 {
3520                 gboolean update = FALSE;
3521
3522                 if (pr->scroller_x > new_width)
3523                         {
3524                         pr->scroller_x = new_width;
3525                         pr->scroller_xpos = new_width;
3526                         update = TRUE;
3527                         }
3528                 if (pr->scroller_y > new_height)
3529                         {
3530                         pr->scroller_y = new_height;
3531                         pr->scroller_ypos = new_height;
3532                         update = TRUE;
3533                         }
3534
3535                 if (update)
3536                         {
3537                         GdkPixbuf *pixbuf;
3538
3539                         if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
3540                                 {
3541                                 gint w, h;
3542
3543                                 w = gdk_pixbuf_get_width(pixbuf);
3544                                 h = gdk_pixbuf_get_height(pixbuf);
3545                                 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
3546                                                             pr->scroller_x - w / 2, pr->scroller_y - h / 2);
3547                                 }
3548                         }
3549                 }
3550
3551         pr_border_clear(pr);
3552
3553         pr_scroll_notify_signal(pr);
3554         if (zoom_changed) pr_zoom_signal(pr);
3555         pr_update_signal(pr);
3556 }
3557
3558 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
3559 {
3560         PixbufRenderer *pr = data;
3561
3562         pr_size_sync(pr, allocation->width, allocation->height);
3563 }
3564
3565 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
3566 {
3567         gint x, y;
3568
3569         pr_border_draw(pr, area->x, area->y, area->width, area->height);
3570
3571         x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
3572         y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
3573
3574         pr_queue(pr, x, y,
3575                  MIN((gint)area->width, pr->width - x),
3576                  MIN((gint)area->height, pr->height - y),
3577                  FALSE, TILE_RENDER_ALL, FALSE, FALSE);
3578 }
3579
3580 /*
3581  *-------------------------------------------------------------------
3582  * scrolling
3583  *-------------------------------------------------------------------
3584  */
3585
3586 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
3587 {
3588         gint old_x, old_y;
3589         gint x_off, y_off;
3590         gint w, h;
3591
3592         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
3593
3594         if (!pr->pixbuf && !pr->source_tiles_enabled) return;
3595
3596         old_x = pr->x_scroll;
3597         old_y = pr->y_scroll;
3598
3599         pr->x_scroll += x;
3600         pr->y_scroll += y;
3601
3602         pr_scroll_clamp(pr);
3603         
3604         pixbuf_renderer_sync_scroll_center(pr);
3605         
3606         if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
3607
3608         pr_scroll_notify_signal(pr);
3609
3610         x_off = pr->x_scroll - old_x;
3611         y_off = pr->y_scroll - old_y;
3612
3613         w = pr->vis_width - abs(x_off);
3614         h = pr->vis_height - abs(y_off);
3615
3616         if (w < 1 || h < 1)
3617                 {
3618                 /* scrolled completely to new material */
3619                 pr_queue(pr, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3620                 return;
3621                 }
3622         else
3623                 {
3624                 gint x1, y1;
3625                 gint x2, y2;
3626                 GtkWidget *box;
3627                 GdkGC *gc;
3628                 GdkEvent *event;
3629
3630                 if (x_off < 0)
3631                         {
3632                         x1 = abs(x_off);
3633                         x2 = 0;
3634                         }
3635                 else
3636                         {
3637                         x1 = 0;
3638                         x2 = abs(x_off);
3639                         }
3640
3641                 if (y_off < 0)
3642                         {
3643                         y1 = abs(y_off);
3644                         y2 = 0;
3645                         }
3646                 else
3647                         {
3648                         y1 = 0;
3649                         y2 = abs(y_off);
3650                         }
3651
3652                 box = GTK_WIDGET(pr);
3653
3654                 gc = gdk_gc_new(box->window);
3655                 gdk_gc_set_exposures(gc, TRUE);
3656                 gdk_draw_drawable(box->window, gc,
3657                                   box->window,
3658                                   x2 + pr->x_offset, y2 + pr->y_offset,
3659                                   x1 + pr->x_offset, y1 + pr->y_offset, w, h);
3660                 g_object_unref(gc);
3661
3662                 if (pr->overlay_list)
3663                         {
3664                         pr_overlay_queue_all(pr);
3665                         }
3666
3667                 w = pr->vis_width - w;
3668                 h = pr->vis_height - h;
3669
3670                 if (w > 0)
3671                         {
3672                         pr_queue(pr,
3673                                  x_off > 0 ? pr->x_scroll + (pr->vis_width - w) : pr->x_scroll, pr->y_scroll,
3674                                  w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3675                         }
3676                 if (h > 0)
3677                         {
3678                         /* FIXME, to optimize this, remove overlap */
3679                         pr_queue(pr,
3680                                  pr->x_scroll, y_off > 0 ? pr->y_scroll + (pr->vis_height - h) : pr->y_scroll,
3681                                  pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
3682                         }
3683
3684                 /* process exposures here, "expose_event" seems to miss a few with obstructed windows */
3685 #if ! GTK_CHECK_VERSION(2,18,0)
3686                 while ((event = gdk_event_get_graphics_expose(box->window)) != NULL)
3687                         {
3688                         pixbuf_renderer_paint(pr, &event->expose.area);
3689
3690                         if (event->expose.count == 0)
3691                                 {
3692                                 gdk_event_free(event);
3693                                 break;
3694                                 }
3695                         gdk_event_free(event);
3696                         }
3697 #endif
3698                 }
3699 }
3700
3701 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
3702                                      gdouble x_align, gdouble y_align)
3703 {
3704         gint px, py;
3705         gint ax, ay;
3706
3707         x_align = CLAMP(x_align, 0.0, 1.0);
3708         y_align = CLAMP(y_align, 0.0, 1.0);
3709
3710         ax = (gdouble)pr->vis_width * x_align;
3711         ay = (gdouble)pr->vis_height * y_align;
3712
3713         px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
3714         py = (gdouble)y * pr->scale - (pr->y_scroll + ay);
3715
3716         pixbuf_renderer_scroll(pr, px, py);
3717 }
3718
3719 /* get or set coordinates of viewport center in the image, in range 0.0 - 1.0 */
3720
3721 void pixbuf_renderer_get_scroll_center(PixbufRenderer *pr, gdouble *x, gdouble *y)
3722 {
3723         *x = pr->norm_center_x;
3724         *y = pr->norm_center_y;
3725 }
3726
3727 void pixbuf_renderer_set_scroll_center(PixbufRenderer *pr, gdouble x, gdouble y)
3728 {
3729         gdouble dst_x, dst_y;
3730
3731         dst_x = x * pr->width  - pr->vis_width  / 2 - pr->x_scroll + CLAMP(pr->subpixel_x_scroll, -1.0, 1.0);
3732         dst_y = y * pr->height - pr->vis_height / 2 - pr->y_scroll + CLAMP(pr->subpixel_y_scroll, -1.0, 1.0);
3733
3734         pr->subpixel_x_scroll = dst_x - (gint)dst_x;
3735         pr->subpixel_y_scroll = dst_y - (gint)dst_y;
3736
3737         pixbuf_renderer_scroll(pr, (gint)dst_x, (gint)dst_y);
3738 }
3739
3740 /*
3741  *-------------------------------------------------------------------
3742  * mouse
3743  *-------------------------------------------------------------------
3744  */
3745
3746 static gboolean pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3747 {
3748         PixbufRenderer *pr;
3749         gint accel;
3750
3751         /* This is a hack, but work far the best, at least for single pointer systems.
3752          * See http://bugzilla.gnome.org/show_bug.cgi?id=587714 for more. */
3753         gint x, y;
3754         gdk_window_get_pointer (bevent->window, &x, &y, NULL);
3755         bevent->x = x;
3756         bevent->y = y;
3757
3758         pr = PIXBUF_RENDERER(widget);
3759
3760         if (pr->scroller_id)
3761                 {
3762                 pr->scroller_xpos = bevent->x;
3763                 pr->scroller_ypos = bevent->y;
3764                 }
3765         
3766         pr->x_mouse = bevent->x;
3767         pr->y_mouse = bevent->y;
3768         pr_update_pixel_signal(pr);
3769         
3770         if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
3771
3772         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3773                 {
3774                 pr->drag_moved++;
3775                 }
3776         else
3777                 {
3778                 widget_set_cursor(widget, GDK_FLEUR);
3779                 }
3780
3781         if (bevent->state & GDK_CONTROL_MASK)
3782                 {
3783                 accel = PR_PAN_SHIFT_MULTIPLIER;
3784                 }
3785         else
3786                 {
3787                 accel = 1;
3788                 }
3789
3790         /* do the scroll */
3791         pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
3792                                (pr->drag_last_y - bevent->y) * accel);
3793
3794         pr_drag_signal(pr, bevent);
3795
3796         pr->drag_last_x = bevent->x;
3797         pr->drag_last_y = bevent->y;
3798
3799         /* This is recommended by the GTK+ documentation, but does not work properly.
3800          * Use deprecated way until GTK+ gets a solution for correct motion hint handling:
3801          * http://bugzilla.gnome.org/show_bug.cgi?id=587714
3802          */
3803         /* gdk_event_request_motions (bevent); */
3804         return FALSE;
3805 }
3806
3807 static gboolean pr_leave_notify_cb(GtkWidget *widget, GdkEventCrossing *cevent, gpointer data)
3808 {
3809         PixbufRenderer *pr;
3810
3811         pr = PIXBUF_RENDERER(widget);
3812         pr->x_mouse = -1;
3813         pr->y_mouse = -1;
3814
3815         pr_update_pixel_signal(pr);
3816         return FALSE;
3817 }
3818
3819 static gboolean pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3820 {
3821         PixbufRenderer *pr;
3822         GtkWidget *parent;
3823
3824         pr = PIXBUF_RENDERER(widget);
3825
3826         if (pr->scroller_id) return TRUE;
3827
3828         switch (bevent->button)
3829                 {
3830                 case MOUSE_BUTTON_LEFT:
3831                         pr->in_drag = TRUE;
3832                         pr->drag_last_x = bevent->x;
3833                         pr->drag_last_y = bevent->y;
3834                         pr->drag_moved = 0;
3835                         gdk_pointer_grab(widget->window, FALSE,
3836                                          GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_RELEASE_MASK,
3837                                          NULL, NULL, bevent->time);
3838                         gtk_grab_add(widget);
3839                         break;
3840                 case MOUSE_BUTTON_MIDDLE:
3841                         pr->drag_moved = 0;
3842                         break;
3843                 case MOUSE_BUTTON_RIGHT:
3844                         pr_clicked_signal(pr, bevent);
3845                         break;
3846                 default:
3847                         break;
3848                 }
3849
3850         parent = gtk_widget_get_parent(widget);
3851         if (widget && GTK_WIDGET_CAN_FOCUS(parent))
3852                 {
3853                 gtk_widget_grab_focus(parent);
3854                 }
3855
3856         return FALSE;
3857 }
3858
3859 static gboolean pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
3860 {
3861         PixbufRenderer *pr;
3862
3863         pr = PIXBUF_RENDERER(widget);
3864
3865         if (pr->scroller_id)
3866                 {
3867                 pr_scroller_stop(pr);
3868                 return TRUE;
3869                 }
3870
3871         if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
3872                 {
3873                 gtk_grab_remove(widget);
3874                 gdk_pointer_ungrab(bevent->time);
3875                 widget_set_cursor(widget, -1);
3876                 }
3877
3878         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
3879                 {
3880                 if (bevent->button == MOUSE_BUTTON_LEFT && (bevent->state & GDK_CONTROL_MASK))
3881                         {
3882                         pr_scroller_start(pr, bevent->x, bevent->y);
3883                         }
3884                 else if (bevent->button == MOUSE_BUTTON_LEFT || bevent->button == MOUSE_BUTTON_MIDDLE)
3885                         {
3886                         pr_clicked_signal(pr, bevent);
3887                         }
3888                 }
3889
3890         pr->in_drag = FALSE;
3891
3892         return FALSE;
3893 }
3894
3895 static gboolean pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
3896 {
3897         PixbufRenderer *pr;
3898
3899         pr = PIXBUF_RENDERER(widget);
3900
3901         if (pr->scroller_id)
3902                 {
3903                 pr->scroller_xpos = pr->scroller_x;
3904                 pr->scroller_ypos = pr->scroller_y;
3905                 pr->scroller_xinc = 0;
3906                 pr->scroller_yinc = 0;
3907                 }
3908
3909         return FALSE;
3910 }
3911
3912 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
3913 {
3914         PixbufRenderer *pr;
3915
3916         pr = PIXBUF_RENDERER(widget);
3917
3918         pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
3919 }
3920
3921 static void pr_signals_connect(PixbufRenderer *pr)
3922 {
3923         g_signal_connect(G_OBJECT(pr), "motion_notify_event",
3924                          G_CALLBACK(pr_mouse_motion_cb), pr);
3925         g_signal_connect(G_OBJECT(pr), "button_press_event",
3926                          G_CALLBACK(pr_mouse_press_cb), pr);
3927         g_signal_connect(G_OBJECT(pr), "button_release_event",
3928                          G_CALLBACK(pr_mouse_release_cb), pr);
3929         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
3930                          G_CALLBACK(pr_mouse_leave_cb), pr);
3931         g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
3932                          G_CALLBACK(pr_hierarchy_changed_cb), pr);
3933         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
3934                          G_CALLBACK(pr_leave_notify_cb), pr);
3935
3936         gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
3937                                               GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
3938                                               GDK_LEAVE_NOTIFY_MASK);
3939
3940         g_signal_connect(G_OBJECT(pr), "drag_begin",
3941                          G_CALLBACK(pr_mouse_drag_cb), pr);
3942
3943 }
3944
3945 /*
3946  *-------------------------------------------------------------------
3947  * public
3948  *-------------------------------------------------------------------
3949  */
3950 static void pr_pixbuf_size_sync(PixbufRenderer *pr)
3951 {
3952         if (!pr->pixbuf) return;
3953         switch (pr->orientation)
3954                 {
3955                 case EXIF_ORIENTATION_LEFT_TOP:
3956                 case EXIF_ORIENTATION_RIGHT_TOP:
3957                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
3958                 case EXIF_ORIENTATION_LEFT_BOTTOM:
3959                         pr->image_width = gdk_pixbuf_get_height(pr->pixbuf);
3960                         pr->image_height = gdk_pixbuf_get_width(pr->pixbuf);
3961                         break;
3962                 default:
3963                         pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
3964                         pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
3965                 }
3966 }
3967
3968 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, PrZoomFlags flags)
3969 {
3970         if (pixbuf) g_object_ref(pixbuf);
3971         if (pr->pixbuf) g_object_unref(pr->pixbuf);
3972         pr->pixbuf = pixbuf;
3973
3974         if (!pr->pixbuf)
3975                 {
3976                 GtkWidget *box;
3977
3978                 /* no pixbuf so just clear the window */
3979                 pr->image_width = 0;
3980                 pr->image_height = 0;
3981                 pr->scale = 1.0;
3982                 pr->zoom = zoom; /* don't throw away the zoom value, it is set by pixbuf_renderer_move, among others,
3983                                     and used for pixbuf_renderer_zoom_get */
3984
3985                 box = GTK_WIDGET(pr);
3986
3987                 if (GTK_WIDGET_REALIZED(box))
3988                         {
3989                         gdk_window_clear(box->window);
3990                         pr_overlay_draw(pr, 0, 0, pr->window_width, pr->window_height, NULL);
3991                         }
3992
3993                 pr_update_signal(pr);
3994
3995                 return;
3996                 }
3997
3998         pr_pixbuf_size_sync(pr);
3999         pr_zoom_sync(pr, zoom, flags | PR_ZOOM_FORCE | PR_ZOOM_NEW, 0, 0);
4000 }
4001
4002 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
4003 {
4004         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4005
4006         pr_source_tile_unset(pr);
4007
4008         pr_set_pixbuf(pr, pixbuf, zoom, 0);
4009
4010         pr_update_signal(pr);
4011 }
4012
4013 void pixbuf_renderer_set_pixbuf_lazy(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, gint orientation)
4014 {
4015         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4016
4017         pr_source_tile_unset(pr);
4018
4019         pr->orientation = orientation;
4020         pr_set_pixbuf(pr, pixbuf, zoom, PR_ZOOM_LAZY);
4021
4022         pr_update_signal(pr);
4023 }
4024
4025 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
4026 {
4027         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
4028
4029         return pr->pixbuf;
4030 }
4031
4032 void pixbuf_renderer_set_orientation(PixbufRenderer *pr, gint orientation)
4033 {
4034         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4035
4036         pr->orientation = orientation;
4037
4038         pr_pixbuf_size_sync(pr);
4039         pr_zoom_sync(pr, pr->zoom, PR_ZOOM_FORCE, 0, 0);
4040 }
4041
4042 gint pixbuf_renderer_get_orientation(PixbufRenderer *pr)
4043 {
4044         if (!pr) return 1;
4045         return pr->orientation;
4046 }
4047
4048 void pixbuf_renderer_set_post_process_func(PixbufRenderer *pr, PixbufRendererPostProcessFunc func, gpointer user_data, gboolean slow)
4049 {
4050         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4051
4052         pr->func_post_process = func;
4053         pr->post_process_user_data = user_data;
4054         pr->post_process_slow = func && slow;
4055
4056 }
4057
4058
4059 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
4060 {
4061         GObject *object;
4062         PixbufRendererScrollResetType scroll_reset;
4063
4064         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4065         g_return_if_fail(IS_PIXBUF_RENDERER(source));
4066
4067         if (pr == source) return;
4068
4069         object = G_OBJECT(pr);
4070
4071         g_object_set(object, "zoom_min", source->zoom_min, NULL);
4072         g_object_set(object, "zoom_max", source->zoom_max, NULL);
4073         g_object_set(object, "loading", source->loading, NULL);
4074
4075         pr->complete = source->complete;
4076
4077         pr->x_scroll = source->x_scroll;
4078         pr->y_scroll = source->y_scroll;
4079         pr->x_mouse  = source->x_mouse;
4080         pr->y_mouse  = source->y_mouse;
4081
4082         scroll_reset = pr->scroll_reset;
4083         pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
4084
4085         pr->func_post_process = source->func_post_process;
4086         pr->post_process_user_data = source->post_process_user_data;
4087         pr->post_process_slow = source->post_process_slow;
4088         pr->orientation = source->orientation;
4089
4090         if (source->source_tiles_enabled)
4091                 {
4092                 pr_source_tile_unset(pr);
4093
4094                 pr->source_tiles_enabled = source->source_tiles_enabled;
4095                 pr->source_tiles_cache_size = source->source_tiles_cache_size;
4096                 pr->source_tile_width = source->source_tile_width;
4097                 pr->source_tile_height = source->source_tile_height;
4098                 pr->image_width = source->image_width;
4099                 pr->image_height = source->image_height;
4100
4101                 pr->func_tile_request = source->func_tile_request;
4102                 pr->func_tile_dispose = source->func_tile_dispose;
4103                 pr->func_tile_data = source->func_tile_data;
4104
4105                 pr->source_tiles = source->source_tiles;
4106                 source->source_tiles = NULL;
4107
4108                 pr_zoom_sync(pr, source->zoom, PR_ZOOM_FORCE | PR_ZOOM_NEW, 0, 0);
4109                 pr_redraw(pr, TRUE);
4110                 }
4111         else
4112                 {
4113                 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
4114                 }
4115
4116         pr->scroll_reset = scroll_reset;
4117
4118         pixbuf_renderer_set_pixbuf(source, NULL, source->zoom);
4119         pr_queue_clear(source);
4120         pr_tile_free_all(source);
4121 }
4122
4123 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint src_x, gint src_y, gint src_w, gint src_h)
4124 {
4125         gint x, y, width, height,  x1, y1, x2, y2;
4126
4127         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4128
4129         pr_coords_map_orientation_reverse(pr,
4130                                      src_x, src_y,
4131                                      pr->image_width, pr->image_height,
4132                                      src_w, src_h,
4133                                      &x, &y,
4134                                      &width, &height);
4135
4136         if (pr->source_tiles_enabled)
4137                 {
4138                 pr_source_tile_changed(pr, x, y, width, height);
4139                 }
4140
4141         if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
4142                 {
4143                 /* increase region when using a zoom quality that may access surrounding pixels */
4144                 y -= 1;
4145                 height += 2;
4146                 }
4147
4148         x1 = (gint)floor((gdouble)x * pr->scale);
4149         y1 = (gint)floor((gdouble)y * pr->scale);
4150         x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
4151         y2 = (gint)ceil((gdouble)(y + height) * pr->scale);
4152
4153         pr_queue(pr, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
4154 }
4155
4156 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
4157 {
4158         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4159
4160         pr_zoom_adjust_real(pr, increment, PR_ZOOM_NONE, 0, 0);
4161 }
4162
4163 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
4164 {
4165         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4166
4167         pr_zoom_adjust_real(pr, increment, PR_ZOOM_CENTER, x, y);
4168 }
4169
4170 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
4171 {
4172         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4173
4174         pr_zoom_sync(pr, zoom, PR_ZOOM_NONE, 0, 0);
4175 }
4176
4177 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
4178 {
4179         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
4180
4181         return pr->zoom;
4182 }
4183
4184 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
4185 {
4186         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
4187
4188         return pr->scale;
4189 }
4190
4191 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
4192 {
4193         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
4194
4195         if (min > 1.0 || max < 1.0) return;
4196         if (min < 1.0 && min > -1.0) return;
4197         if (min < -200.0 || max > 200.0) return;
4198
4199         if (pr->zoom_min != min)
4200                 {
4201                 pr->zoom_min = min;
4202                 g_object_notify(G_OBJECT(pr), "zoom_min");
4203                 }
4204         if (pr->zoom_max != max)
4205                 {
4206                 pr->zoom_max = max;
4207                 g_object_notify(G_OBJECT(pr), "zoom_max");
4208                 }
4209 }
4210
4211 gboolean pixbuf_renderer_get_pixel_colors(PixbufRenderer *pr, gint x_pixel, gint y_pixel, 
4212                                           gint *r_mouse, gint *g_mouse, gint *b_mouse)
4213 {
4214         GdkPixbuf *pb = pr->pixbuf;
4215         gint p_alpha, prs;
4216         guchar *p_pix, *pp;
4217         gint map_x, map_y, map_w, map_h;
4218         
4219         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4220         g_return_val_if_fail(r_mouse != NULL && g_mouse != NULL && b_mouse != NULL, FALSE);
4221
4222         if (!pr->pixbuf && !pr->source_tiles_enabled)
4223                 {
4224                 *r_mouse = -1;
4225                 *g_mouse = -1;
4226                 *b_mouse = -1;
4227                 return FALSE;
4228                 }
4229         
4230         if (!pb) return FALSE;
4231
4232         pr_tile_region_map_orientation(pr,
4233                                         x_pixel, y_pixel,
4234                                         pr->image_width, pr->image_height,
4235                                         1, 1, /*single pixel */
4236                                         &map_x, &map_y,
4237                                         &map_w, &map_h);
4238
4239         if (map_x < 0 || map_x > gdk_pixbuf_get_width(pr->pixbuf) - 1) return  FALSE;
4240         if (map_y < 0 || map_y > gdk_pixbuf_get_height(pr->pixbuf) - 1) return  FALSE;
4241         
4242         p_alpha = gdk_pixbuf_get_has_alpha(pb);
4243         prs = gdk_pixbuf_get_rowstride(pb);
4244         p_pix = gdk_pixbuf_get_pixels(pb);
4245
4246         pp = p_pix + map_y * prs + (map_x * (p_alpha ? 4 : 3));
4247         *r_mouse = *pp;
4248         pp++;
4249         *g_mouse = *pp;
4250         pp++;
4251         *b_mouse = *pp;
4252         
4253         return TRUE;
4254 }
4255
4256 gboolean pixbuf_renderer_get_mouse_position(PixbufRenderer *pr, gint *x_pixel_return, gint *y_pixel_return)
4257 {
4258         gint x_pixel, y_pixel, x_pixel_clamped, y_pixel_clamped;
4259              
4260         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4261         g_return_val_if_fail(x_pixel_return != NULL && y_pixel_return != NULL, FALSE);
4262
4263         if (!pr->pixbuf && !pr->source_tiles_enabled)
4264                 {
4265                 *x_pixel_return = -1;
4266                 *y_pixel_return = -1;
4267                 return FALSE;
4268                 }
4269         
4270         x_pixel = floor((gdouble)(pr->x_mouse - pr->x_offset + pr->x_scroll) / pr->scale);
4271         y_pixel = floor((gdouble)(pr->y_mouse - pr->y_offset + pr->y_scroll) / pr->scale);
4272         x_pixel_clamped = CLAMP(x_pixel, 0, pr->image_width - 1);
4273         y_pixel_clamped = CLAMP(y_pixel, 0, pr->image_height - 1);
4274         
4275         if(x_pixel != x_pixel_clamped || y_pixel != y_pixel_clamped)
4276                 {
4277                 /* mouse is not on pr */
4278                 x_pixel = y_pixel = -1;
4279                 }
4280
4281         *x_pixel_return = x_pixel;
4282         *y_pixel_return = y_pixel;
4283         
4284         return TRUE;
4285 }
4286
4287 gboolean pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
4288 {
4289         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4290         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
4291
4292         if (!pr->pixbuf && !pr->source_tiles_enabled)
4293                 {
4294                 *width = 0;
4295                 *height = 0;
4296                 return FALSE;
4297                 }
4298
4299         *width = pr->image_width;
4300         *height = pr->image_height;
4301         return TRUE;
4302 }
4303
4304 gboolean pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
4305 {
4306         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4307         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
4308
4309         if (!pr->pixbuf && !pr->source_tiles_enabled)
4310                 {
4311                 *width = 0;
4312                 *height = 0;
4313                 return FALSE;
4314                 }
4315
4316         *width = pr->width;
4317         *height = pr->height;
4318         return TRUE;
4319 }
4320
4321 gboolean pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
4322 {
4323         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4324         g_return_val_if_fail(rect != NULL, FALSE);
4325
4326         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
4327             !pr->scale)
4328                 {
4329                 rect->x = 0;
4330                 rect->y = 0;
4331                 rect->width = 0;
4332                 rect->height = 0;
4333                 return FALSE;
4334                 }
4335
4336         rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
4337         rect->y = (gint)((gdouble)pr->y_scroll / pr->scale);
4338         rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
4339         rect->height = (gint)((gdouble)pr->vis_height / pr->scale);
4340         return TRUE;
4341 }
4342
4343 gboolean pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
4344 {
4345         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
4346         g_return_val_if_fail(rect != NULL, FALSE);
4347
4348         if ((!pr->pixbuf && !pr->source_tiles_enabled))
4349                 {
4350                 rect->x = 0;
4351                 rect->y = 0;
4352                 rect->width = 0;
4353                 rect->height = 0;
4354                 return FALSE;
4355                 }
4356
4357         rect->x = pr->x_scroll;
4358         rect->y = pr->y_scroll;
4359         rect->width = pr->vis_width;
4360         rect->height = pr->vis_height;
4361         return TRUE;
4362 }
4363 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */