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