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