swap stereo images
[geeqie.git] / src / pixbuf-renderer.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2010 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <math.h>
17
18 #include "main.h"
19 #include "pixbuf-renderer.h"
20 #include "renderer-tiles.h"
21
22 #include "intl.h"
23 #include "layout.h"
24
25 #include <gtk/gtk.h>
26
27
28 /* comment this out if not using this from within Geeqie
29  * defining GQ_BUILD does these things:
30  *   - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
31  */
32 #define GQ_BUILD 1
33
34 #ifdef GQ_BUILD
35 #include "main.h"
36 #include "pixbuf_util.h"
37 #include "exif.h"
38 #else
39 typedef enum {
40         EXIF_ORIENTATION_UNKNOWN        = 0,
41         EXIF_ORIENTATION_TOP_LEFT       = 1,
42         EXIF_ORIENTATION_TOP_RIGHT      = 2,
43         EXIF_ORIENTATION_BOTTOM_RIGHT   = 3,
44         EXIF_ORIENTATION_BOTTOM_LEFT    = 4,
45         EXIF_ORIENTATION_LEFT_TOP       = 5,
46         EXIF_ORIENTATION_RIGHT_TOP      = 6,
47         EXIF_ORIENTATION_RIGHT_BOTTOM   = 7,
48         EXIF_ORIENTATION_LEFT_BOTTOM    = 8
49 } ExifOrientationType;
50 #endif
51
52
53 /* default min and max zoom */
54 #define PR_ZOOM_MIN -32.0
55 #define PR_ZOOM_MAX 32.0
56
57 /* distance to drag mouse to disable image flip */
58 #define PR_DRAG_SCROLL_THRESHHOLD 4
59
60 /* increase pan rate when holding down shift */
61 #define PR_PAN_SHIFT_MULTIPLIER 6
62
63 /* scroller config */
64 #define PR_SCROLLER_UPDATES_PER_SEC 30
65 #define PR_SCROLLER_DEAD_ZONE 6
66
67 /* when scaling image to below this size, use nearest pixel for scaling
68  * (below about 4, the other scale types become slow generating their conversion tables)
69  */
70 #define PR_MIN_SCALE_SIZE 8
71
72 enum {
73         SIGNAL_ZOOM = 0,
74         SIGNAL_CLICKED,
75         SIGNAL_SCROLL_NOTIFY,
76         SIGNAL_RENDER_COMPLETE,
77         SIGNAL_DRAG,
78         SIGNAL_UPDATE_PIXEL,
79         SIGNAL_COUNT
80 };
81
82 enum {
83         PROP_0,
84         PROP_ZOOM_MIN,
85         PROP_ZOOM_MAX,
86         PROP_ZOOM_QUALITY,
87         PROP_ZOOM_2PASS,
88         PROP_ZOOM_EXPAND,
89         PROP_DITHER_QUALITY,
90         PROP_SCROLL_RESET,
91         PROP_DELAY_FLIP,
92         PROP_LOADING,
93         PROP_COMPLETE,
94         PROP_CACHE_SIZE_DISPLAY,
95         PROP_CACHE_SIZE_TILES,
96         PROP_WINDOW_FIT,
97         PROP_WINDOW_LIMIT,
98         PROP_WINDOW_LIMIT_VALUE,
99         PROP_AUTOFIT_LIMIT,
100         PROP_AUTOFIT_LIMIT_VALUE
101 };
102
103 typedef enum {
104         PR_ZOOM_NONE            = 0,
105         PR_ZOOM_FORCE           = 1 << 0,
106         PR_ZOOM_NEW             = 1 << 1,
107         PR_ZOOM_CENTER          = 1 << 2,
108         PR_ZOOM_INVALIDATE      = 1 << 3,
109         PR_ZOOM_LAZY            = 1 << 4  /* wait with redraw for pixbuf_renderer_area_changed */
110 } PrZoomFlags;
111
112 static guint signals[SIGNAL_COUNT] = { 0 };
113 static GtkEventBoxClass *parent_class = NULL;
114
115
116
117 static void pixbuf_renderer_class_init(PixbufRendererClass *class);
118 static void pixbuf_renderer_init(PixbufRenderer *pr);
119 static void pixbuf_renderer_finalize(GObject *object);
120 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
121                                          const GValue *value, GParamSpec *pspec);
122 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
123                                          GValue *value, GParamSpec *pspec);
124 static gboolean pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event);
125
126 static void pr_scroller_timer_set(PixbufRenderer *pr, gboolean start);
127
128
129 static void pr_source_tile_free_all(PixbufRenderer *pr);
130
131 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
132                          PrZoomFlags flags, gint px, gint py);
133
134 static void pr_signals_connect(PixbufRenderer *pr);
135 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data);
136 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area);
137
138
139 /*
140  *-------------------------------------------------------------------
141  * Pixbuf Renderer object
142  *-------------------------------------------------------------------
143  */
144
145 GType pixbuf_renderer_get_type(void)
146 {
147         static GType pixbuf_renderer_type = 0;
148
149         if (!pixbuf_renderer_type)
150                 {
151                 static const GTypeInfo pixbuf_renderer_info =
152                         {
153                         sizeof(PixbufRendererClass), /* class_size */
154                         NULL,           /* base_init */
155                         NULL,           /* base_finalize */
156                         (GClassInitFunc)pixbuf_renderer_class_init,
157                         NULL,           /* class_finalize */
158                         NULL,           /* class_data */
159                         sizeof(PixbufRenderer), /* instance_size */
160                         0,              /* n_preallocs */
161                         (GInstanceInitFunc)pixbuf_renderer_init, /* instance_init */
162                         NULL,           /* value_table */
163                         };
164
165                 pixbuf_renderer_type = g_type_register_static(GTK_TYPE_EVENT_BOX, "PixbufRenderer",
166                                                               &pixbuf_renderer_info, 0);
167                 }
168
169         return pixbuf_renderer_type;
170 }
171
172 static void pixbuf_renderer_class_init(PixbufRendererClass *class)
173 {
174         GObjectClass *gobject_class = G_OBJECT_CLASS(class);
175         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
176
177         parent_class = g_type_class_peek_parent(class);
178
179         gobject_class->set_property = pixbuf_renderer_set_property;
180         gobject_class->get_property = pixbuf_renderer_get_property;
181
182         gobject_class->finalize = pixbuf_renderer_finalize;
183
184         widget_class->expose_event = pixbuf_renderer_expose;
185
186         g_object_class_install_property(gobject_class,
187                                         PROP_ZOOM_MIN,
188                                         g_param_spec_double("zoom_min",
189                                                             "Zoom minimum",
190                                                             NULL,
191                                                             -1000.0,
192                                                             1000.0,
193                                                             PR_ZOOM_MIN,
194                                                             G_PARAM_READABLE | G_PARAM_WRITABLE));
195
196         g_object_class_install_property(gobject_class,
197                                         PROP_ZOOM_MAX,
198                                         g_param_spec_double("zoom_max",
199                                                             "Zoom maximum",
200                                                             NULL,
201                                                             -1000.0,
202                                                             1000.0,
203                                                             PR_ZOOM_MIN,
204                                                             G_PARAM_READABLE | G_PARAM_WRITABLE));
205
206         g_object_class_install_property(gobject_class,
207                                         PROP_ZOOM_QUALITY,
208                                         g_param_spec_uint("zoom_quality",
209                                                           "Zoom quality",
210                                                           NULL,
211                                                           GDK_INTERP_NEAREST,
212                                                           GDK_INTERP_HYPER,
213                                                           GDK_INTERP_BILINEAR,
214                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
215
216         g_object_class_install_property(gobject_class,
217                                         PROP_ZOOM_2PASS,
218                                         g_param_spec_boolean("zoom_2pass",
219                                                              "2 pass zoom",
220                                                              NULL,
221                                                              TRUE,
222                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
223
224         g_object_class_install_property(gobject_class,
225                                         PROP_ZOOM_EXPAND,
226                                         g_param_spec_boolean("zoom_expand",
227                                                              "Expand image in autozoom.",
228                                                              NULL,
229                                                              FALSE,
230                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
231
232         g_object_class_install_property(gobject_class,
233                                         PROP_DITHER_QUALITY,
234                                         g_param_spec_uint("dither_quality",
235                                                           "Dither quality",
236                                                           NULL,
237                                                           GDK_RGB_DITHER_NONE,
238                                                           GDK_RGB_DITHER_MAX,
239                                                           GDK_RGB_DITHER_NORMAL,
240                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
241
242         g_object_class_install_property(gobject_class,
243                                         PROP_SCROLL_RESET,
244                                         g_param_spec_uint("scroll_reset",
245                                                           "New image scroll reset",
246                                                           NULL,
247                                                           PR_SCROLL_RESET_TOPLEFT,
248                                                           PR_SCROLL_RESET_NOCHANGE,
249                                                           PR_SCROLL_RESET_TOPLEFT,
250                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
251
252         g_object_class_install_property(gobject_class,
253                                         PROP_DELAY_FLIP,
254                                         g_param_spec_boolean("delay_flip",
255                                                              "Delay image update",
256                                                              NULL,
257                                                              FALSE,
258                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
259
260         g_object_class_install_property(gobject_class,
261                                         PROP_LOADING,
262                                         g_param_spec_boolean("loading",
263                                                              "Image actively loading",
264                                                              NULL,
265                                                              FALSE,
266                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
267
268         g_object_class_install_property(gobject_class,
269                                         PROP_COMPLETE,
270                                         g_param_spec_boolean("complete",
271                                                              "Image rendering complete",
272                                                              NULL,
273                                                              FALSE,
274                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
275
276         g_object_class_install_property(gobject_class,
277                                         PROP_CACHE_SIZE_DISPLAY,
278                                         g_param_spec_uint("cache_display",
279                                                           "Display cache size MB",
280                                                           NULL,
281                                                           0,
282                                                           128,
283                                                           PR_CACHE_SIZE_DEFAULT,
284                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
285
286         g_object_class_install_property(gobject_class,
287                                         PROP_CACHE_SIZE_TILES,
288                                         g_param_spec_uint("cache_tiles",
289                                                           "Tile cache count",
290                                                           "Number of tiles to retain in memory at any one time.",
291                                                           0,
292                                                           256,
293                                                           PR_CACHE_SIZE_DEFAULT,
294                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
295
296         g_object_class_install_property(gobject_class,
297                                         PROP_WINDOW_FIT,
298                                         g_param_spec_boolean("window_fit",
299                                                              "Fit window to image size",
300                                                              NULL,
301                                                              FALSE,
302                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
303
304         g_object_class_install_property(gobject_class,
305                                         PROP_WINDOW_LIMIT,
306                                         g_param_spec_boolean("window_limit",
307                                                              "Limit size of parent window",
308                                                              NULL,
309                                                              FALSE,
310                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
311
312         g_object_class_install_property(gobject_class,
313                                         PROP_WINDOW_LIMIT_VALUE,
314                                         g_param_spec_uint("window_limit_value",
315                                                           "Size limit of parent window",
316                                                           NULL,
317                                                           10,
318                                                           150,
319                                                           100,
320                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
321
322         g_object_class_install_property(gobject_class,
323                                         PROP_AUTOFIT_LIMIT,
324                                         g_param_spec_boolean("autofit_limit",
325                                                              "Limit size of image when autofitting",
326                                                              NULL,
327                                                              FALSE,
328                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
329
330         g_object_class_install_property(gobject_class,
331                                         PROP_AUTOFIT_LIMIT_VALUE,
332                                         g_param_spec_uint("autofit_limit_value",
333                                                           "Size limit of image when autofitting",
334                                                           NULL,
335                                                           10,
336                                                           150,
337                                                           100,
338                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
339
340
341         signals[SIGNAL_ZOOM] =
342                 g_signal_new("zoom",
343                              G_OBJECT_CLASS_TYPE(gobject_class),
344                              G_SIGNAL_RUN_LAST,
345                              G_STRUCT_OFFSET(PixbufRendererClass, zoom),
346                              NULL, NULL,
347                              g_cclosure_marshal_VOID__DOUBLE,
348                              G_TYPE_NONE, 1,
349                              G_TYPE_DOUBLE);
350
351         signals[SIGNAL_CLICKED] =
352                 g_signal_new("clicked",
353                              G_OBJECT_CLASS_TYPE(gobject_class),
354                              G_SIGNAL_RUN_LAST,
355                              G_STRUCT_OFFSET(PixbufRendererClass, clicked),
356                              NULL, NULL,
357                              g_cclosure_marshal_VOID__BOXED,
358                              G_TYPE_NONE, 1,
359                              GDK_TYPE_EVENT);
360
361         signals[SIGNAL_SCROLL_NOTIFY] =
362                 g_signal_new("scroll-notify",
363                              G_OBJECT_CLASS_TYPE(gobject_class),
364                              G_SIGNAL_RUN_LAST,
365                              G_STRUCT_OFFSET(PixbufRendererClass, scroll_notify),
366                              NULL, NULL,
367                              g_cclosure_marshal_VOID__VOID,
368                              G_TYPE_NONE, 0);
369
370         signals[SIGNAL_RENDER_COMPLETE] =
371                 g_signal_new("render-complete",
372                              G_OBJECT_CLASS_TYPE(gobject_class),
373                              G_SIGNAL_RUN_LAST,
374                              G_STRUCT_OFFSET(PixbufRendererClass, render_complete),
375                              NULL, NULL,
376                              g_cclosure_marshal_VOID__VOID,
377                              G_TYPE_NONE, 0);
378
379         signals[SIGNAL_DRAG] =
380                 g_signal_new("drag",
381                              G_OBJECT_CLASS_TYPE(gobject_class),
382                              G_SIGNAL_RUN_LAST,
383                              G_STRUCT_OFFSET(PixbufRendererClass, drag),
384                              NULL, NULL,
385                              g_cclosure_marshal_VOID__BOXED,
386                              G_TYPE_NONE, 1,
387                              GDK_TYPE_EVENT);
388                              
389         signals[SIGNAL_UPDATE_PIXEL] =
390                 g_signal_new("update-pixel",
391                              G_OBJECT_CLASS_TYPE(gobject_class),
392                              G_SIGNAL_RUN_LAST,
393                              G_STRUCT_OFFSET(PixbufRendererClass, update_pixel),
394                              NULL, NULL,
395                              g_cclosure_marshal_VOID__VOID,
396                              G_TYPE_NONE, 0);
397 }
398
399 static void pixbuf_renderer_init(PixbufRenderer *pr)
400 {
401         GtkWidget *box;
402
403         box = GTK_WIDGET(pr);
404
405         pr->zoom_min = PR_ZOOM_MIN;
406         pr->zoom_max = PR_ZOOM_MAX;
407         pr->zoom_quality = GDK_INTERP_BILINEAR;
408         pr->zoom_2pass = FALSE;
409
410         pr->zoom = 1.0;
411         pr->scale = 1.0;
412         pr->aspect_ratio = 1.0;
413
414         pr->dither_quality = GDK_RGB_DITHER_NORMAL;
415
416         pr->scroll_reset = PR_SCROLL_RESET_TOPLEFT;
417
418         pr->scroller_id = 0;
419         pr->scroller_overlay = -1;
420         
421         pr->x_mouse = -1;
422         pr->y_mouse = -1;
423
424         pr->source_tiles_enabled = FALSE;
425         pr->source_tiles = NULL;
426
427         pr->orientation = 1;
428
429         pr->norm_center_x = 0.5;
430         pr->norm_center_y = 0.5;
431         
432         pr->stereo_mode = PR_STEREO_HORIZ;
433         
434         pr->renderer = (void *)renderer_tiles_new(pr, pr->stereo_mode);
435         
436         pr->renderer2 = (pr->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT)) ?
437                       (void *)renderer_tiles_new(pr, pr->stereo_mode | PR_STEREO_RIGHT) : NULL;
438
439         gtk_widget_set_double_buffered(box, FALSE);
440         g_signal_connect_after(G_OBJECT(box), "size_allocate",
441                                G_CALLBACK(pr_size_cb), pr);
442
443         pr_signals_connect(pr);
444 }
445
446 static void pixbuf_renderer_finalize(GObject *object)
447 {
448         PixbufRenderer *pr;
449
450         pr = PIXBUF_RENDERER(object);
451
452         pr->renderer->free(pr->renderer);
453         if (pr->renderer2) pr->renderer->free(pr->renderer2);
454
455
456         if (pr->pixbuf) g_object_unref(pr->pixbuf);
457
458         pr_scroller_timer_set(pr, FALSE);
459
460         pr_source_tile_free_all(pr);
461 }
462
463 PixbufRenderer *pixbuf_renderer_new(void)
464 {
465         return g_object_new(TYPE_PIXBUF_RENDERER, NULL);
466 }
467
468 static void pixbuf_renderer_set_property(GObject *object, guint prop_id,
469                                          const GValue *value, GParamSpec *pspec)
470 {
471         PixbufRenderer *pr;
472
473         pr = PIXBUF_RENDERER(object);
474
475         switch (prop_id)
476                 {
477                 case PROP_ZOOM_MIN:
478                         pr->zoom_min = g_value_get_double(value);
479                         break;
480                 case PROP_ZOOM_MAX:
481                         pr->zoom_max = g_value_get_double(value);
482                         break;
483                 case PROP_ZOOM_QUALITY:
484                         pr->zoom_quality = g_value_get_uint(value);
485                         break;
486                 case PROP_ZOOM_2PASS:
487                         pr->zoom_2pass = g_value_get_boolean(value);
488                         break;
489                 case PROP_ZOOM_EXPAND:
490                         pr->zoom_expand = g_value_get_boolean(value);
491                         break;
492                 case PROP_DITHER_QUALITY:
493                         pr->dither_quality = g_value_get_uint(value);
494                         break;
495                 case PROP_SCROLL_RESET:
496                         pr->scroll_reset = g_value_get_uint(value);
497                         break;
498                 case PROP_DELAY_FLIP:
499                         pr->delay_flip = g_value_get_boolean(value);
500                         break;
501                 case PROP_LOADING:
502                         pr->loading = g_value_get_boolean(value);
503                         break;
504                 case PROP_COMPLETE:
505                         pr->complete = g_value_get_boolean(value);
506                         break;
507                 case PROP_CACHE_SIZE_DISPLAY:
508 //                      pr->tile_cache_max = g_value_get_uint(value);
509                         break;
510                 case PROP_CACHE_SIZE_TILES:
511                         pr->source_tiles_cache_size = g_value_get_uint(value);
512                         break;
513                 case PROP_WINDOW_FIT:
514                         pr->window_fit = g_value_get_boolean(value);
515                         break;
516                 case PROP_WINDOW_LIMIT:
517                         pr->window_limit = g_value_get_boolean(value);
518                         break;
519                 case PROP_WINDOW_LIMIT_VALUE:
520                         pr->window_limit_size = g_value_get_uint(value);
521                         break;
522                 case PROP_AUTOFIT_LIMIT:
523                         pr->autofit_limit = g_value_get_boolean(value);
524                         break;
525                 case PROP_AUTOFIT_LIMIT_VALUE:
526                         pr->autofit_limit_size = g_value_get_uint(value);
527                         break;
528                 default:
529                         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
530                         break;
531                 }
532 }
533
534 static void pixbuf_renderer_get_property(GObject *object, guint prop_id,
535                                          GValue *value, GParamSpec *pspec)
536 {
537         PixbufRenderer *pr;
538
539         pr = PIXBUF_RENDERER(object);
540
541         switch (prop_id)
542                 {
543                 case PROP_ZOOM_MIN:
544                         g_value_set_double(value, pr->zoom_min);
545                         break;
546                 case PROP_ZOOM_MAX:
547                         g_value_set_double(value, pr->zoom_max);
548                         break;
549                 case PROP_ZOOM_QUALITY:
550                         g_value_set_uint(value, pr->zoom_quality);
551                         break;
552                 case PROP_ZOOM_2PASS:
553                         g_value_set_boolean(value, pr->zoom_2pass);
554                         break;
555                 case PROP_ZOOM_EXPAND:
556                         g_value_set_boolean(value, pr->zoom_expand);
557                         break;
558                 case PROP_DITHER_QUALITY:
559                         g_value_set_uint(value, pr->dither_quality);
560                         break;
561                 case PROP_SCROLL_RESET:
562                         g_value_set_uint(value, pr->scroll_reset);
563                         break;
564                 case PROP_DELAY_FLIP:
565                         g_value_set_boolean(value, pr->delay_flip);
566                         break;
567                 case PROP_LOADING:
568                         g_value_set_boolean(value, pr->loading);
569                         break;
570                 case PROP_COMPLETE:
571                         g_value_set_boolean(value, pr->complete);
572                         break;
573                 case PROP_CACHE_SIZE_DISPLAY:
574 //                      g_value_set_uint(value, pr->tile_cache_max);
575                         break;
576                 case PROP_CACHE_SIZE_TILES:
577                         g_value_set_uint(value, pr->source_tiles_cache_size);
578                         break;
579                 case PROP_WINDOW_FIT:
580                         g_value_set_boolean(value, pr->window_fit);
581                         break;
582                 case PROP_WINDOW_LIMIT:
583                         g_value_set_boolean(value, pr->window_limit);
584                         break;
585                 case PROP_WINDOW_LIMIT_VALUE:
586                         g_value_set_uint(value, pr->window_limit_size);
587                         break;
588                 case PROP_AUTOFIT_LIMIT:
589                         g_value_set_boolean(value, pr->autofit_limit);
590                         break;
591                 case PROP_AUTOFIT_LIMIT_VALUE:
592                         g_value_set_uint(value, pr->autofit_limit_size);
593                         break;
594                 default:
595                         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
596                         break;
597                 }
598 }
599
600 static gboolean pixbuf_renderer_expose(GtkWidget *widget, GdkEventExpose *event)
601 {
602 #if GTK_CHECK_VERSION(2,20,0)
603         if (gtk_widget_is_drawable(widget))
604 #else
605         if (GTK_WIDGET_DRAWABLE(widget))
606 #endif
607                 {
608 #if GTK_CHECK_VERSION(2,20,0)
609                 if (gtk_widget_get_has_window(widget))
610 #else
611                 if (!GTK_WIDGET_NO_WINDOW(widget))
612 #endif
613                         {
614                         if (event->window != widget->window)
615                                 {
616                                 GdkRectangle area;
617
618                                 gdk_window_get_position(event->window, &area.x, &area.y);
619
620                                 area.x += event->area.x;
621                                 area.y += event->area.y;
622                                 area.width = event->area.width;
623                                 area.height = event->area.height;
624                                 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &area);
625                                 }
626                         else
627                                 {
628                                 pixbuf_renderer_paint(PIXBUF_RENDERER(widget), &event->area);
629                                 }
630                         }
631                 }
632
633         return FALSE;
634 }
635
636 /*
637  *-------------------------------------------------------------------
638  * misc utilities
639  *-------------------------------------------------------------------
640  */
641
642 static void widget_set_cursor(GtkWidget *widget, gint icon)
643 {
644         GdkCursor *cursor;
645
646         if (!widget->window) return;
647
648         if (icon == -1)
649                 {
650                 cursor = NULL;
651                 }
652         else
653                 {
654                 cursor = gdk_cursor_new(icon);
655                 }
656
657         gdk_window_set_cursor(widget->window, cursor);
658
659         if (cursor) gdk_cursor_unref(cursor);
660 }
661
662 gboolean pr_clip_region(gint x, gint y, gint w, gint h,
663                                gint clip_x, gint clip_y, gint clip_w, gint clip_h,
664                                gint *rx, gint *ry, gint *rw, gint *rh)
665 {
666         if (clip_x + clip_w <= x ||
667             clip_x >= x + w ||
668             clip_y + clip_h <= y ||
669             clip_y >= y + h)
670                 {
671                 return FALSE;
672                 }
673
674         *rx = MAX(x, clip_x);
675         *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
676
677         *ry = MAX(y, clip_y);
678         *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
679
680         return TRUE;
681 }
682
683 static gboolean pr_parent_window_sizable(PixbufRenderer *pr)
684 {
685         GdkWindowState state;
686
687         if (!pr->parent_window) return FALSE;
688         if (!pr->window_fit) return FALSE;
689         if (!GTK_WIDGET(pr)->window) return FALSE;
690
691         if (!pr->parent_window->window) return FALSE;
692         state = gdk_window_get_state(pr->parent_window->window);
693         if (state & GDK_WINDOW_STATE_MAXIMIZED) return FALSE;
694
695         return TRUE;
696 }
697
698 static gboolean pr_parent_window_resize(PixbufRenderer *pr, gint w, gint h)
699 {
700         GtkWidget *widget;
701         GtkWidget *parent;
702         gint ww, wh;
703
704         if (!pr_parent_window_sizable(pr)) return FALSE;
705
706         if (pr->window_limit)
707                 {
708                 gint sw = gdk_screen_width() * pr->window_limit_size / 100;
709                 gint sh = gdk_screen_height() * pr->window_limit_size / 100;
710
711                 if (w > sw) w = sw;
712                 if (h > sh) h = sh;
713                 }
714
715         widget = GTK_WIDGET(pr);
716         parent = GTK_WIDGET(pr->parent_window);
717
718         w += (parent->allocation.width - widget->allocation.width);
719         h += (parent->allocation.height - widget->allocation.height);
720
721         gdk_drawable_get_size(parent->window, &ww, &wh);
722         if (w == ww && h == wh) return FALSE;
723
724         gdk_window_resize(parent->window, w, h);
725
726         return TRUE;
727 }
728
729 void pixbuf_renderer_set_parent(PixbufRenderer *pr, GtkWindow *window)
730 {
731         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
732         g_return_if_fail(window == NULL || GTK_IS_WINDOW(window));
733
734         pr->parent_window = GTK_WIDGET(window);
735 }
736
737 GtkWindow *pixbuf_renderer_get_parent(PixbufRenderer *pr)
738 {
739         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
740
741         return GTK_WINDOW(pr->parent_window);
742 }
743
744
745 /*
746  *-------------------------------------------------------------------
747  * overlays
748  *-------------------------------------------------------------------
749  */
750
751
752 gint pixbuf_renderer_overlay_add(PixbufRenderer *pr, GdkPixbuf *pixbuf, gint x, gint y,
753                                  OverlayRendererFlags flags)
754 {
755         /* let's assume both renderers returns the same value */
756         if (pr->renderer2) pr->renderer2->overlay_add(pr->renderer2, pixbuf, x, y, flags);
757         return pr->renderer->overlay_add(pr->renderer, pixbuf, x, y, flags);
758 }
759
760 void pixbuf_renderer_overlay_set(PixbufRenderer *pr, gint id, GdkPixbuf *pixbuf, gint x, gint y)
761 {
762         pr->renderer->overlay_set(pr->renderer, id, pixbuf, x, y);
763         if (pr->renderer2) pr->renderer2->overlay_set(pr->renderer2, id, pixbuf, x, y);
764 }
765
766 gboolean pixbuf_renderer_overlay_get(PixbufRenderer *pr, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
767 {
768         if (pr->renderer2) pr->renderer2->overlay_get(pr->renderer2, id, pixbuf, x, y);
769         return pr->renderer->overlay_get(pr->renderer, id, pixbuf, x, y);
770 }
771
772 void pixbuf_renderer_overlay_remove(PixbufRenderer *pr, gint id)
773 {
774         pr->renderer->overlay_set(pr->renderer, id, NULL, 0, 0);
775         if (pr->renderer2) pr->renderer2->overlay_set(pr->renderer2, id, NULL, 0, 0);
776 }
777
778 /*
779  *-------------------------------------------------------------------
780  * scroller overlay
781  *-------------------------------------------------------------------
782  */
783
784
785 static gboolean pr_scroller_update_cb(gpointer data)
786 {
787         PixbufRenderer *pr = data;
788         gint x, y;
789         gint xinc, yinc;
790
791         /* this was a simple scroll by difference between scroller and mouse position,
792          * but all this math results in a smoother result and accounts for a dead zone.
793          */
794
795         if (abs(pr->scroller_xpos - pr->scroller_x) < PR_SCROLLER_DEAD_ZONE)
796                 {
797                 x = 0;
798                 }
799         else
800                 {
801                 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
802                 x = (pr->scroller_xpos - pr->scroller_x) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
803                 x += (x > 0) ? -shift : shift;
804                 }
805
806         if (abs(pr->scroller_ypos - pr->scroller_y) < PR_SCROLLER_DEAD_ZONE)
807                 {
808                 y = 0;
809                 }
810         else
811                 {
812                 gint shift = PR_SCROLLER_DEAD_ZONE / 2 * PR_SCROLLER_UPDATES_PER_SEC;
813                 y = (pr->scroller_ypos - pr->scroller_y) / 2 * PR_SCROLLER_UPDATES_PER_SEC;
814                 y += (y > 0) ? -shift : shift;
815                 }
816
817         if (abs(x) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
818                 {
819                 xinc = x;
820                 }
821         else
822                 {
823                 xinc = pr->scroller_xinc;
824
825                 if (x >= 0)
826                         {
827                         if (xinc < 0) xinc = 0;
828                         if (x < xinc) xinc = x;
829                         if (x > xinc) xinc = MIN(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
830                         }
831                 else
832                         {
833                         if (xinc > 0) xinc = 0;
834                         if (x > xinc) xinc = x;
835                         if (x < xinc) xinc = MAX(xinc + x / PR_SCROLLER_UPDATES_PER_SEC, x);
836                         }
837                 }
838
839         if (abs(y) < PR_SCROLLER_DEAD_ZONE * PR_SCROLLER_UPDATES_PER_SEC)
840                 {
841                 yinc = y;
842                 }
843         else
844                 {
845                 yinc = pr->scroller_yinc;
846
847                 if (y >= 0)
848                         {
849                         if (yinc < 0) yinc = 0;
850                         if (y < yinc) yinc = y;
851                         if (y > yinc) yinc = MIN(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
852                         }
853                 else
854                         {
855                         if (yinc > 0) yinc = 0;
856                         if (y > yinc) yinc = y;
857                         if (y < yinc) yinc = MAX(yinc + y / PR_SCROLLER_UPDATES_PER_SEC, y);
858                         }
859                 }
860
861         pr->scroller_xinc = xinc;
862         pr->scroller_yinc = yinc;
863
864         xinc = xinc / PR_SCROLLER_UPDATES_PER_SEC;
865         yinc = yinc / PR_SCROLLER_UPDATES_PER_SEC;
866
867         pixbuf_renderer_scroll(pr, xinc, yinc);
868
869         return TRUE;
870 }
871
872 static void pr_scroller_timer_set(PixbufRenderer *pr, gboolean start)
873 {
874         if (pr->scroller_id)
875                 {
876                 g_source_remove(pr->scroller_id);
877                 pr->scroller_id = 0;
878                 }
879
880         if (start)
881                 {
882                 pr->scroller_id = g_timeout_add(1000 / PR_SCROLLER_UPDATES_PER_SEC,
883                                                 pr_scroller_update_cb, pr);
884                 }
885 }
886
887 static void pr_scroller_start(PixbufRenderer *pr, gint x, gint y)
888 {
889         if (pr->scroller_overlay == -1)
890                 {
891                 GdkPixbuf *pixbuf;
892                 gint w, h;
893
894 #ifdef GQ_BUILD
895                 pixbuf = pixbuf_inline(PIXBUF_INLINE_SCROLLER);
896 #else
897                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 32, 32);
898                 gdk_pixbuf_fill(pixbuf, 0x000000ff);
899 #endif
900                 w = gdk_pixbuf_get_width(pixbuf);
901                 h = gdk_pixbuf_get_height(pixbuf);
902
903                 pr->scroller_overlay = pixbuf_renderer_overlay_add(pr, pixbuf, x - w / 2, y - h / 2, OVL_NORMAL);
904                 g_object_unref(pixbuf);
905                 }
906
907         pr->scroller_x = x;
908         pr->scroller_y = y;
909         pr->scroller_xpos = x;
910         pr->scroller_ypos = y;
911
912         pr_scroller_timer_set(pr, TRUE);
913 }
914
915 static void pr_scroller_stop(PixbufRenderer *pr)
916 {
917         if (!pr->scroller_id) return;
918
919         pixbuf_renderer_overlay_remove(pr, pr->scroller_overlay);
920         pr->scroller_overlay = -1;
921
922         pr_scroller_timer_set(pr, FALSE);
923 }
924
925 /*
926  *-------------------------------------------------------------------
927  * borders
928  *-------------------------------------------------------------------
929  */
930
931 static void pr_border_clear(PixbufRenderer *pr)
932 {
933         pr->renderer->border_draw(pr->renderer, 0, 0, pr->viewport_width, pr->viewport_height);
934         if (pr->renderer2) pr->renderer2->border_draw(pr->renderer2, 0, 0, pr->viewport_width, pr->viewport_height);
935 }
936
937 void pixbuf_renderer_set_color(PixbufRenderer *pr, GdkColor *color)
938 {
939         GtkStyle *style;
940         GtkWidget *widget;
941
942         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
943
944         widget = GTK_WIDGET(pr);
945
946         if (color) {
947                 GdkColor *slot;
948
949                 style = gtk_style_copy(gtk_widget_get_style(widget));
950                 slot = &style->bg[GTK_STATE_NORMAL];
951
952                 slot->red = color->red;
953                 slot->green = color->green;
954                 slot->blue = color->blue;
955                 }
956         else {
957                 style = gtk_style_copy(gtk_widget_get_default_style());
958         }
959
960         gtk_widget_set_style(widget, style);
961
962 #if GTK_CHECK_VERSION(2,20,0)
963         if (gtk_widget_get_visible(widget)) pr_border_clear(pr);
964 #else
965         if (GTK_WIDGET_VISIBLE(widget)) pr_border_clear(pr);
966 #endif
967 }
968
969 static void pr_redraw(PixbufRenderer *pr, gboolean new_data)
970 {
971         pr->renderer->queue_clear(pr->renderer);
972         pr->renderer->queue(pr->renderer, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data, FALSE);
973         if (pr->renderer2) {
974                 pr->renderer2->queue_clear(pr->renderer2);
975                 pr->renderer2->queue(pr->renderer2, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, new_data, FALSE);
976         }
977 }
978
979 /*
980  *-------------------------------------------------------------------
981  * source tiles
982  *-------------------------------------------------------------------
983  */
984
985 static void pr_source_tile_free(SourceTile *st)
986 {
987         if (!st) return;
988
989         if (st->pixbuf) g_object_unref(st->pixbuf);
990         g_free(st);
991 }
992
993 static void pr_source_tile_free_all(PixbufRenderer *pr)
994 {
995         GList *work;
996
997         work = pr->source_tiles;
998         while (work)
999                 {
1000                 SourceTile *st;
1001
1002                 st = work->data;
1003                 work = work->next;
1004
1005                 pr_source_tile_free(st);
1006                 }
1007
1008         g_list_free(pr->source_tiles);
1009         pr->source_tiles = NULL;
1010 }
1011
1012 static void pr_source_tile_unset(PixbufRenderer *pr)
1013 {
1014         pr_source_tile_free_all(pr);
1015         pr->source_tiles_enabled = FALSE;
1016 }
1017
1018 static gboolean pr_source_tile_visible(PixbufRenderer *pr, SourceTile *st)
1019 {
1020         gint x1, y1, x2, y2;
1021
1022         if (!st) return FALSE;
1023
1024 //      x1 = ROUND_DOWN(pr->x_scroll, pr->tile_width);
1025 //      y1 = ROUND_DOWN(pr->y_scroll, pr->tile_height);
1026 //      x2 = ROUND_UP(pr->x_scroll + pr->vis_width, pr->tile_width);
1027 //      y2 = ROUND_UP(pr->y_scroll + pr->vis_height, pr->tile_height);
1028         x1 = pr->x_scroll;
1029         y1 = pr->y_scroll;
1030         x2 = pr->x_scroll + pr->vis_width;
1031         y2 = pr->y_scroll + pr->vis_height;
1032
1033         return !((gdouble)st->x * pr->scale > (gdouble)x2 ||
1034                  (gdouble)(st->x + pr->source_tile_width) * pr->scale < (gdouble)x1 ||
1035                  (gdouble)st->y * pr->scale > (gdouble)y2 ||
1036                  (gdouble)(st->y + pr->source_tile_height) * pr->scale < (gdouble)y1);
1037 }
1038
1039 static SourceTile *pr_source_tile_new(PixbufRenderer *pr, gint x, gint y)
1040 {
1041         SourceTile *st = NULL;
1042         gint count;
1043
1044         g_return_val_if_fail(pr->source_tile_width >= 1 && pr->source_tile_height >= 1, NULL);
1045
1046         if (pr->source_tiles_cache_size < 4) pr->source_tiles_cache_size = 4;
1047
1048         count = g_list_length(pr->source_tiles);
1049         if (count >= pr->source_tiles_cache_size)
1050                 {
1051                 GList *work;
1052
1053                 work = g_list_last(pr->source_tiles);
1054                 while (work && count >= pr->source_tiles_cache_size)
1055                         {
1056                         SourceTile *needle;
1057
1058                         needle = work->data;
1059                         work = work->prev;
1060
1061                         if (!pr_source_tile_visible(pr, needle))
1062                                 {
1063                                 pr->source_tiles = g_list_remove(pr->source_tiles, needle);
1064
1065                                 if (pr->func_tile_dispose)
1066                                         {
1067                                         pr->func_tile_dispose(pr, needle->x, needle->y,
1068                                                               pr->source_tile_width, pr->source_tile_height,
1069                                                               needle->pixbuf, pr->func_tile_data);
1070                                         }
1071
1072                                 if (!st)
1073                                         {
1074                                         st = needle;
1075                                         }
1076                                 else
1077                                         {
1078                                         pr_source_tile_free(needle);
1079                                         }
1080
1081                                 count--;
1082                                 }
1083                         }
1084                 }
1085
1086         if (!st)
1087                 {
1088                 st = g_new0(SourceTile, 1);
1089                 st->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
1090                                             pr->source_tile_width, pr->source_tile_height);
1091                 }
1092
1093         st->x = ROUND_DOWN(x, pr->source_tile_width);
1094         st->y = ROUND_DOWN(y, pr->source_tile_height);
1095         st->blank = TRUE;
1096
1097         pr->source_tiles = g_list_prepend(pr->source_tiles, st);
1098
1099         return st;
1100 }
1101
1102 static SourceTile *pr_source_tile_request(PixbufRenderer *pr, gint x, gint y)
1103 {
1104         SourceTile *st;
1105
1106         st = pr_source_tile_new(pr, x, y);
1107         if (!st) return NULL;
1108
1109         if (pr->func_tile_request &&
1110             pr->func_tile_request(pr, st->x, st->y,
1111                                    pr->source_tile_width, pr->source_tile_height, st->pixbuf, pr->func_tile_data))
1112                 {
1113                 st->blank = FALSE;
1114                 }
1115
1116         pr->renderer->invalidate_region(pr->renderer, st->x * pr->scale, st->y * pr->scale,
1117                                   pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1118         if (pr->renderer2) pr->renderer2->invalidate_region(pr->renderer2, st->x * pr->scale, st->y * pr->scale,
1119                                   pr->source_tile_width * pr->scale, pr->source_tile_height * pr->scale);
1120         return st;
1121 }
1122
1123 static SourceTile *pr_source_tile_find(PixbufRenderer *pr, gint x, gint y)
1124 {
1125         GList *work;
1126
1127         work = pr->source_tiles;
1128         while (work)
1129                 {
1130                 SourceTile *st = work->data;
1131
1132                 if (x >= st->x && x < st->x + pr->source_tile_width &&
1133                     y >= st->y && y < st->y + pr->source_tile_height)
1134                         {
1135                         if (work != pr->source_tiles)
1136                                 {
1137                                 pr->source_tiles = g_list_remove_link(pr->source_tiles, work);
1138                                 pr->source_tiles = g_list_concat(work, pr->source_tiles);
1139                                 }
1140                         return st;
1141                         }
1142
1143                 work = work->next;
1144                 }
1145
1146         return NULL;
1147 }
1148
1149 GList *pr_source_tile_compute_region(PixbufRenderer *pr, gint x, gint y, gint w, gint h, gboolean request)
1150 {
1151         gint x1, y1;
1152         GList *list = NULL;
1153         gint sx, sy;
1154
1155         if (x < 0) x = 0;
1156         if (y < 0) y = 0;
1157         if (w > pr->image_width) w = pr->image_width;
1158         if (h > pr->image_height) h = pr->image_height;
1159
1160         sx = ROUND_DOWN(x, pr->source_tile_width);
1161         sy = ROUND_DOWN(y, pr->source_tile_height);
1162
1163         for (x1 = sx; x1 < x + w; x1+= pr->source_tile_width)
1164                 {
1165                 for (y1 = sy; y1 < y + h; y1 += pr->source_tile_height)
1166                         {
1167                         SourceTile *st;
1168
1169                         st = pr_source_tile_find(pr, x1, y1);
1170                         if (!st && request) st = pr_source_tile_request(pr, x1, y1);
1171
1172                         if (st) list = g_list_prepend(list, st);
1173                         }
1174                 }
1175
1176         return g_list_reverse(list);
1177 }
1178
1179 static void pr_source_tile_changed(PixbufRenderer *pr, gint x, gint y, gint width, gint height)
1180 {
1181         GList *work;
1182
1183         if (width < 1 || height < 1) return;
1184
1185         work = pr->source_tiles;
1186         while (work)
1187                 {
1188                 SourceTile *st;
1189                 gint rx, ry, rw, rh;
1190
1191                 st = work->data;
1192                 work = work->next;
1193
1194                 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1195                                    x, y, width, height,
1196                                    &rx, &ry, &rw, &rh))
1197                         {
1198                         GdkPixbuf *pixbuf;
1199
1200                         pixbuf = gdk_pixbuf_new_subpixbuf(st->pixbuf, rx - st->x, ry - st->y, rw, rh);
1201                         if (pr->func_tile_request &&
1202                             pr->func_tile_request(pr, rx, ry, rw, rh, pixbuf, pr->func_tile_data))
1203                                 {
1204                                         pr->renderer->invalidate_region(pr->renderer, rx * pr->scale, ry * pr->scale,
1205                                                               rw * pr->scale, rh * pr->scale);
1206                                         if (pr->renderer2) pr->renderer2->invalidate_region(pr->renderer2, rx * pr->scale, ry * pr->scale,
1207                                                                 rw * pr->scale, rh * pr->scale);
1208                                 }
1209                         g_object_unref(pixbuf);
1210                         }
1211                 }
1212 }
1213
1214 void pixbuf_renderer_set_tiles(PixbufRenderer *pr, gint width, gint height,
1215                                gint tile_width, gint tile_height, gint cache_size,
1216                                PixbufRendererTileRequestFunc func_request,
1217                                PixbufRendererTileDisposeFunc func_dispose,
1218                                gpointer user_data,
1219                                gdouble zoom)
1220 {
1221         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1222         g_return_if_fail(tile_width >= 32 && tile_width >= 32);
1223         g_return_if_fail(width >= 32 && height > 32);
1224         g_return_if_fail(func_request != NULL);
1225
1226         if (pr->pixbuf) g_object_unref(pr->pixbuf);
1227         pr->pixbuf = NULL;
1228
1229         pr_source_tile_unset(pr);
1230
1231         if (cache_size < 4) cache_size = 4;
1232
1233         pr->source_tiles_enabled = TRUE;
1234         pr->source_tiles_cache_size = cache_size;
1235         pr->source_tile_width = tile_width;
1236         pr->source_tile_height = tile_height;
1237
1238         pr->image_width = width;
1239         pr->image_height = height;
1240
1241         pr->func_tile_request = func_request;
1242         pr->func_tile_dispose = func_dispose;
1243         pr->func_tile_data = user_data;
1244
1245         pr_zoom_sync(pr, zoom, PR_ZOOM_FORCE | PR_ZOOM_NEW, 0, 0);
1246         pr_redraw(pr, TRUE);
1247 }
1248
1249 void pixbuf_renderer_set_tiles_size(PixbufRenderer *pr, gint width, gint height)
1250 {
1251         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1252         g_return_if_fail(width >= 32 && height > 32);
1253
1254         if (!pr->source_tiles_enabled) return;
1255         if (pr->image_width == width && pr->image_height == height) return;
1256
1257         pr->image_width = width;
1258         pr->image_height = height;
1259
1260         pr_zoom_sync(pr, pr->zoom, PR_ZOOM_FORCE, 0, 0);
1261 }
1262
1263 gint pixbuf_renderer_get_tiles(PixbufRenderer *pr)
1264 {
1265         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
1266
1267         return pr->source_tiles_enabled;
1268 }
1269
1270 static void pr_zoom_adjust_real(PixbufRenderer *pr, gdouble increment,
1271                                 PrZoomFlags flags, gint x, gint y)
1272 {
1273         gdouble zoom = pr->zoom;
1274
1275         if (increment == 0.0) return;
1276
1277         if (zoom == 0.0)
1278                 {
1279                 if (pr->scale < 1.0)
1280                         {
1281                         zoom = 0.0 - 1.0 / pr->scale;
1282                         }
1283                 else
1284                         {
1285                         zoom = pr->scale;
1286                         }
1287                 }
1288
1289         if (increment < 0.0)
1290                 {
1291                 if (zoom >= 1.0 && zoom + increment < 1.0)
1292                         {
1293                         zoom = zoom + increment - 2.0;
1294                         }
1295                 else
1296                         {
1297                         zoom = zoom + increment;
1298                         }
1299                 }
1300         else
1301                 {
1302                 if (zoom <= -1.0 && zoom + increment > -1.0)
1303                         {
1304                         zoom = zoom + increment + 2.0;
1305                         }
1306                 else
1307                         {
1308                         zoom = zoom + increment;
1309                         }
1310                 }
1311
1312         pr_zoom_sync(pr, zoom, flags, x, y);
1313 }
1314
1315
1316 /*
1317  *-------------------------------------------------------------------
1318  * signal emission
1319  *-------------------------------------------------------------------
1320  */
1321
1322 static void pr_update_signal(PixbufRenderer *pr)
1323 {
1324 #if 0
1325         log_printf("FIXME: send updated signal\n");
1326 #endif
1327         DEBUG_1("%s pixbuf renderer updated - started drawing %p, img: %dx%d", get_exec_time(), pr, pr->image_width, pr->image_height);
1328         pr->debug_updated = TRUE;
1329 }
1330
1331 static void pr_zoom_signal(PixbufRenderer *pr)
1332 {
1333         g_signal_emit(pr, signals[SIGNAL_ZOOM], 0, pr->zoom);
1334 }
1335
1336 static void pr_clicked_signal(PixbufRenderer *pr, GdkEventButton *bevent)
1337 {
1338         g_signal_emit(pr, signals[SIGNAL_CLICKED], 0, bevent);
1339 }
1340
1341 static void pr_scroll_notify_signal(PixbufRenderer *pr)
1342 {
1343         g_signal_emit(pr, signals[SIGNAL_SCROLL_NOTIFY], 0);
1344 }
1345
1346 void pr_render_complete_signal(PixbufRenderer *pr)
1347 {
1348         if (!pr->complete)
1349                 {
1350                 g_signal_emit(pr, signals[SIGNAL_RENDER_COMPLETE], 0);
1351                 g_object_set(G_OBJECT(pr), "complete", TRUE, NULL);
1352                 }
1353         if (pr->debug_updated)
1354                 {
1355                 DEBUG_1("%s pixbuf renderer done %p", get_exec_time(), pr);
1356                 pr->debug_updated = FALSE;
1357                 }
1358 }
1359
1360 static void pr_drag_signal(PixbufRenderer *pr, GdkEventButton *bevent)
1361 {
1362         g_signal_emit(pr, signals[SIGNAL_DRAG], 0, bevent);
1363 }
1364
1365 static void pr_update_pixel_signal(PixbufRenderer *pr)
1366 {
1367         g_signal_emit(pr, signals[SIGNAL_UPDATE_PIXEL], 0);
1368 }
1369
1370 /*
1371  *-------------------------------------------------------------------
1372  * sync and clamp
1373  *-------------------------------------------------------------------
1374  */
1375
1376
1377 void pr_tile_coords_map_orientation(gint orientation,
1378                                      gdouble tile_x, gdouble tile_y, /* coordinates of the tile */
1379                                      gdouble image_w, gdouble image_h,
1380                                      gdouble tile_w, gdouble tile_h,
1381                                      gdouble *res_x, gdouble *res_y)
1382 {
1383         *res_x = tile_x;
1384         *res_y = tile_y;
1385         switch (orientation)
1386                 {
1387                 case EXIF_ORIENTATION_TOP_LEFT:
1388                         /* normal -- nothing to do */
1389                         break;
1390                 case EXIF_ORIENTATION_TOP_RIGHT:
1391                         /* mirrored */
1392                         *res_x = image_w - tile_x - tile_w;
1393                         break;
1394                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1395                         /* upside down */
1396                         *res_x = image_w - tile_x - tile_w;
1397                         *res_y = image_h - tile_y - tile_h;
1398                         break;
1399                 case EXIF_ORIENTATION_BOTTOM_LEFT:
1400                         /* flipped */
1401                         *res_y = image_h - tile_y - tile_h;
1402                         break;
1403                 case EXIF_ORIENTATION_LEFT_TOP:
1404                         *res_x = tile_y;
1405                         *res_y = tile_x;
1406                         break;
1407                 case EXIF_ORIENTATION_RIGHT_TOP:
1408                         /* rotated -90 (270) */
1409                         *res_x = tile_y;
1410                         *res_y = image_w - tile_x - tile_w;
1411                         break;
1412                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1413                         *res_x = image_h - tile_y - tile_h;
1414                         *res_y = image_w - tile_x - tile_w;
1415                         break;
1416                 case EXIF_ORIENTATION_LEFT_BOTTOM:
1417                         /* rotated 90 */
1418                         *res_x = image_h - tile_y - tile_h;
1419                         *res_y = tile_x;
1420                         break;
1421                 default:
1422                         /* The other values are out of range */
1423                         break;
1424                 }
1425 //      log_printf("tile coord y:%f, ih:%d, th:%f ry:%f\n", tile_y, image_h, tile_h, *res_x);
1426 }
1427
1428 void pr_tile_region_map_orientation(gint orientation,
1429                                      gint area_x, gint area_y, /* coordinates of the area inside tile */
1430                                      gint tile_w, gint tile_h,
1431                                      gint area_w, gint area_h,
1432                                      gint *res_x, gint *res_y,
1433                                      gint *res_w, gint *res_h)
1434 {
1435         *res_x = area_x;
1436         *res_y = area_y;
1437         *res_w = area_w;
1438         *res_h = area_h;
1439
1440         switch (orientation)
1441                 {
1442                 case EXIF_ORIENTATION_TOP_LEFT:
1443                         /* normal -- nothing to do */
1444                         break;
1445                 case EXIF_ORIENTATION_TOP_RIGHT:
1446                         /* mirrored */
1447                         *res_x = tile_w - area_x - area_w;
1448                         break;
1449                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1450                         /* upside down */
1451                         *res_x = tile_w - area_x - area_w;
1452                         *res_y = tile_h - area_y - area_h;
1453                         break;
1454                 case EXIF_ORIENTATION_BOTTOM_LEFT:
1455                         /* flipped */
1456                         *res_y = tile_h - area_y - area_h;
1457                         break;
1458                 case EXIF_ORIENTATION_LEFT_TOP:
1459                         *res_x = area_y;
1460                         *res_y = area_x;
1461                         *res_w = area_h;
1462                         *res_h = area_w;
1463                         break;
1464                 case EXIF_ORIENTATION_RIGHT_TOP:
1465                         /* rotated -90 (270) */
1466                         *res_x = area_y;
1467                         *res_y = tile_w - area_x - area_w;
1468                         *res_w = area_h;
1469                         *res_h = area_w;
1470                         break;
1471                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1472                         *res_x = tile_h - area_y - area_h;
1473                         *res_y = tile_w - area_x - area_w;
1474                         *res_w = area_h;
1475                         *res_h = area_w;
1476                         break;
1477                 case EXIF_ORIENTATION_LEFT_BOTTOM:
1478                         /* rotated 90 */
1479                         *res_x = tile_h - area_y - area_h;
1480                         *res_y = area_x;
1481                         *res_w = area_h;
1482                         *res_h = area_w;
1483                         break;
1484                 default:
1485                         /* The other values are out of range */
1486                         break;
1487                 }
1488 //      log_printf("inside y:%d, th:%d, ah:%d ry:%d\n", area_y, tile_h, area_h, *res_x);
1489 }
1490
1491 void pr_coords_map_orientation_reverse(gint orientation,
1492                                      gint area_x, gint area_y,
1493                                      gint tile_w, gint tile_h,
1494                                      gint area_w, gint area_h,
1495                                      gint *res_x, gint *res_y,
1496                                      gint *res_w, gint *res_h)
1497 {
1498         *res_x = area_x;
1499         *res_y = area_y;
1500         *res_w = area_w;
1501         *res_h = area_h;
1502
1503         switch (orientation)
1504                 {
1505                 case EXIF_ORIENTATION_TOP_LEFT:
1506                         /* normal -- nothing to do */
1507                         break;
1508                 case EXIF_ORIENTATION_TOP_RIGHT:
1509                         /* mirrored */
1510                         *res_x = tile_w - area_x - area_w;
1511                         break;
1512                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1513                         /* upside down */
1514                         *res_x = tile_w - area_x - area_w;
1515                         *res_y = tile_h - area_y - area_h;
1516                         break;
1517                 case EXIF_ORIENTATION_BOTTOM_LEFT:
1518                         /* flipped */
1519                         *res_y = tile_h - area_y - area_h;
1520                         break;
1521                 case EXIF_ORIENTATION_LEFT_TOP:
1522                         *res_x = area_y;
1523                         *res_y = area_x;
1524                         *res_w = area_h;
1525                         *res_h = area_w;
1526                         break;
1527                 case EXIF_ORIENTATION_RIGHT_TOP:
1528                         /* rotated -90 (270) */
1529                         *res_x = tile_w - area_y - area_h;
1530                         *res_y = area_x;
1531                         *res_w = area_h;
1532                         *res_h = area_w;
1533                         break;
1534                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1535                         *res_x = tile_w - area_y - area_h;
1536                         *res_y = tile_h - area_x - area_w;
1537                         *res_w = area_h;
1538                         *res_h = area_w;
1539                         break;
1540                 case EXIF_ORIENTATION_LEFT_BOTTOM:
1541                         /* rotated 90 */
1542                         *res_x = area_y;
1543                         *res_y = tile_h - area_x - area_w;
1544                         *res_w = area_h;
1545                         *res_h = area_w;
1546                         break;
1547                 default:
1548                         /* The other values are out of range */
1549                         break;
1550                 }
1551 }
1552
1553
1554
1555 static void pixbuf_renderer_sync_scroll_center(PixbufRenderer *pr)
1556 {
1557         gint src_x, src_y;
1558         if (!pr->width || !pr->height) return;
1559
1560         /* 
1561          * Update norm_center only if the image is bigger than the window.
1562          * With this condition the stored center survives also a temporary display
1563          * of the "broken image" icon.
1564         */
1565
1566         if (pr->width > pr->viewport_width)
1567                 {
1568                 src_x = pr->x_scroll + pr->vis_width / 2;
1569                 pr->norm_center_x = (gdouble)src_x / pr->width;
1570                 }
1571         
1572         if (pr->height > pr->viewport_height)
1573                 {
1574                 src_y = pr->y_scroll + pr->vis_height / 2;
1575                 pr->norm_center_y = (gdouble)src_y / pr->height;
1576                 }
1577 }
1578
1579
1580 static gboolean pr_scroll_clamp(PixbufRenderer *pr)
1581 {
1582         gint old_xs;
1583         gint old_ys;
1584
1585         if (pr->zoom == 0.0)
1586                 {
1587                 pr->x_scroll = 0;
1588                 pr->y_scroll = 0;
1589
1590                 return FALSE;
1591                 }
1592
1593         old_xs = pr->x_scroll;
1594         old_ys = pr->y_scroll;
1595
1596         if (pr->x_offset > 0)
1597                 {
1598                 pr->x_scroll = 0;
1599                 }
1600         else
1601                 {
1602                 pr->x_scroll = CLAMP(pr->x_scroll, 0, pr->width - pr->vis_width);
1603                 }
1604
1605         if (pr->y_offset > 0)
1606                 {
1607                 pr->y_scroll = 0;
1608                 }
1609         else
1610                 {
1611                 pr->y_scroll = CLAMP(pr->y_scroll, 0, pr->height - pr->vis_height);
1612                 }
1613
1614         pixbuf_renderer_sync_scroll_center(pr);
1615
1616         return (old_xs != pr->x_scroll || old_ys != pr->y_scroll);
1617 }
1618
1619 static gboolean pr_size_clamp(PixbufRenderer *pr)
1620 {
1621         gint old_vw, old_vh;
1622
1623         old_vw = pr->vis_width;
1624         old_vh = pr->vis_height;
1625
1626         if (pr->width < pr->viewport_width)
1627                 {
1628                 pr->vis_width = pr->width;
1629                 pr->x_offset = (pr->viewport_width - pr->width) / 2;
1630                 }
1631         else
1632                 {
1633                 pr->vis_width = pr->viewport_width;
1634                 pr->x_offset = 0;
1635                 }
1636
1637         if (pr->height < pr->viewport_height)
1638                 {
1639                 pr->vis_height = pr->height;
1640                 pr->y_offset = (pr->viewport_height - pr->height) / 2;
1641                 }
1642         else
1643                 {
1644                 pr->vis_height = pr->viewport_height;
1645                 pr->y_offset = 0;
1646                 }
1647
1648         pixbuf_renderer_sync_scroll_center(pr);
1649
1650         return (old_vw != pr->vis_width || old_vh != pr->vis_height);
1651 }
1652
1653 static gboolean pr_zoom_clamp(PixbufRenderer *pr, gdouble zoom,
1654                               PrZoomFlags flags, gboolean *redrawn)
1655 {
1656         gint w, h;
1657         gdouble scale;
1658         gboolean invalid;
1659         gboolean force = !!(flags & PR_ZOOM_FORCE);
1660         gboolean new = !!(flags & PR_ZOOM_NEW);
1661         gboolean invalidate = !!(flags & PR_ZOOM_INVALIDATE);
1662         gboolean lazy = !!(flags & PR_ZOOM_LAZY);
1663
1664         zoom = CLAMP(zoom, pr->zoom_min, pr->zoom_max);
1665
1666         if (pr->zoom == zoom && !force) return FALSE;
1667
1668         w = pr->image_width;
1669         h = pr->image_height;
1670
1671         if (zoom == 0.0 && !pr->pixbuf)
1672                 {
1673                 scale = 1.0;
1674                 }
1675         else if (zoom == 0.0)
1676                 {
1677                 gint max_w;
1678                 gint max_h;
1679                 gboolean sizeable;
1680
1681                 sizeable = (new && pr_parent_window_sizable(pr));
1682
1683                 if (sizeable)
1684                         {
1685                         max_w = gdk_screen_width();
1686                         max_h = gdk_screen_height();
1687
1688                         if (pr->window_limit)
1689                                 {
1690                                 max_w = max_w * pr->window_limit_size / 100;
1691                                 max_h = max_h * pr->window_limit_size / 100;
1692                                 }
1693                         }
1694                 else
1695                         {
1696                         max_w = pr->viewport_width;
1697                         max_h = pr->viewport_height;
1698                         }
1699
1700                 if ((pr->zoom_expand && !sizeable) || w > max_w || h > max_h)
1701                         {
1702                         if ((gdouble)max_w / w > (gdouble)max_h / h / pr->aspect_ratio)
1703                                 {
1704                                 scale = (gdouble)max_h / h / pr->aspect_ratio;
1705                                 h = max_h;
1706                                 w = w * scale + 0.5;
1707                                 if (w > max_w) w = max_w;
1708                                 }
1709                         else
1710                                 {
1711                                 scale = (gdouble)max_w / w;
1712                                 w = max_w;
1713                                 h = h * scale * pr->aspect_ratio + 0.5;
1714                                 if (h > max_h) h = max_h;
1715                                 }
1716
1717                         if (pr->autofit_limit)
1718                                 {
1719                                 gdouble factor = (gdouble)pr->autofit_limit_size / 100;
1720                                 w = w * factor + 0.5;
1721                                 h = h * factor + 0.5;
1722                                 scale = scale * factor;
1723                                 }
1724
1725                         if (w < 1) w = 1;
1726                         if (h < 1) h = 1;
1727                         }
1728                 else
1729                         {
1730                         scale = 1.0;
1731                         }
1732                 }
1733         else if (zoom > 0.0) /* zoom orig, in */
1734                 {
1735                 scale = zoom;
1736                 w = w * scale;
1737                 h = h * scale * pr->aspect_ratio;
1738                 }
1739         else /* zoom out */
1740                 {
1741                 scale = 1.0 / (0.0 - zoom);
1742                 w = w * scale;
1743                 h = h * scale * pr->aspect_ratio;
1744                 }
1745
1746         invalid = (pr->width != w || pr->height != h);
1747
1748         pr->zoom = zoom;
1749         pr->width = w;
1750         pr->height = h;
1751         pr->scale = scale;
1752
1753         if (invalidate || invalid)
1754                 {
1755                 pr->renderer->invalidate_all(pr->renderer);
1756                 if (pr->renderer2) pr->renderer2->invalidate_all(pr->renderer2);
1757                 if (!lazy) pr_redraw(pr, TRUE);
1758                 }
1759         if (redrawn) *redrawn = (invalidate || invalid);
1760
1761         pixbuf_renderer_sync_scroll_center(pr);
1762
1763         return TRUE;
1764 }
1765
1766 static void pr_zoom_sync(PixbufRenderer *pr, gdouble zoom,
1767                          PrZoomFlags flags, gint px, gint py)
1768 {
1769         gdouble old_scale;
1770         gint old_cx, old_cy;
1771         gboolean clamped;
1772         gboolean sized;
1773         gboolean redrawn = FALSE;
1774         gboolean center_point = !!(flags & PR_ZOOM_CENTER);
1775         gboolean force = !!(flags & PR_ZOOM_FORCE);
1776         gboolean new = !!(flags & PR_ZOOM_NEW);
1777         gboolean lazy = !!(flags & PR_ZOOM_LAZY);
1778         PrZoomFlags clamp_flags = flags;
1779         gdouble old_center_x = pr->norm_center_x;
1780         gdouble old_center_y = pr->norm_center_y;
1781         
1782         old_scale = pr->scale;
1783         if (center_point)
1784                 {
1785                 px = CLAMP(px, 0, pr->width);
1786                 py = CLAMP(py, 0, pr->height);
1787                 old_cx = pr->x_scroll + (px - pr->x_offset);
1788                 old_cy = pr->y_scroll + (py - pr->y_offset);
1789                 }
1790         else
1791                 {
1792                 px = py = 0;
1793                 old_cx = pr->x_scroll + pr->vis_width / 2;
1794                 old_cy = pr->y_scroll + pr->vis_height / 2;
1795                 }
1796
1797         if (force) clamp_flags |= PR_ZOOM_INVALIDATE;
1798         if (lazy) clamp_flags |= PR_ZOOM_LAZY;
1799         if (!pr_zoom_clamp(pr, zoom, clamp_flags, &redrawn)) return;
1800
1801         clamped = pr_size_clamp(pr);
1802         sized = pr_parent_window_resize(pr, pr->width, pr->height);
1803
1804         if (force && new)
1805                 {
1806                 switch (pr->scroll_reset)
1807                         {
1808                         case PR_SCROLL_RESET_NOCHANGE:
1809                                 /* maintain old scroll position */
1810                                 pr->x_scroll = ((gdouble)pr->image_width * old_center_x * pr->scale) - pr->vis_width / 2;
1811                                 pr->y_scroll = ((gdouble)pr->image_height * old_center_y * pr->scale * pr->aspect_ratio) - pr->vis_height / 2;
1812                                 break;
1813                         case PR_SCROLL_RESET_CENTER:
1814                                 /* center new image */
1815                                 pr->x_scroll = ((gdouble)pr->image_width / 2.0 * pr->scale) - pr->vis_width / 2;
1816                                 pr->y_scroll = ((gdouble)pr->image_height / 2.0 * pr->scale * pr->aspect_ratio) - pr->vis_height / 2;
1817                                 break;
1818                         case PR_SCROLL_RESET_TOPLEFT:
1819                         default:
1820                                 /* reset to upper left */
1821                                 pr->x_scroll = 0;
1822                                 pr->y_scroll = 0;
1823                                 break;
1824                         }
1825                 }
1826         else
1827                 {
1828                 /* user zoom does not force, so keep visible center point */
1829                 if (center_point)
1830                         {
1831                         pr->x_scroll = old_cx / old_scale * pr->scale - (px - pr->x_offset);
1832                         pr->y_scroll = old_cy / old_scale * pr->scale * pr->aspect_ratio - (py - pr->y_offset);
1833                         }
1834                 else
1835                         {
1836                         pr->x_scroll = old_cx / old_scale * pr->scale - (pr->vis_width / 2);
1837                         pr->y_scroll = old_cy / old_scale * pr->scale * pr->aspect_ratio - (pr->vis_height / 2);
1838                         }
1839                 }
1840
1841         pr_scroll_clamp(pr);
1842
1843         /* If the window was not sized, redraw the image - we know there will be no size/expose signal.
1844          * But even if a size is claimed, there is no guarantee that the window manager will allow it,
1845          * so redraw the window anyway :/
1846          */
1847         if (sized || clamped) pr_border_clear(pr);
1848         
1849         if (lazy)
1850                 {
1851                 pr->renderer->queue_clear(pr->renderer);
1852                 if (pr->renderer2) pr->renderer2->queue_clear(pr->renderer2);
1853                 }
1854         else
1855                 {
1856                 pr_redraw(pr, redrawn);
1857                 }
1858
1859         pr_scroll_notify_signal(pr);
1860         pr_zoom_signal(pr);
1861         pr_update_signal(pr);
1862 }
1863
1864 static void pr_size_sync(PixbufRenderer *pr, gint new_width, gint new_height)
1865 {
1866         gboolean zoom_changed = FALSE;
1867
1868         gint new_viewport_width = (pr->stereo_mode & PR_STEREO_HORIZ) ? new_width / 2 : new_width;
1869         gint new_viewport_height = (pr->stereo_mode & PR_STEREO_VERT) ? new_height / 2 : new_height;
1870         
1871         if (pr->window_width == new_width && pr->window_height == new_height &&
1872             pr->viewport_width == new_viewport_width && pr->viewport_height == new_viewport_height) return;
1873
1874         pr->window_width = new_width;
1875         pr->window_height = new_height;
1876         pr->viewport_width = new_viewport_width;
1877         pr->viewport_height = new_viewport_height;
1878
1879         if (pr->zoom == 0.0)
1880                 {
1881                 gdouble old_scale = pr->scale;
1882                 pr_zoom_clamp(pr, 0.0, PR_ZOOM_FORCE, NULL);
1883                 zoom_changed = (old_scale != pr->scale);
1884                 }
1885
1886         pr_size_clamp(pr);
1887         pr_scroll_clamp(pr);
1888
1889         pr->renderer->update_sizes(pr->renderer);
1890         if (pr->renderer2) pr->renderer2->update_sizes(pr->renderer2);
1891
1892         /* ensure scroller remains visible */
1893         if (pr->scroller_overlay != -1)
1894                 {
1895                 gboolean update = FALSE;
1896
1897                 if (pr->scroller_x > new_width)
1898                         {
1899                         pr->scroller_x = new_width;
1900                         pr->scroller_xpos = new_width;
1901                         update = TRUE;
1902                         }
1903                 if (pr->scroller_y > new_height)
1904                         {
1905                         pr->scroller_y = new_height;
1906                         pr->scroller_ypos = new_height;
1907                         update = TRUE;
1908                         }
1909
1910                 if (update)
1911                         {
1912                         GdkPixbuf *pixbuf;
1913
1914                         if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
1915                                 {
1916                                 gint w, h;
1917
1918                                 w = gdk_pixbuf_get_width(pixbuf);
1919                                 h = gdk_pixbuf_get_height(pixbuf);
1920                                 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
1921                                                             pr->scroller_x - w / 2, pr->scroller_y - h / 2);
1922                                 }
1923                         }
1924                 }
1925
1926         pr_border_clear(pr);
1927
1928         pr_scroll_notify_signal(pr);
1929         if (zoom_changed) pr_zoom_signal(pr);
1930         pr_update_signal(pr);
1931 }
1932
1933 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
1934 {
1935         PixbufRenderer *pr = data;
1936
1937         pr_size_sync(pr, allocation->width, allocation->height);
1938 }
1939
1940 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
1941 {
1942         gint x, y;
1943
1944
1945         x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
1946         y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
1947
1948         pr->renderer->border_draw(pr->renderer, area->x, area->y, area->width, area->height);
1949         pr->renderer->queue(pr->renderer,
1950                               x, y,
1951                               MIN((gint)area->width, pr->width - x),
1952                               MIN((gint)area->height, pr->height - y),
1953                               FALSE, TILE_RENDER_ALL, FALSE, FALSE);
1954         if (pr->renderer2) 
1955                 {
1956                 pr->renderer2->border_draw(pr->renderer2, area->x, area->y, area->width, area->height);
1957                 pr->renderer2->queue(pr->renderer2,
1958                               x, y,
1959                               MIN((gint)area->width, pr->width - x),
1960                               MIN((gint)area->height, pr->height - y),
1961                               FALSE, TILE_RENDER_ALL, FALSE, FALSE);
1962                 }
1963 }
1964
1965 /*
1966  *-------------------------------------------------------------------
1967  * scrolling
1968  *-------------------------------------------------------------------
1969  */
1970
1971 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
1972 {
1973         gint old_x, old_y;
1974         gint x_off, y_off;
1975
1976         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1977
1978         if (!pr->pixbuf && !pr->source_tiles_enabled) return;
1979
1980         old_x = pr->x_scroll;
1981         old_y = pr->y_scroll;
1982
1983         pr->x_scroll += x;
1984         pr->y_scroll += y;
1985
1986         pr_scroll_clamp(pr);
1987         
1988         pixbuf_renderer_sync_scroll_center(pr);
1989         
1990         if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
1991
1992         pr_scroll_notify_signal(pr);
1993
1994         x_off = pr->x_scroll - old_x;
1995         y_off = pr->y_scroll - old_y;
1996         
1997         pr->renderer->scroll(pr->renderer, x_off, y_off);
1998         if (pr->renderer2) pr->renderer2->scroll(pr->renderer2, x_off, y_off);
1999 }
2000
2001 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
2002                                      gdouble x_align, gdouble y_align)
2003 {
2004         gint px, py;
2005         gint ax, ay;
2006
2007         x_align = CLAMP(x_align, 0.0, 1.0);
2008         y_align = CLAMP(y_align, 0.0, 1.0);
2009
2010         ax = (gdouble)pr->vis_width * x_align;
2011         ay = (gdouble)pr->vis_height * y_align;
2012
2013         px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
2014         py = (gdouble)y * pr->scale * pr->aspect_ratio - (pr->y_scroll + ay);
2015
2016         pixbuf_renderer_scroll(pr, px, py);
2017 }
2018
2019 /* get or set coordinates of viewport center in the image, in range 0.0 - 1.0 */
2020
2021 void pixbuf_renderer_get_scroll_center(PixbufRenderer *pr, gdouble *x, gdouble *y)
2022 {
2023         *x = pr->norm_center_x;
2024         *y = pr->norm_center_y;
2025 }
2026
2027 void pixbuf_renderer_set_scroll_center(PixbufRenderer *pr, gdouble x, gdouble y)
2028 {
2029         gdouble dst_x, dst_y;
2030
2031         dst_x = x * pr->width  - pr->vis_width  / 2 - pr->x_scroll + CLAMP(pr->subpixel_x_scroll, -1.0, 1.0);
2032         dst_y = y * pr->height - pr->vis_height / 2 - pr->y_scroll + CLAMP(pr->subpixel_y_scroll, -1.0, 1.0);
2033
2034         pr->subpixel_x_scroll = dst_x - (gint)dst_x;
2035         pr->subpixel_y_scroll = dst_y - (gint)dst_y;
2036
2037         pixbuf_renderer_scroll(pr, (gint)dst_x, (gint)dst_y);
2038 }
2039
2040 /*
2041  *-------------------------------------------------------------------
2042  * mouse
2043  *-------------------------------------------------------------------
2044  */
2045
2046 static gboolean pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2047 {
2048         PixbufRenderer *pr;
2049         gint accel;
2050
2051         /* This is a hack, but work far the best, at least for single pointer systems.
2052          * See http://bugzilla.gnome.org/show_bug.cgi?id=587714 for more. */
2053         gint x, y;
2054         gdk_window_get_pointer (bevent->window, &x, &y, NULL);
2055         bevent->x = x;
2056         bevent->y = y;
2057
2058         pr = PIXBUF_RENDERER(widget);
2059
2060         if (pr->scroller_id)
2061                 {
2062                 pr->scroller_xpos = bevent->x;
2063                 pr->scroller_ypos = bevent->y;
2064                 }
2065         
2066         pr->x_mouse = bevent->x;
2067         pr->y_mouse = bevent->y;
2068         pr_update_pixel_signal(pr);
2069         
2070         if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
2071
2072         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2073                 {
2074                 pr->drag_moved++;
2075                 }
2076         else
2077                 {
2078                 widget_set_cursor(widget, GDK_FLEUR);
2079                 }
2080
2081         if (bevent->state & GDK_CONTROL_MASK)
2082                 {
2083                 accel = PR_PAN_SHIFT_MULTIPLIER;
2084                 }
2085         else
2086                 {
2087                 accel = 1;
2088                 }
2089
2090         /* do the scroll */
2091         pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
2092                                (pr->drag_last_y - bevent->y) * accel);
2093
2094         pr_drag_signal(pr, bevent);
2095
2096         pr->drag_last_x = bevent->x;
2097         pr->drag_last_y = bevent->y;
2098
2099         /* This is recommended by the GTK+ documentation, but does not work properly.
2100          * Use deprecated way until GTK+ gets a solution for correct motion hint handling:
2101          * http://bugzilla.gnome.org/show_bug.cgi?id=587714
2102          */
2103         /* gdk_event_request_motions (bevent); */
2104         return FALSE;
2105 }
2106
2107 static gboolean pr_leave_notify_cb(GtkWidget *widget, GdkEventCrossing *cevent, gpointer data)
2108 {
2109         PixbufRenderer *pr;
2110
2111         pr = PIXBUF_RENDERER(widget);
2112         pr->x_mouse = -1;
2113         pr->y_mouse = -1;
2114
2115         pr_update_pixel_signal(pr);
2116         return FALSE;
2117 }
2118
2119 static gboolean pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2120 {
2121         PixbufRenderer *pr;
2122         GtkWidget *parent;
2123
2124         pr = PIXBUF_RENDERER(widget);
2125
2126         if (pr->scroller_id) return TRUE;
2127
2128         switch (bevent->button)
2129                 {
2130                 case MOUSE_BUTTON_LEFT:
2131                         pr->in_drag = TRUE;
2132                         pr->drag_last_x = bevent->x;
2133                         pr->drag_last_y = bevent->y;
2134                         pr->drag_moved = 0;
2135                         gdk_pointer_grab(widget->window, FALSE,
2136                                          GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_RELEASE_MASK,
2137                                          NULL, NULL, bevent->time);
2138                         gtk_grab_add(widget);
2139                         break;
2140                 case MOUSE_BUTTON_MIDDLE:
2141                         pr->drag_moved = 0;
2142                         break;
2143                 case MOUSE_BUTTON_RIGHT:
2144                         pr_clicked_signal(pr, bevent);
2145                         break;
2146                 default:
2147                         break;
2148                 }
2149
2150         parent = gtk_widget_get_parent(widget);
2151 #if GTK_CHECK_VERSION(2,20,0)
2152         if (widget && gtk_widget_get_can_focus(parent))
2153 #else
2154         if (widget && GTK_WIDGET_CAN_FOCUS(parent))
2155 #endif
2156                 {
2157                 gtk_widget_grab_focus(parent);
2158                 }
2159
2160         return FALSE;
2161 }
2162
2163 static gboolean pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2164 {
2165         PixbufRenderer *pr;
2166
2167         pr = PIXBUF_RENDERER(widget);
2168
2169         if (pr->scroller_id)
2170                 {
2171                 pr_scroller_stop(pr);
2172                 return TRUE;
2173                 }
2174
2175 #if GTK_CHECK_VERSION(2,20,0)
2176         if (gdk_pointer_is_grabbed() && gtk_widget_has_grab(GTK_WIDGET(pr)))
2177 #else
2178         if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
2179 #endif
2180                 {
2181                 gtk_grab_remove(widget);
2182                 gdk_pointer_ungrab(bevent->time);
2183                 widget_set_cursor(widget, -1);
2184                 }
2185
2186         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2187                 {
2188                 if (bevent->button == MOUSE_BUTTON_LEFT && (bevent->state & GDK_CONTROL_MASK))
2189                         {
2190                         pr_scroller_start(pr, bevent->x, bevent->y);
2191                         }
2192                 else if (bevent->button == MOUSE_BUTTON_LEFT || bevent->button == MOUSE_BUTTON_MIDDLE)
2193                         {
2194                         pr_clicked_signal(pr, bevent);
2195                         }
2196                 }
2197
2198         pr->in_drag = FALSE;
2199
2200         return FALSE;
2201 }
2202
2203 static gboolean pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
2204 {
2205         PixbufRenderer *pr;
2206
2207         pr = PIXBUF_RENDERER(widget);
2208
2209         if (pr->scroller_id)
2210                 {
2211                 pr->scroller_xpos = pr->scroller_x;
2212                 pr->scroller_ypos = pr->scroller_y;
2213                 pr->scroller_xinc = 0;
2214                 pr->scroller_yinc = 0;
2215                 }
2216
2217         return FALSE;
2218 }
2219
2220 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
2221 {
2222         PixbufRenderer *pr;
2223
2224         pr = PIXBUF_RENDERER(widget);
2225
2226         pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
2227 }
2228
2229 static void pr_signals_connect(PixbufRenderer *pr)
2230 {
2231         g_signal_connect(G_OBJECT(pr), "motion_notify_event",
2232                          G_CALLBACK(pr_mouse_motion_cb), pr);
2233         g_signal_connect(G_OBJECT(pr), "button_press_event",
2234                          G_CALLBACK(pr_mouse_press_cb), pr);
2235         g_signal_connect(G_OBJECT(pr), "button_release_event",
2236                          G_CALLBACK(pr_mouse_release_cb), pr);
2237         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
2238                          G_CALLBACK(pr_mouse_leave_cb), pr);
2239         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
2240                          G_CALLBACK(pr_leave_notify_cb), pr);
2241
2242         gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
2243                                               GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
2244                                               GDK_LEAVE_NOTIFY_MASK);
2245
2246         g_signal_connect(G_OBJECT(pr), "drag_begin",
2247                          G_CALLBACK(pr_mouse_drag_cb), pr);
2248
2249 }
2250
2251 /*
2252  *-------------------------------------------------------------------
2253  * stereo support
2254  *-------------------------------------------------------------------
2255  */
2256
2257 #define COLOR_BYTES 3   /* rgb */
2258 void pr_create_anaglyph(GdkPixbuf *pixbuf, GdkPixbuf *right, gint x, gint y, gint w, gint h)
2259 {
2260         gint srs, drs;
2261         guchar *s_pix, *d_pix;
2262         guchar *sp, *dp;
2263         guchar *spi, *dpi;
2264         gint i, j;
2265
2266         srs = gdk_pixbuf_get_rowstride(right);
2267         s_pix = gdk_pixbuf_get_pixels(right);
2268         spi = s_pix + (x * COLOR_BYTES);
2269
2270         drs = gdk_pixbuf_get_rowstride(pixbuf);
2271         d_pix = gdk_pixbuf_get_pixels(pixbuf);
2272         dpi =  d_pix + x * COLOR_BYTES;
2273
2274         for (i = y; i < y + h; i++)
2275                 {
2276                 sp = spi + (i * srs);
2277                 dp = dpi + (i * drs);
2278                 for (j = 0; j < w; j++)
2279                         {
2280                         *dp = *sp; /* copy red channel */
2281                         sp += COLOR_BYTES;
2282                         dp += COLOR_BYTES;
2283                         }
2284                 }
2285 }
2286  
2287  
2288
2289 /*
2290  *-------------------------------------------------------------------
2291  * public
2292  *-------------------------------------------------------------------
2293  */
2294 static void pr_pixbuf_size_sync(PixbufRenderer *pr)
2295 {
2296         pr->stereo_pixbuf_offset = 0;
2297         if (!pr->pixbuf) return;
2298         switch (pr->orientation)
2299                 {
2300                 case EXIF_ORIENTATION_LEFT_TOP:
2301                 case EXIF_ORIENTATION_RIGHT_TOP:
2302                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2303                 case EXIF_ORIENTATION_LEFT_BOTTOM:
2304                         pr->image_width = gdk_pixbuf_get_height(pr->pixbuf);
2305                         pr->image_height = gdk_pixbuf_get_width(pr->pixbuf);
2306                         if (GPOINTER_TO_INT(g_object_get_data(G_OBJECT(pr->pixbuf), "stereo_sbs"))) 
2307                                 {
2308                                 pr->image_height /= 2;
2309                                 pr->stereo_pixbuf_offset = pr->image_height;
2310                                 }
2311                         
2312                         break;
2313                 default:
2314                         pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
2315                         pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
2316                         if (GPOINTER_TO_INT(g_object_get_data(G_OBJECT(pr->pixbuf), "stereo_sbs"))) 
2317                                 {
2318                                 pr->image_width /= 2;
2319                                 pr->stereo_pixbuf_offset = pr->image_width;
2320                                 }
2321                 }
2322 }
2323
2324 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, PrZoomFlags flags)
2325 {
2326         if (pixbuf) g_object_ref(pixbuf);
2327         if (pr->pixbuf) g_object_unref(pr->pixbuf);
2328         pr->pixbuf = pixbuf;
2329
2330         if (!pr->pixbuf)
2331                 {
2332                 GtkWidget *box;
2333
2334                 /* no pixbuf so just clear the window */
2335                 pr->image_width = 0;
2336                 pr->image_height = 0;
2337                 pr->scale = 1.0;
2338                 pr->zoom = zoom; /* don't throw away the zoom value, it is set by pixbuf_renderer_move, among others,
2339                                     and used for pixbuf_renderer_zoom_get */
2340
2341                 box = GTK_WIDGET(pr);
2342
2343 #if GTK_CHECK_VERSION(2,20,0)
2344                 if (gtk_widget_get_realized(box))
2345 #else
2346                 if (GTK_WIDGET_REALIZED(box))
2347 #endif
2348                         {
2349                         gdk_window_clear(box->window);
2350                         pr->renderer->overlay_draw(pr->renderer, 0, 0, pr->viewport_width, pr->viewport_height);
2351                         if (pr->renderer2) pr->renderer2->overlay_draw(pr->renderer2, 0, 0, pr->viewport_width, pr->viewport_height);
2352                         }
2353
2354                 pr_update_signal(pr);
2355
2356                 return;
2357                 }
2358
2359         pr_pixbuf_size_sync(pr);
2360         pr_zoom_sync(pr, zoom, flags | PR_ZOOM_FORCE | PR_ZOOM_NEW, 0, 0);
2361 }
2362
2363 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
2364 {
2365         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2366
2367         pr_source_tile_unset(pr);
2368
2369         pr_set_pixbuf(pr, pixbuf, zoom, 0);
2370
2371         pr_update_signal(pr);
2372 }
2373
2374 void pixbuf_renderer_set_pixbuf_lazy(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, gint orientation)
2375 {
2376         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2377
2378         pr_source_tile_unset(pr);
2379
2380         pr->orientation = orientation;
2381         pr_set_pixbuf(pr, pixbuf, zoom, PR_ZOOM_LAZY);
2382
2383         pr_update_signal(pr);
2384 }
2385
2386 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
2387 {
2388         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
2389
2390         return pr->pixbuf;
2391 }
2392
2393 void pixbuf_renderer_set_orientation(PixbufRenderer *pr, gint orientation)
2394 {
2395         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2396
2397         pr->orientation = orientation;
2398
2399         pr_pixbuf_size_sync(pr);
2400         pr_zoom_sync(pr, pr->zoom, PR_ZOOM_FORCE, 0, 0);
2401 }
2402
2403 gint pixbuf_renderer_get_orientation(PixbufRenderer *pr)
2404 {
2405         if (!pr) return 1;
2406         return pr->orientation;
2407 }
2408
2409 void pixbuf_renderer_set_post_process_func(PixbufRenderer *pr, PixbufRendererPostProcessFunc func, gpointer user_data, gboolean slow)
2410 {
2411         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2412
2413         pr->func_post_process = func;
2414         pr->post_process_user_data = user_data;
2415         pr->post_process_slow = func && slow;
2416
2417 }
2418
2419
2420 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
2421 {
2422         GObject *object;
2423         PixbufRendererScrollResetType scroll_reset;
2424
2425         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2426         g_return_if_fail(IS_PIXBUF_RENDERER(source));
2427
2428         if (pr == source) return;
2429
2430         object = G_OBJECT(pr);
2431
2432         g_object_set(object, "zoom_min", source->zoom_min, NULL);
2433         g_object_set(object, "zoom_max", source->zoom_max, NULL);
2434         g_object_set(object, "loading", source->loading, NULL);
2435
2436         pr->complete = source->complete;
2437
2438         pr->x_scroll = source->x_scroll;
2439         pr->y_scroll = source->y_scroll;
2440         pr->x_mouse  = source->x_mouse;
2441         pr->y_mouse  = source->y_mouse;
2442
2443         scroll_reset = pr->scroll_reset;
2444         pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
2445
2446         pr->func_post_process = source->func_post_process;
2447         pr->post_process_user_data = source->post_process_user_data;
2448         pr->post_process_slow = source->post_process_slow;
2449         pr->orientation = source->orientation;
2450
2451         if (source->source_tiles_enabled)
2452                 {
2453                 pr_source_tile_unset(pr);
2454
2455                 pr->source_tiles_enabled = source->source_tiles_enabled;
2456                 pr->source_tiles_cache_size = source->source_tiles_cache_size;
2457                 pr->source_tile_width = source->source_tile_width;
2458                 pr->source_tile_height = source->source_tile_height;
2459                 pr->image_width = source->image_width;
2460                 pr->image_height = source->image_height;
2461
2462                 pr->func_tile_request = source->func_tile_request;
2463                 pr->func_tile_dispose = source->func_tile_dispose;
2464                 pr->func_tile_data = source->func_tile_data;
2465
2466                 pr->source_tiles = source->source_tiles;
2467                 source->source_tiles = NULL;
2468
2469                 pr_zoom_sync(pr, source->zoom, PR_ZOOM_FORCE | PR_ZOOM_NEW, 0, 0);
2470                 pr_redraw(pr, TRUE);
2471                 }
2472         else
2473                 {
2474                 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
2475                 }
2476
2477         pr->scroll_reset = scroll_reset;
2478
2479         pixbuf_renderer_set_pixbuf(source, NULL, source->zoom);
2480 //      pr_queue_clear(source);
2481 //      pr_tile_free_all(source);
2482 }
2483
2484 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint src_x, gint src_y, gint src_w, gint src_h)
2485 {
2486         gint x, y, width, height,  x1, y1, x2, y2;
2487
2488         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2489
2490         pr_coords_map_orientation_reverse(pr->orientation,
2491                                      src_x, src_y,
2492                                      pr->image_width, pr->image_height,
2493                                      src_w, src_h,
2494                                      &x, &y,
2495                                      &width, &height);
2496
2497         if (pr->source_tiles_enabled)
2498                 {
2499                 pr_source_tile_changed(pr, x, y, width, height);
2500                 }
2501
2502         if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2503                 {
2504                 /* increase region when using a zoom quality that may access surrounding pixels */
2505                 y -= 1;
2506                 height += 2;
2507                 }
2508
2509         x1 = (gint)floor((gdouble)x * pr->scale);
2510         y1 = (gint)floor((gdouble)y * pr->scale * pr->aspect_ratio);
2511         x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
2512         y2 = (gint)ceil((gdouble)(y + height) * pr->scale * pr->aspect_ratio);
2513
2514         pr->renderer->queue(pr->renderer, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2515         if (pr->renderer2) pr->renderer2->queue(pr->renderer2, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2516 }
2517
2518 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
2519 {
2520         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2521
2522         pr_zoom_adjust_real(pr, increment, PR_ZOOM_NONE, 0, 0);
2523 }
2524
2525 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
2526 {
2527         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2528
2529         pr_zoom_adjust_real(pr, increment, PR_ZOOM_CENTER, x, y);
2530 }
2531
2532 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
2533 {
2534         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2535
2536         pr_zoom_sync(pr, zoom, PR_ZOOM_NONE, 0, 0);
2537 }
2538
2539 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
2540 {
2541         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
2542
2543         return pr->zoom;
2544 }
2545
2546 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
2547 {
2548         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
2549
2550         return pr->scale;
2551 }
2552
2553 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
2554 {
2555         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2556
2557         if (min > 1.0 || max < 1.0) return;
2558         if (min < 1.0 && min > -1.0) return;
2559         if (min < -200.0 || max > 200.0) return;
2560
2561         if (pr->zoom_min != min)
2562                 {
2563                 pr->zoom_min = min;
2564                 g_object_notify(G_OBJECT(pr), "zoom_min");
2565                 }
2566         if (pr->zoom_max != max)
2567                 {
2568                 pr->zoom_max = max;
2569                 g_object_notify(G_OBJECT(pr), "zoom_max");
2570                 }
2571 }
2572
2573 gboolean pixbuf_renderer_get_pixel_colors(PixbufRenderer *pr, gint x_pixel, gint y_pixel, 
2574                                           gint *r_mouse, gint *g_mouse, gint *b_mouse)
2575 {
2576         GdkPixbuf *pb = pr->pixbuf;
2577         gint p_alpha, prs;
2578         guchar *p_pix, *pp;
2579         gint map_x, map_y, map_w, map_h;
2580         
2581         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2582         g_return_val_if_fail(r_mouse != NULL && g_mouse != NULL && b_mouse != NULL, FALSE);
2583
2584         if (!pr->pixbuf && !pr->source_tiles_enabled)
2585                 {
2586                 *r_mouse = -1;
2587                 *g_mouse = -1;
2588                 *b_mouse = -1;
2589                 return FALSE;
2590                 }
2591         
2592         if (!pb) return FALSE;
2593
2594         pr_tile_region_map_orientation(pr->orientation,
2595                                         x_pixel, y_pixel,
2596                                         pr->image_width, pr->image_height,
2597                                         1, 1, /*single pixel */
2598                                         &map_x, &map_y,
2599                                         &map_w, &map_h);
2600
2601         if (map_x < 0 || map_x > gdk_pixbuf_get_width(pr->pixbuf) - 1) return  FALSE;
2602         if (map_y < 0 || map_y > gdk_pixbuf_get_height(pr->pixbuf) - 1) return  FALSE;
2603         
2604         p_alpha = gdk_pixbuf_get_has_alpha(pb);
2605         prs = gdk_pixbuf_get_rowstride(pb);
2606         p_pix = gdk_pixbuf_get_pixels(pb);
2607
2608         pp = p_pix + map_y * prs + (map_x * (p_alpha ? 4 : 3));
2609         *r_mouse = *pp;
2610         pp++;
2611         *g_mouse = *pp;
2612         pp++;
2613         *b_mouse = *pp;
2614         
2615         return TRUE;
2616 }
2617
2618 gboolean pixbuf_renderer_get_mouse_position(PixbufRenderer *pr, gint *x_pixel_return, gint *y_pixel_return)
2619 {
2620         gint x_pixel, y_pixel, x_pixel_clamped, y_pixel_clamped;
2621              
2622         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2623         g_return_val_if_fail(x_pixel_return != NULL && y_pixel_return != NULL, FALSE);
2624
2625         if (!pr->pixbuf && !pr->source_tiles_enabled)
2626                 {
2627                 *x_pixel_return = -1;
2628                 *y_pixel_return = -1;
2629                 return FALSE;
2630                 }
2631         
2632         x_pixel = floor((gdouble)(pr->x_mouse - pr->x_offset + pr->x_scroll) / pr->scale);
2633         y_pixel = floor((gdouble)(pr->y_mouse - pr->y_offset + pr->y_scroll) / pr->scale / pr->aspect_ratio);
2634         x_pixel_clamped = CLAMP(x_pixel, 0, pr->image_width - 1);
2635         y_pixel_clamped = CLAMP(y_pixel, 0, pr->image_height - 1);
2636         
2637         if(x_pixel != x_pixel_clamped || y_pixel != y_pixel_clamped)
2638                 {
2639                 /* mouse is not on pr */
2640                 x_pixel = y_pixel = -1;
2641                 }
2642
2643         *x_pixel_return = x_pixel;
2644         *y_pixel_return = y_pixel;
2645         
2646         return TRUE;
2647 }
2648
2649 gboolean pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
2650 {
2651         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2652         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
2653
2654         if (!pr->pixbuf && !pr->source_tiles_enabled && (!pr->image_width || !pr->image_height))
2655                 {
2656                 *width = 0;
2657                 *height = 0;
2658                 return FALSE;
2659                 }
2660
2661         *width = pr->image_width;
2662         *height = pr->image_height;
2663         return TRUE;
2664 }
2665
2666 gboolean pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
2667 {
2668         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2669         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
2670
2671         if (!pr->pixbuf && !pr->source_tiles_enabled && (!pr->image_width || !pr->image_height))
2672                 {
2673                 *width = 0;
2674                 *height = 0;
2675                 return FALSE;
2676                 }
2677
2678         *width = pr->width;
2679         *height = pr->height;
2680         return TRUE;
2681 }
2682
2683 gboolean pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
2684 {
2685         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2686         g_return_val_if_fail(rect != NULL, FALSE);
2687
2688         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2689             !pr->scale)
2690                 {
2691                 rect->x = 0;
2692                 rect->y = 0;
2693                 rect->width = 0;
2694                 rect->height = 0;
2695                 return FALSE;
2696                 }
2697
2698         rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
2699         rect->y = (gint)((gdouble)pr->y_scroll / pr->scale / pr->aspect_ratio);
2700         rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
2701         rect->height = (gint)((gdouble)pr->vis_height / pr->scale / pr->aspect_ratio);
2702         return TRUE;
2703 }
2704
2705 gboolean pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
2706 {
2707         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2708         g_return_val_if_fail(rect != NULL, FALSE);
2709
2710         if ((!pr->pixbuf && !pr->source_tiles_enabled))
2711                 {
2712                 rect->x = 0;
2713                 rect->y = 0;
2714                 rect->width = 0;
2715                 rect->height = 0;
2716                 return FALSE;
2717                 }
2718
2719         rect->x = pr->x_scroll;
2720         rect->y = pr->y_scroll;
2721         rect->width = pr->vis_width;
2722         rect->height = pr->vis_height;
2723         return TRUE;
2724 }
2725
2726 void pixbuf_renderer_set_size_early(PixbufRenderer *pr, guint width, guint height)
2727 {
2728         gdouble zoom;
2729         gint w, h;
2730
2731         zoom = pixbuf_renderer_zoom_get(pr);
2732         pr->image_width = width;
2733         pr->image_height = height;
2734
2735         pr_zoom_clamp(pr, zoom, PR_ZOOM_FORCE, NULL);
2736
2737         //w = width;
2738         //h = height;
2739
2740         //pr->width = width;
2741         //pr->height = height;
2742 }
2743
2744 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */