e39c5548b661c4b15a41116f91f17ac681eb9677
[geeqie.git] / src / layout-image.cc
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 "layout-image.h"
23
24 #include <config.h>
25
26 #include "collect.h"
27 #include "debug.h"
28 #include "dnd.h"
29 #include "editors.h"
30 #include "exif.h"
31 #include "filedata.h"
32 #include "fullscreen.h"
33 #include "history-list.h"
34 #include "image.h"
35 #include "image-overlay.h"
36 #include "img-view.h"
37 #include "intl.h"
38 #include "layout-util.h"
39 #include "main-defines.h"
40 #include "menu.h"
41 #include "metadata.h"
42 #include "misc.h"
43 #include "pixbuf-util.h"
44 #include "pixbuf-renderer.h"
45 #include "slideshow.h"
46 #include "ui-fileops.h"
47 #include "ui-menu.h"
48 #include "uri-utils.h"
49 #include "utilops.h"
50 #include "view-file.h"
51
52 enum {
53         FILE_COLUMN_POINTER = 0
54 };
55
56 static GtkWidget *layout_image_pop_menu(LayoutWindow *lw);
57 static void layout_image_set_buttons(LayoutWindow *lw);
58 static gboolean layout_image_animate_new_file(LayoutWindow *lw);
59 static void layout_image_animate_update_image(LayoutWindow *lw);
60
61 /*
62  *----------------------------------------------------------------------------
63  * full screen overlay
64  *----------------------------------------------------------------------------
65  */
66 #pragma GCC diagnostic push
67 #pragma GCC diagnostic ignored "-Wunused-function"
68 static void layout_image_overlay_toggle_unused(LayoutWindow *lw)
69 {
70         if (!lw) return;
71         image_osd_toggle(lw->image);
72 }
73 #pragma GCC diagnostic pop
74
75 /*
76  *----------------------------------------------------------------------------
77  * full screen
78  *----------------------------------------------------------------------------
79  */
80
81 static void layout_image_full_screen_stop_func(FullScreenData *fs, gpointer data)
82 {
83         auto lw = static_cast<LayoutWindow *>(data);
84
85         /* restore image window */
86         if (lw->image == fs->imd)
87                 lw->image = fs->normal_imd;
88
89         lw->full_screen = nullptr;
90 }
91
92 void layout_image_full_screen_start(LayoutWindow *lw)
93 {
94         if (!layout_valid(&lw)) return;
95
96         if (lw->full_screen) return;
97
98         lw->full_screen = fullscreen_start(lw->window, lw->image,
99                                            layout_image_full_screen_stop_func, lw);
100
101         /* set to new image window */
102         if (lw->full_screen->same_region)
103                 lw->image = lw->full_screen->imd;
104
105         layout_image_set_buttons(lw);
106
107         g_signal_connect(G_OBJECT(lw->full_screen->window), "key_press_event",
108                          G_CALLBACK(layout_key_press_cb), lw);
109
110         layout_actions_add_window(lw, lw->full_screen->window);
111
112         image_osd_copy_status(lw->full_screen->normal_imd, lw->image);
113         layout_image_animate_update_image(lw);
114
115         /** @FIXME This is a hack to fix #1037 Fullscreen loads black
116          * The problem occurs when zoom is set to Original Size.
117          * An extra reload is required to force the image to be displayed.
118          * See also image-view.cc real_view_window_new()
119          * This is probably not the correct solution.
120          **/
121         image_reload(lw->image);
122 }
123
124 void layout_image_full_screen_stop(LayoutWindow *lw)
125 {
126         if (!layout_valid(&lw)) return;
127         if (!lw->full_screen) return;
128
129         if (lw->image == lw->full_screen->imd)
130                 image_osd_copy_status(lw->image, lw->full_screen->normal_imd);
131
132         fullscreen_stop(lw->full_screen);
133
134         layout_image_animate_update_image(lw);
135 }
136
137 void layout_image_full_screen_toggle(LayoutWindow *lw)
138 {
139         if (!layout_valid(&lw)) return;
140         if (lw->full_screen)
141                 {
142                 layout_image_full_screen_stop(lw);
143                 }
144         else
145                 {
146                 layout_image_full_screen_start(lw);
147                 }
148 }
149
150 gboolean layout_image_full_screen_active(LayoutWindow *lw)
151 {
152         if (!layout_valid(&lw)) return FALSE;
153
154         return (lw->full_screen != nullptr);
155 }
156
157 /*
158  *----------------------------------------------------------------------------
159  * slideshow
160  *----------------------------------------------------------------------------
161  */
162
163 static void layout_image_slideshow_next(LayoutWindow *lw)
164 {
165         if (lw->slideshow) slideshow_next(lw->slideshow);
166 }
167
168 static void layout_image_slideshow_prev(LayoutWindow *lw)
169 {
170         if (lw->slideshow) slideshow_prev(lw->slideshow);
171 }
172
173 static void layout_image_slideshow_stop_func(SlideShowData *, gpointer data)
174 {
175         auto lw = static_cast<LayoutWindow *>(data);
176
177         lw->slideshow = nullptr;
178         layout_status_update_info(lw, nullptr);
179 }
180
181 void layout_image_slideshow_start(LayoutWindow *lw)
182 {
183         CollectionData *cd;
184         CollectInfo *info;
185
186         if (!layout_valid(&lw)) return;
187         if (lw->slideshow) return;
188
189         cd = image_get_collection(lw->image, &info);
190
191         if (cd && info)
192                 {
193                 lw->slideshow = slideshow_start_from_collection(lw, nullptr, cd,
194                                 layout_image_slideshow_stop_func, lw, info);
195                 }
196         else
197                 {
198                 lw->slideshow = slideshow_start(lw,
199                                 layout_list_get_index(lw, layout_image_get_fd(lw)),
200                                 layout_image_slideshow_stop_func, lw);
201                 }
202
203         layout_status_update_info(lw, nullptr);
204 }
205
206 /* note that slideshow will take ownership of the list, do not free it */
207 void layout_image_slideshow_start_from_list(LayoutWindow *lw, GList *list)
208 {
209         if (!layout_valid(&lw)) return;
210
211         if (lw->slideshow || !list)
212                 {
213                 filelist_free(list);
214                 return;
215                 }
216
217         lw->slideshow = slideshow_start_from_filelist(lw, nullptr, list,
218                                                        layout_image_slideshow_stop_func, lw);
219
220         layout_status_update_info(lw, nullptr);
221 }
222
223 void layout_image_slideshow_stop(LayoutWindow *lw)
224 {
225         if (!layout_valid(&lw)) return;
226
227         if (!lw->slideshow) return;
228
229         slideshow_free(lw->slideshow);
230         /* the stop_func sets lw->slideshow to NULL for us */
231 }
232
233 void layout_image_slideshow_toggle(LayoutWindow *lw)
234 {
235         if (!layout_valid(&lw)) return;
236
237         if (lw->slideshow)
238                 {
239                 layout_image_slideshow_stop(lw);
240                 }
241         else
242                 {
243                 layout_image_slideshow_start(lw);
244                 }
245 }
246
247 gboolean layout_image_slideshow_active(LayoutWindow *lw)
248 {
249         if (!layout_valid(&lw)) return FALSE;
250
251         return (lw->slideshow != nullptr);
252 }
253
254 gboolean layout_image_slideshow_pause_toggle(LayoutWindow *lw)
255 {
256         gboolean ret;
257
258         if (!layout_valid(&lw)) return FALSE;
259
260         ret = slideshow_pause_toggle(lw->slideshow);
261
262         layout_status_update_info(lw, nullptr);
263
264         return ret;
265 }
266
267 gboolean layout_image_slideshow_paused(LayoutWindow *lw)
268 {
269         if (!layout_valid(&lw)) return FALSE;
270
271         return (slideshow_paused(lw->slideshow));
272 }
273
274 static gboolean layout_image_slideshow_continue_check(LayoutWindow *lw)
275 {
276         if (!lw->slideshow) return FALSE;
277
278         if (!slideshow_should_continue(lw->slideshow))
279                 {
280                 layout_image_slideshow_stop(lw);
281                 return FALSE;
282                 }
283
284         return TRUE;
285 }
286
287 /*
288  *----------------------------------------------------------------------------
289  * Animation
290  *----------------------------------------------------------------------------
291  */
292
293 struct AnimationData
294 {
295         ImageWindow *iw;
296         LayoutWindow *lw;
297         GdkPixbufAnimation *gpa;
298         GdkPixbufAnimationIter *iter;
299         GdkPixbuf *gpb;
300         FileData *data_adr;
301         gint delay;
302         gboolean valid;
303         GCancellable *cancellable;
304         GFile *in_file;
305         GFileInputStream *gfstream;
306 };
307
308 static void image_animation_data_free(AnimationData *fd)
309 {
310         if(!fd) return;
311         if(fd->iter) g_object_unref(fd->iter);
312         if(fd->gpa) g_object_unref(fd->gpa);
313         if(fd->cancellable) g_object_unref(fd->cancellable);
314         g_free(fd);
315 }
316
317 static gboolean animation_should_continue(AnimationData *fd)
318 {
319         if (!fd->valid)
320                 return FALSE;
321
322         return TRUE;
323 }
324
325 static gboolean show_next_frame(gpointer data)
326 {
327         auto fd = static_cast<AnimationData*>(data);
328         int delay;
329         PixbufRenderer *pr;
330
331         if(animation_should_continue(fd)==FALSE)
332                 {
333                 image_animation_data_free(fd);
334                 return FALSE;
335                 }
336
337         pr = reinterpret_cast<PixbufRenderer*>(fd->iw->pr);
338
339         if (gdk_pixbuf_animation_iter_advance(fd->iter,nullptr)==FALSE)
340                 {
341                 /* This indicates the animation is complete.
342                    Return FALSE here to disable looping. */
343                 }
344
345         fd->gpb = gdk_pixbuf_animation_iter_get_pixbuf(fd->iter);
346         image_change_pixbuf(fd->iw,fd->gpb,pr->zoom,FALSE);
347
348         if (fd->iw->func_update)
349                 fd->iw->func_update(fd->iw, fd->iw->data_update);
350
351         delay = gdk_pixbuf_animation_iter_get_delay_time(fd->iter);
352         if (delay!=fd->delay)
353                 {
354                 if (delay>0) /* Current frame not static. */
355                         {
356                         fd->delay=delay;
357                         g_timeout_add(delay,show_next_frame,fd);
358                         }
359                 else
360                         {
361                         image_animation_data_free(fd);
362                         }
363                 return FALSE;
364                 }
365
366         return TRUE;
367 }
368
369 static gboolean layout_image_animate_check(LayoutWindow *lw)
370 {
371         if (!layout_valid(&lw)) return FALSE;
372
373         if(!lw->options.animate || lw->image->image_fd == nullptr || lw->image->image_fd->extension == nullptr || (g_ascii_strcasecmp(lw->image->image_fd->extension,".GIF")!=0 && g_ascii_strcasecmp(lw->image->image_fd->extension,".WEBP")!=0))
374                 {
375                 if(lw->animation)
376                         {
377                         lw->animation->valid = FALSE;
378                         if (lw->animation->cancellable)
379                                 {
380                                 g_cancellable_cancel(lw->animation->cancellable);
381                                 }
382                         lw->animation = nullptr;
383                         }
384                 return FALSE;
385                 }
386
387         return TRUE;
388 }
389
390 static void layout_image_animate_update_image(LayoutWindow *lw)
391 {
392         if (!layout_valid(&lw)) return;
393
394         if(lw->options.animate && lw->animation)
395                 {
396                 if (lw->full_screen && lw->image != lw->full_screen->imd)
397                         lw->animation->iw = lw->full_screen->imd;
398                 else
399                         lw->animation->iw = lw->image;
400                 }
401 }
402
403
404 static void animation_async_ready_cb(GObject *, GAsyncResult *res, gpointer data)
405 {
406         GError *error = nullptr;
407         auto animation = static_cast<AnimationData *>(data);
408
409         if (animation)
410                 {
411                 if (g_cancellable_is_cancelled(animation->cancellable))
412                         {
413                         gdk_pixbuf_animation_new_from_stream_finish(res, nullptr);
414                         g_object_unref(animation->in_file);
415                         g_object_unref(animation->gfstream);
416                         image_animation_data_free(animation);
417                         return;
418                         }
419
420                 animation->gpa = gdk_pixbuf_animation_new_from_stream_finish(res, &error);
421                 if (animation->gpa)
422                         {
423                         if (!gdk_pixbuf_animation_is_static_image(animation->gpa))
424                                 {
425                                 animation->iter = gdk_pixbuf_animation_get_iter(animation->gpa, nullptr);
426                                 if (animation->iter)
427                                         {
428                                         animation->data_adr = animation->lw->image->image_fd;
429                                         animation->delay = gdk_pixbuf_animation_iter_get_delay_time(animation->iter);
430                                         animation->valid = TRUE;
431
432                                         layout_image_animate_update_image(animation->lw);
433
434                                         g_timeout_add(animation->delay, show_next_frame, animation);
435                                         }
436                                 }
437                         }
438                 else
439                         {
440                         log_printf("Error reading GIF file: %s\n", error->message);
441                         }
442
443                 g_object_unref(animation->in_file);
444                 g_object_unref(animation->gfstream);
445                 }
446 }
447
448 static gboolean layout_image_animate_new_file(LayoutWindow *lw)
449 {
450         GFileInputStream *gfstream;
451         GError *error = nullptr;
452         AnimationData *animation;
453         GFile *in_file;
454
455         if(!layout_image_animate_check(lw)) return FALSE;
456
457         if(lw->animation) lw->animation->valid = FALSE;
458
459         if (lw->animation)
460                 {
461                 g_cancellable_cancel(lw->animation->cancellable);
462                 }
463
464         animation = g_new0(AnimationData, 1);
465         lw->animation = animation;
466         animation->lw = lw;
467         animation->cancellable = g_cancellable_new();
468
469         in_file = g_file_new_for_path(lw->image->image_fd->path);
470         animation->in_file = in_file;
471         gfstream = g_file_read(in_file, nullptr, &error);
472         if (gfstream)
473                 {
474                 animation->gfstream = gfstream;
475                 gdk_pixbuf_animation_new_from_stream_async(reinterpret_cast<GInputStream*>(gfstream), animation->cancellable, animation_async_ready_cb, animation);
476                 }
477         else
478                 {
479                 log_printf("Error reading animation file: %s\nError: %s\n", lw->image->image_fd->path, error->message);
480                 }
481
482         return TRUE;
483 }
484
485 void layout_image_animate_toggle(LayoutWindow *lw)
486 {
487         GtkAction *action;
488
489         if (!lw) return;
490
491         lw->options.animate = !lw->options.animate;
492
493         action = gtk_action_group_get_action(lw->action_group, "Animate");
494         gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), lw->options.animate);
495
496         layout_image_animate_new_file(lw);
497 }
498
499 /*
500  *----------------------------------------------------------------------------
501  * pop-up menus
502  *----------------------------------------------------------------------------
503  */
504
505 static void li_pop_menu_zoom_in_cb(GtkWidget *, gpointer data)
506 {
507         auto lw = static_cast<LayoutWindow *>(data);
508
509         layout_image_zoom_adjust(lw, get_zoom_increment(), FALSE);
510 }
511
512 static void li_pop_menu_zoom_out_cb(GtkWidget *, gpointer data)
513 {
514         auto lw = static_cast<LayoutWindow *>(data);
515         layout_image_zoom_adjust(lw, -get_zoom_increment(), FALSE);
516 }
517
518 static void li_pop_menu_zoom_1_1_cb(GtkWidget *, gpointer data)
519 {
520         auto lw = static_cast<LayoutWindow *>(data);
521
522         layout_image_zoom_set(lw, 1.0, FALSE);
523 }
524
525 static void li_pop_menu_zoom_fit_cb(GtkWidget *, gpointer data)
526 {
527         auto lw = static_cast<LayoutWindow *>(data);
528
529         layout_image_zoom_set(lw, 0.0, FALSE);
530 }
531
532 static void li_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
533 {
534         LayoutWindow *lw;
535         auto key = static_cast<const gchar *>(data);
536
537         lw = static_cast<LayoutWindow *>(submenu_item_get_data(widget));
538
539         if (!editor_window_flag_set(key))
540                 {
541                 layout_image_full_screen_stop(lw);
542                 }
543         file_util_start_editor_from_file(key, layout_image_get_fd(lw), lw->window);
544 }
545
546 static void li_pop_menu_alter_cb(GtkWidget *widget, gpointer data)
547 {
548         auto lw = static_cast<LayoutWindow *>(data);
549         AlterType type;
550
551         lw = static_cast<LayoutWindow *>(submenu_item_get_data(widget));
552         type = static_cast<AlterType>GPOINTER_TO_INT(data);
553
554         image_alter_orientation(lw->image, lw->image->image_fd, type);
555 }
556
557 static void li_pop_menu_new_cb(GtkWidget *, gpointer data)
558 {
559         auto lw = static_cast<LayoutWindow *>(data);
560
561         view_window_new(layout_image_get_fd(lw));
562 }
563
564 static GtkWidget *li_pop_menu_click_parent(GtkWidget *widget, LayoutWindow *lw)
565 {
566         GtkWidget *menu;
567         GtkWidget *parent;
568
569         menu = gtk_widget_get_toplevel(widget);
570         if (!menu) return nullptr;
571
572         parent = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(menu), "click_parent"));
573
574         if (!parent && lw->full_screen)
575                 {
576                 parent = lw->full_screen->imd->widget;
577                 }
578
579         return parent;
580 }
581
582 static void li_pop_menu_copy_cb(GtkWidget *widget, gpointer data)
583 {
584         auto lw = static_cast<LayoutWindow *>(data);
585
586         file_util_copy(layout_image_get_fd(lw), nullptr, nullptr,
587                        li_pop_menu_click_parent(widget, lw));
588 }
589
590 static void li_pop_menu_copy_path_cb(GtkWidget *, gpointer data)
591 {
592         auto lw = static_cast<LayoutWindow *>(data);
593
594         file_util_copy_path_to_clipboard(layout_image_get_fd(lw), TRUE);
595 }
596
597 static void li_pop_menu_copy_path_unquoted_cb(GtkWidget *, gpointer data)
598 {
599         auto lw = static_cast<LayoutWindow *>(data);
600
601         file_util_copy_path_to_clipboard(layout_image_get_fd(lw), FALSE);
602 }
603
604 #ifdef HAVE_GTK4
605 static void li_pop_menu_copy_image_cb(GtkWidget *, gpointer data)
606 {
607 /* @FIXME GTK4 stub */
608 }
609 #else
610 static void li_pop_menu_copy_image_cb(GtkWidget *, gpointer data)
611 {
612         auto lw = static_cast<LayoutWindow *>(data);
613         ImageWindow *imd = lw->image;
614
615         GdkPixbuf *pixbuf;
616         pixbuf = image_get_pixbuf(imd);
617         if (!pixbuf) return;
618         gtk_clipboard_set_image(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), pixbuf);
619 }
620 #endif
621
622 static void li_pop_menu_move_cb(GtkWidget *widget, gpointer data)
623 {
624         auto lw = static_cast<LayoutWindow *>(data);
625
626         file_util_move(layout_image_get_fd(lw), nullptr, nullptr,
627                        li_pop_menu_click_parent(widget, lw));
628 }
629
630 static void li_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
631 {
632         auto lw = static_cast<LayoutWindow *>(data);
633
634         file_util_rename(layout_image_get_fd(lw), nullptr,
635                          li_pop_menu_click_parent(widget, lw));
636 }
637
638 static void li_pop_menu_delete_cb(GtkWidget *widget, gpointer data)
639 {
640         auto lw = static_cast<LayoutWindow *>(data);
641
642         options->file_ops.safe_delete_enable = FALSE;
643         file_util_delete(layout_image_get_fd(lw), nullptr,
644                          li_pop_menu_click_parent(widget, lw));
645 }
646
647 static void li_pop_menu_move_to_trash_cb(GtkWidget *widget, gpointer data)
648 {
649         auto lw = static_cast<LayoutWindow *>(data);
650
651         options->file_ops.safe_delete_enable = TRUE;
652         file_util_delete(layout_image_get_fd(lw), nullptr,
653                          li_pop_menu_click_parent(widget, lw));
654 }
655
656 static void li_pop_menu_slide_start_cb(GtkWidget *, gpointer data)
657 {
658         auto lw = static_cast<LayoutWindow *>(data);
659
660         layout_image_slideshow_start(lw);
661 }
662
663 static void li_pop_menu_slide_stop_cb(GtkWidget *, gpointer data)
664 {
665         auto lw = static_cast<LayoutWindow *>(data);
666
667         layout_image_slideshow_stop(lw);
668 }
669
670 static void li_pop_menu_slide_pause_cb(GtkWidget *, gpointer data)
671 {
672         auto lw = static_cast<LayoutWindow *>(data);
673
674         layout_image_slideshow_pause_toggle(lw);
675 }
676
677 static void li_pop_menu_full_screen_cb(GtkWidget *, gpointer data)
678 {
679         auto lw = static_cast<LayoutWindow *>(data);
680
681         layout_image_full_screen_toggle(lw);
682 }
683
684 static void li_pop_menu_animate_cb(GtkWidget *, gpointer data)
685 {
686         auto lw = static_cast<LayoutWindow *>(data);
687
688         layout_image_animate_toggle(lw);
689 }
690
691 static void li_pop_menu_hide_cb(GtkWidget *, gpointer data)
692 {
693         auto lw = static_cast<LayoutWindow *>(data);
694
695         layout_tools_hide_toggle(lw);
696 }
697
698 static void li_set_layout_path_cb(GtkWidget *, gpointer data)
699 {
700         auto lw = static_cast<LayoutWindow *>(data);
701         FileData *fd;
702
703         if (!layout_valid(&lw)) return;
704
705         fd = layout_image_get_fd(lw);
706         if (fd) layout_set_fd(lw, fd);
707 }
708
709 static void li_open_archive_cb(GtkWidget *, gpointer data)
710 {
711         auto lw = static_cast<LayoutWindow *>(data);
712         LayoutWindow *lw_new;
713         gchar *dest_dir;
714
715         if (!layout_valid(&lw)) return;
716
717         dest_dir = open_archive(layout_image_get_fd(lw));
718         if (dest_dir)
719                 {
720                 lw_new = layout_new_from_default();
721                 layout_set_path(lw_new, dest_dir);
722                 g_free(dest_dir);
723                 }
724         else
725                 {
726                 warning_dialog(_("Cannot open archive file"), _("See the Log Window"), GQ_ICON_DIALOG_WARNING, nullptr);
727                 }
728 }
729
730 static gboolean li_check_if_current_path(LayoutWindow *lw, const gchar *path)
731 {
732         gchar *dirname;
733         gboolean ret;
734
735         if (!path || !layout_valid(&lw) || !lw->dir_fd) return FALSE;
736
737         dirname = g_path_get_dirname(path);
738         ret = (strcmp(lw->dir_fd->path, dirname) == 0);
739         g_free(dirname);
740         return ret;
741 }
742
743 static void layout_image_popup_menu_destroy_cb(GtkWidget *, gpointer data)
744 {
745         auto editmenu_fd_list = static_cast<GList *>(data);
746
747         filelist_free(editmenu_fd_list);
748 }
749
750 static GList *layout_image_get_fd_list(LayoutWindow *lw)
751 {
752         GList *list = nullptr;
753         FileData *fd = layout_image_get_fd(lw);
754
755         if (fd)
756                 {
757                 if (lw->vf)
758                         /* optionally include sidecars if the filelist entry is not expanded */
759                         list = vf_selection_get_one(lw->vf, fd);
760                 else
761                         list = g_list_append(nullptr, file_data_ref(fd));
762                 }
763
764         return list;
765 }
766
767 /**
768  * @brief Add file selection list to a collection
769  * @param[in] widget
770  * @param[in] data Index to the collection list menu item selected, or -1 for new collection
771  *
772  *
773  */
774 static void layout_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
775 {
776         LayoutWindow *lw;
777         GList *selection_list = nullptr;
778
779         lw = static_cast<LayoutWindow *>(submenu_item_get_data(widget));
780         selection_list = g_list_append(selection_list, layout_image_get_fd(lw));
781         pop_menu_collections(selection_list, data);
782
783         filelist_free(selection_list);
784 }
785
786 static GtkWidget *layout_image_pop_menu(LayoutWindow *lw)
787 {
788         GtkWidget *menu;
789         GtkWidget *item;
790         GtkWidget *submenu;
791         const gchar *path;
792         gboolean fullscreen;
793         GList *editmenu_fd_list;
794         GtkAccelGroup *accel_group;
795
796         path = layout_image_get_path(lw);
797         fullscreen = layout_image_full_screen_active(lw);
798
799         menu = popup_menu_short_lived();
800
801         accel_group = gtk_accel_group_new();
802         gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
803
804         g_object_set_data(G_OBJECT(menu), "window_keys", nullptr);
805         g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
806
807         menu_item_add_icon(menu, _("Zoom _in"), GQ_ICON_ZOOM_IN, G_CALLBACK(li_pop_menu_zoom_in_cb), lw);
808         menu_item_add_icon(menu, _("Zoom _out"), GQ_ICON_ZOOM_OUT, G_CALLBACK(li_pop_menu_zoom_out_cb), lw);
809         menu_item_add_icon(menu, _("Zoom _1:1"), GQ_ICON_ZOOM_100, G_CALLBACK(li_pop_menu_zoom_1_1_cb), lw);
810         menu_item_add_icon(menu, _("Zoom to fit"), GQ_ICON_ZOOM_FIT, G_CALLBACK(li_pop_menu_zoom_fit_cb), lw);
811         menu_item_add_divider(menu);
812
813         editmenu_fd_list = layout_image_get_fd_list(lw);
814         g_signal_connect(G_OBJECT(menu), "destroy",
815                          G_CALLBACK(layout_image_popup_menu_destroy_cb), editmenu_fd_list);
816         submenu = submenu_add_edit(menu, &item, G_CALLBACK(li_pop_menu_edit_cb), lw, editmenu_fd_list);
817         if (!path) gtk_widget_set_sensitive(item, FALSE);
818         menu_item_add_divider(submenu);
819         item = submenu_add_alter(menu, G_CALLBACK(li_pop_menu_alter_cb), lw);
820
821         item = menu_item_add_icon(menu, _("View in _new window"), GQ_ICON_NEW, G_CALLBACK(li_pop_menu_new_cb), lw);
822         if (!path || fullscreen) gtk_widget_set_sensitive(item, FALSE);
823
824         item = menu_item_add(menu, _("_Go to directory view"), G_CALLBACK(li_set_layout_path_cb), lw);
825         if (!path || li_check_if_current_path(lw, path)) gtk_widget_set_sensitive(item, FALSE);
826
827         item = menu_item_add_icon(menu, _("Open archive"), GQ_ICON_OPEN, G_CALLBACK(li_open_archive_cb), lw);
828         if (!path || lw->image->image_fd->format_class != FORMAT_CLASS_ARCHIVE)
829                 {
830                 gtk_widget_set_sensitive(item, FALSE);
831                 }
832
833         menu_item_add_divider(menu);
834
835         item = menu_item_add_icon(menu, _("_Copy..."), GQ_ICON_COPY, G_CALLBACK(li_pop_menu_copy_cb), lw);
836         if (!path) gtk_widget_set_sensitive(item, FALSE);
837         item = menu_item_add(menu, _("_Move..."), G_CALLBACK(li_pop_menu_move_cb), lw);
838         if (!path) gtk_widget_set_sensitive(item, FALSE);
839         item = menu_item_add(menu, _("_Rename..."), G_CALLBACK(li_pop_menu_rename_cb), lw);
840         if (!path) gtk_widget_set_sensitive(item, FALSE);
841         item = menu_item_add(menu, _("_Copy path to clipboard"), G_CALLBACK(li_pop_menu_copy_path_cb), lw);
842         item = menu_item_add(menu, _("_Copy path unquoted to clipboard"), G_CALLBACK(li_pop_menu_copy_path_unquoted_cb), lw);
843         item = menu_item_add(menu, _("Copy _image to clipboard"), G_CALLBACK(li_pop_menu_copy_image_cb), lw);
844         if (!path) gtk_widget_set_sensitive(item, FALSE);
845         menu_item_add_divider(menu);
846
847         item = menu_item_add_icon(menu,
848                                 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
849                                         _("Move to Trash"), GQ_ICON_DELETE,
850                                                                 G_CALLBACK(li_pop_menu_move_to_trash_cb), lw);
851         if (!path) gtk_widget_set_sensitive(item, FALSE);
852         item = menu_item_add_icon(menu,
853                                 options->file_ops.confirm_delete ? _("_Delete...") :
854                                         _("_Delete"), GQ_ICON_DELETE_SHRED,
855                                                                 G_CALLBACK(li_pop_menu_delete_cb), lw);
856         if (!path) gtk_widget_set_sensitive(item, FALSE);
857         menu_item_add_divider(menu);
858
859         submenu = submenu_add_collections(menu, &item,
860                                 G_CALLBACK(layout_pop_menu_collections_cb), lw);
861         gtk_widget_set_sensitive(item, TRUE);
862         menu_item_add_divider(menu);
863
864         if (layout_image_slideshow_active(lw))
865                 {
866                 menu_item_add(menu, _("Toggle _slideshow"), G_CALLBACK(li_pop_menu_slide_stop_cb), lw);
867                 if (layout_image_slideshow_paused(lw))
868                         {
869                         item = menu_item_add(menu, _("Continue slides_how"),
870                                              G_CALLBACK(li_pop_menu_slide_pause_cb), lw);
871                         }
872                 else
873                         {
874                         item = menu_item_add(menu, _("Pause slides_how"),
875                                              G_CALLBACK(li_pop_menu_slide_pause_cb), lw);
876                         }
877                 }
878         else
879                 {
880                 menu_item_add(menu, _("Toggle _slideshow"), G_CALLBACK(li_pop_menu_slide_start_cb), lw);
881                 item = menu_item_add(menu, _("Pause slides_how"), G_CALLBACK(li_pop_menu_slide_pause_cb), lw);
882                 gtk_widget_set_sensitive(item, FALSE);
883                 }
884
885         if (!fullscreen)
886                 {
887                 menu_item_add_icon(menu, _("_Full screen"), GQ_ICON_FULLSCREEN, G_CALLBACK(li_pop_menu_full_screen_cb), lw);
888                 }
889         else
890                 {
891                 menu_item_add_icon(menu, _("Exit _full screen"), GQ_ICON_LEAVE_FULLSCREEN, G_CALLBACK(li_pop_menu_full_screen_cb), lw);
892                 }
893
894         menu_item_add_check(menu, _("GIF _animation"), lw->options.animate, G_CALLBACK(li_pop_menu_animate_cb), lw);
895
896         menu_item_add_divider(menu);
897
898         item = menu_item_add_check(menu, _("Hide file _list"), lw->options.tools_hidden,
899                                    G_CALLBACK(li_pop_menu_hide_cb), lw);
900
901         item = menu_item_add_check(menu, _("Hide Selectable Bars"), lw->options.selectable_toolbars_hidden, G_CALLBACK(layout_selectable_toolbars_toggle), lw);
902         if (fullscreen) gtk_widget_set_sensitive(item, FALSE);
903
904         return menu;
905 }
906
907 void layout_image_menu_popup(LayoutWindow *lw)
908 {
909         GtkWidget *menu;
910
911         menu = layout_image_pop_menu(lw);
912         gtk_menu_popup_at_widget(GTK_MENU(menu), lw->image->widget, GDK_GRAVITY_EAST, GDK_GRAVITY_CENTER, nullptr);
913 }
914
915 /*
916  *----------------------------------------------------------------------------
917  * dnd
918  *----------------------------------------------------------------------------
919  */
920
921 static void layout_image_dnd_receive(GtkWidget *widget, GdkDragContext *,
922                                      gint, gint,
923                                      GtkSelectionData *selection_data, guint info,
924                                      guint, gpointer data)
925 {
926         auto lw = static_cast<LayoutWindow *>(data);
927         gint i;
928         gchar *url;
929
930
931         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
932                 {
933                 if (lw->split_images[i] && lw->split_images[i]->pr == widget)
934                         break;
935                 }
936         if (i < MAX_SPLIT_IMAGES)
937                 {
938                 DEBUG_1("dnd image activate %d", i);
939                 layout_image_activate(lw, i, FALSE);
940                 }
941
942         if (info == TARGET_TEXT_PLAIN)
943                 {
944                 url = g_strdup(reinterpret_cast<const gchar *>(gtk_selection_data_get_data(selection_data)));
945                 download_web_file(url, FALSE, lw);
946                 g_free(url);
947                 }
948         else if (info == TARGET_URI_LIST || info == TARGET_APP_COLLECTION_MEMBER)
949                 {
950                 CollectionData *source;
951                 GList *list;
952                 GList *info_list;
953
954                 if (info == TARGET_URI_LIST)
955                         {
956                         list = uri_filelist_from_gtk_selection_data(selection_data);
957                         source = nullptr;
958                         info_list = nullptr;
959                         }
960                 else
961                         {
962                         source = collection_from_dnd_data(reinterpret_cast<const gchar *>(gtk_selection_data_get_data(selection_data)), &list, &info_list);
963                         }
964
965                 if (list)
966                         {
967                         auto fd = static_cast<FileData *>(list->data);
968
969                         if (isfile(fd->path))
970                                 {
971                                 gchar *base;
972                                 gint row;
973                                 FileData *dir_fd;
974
975                                 base = remove_level_from_path(fd->path);
976                                 dir_fd = file_data_new_dir(base);
977                                 if (dir_fd != lw->dir_fd)
978                                         {
979                                         layout_set_fd(lw, dir_fd);
980                                         }
981                                 file_data_unref(dir_fd);
982                                 g_free(base);
983
984                                 row = layout_list_get_index(lw, fd);
985                                 if (source && info_list)
986                                         {
987                                         layout_image_set_collection(lw, source, static_cast<CollectInfo *>(info_list->data));
988                                         }
989                                 else if (row == -1)
990                                         {
991                                         layout_image_set_fd(lw, fd);
992                                         }
993                                 else
994                                         {
995                                         layout_image_set_index(lw, row);
996                                         }
997                                 }
998                         else if (isdir(fd->path))
999                                 {
1000                                 layout_set_fd(lw, fd);
1001                                 layout_image_set_fd(lw, nullptr);
1002                                 }
1003                         }
1004
1005                 filelist_free(list);
1006                 g_list_free(info_list);
1007                 }
1008 }
1009
1010 static void layout_image_dnd_get(GtkWidget *widget, GdkDragContext *,
1011                                  GtkSelectionData *selection_data, guint,
1012                                  guint, gpointer data)
1013 {
1014         auto lw = static_cast<LayoutWindow *>(data);
1015         FileData *fd;
1016         gint i;
1017
1018
1019         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1020                 {
1021                 if (lw->split_images[i] && lw->split_images[i]->pr == widget)
1022                         break;
1023                 }
1024         if (i < MAX_SPLIT_IMAGES)
1025                 {
1026                 DEBUG_1("dnd get from %d", i);
1027                 fd = image_get_fd(lw->split_images[i]);
1028                 }
1029         else
1030                 fd = layout_image_get_fd(lw);
1031
1032         if (fd)
1033                 {
1034                 GList *list;
1035
1036                 list = g_list_append(nullptr, fd);
1037                 uri_selection_data_set_uris_from_filelist(selection_data, list);
1038                 g_list_free(list);
1039                 }
1040         else
1041                 {
1042                 gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
1043                                        8, nullptr, 0);
1044                 }
1045 }
1046
1047 static void layout_image_dnd_end(GtkWidget *, GdkDragContext *context, gpointer data)
1048 {
1049         auto lw = static_cast<LayoutWindow *>(data);
1050         if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
1051                 {
1052                 FileData *fd;
1053                 gint row;
1054
1055                 fd = layout_image_get_fd(lw);
1056                 row = layout_list_get_index(lw, fd);
1057                 if (row < 0) return;
1058
1059                 if (!isfile(fd->path))
1060                         {
1061                         if (static_cast<guint>(row) < layout_list_count(lw, nullptr) - 1)
1062                                 {
1063                                 layout_image_next(lw);
1064                                 }
1065                         else
1066                                 {
1067                                 layout_image_prev(lw);
1068                                 }
1069                         }
1070                 layout_refresh(lw);
1071                 }
1072 }
1073
1074 static void layout_image_dnd_init(LayoutWindow *lw, gint i)
1075 {
1076         ImageWindow *imd = lw->split_images[i];
1077
1078         gtk_drag_source_set(imd->pr, GDK_BUTTON2_MASK,
1079                             dnd_file_drag_types, dnd_file_drag_types_count,
1080                             static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
1081         g_signal_connect(G_OBJECT(imd->pr), "drag_data_get",
1082                          G_CALLBACK(layout_image_dnd_get), lw);
1083         g_signal_connect(G_OBJECT(imd->pr), "drag_end",
1084                          G_CALLBACK(layout_image_dnd_end), lw);
1085
1086         gtk_drag_dest_set(imd->pr,
1087                           static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP),
1088                           dnd_file_drop_types, dnd_file_drop_types_count,
1089                           static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
1090         g_signal_connect(G_OBJECT(imd->pr), "drag_data_received",
1091                          G_CALLBACK(layout_image_dnd_receive), lw);
1092 }
1093
1094
1095 /*
1096  *----------------------------------------------------------------------------
1097  * misc
1098  *----------------------------------------------------------------------------
1099  */
1100
1101 void layout_image_to_root(LayoutWindow *lw)
1102 {
1103         image_to_root_window(lw->image, (image_zoom_get(lw->image) == 0));
1104 }
1105
1106 /*
1107  *----------------------------------------------------------------------------
1108  * manipulation + accessors
1109  *----------------------------------------------------------------------------
1110  */
1111
1112 void layout_image_scroll(LayoutWindow *lw, gint x, gint y, gboolean connect_scroll)
1113 {
1114         gint i;
1115         if (!layout_valid(&lw)) return;
1116
1117         image_scroll(lw->image, x, y);
1118
1119         if (lw->full_screen && lw->image != lw->full_screen->imd)
1120                 {
1121                 image_scroll(lw->full_screen->imd, x, y);
1122                 }
1123
1124         if (!connect_scroll) return;
1125
1126         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1127                 {
1128                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
1129                         {
1130                         image_scroll(lw->split_images[i], x, y);
1131                         }
1132                 }
1133
1134 }
1135
1136 void layout_image_zoom_adjust(LayoutWindow *lw, gdouble increment, gboolean connect_zoom)
1137 {
1138         gint i;
1139         if (!layout_valid(&lw)) return;
1140
1141         image_zoom_adjust(lw->image, increment);
1142
1143         if (lw->full_screen && lw->image != lw->full_screen->imd)
1144                 {
1145                 image_zoom_adjust(lw->full_screen->imd, increment);
1146                 }
1147
1148         if (!connect_zoom) return;
1149
1150         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1151                 {
1152                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
1153                         image_zoom_adjust(lw->split_images[i], increment); ;
1154                 }
1155 }
1156
1157 void layout_image_zoom_adjust_at_point(LayoutWindow *lw, gdouble increment, gint x, gint y, gboolean connect_zoom)
1158 {
1159         gint i;
1160         if (!layout_valid(&lw)) return;
1161
1162         image_zoom_adjust_at_point(lw->image, increment, x, y);
1163
1164         if (lw->full_screen && lw->image != lw->full_screen->imd)
1165                 {
1166                 image_zoom_adjust_at_point(lw->full_screen->imd, increment, x, y);
1167                 }
1168         if (!connect_zoom && !lw->split_mode) return;
1169
1170         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1171                 {
1172                 if (lw->split_images[i] && lw->split_images[i] != lw->image &&
1173                                                 lw->split_images[i]->mouse_wheel_mode)
1174                         image_zoom_adjust_at_point(lw->split_images[i], increment, x, y);
1175                 }
1176 }
1177
1178 void layout_image_zoom_set(LayoutWindow *lw, gdouble zoom, gboolean connect_zoom)
1179 {
1180         gint i;
1181         if (!layout_valid(&lw)) return;
1182
1183         image_zoom_set(lw->image, zoom);
1184
1185         if (lw->full_screen && lw->image != lw->full_screen->imd)
1186                 {
1187                 image_zoom_set(lw->full_screen->imd, zoom);
1188                 }
1189
1190         if (!connect_zoom) return;
1191
1192         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1193                 {
1194                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
1195                         image_zoom_set(lw->split_images[i], zoom);
1196                 }
1197 }
1198
1199 void layout_image_zoom_set_fill_geometry(LayoutWindow *lw, gboolean vertical, gboolean connect_zoom)
1200 {
1201         gint i;
1202         if (!layout_valid(&lw)) return;
1203
1204         image_zoom_set_fill_geometry(lw->image, vertical);
1205
1206         if (lw->full_screen && lw->image != lw->full_screen->imd)
1207                 {
1208                 image_zoom_set_fill_geometry(lw->full_screen->imd, vertical);
1209                 }
1210
1211         if (!connect_zoom) return;
1212
1213         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1214                 {
1215                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
1216                         image_zoom_set_fill_geometry(lw->split_images[i], vertical);
1217                 }
1218 }
1219
1220 void layout_image_alter_orientation(LayoutWindow *lw, AlterType type)
1221 {
1222         if (!layout_valid(&lw)) return;
1223
1224         GtkTreeModel *store;
1225         GList *work;
1226         GtkTreeSelection *selection;
1227         GtkTreePath *tpath;
1228         FileData *fd_n;
1229         GtkTreeIter iter;
1230
1231         if (!lw || !lw->vf) return;
1232
1233         if (lw->vf->type == FILEVIEW_ICON)
1234                 {
1235                 if (!VFICON(lw->vf)->selection) return;
1236                 work = VFICON(lw->vf)->selection;
1237                 }
1238         else
1239                 {
1240                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(lw->vf->listview));
1241                 work = gtk_tree_selection_get_selected_rows(selection, &store);
1242                 }
1243
1244         while (work)
1245                 {
1246                 if (lw->vf->type == FILEVIEW_ICON)
1247                         {
1248                         fd_n = static_cast<FileData *>(work->data);
1249                         work = work->next;
1250                         }
1251                 else
1252                         {
1253                         tpath = static_cast<GtkTreePath *>(work->data);
1254                         gtk_tree_model_get_iter(store, &iter, tpath);
1255                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd_n, -1);
1256                         work = work->next;
1257                         }
1258
1259                 image_alter_orientation(lw->image, fd_n, type);
1260                 }
1261 }
1262
1263 static void image_alter_rating(FileData *fd_n, const gchar *rating)
1264 {
1265         metadata_write_string(fd_n, RATING_KEY, rating);
1266         read_rating_data(fd_n);
1267 }
1268
1269 void layout_image_rating(LayoutWindow *lw, const gchar *rating)
1270 {
1271         if (!layout_valid(&lw)) return;
1272
1273         GtkTreeModel *store;
1274         GList *work;
1275         GtkTreeSelection *selection;
1276         GtkTreePath *tpath;
1277         FileData *fd_n;
1278         GtkTreeIter iter;
1279
1280         if (!lw || !lw->vf) return;
1281
1282         if (lw->vf->type == FILEVIEW_ICON)
1283                 {
1284                 if (!VFICON(lw->vf)->selection) return;
1285                 work = VFICON(lw->vf)->selection;
1286                 }
1287         else
1288                 {
1289                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(lw->vf->listview));
1290                 work = gtk_tree_selection_get_selected_rows(selection, &store);
1291                 }
1292
1293         while (work)
1294                 {
1295                 if (lw->vf->type == FILEVIEW_ICON)
1296                         {
1297                         fd_n = static_cast<FileData *>(work->data);
1298                         work = work->next;
1299                         }
1300                 else
1301                         {
1302                         tpath = static_cast<GtkTreePath *>(work->data);
1303                         gtk_tree_model_get_iter(store, &iter, tpath);
1304                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd_n, -1);
1305                         work = work->next;
1306                         }
1307
1308                 image_alter_rating(fd_n, rating);
1309                 }
1310 }
1311
1312 void layout_image_reset_orientation(LayoutWindow *lw)
1313 {
1314         ImageWindow *imd= lw->image;
1315
1316         if (!layout_valid(&lw)) return;
1317         if (!imd || !imd->pr || !imd->image_fd) return;
1318
1319         if (imd->orientation < 1 || imd->orientation > 8) imd->orientation = 1;
1320
1321         if (options->image.exif_rotate_enable)
1322                 {
1323                 if (g_strcmp0(imd->image_fd->format_name, "heif") != 0)
1324                         {
1325                         imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
1326                         }
1327                 else
1328                         {
1329                         imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
1330                         }
1331                 }
1332         else
1333                 {
1334                 imd->orientation = 1;
1335                 }
1336
1337         if (imd->image_fd->user_orientation != 0)
1338                 {
1339                  imd->orientation = imd->image_fd->user_orientation;
1340                 }
1341
1342         pixbuf_renderer_set_orientation(reinterpret_cast<PixbufRenderer *>(imd->pr), imd->orientation);
1343 }
1344
1345 void layout_image_set_desaturate(LayoutWindow *lw, gboolean desaturate)
1346 {
1347         if (!layout_valid(&lw)) return;
1348
1349         image_set_desaturate(lw->image, desaturate);
1350 }
1351
1352 gboolean layout_image_get_desaturate(LayoutWindow *lw)
1353 {
1354         if (!layout_valid(&lw)) return FALSE;
1355
1356         return image_get_desaturate(lw->image);
1357 }
1358
1359 void layout_image_set_overunderexposed(LayoutWindow *lw, gboolean overunderexposed)
1360 {
1361         if (!layout_valid(&lw)) return;
1362
1363         image_set_overunderexposed(lw->image, overunderexposed);
1364 }
1365
1366 #pragma GCC diagnostic push
1367 #pragma GCC diagnostic ignored "-Wunused-function"
1368 gboolean layout_image_get_overunderexposed_unused(LayoutWindow *lw)
1369 {
1370         if (!layout_valid(&lw)) return FALSE;
1371
1372 //      return image_get_overunderexposed(lw->image);
1373         return FALSE;
1374 }
1375 #pragma GCC diagnostic pop
1376
1377 void layout_image_set_ignore_alpha(LayoutWindow *lw, gboolean ignore_alpha)
1378 {
1379    if (!layout_valid(&lw)) return;
1380
1381    lw->options.ignore_alpha = ignore_alpha;
1382    image_set_ignore_alpha(lw->image, ignore_alpha);
1383 }
1384
1385 /* stereo */
1386 #pragma GCC diagnostic push
1387 #pragma GCC diagnostic ignored "-Wunused-function"
1388 gint layout_image_stereo_get_unused(LayoutWindow *lw)
1389 {
1390         if (!layout_valid(&lw)) return 0;
1391
1392 //      return image_stereo_get(lw->image);
1393         return 0;
1394 }
1395
1396 void layout_image_stereo_set_unused(LayoutWindow *lw, gint stereo_mode)
1397 {
1398         if (!layout_valid(&lw)) return;
1399
1400         image_stereo_set(lw->image, stereo_mode);
1401 }
1402 void layout_image_stereo_swap_unused(LayoutWindow *lw)
1403 {
1404         if (!layout_valid(&lw)) return;
1405
1406 //      image_stereo_swap(lw->image);
1407 }
1408 #pragma GCC diagnostic pop
1409
1410 gint layout_image_stereo_pixbuf_get(LayoutWindow *lw)
1411 {
1412         if (!layout_valid(&lw)) return 0;
1413
1414         return image_stereo_pixbuf_get(lw->image);
1415 }
1416
1417 void layout_image_stereo_pixbuf_set(LayoutWindow *lw, gint stereo_mode)
1418 {
1419         if (!layout_valid(&lw)) return;
1420
1421         image_stereo_pixbuf_set(lw->image, static_cast<StereoPixbufData>(stereo_mode));
1422 }
1423
1424 const gchar *layout_image_get_path(LayoutWindow *lw)
1425 {
1426         if (!layout_valid(&lw)) return nullptr;
1427
1428         return image_get_path(lw->image);
1429 }
1430
1431 #pragma GCC diagnostic push
1432 #pragma GCC diagnostic ignored "-Wunused-function"
1433 const gchar *layout_image_get_name_unused(LayoutWindow *lw)
1434 {
1435         if (!layout_valid(&lw)) return nullptr;
1436
1437         return image_get_name(lw->image);
1438 }
1439 #pragma GCC diagnostic pop
1440
1441 FileData *layout_image_get_fd(LayoutWindow *lw)
1442 {
1443         if (!layout_valid(&lw)) return nullptr;
1444
1445         return image_get_fd(lw->image);
1446 }
1447
1448 CollectionData *layout_image_get_collection(LayoutWindow *lw, CollectInfo **info)
1449 {
1450         if (!layout_valid(&lw)) return nullptr;
1451
1452         return image_get_collection(lw->image, info);
1453 }
1454
1455 gint layout_image_get_index(LayoutWindow *lw)
1456 {
1457         return layout_list_get_index(lw, image_get_fd(lw->image));
1458 }
1459
1460 /*
1461  *----------------------------------------------------------------------------
1462  * image changers
1463  *----------------------------------------------------------------------------
1464  */
1465
1466 void layout_image_set_fd(LayoutWindow *lw, FileData *fd)
1467 {
1468         if (!layout_valid(&lw)) return;
1469
1470         image_change_fd(lw->image, fd, image_zoom_get_default(lw->image));
1471
1472         if (lw->full_screen && lw->image != lw->full_screen->imd)
1473                 {
1474                 image_change_fd(lw->full_screen->imd, fd, image_zoom_get_default(lw->full_screen->imd));
1475                 }
1476
1477
1478         layout_list_sync_fd(lw, fd);
1479         layout_image_slideshow_continue_check(lw);
1480         layout_bars_new_image(lw);
1481         layout_image_animate_new_file(lw);
1482
1483         if (fd)
1484                 {
1485                 image_chain_append_end(fd->path);
1486                 }
1487 }
1488
1489 void layout_image_set_with_ahead(LayoutWindow *lw, FileData *fd, FileData *read_ahead_fd)
1490 {
1491         if (!layout_valid(&lw)) return;
1492
1493 /** @FIXME This should be handled at the caller: in vflist_select_image
1494         if (path)
1495                 {
1496                 const gchar *old_path;
1497
1498                 old_path = layout_image_get_path(lw);
1499                 if (old_path && strcmp(path, old_path) == 0) return;
1500                 }
1501 */
1502         layout_image_set_fd(lw, fd);
1503         if (options->image.enable_read_ahead) image_prebuffer_set(lw->image, read_ahead_fd);
1504 }
1505
1506 void layout_image_set_index(LayoutWindow *lw, gint index)
1507 {
1508         FileData *fd;
1509         FileData *read_ahead_fd;
1510         gint old;
1511
1512         if (!layout_valid(&lw)) return;
1513
1514         old = layout_list_get_index(lw, layout_image_get_fd(lw));
1515         fd = layout_list_get_fd(lw, index);
1516
1517         if (old > index)
1518                 {
1519                 read_ahead_fd = layout_list_get_fd(lw, index - 1);
1520                 }
1521         else
1522                 {
1523                 read_ahead_fd = layout_list_get_fd(lw, index + 1);
1524                 }
1525
1526         if (layout_selection_count(lw, nullptr) > 1)
1527                 {
1528                 GList *x = layout_selection_list_by_index(lw);
1529                 GList *y;
1530                 GList *last;
1531
1532                 for (last = y = x; y; y = y->next)
1533                         last = y;
1534                 for (y = x; y && (GPOINTER_TO_INT(y->data)) != index; y = y->next)
1535                         ;
1536
1537                 if (y)
1538                         {
1539                         gint newindex;
1540
1541                         if ((index > old && (index != GPOINTER_TO_INT(last->data) || old != GPOINTER_TO_INT(x->data)))
1542                             || (old == GPOINTER_TO_INT(last->data) && index == GPOINTER_TO_INT(x->data)))
1543                                 {
1544                                 if (y->next)
1545                                         newindex = GPOINTER_TO_INT(y->next->data);
1546                                 else
1547                                         newindex = GPOINTER_TO_INT(x->data);
1548                                 }
1549                         else
1550                                 {
1551                                 if (y->prev)
1552                                         newindex = GPOINTER_TO_INT(y->prev->data);
1553                                 else
1554                                         newindex = GPOINTER_TO_INT(last->data);
1555                                 }
1556
1557                         read_ahead_fd = layout_list_get_fd(lw, newindex);
1558                         }
1559
1560                 while (x)
1561                         x = g_list_remove(x, x->data);
1562                 }
1563
1564         layout_image_set_with_ahead(lw, fd, read_ahead_fd);
1565 }
1566
1567 static void layout_image_set_collection_real(LayoutWindow *lw, CollectionData *cd, CollectInfo *info, gboolean forward)
1568 {
1569         if (!layout_valid(&lw)) return;
1570
1571         image_change_from_collection(lw->image, cd, info, image_zoom_get_default(lw->image));
1572         if (options->image.enable_read_ahead)
1573                 {
1574                 CollectInfo *r_info;
1575                 if (forward)
1576                         {
1577                         r_info = collection_next_by_info(cd, info);
1578                         if (!r_info) r_info = collection_prev_by_info(cd, info);
1579                         }
1580                 else
1581                         {
1582                         r_info = collection_prev_by_info(cd, info);
1583                         if (!r_info) r_info = collection_next_by_info(cd, info);
1584                         }
1585                 if (r_info) image_prebuffer_set(lw->image, r_info->fd);
1586                 }
1587
1588         layout_image_slideshow_continue_check(lw);
1589         layout_bars_new_image(lw);
1590 }
1591
1592 void layout_image_set_collection(LayoutWindow *lw, CollectionData *cd, CollectInfo *info)
1593 {
1594         layout_image_set_collection_real(lw, cd, info, TRUE);
1595         layout_list_sync_fd(lw, layout_image_get_fd(lw));
1596 }
1597
1598 void layout_image_refresh(LayoutWindow *lw)
1599 {
1600         if (!layout_valid(&lw)) return;
1601
1602         image_reload(lw->image);
1603 }
1604
1605 void layout_image_color_profile_set(LayoutWindow *lw,
1606                                     gint input_type,
1607                                     gboolean use_image)
1608 {
1609         if (!layout_valid(&lw)) return;
1610
1611         image_color_profile_set(lw->image, input_type, use_image);
1612 }
1613
1614 gboolean layout_image_color_profile_get(LayoutWindow *lw,
1615                                         gint *input_type,
1616                                         gboolean *use_image)
1617 {
1618         if (!layout_valid(&lw)) return FALSE;
1619
1620         return image_color_profile_get(lw->image, input_type, use_image);
1621 }
1622
1623 void layout_image_color_profile_set_use(LayoutWindow *lw, gboolean enable)
1624 {
1625         if (!layout_valid(&lw)) return;
1626
1627         image_color_profile_set_use(lw->image, enable);
1628 }
1629
1630 gboolean layout_image_color_profile_get_use(LayoutWindow *lw)
1631 {
1632         if (!layout_valid(&lw)) return FALSE;
1633
1634         return image_color_profile_get_use(lw->image);
1635 }
1636
1637 gboolean layout_image_color_profile_get_status(LayoutWindow *lw, gchar **image_profile, gchar **screen_profile)
1638 {
1639         if (!layout_valid(&lw)) return FALSE;
1640
1641         return image_color_profile_get_status(lw->image, image_profile, screen_profile);
1642 }
1643
1644 /*
1645  *----------------------------------------------------------------------------
1646  * list walkers
1647  *----------------------------------------------------------------------------
1648  */
1649
1650 void layout_image_next(LayoutWindow *lw)
1651 {
1652         gint current;
1653         CollectionData *cd;
1654         CollectInfo *info;
1655
1656         if (!layout_valid(&lw)) return;
1657
1658         if (layout_image_slideshow_active(lw))
1659                 {
1660                 layout_image_slideshow_next(lw);
1661                 return;
1662                 }
1663
1664         if (layout_selection_count(lw, nullptr) > 1)
1665                 {
1666                 GList *x = layout_selection_list_by_index(lw);
1667                 gint old = layout_list_get_index(lw, layout_image_get_fd(lw));
1668                 GList *y;
1669
1670                 for (y = x; y && (GPOINTER_TO_INT(y->data)) != old; y = y->next)
1671                         ;
1672                 if (y)
1673                         {
1674                         if (y->next)
1675                                 layout_image_set_index(lw, GPOINTER_TO_INT(y->next->data));
1676                         else
1677                                 {
1678                                 if (options->circular_selection_lists)
1679                                         {
1680                                         layout_image_set_index(lw, GPOINTER_TO_INT(x->data));
1681                                         }
1682                                 }
1683                         }
1684                 while (x)
1685                         x = g_list_remove(x, x->data);
1686                 if (y) /* not dereferenced */
1687                         return;
1688                 }
1689
1690         cd = image_get_collection(lw->image, &info);
1691
1692         if (cd && info)
1693                 {
1694                 info = collection_next_by_info(cd, info);
1695                 if (info)
1696                         {
1697                         layout_image_set_collection_real(lw, cd, info, TRUE);
1698                         }
1699                 else
1700                         {
1701                         image_osd_icon(lw->image, IMAGE_OSD_LAST, -1);
1702                         }
1703                 return;
1704                 }
1705
1706         current = layout_image_get_index(lw);
1707
1708         if (current >= 0)
1709                 {
1710                 if (static_cast<guint>(current) < layout_list_count(lw, nullptr) - 1)
1711                         {
1712                         layout_image_set_index(lw, current + 1);
1713                         }
1714                 else
1715                         {
1716                         image_osd_icon(lw->image, IMAGE_OSD_LAST, -1);
1717                         }
1718                 }
1719         else
1720                 {
1721                 layout_image_set_index(lw, 0);
1722                 }
1723 }
1724
1725 void layout_image_prev(LayoutWindow *lw)
1726 {
1727         gint current;
1728         CollectionData *cd;
1729         CollectInfo *info;
1730
1731         if (!layout_valid(&lw)) return;
1732
1733         if (layout_image_slideshow_active(lw))
1734                 {
1735                 layout_image_slideshow_prev(lw);
1736                 return;
1737                 }
1738
1739         if (layout_selection_count(lw, nullptr) > 1)
1740                 {
1741                 GList *x = layout_selection_list_by_index(lw);
1742                 gint old = layout_list_get_index(lw, layout_image_get_fd(lw));
1743                 GList *y;
1744                 GList *last;
1745
1746                 for (last = y = x; y; y = y->next)
1747                         last = y;
1748                 for (y = x; y && (GPOINTER_TO_INT(y->data)) != old; y = y->next)
1749                         ;
1750                 if (y)
1751                         {
1752                         if (y->prev)
1753                                 layout_image_set_index(lw, GPOINTER_TO_INT(y->prev->data));
1754                         else
1755                                 {
1756                                 if (options->circular_selection_lists)
1757                                         {
1758                                         layout_image_set_index(lw, GPOINTER_TO_INT(last->data));
1759                                         }
1760                                 }
1761                         }
1762                 while (x)
1763                         x = g_list_remove(x, x->data);
1764                 if (y) /* not dereferenced */
1765                         return;
1766                 }
1767
1768         cd = image_get_collection(lw->image, &info);
1769
1770         if (cd && info)
1771                 {
1772                 info = collection_prev_by_info(cd, info);
1773                 if (info)
1774                         {
1775                         layout_image_set_collection_real(lw, cd, info, FALSE);
1776                         }
1777                 else
1778                         {
1779                         image_osd_icon(lw->image, IMAGE_OSD_FIRST, -1);
1780                         }
1781                 return;
1782                 }
1783
1784         current = layout_image_get_index(lw);
1785
1786         if (current >= 0)
1787                 {
1788                 if (current > 0)
1789                         {
1790                         layout_image_set_index(lw, current - 1);
1791                         }
1792                 else
1793                         {
1794                         image_osd_icon(lw->image, IMAGE_OSD_FIRST, -1);
1795                         }
1796                 }
1797         else
1798                 {
1799                 layout_image_set_index(lw, layout_list_count(lw, nullptr) - 1);
1800                 }
1801 }
1802
1803 void layout_image_first(LayoutWindow *lw)
1804 {
1805         gint current;
1806         CollectionData *cd;
1807         CollectInfo *info;
1808
1809         if (!layout_valid(&lw)) return;
1810
1811         cd = image_get_collection(lw->image, &info);
1812
1813         if (cd && info)
1814                 {
1815                 CollectInfo *first_collection;
1816                 first_collection = collection_get_first(cd);
1817                 if (first_collection != info)
1818                         {
1819                         layout_image_set_collection_real(lw, cd, first_collection, TRUE);
1820                         }
1821                 return;
1822                 }
1823
1824         current = layout_image_get_index(lw);
1825         if (current != 0 && layout_list_count(lw, nullptr) > 0)
1826                 {
1827                 layout_image_set_index(lw, 0);
1828                 }
1829 }
1830
1831 void layout_image_last(LayoutWindow *lw)
1832 {
1833         gint current;
1834         gint count;
1835         CollectionData *cd;
1836         CollectInfo *info;
1837
1838         if (!layout_valid(&lw)) return;
1839
1840         cd = image_get_collection(lw->image, &info);
1841
1842         if (cd && info)
1843                 {
1844                 CollectInfo *last_collection;
1845                 last_collection = collection_get_last(cd);
1846                 if (last_collection != info)
1847                         {
1848                         layout_image_set_collection_real(lw, cd, last_collection, FALSE);
1849                         }
1850                 return;
1851                 }
1852
1853         current = layout_image_get_index(lw);
1854         count = layout_list_count(lw, nullptr);
1855         if (current != count - 1 && count > 0)
1856                 {
1857                 layout_image_set_index(lw, count - 1);
1858                 }
1859 }
1860
1861 /*
1862  *----------------------------------------------------------------------------
1863  * mouse callbacks
1864  *----------------------------------------------------------------------------
1865  */
1866
1867 static gint image_idx(LayoutWindow *lw, ImageWindow *imd)
1868 {
1869         gint i;
1870
1871         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1872                 {
1873                 if (lw->split_images[i] == imd)
1874                         break;
1875                 }
1876         if (i < MAX_SPLIT_IMAGES)
1877                 {
1878                 return i;
1879                 }
1880         return -1;
1881 }
1882
1883 static void layout_image_focus_in_cb(ImageWindow *imd, gpointer data)
1884 {
1885         auto lw = static_cast<LayoutWindow *>(data);
1886
1887         gint i = image_idx(lw, imd);
1888
1889         if (i != -1)
1890                 {
1891                 DEBUG_1("image activate focus_in %d", i);
1892                 layout_image_activate(lw, i, FALSE);
1893                 }
1894 }
1895
1896
1897 static void layout_image_button_cb(ImageWindow *imd, GdkEventButton *event, gpointer data)
1898 {
1899         auto lw = static_cast<LayoutWindow *>(data);
1900         GtkWidget *menu;
1901         LayoutWindow *lw_new;
1902         gchar *dest_dir;
1903
1904         switch (event->button)
1905                 {
1906                 case MOUSE_BUTTON_LEFT:
1907                         if (event->button == MOUSE_BUTTON_LEFT && event->type == GDK_2BUTTON_PRESS)
1908                                 {
1909                                 layout_image_full_screen_toggle(lw);
1910                                 }
1911
1912                         else if (options->image_l_click_archive && imd-> image_fd && imd->image_fd->format_class == FORMAT_CLASS_ARCHIVE)
1913                                 {
1914                                 dest_dir = open_archive(imd->image_fd);
1915                                 if (dest_dir)
1916                                         {
1917                                         lw_new = layout_new_from_default();
1918                                         layout_set_path(lw_new, dest_dir);
1919                                         g_free(dest_dir);
1920                                         }
1921                                 else
1922                                         {
1923                                         warning_dialog(_("Cannot open archive file"), _("See the Log Window"), GQ_ICON_DIALOG_WARNING, nullptr);
1924                                         }
1925                                 }
1926                         else if (options->image_l_click_video && options->image_l_click_video_editor && imd-> image_fd && imd->image_fd->format_class == FORMAT_CLASS_VIDEO)
1927                                 {
1928                                 start_editor_from_file(options->image_l_click_video_editor, imd->image_fd);
1929                                 }
1930                         else if (options->image_lm_click_nav && lw->split_mode == SPLIT_NONE)
1931                                 layout_image_next(lw);
1932                         break;
1933                 case MOUSE_BUTTON_MIDDLE:
1934                         if (options->image_lm_click_nav && lw->split_mode == SPLIT_NONE)
1935                                 layout_image_prev(lw);
1936                         break;
1937                 case MOUSE_BUTTON_RIGHT:
1938                         menu = layout_image_pop_menu(lw);
1939                         if (imd == lw->image)
1940                                 {
1941                                 g_object_set_data(G_OBJECT(menu), "click_parent", imd->widget);
1942                                 }
1943                         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
1944                         break;
1945                 default:
1946                         break;
1947                 }
1948 }
1949
1950 static void layout_image_scroll_cb(ImageWindow *imd, GdkEventScroll *event, gpointer data)
1951 {
1952         auto lw = static_cast<LayoutWindow *>(data);
1953
1954         gint i = image_idx(lw, imd);
1955
1956         if (i != -1)
1957                 {
1958                 DEBUG_1("image activate scroll %d", i);
1959                 layout_image_activate(lw, i, FALSE);
1960                 }
1961
1962
1963         if ((event->state & GDK_CONTROL_MASK) ||
1964                                 (imd->mouse_wheel_mode && !options->image_lm_click_nav))
1965                 {
1966                 switch (event->direction)
1967                         {
1968                         case GDK_SCROLL_UP:
1969                                 layout_image_zoom_adjust_at_point(lw, get_zoom_increment(), event->x, event->y, event->state & GDK_SHIFT_MASK);
1970                                 break;
1971                         case GDK_SCROLL_DOWN:
1972                                 layout_image_zoom_adjust_at_point(lw, -get_zoom_increment(), event->x, event->y, event->state & GDK_SHIFT_MASK);
1973                                 break;
1974                         default:
1975                                 break;
1976                         }
1977                 }
1978         else if (options->mousewheel_scrolls)
1979                 {
1980                 switch (event->direction)
1981                         {
1982                         case GDK_SCROLL_UP:
1983                                 image_scroll(imd, 0, -MOUSEWHEEL_SCROLL_SIZE);
1984                                 break;
1985                         case GDK_SCROLL_DOWN:
1986                                 image_scroll(imd, 0, MOUSEWHEEL_SCROLL_SIZE);
1987                                 break;
1988                         case GDK_SCROLL_LEFT:
1989                                 image_scroll(imd, -MOUSEWHEEL_SCROLL_SIZE, 0);
1990                                 break;
1991                         case GDK_SCROLL_RIGHT:
1992                                 image_scroll(imd, MOUSEWHEEL_SCROLL_SIZE, 0);
1993                                 break;
1994                         default:
1995                                 break;
1996                         }
1997                 }
1998         else
1999                 {
2000                 switch (event->direction)
2001                         {
2002                         case GDK_SCROLL_UP:
2003                                 layout_image_prev(lw);
2004                                 break;
2005                         case GDK_SCROLL_DOWN:
2006                                 layout_image_next(lw);
2007                                 break;
2008                         default:
2009                                 break;
2010                         }
2011                 }
2012 }
2013
2014 static void layout_image_drag_cb(ImageWindow *imd, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer data)
2015 {
2016         gint i;
2017         auto lw = static_cast<LayoutWindow *>(data);
2018         gdouble sx;
2019         gdouble sy;
2020
2021         if (lw->full_screen && lw->image != lw->full_screen->imd &&
2022             imd != lw->full_screen->imd)
2023                 {
2024                 if (event->state & GDK_CONTROL_MASK)
2025                         {
2026                         image_get_scroll_center(imd, &sx, &sy);
2027                         }
2028                 else
2029                         {
2030                         image_get_scroll_center(lw->full_screen->imd, &sx, &sy);
2031                         sx += dx;
2032                         sy += dy;
2033                         }
2034                 image_set_scroll_center(lw->full_screen->imd, sx, sy);
2035                 }
2036
2037         if (!(event->state & GDK_SHIFT_MASK)) return;
2038
2039         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
2040                 {
2041                 if (lw->split_images[i] && lw->split_images[i] != imd)
2042                         {
2043
2044                         if (event->state & GDK_CONTROL_MASK)
2045                                 {
2046                                 image_get_scroll_center(imd, &sx, &sy);
2047                                 }
2048                         else
2049                                 {
2050                                 image_get_scroll_center(lw->split_images[i], &sx, &sy);
2051                                 sx += dx;
2052                                 sy += dy;
2053                                 }
2054                         image_set_scroll_center(lw->split_images[i], sx, sy);
2055                         }
2056                 }
2057 }
2058
2059 static void layout_image_button_inactive_cb(ImageWindow *imd, GdkEventButton *event, gpointer data)
2060 {
2061         auto lw = static_cast<LayoutWindow *>(data);
2062         GtkWidget *menu;
2063         gint i = image_idx(lw, imd);
2064
2065         if (i != -1)
2066                 {
2067                 layout_image_activate(lw, i, FALSE);
2068                 }
2069
2070         switch (event->button)
2071                 {
2072                 case MOUSE_BUTTON_RIGHT:
2073                         menu = layout_image_pop_menu(lw);
2074                         if (imd == lw->image)
2075                                 {
2076                                 g_object_set_data(G_OBJECT(menu), "click_parent", imd->widget);
2077                                 }
2078                         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
2079                         break;
2080                 default:
2081                         break;
2082                 }
2083
2084 }
2085
2086 static void layout_image_drag_inactive_cb(ImageWindow *imd, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer data)
2087 {
2088         auto lw = static_cast<LayoutWindow *>(data);
2089         gint i = image_idx(lw, imd);
2090
2091         if (i != -1)
2092                 {
2093                 layout_image_activate(lw, i, FALSE);
2094                 }
2095
2096         /* continue as with active image */
2097         layout_image_drag_cb(imd, event, dx, dy, data);
2098 }
2099
2100
2101 static void layout_image_set_buttons(LayoutWindow *lw)
2102 {
2103         image_set_button_func(lw->image, layout_image_button_cb, lw);
2104         image_set_scroll_func(lw->image, layout_image_scroll_cb, lw);
2105 }
2106
2107 static void layout_image_set_buttons_inactive(LayoutWindow *lw, gint i)
2108 {
2109         image_set_button_func(lw->split_images[i], layout_image_button_inactive_cb, lw);
2110         image_set_scroll_func(lw->split_images[i], layout_image_scroll_cb, lw);
2111 }
2112
2113 /* Returns the length of an integer */
2114 static gint num_length(gint num)
2115 {
2116         gint len = 0;
2117         if (num < 0) num = -num;
2118         while (num)
2119                 {
2120                 num /= 10;
2121                 len++;
2122                 }
2123         return len;
2124 }
2125
2126 void layout_status_update_pixel_cb(PixbufRenderer *pr, gpointer data)
2127 {
2128         auto lw = static_cast<LayoutWindow *>(data);
2129         gint x_pixel;
2130         gint y_pixel;
2131         gint width;
2132         gint height;
2133         gchar *text;
2134         PangoAttrList *attrs;
2135
2136         if (!data || !layout_valid(&lw) || !lw->image
2137             || !lw->options.show_info_pixel || lw->image->unknown) return;
2138
2139         pixbuf_renderer_get_image_size(pr, &width, &height);
2140         if (width < 1 || height < 1) return;
2141
2142         pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
2143
2144         if(x_pixel >= 0 && y_pixel >= 0)
2145                 {
2146                 gint r_mouse;
2147                 gint g_mouse;
2148                 gint b_mouse;
2149
2150                 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
2151                                                  &r_mouse, &g_mouse, &b_mouse);
2152
2153                 text = g_strdup_printf(_("[%*d,%*d]: RGB(%3d,%3d,%3d)"),
2154                                          num_length(width - 1), x_pixel,
2155                                          num_length(height - 1), y_pixel,
2156                                          r_mouse, g_mouse, b_mouse);
2157
2158                 }
2159         else
2160                 {
2161                 text = g_strdup_printf(_("[%*s,%*s]: RGB(---,---,---)"),
2162                                          num_length(width - 1), " ",
2163                                          num_length(height - 1), " ");
2164                 }
2165
2166         attrs = pango_attr_list_new();
2167         pango_attr_list_insert(attrs, pango_attr_family_new("Monospace"));
2168         gtk_label_set_text(GTK_LABEL(lw->info_pixel), text);
2169         gtk_label_set_attributes(GTK_LABEL(lw->info_pixel), attrs);
2170         pango_attr_list_unref(attrs);
2171         g_free(text);
2172 }
2173
2174
2175 /*
2176  *----------------------------------------------------------------------------
2177  * setup
2178  *----------------------------------------------------------------------------
2179  */
2180
2181 static void layout_image_update_cb(ImageWindow *, gpointer data)
2182 {
2183         auto lw = static_cast<LayoutWindow *>(data);
2184         layout_status_update_image(lw);
2185 }
2186
2187 GtkWidget *layout_image_new(LayoutWindow *lw, gint i)
2188 {
2189         if (!lw->split_image_sizegroup) lw->split_image_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
2190
2191         if (!lw->split_images[i])
2192                 {
2193                 lw->split_images[i] = image_new(TRUE);
2194
2195                 g_object_ref(lw->split_images[i]->widget);
2196
2197                 g_signal_connect(G_OBJECT(lw->split_images[i]->pr), "update-pixel",
2198                                  G_CALLBACK(layout_status_update_pixel_cb), lw);
2199
2200                 image_background_set_color_from_options(lw->split_images[i], FALSE);
2201
2202                 image_auto_refresh_enable(lw->split_images[i], TRUE);
2203
2204                 layout_image_dnd_init(lw, i);
2205                 image_color_profile_set(lw->split_images[i],
2206                                         options->color_profile.input_type,
2207                                         options->color_profile.use_image);
2208                 image_color_profile_set_use(lw->split_images[i], options->color_profile.enabled);
2209
2210                 gtk_size_group_add_widget(lw->split_image_sizegroup, lw->split_images[i]->widget);
2211                 gtk_widget_set_size_request(lw->split_images[i]->widget, IMAGE_MIN_WIDTH, -1);
2212
2213                 image_set_focus_in_func(lw->split_images[i], layout_image_focus_in_cb, lw);
2214
2215                 }
2216
2217         return lw->split_images[i]->widget;
2218 }
2219
2220 void layout_image_deactivate(LayoutWindow *lw, gint i)
2221 {
2222         if (!lw->split_images[i]) return;
2223         image_set_update_func(lw->split_images[i], nullptr, nullptr);
2224         layout_image_set_buttons_inactive(lw, i);
2225         image_set_drag_func(lw->split_images[i], layout_image_drag_inactive_cb, lw);
2226
2227         image_attach_window(lw->split_images[i], nullptr, nullptr, nullptr, FALSE);
2228         image_select(lw->split_images[i], FALSE);
2229 }
2230
2231 /* force should be set after change of lw->split_mode */
2232 void layout_image_activate(LayoutWindow *lw, gint i, gboolean force)
2233 {
2234         FileData *fd;
2235
2236         if (!lw->split_images[i]) return;
2237         if (!force && lw->active_split_image == i) return;
2238
2239         /* deactivate currently active */
2240         if (lw->active_split_image != i)
2241                 layout_image_deactivate(lw, lw->active_split_image);
2242
2243         lw->image = lw->split_images[i];
2244         lw->active_split_image = i;
2245
2246         image_set_update_func(lw->image, layout_image_update_cb, lw);
2247         layout_image_set_buttons(lw);
2248         image_set_drag_func(lw->image, layout_image_drag_cb, lw);
2249
2250         image_attach_window(lw->image, lw->window, nullptr, GQ_APPNAME, FALSE);
2251
2252         /* do not highlight selected image in SPLIT_NONE */
2253         /* maybe the image should be selected always and highlight should be controlled by
2254            another image option */
2255         if (lw->split_mode != SPLIT_NONE)
2256                 image_select(lw->split_images[i], TRUE);
2257         else
2258                 image_select(lw->split_images[i], FALSE);
2259
2260         fd = image_get_fd(lw->image);
2261
2262         if (fd)
2263                 {
2264                 layout_set_fd(lw, fd);
2265                 }
2266         layout_status_update_image(lw);
2267 }
2268
2269
2270 static void layout_image_setup_split_common(LayoutWindow *lw, gint n)
2271 {
2272         gboolean frame = (n > 1) || (!lw->options.tools_float && !lw->options.tools_hidden);
2273         gint i;
2274
2275         for (i = 0; i < n; i++)
2276                 if (!lw->split_images[i])
2277                         {
2278                         FileData *img_fd = nullptr;
2279                         double zoom = 0.0;
2280
2281                         layout_image_new(lw, i);
2282                         image_set_frame(lw->split_images[i], frame);
2283                         image_set_selectable(lw->split_images[i], (n > 1));
2284
2285                         if (lw->image)
2286                                 {
2287                                 image_osd_copy_status(lw->image, lw->split_images[i]);
2288                                 }
2289
2290                         if (layout_selection_count(lw, nullptr) > 1)
2291                                 {
2292                                 GList *work = g_list_last(layout_selection_list(lw));
2293                                 gint j = 0;
2294
2295                                 while (work && j < i)
2296                                         {
2297                                         auto fd = static_cast<FileData *>(work->data);
2298                                         work = work->prev;
2299
2300                                         if (!fd || !*fd->path || fd->parent ||
2301                                                                                 fd == lw->split_images[0]->image_fd)
2302                                                 {
2303                                                 continue;
2304                                                 }
2305                                         img_fd = fd;
2306
2307                                         j++;
2308                                         }
2309                                 }
2310
2311                         if (!img_fd && lw->image)
2312                                 {
2313                                 img_fd = image_get_fd(lw->image);
2314                                 zoom = image_zoom_get(lw->image);
2315                                 }
2316
2317                         if (img_fd)
2318                                 {
2319                                 gdouble sx;
2320                                 gdouble sy;
2321                                 image_change_fd(lw->split_images[i], img_fd, zoom);
2322                                 image_get_scroll_center(lw->image, &sx, &sy);
2323                                 image_set_scroll_center(lw->split_images[i], sx, sy);
2324                                 }
2325                         layout_image_deactivate(lw, i);
2326                         }
2327                 else
2328                         {
2329                         image_set_frame(lw->split_images[i], frame);
2330                         image_set_selectable(lw->split_images[i], (n > 1));
2331                         }
2332
2333         for (i = n; i < MAX_SPLIT_IMAGES; i++)
2334                 {
2335                 if (lw->split_images[i])
2336                         {
2337                         g_object_unref(lw->split_images[i]->widget);
2338                         lw->split_images[i] = nullptr;
2339                         }
2340                 }
2341
2342         if (!lw->image || lw->active_split_image < 0 || lw->active_split_image >= n)
2343                 {
2344                 layout_image_activate(lw, 0, TRUE);
2345                 }
2346         else
2347                 {
2348                 /* this will draw the frame around selected image (image_select)
2349                    on switch from single to split images */
2350                 layout_image_activate(lw, lw->active_split_image, TRUE);
2351                 }
2352 }
2353
2354 GtkWidget *layout_image_setup_split_none(LayoutWindow *lw)
2355 {
2356         lw->split_mode = SPLIT_NONE;
2357
2358         layout_image_setup_split_common(lw, 1);
2359
2360         lw->split_image_widget = lw->split_images[0]->widget;
2361
2362         return lw->split_image_widget;
2363 }
2364
2365
2366 GtkWidget *layout_image_setup_split_hv(LayoutWindow *lw, gboolean horizontal)
2367 {
2368         GtkWidget *paned;
2369
2370         lw->split_mode = horizontal ? SPLIT_HOR : SPLIT_VERT;
2371
2372         layout_image_setup_split_common(lw, 2);
2373
2374         /* horizontal split means vpaned and vice versa */
2375         paned = gtk_paned_new(horizontal ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL);
2376         DEBUG_NAME(paned);
2377
2378         gtk_paned_pack1(GTK_PANED(paned), lw->split_images[0]->widget, TRUE, TRUE);
2379         gtk_paned_pack2(GTK_PANED(paned), lw->split_images[1]->widget, TRUE, TRUE);
2380
2381         gtk_widget_show(lw->split_images[0]->widget);
2382         gtk_widget_show(lw->split_images[1]->widget);
2383
2384         lw->split_image_widget = paned;
2385
2386         return lw->split_image_widget;
2387
2388 }
2389
2390 static GtkWidget *layout_image_setup_split_triple(LayoutWindow *lw)
2391 {
2392         GtkWidget *hpaned1;
2393         GtkWidget *hpaned2;
2394         GtkAllocation allocation;
2395         gint i;
2396         gint pane_pos;
2397
2398         lw->split_mode = SPLIT_TRIPLE;
2399
2400         layout_image_setup_split_common(lw, 3);
2401
2402         gtk_widget_get_allocation(lw->utility_paned, &allocation);
2403
2404         hpaned1 = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
2405         DEBUG_NAME(hpaned1);
2406         hpaned2 = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
2407         DEBUG_NAME(hpaned2);
2408
2409         if (lw->bar && gtk_widget_get_visible(lw->bar))
2410                 {
2411                 pane_pos = (gtk_paned_get_position(GTK_PANED(lw->utility_paned))) / 3;
2412                 }
2413         else
2414                 {
2415                 pane_pos = allocation.width / 3;
2416                 }
2417
2418         gtk_paned_set_position(GTK_PANED(hpaned1), pane_pos);
2419         gtk_paned_set_position(GTK_PANED(hpaned2), pane_pos);
2420
2421         gtk_paned_pack1(GTK_PANED(hpaned1), lw->split_images[0]->widget, TRUE, TRUE);
2422         gtk_paned_pack1(GTK_PANED(hpaned2), lw->split_images[1]->widget, TRUE, TRUE);
2423         gtk_paned_pack2(GTK_PANED(hpaned2), lw->split_images[2]->widget, TRUE, TRUE);
2424         gtk_paned_pack2(GTK_PANED(hpaned1), hpaned2, TRUE, TRUE);
2425
2426         for (i = 0; i < 3; i++)
2427                 {
2428                 gtk_widget_show(lw->split_images[i]->widget);
2429                 }
2430
2431         gtk_widget_show(hpaned1);
2432         gtk_widget_show(hpaned2);
2433
2434         lw->split_image_widget = hpaned1;
2435
2436         return lw->split_image_widget;
2437 }
2438
2439 GtkWidget *layout_image_setup_split_quad(LayoutWindow *lw)
2440 {
2441         GtkWidget *hpaned;
2442         GtkWidget *vpaned1;
2443         GtkWidget *vpaned2;
2444         gint i;
2445
2446         lw->split_mode = SPLIT_QUAD;
2447
2448         layout_image_setup_split_common(lw, 4);
2449
2450         hpaned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
2451         DEBUG_NAME(hpaned);
2452         vpaned1 = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
2453         DEBUG_NAME(vpaned1);
2454         vpaned2 = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
2455         DEBUG_NAME(vpaned2);
2456
2457         gtk_paned_pack1(GTK_PANED(vpaned1), lw->split_images[0]->widget, TRUE, TRUE);
2458         gtk_paned_pack2(GTK_PANED(vpaned1), lw->split_images[2]->widget, TRUE, TRUE);
2459
2460         gtk_paned_pack1(GTK_PANED(vpaned2), lw->split_images[1]->widget, TRUE, TRUE);
2461         gtk_paned_pack2(GTK_PANED(vpaned2), lw->split_images[3]->widget, TRUE, TRUE);
2462
2463         gtk_paned_pack1(GTK_PANED(hpaned), vpaned1, TRUE, TRUE);
2464         gtk_paned_pack2(GTK_PANED(hpaned), vpaned2, TRUE, TRUE);
2465
2466         for (i = 0; i < 4; i++)
2467                 gtk_widget_show(lw->split_images[i]->widget);
2468
2469         gtk_widget_show(vpaned1);
2470         gtk_widget_show(vpaned2);
2471
2472         lw->split_image_widget = hpaned;
2473
2474         return lw->split_image_widget;
2475
2476 }
2477
2478 GtkWidget *layout_image_setup_split(LayoutWindow *lw, ImageSplitMode mode)
2479 {
2480         switch (mode)
2481                 {
2482                 case SPLIT_HOR:
2483                         return layout_image_setup_split_hv(lw, TRUE);
2484                 case SPLIT_VERT:
2485                         return layout_image_setup_split_hv(lw, FALSE);
2486                 case SPLIT_TRIPLE:
2487                         return layout_image_setup_split_triple(lw);
2488                 case SPLIT_QUAD:
2489                         return layout_image_setup_split_quad(lw);
2490                 case SPLIT_NONE:
2491                 default:
2492                         return layout_image_setup_split_none(lw);
2493                 }
2494 }
2495
2496
2497 /*
2498  *-----------------------------------------------------------------------------
2499  * maintenance (for rename, move, remove)
2500  *-----------------------------------------------------------------------------
2501  */
2502
2503 static void layout_image_maint_renamed(LayoutWindow *lw, FileData *fd)
2504 {
2505         if (fd == layout_image_get_fd(lw))
2506                 {
2507                 image_set_fd(lw->image, fd);
2508                 }
2509 }
2510
2511 static void layout_image_maint_removed(LayoutWindow *lw, FileData *fd)
2512 {
2513         if (fd == layout_image_get_fd(lw))
2514                 {
2515                 CollectionData *cd;
2516                 CollectInfo *info;
2517
2518                 cd = image_get_collection(lw->image, &info);
2519                 if (cd && info)
2520                         {
2521                         CollectInfo *next_collection;
2522
2523                         next_collection = collection_next_by_info(cd, info);
2524                         if (!next_collection) next_collection = collection_prev_by_info(cd, info);
2525
2526                         if (next_collection)
2527                                 {
2528                                 layout_image_set_collection(lw, cd, next_collection);
2529                                 return;
2530                                 }
2531                         layout_image_set_fd(lw, nullptr);
2532                         }
2533
2534                 /* the image will be set to the next image from the list soon,
2535                    setting it to NULL here is not necessary*/
2536                 }
2537 }
2538
2539
2540 void layout_image_notify_cb(FileData *fd, NotifyType type, gpointer data)
2541 {
2542         auto lw = static_cast<LayoutWindow *>(data);
2543
2544         if (!(type & NOTIFY_CHANGE) || !fd->change) return;
2545
2546         DEBUG_1("Notify layout_image: %s %04x", fd->path, type);
2547
2548         switch (fd->change->type)
2549                 {
2550                 case FILEDATA_CHANGE_MOVE:
2551                 case FILEDATA_CHANGE_RENAME:
2552                         layout_image_maint_renamed(lw, fd);
2553                         break;
2554                 case FILEDATA_CHANGE_DELETE:
2555                         layout_image_maint_removed(lw, fd);
2556                         break;
2557                 case FILEDATA_CHANGE_COPY:
2558                 case FILEDATA_CHANGE_UNSPECIFIED:
2559                 case FILEDATA_CHANGE_WRITE_METADATA:
2560                         break;
2561                 }
2562
2563 }
2564 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */