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