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