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