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