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