fixed sbs mode
[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 static void pr_stereo_temp_disable(PixbufRenderer *pr, gboolean disable);
138
139
140 /*
141  *-------------------------------------------------------------------
142  * Pixbuf Renderer object
143  *-------------------------------------------------------------------
144  */
145
146 GType pixbuf_renderer_get_type(void)
147 {
148         static GType pixbuf_renderer_type = 0;
149
150         if (!pixbuf_renderer_type)
151                 {
152                 static const GTypeInfo pixbuf_renderer_info =
153                         {
154                         sizeof(PixbufRendererClass), /* class_size */
155                         NULL,           /* base_init */
156                         NULL,           /* base_finalize */
157                         (GClassInitFunc)pixbuf_renderer_class_init,
158                         NULL,           /* class_finalize */
159                         NULL,           /* class_data */
160                         sizeof(PixbufRenderer), /* instance_size */
161                         0,              /* n_preallocs */
162                         (GInstanceInitFunc)pixbuf_renderer_init, /* instance_init */
163                         NULL,           /* value_table */
164                         };
165
166                 pixbuf_renderer_type = g_type_register_static(GTK_TYPE_EVENT_BOX, "PixbufRenderer",
167                                                               &pixbuf_renderer_info, 0);
168                 }
169
170         return pixbuf_renderer_type;
171 }
172
173 static void pixbuf_renderer_class_init(PixbufRendererClass *class)
174 {
175         GObjectClass *gobject_class = G_OBJECT_CLASS(class);
176         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
177
178         parent_class = g_type_class_peek_parent(class);
179
180         gobject_class->set_property = pixbuf_renderer_set_property;
181         gobject_class->get_property = pixbuf_renderer_get_property;
182
183         gobject_class->finalize = pixbuf_renderer_finalize;
184
185         widget_class->expose_event = pixbuf_renderer_expose;
186
187         g_object_class_install_property(gobject_class,
188                                         PROP_ZOOM_MIN,
189                                         g_param_spec_double("zoom_min",
190                                                             "Zoom minimum",
191                                                             NULL,
192                                                             -1000.0,
193                                                             1000.0,
194                                                             PR_ZOOM_MIN,
195                                                             G_PARAM_READABLE | G_PARAM_WRITABLE));
196
197         g_object_class_install_property(gobject_class,
198                                         PROP_ZOOM_MAX,
199                                         g_param_spec_double("zoom_max",
200                                                             "Zoom maximum",
201                                                             NULL,
202                                                             -1000.0,
203                                                             1000.0,
204                                                             PR_ZOOM_MIN,
205                                                             G_PARAM_READABLE | G_PARAM_WRITABLE));
206
207         g_object_class_install_property(gobject_class,
208                                         PROP_ZOOM_QUALITY,
209                                         g_param_spec_uint("zoom_quality",
210                                                           "Zoom quality",
211                                                           NULL,
212                                                           GDK_INTERP_NEAREST,
213                                                           GDK_INTERP_HYPER,
214                                                           GDK_INTERP_BILINEAR,
215                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
216
217         g_object_class_install_property(gobject_class,
218                                         PROP_ZOOM_2PASS,
219                                         g_param_spec_boolean("zoom_2pass",
220                                                              "2 pass zoom",
221                                                              NULL,
222                                                              TRUE,
223                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
224
225         g_object_class_install_property(gobject_class,
226                                         PROP_ZOOM_EXPAND,
227                                         g_param_spec_boolean("zoom_expand",
228                                                              "Expand image in autozoom.",
229                                                              NULL,
230                                                              FALSE,
231                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
232
233         g_object_class_install_property(gobject_class,
234                                         PROP_DITHER_QUALITY,
235                                         g_param_spec_uint("dither_quality",
236                                                           "Dither quality",
237                                                           NULL,
238                                                           GDK_RGB_DITHER_NONE,
239                                                           GDK_RGB_DITHER_MAX,
240                                                           GDK_RGB_DITHER_NORMAL,
241                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
242
243         g_object_class_install_property(gobject_class,
244                                         PROP_SCROLL_RESET,
245                                         g_param_spec_uint("scroll_reset",
246                                                           "New image scroll reset",
247                                                           NULL,
248                                                           PR_SCROLL_RESET_TOPLEFT,
249                                                           PR_SCROLL_RESET_NOCHANGE,
250                                                           PR_SCROLL_RESET_TOPLEFT,
251                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
252
253         g_object_class_install_property(gobject_class,
254                                         PROP_DELAY_FLIP,
255                                         g_param_spec_boolean("delay_flip",
256                                                              "Delay image update",
257                                                              NULL,
258                                                              FALSE,
259                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
260
261         g_object_class_install_property(gobject_class,
262                                         PROP_LOADING,
263                                         g_param_spec_boolean("loading",
264                                                              "Image actively loading",
265                                                              NULL,
266                                                              FALSE,
267                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
268
269         g_object_class_install_property(gobject_class,
270                                         PROP_COMPLETE,
271                                         g_param_spec_boolean("complete",
272                                                              "Image rendering complete",
273                                                              NULL,
274                                                              FALSE,
275                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
276
277         g_object_class_install_property(gobject_class,
278                                         PROP_CACHE_SIZE_DISPLAY,
279                                         g_param_spec_uint("cache_display",
280                                                           "Display cache size MB",
281                                                           NULL,
282                                                           0,
283                                                           128,
284                                                           PR_CACHE_SIZE_DEFAULT,
285                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
286
287         g_object_class_install_property(gobject_class,
288                                         PROP_CACHE_SIZE_TILES,
289                                         g_param_spec_uint("cache_tiles",
290                                                           "Tile cache count",
291                                                           "Number of tiles to retain in memory at any one time.",
292                                                           0,
293                                                           256,
294                                                           PR_CACHE_SIZE_DEFAULT,
295                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
296
297         g_object_class_install_property(gobject_class,
298                                         PROP_WINDOW_FIT,
299                                         g_param_spec_boolean("window_fit",
300                                                              "Fit window to image size",
301                                                              NULL,
302                                                              FALSE,
303                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
304
305         g_object_class_install_property(gobject_class,
306                                         PROP_WINDOW_LIMIT,
307                                         g_param_spec_boolean("window_limit",
308                                                              "Limit size of parent window",
309                                                              NULL,
310                                                              FALSE,
311                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
312
313         g_object_class_install_property(gobject_class,
314                                         PROP_WINDOW_LIMIT_VALUE,
315                                         g_param_spec_uint("window_limit_value",
316                                                           "Size limit of parent window",
317                                                           NULL,
318                                                           10,
319                                                           150,
320                                                           100,
321                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
322
323         g_object_class_install_property(gobject_class,
324                                         PROP_AUTOFIT_LIMIT,
325                                         g_param_spec_boolean("autofit_limit",
326                                                              "Limit size of image when autofitting",
327                                                              NULL,
328                                                              FALSE,
329                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));
330
331         g_object_class_install_property(gobject_class,
332                                         PROP_AUTOFIT_LIMIT_VALUE,
333                                         g_param_spec_uint("autofit_limit_value",
334                                                           "Size limit of image when autofitting",
335                                                           NULL,
336                                                           10,
337                                                           150,
338                                                           100,
339                                                           G_PARAM_READABLE | G_PARAM_WRITABLE));
340
341
342         signals[SIGNAL_ZOOM] =
343                 g_signal_new("zoom",
344                              G_OBJECT_CLASS_TYPE(gobject_class),
345                              G_SIGNAL_RUN_LAST,
346                              G_STRUCT_OFFSET(PixbufRendererClass, zoom),
347                              NULL, NULL,
348                              g_cclosure_marshal_VOID__DOUBLE,
349                              G_TYPE_NONE, 1,
350                              G_TYPE_DOUBLE);
351
352         signals[SIGNAL_CLICKED] =
353                 g_signal_new("clicked",
354                              G_OBJECT_CLASS_TYPE(gobject_class),
355                              G_SIGNAL_RUN_LAST,
356                              G_STRUCT_OFFSET(PixbufRendererClass, clicked),
357                              NULL, NULL,
358                              g_cclosure_marshal_VOID__BOXED,
359                              G_TYPE_NONE, 1,
360                              GDK_TYPE_EVENT);
361
362         signals[SIGNAL_SCROLL_NOTIFY] =
363                 g_signal_new("scroll-notify",
364                              G_OBJECT_CLASS_TYPE(gobject_class),
365                              G_SIGNAL_RUN_LAST,
366                              G_STRUCT_OFFSET(PixbufRendererClass, scroll_notify),
367                              NULL, NULL,
368                              g_cclosure_marshal_VOID__VOID,
369                              G_TYPE_NONE, 0);
370
371         signals[SIGNAL_RENDER_COMPLETE] =
372                 g_signal_new("render-complete",
373                              G_OBJECT_CLASS_TYPE(gobject_class),
374                              G_SIGNAL_RUN_LAST,
375                              G_STRUCT_OFFSET(PixbufRendererClass, render_complete),
376                              NULL, NULL,
377                              g_cclosure_marshal_VOID__VOID,
378                              G_TYPE_NONE, 0);
379
380         signals[SIGNAL_DRAG] =
381                 g_signal_new("drag",
382                              G_OBJECT_CLASS_TYPE(gobject_class),
383                              G_SIGNAL_RUN_LAST,
384                              G_STRUCT_OFFSET(PixbufRendererClass, drag),
385                              NULL, NULL,
386                              g_cclosure_marshal_VOID__BOXED,
387                              G_TYPE_NONE, 1,
388                              GDK_TYPE_EVENT);
389                              
390         signals[SIGNAL_UPDATE_PIXEL] =
391                 g_signal_new("update-pixel",
392                              G_OBJECT_CLASS_TYPE(gobject_class),
393                              G_SIGNAL_RUN_LAST,
394                              G_STRUCT_OFFSET(PixbufRendererClass, update_pixel),
395                              NULL, NULL,
396                              g_cclosure_marshal_VOID__VOID,
397                              G_TYPE_NONE, 0);
398 }
399
400 static void pixbuf_renderer_init(PixbufRenderer *pr)
401 {
402         GtkWidget *box;
403
404         box = GTK_WIDGET(pr);
405
406         pr->zoom_min = PR_ZOOM_MIN;
407         pr->zoom_max = PR_ZOOM_MAX;
408         pr->zoom_quality = GDK_INTERP_BILINEAR;
409         pr->zoom_2pass = FALSE;
410
411         pr->zoom = 1.0;
412         pr->scale = 1.0;
413         pr->aspect_ratio = 1.0;
414
415         pr->dither_quality = GDK_RGB_DITHER_NORMAL;
416
417         pr->scroll_reset = PR_SCROLL_RESET_TOPLEFT;
418
419         pr->scroller_id = 0;
420         pr->scroller_overlay = -1;
421         
422         pr->x_mouse = -1;
423         pr->y_mouse = -1;
424
425         pr->source_tiles_enabled = FALSE;
426         pr->source_tiles = NULL;
427
428         pr->orientation = 1;
429
430         pr->norm_center_x = 0.5;
431         pr->norm_center_y = 0.5;
432         
433         pr->stereo_mode = PR_STEREO_NONE;
434         
435         pr->renderer = (void *)renderer_tiles_new(pr);
436         
437         pr->renderer2 = 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 = new_width;
1869         gint new_viewport_height = new_height;
1870
1871         if (!pr->stereo_temp_disable)
1872                 {
1873                 if (pr->stereo_mode & PR_STEREO_HORIZ)
1874                         {
1875                         new_viewport_width = new_width / 2;
1876                         }
1877                 else if (pr->stereo_mode & PR_STEREO_VERT)
1878                         {
1879                         new_viewport_height = new_height / 2;
1880                         }
1881                 else if (pr->stereo_mode & PR_STEREO_FIXED)
1882                         {
1883                         new_viewport_width = pr->stereo_fixed_width;
1884                         new_viewport_height = pr->stereo_fixed_height;
1885                         }
1886                 }
1887                 
1888         if (pr->window_width == new_width && pr->window_height == new_height &&
1889             pr->viewport_width == new_viewport_width && pr->viewport_height == new_viewport_height) return;
1890
1891         pr->window_width = new_width;
1892         pr->window_height = new_height;
1893         pr->viewport_width = new_viewport_width;
1894         pr->viewport_height = new_viewport_height;
1895
1896         if (pr->zoom == 0.0)
1897                 {
1898                 gdouble old_scale = pr->scale;
1899                 pr_zoom_clamp(pr, 0.0, PR_ZOOM_FORCE, NULL);
1900                 zoom_changed = (old_scale != pr->scale);
1901                 }
1902
1903         pr_size_clamp(pr);
1904         pr_scroll_clamp(pr);
1905
1906         pr->renderer->update_sizes(pr->renderer);
1907         if (pr->renderer2) pr->renderer2->update_sizes(pr->renderer2);
1908
1909         /* ensure scroller remains visible */
1910         if (pr->scroller_overlay != -1)
1911                 {
1912                 gboolean update = FALSE;
1913
1914                 if (pr->scroller_x > new_width)
1915                         {
1916                         pr->scroller_x = new_width;
1917                         pr->scroller_xpos = new_width;
1918                         update = TRUE;
1919                         }
1920                 if (pr->scroller_y > new_height)
1921                         {
1922                         pr->scroller_y = new_height;
1923                         pr->scroller_ypos = new_height;
1924                         update = TRUE;
1925                         }
1926
1927                 if (update)
1928                         {
1929                         GdkPixbuf *pixbuf;
1930
1931                         if (pixbuf_renderer_overlay_get(pr, pr->scroller_overlay, &pixbuf, NULL, NULL))
1932                                 {
1933                                 gint w, h;
1934
1935                                 w = gdk_pixbuf_get_width(pixbuf);
1936                                 h = gdk_pixbuf_get_height(pixbuf);
1937                                 pixbuf_renderer_overlay_set(pr, pr->scroller_overlay, pixbuf,
1938                                                             pr->scroller_x - w / 2, pr->scroller_y - h / 2);
1939                                 }
1940                         }
1941                 }
1942
1943         pr_border_clear(pr);
1944
1945         pr_scroll_notify_signal(pr);
1946         if (zoom_changed) pr_zoom_signal(pr);
1947         pr_update_signal(pr);
1948 }
1949
1950 static void pr_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
1951 {
1952         PixbufRenderer *pr = data;
1953
1954         pr_size_sync(pr, allocation->width, allocation->height);
1955 }
1956
1957 static void pixbuf_renderer_paint(PixbufRenderer *pr, GdkRectangle *area)
1958 {
1959         gint x, y;
1960
1961
1962         x = MAX(0, (gint)area->x - pr->x_offset + pr->x_scroll);
1963         y = MAX(0, (gint)area->y - pr->y_offset + pr->y_scroll);
1964
1965         pr->renderer->border_draw(pr->renderer, area->x, area->y, area->width, area->height);
1966         pr->renderer->queue(pr->renderer,
1967                               x, y,
1968                               MIN((gint)area->width, pr->width - x),
1969                               MIN((gint)area->height, pr->height - y),
1970                               FALSE, TILE_RENDER_ALL, FALSE, FALSE);
1971         if (pr->renderer2) 
1972                 {
1973                 pr->renderer2->border_draw(pr->renderer2, area->x, area->y, area->width, area->height);
1974                 pr->renderer2->queue(pr->renderer2,
1975                               x, y,
1976                               MIN((gint)area->width, pr->width - x),
1977                               MIN((gint)area->height, pr->height - y),
1978                               FALSE, TILE_RENDER_ALL, FALSE, FALSE);
1979                 }
1980 }
1981
1982 /*
1983  *-------------------------------------------------------------------
1984  * scrolling
1985  *-------------------------------------------------------------------
1986  */
1987
1988 void pixbuf_renderer_scroll(PixbufRenderer *pr, gint x, gint y)
1989 {
1990         gint old_x, old_y;
1991         gint x_off, y_off;
1992
1993         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
1994
1995         if (!pr->pixbuf && !pr->source_tiles_enabled) return;
1996
1997         old_x = pr->x_scroll;
1998         old_y = pr->y_scroll;
1999
2000         pr->x_scroll += x;
2001         pr->y_scroll += y;
2002
2003         pr_scroll_clamp(pr);
2004         
2005         pixbuf_renderer_sync_scroll_center(pr);
2006         
2007         if (pr->x_scroll == old_x && pr->y_scroll == old_y) return;
2008
2009         pr_scroll_notify_signal(pr);
2010
2011         x_off = pr->x_scroll - old_x;
2012         y_off = pr->y_scroll - old_y;
2013         
2014         pr->renderer->scroll(pr->renderer, x_off, y_off);
2015         if (pr->renderer2) pr->renderer2->scroll(pr->renderer2, x_off, y_off);
2016 }
2017
2018 void pixbuf_renderer_scroll_to_point(PixbufRenderer *pr, gint x, gint y,
2019                                      gdouble x_align, gdouble y_align)
2020 {
2021         gint px, py;
2022         gint ax, ay;
2023
2024         x_align = CLAMP(x_align, 0.0, 1.0);
2025         y_align = CLAMP(y_align, 0.0, 1.0);
2026
2027         ax = (gdouble)pr->vis_width * x_align;
2028         ay = (gdouble)pr->vis_height * y_align;
2029
2030         px = (gdouble)x * pr->scale - (pr->x_scroll + ax);
2031         py = (gdouble)y * pr->scale * pr->aspect_ratio - (pr->y_scroll + ay);
2032
2033         pixbuf_renderer_scroll(pr, px, py);
2034 }
2035
2036 /* get or set coordinates of viewport center in the image, in range 0.0 - 1.0 */
2037
2038 void pixbuf_renderer_get_scroll_center(PixbufRenderer *pr, gdouble *x, gdouble *y)
2039 {
2040         *x = pr->norm_center_x;
2041         *y = pr->norm_center_y;
2042 }
2043
2044 void pixbuf_renderer_set_scroll_center(PixbufRenderer *pr, gdouble x, gdouble y)
2045 {
2046         gdouble dst_x, dst_y;
2047
2048         dst_x = x * pr->width  - pr->vis_width  / 2 - pr->x_scroll + CLAMP(pr->subpixel_x_scroll, -1.0, 1.0);
2049         dst_y = y * pr->height - pr->vis_height / 2 - pr->y_scroll + CLAMP(pr->subpixel_y_scroll, -1.0, 1.0);
2050
2051         pr->subpixel_x_scroll = dst_x - (gint)dst_x;
2052         pr->subpixel_y_scroll = dst_y - (gint)dst_y;
2053
2054         pixbuf_renderer_scroll(pr, (gint)dst_x, (gint)dst_y);
2055 }
2056
2057 /*
2058  *-------------------------------------------------------------------
2059  * mouse
2060  *-------------------------------------------------------------------
2061  */
2062
2063 static gboolean pr_mouse_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2064 {
2065         PixbufRenderer *pr;
2066         gint accel;
2067
2068         /* This is a hack, but work far the best, at least for single pointer systems.
2069          * See http://bugzilla.gnome.org/show_bug.cgi?id=587714 for more. */
2070         gint x, y;
2071         gdk_window_get_pointer (bevent->window, &x, &y, NULL);
2072         bevent->x = x;
2073         bevent->y = y;
2074
2075         pr = PIXBUF_RENDERER(widget);
2076
2077         if (pr->scroller_id)
2078                 {
2079                 pr->scroller_xpos = bevent->x;
2080                 pr->scroller_ypos = bevent->y;
2081                 }
2082         
2083         pr->x_mouse = bevent->x;
2084         pr->y_mouse = bevent->y;
2085         pr_update_pixel_signal(pr);
2086         
2087         if (!pr->in_drag || !gdk_pointer_is_grabbed()) return FALSE;
2088
2089         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2090                 {
2091                 pr->drag_moved++;
2092                 }
2093         else
2094                 {
2095                 widget_set_cursor(widget, GDK_FLEUR);
2096                 }
2097
2098         if (bevent->state & GDK_CONTROL_MASK)
2099                 {
2100                 accel = PR_PAN_SHIFT_MULTIPLIER;
2101                 }
2102         else
2103                 {
2104                 accel = 1;
2105                 }
2106
2107         /* do the scroll */
2108         pixbuf_renderer_scroll(pr, (pr->drag_last_x - bevent->x) * accel,
2109                                (pr->drag_last_y - bevent->y) * accel);
2110
2111         pr_drag_signal(pr, bevent);
2112
2113         pr->drag_last_x = bevent->x;
2114         pr->drag_last_y = bevent->y;
2115
2116         /* This is recommended by the GTK+ documentation, but does not work properly.
2117          * Use deprecated way until GTK+ gets a solution for correct motion hint handling:
2118          * http://bugzilla.gnome.org/show_bug.cgi?id=587714
2119          */
2120         /* gdk_event_request_motions (bevent); */
2121         return FALSE;
2122 }
2123
2124 static gboolean pr_leave_notify_cb(GtkWidget *widget, GdkEventCrossing *cevent, gpointer data)
2125 {
2126         PixbufRenderer *pr;
2127
2128         pr = PIXBUF_RENDERER(widget);
2129         pr->x_mouse = -1;
2130         pr->y_mouse = -1;
2131
2132         pr_update_pixel_signal(pr);
2133         return FALSE;
2134 }
2135
2136 static gboolean pr_mouse_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2137 {
2138         PixbufRenderer *pr;
2139         GtkWidget *parent;
2140
2141         pr = PIXBUF_RENDERER(widget);
2142
2143         if (pr->scroller_id) return TRUE;
2144
2145         switch (bevent->button)
2146                 {
2147                 case MOUSE_BUTTON_LEFT:
2148                         pr->in_drag = TRUE;
2149                         pr->drag_last_x = bevent->x;
2150                         pr->drag_last_y = bevent->y;
2151                         pr->drag_moved = 0;
2152                         gdk_pointer_grab(widget->window, FALSE,
2153                                          GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_RELEASE_MASK,
2154                                          NULL, NULL, bevent->time);
2155                         gtk_grab_add(widget);
2156                         break;
2157                 case MOUSE_BUTTON_MIDDLE:
2158                         pr->drag_moved = 0;
2159                         break;
2160                 case MOUSE_BUTTON_RIGHT:
2161                         pr_clicked_signal(pr, bevent);
2162                         break;
2163                 default:
2164                         break;
2165                 }
2166
2167         parent = gtk_widget_get_parent(widget);
2168 #if GTK_CHECK_VERSION(2,20,0)
2169         if (widget && gtk_widget_get_can_focus(parent))
2170 #else
2171         if (widget && GTK_WIDGET_CAN_FOCUS(parent))
2172 #endif
2173                 {
2174                 gtk_widget_grab_focus(parent);
2175                 }
2176
2177         return FALSE;
2178 }
2179
2180 static gboolean pr_mouse_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
2181 {
2182         PixbufRenderer *pr;
2183
2184         pr = PIXBUF_RENDERER(widget);
2185
2186         if (pr->scroller_id)
2187                 {
2188                 pr_scroller_stop(pr);
2189                 return TRUE;
2190                 }
2191
2192 #if GTK_CHECK_VERSION(2,20,0)
2193         if (gdk_pointer_is_grabbed() && gtk_widget_has_grab(GTK_WIDGET(pr)))
2194 #else
2195         if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(pr))
2196 #endif
2197                 {
2198                 gtk_grab_remove(widget);
2199                 gdk_pointer_ungrab(bevent->time);
2200                 widget_set_cursor(widget, -1);
2201                 }
2202
2203         if (pr->drag_moved < PR_DRAG_SCROLL_THRESHHOLD)
2204                 {
2205                 if (bevent->button == MOUSE_BUTTON_LEFT && (bevent->state & GDK_CONTROL_MASK))
2206                         {
2207                         pr_scroller_start(pr, bevent->x, bevent->y);
2208                         }
2209                 else if (bevent->button == MOUSE_BUTTON_LEFT || bevent->button == MOUSE_BUTTON_MIDDLE)
2210                         {
2211                         pr_clicked_signal(pr, bevent);
2212                         }
2213                 }
2214
2215         pr->in_drag = FALSE;
2216
2217         return FALSE;
2218 }
2219
2220 static gboolean pr_mouse_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
2221 {
2222         PixbufRenderer *pr;
2223
2224         pr = PIXBUF_RENDERER(widget);
2225
2226         if (pr->scroller_id)
2227                 {
2228                 pr->scroller_xpos = pr->scroller_x;
2229                 pr->scroller_ypos = pr->scroller_y;
2230                 pr->scroller_xinc = 0;
2231                 pr->scroller_yinc = 0;
2232                 }
2233
2234         return FALSE;
2235 }
2236
2237 static void pr_mouse_drag_cb(GtkWidget *widget, GdkDragContext *context, gpointer data)
2238 {
2239         PixbufRenderer *pr;
2240
2241         pr = PIXBUF_RENDERER(widget);
2242
2243         pr->drag_moved = PR_DRAG_SCROLL_THRESHHOLD;
2244 }
2245
2246 static void pr_signals_connect(PixbufRenderer *pr)
2247 {
2248         g_signal_connect(G_OBJECT(pr), "motion_notify_event",
2249                          G_CALLBACK(pr_mouse_motion_cb), pr);
2250         g_signal_connect(G_OBJECT(pr), "button_press_event",
2251                          G_CALLBACK(pr_mouse_press_cb), pr);
2252         g_signal_connect(G_OBJECT(pr), "button_release_event",
2253                          G_CALLBACK(pr_mouse_release_cb), pr);
2254         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
2255                          G_CALLBACK(pr_mouse_leave_cb), pr);
2256         g_signal_connect(G_OBJECT(pr), "leave_notify_event",
2257                          G_CALLBACK(pr_leave_notify_cb), pr);
2258
2259         gtk_widget_set_events(GTK_WIDGET(pr), GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
2260                                               GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
2261                                               GDK_LEAVE_NOTIFY_MASK);
2262
2263         g_signal_connect(G_OBJECT(pr), "drag_begin",
2264                          G_CALLBACK(pr_mouse_drag_cb), pr);
2265
2266 }
2267
2268 /*
2269  *-------------------------------------------------------------------
2270  * stereo support
2271  *-------------------------------------------------------------------
2272  */
2273
2274 #define COLOR_BYTES 3   /* rgb */
2275 void pr_create_anaglyph(GdkPixbuf *pixbuf, GdkPixbuf *right, gint x, gint y, gint w, gint h)
2276 {
2277         gint srs, drs;
2278         guchar *s_pix, *d_pix;
2279         guchar *sp, *dp;
2280         guchar *spi, *dpi;
2281         gint i, j;
2282
2283         srs = gdk_pixbuf_get_rowstride(right);
2284         s_pix = gdk_pixbuf_get_pixels(right);
2285         spi = s_pix + (x * COLOR_BYTES);
2286
2287         drs = gdk_pixbuf_get_rowstride(pixbuf);
2288         d_pix = gdk_pixbuf_get_pixels(pixbuf);
2289         dpi =  d_pix + x * COLOR_BYTES;
2290
2291         for (i = y; i < y + h; i++)
2292                 {
2293                 sp = spi + (i * srs);
2294                 dp = dpi + (i * drs);
2295                 for (j = 0; j < w; j++)
2296                         {
2297                         *dp = *sp; /* copy red channel */
2298                         sp += COLOR_BYTES;
2299                         dp += COLOR_BYTES;
2300                         }
2301                 }
2302 }
2303  
2304  
2305
2306 /*
2307  *-------------------------------------------------------------------
2308  * public
2309  *-------------------------------------------------------------------
2310  */
2311 static void pr_pixbuf_size_sync(PixbufRenderer *pr)
2312 {
2313         pr->stereo_pixbuf_offset_left = 0;
2314         pr->stereo_pixbuf_offset_right = 0;
2315         if (!pr->pixbuf) return;
2316         gint stereo_data = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(pr->pixbuf), "stereo_data"));
2317         switch (pr->orientation)
2318                 {
2319                 case EXIF_ORIENTATION_LEFT_TOP:
2320                 case EXIF_ORIENTATION_RIGHT_TOP:
2321                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
2322                 case EXIF_ORIENTATION_LEFT_BOTTOM:
2323                         pr->image_width = gdk_pixbuf_get_height(pr->pixbuf);
2324                         pr->image_height = gdk_pixbuf_get_width(pr->pixbuf);
2325                         if (stereo_data == STEREO_PIXBUF_SBS) 
2326                                 {
2327                                 pr->image_height /= 2;
2328                                 pr->stereo_pixbuf_offset_right = pr->image_height;
2329                                 }
2330                         else if (stereo_data == STEREO_PIXBUF_CROSS) 
2331                                 {
2332                                 pr->image_height /= 2;
2333                                 pr->stereo_pixbuf_offset_left = pr->image_height;
2334                                 }
2335                         
2336                         break;
2337                 default:
2338                         pr->image_width = gdk_pixbuf_get_width(pr->pixbuf);
2339                         pr->image_height = gdk_pixbuf_get_height(pr->pixbuf);
2340                         if (stereo_data == STEREO_PIXBUF_SBS) 
2341                                 {
2342                                 pr->image_width /= 2;
2343                                 pr->stereo_pixbuf_offset_right = pr->image_width;
2344                                 }
2345                         else if (stereo_data == STEREO_PIXBUF_CROSS) 
2346                                 {
2347                                 pr->image_width /= 2;
2348                                 pr->stereo_pixbuf_offset_left = pr->image_width;
2349                                 }
2350                 }
2351 }
2352
2353 static void pr_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, PrZoomFlags flags)
2354 {
2355         if (pixbuf) g_object_ref(pixbuf);
2356         if (pr->pixbuf) g_object_unref(pr->pixbuf);
2357         pr->pixbuf = pixbuf;
2358
2359         if (!pr->pixbuf)
2360                 {
2361                 GtkWidget *box;
2362
2363                 /* no pixbuf so just clear the window */
2364                 pr->image_width = 0;
2365                 pr->image_height = 0;
2366                 pr->scale = 1.0;
2367                 pr->zoom = zoom; /* don't throw away the zoom value, it is set by pixbuf_renderer_move, among others,
2368                                     and used for pixbuf_renderer_zoom_get */
2369
2370                 box = GTK_WIDGET(pr);
2371
2372 #if GTK_CHECK_VERSION(2,20,0)
2373                 if (gtk_widget_get_realized(box))
2374 #else
2375                 if (GTK_WIDGET_REALIZED(box))
2376 #endif
2377                         {
2378                         gdk_window_clear(box->window);
2379                         pr->renderer->overlay_draw(pr->renderer, 0, 0, pr->viewport_width, pr->viewport_height);
2380                         if (pr->renderer2) pr->renderer2->overlay_draw(pr->renderer2, 0, 0, pr->viewport_width, pr->viewport_height);
2381                         }
2382
2383                 pr_update_signal(pr);
2384
2385                 return;
2386                 }
2387
2388         if (pr->stereo_mode & PR_STEREO_TEMP_DISABLE) 
2389                 {
2390                 gint disable = !pr->pixbuf || ! GPOINTER_TO_INT(g_object_get_data(G_OBJECT(pr->pixbuf), "stereo_data"));
2391                 pr_stereo_temp_disable(pr, disable);
2392                 }
2393
2394         pr_pixbuf_size_sync(pr);
2395         pr_zoom_sync(pr, zoom, flags | PR_ZOOM_FORCE | PR_ZOOM_NEW, 0, 0);
2396 }
2397
2398 void pixbuf_renderer_set_pixbuf(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom)
2399 {
2400         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2401
2402         pr_source_tile_unset(pr);
2403
2404         pr_set_pixbuf(pr, pixbuf, zoom, 0);
2405
2406         pr_update_signal(pr);
2407 }
2408
2409 void pixbuf_renderer_set_pixbuf_lazy(PixbufRenderer *pr, GdkPixbuf *pixbuf, gdouble zoom, gint orientation)
2410 {
2411         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2412
2413         pr_source_tile_unset(pr);
2414
2415         pr->orientation = orientation;
2416         pr_set_pixbuf(pr, pixbuf, zoom, PR_ZOOM_LAZY);
2417
2418         pr_update_signal(pr);
2419 }
2420
2421 GdkPixbuf *pixbuf_renderer_get_pixbuf(PixbufRenderer *pr)
2422 {
2423         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), NULL);
2424
2425         return pr->pixbuf;
2426 }
2427
2428 void pixbuf_renderer_set_orientation(PixbufRenderer *pr, gint orientation)
2429 {
2430         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2431
2432         pr->orientation = orientation;
2433
2434         pr_pixbuf_size_sync(pr);
2435         pr_zoom_sync(pr, pr->zoom, PR_ZOOM_FORCE, 0, 0);
2436 }
2437
2438 gint pixbuf_renderer_get_orientation(PixbufRenderer *pr)
2439 {
2440         if (!pr) return 1;
2441         return pr->orientation;
2442 }
2443
2444 void pixbuf_renderer_set_post_process_func(PixbufRenderer *pr, PixbufRendererPostProcessFunc func, gpointer user_data, gboolean slow)
2445 {
2446         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2447
2448         pr->func_post_process = func;
2449         pr->post_process_user_data = user_data;
2450         pr->post_process_slow = func && slow;
2451
2452 }
2453
2454
2455 void pixbuf_renderer_move(PixbufRenderer *pr, PixbufRenderer *source)
2456 {
2457         GObject *object;
2458         PixbufRendererScrollResetType scroll_reset;
2459
2460         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2461         g_return_if_fail(IS_PIXBUF_RENDERER(source));
2462
2463         if (pr == source) return;
2464
2465         object = G_OBJECT(pr);
2466
2467         g_object_set(object, "zoom_min", source->zoom_min, NULL);
2468         g_object_set(object, "zoom_max", source->zoom_max, NULL);
2469         g_object_set(object, "loading", source->loading, NULL);
2470
2471         pr->complete = source->complete;
2472
2473         pr->x_scroll = source->x_scroll;
2474         pr->y_scroll = source->y_scroll;
2475         pr->x_mouse  = source->x_mouse;
2476         pr->y_mouse  = source->y_mouse;
2477
2478         scroll_reset = pr->scroll_reset;
2479         pr->scroll_reset = PR_SCROLL_RESET_NOCHANGE;
2480
2481         pr->func_post_process = source->func_post_process;
2482         pr->post_process_user_data = source->post_process_user_data;
2483         pr->post_process_slow = source->post_process_slow;
2484         pr->orientation = source->orientation;
2485
2486         if (source->source_tiles_enabled)
2487                 {
2488                 pr_source_tile_unset(pr);
2489
2490                 pr->source_tiles_enabled = source->source_tiles_enabled;
2491                 pr->source_tiles_cache_size = source->source_tiles_cache_size;
2492                 pr->source_tile_width = source->source_tile_width;
2493                 pr->source_tile_height = source->source_tile_height;
2494                 pr->image_width = source->image_width;
2495                 pr->image_height = source->image_height;
2496
2497                 pr->func_tile_request = source->func_tile_request;
2498                 pr->func_tile_dispose = source->func_tile_dispose;
2499                 pr->func_tile_data = source->func_tile_data;
2500
2501                 pr->source_tiles = source->source_tiles;
2502                 source->source_tiles = NULL;
2503
2504                 pr_zoom_sync(pr, source->zoom, PR_ZOOM_FORCE | PR_ZOOM_NEW, 0, 0);
2505                 pr_redraw(pr, TRUE);
2506                 }
2507         else
2508                 {
2509                 pixbuf_renderer_set_pixbuf(pr, source->pixbuf, source->zoom);
2510                 }
2511
2512         pr->scroll_reset = scroll_reset;
2513
2514         pixbuf_renderer_set_pixbuf(source, NULL, source->zoom);
2515 //      pr_queue_clear(source);
2516 //      pr_tile_free_all(source);
2517 }
2518
2519 void pixbuf_renderer_area_changed(PixbufRenderer *pr, gint src_x, gint src_y, gint src_w, gint src_h)
2520 {
2521         gint x, y, width, height,  x1, y1, x2, y2;
2522
2523         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2524
2525         pr_coords_map_orientation_reverse(pr->orientation,
2526                                      src_x, src_y,
2527                                      pr->image_width, pr->image_height,
2528                                      src_w, src_h,
2529                                      &x, &y,
2530                                      &width, &height);
2531
2532         if (pr->source_tiles_enabled)
2533                 {
2534                 pr_source_tile_changed(pr, x, y, width, height);
2535                 }
2536
2537         if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2538                 {
2539                 /* increase region when using a zoom quality that may access surrounding pixels */
2540                 y -= 1;
2541                 height += 2;
2542                 }
2543
2544         x1 = (gint)floor((gdouble)x * pr->scale);
2545         y1 = (gint)floor((gdouble)y * pr->scale * pr->aspect_ratio);
2546         x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
2547         y2 = (gint)ceil((gdouble)(y + height) * pr->scale * pr->aspect_ratio);
2548
2549         pr->renderer->queue(pr->renderer, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2550         if (pr->renderer2) pr->renderer2->queue(pr->renderer2, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2551 }
2552
2553 void pixbuf_renderer_zoom_adjust(PixbufRenderer *pr, gdouble increment)
2554 {
2555         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2556
2557         pr_zoom_adjust_real(pr, increment, PR_ZOOM_NONE, 0, 0);
2558 }
2559
2560 void pixbuf_renderer_zoom_adjust_at_point(PixbufRenderer *pr, gdouble increment, gint x, gint y)
2561 {
2562         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2563
2564         pr_zoom_adjust_real(pr, increment, PR_ZOOM_CENTER, x, y);
2565 }
2566
2567 void pixbuf_renderer_zoom_set(PixbufRenderer *pr, gdouble zoom)
2568 {
2569         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2570
2571         pr_zoom_sync(pr, zoom, PR_ZOOM_NONE, 0, 0);
2572 }
2573
2574 gdouble pixbuf_renderer_zoom_get(PixbufRenderer *pr)
2575 {
2576         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
2577
2578         return pr->zoom;
2579 }
2580
2581 gdouble pixbuf_renderer_zoom_get_scale(PixbufRenderer *pr)
2582 {
2583         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), 1.0);
2584
2585         return pr->scale;
2586 }
2587
2588 void pixbuf_renderer_zoom_set_limits(PixbufRenderer *pr, gdouble min, gdouble max)
2589 {
2590         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
2591
2592         if (min > 1.0 || max < 1.0) return;
2593         if (min < 1.0 && min > -1.0) return;
2594         if (min < -200.0 || max > 200.0) return;
2595
2596         if (pr->zoom_min != min)
2597                 {
2598                 pr->zoom_min = min;
2599                 g_object_notify(G_OBJECT(pr), "zoom_min");
2600                 }
2601         if (pr->zoom_max != max)
2602                 {
2603                 pr->zoom_max = max;
2604                 g_object_notify(G_OBJECT(pr), "zoom_max");
2605                 }
2606 }
2607
2608 static void pr_stereo_set(PixbufRenderer *pr)
2609 {
2610         if (!pr->renderer) pr->renderer = (void *)renderer_tiles_new(pr);
2611         
2612         pr->renderer->stereo_set(pr->renderer, pr->stereo_mode & ~PR_STEREO_MIRROR_RIGHT & ~PR_STEREO_FLIP_RIGHT);
2613         
2614         if (pr->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT | PR_STEREO_FIXED))
2615                 {
2616                 if (!pr->renderer2) pr->renderer2 = (void *)renderer_tiles_new(pr);
2617                 pr->renderer2->stereo_set(pr->renderer2, (pr->stereo_mode & ~PR_STEREO_MIRROR_LEFT & ~PR_STEREO_FLIP_LEFT) | PR_STEREO_RIGHT);
2618                 }
2619         else
2620                 {
2621                 if (pr->renderer2) pr->renderer2->free(pr->renderer2);
2622                 pr->renderer2 = NULL;
2623                 }
2624 }
2625
2626 void pixbuf_renderer_stereo_set(PixbufRenderer *pr, gint stereo_mode)
2627 {
2628         gboolean redraw = !(pr->stereo_mode == stereo_mode) || pr->stereo_temp_disable;
2629         pr->stereo_mode = stereo_mode;
2630         if ((stereo_mode & PR_STEREO_TEMP_DISABLE) && pr->stereo_temp_disable) return;
2631         
2632         pr->stereo_temp_disable = FALSE;
2633         
2634         pr_stereo_set(pr);
2635         
2636         if (redraw) 
2637                 {
2638                 pr_size_sync(pr, pr->window_width, pr->window_height); /* recalculate new viewport */
2639                 pr_zoom_sync(pr, pr->zoom, PR_ZOOM_FORCE | PR_ZOOM_NEW, 0, 0);
2640                 }
2641 }
2642
2643 void pixbuf_renderer_stereo_fixed_set(PixbufRenderer *pr, gint width, gint height, gint x1, gint y1, gint x2, gint y2)
2644 {
2645         pr->stereo_fixed_width = width;
2646         pr->stereo_fixed_height = height;
2647         pr->stereo_fixed_x_left = x1;
2648         pr->stereo_fixed_y_left = y1;
2649         pr->stereo_fixed_x_right = x2;
2650         pr->stereo_fixed_y_right = y2;
2651 }
2652
2653 gint pixbuf_renderer_stereo_get(PixbufRenderer *pr)
2654 {
2655         return pr->stereo_mode;
2656 }
2657
2658 static void pr_stereo_temp_disable(PixbufRenderer *pr, gboolean disable)
2659 {
2660         if (pr->stereo_temp_disable == disable) return;
2661         pr->stereo_temp_disable = disable;
2662         if (disable)
2663                 {
2664                 if (!pr->renderer) pr->renderer = (void *)renderer_tiles_new(pr);
2665                 pr->renderer->stereo_set(pr->renderer, PR_STEREO_NONE);
2666                 if (pr->renderer2) pr->renderer2->free(pr->renderer2);
2667                 pr->renderer2 = NULL;
2668                 }
2669         else
2670                 {
2671                 pr_stereo_set(pr);
2672                 }
2673         pr_size_sync(pr, pr->window_width, pr->window_height); /* recalculate new viewport */
2674 }
2675
2676 gboolean pixbuf_renderer_get_pixel_colors(PixbufRenderer *pr, gint x_pixel, gint y_pixel, 
2677                                           gint *r_mouse, gint *g_mouse, gint *b_mouse)
2678 {
2679         GdkPixbuf *pb = pr->pixbuf;
2680         gint p_alpha, prs;
2681         guchar *p_pix, *pp;
2682         gint map_x, map_y, map_w, map_h;
2683         
2684         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2685         g_return_val_if_fail(r_mouse != NULL && g_mouse != NULL && b_mouse != NULL, FALSE);
2686
2687         if (!pr->pixbuf && !pr->source_tiles_enabled)
2688                 {
2689                 *r_mouse = -1;
2690                 *g_mouse = -1;
2691                 *b_mouse = -1;
2692                 return FALSE;
2693                 }
2694         
2695         if (!pb) return FALSE;
2696
2697         pr_tile_region_map_orientation(pr->orientation,
2698                                         x_pixel, y_pixel,
2699                                         pr->image_width, pr->image_height,
2700                                         1, 1, /*single pixel */
2701                                         &map_x, &map_y,
2702                                         &map_w, &map_h);
2703
2704         if (map_x < 0 || map_x > gdk_pixbuf_get_width(pr->pixbuf) - 1) return  FALSE;
2705         if (map_y < 0 || map_y > gdk_pixbuf_get_height(pr->pixbuf) - 1) return  FALSE;
2706         
2707         p_alpha = gdk_pixbuf_get_has_alpha(pb);
2708         prs = gdk_pixbuf_get_rowstride(pb);
2709         p_pix = gdk_pixbuf_get_pixels(pb);
2710
2711         pp = p_pix + map_y * prs + (map_x * (p_alpha ? 4 : 3));
2712         *r_mouse = *pp;
2713         pp++;
2714         *g_mouse = *pp;
2715         pp++;
2716         *b_mouse = *pp;
2717         
2718         return TRUE;
2719 }
2720
2721 gboolean pixbuf_renderer_get_mouse_position(PixbufRenderer *pr, gint *x_pixel_return, gint *y_pixel_return)
2722 {
2723         gint x_pixel, y_pixel, x_pixel_clamped, y_pixel_clamped;
2724              
2725         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2726         g_return_val_if_fail(x_pixel_return != NULL && y_pixel_return != NULL, FALSE);
2727
2728         if (!pr->pixbuf && !pr->source_tiles_enabled)
2729                 {
2730                 *x_pixel_return = -1;
2731                 *y_pixel_return = -1;
2732                 return FALSE;
2733                 }
2734         
2735         x_pixel = floor((gdouble)(pr->x_mouse - pr->x_offset + pr->x_scroll) / pr->scale);
2736         y_pixel = floor((gdouble)(pr->y_mouse - pr->y_offset + pr->y_scroll) / pr->scale / pr->aspect_ratio);
2737         x_pixel_clamped = CLAMP(x_pixel, 0, pr->image_width - 1);
2738         y_pixel_clamped = CLAMP(y_pixel, 0, pr->image_height - 1);
2739         
2740         if(x_pixel != x_pixel_clamped || y_pixel != y_pixel_clamped)
2741                 {
2742                 /* mouse is not on pr */
2743                 x_pixel = y_pixel = -1;
2744                 }
2745
2746         *x_pixel_return = x_pixel;
2747         *y_pixel_return = y_pixel;
2748         
2749         return TRUE;
2750 }
2751
2752 gboolean pixbuf_renderer_get_image_size(PixbufRenderer *pr, gint *width, gint *height)
2753 {
2754         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2755         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
2756
2757         if (!pr->pixbuf && !pr->source_tiles_enabled && (!pr->image_width || !pr->image_height))
2758                 {
2759                 *width = 0;
2760                 *height = 0;
2761                 return FALSE;
2762                 }
2763
2764         *width = pr->image_width;
2765         *height = pr->image_height;
2766         return TRUE;
2767 }
2768
2769 gboolean pixbuf_renderer_get_scaled_size(PixbufRenderer *pr, gint *width, gint *height)
2770 {
2771         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2772         g_return_val_if_fail(width != NULL && height != NULL, FALSE);
2773
2774         if (!pr->pixbuf && !pr->source_tiles_enabled && (!pr->image_width || !pr->image_height))
2775                 {
2776                 *width = 0;
2777                 *height = 0;
2778                 return FALSE;
2779                 }
2780
2781         *width = pr->width;
2782         *height = pr->height;
2783         return TRUE;
2784 }
2785
2786 gboolean pixbuf_renderer_get_visible_rect(PixbufRenderer *pr, GdkRectangle *rect)
2787 {
2788         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2789         g_return_val_if_fail(rect != NULL, FALSE);
2790
2791         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
2792             !pr->scale)
2793                 {
2794                 rect->x = 0;
2795                 rect->y = 0;
2796                 rect->width = 0;
2797                 rect->height = 0;
2798                 return FALSE;
2799                 }
2800
2801         rect->x = (gint)((gdouble)pr->x_scroll / pr->scale);
2802         rect->y = (gint)((gdouble)pr->y_scroll / pr->scale / pr->aspect_ratio);
2803         rect->width = (gint)((gdouble)pr->vis_width / pr->scale);
2804         rect->height = (gint)((gdouble)pr->vis_height / pr->scale / pr->aspect_ratio);
2805         return TRUE;
2806 }
2807
2808 gboolean pixbuf_renderer_get_virtual_rect(PixbufRenderer *pr, GdkRectangle *rect)
2809 {
2810         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
2811         g_return_val_if_fail(rect != NULL, FALSE);
2812
2813         if ((!pr->pixbuf && !pr->source_tiles_enabled))
2814                 {
2815                 rect->x = 0;
2816                 rect->y = 0;
2817                 rect->width = 0;
2818                 rect->height = 0;
2819                 return FALSE;
2820                 }
2821
2822         rect->x = pr->x_scroll;
2823         rect->y = pr->y_scroll;
2824         rect->width = pr->vis_width;
2825         rect->height = pr->vis_height;
2826         return TRUE;
2827 }
2828
2829 void pixbuf_renderer_set_size_early(PixbufRenderer *pr, guint width, guint height)
2830 {
2831         gdouble zoom;
2832         gint w, h;
2833
2834         zoom = pixbuf_renderer_zoom_get(pr);
2835         pr->image_width = width;
2836         pr->image_height = height;
2837
2838         pr_zoom_clamp(pr, zoom, PR_ZOOM_FORCE, NULL);
2839
2840         //w = width;
2841         //h = height;
2842
2843         //pr->width = width;
2844         //pr->height = height;
2845 }
2846
2847 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */