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