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