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