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