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