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