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