Bug fix: View in new window wrong size
[geeqie.git] / src / image.c
1 /*
2  * Copyright (C) 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "main.h"
23 #include "image.h"
24
25
26 #include "collect.h"
27 #include "collect-table.h"
28 #include "color-man.h"
29 #include "exif.h"
30 #include "metadata.h"
31 #include "histogram.h"
32 #include "image-load.h"
33 #include "image-overlay.h"
34 #include "layout.h"
35 #include "layout_image.h"
36 #include "pixbuf-renderer.h"
37 #include "pixbuf_util.h"
38 #include "ui_fileops.h"
39
40 #include "filedata.h"
41 #include "filecache.h"
42
43 #include <math.h>
44
45 static GList *image_list = NULL;
46
47 static void image_update_title(ImageWindow *imd);
48 static void image_read_ahead_start(ImageWindow *imd);
49 static void image_cache_set(ImageWindow *imd, FileData *fd);
50
51 // For draw rectangle function
52 static gint pixbuf_start_x;
53 static gint pixbuf_start_y;
54 static gint image_start_x;
55 static gint image_start_y;
56 static gint rect_x1, rect_x2, rect_y1, rect_y2;
57 static gint rect_id = 0;
58
59 /*
60  *-------------------------------------------------------------------
61  * 'signals'
62  *-------------------------------------------------------------------
63  */
64
65 static void image_click_cb(PixbufRenderer *pr, GdkEventButton *event, gpointer data)
66 {
67         ImageWindow *imd = data;
68         if (!options->image_lm_click_nav && event->button == MOUSE_BUTTON_MIDDLE)
69                 {
70                 imd->mouse_wheel_mode = !imd->mouse_wheel_mode;
71                 }
72
73         if (imd->func_button)
74                 {
75                 imd->func_button(imd, event, imd->data_button);
76                 }
77 }
78
79 static void switch_coords_orientation(ImageWindow *imd, gint x, gint y, gint width, gint height)
80 {
81         switch (imd->orientation)
82                 {
83                 case EXIF_ORIENTATION_TOP_LEFT:
84                         /* normal -- nothing to do */
85                         rect_x1 = image_start_x;
86                         rect_y1 = image_start_y;
87                         rect_x2 = x;
88                         rect_y2 = y;
89                         break;
90                 case EXIF_ORIENTATION_TOP_RIGHT:
91                         /* mirrored */
92                         rect_x1 = width - x;
93                         rect_y1 = image_start_y;
94                         rect_x2 = width - image_start_x;
95                         rect_y2 = y;
96                         break;
97                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
98                         /* upside down */
99                         rect_x1 = width - x;
100                         rect_y1 = height - y;
101                         rect_x2 = width - image_start_x;
102                         rect_y2 = height - image_start_y;
103                         break;
104                 case EXIF_ORIENTATION_BOTTOM_LEFT:
105                         /* flipped */
106                         rect_x1 = image_start_x;
107                         rect_y1 = height - y;
108                         rect_x2 = x;
109                         rect_y2 = height - image_start_y;
110                         break;
111                 case EXIF_ORIENTATION_LEFT_TOP:
112                         /* left mirrored */
113                         rect_x1 = image_start_y;
114                         rect_y1 = image_start_x;
115                         rect_x2 = y;
116                         rect_y2 = x;
117                         break;
118                 case EXIF_ORIENTATION_RIGHT_TOP:
119                         /* rotated -90 (270) */
120                         rect_x1 = image_start_y;
121                         rect_y1 = width - x;
122                         rect_x2 = y;
123                         rect_y2 = width - image_start_x;
124                         break;
125                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
126                         /* right mirrored */
127                         rect_x1 = height - y;
128                         rect_y1 = width - x;
129                         rect_x2 = height - image_start_y;
130                         rect_y2 = width - image_start_x;
131                         break;
132                 case EXIF_ORIENTATION_LEFT_BOTTOM:
133                         /* rotated 90 */
134                         rect_x1 = height - y;
135                         rect_y1 = image_start_x;
136                         rect_x2 = height - image_start_y;
137                         rect_y2 = x;
138                         break;
139                 default:
140                         /* The other values are out of range */
141                         break;
142                 }
143 }
144
145 static void image_press_cb(PixbufRenderer *pr, GdkEventButton *event, gpointer data)
146 {
147         ImageWindow *imd = data;
148         LayoutWindow *lw;
149         gint x_pixel, y_pixel;
150
151         if(options->draw_rectangle)
152                 {
153                 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
154
155                 pixbuf_start_x = event->x;
156                 pixbuf_start_y = event->y;
157                 image_start_x = x_pixel;
158                 image_start_y = y_pixel;
159                 }
160
161         if (rect_id)
162                 {
163                 pixbuf_renderer_overlay_remove((PixbufRenderer *)imd->pr, rect_id);
164                 }
165
166         lw = layout_find_by_image(imd);
167         if (lw && event->button == MOUSE_BUTTON_LEFT && event->type == GDK_2BUTTON_PRESS
168                                                                                                 && !options->image_lm_click_nav)
169                 {
170                 if (lw->full_screen)
171                         layout_image_full_screen_stop(lw);
172                 }
173 }
174
175 static void image_drag_cb(PixbufRenderer *pr, GdkEventMotion *event, gpointer data)
176 {
177         ImageWindow *imd = data;
178         gint width, height;
179         gint rect_width;
180         gint rect_height;
181         GdkPixbuf *rect_pixbuf;
182         gint x_pixel, y_pixel;
183
184         if (options->draw_rectangle)
185                 {
186                 pixbuf_renderer_get_image_size(pr, &width, &height);
187                 pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
188                 switch_coords_orientation(imd, x_pixel, y_pixel, width, height);
189
190                 if (rect_id)
191                         {
192                         pixbuf_renderer_overlay_remove((PixbufRenderer *)imd->pr, rect_id);
193                         }
194
195                 rect_width = pr->drag_last_x - pixbuf_start_x;
196                 if (rect_width <= 0)
197                         {
198                         rect_width = 1;
199                         }
200                 rect_height = pr->drag_last_y - pixbuf_start_y;
201                 if (rect_height <= 0)
202                         {
203                         rect_height = 1;
204                         }
205
206                 rect_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, rect_width, rect_height);
207                 pixbuf_set_rect_fill(rect_pixbuf, 0, 0, rect_width, rect_height, 255, 255, 255, 0);
208                 pixbuf_set_rect(rect_pixbuf, 1, 1, rect_width-2, rect_height - 2, 0, 0, 0, 255, 1, 1, 1, 1);
209                 pixbuf_set_rect(rect_pixbuf, 2, 2, rect_width-4, rect_height - 4, 255, 255, 255, 255, 1, 1, 1, 1);
210
211                 rect_id = pixbuf_renderer_overlay_add((PixbufRenderer *)imd->pr, rect_pixbuf, pixbuf_start_x, pixbuf_start_y, OVL_NORMAL);
212                 }
213
214         pixbuf_renderer_get_scaled_size(pr, &width, &height);
215
216         if (imd->func_drag)
217                 {
218                 imd->func_drag(imd, event,
219                                (gfloat)(pr->drag_last_x - event->x) / width,
220                                (gfloat)(pr->drag_last_y - event->y) / height,
221                                imd->data_button);
222                 }
223 }
224
225 static void image_scroll_notify_cb(PixbufRenderer *pr, gpointer data)
226 {
227         ImageWindow *imd = data;
228
229         if (imd->func_scroll_notify && pr->scale)
230                 {
231                 imd->func_scroll_notify(imd,
232                                         (gint)((gdouble)pr->x_scroll / pr->scale),
233                                         (gint)((gdouble)pr->y_scroll / pr->scale),
234                                         (gint)((gdouble)pr->image_width - pr->vis_width / pr->scale),
235                                         (gint)((gdouble)pr->image_height - pr->vis_height / pr->scale),
236                                         imd->data_scroll_notify);
237                 }
238 }
239
240 static void image_update_util(ImageWindow *imd)
241 {
242         if (imd->func_update) imd->func_update(imd, imd->data_update);
243 }
244
245
246 static void image_complete_util(ImageWindow *imd, gboolean preload)
247 {
248         if (imd->il && image_get_pixbuf(imd) != image_loader_get_pixbuf(imd->il)) return;
249
250         DEBUG_1("%s image load completed \"%s\" (%s)", get_exec_time(),
251                           (preload) ? (imd->read_ahead_fd ? imd->read_ahead_fd->path : "null") :
252                                       (imd->image_fd ? imd->image_fd->path : "null"),
253                           (preload) ? "preload" : "current");
254
255         if (!preload) imd->completed = TRUE;
256         if (imd->func_complete) imd->func_complete(imd, preload, imd->data_complete);
257 }
258
259 static void image_render_complete_cb(PixbufRenderer *pr, gpointer data)
260 {
261         ImageWindow *imd = data;
262
263         image_complete_util(imd, FALSE);
264 }
265
266 static void image_state_set(ImageWindow *imd, ImageState state)
267 {
268         if (state == IMAGE_STATE_NONE)
269                 {
270                 imd->state = state;
271                 }
272         else
273                 {
274                 imd->state |= state;
275                 }
276         if (imd->func_state) imd->func_state(imd, state, imd->data_state);
277 }
278
279 static void image_state_unset(ImageWindow *imd, ImageState state)
280 {
281         imd->state &= ~state;
282         if (imd->func_state) imd->func_state(imd, state, imd->data_state);
283 }
284
285 static void image_zoom_cb(PixbufRenderer *pr, gdouble zoom, gpointer data)
286 {
287         ImageWindow *imd = data;
288
289         if (imd->title_show_zoom) image_update_title(imd);
290         image_state_set(imd, IMAGE_STATE_IMAGE);
291         image_update_util(imd);
292 }
293
294 /*
295  *-------------------------------------------------------------------
296  * misc
297  *-------------------------------------------------------------------
298  */
299
300 static void image_update_title(ImageWindow *imd)
301 {
302         gchar *title = NULL;
303         gchar *zoom = NULL;
304         gchar *collection = NULL;
305         LayoutWindow *lw;
306         gchar *lw_ident = NULL;
307
308         if (!imd->top_window) return;
309
310         if (imd->collection && collection_to_number(imd->collection) >= 0)
311                 {
312                 const gchar *name = imd->collection->name;
313                 if (!name) name = _("Untitled");
314                 collection = g_strdup_printf(_(" (Collection %s)"), name);
315                 }
316
317         if (imd->title_show_zoom)
318                 {
319                 gchar *buf = image_zoom_get_as_text(imd);
320                 zoom = g_strconcat(" [", buf, "]", NULL);
321                 g_free(buf);
322                 }
323
324         lw = layout_find_by_image(imd);
325         if (lw)
326                 {
327                 lw_ident = g_strconcat(" (", lw->options.id, ")", NULL);
328                 }
329
330         title = g_strdup_printf("%s%s%s%s%s%s%s",
331                 imd->title ? imd->title : "",
332                 imd->image_fd ? imd->image_fd->name : "",
333                 zoom ? zoom : "",
334                 collection ? collection : "",
335                 imd->image_fd ? " - " : "",
336                 imd->title_right ? imd->title_right : "",
337                 options->show_window_ids ? (lw_ident ? lw_ident : "") : ""
338                 );
339         if (lw_ident)
340                 {
341                 g_free(lw_ident);
342                 }
343
344         gtk_window_set_title(GTK_WINDOW(imd->top_window), title);
345
346         g_free(title);
347         g_free(zoom);
348         g_free(collection);
349 }
350
351 /*
352  *-------------------------------------------------------------------
353  * rotation, flip, etc.
354  *-------------------------------------------------------------------
355  */
356 static gboolean image_get_x11_screen_profile(ImageWindow *imd, guchar **screen_profile, gint *screen_profile_len)
357 {
358         GdkScreen *screen = gtk_widget_get_screen(imd->widget);;
359         GdkAtom    type   = GDK_NONE;
360         gint       format = 0;
361
362         return (gdk_property_get(gdk_screen_get_root_window(screen),
363                                  gdk_atom_intern ("_ICC_PROFILE", FALSE),
364                                  GDK_NONE,
365                                  0, 64 * 1024 * 1024, FALSE,
366                                  &type, &format, screen_profile_len, screen_profile) && *screen_profile_len > 0);
367 }
368
369 static gboolean image_post_process_color(ImageWindow *imd, gint start_row, gboolean run_in_bg)
370 {
371         ColorMan *cm;
372         ColorManProfileType input_type;
373         ColorManProfileType screen_type;
374         const gchar *input_file = NULL;
375         const gchar *screen_file = NULL;
376         guchar *profile = NULL;
377         guint profile_len;
378         guchar *screen_profile = NULL;
379         gint screen_profile_len;
380         ExifData *exif;
381
382         if (imd->cm) return FALSE;
383
384         if (imd->color_profile_input >= COLOR_PROFILE_FILE &&
385             imd->color_profile_input <  COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS)
386                 {
387                 const gchar *file = options->color_profile.input_file[imd->color_profile_input - COLOR_PROFILE_FILE];
388
389                 if (!is_readable_file(file)) return FALSE;
390
391                 input_type = COLOR_PROFILE_FILE;
392                 input_file = file;
393                 }
394         else if (imd->color_profile_input >= COLOR_PROFILE_SRGB &&
395                  imd->color_profile_input <  COLOR_PROFILE_FILE)
396                 {
397                 input_type = imd->color_profile_input;
398                 input_file = NULL;
399                 }
400         else
401                 {
402                 return FALSE;
403                 }
404
405         if (options->color_profile.use_x11_screen_profile &&
406             image_get_x11_screen_profile(imd, &screen_profile, &screen_profile_len))
407                 {
408                 screen_type = COLOR_PROFILE_MEM;
409                 DEBUG_1("Using X11 screen profile, length: %d", screen_profile_len);
410                 }
411         else if (options->color_profile.screen_file &&
412             is_readable_file(options->color_profile.screen_file))
413                 {
414                 screen_type = COLOR_PROFILE_FILE;
415                 screen_file = options->color_profile.screen_file;
416                 }
417         else
418                 {
419                 screen_type = COLOR_PROFILE_SRGB;
420                 screen_file = NULL;
421                 }
422
423
424         imd->color_profile_from_image = COLOR_PROFILE_NONE;
425
426         exif = exif_read_fd(imd->image_fd);
427
428         if (exif)
429                 {
430                 profile = exif_get_color_profile(exif, &profile_len);
431                 if (profile)
432                         {
433                         if (!imd->color_profile_use_image)
434                                 {
435                                 g_free(profile);
436                                 profile = NULL;
437                                 }
438                         DEBUG_1("Found embedded color profile");
439                         imd->color_profile_from_image = COLOR_PROFILE_MEM;
440                         }
441                 else
442                         {
443                         gchar *interop_index = exif_get_data_as_text(exif, "Exif.Iop.InteroperabilityIndex");
444
445                         if (interop_index)
446                                 {
447                                 /* Exif 2.21 specification */
448                                 if (!strcmp(interop_index, "R98"))
449                                         {
450                                         imd->color_profile_from_image = COLOR_PROFILE_SRGB;
451                                         DEBUG_1("Found EXIF 2.21 ColorSpace of sRGB");
452                                         }
453                                 else if (!strcmp(interop_index, "R03"))
454                                         {
455                                         imd->color_profile_from_image = COLOR_PROFILE_ADOBERGB;
456                                         DEBUG_1("Found EXIF 2.21 ColorSpace of AdobeRGB");
457                                         }
458                                 g_free(interop_index);
459                                 }
460                         else
461                                 {
462                                 gint cs;
463
464                                 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */
465                                 if (!exif_get_integer(exif, "Exif.Photo.ColorSpace", &cs)) cs = 0;
466                                 if (cs == 1)
467                                         {
468                                         imd->color_profile_from_image = COLOR_PROFILE_SRGB;
469                                         DEBUG_1("Found EXIF 2.2 ColorSpace of sRGB");
470                                         }
471                                 else if (cs == 2)
472                                         {
473                                         /* non-standard way of specifying AdobeRGB (used by some software) */
474                                         imd->color_profile_from_image = COLOR_PROFILE_ADOBERGB;
475                                         DEBUG_1("Found EXIF 2.2 ColorSpace of AdobeRGB");
476                                         }
477                                 }
478
479                         if (imd->color_profile_use_image && imd->color_profile_from_image != COLOR_PROFILE_NONE)
480                                {
481                                input_type = imd->color_profile_from_image;
482                                input_file = NULL;
483                                }
484                         }
485
486                 exif_free_fd(imd->image_fd, exif);
487                 }
488
489
490         if (profile)
491                 {
492                 cm = color_man_new_embedded(run_in_bg ? imd : NULL, NULL,
493                                             profile, profile_len,
494                                             screen_type, screen_file, screen_profile, screen_profile_len);
495                 g_free(profile);
496                 }
497         else
498                 {
499                 cm = color_man_new(run_in_bg ? imd : NULL, NULL,
500                                    input_type, input_file,
501                                    screen_type, screen_file, screen_profile, screen_profile_len);
502                 }
503
504         if (cm)
505                 {
506                 if (start_row > 0)
507                         {
508                         cm->row = start_row;
509                         cm->incremental_sync = TRUE;
510                         }
511
512                 imd->cm = (gpointer)cm;
513                 }
514
515         image_update_util(imd);
516
517         if (screen_profile)
518                 {
519                 g_free(screen_profile);
520                 screen_profile = NULL;
521                 }
522
523         return !!cm;
524 }
525
526
527 static void image_post_process_tile_color_cb(PixbufRenderer *pr, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h, gpointer data)
528 {
529         ImageWindow *imd = (ImageWindow *)data;
530         if (imd->cm) color_man_correct_region(imd->cm, *pixbuf, x, y, w, h);
531         if (imd->desaturate) pixbuf_desaturate_rect(*pixbuf, x, y, w, h);
532
533 }
534
535 void image_alter_orientation(ImageWindow *imd, FileData *fd_n, AlterType type)
536 {
537         static const gint rotate_90[]    = {1,   6, 7, 8, 5, 2, 3, 4, 1};
538         static const gint rotate_90_cc[] = {1,   8, 5, 6, 7, 4, 1, 2, 3};
539         static const gint rotate_180[]   = {1,   3, 4, 1, 2, 7, 8, 5, 6};
540         static const gint mirror[]       = {1,   2, 1, 4, 3, 6, 5, 8, 7};
541         static const gint flip[]         = {1,   4, 3, 2, 1, 8, 7, 6, 5};
542
543         gint orientation;
544
545         if (!imd || !imd->pr || !imd->image_fd || !fd_n) return;
546
547         orientation = EXIF_ORIENTATION_TOP_LEFT;
548         {
549         if (fd_n->user_orientation)
550                 {
551                 orientation = fd_n->user_orientation;
552                 }
553         else
554                 if (options->metadata.write_orientation)
555                         {
556                         orientation = metadata_read_int(fd_n, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
557                         }
558         }
559
560         switch (type)
561                 {
562                 case ALTER_ROTATE_90:
563                         orientation = rotate_90[orientation];
564                         break;
565                 case ALTER_ROTATE_90_CC:
566                         orientation = rotate_90_cc[orientation];
567                         break;
568                 case ALTER_ROTATE_180:
569                         orientation = rotate_180[orientation];
570                         break;
571                 case ALTER_MIRROR:
572                         orientation = mirror[orientation];
573                         break;
574                 case ALTER_FLIP:
575                         orientation = flip[orientation];
576                         break;
577                 case ALTER_NONE:
578                         orientation = fd_n->exif_orientation ? fd_n->exif_orientation : 1;
579                         break;
580                 default:
581                         return;
582                         break;
583                 }
584
585         if (orientation != (fd_n->exif_orientation ? fd_n->exif_orientation : 1))
586                 {
587                 if (!options->metadata.write_orientation)
588                         {
589                         /* user_orientation does not work together with options->metadata.write_orientation,
590                            use either one or the other.
591                            we must however handle switching metadata.write_orientation on and off, therefore
592                            we just disable referencing new fd's, not unreferencing the old ones
593                         */
594                         if (fd_n->user_orientation == 0) file_data_ref(fd_n);
595                         fd_n->user_orientation = orientation;
596                         }
597                 }
598         else
599                 {
600                 if (fd_n->user_orientation != 0) file_data_unref(fd_n);
601                 fd_n->user_orientation = 0;
602                 }
603
604         if (options->metadata.write_orientation)
605                 {
606                 if (type == ALTER_NONE)
607                         {
608                         metadata_write_revert(fd_n, ORIENTATION_KEY);
609                         }
610                 else
611                         {
612                         metadata_write_int(fd_n, ORIENTATION_KEY, orientation);
613                         }
614                 }
615
616         if (imd->image_fd == fd_n && !(options->metadata.write_orientation && !options->image.exif_rotate_enable))
617                 {
618                 imd->orientation = orientation;
619                 pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, orientation);
620                 }
621 }
622
623 void image_set_desaturate(ImageWindow *imd, gboolean desaturate)
624 {
625         imd->desaturate = desaturate;
626         if (imd->cm || imd->desaturate)
627                 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
628         else
629                 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
630         pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, imd->orientation);
631 }
632
633 gboolean image_get_desaturate(ImageWindow *imd)
634 {
635         return imd->desaturate;
636 }
637
638 /*
639  *-------------------------------------------------------------------
640  * read ahead (prebuffer)
641  *-------------------------------------------------------------------
642  */
643
644 static void image_read_ahead_cancel(ImageWindow *imd)
645 {
646         DEBUG_1("%s read ahead cancelled for :%s", get_exec_time(), imd->read_ahead_fd ? imd->read_ahead_fd->path : "null");
647
648         image_loader_free(imd->read_ahead_il);
649         imd->read_ahead_il = NULL;
650
651         file_data_unref(imd->read_ahead_fd);
652         imd->read_ahead_fd = NULL;
653 }
654
655 static void image_read_ahead_done_cb(ImageLoader *il, gpointer data)
656 {
657         ImageWindow *imd = data;
658
659         if (!imd->read_ahead_fd || !imd->read_ahead_il) return;
660
661         DEBUG_1("%s read ahead done for :%s", get_exec_time(), imd->read_ahead_fd->path);
662
663         if (!imd->read_ahead_fd->pixbuf)
664                 {
665                 imd->read_ahead_fd->pixbuf = image_loader_get_pixbuf(imd->read_ahead_il);
666                 if (imd->read_ahead_fd->pixbuf)
667                         {
668                         g_object_ref(imd->read_ahead_fd->pixbuf);
669                         image_cache_set(imd, imd->read_ahead_fd);
670                         }
671                 }
672         image_loader_free(imd->read_ahead_il);
673         imd->read_ahead_il = NULL;
674
675         image_complete_util(imd, TRUE);
676 }
677
678 static void image_read_ahead_error_cb(ImageLoader *il, gpointer data)
679 {
680         /* we even treat errors as success, maybe at least some of the file was ok */
681         image_read_ahead_done_cb(il, data);
682 }
683
684 static void image_read_ahead_start(ImageWindow *imd)
685 {
686         /* already started ? */
687         if (!imd->read_ahead_fd || imd->read_ahead_il || imd->read_ahead_fd->pixbuf) return;
688
689         /* still loading ?, do later */
690         if (imd->il /*|| imd->cm*/) return;
691
692         DEBUG_1("%s read ahead started for :%s", get_exec_time(), imd->read_ahead_fd->path);
693
694         imd->read_ahead_il = image_loader_new(imd->read_ahead_fd);
695
696         image_loader_delay_area_ready(imd->read_ahead_il, TRUE); /* we will need the area_ready signals later */
697
698         g_signal_connect(G_OBJECT(imd->read_ahead_il), "error", (GCallback)image_read_ahead_error_cb, imd);
699         g_signal_connect(G_OBJECT(imd->read_ahead_il), "done", (GCallback)image_read_ahead_done_cb, imd);
700
701         if (!image_loader_start(imd->read_ahead_il))
702                 {
703                 image_read_ahead_cancel(imd);
704                 image_complete_util(imd, TRUE);
705                 }
706 }
707
708 static void image_read_ahead_set(ImageWindow *imd, FileData *fd)
709 {
710         if (imd->read_ahead_fd && fd && imd->read_ahead_fd == fd) return;
711
712         image_read_ahead_cancel(imd);
713
714         imd->read_ahead_fd = file_data_ref(fd);
715
716         DEBUG_1("read ahead set to :%s", imd->read_ahead_fd->path);
717
718         image_read_ahead_start(imd);
719 }
720
721 /*
722  *-------------------------------------------------------------------
723  * post buffering
724  *-------------------------------------------------------------------
725  */
726
727 static void image_cache_release_cb(FileData *fd)
728 {
729         g_object_unref(fd->pixbuf);
730         fd->pixbuf = NULL;
731 }
732
733 static FileCacheData *image_get_cache(void)
734 {
735         static FileCacheData *cache = NULL;
736         if (!cache) cache = file_cache_new(image_cache_release_cb, 1);
737         file_cache_set_max_size(cache, (gulong)options->image.image_cache_max * 1048576); /* update from options */
738         return cache;
739 }
740
741 static void image_cache_set(ImageWindow *imd, FileData *fd)
742 {
743         g_assert(fd->pixbuf);
744
745         file_cache_put(image_get_cache(), fd, (gulong)gdk_pixbuf_get_rowstride(fd->pixbuf) * (gulong)gdk_pixbuf_get_height(fd->pixbuf));
746         file_data_send_notification(fd, NOTIFY_PIXBUF); /* to update histogram */
747 }
748
749 static gint image_cache_get(ImageWindow *imd)
750 {
751         gint success;
752
753         success = file_cache_get(image_get_cache(), imd->image_fd);
754         if (success)
755                 {
756                 g_assert(imd->image_fd->pixbuf);
757                 image_change_pixbuf(imd, imd->image_fd->pixbuf, image_zoom_get(imd), FALSE);
758                 }
759
760 //      file_cache_dump(image_get_cache());
761         return success;
762 }
763
764 /*
765  *-------------------------------------------------------------------
766  * loading
767  *-------------------------------------------------------------------
768  */
769
770 static void image_load_pixbuf_ready(ImageWindow *imd)
771 {
772         if (image_get_pixbuf(imd) || !imd->il) return;
773
774         image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), FALSE);
775 }
776
777 static void image_load_area_cb(ImageLoader *il, guint x, guint y, guint w, guint h, gpointer data)
778 {
779         ImageWindow *imd = data;
780         PixbufRenderer *pr;
781
782         pr = (PixbufRenderer *)imd->pr;
783
784         if (imd->delay_flip &&
785             pr->pixbuf != image_loader_get_pixbuf(il))
786                 {
787                 return;
788                 }
789
790         if (!pr->pixbuf) image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
791
792         pixbuf_renderer_area_changed(pr, x, y, w, h);
793 }
794
795 static void image_load_done_cb(ImageLoader *il, gpointer data)
796 {
797         ImageWindow *imd = data;
798
799         DEBUG_1("%s image done", get_exec_time());
800
801         if (options->image.enable_read_ahead && imd->image_fd && !imd->image_fd->pixbuf && image_loader_get_pixbuf(imd->il))
802                 {
803                 imd->image_fd->pixbuf = g_object_ref(image_loader_get_pixbuf(imd->il));
804                 image_cache_set(imd, imd->image_fd);
805                 }
806         /* call the callback triggered by image_state after fd->pixbuf is set */
807         g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
808         image_state_unset(imd, IMAGE_STATE_LOADING);
809
810         if (!image_loader_get_pixbuf(imd->il))
811                 {
812                 GdkPixbuf *pixbuf;
813
814                 switch (imd->image_fd->format_class)
815                         {
816                         case FORMAT_CLASS_UNKNOWN:
817                                 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
818                                 break;
819                         case FORMAT_CLASS_META:
820                                 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
821                                 break;
822                         case FORMAT_CLASS_VIDEO:
823                                 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
824                         case FORMAT_CLASS_COLLECTION:
825                                 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
826                                 break;
827                         case FORMAT_CLASS_PDF:
828                                 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
829                                 break;
830                         default:
831                                 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
832                         }
833
834                 image_change_pixbuf(imd, pixbuf, image_zoom_get(imd), FALSE);
835                 g_object_unref(pixbuf);
836
837                 imd->unknown = TRUE;
838                 }
839         else if (imd->delay_flip &&
840             image_get_pixbuf(imd) != image_loader_get_pixbuf(imd->il))
841                 {
842                 g_object_set(G_OBJECT(imd->pr), "complete", FALSE, NULL);
843                 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), FALSE);
844                 }
845
846         image_loader_free(imd->il);
847         imd->il = NULL;
848
849 //      image_post_process(imd, TRUE);
850
851         image_read_ahead_start(imd);
852 }
853
854 static void image_load_size_cb(ImageLoader *il, guint width, guint height, gpointer data)
855 {
856         ImageWindow *imd = data;
857
858         DEBUG_1("image_load_size_cb: %dx%d", width, height);
859         pixbuf_renderer_set_size_early((PixbufRenderer *)imd->pr, width, height);
860 }
861
862 static void image_load_error_cb(ImageLoader *il, gpointer data)
863 {
864         DEBUG_1("%s image error", get_exec_time());
865
866         /* even on error handle it like it was done,
867          * since we have a pixbuf with _something_ */
868
869         image_load_done_cb(il, data);
870 }
871
872 static void image_load_set_signals(ImageWindow *imd, gboolean override_old_signals)
873 {
874         g_assert(imd->il);
875         if (override_old_signals)
876                 {
877                 /* override the old signals */
878                 g_signal_handlers_disconnect_matched(G_OBJECT(imd->il), G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, imd);
879                 }
880
881         g_signal_connect(G_OBJECT(imd->il), "area_ready", (GCallback)image_load_area_cb, imd);
882         g_signal_connect(G_OBJECT(imd->il), "error", (GCallback)image_load_error_cb, imd);
883         g_signal_connect(G_OBJECT(imd->il), "done", (GCallback)image_load_done_cb, imd);
884         g_signal_connect(G_OBJECT(imd->il), "size_prepared", (GCallback)image_load_size_cb, imd);
885 }
886
887 /* this read ahead is located here merely for the callbacks, above */
888
889 static gboolean image_read_ahead_check(ImageWindow *imd)
890 {
891         if (!imd->read_ahead_fd) return FALSE;
892         if (imd->il) return FALSE;
893
894         if (!imd->image_fd || imd->read_ahead_fd != imd->image_fd)
895                 {
896                 image_read_ahead_cancel(imd);
897                 return FALSE;
898                 }
899
900         if (imd->read_ahead_il)
901                 {
902                 imd->il = imd->read_ahead_il;
903                 imd->read_ahead_il = NULL;
904
905                 image_load_set_signals(imd, TRUE);
906
907                 g_object_set(G_OBJECT(imd->pr), "loading", TRUE, NULL);
908                 image_state_set(imd, IMAGE_STATE_LOADING);
909
910                 if (!imd->delay_flip)
911                         {
912                         image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
913                         }
914
915                 image_loader_delay_area_ready(imd->il, FALSE); /* send the delayed area_ready signals */
916
917                 file_data_unref(imd->read_ahead_fd);
918                 imd->read_ahead_fd = NULL;
919                 return TRUE;
920                 }
921         else if (imd->read_ahead_fd->pixbuf)
922                 {
923                 image_change_pixbuf(imd, imd->read_ahead_fd->pixbuf, image_zoom_get(imd), FALSE);
924
925                 file_data_unref(imd->read_ahead_fd);
926                 imd->read_ahead_fd = NULL;
927
928 //              image_post_process(imd, FALSE);
929                 return TRUE;
930                 }
931
932         image_read_ahead_cancel(imd);
933         return FALSE;
934 }
935
936 static gboolean image_load_begin(ImageWindow *imd, FileData *fd)
937 {
938         DEBUG_1("%s image begin", get_exec_time());
939
940         if (imd->il) return FALSE;
941
942         imd->completed = FALSE;
943         g_object_set(G_OBJECT(imd->pr), "complete", FALSE, NULL);
944
945         if (image_cache_get(imd))
946                 {
947                 DEBUG_1("from cache: %s", imd->image_fd->path);
948                 return TRUE;
949                 }
950
951         if (image_read_ahead_check(imd))
952                 {
953                 DEBUG_1("from read ahead buffer: %s", imd->image_fd->path);
954                 return TRUE;
955                 }
956
957         if (!imd->delay_flip && image_get_pixbuf(imd))
958                 {
959                 PixbufRenderer *pr;
960
961                 pr = PIXBUF_RENDERER(imd->pr);
962                 if (pr->pixbuf) g_object_unref(pr->pixbuf);
963                 pr->pixbuf = NULL;
964                 }
965
966         g_object_set(G_OBJECT(imd->pr), "loading", TRUE, NULL);
967
968         imd->il = image_loader_new(fd);
969
970         image_load_set_signals(imd, FALSE);
971
972         if (!image_loader_start(imd->il))
973                 {
974                 DEBUG_1("image start error");
975
976                 g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
977
978                 image_loader_free(imd->il);
979                 imd->il = NULL;
980
981                 image_complete_util(imd, FALSE);
982
983                 return FALSE;
984                 }
985
986         image_state_set(imd, IMAGE_STATE_LOADING);
987
988 /*
989         if (!imd->delay_flip && !image_get_pixbuf(imd) && image_loader_get_pixbuf(imd->il))
990                 {
991                 image_change_pixbuf(imd, image_loader_get_pixbuf(imd->il), image_zoom_get(imd), TRUE);
992                 }
993 */
994         return TRUE;
995 }
996
997 static void image_reset(ImageWindow *imd)
998 {
999         /* stops anything currently being done */
1000
1001         DEBUG_1("%s image reset", get_exec_time());
1002
1003         g_object_set(G_OBJECT(imd->pr), "loading", FALSE, NULL);
1004
1005         image_loader_free(imd->il);
1006         imd->il = NULL;
1007
1008         color_man_free((ColorMan *)imd->cm);
1009         imd->cm = NULL;
1010
1011         imd->delay_alter_type = ALTER_NONE;
1012
1013         image_state_set(imd, IMAGE_STATE_NONE);
1014 }
1015
1016 /*
1017  *-------------------------------------------------------------------
1018  * image changer
1019  *-------------------------------------------------------------------
1020  */
1021
1022 static void image_change_complete(ImageWindow *imd, gdouble zoom)
1023 {
1024         image_reset(imd);
1025         imd->unknown = TRUE;
1026
1027         if (!imd->image_fd)
1028                 {
1029                 image_change_pixbuf(imd, NULL, zoom, FALSE);
1030                 }
1031         else
1032                 {
1033
1034                 if (is_readable_file(imd->image_fd->path))
1035                         {
1036                         PixbufRenderer *pr;
1037
1038                         pr = PIXBUF_RENDERER(imd->pr);
1039                         pr->zoom = zoom;        /* store the zoom, needed by the loader */
1040
1041                         if (image_load_begin(imd, imd->image_fd))
1042                                 {
1043                                 imd->unknown = FALSE;
1044                                 }
1045                         }
1046
1047                 if (imd->unknown == TRUE)
1048                         {
1049                         GdkPixbuf *pixbuf;
1050
1051                         pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
1052                         image_change_pixbuf(imd, pixbuf, zoom, FALSE);
1053                         g_object_unref(pixbuf);
1054                         }
1055                 }
1056
1057         image_update_util(imd);
1058 }
1059
1060 static void image_change_real(ImageWindow *imd, FileData *fd,
1061                               CollectionData *cd, CollectInfo *info, gdouble zoom)
1062 {
1063
1064         imd->collection = cd;
1065         imd->collection_info = info;
1066
1067         if (imd->auto_refresh && imd->image_fd)
1068                 file_data_unregister_real_time_monitor(imd->image_fd);
1069
1070         file_data_unref(imd->image_fd);
1071         imd->image_fd = file_data_ref(fd);
1072
1073
1074         image_change_complete(imd, zoom);
1075
1076         image_update_title(imd);
1077         image_state_set(imd, IMAGE_STATE_IMAGE);
1078
1079         if (imd->auto_refresh && imd->image_fd)
1080                 file_data_register_real_time_monitor(imd->image_fd);
1081 }
1082
1083 /*
1084  *-------------------------------------------------------------------
1085  * focus stuff
1086  *-------------------------------------------------------------------
1087  */
1088
1089 static gboolean image_focus_in_cb(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1090 {
1091         ImageWindow *imd = data;
1092
1093         if (imd->func_focus_in)
1094                 {
1095                 imd->func_focus_in(imd, imd->data_focus_in);
1096                 }
1097
1098         return TRUE;
1099 }
1100
1101 static gboolean image_scroll_cb(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1102 {
1103         ImageWindow *imd = data;
1104
1105         if (imd->func_scroll &&
1106             event && event->type == GDK_SCROLL)
1107                 {
1108                 imd->func_scroll(imd, event, imd->data_scroll);
1109                 return TRUE;
1110                 }
1111
1112         return FALSE;
1113 }
1114
1115 /*
1116  *-------------------------------------------------------------------
1117  * public interface
1118  *-------------------------------------------------------------------
1119  */
1120
1121 void image_attach_window(ImageWindow *imd, GtkWidget *window,
1122                          const gchar *title, const gchar *title_right, gboolean show_zoom)
1123 {
1124         LayoutWindow *lw;
1125
1126         imd->top_window = window;
1127         g_free(imd->title);
1128         imd->title = g_strdup(title);
1129         g_free(imd->title_right);
1130         imd->title_right = g_strdup(title_right);
1131         imd->title_show_zoom = show_zoom;
1132
1133         lw = layout_find_by_image(imd);
1134
1135         if (!(options->image.fit_window_to_image && lw && lw->options.tools_float)) window = NULL;
1136
1137         pixbuf_renderer_set_parent((PixbufRenderer *)imd->pr, (GtkWindow *)window);
1138
1139         image_update_title(imd);
1140 }
1141
1142 void image_set_update_func(ImageWindow *imd,
1143                            void (*func)(ImageWindow *imd, gpointer data),
1144                            gpointer data)
1145 {
1146         imd->func_update = func;
1147         imd->data_update = data;
1148 }
1149
1150 void image_set_complete_func(ImageWindow *imd,
1151                              void (*func)(ImageWindow *imd, gboolean preload, gpointer data),
1152                              gpointer data)
1153 {
1154         imd->func_complete = func;
1155         imd->data_complete = data;
1156 }
1157
1158 void image_set_state_func(ImageWindow *imd,
1159                         void (*func)(ImageWindow *imd, ImageState state, gpointer data),
1160                         gpointer data)
1161 {
1162         imd->func_state = func;
1163         imd->data_state = data;
1164 }
1165
1166
1167 void image_set_button_func(ImageWindow *imd,
1168                            void (*func)(ImageWindow *, GdkEventButton *event, gpointer),
1169                            gpointer data)
1170 {
1171         imd->func_button = func;
1172         imd->data_button = data;
1173 }
1174
1175 void image_set_drag_func(ImageWindow *imd,
1176                            void (*func)(ImageWindow *, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer),
1177                            gpointer data)
1178 {
1179         imd->func_drag = func;
1180         imd->data_drag = data;
1181 }
1182
1183 void image_set_scroll_func(ImageWindow *imd,
1184                            void (*func)(ImageWindow *, GdkEventScroll *event, gpointer),
1185                            gpointer data)
1186 {
1187         imd->func_scroll = func;
1188         imd->data_scroll = data;
1189 }
1190
1191 void image_set_scroll_notify_func(ImageWindow *imd,
1192                                   void (*func)(ImageWindow *imd, gint x, gint y, gint width, gint height, gpointer data),
1193                                   gpointer data)
1194 {
1195         imd->func_scroll_notify = func;
1196         imd->data_scroll_notify = data;
1197 }
1198
1199 void image_set_focus_in_func(ImageWindow *imd,
1200                            void (*func)(ImageWindow *, gpointer),
1201                            gpointer data)
1202 {
1203         imd->func_focus_in = func;
1204         imd->data_focus_in = data;
1205 }
1206
1207 /* path, name */
1208
1209 const gchar *image_get_path(ImageWindow *imd)
1210 {
1211         if (imd->image_fd == NULL) return NULL;
1212         return imd->image_fd->path;
1213 }
1214
1215 const gchar *image_get_name(ImageWindow *imd)
1216 {
1217         if (imd->image_fd == NULL) return NULL;
1218         return imd->image_fd->name;
1219 }
1220
1221 FileData *image_get_fd(ImageWindow *imd)
1222 {
1223         return imd->image_fd;
1224 }
1225
1226 /* merely changes path string, does not change the image! */
1227 void image_set_fd(ImageWindow *imd, FileData *fd)
1228 {
1229         if (imd->auto_refresh && imd->image_fd)
1230                 file_data_unregister_real_time_monitor(imd->image_fd);
1231
1232         file_data_unref(imd->image_fd);
1233         imd->image_fd = file_data_ref(fd);
1234
1235         image_update_title(imd);
1236         image_state_set(imd, IMAGE_STATE_IMAGE);
1237
1238         if (imd->auto_refresh && imd->image_fd)
1239                 file_data_register_real_time_monitor(imd->image_fd);
1240 }
1241
1242 /* load a new image */
1243
1244 void image_change_fd(ImageWindow *imd, FileData *fd, gdouble zoom)
1245 {
1246         if (imd->image_fd == fd) return;
1247
1248         image_change_real(imd, fd, NULL, NULL, zoom);
1249 }
1250
1251 gboolean image_get_image_size(ImageWindow *imd, gint *width, gint *height)
1252 {
1253         return pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), width, height);
1254 }
1255
1256 GdkPixbuf *image_get_pixbuf(ImageWindow *imd)
1257 {
1258         return pixbuf_renderer_get_pixbuf((PixbufRenderer *)imd->pr);
1259 }
1260
1261 void image_change_pixbuf(ImageWindow *imd, GdkPixbuf *pixbuf, gdouble zoom, gboolean lazy)
1262 {
1263         StereoPixbufData stereo_data = STEREO_PIXBUF_DEFAULT;
1264         /* read_exif and similar functions can actually notice that the file has changed and trigger
1265            a notification that removes the pixbuf from cache and unrefs it. Therefore we must ref it
1266            here before it is taken over by the renderer. */
1267         if (pixbuf) g_object_ref(pixbuf);
1268
1269         imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
1270         if (imd->image_fd)
1271                 {
1272                 if (imd->image_fd->user_orientation)
1273                         {
1274                         imd->orientation = imd->image_fd->user_orientation;
1275                         }
1276                 else if (options->image.exif_rotate_enable)
1277                         {
1278                         imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
1279                         imd->image_fd->exif_orientation = imd->orientation;
1280                         }
1281                 }
1282
1283         if (pixbuf)
1284                 {
1285                 stereo_data = imd->user_stereo;
1286                 if (stereo_data == STEREO_PIXBUF_DEFAULT)
1287                         {
1288                         stereo_data = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(pixbuf), "stereo_data"));
1289                         }
1290                 }
1291
1292         pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, FALSE);
1293         if (imd->cm)
1294                 {
1295                 color_man_free(imd->cm);
1296                 imd->cm = NULL;
1297                 }
1298
1299         if (lazy)
1300                 {
1301                 pixbuf_renderer_set_pixbuf_lazy((PixbufRenderer *)imd->pr, pixbuf, zoom, imd->orientation, stereo_data);
1302                 }
1303         else
1304                 {
1305                 pixbuf_renderer_set_pixbuf((PixbufRenderer *)imd->pr, pixbuf, zoom);
1306                 pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, imd->orientation);
1307                 pixbuf_renderer_set_stereo_data((PixbufRenderer *)imd->pr, stereo_data);
1308                 }
1309
1310         if (pixbuf) g_object_unref(pixbuf);
1311
1312         if (imd->color_profile_enable)
1313                 {
1314                 image_post_process_color(imd, 0, FALSE); /* TODO: error handling */
1315                 }
1316
1317         if (imd->cm || imd->desaturate)
1318                 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
1319
1320         image_state_set(imd, IMAGE_STATE_IMAGE);
1321 }
1322
1323 void image_change_from_collection(ImageWindow *imd, CollectionData *cd, CollectInfo *info, gdouble zoom)
1324 {
1325         CollectWindow *cw;
1326
1327         if (!cd || !info || !g_list_find(cd->list, info)) return;
1328
1329         image_change_real(imd, info->fd, cd, info, zoom);
1330         cw = collection_window_find(cd);
1331         if (cw)
1332                 {
1333                 collection_table_set_focus(cw->table, info);
1334                 collection_table_unselect_all(cw->table);
1335                 collection_table_select(cw->table,info);
1336                 }
1337 }
1338
1339 CollectionData *image_get_collection(ImageWindow *imd, CollectInfo **info)
1340 {
1341         if (collection_to_number(imd->collection) >= 0)
1342                 {
1343                 if (g_list_find(imd->collection->list, imd->collection_info) != NULL)
1344                         {
1345                         if (info) *info = imd->collection_info;
1346                         }
1347                 else
1348                         {
1349                         if (info) *info = NULL;
1350                         }
1351                 return imd->collection;
1352                 }
1353
1354         if (info) *info = NULL;
1355         return NULL;
1356 }
1357
1358 static void image_loader_sync_read_ahead_data(ImageLoader *il, gpointer old_data, gpointer data)
1359 {
1360         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_read_ahead_error_cb, old_data))
1361                 g_signal_connect(G_OBJECT(il), "error", (GCallback)image_read_ahead_error_cb, data);
1362
1363         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_read_ahead_done_cb, old_data))
1364                 g_signal_connect(G_OBJECT(il), "done", (GCallback)image_read_ahead_done_cb, data);
1365 }
1366
1367 static void image_loader_sync_data(ImageLoader *il, gpointer old_data, gpointer data)
1368 {
1369         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_load_area_cb, old_data))
1370                 g_signal_connect(G_OBJECT(il), "area_ready", (GCallback)image_load_area_cb, data);
1371
1372         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_load_error_cb, old_data))
1373                 g_signal_connect(G_OBJECT(il), "error", (GCallback)image_load_error_cb, data);
1374
1375         if (g_signal_handlers_disconnect_by_func(G_OBJECT(il), (GCallback)image_load_done_cb, old_data))
1376                 g_signal_connect(G_OBJECT(il), "done", (GCallback)image_load_done_cb, data);
1377 }
1378
1379 /* this is more like a move function
1380  * it moves most data from source to imd
1381  */
1382 void image_move_from_image(ImageWindow *imd, ImageWindow *source)
1383 {
1384         if (imd == source) return;
1385
1386         imd->unknown = source->unknown;
1387
1388         imd->collection = source->collection;
1389         imd->collection_info = source->collection_info;
1390
1391         image_loader_free(imd->il);
1392         imd->il = NULL;
1393
1394         image_set_fd(imd, image_get_fd(source));
1395
1396
1397         if (source->il)
1398                 {
1399                 imd->il = source->il;
1400                 source->il = NULL;
1401
1402                 image_loader_sync_data(imd->il, source, imd);
1403
1404                 imd->delay_alter_type = source->delay_alter_type;
1405                 source->delay_alter_type = ALTER_NONE;
1406                 }
1407
1408         imd->color_profile_enable = source->color_profile_enable;
1409         imd->color_profile_input = source->color_profile_input;
1410         imd->color_profile_use_image = source->color_profile_use_image;
1411         color_man_free((ColorMan *)imd->cm);
1412         imd->cm = NULL;
1413         if (source->cm)
1414                 {
1415                 ColorMan *cm;
1416
1417                 imd->cm = source->cm;
1418                 source->cm = NULL;
1419
1420                 cm = (ColorMan *)imd->cm;
1421                 cm->imd = imd;
1422                 cm->func_done_data = imd;
1423                 }
1424
1425         file_data_unref(imd->read_ahead_fd);
1426         source->read_ahead_fd = NULL;
1427
1428         imd->orientation = source->orientation;
1429         imd->desaturate = source->desaturate;
1430
1431         imd->user_stereo = source->user_stereo;
1432
1433         pixbuf_renderer_move(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
1434
1435         if (imd->cm || imd->desaturate)
1436                 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
1437         else
1438                 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
1439
1440 }
1441
1442 /* this is  a copy function
1443  * source stays unchanged
1444  */
1445 void image_copy_from_image(ImageWindow *imd, ImageWindow *source)
1446 {
1447         if (imd == source) return;
1448
1449         imd->unknown = source->unknown;
1450
1451         imd->collection = source->collection;
1452         imd->collection_info = source->collection_info;
1453
1454         image_loader_free(imd->il);
1455         imd->il = NULL;
1456
1457         image_set_fd(imd, image_get_fd(source));
1458
1459
1460         imd->color_profile_enable = source->color_profile_enable;
1461         imd->color_profile_input = source->color_profile_input;
1462         imd->color_profile_use_image = source->color_profile_use_image;
1463         color_man_free((ColorMan *)imd->cm);
1464         imd->cm = NULL;
1465         if (source->cm)
1466                 {
1467                 ColorMan *cm;
1468
1469                 imd->cm = source->cm;
1470                 source->cm = NULL;
1471
1472                 cm = (ColorMan *)imd->cm;
1473                 cm->imd = imd;
1474                 cm->func_done_data = imd;
1475                 }
1476
1477         image_loader_free(imd->read_ahead_il);
1478         imd->read_ahead_il = source->read_ahead_il;
1479         source->read_ahead_il = NULL;
1480         if (imd->read_ahead_il) image_loader_sync_read_ahead_data(imd->read_ahead_il, source, imd);
1481
1482         file_data_unref(imd->read_ahead_fd);
1483         imd->read_ahead_fd = source->read_ahead_fd;
1484         source->read_ahead_fd = NULL;
1485
1486         imd->completed = source->completed;
1487         imd->state = source->state;
1488         source->state = IMAGE_STATE_NONE;
1489
1490         imd->orientation = source->orientation;
1491         imd->desaturate = source->desaturate;
1492
1493         imd->user_stereo = source->user_stereo;
1494
1495         pixbuf_renderer_copy(PIXBUF_RENDERER(imd->pr), PIXBUF_RENDERER(source->pr));
1496
1497         if (imd->cm || imd->desaturate)
1498                 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, image_post_process_tile_color_cb, (gpointer) imd, (imd->cm != NULL) );
1499         else
1500                 pixbuf_renderer_set_post_process_func((PixbufRenderer *)imd->pr, NULL, NULL, TRUE);
1501
1502 }
1503
1504
1505 /* manipulation */
1506
1507 void image_area_changed(ImageWindow *imd, gint x, gint y, gint width, gint height)
1508 {
1509         pixbuf_renderer_area_changed((PixbufRenderer *)imd->pr, x, y, width, height);
1510 }
1511
1512 void image_reload(ImageWindow *imd)
1513 {
1514         if (pixbuf_renderer_get_tiles((PixbufRenderer *)imd->pr)) return;
1515
1516         image_change_complete(imd, image_zoom_get(imd));
1517 }
1518
1519 void image_scroll(ImageWindow *imd, gint x, gint y)
1520 {
1521         pixbuf_renderer_scroll((PixbufRenderer *)imd->pr, x, y);
1522 }
1523
1524 void image_scroll_to_point(ImageWindow *imd, gint x, gint y,
1525                            gdouble x_align, gdouble y_align)
1526 {
1527         pixbuf_renderer_scroll_to_point((PixbufRenderer *)imd->pr, x, y, x_align, y_align);
1528 }
1529
1530 void image_get_scroll_center(ImageWindow *imd, gdouble *x, gdouble *y)
1531 {
1532         pixbuf_renderer_get_scroll_center(PIXBUF_RENDERER(imd->pr), x, y);
1533 }
1534
1535 void image_set_scroll_center(ImageWindow *imd, gdouble x, gdouble y)
1536 {
1537         pixbuf_renderer_set_scroll_center(PIXBUF_RENDERER(imd->pr), x, y);
1538 }
1539
1540 void image_zoom_adjust(ImageWindow *imd, gdouble increment)
1541 {
1542         pixbuf_renderer_zoom_adjust((PixbufRenderer *)imd->pr, increment);
1543 }
1544
1545 void image_zoom_adjust_at_point(ImageWindow *imd, gdouble increment, gint x, gint y)
1546 {
1547         pixbuf_renderer_zoom_adjust_at_point((PixbufRenderer *)imd->pr, increment, x, y);
1548 }
1549
1550 void image_zoom_set_limits(ImageWindow *imd, gdouble min, gdouble max)
1551 {
1552         pixbuf_renderer_zoom_set_limits((PixbufRenderer *)imd->pr, min, max);
1553 }
1554
1555 void image_zoom_set(ImageWindow *imd, gdouble zoom)
1556 {
1557         pixbuf_renderer_zoom_set((PixbufRenderer *)imd->pr, zoom);
1558 }
1559
1560 void image_zoom_set_fill_geometry(ImageWindow *imd, gboolean vertical)
1561 {
1562         PixbufRenderer *pr;
1563         gdouble zoom;
1564         gint width, height;
1565
1566         pr = (PixbufRenderer *)imd->pr;
1567
1568         if (!pixbuf_renderer_get_pixbuf(pr) ||
1569             !pixbuf_renderer_get_image_size(pr, &width, &height)) return;
1570
1571         if (vertical)
1572                 {
1573                 zoom = (gdouble)pr->viewport_height / height;
1574                 }
1575         else
1576                 {
1577                 zoom = (gdouble)pr->viewport_width / width;
1578                 }
1579
1580         if (zoom < 1.0)
1581                 {
1582                 zoom = 0.0 - 1.0 / zoom;
1583                 }
1584
1585         pixbuf_renderer_zoom_set(pr, zoom);
1586 }
1587
1588 gdouble image_zoom_get(ImageWindow *imd)
1589 {
1590         return pixbuf_renderer_zoom_get((PixbufRenderer *)imd->pr);
1591 }
1592
1593 gdouble image_zoom_get_real(ImageWindow *imd)
1594 {
1595         return pixbuf_renderer_zoom_get_scale((PixbufRenderer *)imd->pr);
1596 }
1597
1598 gchar *image_zoom_get_as_text(ImageWindow *imd)
1599 {
1600         gdouble zoom;
1601         gdouble scale;
1602         gdouble l = 1.0;
1603         gdouble r = 1.0;
1604         gint pl = 0;
1605         gint pr = 0;
1606         gchar *approx = " ";
1607
1608         zoom = image_zoom_get(imd);
1609         scale = image_zoom_get_real(imd);
1610
1611         if (zoom > 0.0)
1612                 {
1613                 l = zoom;
1614                 }
1615         else if (zoom < 0.0)
1616                 {
1617                 r = 0.0 - zoom;
1618                 }
1619         else if (zoom == 0.0 && scale != 0.0)
1620                 {
1621                 if (scale >= 1.0)
1622                         {
1623                         l = scale;
1624                         }
1625                 else
1626                         {
1627                         r = 1.0 / scale;
1628                         }
1629                 approx = "~";
1630                 }
1631
1632         if (rint(l) != l) pl = 2;
1633         if (rint(r) != r) pr = 2;
1634
1635         return g_strdup_printf("%.*f :%s%.*f", pl, l, approx, pr, r);
1636 }
1637
1638 gdouble image_zoom_get_default(ImageWindow *imd)
1639 {
1640         gdouble zoom = 1.0;
1641
1642         switch (options->image.zoom_mode)
1643         {
1644         case ZOOM_RESET_ORIGINAL:
1645                 break;
1646         case ZOOM_RESET_FIT_WINDOW:
1647                 zoom = 0.0;
1648                 break;
1649         case ZOOM_RESET_NONE:
1650                 if (imd) zoom = image_zoom_get(imd);
1651                 break;
1652         }
1653
1654         return zoom;
1655 }
1656
1657 /* stereo */
1658 gint image_stereo_get(ImageWindow *imd)
1659 {
1660         return pixbuf_renderer_stereo_get((PixbufRenderer *)imd->pr);
1661 }
1662
1663 void image_stereo_set(ImageWindow *imd, gint stereo_mode)
1664 {
1665         DEBUG_1("Setting stereo mode %04x for imd %p", stereo_mode, imd);
1666         pixbuf_renderer_stereo_set((PixbufRenderer *)imd->pr, stereo_mode);
1667 }
1668
1669 void image_stereo_swap(ImageWindow *imd)
1670 {
1671         gint stereo_mode = pixbuf_renderer_stereo_get((PixbufRenderer *)imd->pr);
1672         stereo_mode ^= PR_STEREO_SWAP;
1673         pixbuf_renderer_stereo_set((PixbufRenderer *)imd->pr, stereo_mode);
1674 }
1675
1676 StereoPixbufData image_stereo_pixbuf_get(ImageWindow *imd)
1677 {
1678         return imd->user_stereo;
1679 }
1680
1681 void image_stereo_pixbuf_set(ImageWindow *imd, StereoPixbufData stereo_mode)
1682 {
1683         imd->user_stereo = stereo_mode;
1684         image_reload(imd);
1685 }
1686
1687 /* read ahead */
1688
1689 void image_prebuffer_set(ImageWindow *imd, FileData *fd)
1690 {
1691         if (pixbuf_renderer_get_tiles((PixbufRenderer *)imd->pr)) return;
1692
1693         if (fd)
1694                 {
1695                 if (!file_cache_get(image_get_cache(), fd))
1696                         {
1697                         image_read_ahead_set(imd, fd);
1698                         }
1699                 }
1700         else
1701                 {
1702                 image_read_ahead_cancel(imd);
1703                 }
1704 }
1705
1706 static void image_notify_cb(FileData *fd, NotifyType type, gpointer data)
1707 {
1708         ImageWindow *imd = data;
1709
1710         if (!imd || !image_get_pixbuf(imd) ||
1711             /* imd->il || */ /* loading in progress - do not check - it should start from the beginning anyway */
1712             !imd->image_fd || /* nothing to reload */
1713             imd->state == IMAGE_STATE_NONE /* loading not started, no need to reload */
1714             ) return;
1715
1716         if ((type & NOTIFY_REREAD) && fd == imd->image_fd)
1717                 {
1718                 /* there is no need to reload on NOTIFY_CHANGE,
1719                    modified files should be detacted anyway and NOTIFY_REREAD should be recieved
1720                    or they are removed from the filelist completely on "move" and "delete"
1721                 */
1722                 DEBUG_1("Notify image: %s %04x", fd->path, type);
1723                 image_reload(imd);
1724                 }
1725 }
1726
1727 void image_auto_refresh_enable(ImageWindow *imd, gboolean enable)
1728 {
1729         if (!enable && imd->auto_refresh && imd->image_fd)
1730                 file_data_unregister_real_time_monitor(imd->image_fd);
1731
1732         if (enable && !imd->auto_refresh && imd->image_fd)
1733                 file_data_register_real_time_monitor(imd->image_fd);
1734
1735         imd->auto_refresh = enable;
1736 }
1737
1738 void image_top_window_set_sync(ImageWindow *imd, gboolean allow_sync)
1739 {
1740         imd->top_window_sync = allow_sync;
1741
1742         g_object_set(G_OBJECT(imd->pr), "window_fit", allow_sync, NULL);
1743 }
1744
1745 void image_background_set_color(ImageWindow *imd, GdkColor *color)
1746 {
1747         pixbuf_renderer_set_color((PixbufRenderer *)imd->pr, color);
1748 }
1749
1750 void image_background_set_color_from_options(ImageWindow *imd, gboolean fullscreen)
1751 {
1752         GdkColor *color = NULL;
1753
1754         if ((options->image.use_custom_border_color && !fullscreen) ||
1755             (options->image.use_custom_border_color_in_fullscreen && fullscreen))
1756                 {
1757                 color = &options->image.border_color;
1758                 }
1759
1760         image_background_set_color(imd, color);
1761 }
1762
1763 void image_color_profile_set(ImageWindow *imd,
1764                              gint input_type,
1765                              gboolean use_image)
1766 {
1767         if (!imd) return;
1768
1769         if (input_type < 0 || input_type >= COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS)
1770                 {
1771                 return;
1772                 }
1773
1774         imd->color_profile_input = input_type;
1775         imd->color_profile_use_image = use_image;
1776 }
1777
1778 gboolean image_color_profile_get(ImageWindow *imd,
1779                                  gint *input_type,
1780                                  gboolean *use_image)
1781 {
1782         if (!imd) return FALSE;
1783
1784         if (input_type) *input_type = imd->color_profile_input;
1785         if (use_image) *use_image = imd->color_profile_use_image;
1786
1787         return TRUE;
1788 }
1789
1790 void image_color_profile_set_use(ImageWindow *imd, gboolean enable)
1791 {
1792         if (!imd) return;
1793
1794         if (imd->color_profile_enable == enable) return;
1795
1796         imd->color_profile_enable = enable;
1797 }
1798
1799 gboolean image_color_profile_get_use(ImageWindow *imd)
1800 {
1801         if (!imd) return FALSE;
1802
1803         return imd->color_profile_enable;
1804 }
1805
1806 gboolean image_color_profile_get_status(ImageWindow *imd, gchar **image_profile, gchar **screen_profile)
1807 {
1808         ColorMan *cm;
1809         if (!imd) return FALSE;
1810
1811         cm = imd->cm;
1812         if (!cm) return FALSE;
1813         return color_man_get_status(cm, image_profile, screen_profile);
1814
1815 }
1816
1817 void image_set_delay_flip(ImageWindow *imd, gboolean delay)
1818 {
1819         if (!imd ||
1820             imd->delay_flip == delay) return;
1821
1822         imd->delay_flip = delay;
1823
1824         g_object_set(G_OBJECT(imd->pr), "delay_flip", delay, NULL);
1825
1826         if (!imd->delay_flip && imd->il)
1827                 {
1828                 PixbufRenderer *pr;
1829
1830                 pr = PIXBUF_RENDERER(imd->pr);
1831                 if (pr->pixbuf) g_object_unref(pr->pixbuf);
1832                 pr->pixbuf = NULL;
1833
1834                 image_load_pixbuf_ready(imd);
1835                 }
1836 }
1837
1838 void image_to_root_window(ImageWindow *imd, gboolean scaled)
1839 {
1840 #if !GTK_CHECK_VERSION(3,0,0)
1841         GdkScreen *screen;
1842         GdkWindow *rootwindow;
1843         GdkPixmap *pixmap;
1844         GdkPixbuf *pixbuf;
1845         GdkPixbuf *pb;
1846         gint width, height;
1847
1848         if (!imd) return;
1849
1850         pixbuf = image_get_pixbuf(imd);
1851         if (!pixbuf) return;
1852
1853         screen = gtk_widget_get_screen(imd->widget);
1854         rootwindow = gdk_screen_get_root_window(screen);
1855         if (gdk_window_get_visual(rootwindow) != gdk_visual_get_system()) return;
1856
1857         if (scaled)
1858                 {
1859                 width = gdk_screen_width();
1860                 height = gdk_screen_height();
1861                 }
1862         else
1863                 {
1864                 pixbuf_renderer_get_scaled_size((PixbufRenderer *)imd->pr, &width, &height);
1865                 }
1866
1867         pb = gdk_pixbuf_scale_simple(pixbuf, width, height, (GdkInterpType)options->image.zoom_quality);
1868
1869         gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, NULL, 128);
1870         gdk_window_set_back_pixmap(rootwindow, pixmap, FALSE);
1871         gdk_window_clear(rootwindow);
1872         g_object_unref(pb);
1873         g_object_unref(pixmap);
1874
1875         gdk_flush();
1876 #endif
1877 }
1878
1879 void image_select(ImageWindow *imd, gboolean select)
1880 {
1881         if (!imd->has_frame) return;
1882
1883         if (select)
1884                 {
1885                 gtk_widget_set_state(imd->widget, GTK_STATE_SELECTED);
1886                 gtk_widget_set_state(imd->pr, GTK_STATE_NORMAL); /* do not propagate */
1887                 }
1888         else
1889                 gtk_widget_set_state(imd->widget, GTK_STATE_NORMAL);
1890 }
1891
1892 void image_set_selectable(ImageWindow *imd, gboolean selectable)
1893 {
1894         if (!imd->has_frame) return;
1895
1896         gtk_frame_set_shadow_type(GTK_FRAME(imd->frame), GTK_SHADOW_NONE);
1897         gtk_container_set_border_width(GTK_CONTAINER(imd->frame), selectable ? 4 : 0);
1898 }
1899
1900 void image_grab_focus(ImageWindow *imd)
1901 {
1902         if (imd->has_frame)
1903                 {
1904                 gtk_widget_grab_focus(imd->frame);
1905                 }
1906         else
1907                 {
1908                 gtk_widget_grab_focus(imd->widget);
1909                 }
1910 }
1911
1912
1913 /*
1914  *-------------------------------------------------------------------
1915  * prefs sync
1916  *-------------------------------------------------------------------
1917  */
1918
1919 static void image_options_set(ImageWindow *imd)
1920 {
1921         g_object_set(G_OBJECT(imd->pr), "zoom_quality", options->image.zoom_quality,
1922                                         "zoom_2pass", options->image.zoom_2pass,
1923                                         "zoom_expand", options->image.zoom_to_fit_allow_expand,
1924                                         "scroll_reset", options->image.scroll_reset_method,
1925                                         "cache_display", options->image.tile_cache_max,
1926                                         "window_fit", (imd->top_window_sync && options->image.fit_window_to_image),
1927                                         "window_limit", options->image.limit_window_size,
1928                                         "window_limit_value", options->image.max_window_size,
1929                                         "autofit_limit", options->image.limit_autofit_size,
1930                                         "autofit_limit_value", options->image.max_autofit_size,
1931                                         "enlargement_limit_value", options->image.max_enlargement_size,
1932
1933                                         NULL);
1934
1935         pixbuf_renderer_set_parent((PixbufRenderer *)imd->pr, (GtkWindow *)imd->top_window);
1936
1937         image_stereo_set(imd, options->stereo.mode);
1938         pixbuf_renderer_stereo_fixed_set((PixbufRenderer *)imd->pr,
1939                                         options->stereo.fixed_w, options->stereo.fixed_h,
1940                                         options->stereo.fixed_x1, options->stereo.fixed_y1,
1941                                         options->stereo.fixed_x2, options->stereo.fixed_y2);
1942 }
1943
1944 void image_options_sync(void)
1945 {
1946         GList *work;
1947
1948         work = image_list;
1949         while (work)
1950                 {
1951                 ImageWindow *imd;
1952
1953                 imd = work->data;
1954                 work = work->next;
1955
1956                 image_options_set(imd);
1957                 }
1958 }
1959
1960 /*
1961  *-------------------------------------------------------------------
1962  * init / destroy
1963  *-------------------------------------------------------------------
1964  */
1965
1966 static void image_free(ImageWindow *imd)
1967 {
1968         image_list = g_list_remove(image_list, imd);
1969
1970         if (imd->auto_refresh && imd->image_fd)
1971                 file_data_unregister_real_time_monitor(imd->image_fd);
1972
1973         file_data_unregister_notify_func(image_notify_cb, imd);
1974
1975         image_reset(imd);
1976
1977         image_read_ahead_cancel(imd);
1978
1979         file_data_unref(imd->image_fd);
1980         g_free(imd->title);
1981         g_free(imd->title_right);
1982         g_free(imd);
1983 }
1984
1985 static void image_destroy_cb(GtkWidget *widget, gpointer data)
1986 {
1987         ImageWindow *imd = data;
1988         image_free(imd);
1989 }
1990 #if GTK_CHECK_VERSION(3,0,0)
1991 gboolean selectable_frame_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
1992 {
1993         GtkAllocation allocation;
1994         gtk_widget_get_allocation(widget, &allocation);
1995         gtk_paint_flat_box(gtk_widget_get_style(widget),
1996                            cr,
1997                            gtk_widget_get_state(widget),
1998                            gtk_frame_get_shadow_type(GTK_FRAME(widget)),
1999                            widget,
2000                            NULL,
2001                            allocation.x + 3, allocation.y + 3,
2002                            allocation.width - 6, allocation.height - 6);
2003
2004         if (gtk_widget_has_focus(widget))
2005                 {
2006                 gtk_paint_focus(gtk_widget_get_style(widget), cr, GTK_STATE_ACTIVE,
2007                                 widget, "image_window",
2008                                 allocation.x, allocation.y,
2009                                 allocation.width - 1, allocation.height - 1);
2010                 }
2011         else
2012                 {
2013                 gtk_paint_shadow(gtk_widget_get_style(widget), cr, GTK_STATE_NORMAL, GTK_SHADOW_IN,
2014                                  widget, "image_window",
2015                                  allocation.x, allocation.y,
2016                                  allocation.width - 1, allocation.height - 1);
2017                 }
2018         return FALSE;
2019 }
2020
2021 #else
2022 gboolean selectable_frame_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
2023 {
2024         GtkAllocation allocation;
2025         gtk_widget_get_allocation(widget, &allocation);
2026         gtk_paint_flat_box(gtk_widget_get_style(widget),
2027                            gtk_widget_get_window(widget),
2028                            gtk_widget_get_state(widget),
2029                            gtk_frame_get_shadow_type(GTK_FRAME(widget)),
2030                            NULL,
2031                            widget,
2032                            NULL,
2033                            allocation.x + 3, allocation.y + 3,
2034                            allocation.width - 6, allocation.height - 6);
2035
2036         if (gtk_widget_has_focus(widget))
2037                 {
2038                 gtk_paint_focus(gtk_widget_get_style(widget), gtk_widget_get_window(widget), GTK_STATE_ACTIVE,
2039                                 &event->area, widget, "image_window",
2040                                 allocation.x, allocation.y,
2041                                 allocation.width - 1, allocation.height - 1);
2042                 }
2043         else
2044                 {
2045                 gtk_paint_shadow(gtk_widget_get_style(widget), gtk_widget_get_window(widget), GTK_STATE_NORMAL, GTK_SHADOW_IN,
2046                                  &event->area, widget, "image_window",
2047                                  allocation.x, allocation.y,
2048                                  allocation.width - 1, allocation.height - 1);
2049                 }
2050         return FALSE;
2051 }
2052 #endif
2053
2054
2055 void image_set_frame(ImageWindow *imd, gboolean frame)
2056 {
2057         frame = !!frame;
2058
2059         if (frame == imd->has_frame) return;
2060
2061         gtk_widget_hide(imd->pr);
2062
2063         if (frame)
2064                 {
2065                 imd->frame = gtk_frame_new(NULL);
2066                 g_object_ref(imd->pr);
2067                 if (imd->has_frame != -1) gtk_container_remove(GTK_CONTAINER(imd->widget), imd->pr);
2068                 gtk_container_add(GTK_CONTAINER(imd->frame), imd->pr);
2069
2070                 g_object_unref(imd->pr);
2071                 gtk_widget_set_can_focus(imd->frame, TRUE);
2072                 gtk_widget_set_app_paintable(imd->frame, TRUE);
2073
2074 #if GTK_CHECK_VERSION(3,0,0)
2075                 g_signal_connect(G_OBJECT(imd->frame), "draw",
2076                                  G_CALLBACK(selectable_frame_draw_cb), NULL);
2077 #else
2078                 g_signal_connect(G_OBJECT(imd->frame), "expose_event",
2079                                  G_CALLBACK(selectable_frame_expose_cb), NULL);
2080 #endif
2081                 g_signal_connect(G_OBJECT(imd->frame), "focus_in_event",
2082                                  G_CALLBACK(image_focus_in_cb), imd);
2083
2084                 gtk_box_pack_start(GTK_BOX(imd->widget), imd->frame, TRUE, TRUE, 0);
2085                 gtk_widget_show(imd->frame);
2086                 }
2087         else
2088                 {
2089                 g_object_ref(imd->pr);
2090                 if (imd->frame)
2091                         {
2092                         gtk_container_remove(GTK_CONTAINER(imd->frame), imd->pr);
2093                         gtk_widget_destroy(imd->frame);
2094                         imd->frame = NULL;
2095                         }
2096                 gtk_box_pack_start(GTK_BOX(imd->widget), imd->pr, TRUE, TRUE, 0);
2097
2098                 g_object_unref(imd->pr);
2099                 }
2100
2101         gtk_widget_show(imd->pr);
2102
2103         imd->has_frame = frame;
2104 }
2105
2106 ImageWindow *image_new(gboolean frame)
2107 {
2108         ImageWindow *imd;
2109
2110         imd = g_new0(ImageWindow, 1);
2111
2112         imd->unknown = TRUE;
2113         imd->has_frame = -1; /* not initialized; for image_set_frame */
2114         imd->delay_alter_type = ALTER_NONE;
2115         imd->state = IMAGE_STATE_NONE;
2116         imd->color_profile_from_image = COLOR_PROFILE_NONE;
2117         imd->orientation = 1;
2118
2119         imd->pr = GTK_WIDGET(pixbuf_renderer_new());
2120
2121         image_options_set(imd);
2122
2123         imd->widget = gtk_vbox_new(0, 0);
2124
2125         image_set_frame(imd, frame);
2126
2127         image_set_selectable(imd, 0);
2128
2129         g_signal_connect(G_OBJECT(imd->pr), "clicked",
2130                          G_CALLBACK(image_click_cb), imd);
2131         g_signal_connect(G_OBJECT(imd->pr), "button_press_event",
2132                          G_CALLBACK(image_press_cb), imd);
2133         g_signal_connect(G_OBJECT(imd->pr), "scroll_notify",
2134                          G_CALLBACK(image_scroll_notify_cb), imd);
2135
2136         g_signal_connect(G_OBJECT(imd->pr), "scroll_event",
2137                          G_CALLBACK(image_scroll_cb), imd);
2138
2139         g_signal_connect(G_OBJECT(imd->pr), "destroy",
2140                          G_CALLBACK(image_destroy_cb), imd);
2141
2142         g_signal_connect(G_OBJECT(imd->pr), "zoom",
2143                          G_CALLBACK(image_zoom_cb), imd);
2144         g_signal_connect(G_OBJECT(imd->pr), "render_complete",
2145                          G_CALLBACK(image_render_complete_cb), imd);
2146         g_signal_connect(G_OBJECT(imd->pr), "drag",
2147                          G_CALLBACK(image_drag_cb), imd);
2148
2149         file_data_register_notify_func(image_notify_cb, imd, NOTIFY_PRIORITY_LOW);
2150
2151         image_list = g_list_append(image_list, imd);
2152
2153         return imd;
2154 }
2155
2156 void image_get_rectangle(gint *x1, gint *y1, gint *x2, gint *y2)
2157 {
2158         *x1 = rect_x1;
2159         *y1 = rect_y1;
2160         *x2 = rect_x2;
2161         *y2 = rect_y2;
2162 }
2163
2164 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */