Remove ToolbarButtonData
[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         if (!lw || !lw->vf) return;
1246
1247         vf_selection_foreach(lw->vf, [lw, type](FileData *fd_n) { image_alter_orientation(lw->image, fd_n, type); });
1248 }
1249
1250 static void image_alter_rating(FileData *fd_n, const gchar *rating)
1251 {
1252         metadata_write_string(fd_n, RATING_KEY, rating);
1253         read_rating_data(fd_n);
1254 }
1255
1256 void layout_image_rating(LayoutWindow *lw, const gchar *rating)
1257 {
1258         if (!layout_valid(&lw)) return;
1259         if (!lw || !lw->vf) return;
1260
1261         vf_selection_foreach(lw->vf, [rating](FileData *fd_n) { image_alter_rating(fd_n, rating); });
1262 }
1263
1264 void layout_image_reset_orientation(LayoutWindow *lw)
1265 {
1266         ImageWindow *imd= lw->image;
1267
1268         if (!layout_valid(&lw)) return;
1269         if (!imd || !imd->pr || !imd->image_fd) return;
1270
1271         if (imd->orientation < 1 || imd->orientation > 8) imd->orientation = 1;
1272
1273         if (options->image.exif_rotate_enable)
1274                 {
1275                 if (g_strcmp0(imd->image_fd->format_name, "heif") != 0)
1276                         {
1277                         imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
1278                         }
1279                 else
1280                         {
1281                         imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
1282                         }
1283                 }
1284         else
1285                 {
1286                 imd->orientation = 1;
1287                 }
1288
1289         if (imd->image_fd->user_orientation != 0)
1290                 {
1291                  imd->orientation = imd->image_fd->user_orientation;
1292                 }
1293
1294         pixbuf_renderer_set_orientation(reinterpret_cast<PixbufRenderer *>(imd->pr), imd->orientation);
1295 }
1296
1297 void layout_image_set_desaturate(LayoutWindow *lw, gboolean desaturate)
1298 {
1299         if (!layout_valid(&lw)) return;
1300
1301         image_set_desaturate(lw->image, desaturate);
1302 }
1303
1304 gboolean layout_image_get_desaturate(LayoutWindow *lw)
1305 {
1306         if (!layout_valid(&lw)) return FALSE;
1307
1308         return image_get_desaturate(lw->image);
1309 }
1310
1311 void layout_image_set_overunderexposed(LayoutWindow *lw, gboolean overunderexposed)
1312 {
1313         if (!layout_valid(&lw)) return;
1314
1315         image_set_overunderexposed(lw->image, overunderexposed);
1316 }
1317
1318 #pragma GCC diagnostic push
1319 #pragma GCC diagnostic ignored "-Wunused-function"
1320 gboolean layout_image_get_overunderexposed_unused(LayoutWindow *lw)
1321 {
1322         if (!layout_valid(&lw)) return FALSE;
1323
1324 //      return image_get_overunderexposed(lw->image);
1325         return FALSE;
1326 }
1327 #pragma GCC diagnostic pop
1328
1329 void layout_image_set_ignore_alpha(LayoutWindow *lw, gboolean ignore_alpha)
1330 {
1331    if (!layout_valid(&lw)) return;
1332
1333    lw->options.ignore_alpha = ignore_alpha;
1334    image_set_ignore_alpha(lw->image, ignore_alpha);
1335 }
1336
1337 /* stereo */
1338 #pragma GCC diagnostic push
1339 #pragma GCC diagnostic ignored "-Wunused-function"
1340 gint layout_image_stereo_get_unused(LayoutWindow *lw)
1341 {
1342         if (!layout_valid(&lw)) return 0;
1343
1344 //      return image_stereo_get(lw->image);
1345         return 0;
1346 }
1347
1348 void layout_image_stereo_set_unused(LayoutWindow *lw, gint stereo_mode)
1349 {
1350         if (!layout_valid(&lw)) return;
1351
1352         image_stereo_set(lw->image, stereo_mode);
1353 }
1354 void layout_image_stereo_swap_unused(LayoutWindow *lw)
1355 {
1356         if (!layout_valid(&lw)) return;
1357
1358 //      image_stereo_swap(lw->image);
1359 }
1360 #pragma GCC diagnostic pop
1361
1362 gint layout_image_stereo_pixbuf_get(LayoutWindow *lw)
1363 {
1364         if (!layout_valid(&lw)) return 0;
1365
1366         return image_stereo_pixbuf_get(lw->image);
1367 }
1368
1369 void layout_image_stereo_pixbuf_set(LayoutWindow *lw, gint stereo_mode)
1370 {
1371         if (!layout_valid(&lw)) return;
1372
1373         image_stereo_pixbuf_set(lw->image, static_cast<StereoPixbufData>(stereo_mode));
1374 }
1375
1376 const gchar *layout_image_get_path(LayoutWindow *lw)
1377 {
1378         if (!layout_valid(&lw)) return nullptr;
1379
1380         return image_get_path(lw->image);
1381 }
1382
1383 #pragma GCC diagnostic push
1384 #pragma GCC diagnostic ignored "-Wunused-function"
1385 const gchar *layout_image_get_name_unused(LayoutWindow *lw)
1386 {
1387         if (!layout_valid(&lw)) return nullptr;
1388
1389         return image_get_name(lw->image);
1390 }
1391 #pragma GCC diagnostic pop
1392
1393 FileData *layout_image_get_fd(LayoutWindow *lw)
1394 {
1395         if (!layout_valid(&lw)) return nullptr;
1396
1397         return image_get_fd(lw->image);
1398 }
1399
1400 CollectionData *layout_image_get_collection(LayoutWindow *lw, CollectInfo **info)
1401 {
1402         if (!layout_valid(&lw)) return nullptr;
1403
1404         return image_get_collection(lw->image, info);
1405 }
1406
1407 gint layout_image_get_index(LayoutWindow *lw)
1408 {
1409         return layout_list_get_index(lw, image_get_fd(lw->image));
1410 }
1411
1412 /*
1413  *----------------------------------------------------------------------------
1414  * image changers
1415  *----------------------------------------------------------------------------
1416  */
1417
1418 void layout_image_set_fd(LayoutWindow *lw, FileData *fd)
1419 {
1420         if (!layout_valid(&lw)) return;
1421
1422         image_change_fd(lw->image, fd, image_zoom_get_default(lw->image));
1423
1424         if (lw->full_screen && lw->image != lw->full_screen->imd)
1425                 {
1426                 image_change_fd(lw->full_screen->imd, fd, image_zoom_get_default(lw->full_screen->imd));
1427                 }
1428
1429
1430         layout_list_sync_fd(lw, fd);
1431         layout_image_slideshow_continue_check(lw);
1432         layout_bars_new_image(lw);
1433         layout_image_animate_new_file(lw);
1434
1435         if (fd)
1436                 {
1437                 image_chain_append_end(fd->path);
1438                 }
1439 }
1440
1441 void layout_image_set_with_ahead(LayoutWindow *lw, FileData *fd, FileData *read_ahead_fd)
1442 {
1443         if (!layout_valid(&lw)) return;
1444
1445 /** @FIXME This should be handled at the caller: in vflist_select_image
1446         if (path)
1447                 {
1448                 const gchar *old_path;
1449
1450                 old_path = layout_image_get_path(lw);
1451                 if (old_path && strcmp(path, old_path) == 0) return;
1452                 }
1453 */
1454         layout_image_set_fd(lw, fd);
1455         if (options->image.enable_read_ahead) image_prebuffer_set(lw->image, read_ahead_fd);
1456 }
1457
1458 void layout_image_set_index(LayoutWindow *lw, gint index)
1459 {
1460         FileData *fd;
1461         FileData *read_ahead_fd;
1462         gint old;
1463
1464         if (!layout_valid(&lw)) return;
1465
1466         old = layout_list_get_index(lw, layout_image_get_fd(lw));
1467         fd = layout_list_get_fd(lw, index);
1468
1469         if (old > index)
1470                 {
1471                 read_ahead_fd = layout_list_get_fd(lw, index - 1);
1472                 }
1473         else
1474                 {
1475                 read_ahead_fd = layout_list_get_fd(lw, index + 1);
1476                 }
1477
1478         if (layout_selection_count(lw, nullptr) > 1)
1479                 {
1480                 GList *x = layout_selection_list_by_index(lw);
1481                 GList *y;
1482                 GList *last;
1483
1484                 for (last = y = x; y; y = y->next)
1485                         last = y;
1486                 for (y = x; y && (GPOINTER_TO_INT(y->data)) != index; y = y->next)
1487                         ;
1488
1489                 if (y)
1490                         {
1491                         gint newindex;
1492
1493                         if ((index > old && (index != GPOINTER_TO_INT(last->data) || old != GPOINTER_TO_INT(x->data)))
1494                             || (old == GPOINTER_TO_INT(last->data) && index == GPOINTER_TO_INT(x->data)))
1495                                 {
1496                                 if (y->next)
1497                                         newindex = GPOINTER_TO_INT(y->next->data);
1498                                 else
1499                                         newindex = GPOINTER_TO_INT(x->data);
1500                                 }
1501                         else
1502                                 {
1503                                 if (y->prev)
1504                                         newindex = GPOINTER_TO_INT(y->prev->data);
1505                                 else
1506                                         newindex = GPOINTER_TO_INT(last->data);
1507                                 }
1508
1509                         read_ahead_fd = layout_list_get_fd(lw, newindex);
1510                         }
1511
1512                 while (x)
1513                         x = g_list_remove(x, x->data);
1514                 }
1515
1516         layout_image_set_with_ahead(lw, fd, read_ahead_fd);
1517 }
1518
1519 static void layout_image_set_collection_real(LayoutWindow *lw, CollectionData *cd, CollectInfo *info, gboolean forward)
1520 {
1521         if (!layout_valid(&lw)) return;
1522
1523         image_change_from_collection(lw->image, cd, info, image_zoom_get_default(lw->image));
1524         if (options->image.enable_read_ahead)
1525                 {
1526                 CollectInfo *r_info;
1527                 if (forward)
1528                         {
1529                         r_info = collection_next_by_info(cd, info);
1530                         if (!r_info) r_info = collection_prev_by_info(cd, info);
1531                         }
1532                 else
1533                         {
1534                         r_info = collection_prev_by_info(cd, info);
1535                         if (!r_info) r_info = collection_next_by_info(cd, info);
1536                         }
1537                 if (r_info) image_prebuffer_set(lw->image, r_info->fd);
1538                 }
1539
1540         layout_image_slideshow_continue_check(lw);
1541         layout_bars_new_image(lw);
1542 }
1543
1544 void layout_image_set_collection(LayoutWindow *lw, CollectionData *cd, CollectInfo *info)
1545 {
1546         layout_image_set_collection_real(lw, cd, info, TRUE);
1547         layout_list_sync_fd(lw, layout_image_get_fd(lw));
1548 }
1549
1550 void layout_image_refresh(LayoutWindow *lw)
1551 {
1552         if (!layout_valid(&lw)) return;
1553
1554         image_reload(lw->image);
1555 }
1556
1557 void layout_image_color_profile_set(LayoutWindow *lw,
1558                                     gint input_type,
1559                                     gboolean use_image)
1560 {
1561         if (!layout_valid(&lw)) return;
1562
1563         image_color_profile_set(lw->image, input_type, use_image);
1564 }
1565
1566 gboolean layout_image_color_profile_get(LayoutWindow *lw,
1567                                         gint *input_type,
1568                                         gboolean *use_image)
1569 {
1570         if (!layout_valid(&lw)) return FALSE;
1571
1572         return image_color_profile_get(lw->image, input_type, use_image);
1573 }
1574
1575 void layout_image_color_profile_set_use(LayoutWindow *lw, gboolean enable)
1576 {
1577         if (!layout_valid(&lw)) return;
1578
1579         image_color_profile_set_use(lw->image, enable);
1580 }
1581
1582 gboolean layout_image_color_profile_get_use(LayoutWindow *lw)
1583 {
1584         if (!layout_valid(&lw)) return FALSE;
1585
1586         return image_color_profile_get_use(lw->image);
1587 }
1588
1589 gboolean layout_image_color_profile_get_status(LayoutWindow *lw, gchar **image_profile, gchar **screen_profile)
1590 {
1591         if (!layout_valid(&lw)) return FALSE;
1592
1593         return image_color_profile_get_status(lw->image, image_profile, screen_profile);
1594 }
1595
1596 /*
1597  *----------------------------------------------------------------------------
1598  * list walkers
1599  *----------------------------------------------------------------------------
1600  */
1601
1602 void layout_image_next(LayoutWindow *lw)
1603 {
1604         gint current;
1605         CollectionData *cd;
1606         CollectInfo *info;
1607
1608         if (!layout_valid(&lw)) return;
1609
1610         if (layout_image_slideshow_active(lw))
1611                 {
1612                 layout_image_slideshow_next(lw);
1613                 return;
1614                 }
1615
1616         if (layout_selection_count(lw, nullptr) > 1)
1617                 {
1618                 GList *x = layout_selection_list_by_index(lw);
1619                 gint old = layout_list_get_index(lw, layout_image_get_fd(lw));
1620                 GList *y;
1621
1622                 for (y = x; y && (GPOINTER_TO_INT(y->data)) != old; y = y->next)
1623                         ;
1624                 if (y)
1625                         {
1626                         if (y->next)
1627                                 layout_image_set_index(lw, GPOINTER_TO_INT(y->next->data));
1628                         else
1629                                 {
1630                                 if (options->circular_selection_lists)
1631                                         {
1632                                         layout_image_set_index(lw, GPOINTER_TO_INT(x->data));
1633                                         }
1634                                 }
1635                         }
1636                 while (x)
1637                         x = g_list_remove(x, x->data);
1638                 if (y) /* not dereferenced */
1639                         return;
1640                 }
1641
1642         cd = image_get_collection(lw->image, &info);
1643
1644         if (cd && info)
1645                 {
1646                 info = collection_next_by_info(cd, info);
1647                 if (info)
1648                         {
1649                         layout_image_set_collection_real(lw, cd, info, TRUE);
1650                         }
1651                 else
1652                         {
1653                         image_osd_icon(lw->image, IMAGE_OSD_LAST, -1);
1654                         }
1655                 return;
1656                 }
1657
1658         current = layout_image_get_index(lw);
1659
1660         if (current >= 0)
1661                 {
1662                 if (static_cast<guint>(current) < layout_list_count(lw, nullptr) - 1)
1663                         {
1664                         layout_image_set_index(lw, current + 1);
1665                         }
1666                 else
1667                         {
1668                         image_osd_icon(lw->image, IMAGE_OSD_LAST, -1);
1669                         }
1670                 }
1671         else
1672                 {
1673                 layout_image_set_index(lw, 0);
1674                 }
1675 }
1676
1677 void layout_image_prev(LayoutWindow *lw)
1678 {
1679         gint current;
1680         CollectionData *cd;
1681         CollectInfo *info;
1682
1683         if (!layout_valid(&lw)) return;
1684
1685         if (layout_image_slideshow_active(lw))
1686                 {
1687                 layout_image_slideshow_prev(lw);
1688                 return;
1689                 }
1690
1691         if (layout_selection_count(lw, nullptr) > 1)
1692                 {
1693                 GList *x = layout_selection_list_by_index(lw);
1694                 gint old = layout_list_get_index(lw, layout_image_get_fd(lw));
1695                 GList *y;
1696                 GList *last;
1697
1698                 for (last = y = x; y; y = y->next)
1699                         last = y;
1700                 for (y = x; y && (GPOINTER_TO_INT(y->data)) != old; y = y->next)
1701                         ;
1702                 if (y)
1703                         {
1704                         if (y->prev)
1705                                 layout_image_set_index(lw, GPOINTER_TO_INT(y->prev->data));
1706                         else
1707                                 {
1708                                 if (options->circular_selection_lists)
1709                                         {
1710                                         layout_image_set_index(lw, GPOINTER_TO_INT(last->data));
1711                                         }
1712                                 }
1713                         }
1714                 while (x)
1715                         x = g_list_remove(x, x->data);
1716                 if (y) /* not dereferenced */
1717                         return;
1718                 }
1719
1720         cd = image_get_collection(lw->image, &info);
1721
1722         if (cd && info)
1723                 {
1724                 info = collection_prev_by_info(cd, info);
1725                 if (info)
1726                         {
1727                         layout_image_set_collection_real(lw, cd, info, FALSE);
1728                         }
1729                 else
1730                         {
1731                         image_osd_icon(lw->image, IMAGE_OSD_FIRST, -1);
1732                         }
1733                 return;
1734                 }
1735
1736         current = layout_image_get_index(lw);
1737
1738         if (current >= 0)
1739                 {
1740                 if (current > 0)
1741                         {
1742                         layout_image_set_index(lw, current - 1);
1743                         }
1744                 else
1745                         {
1746                         image_osd_icon(lw->image, IMAGE_OSD_FIRST, -1);
1747                         }
1748                 }
1749         else
1750                 {
1751                 layout_image_set_index(lw, layout_list_count(lw, nullptr) - 1);
1752                 }
1753 }
1754
1755 void layout_image_first(LayoutWindow *lw)
1756 {
1757         gint current;
1758         CollectionData *cd;
1759         CollectInfo *info;
1760
1761         if (!layout_valid(&lw)) return;
1762
1763         cd = image_get_collection(lw->image, &info);
1764
1765         if (cd && info)
1766                 {
1767                 CollectInfo *first_collection;
1768                 first_collection = collection_get_first(cd);
1769                 if (first_collection != info)
1770                         {
1771                         layout_image_set_collection_real(lw, cd, first_collection, TRUE);
1772                         }
1773                 return;
1774                 }
1775
1776         current = layout_image_get_index(lw);
1777         if (current != 0 && layout_list_count(lw, nullptr) > 0)
1778                 {
1779                 layout_image_set_index(lw, 0);
1780                 }
1781 }
1782
1783 void layout_image_last(LayoutWindow *lw)
1784 {
1785         gint current;
1786         gint count;
1787         CollectionData *cd;
1788         CollectInfo *info;
1789
1790         if (!layout_valid(&lw)) return;
1791
1792         cd = image_get_collection(lw->image, &info);
1793
1794         if (cd && info)
1795                 {
1796                 CollectInfo *last_collection;
1797                 last_collection = collection_get_last(cd);
1798                 if (last_collection != info)
1799                         {
1800                         layout_image_set_collection_real(lw, cd, last_collection, FALSE);
1801                         }
1802                 return;
1803                 }
1804
1805         current = layout_image_get_index(lw);
1806         count = layout_list_count(lw, nullptr);
1807         if (current != count - 1 && count > 0)
1808                 {
1809                 layout_image_set_index(lw, count - 1);
1810                 }
1811 }
1812
1813 /*
1814  *----------------------------------------------------------------------------
1815  * mouse callbacks
1816  *----------------------------------------------------------------------------
1817  */
1818
1819 static gint image_idx(LayoutWindow *lw, ImageWindow *imd)
1820 {
1821         gint i;
1822
1823         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1824                 {
1825                 if (lw->split_images[i] == imd)
1826                         break;
1827                 }
1828         if (i < MAX_SPLIT_IMAGES)
1829                 {
1830                 return i;
1831                 }
1832         return -1;
1833 }
1834
1835 static void layout_image_focus_in_cb(ImageWindow *imd, gpointer data)
1836 {
1837         auto lw = static_cast<LayoutWindow *>(data);
1838
1839         gint i = image_idx(lw, imd);
1840
1841         if (i != -1)
1842                 {
1843                 DEBUG_1("image activate focus_in %d", i);
1844                 layout_image_activate(lw, i, FALSE);
1845                 }
1846 }
1847
1848
1849 static void layout_image_button_cb(ImageWindow *imd, GdkEventButton *event, gpointer data)
1850 {
1851         auto lw = static_cast<LayoutWindow *>(data);
1852         GtkWidget *menu;
1853         LayoutWindow *lw_new;
1854         gchar *dest_dir;
1855
1856         switch (event->button)
1857                 {
1858                 case MOUSE_BUTTON_LEFT:
1859                         if (event->button == MOUSE_BUTTON_LEFT && event->type == GDK_2BUTTON_PRESS)
1860                                 {
1861                                 layout_image_full_screen_toggle(lw);
1862                                 }
1863
1864                         else if (options->image_l_click_archive && imd-> image_fd && imd->image_fd->format_class == FORMAT_CLASS_ARCHIVE)
1865                                 {
1866                                 dest_dir = open_archive(imd->image_fd);
1867                                 if (dest_dir)
1868                                         {
1869                                         lw_new = layout_new_from_default();
1870                                         layout_set_path(lw_new, dest_dir);
1871                                         g_free(dest_dir);
1872                                         }
1873                                 else
1874                                         {
1875                                         warning_dialog(_("Cannot open archive file"), _("See the Log Window"), GQ_ICON_DIALOG_WARNING, nullptr);
1876                                         }
1877                                 }
1878                         else if (options->image_l_click_video && options->image_l_click_video_editor && imd-> image_fd && imd->image_fd->format_class == FORMAT_CLASS_VIDEO)
1879                                 {
1880                                 start_editor_from_file(options->image_l_click_video_editor, imd->image_fd);
1881                                 }
1882                         else if (options->image_lm_click_nav && lw->split_mode == SPLIT_NONE)
1883                                 layout_image_next(lw);
1884                         break;
1885                 case MOUSE_BUTTON_MIDDLE:
1886                         if (options->image_lm_click_nav && lw->split_mode == SPLIT_NONE)
1887                                 layout_image_prev(lw);
1888                         break;
1889                 case MOUSE_BUTTON_RIGHT:
1890                         menu = layout_image_pop_menu(lw);
1891                         if (imd == lw->image)
1892                                 {
1893                                 g_object_set_data(G_OBJECT(menu), "click_parent", imd->widget);
1894                                 }
1895                         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
1896                         break;
1897                 default:
1898                         break;
1899                 }
1900 }
1901
1902 static void layout_image_scroll_cb(ImageWindow *imd, GdkEventScroll *event, gpointer data)
1903 {
1904         auto lw = static_cast<LayoutWindow *>(data);
1905
1906         gint i = image_idx(lw, imd);
1907
1908         if (i != -1)
1909                 {
1910                 DEBUG_1("image activate scroll %d", i);
1911                 layout_image_activate(lw, i, FALSE);
1912                 }
1913
1914
1915         if ((event->state & GDK_CONTROL_MASK) ||
1916                                 (imd->mouse_wheel_mode && !options->image_lm_click_nav))
1917                 {
1918                 switch (event->direction)
1919                         {
1920                         case GDK_SCROLL_UP:
1921                                 layout_image_zoom_adjust_at_point(lw, get_zoom_increment(), event->x, event->y, event->state & GDK_SHIFT_MASK);
1922                                 break;
1923                         case GDK_SCROLL_DOWN:
1924                                 layout_image_zoom_adjust_at_point(lw, -get_zoom_increment(), event->x, event->y, event->state & GDK_SHIFT_MASK);
1925                                 break;
1926                         default:
1927                                 break;
1928                         }
1929                 }
1930         else if (options->mousewheel_scrolls)
1931                 {
1932                 switch (event->direction)
1933                         {
1934                         case GDK_SCROLL_UP:
1935                                 image_scroll(imd, 0, -MOUSEWHEEL_SCROLL_SIZE);
1936                                 break;
1937                         case GDK_SCROLL_DOWN:
1938                                 image_scroll(imd, 0, MOUSEWHEEL_SCROLL_SIZE);
1939                                 break;
1940                         case GDK_SCROLL_LEFT:
1941                                 image_scroll(imd, -MOUSEWHEEL_SCROLL_SIZE, 0);
1942                                 break;
1943                         case GDK_SCROLL_RIGHT:
1944                                 image_scroll(imd, MOUSEWHEEL_SCROLL_SIZE, 0);
1945                                 break;
1946                         default:
1947                                 break;
1948                         }
1949                 }
1950         else
1951                 {
1952                 switch (event->direction)
1953                         {
1954                         case GDK_SCROLL_UP:
1955                                 layout_image_prev(lw);
1956                                 break;
1957                         case GDK_SCROLL_DOWN:
1958                                 layout_image_next(lw);
1959                                 break;
1960                         default:
1961                                 break;
1962                         }
1963                 }
1964 }
1965
1966 static void layout_image_drag_cb(ImageWindow *imd, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer data)
1967 {
1968         gint i;
1969         auto lw = static_cast<LayoutWindow *>(data);
1970         gdouble sx;
1971         gdouble sy;
1972
1973         if (lw->full_screen && lw->image != lw->full_screen->imd &&
1974             imd != lw->full_screen->imd)
1975                 {
1976                 if (event->state & GDK_CONTROL_MASK)
1977                         {
1978                         image_get_scroll_center(imd, &sx, &sy);
1979                         }
1980                 else
1981                         {
1982                         image_get_scroll_center(lw->full_screen->imd, &sx, &sy);
1983                         sx += dx;
1984                         sy += dy;
1985                         }
1986                 image_set_scroll_center(lw->full_screen->imd, sx, sy);
1987                 }
1988
1989         if (!(event->state & GDK_SHIFT_MASK)) return;
1990
1991         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1992                 {
1993                 if (lw->split_images[i] && lw->split_images[i] != imd)
1994                         {
1995
1996                         if (event->state & GDK_CONTROL_MASK)
1997                                 {
1998                                 image_get_scroll_center(imd, &sx, &sy);
1999                                 }
2000                         else
2001                                 {
2002                                 image_get_scroll_center(lw->split_images[i], &sx, &sy);
2003                                 sx += dx;
2004                                 sy += dy;
2005                                 }
2006                         image_set_scroll_center(lw->split_images[i], sx, sy);
2007                         }
2008                 }
2009 }
2010
2011 static void layout_image_button_inactive_cb(ImageWindow *imd, GdkEventButton *event, gpointer data)
2012 {
2013         auto lw = static_cast<LayoutWindow *>(data);
2014         GtkWidget *menu;
2015         gint i = image_idx(lw, imd);
2016
2017         if (i != -1)
2018                 {
2019                 layout_image_activate(lw, i, FALSE);
2020                 }
2021
2022         switch (event->button)
2023                 {
2024                 case MOUSE_BUTTON_RIGHT:
2025                         menu = layout_image_pop_menu(lw);
2026                         if (imd == lw->image)
2027                                 {
2028                                 g_object_set_data(G_OBJECT(menu), "click_parent", imd->widget);
2029                                 }
2030                         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
2031                         break;
2032                 default:
2033                         break;
2034                 }
2035
2036 }
2037
2038 static void layout_image_drag_inactive_cb(ImageWindow *imd, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer data)
2039 {
2040         auto lw = static_cast<LayoutWindow *>(data);
2041         gint i = image_idx(lw, imd);
2042
2043         if (i != -1)
2044                 {
2045                 layout_image_activate(lw, i, FALSE);
2046                 }
2047
2048         /* continue as with active image */
2049         layout_image_drag_cb(imd, event, dx, dy, data);
2050 }
2051
2052
2053 static void layout_image_set_buttons(LayoutWindow *lw)
2054 {
2055         image_set_button_func(lw->image, layout_image_button_cb, lw);
2056         image_set_scroll_func(lw->image, layout_image_scroll_cb, lw);
2057 }
2058
2059 static void layout_image_set_buttons_inactive(LayoutWindow *lw, gint i)
2060 {
2061         image_set_button_func(lw->split_images[i], layout_image_button_inactive_cb, lw);
2062         image_set_scroll_func(lw->split_images[i], layout_image_scroll_cb, lw);
2063 }
2064
2065 /* Returns the length of an integer */
2066 static gint num_length(gint num)
2067 {
2068         gint len = 0;
2069         if (num < 0) num = -num;
2070         while (num)
2071                 {
2072                 num /= 10;
2073                 len++;
2074                 }
2075         return len;
2076 }
2077
2078 void layout_status_update_pixel_cb(PixbufRenderer *pr, gpointer data)
2079 {
2080         auto lw = static_cast<LayoutWindow *>(data);
2081         gint x_pixel;
2082         gint y_pixel;
2083         gint width;
2084         gint height;
2085         gchar *text;
2086         PangoAttrList *attrs;
2087
2088         if (!data || !layout_valid(&lw) || !lw->image
2089             || !lw->options.show_info_pixel || lw->image->unknown) return;
2090
2091         pixbuf_renderer_get_image_size(pr, &width, &height);
2092         if (width < 1 || height < 1) return;
2093
2094         pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
2095
2096         if(x_pixel >= 0 && y_pixel >= 0)
2097                 {
2098                 gint r_mouse;
2099                 gint g_mouse;
2100                 gint b_mouse;
2101
2102                 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
2103                                                  &r_mouse, &g_mouse, &b_mouse);
2104
2105                 text = g_strdup_printf(_("[%*d,%*d]: RGB(%3d,%3d,%3d)"),
2106                                          num_length(width - 1), x_pixel,
2107                                          num_length(height - 1), y_pixel,
2108                                          r_mouse, g_mouse, b_mouse);
2109
2110                 }
2111         else
2112                 {
2113                 text = g_strdup_printf(_("[%*s,%*s]: RGB(---,---,---)"),
2114                                          num_length(width - 1), " ",
2115                                          num_length(height - 1), " ");
2116                 }
2117
2118         attrs = pango_attr_list_new();
2119         pango_attr_list_insert(attrs, pango_attr_family_new("Monospace"));
2120         gtk_label_set_text(GTK_LABEL(lw->info_pixel), text);
2121         gtk_label_set_attributes(GTK_LABEL(lw->info_pixel), attrs);
2122         pango_attr_list_unref(attrs);
2123         g_free(text);
2124 }
2125
2126
2127 /*
2128  *----------------------------------------------------------------------------
2129  * setup
2130  *----------------------------------------------------------------------------
2131  */
2132
2133 static void layout_image_update_cb(ImageWindow *, gpointer data)
2134 {
2135         auto lw = static_cast<LayoutWindow *>(data);
2136         layout_status_update_image(lw);
2137 }
2138
2139 GtkWidget *layout_image_new(LayoutWindow *lw, gint i)
2140 {
2141         if (!lw->split_image_sizegroup) lw->split_image_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
2142
2143         if (!lw->split_images[i])
2144                 {
2145                 lw->split_images[i] = image_new(TRUE);
2146
2147                 g_object_ref(lw->split_images[i]->widget);
2148
2149                 g_signal_connect(G_OBJECT(lw->split_images[i]->pr), "update-pixel",
2150                                  G_CALLBACK(layout_status_update_pixel_cb), lw);
2151
2152                 image_background_set_color_from_options(lw->split_images[i], FALSE);
2153
2154                 image_auto_refresh_enable(lw->split_images[i], TRUE);
2155
2156                 layout_image_dnd_init(lw, i);
2157                 image_color_profile_set(lw->split_images[i],
2158                                         options->color_profile.input_type,
2159                                         options->color_profile.use_image);
2160                 image_color_profile_set_use(lw->split_images[i], options->color_profile.enabled);
2161
2162                 gtk_size_group_add_widget(lw->split_image_sizegroup, lw->split_images[i]->widget);
2163                 gtk_widget_set_size_request(lw->split_images[i]->widget, IMAGE_MIN_WIDTH, -1);
2164
2165                 image_set_focus_in_func(lw->split_images[i], layout_image_focus_in_cb, lw);
2166
2167                 }
2168
2169         return lw->split_images[i]->widget;
2170 }
2171
2172 void layout_image_deactivate(LayoutWindow *lw, gint i)
2173 {
2174         if (!lw->split_images[i]) return;
2175         image_set_update_func(lw->split_images[i], nullptr, nullptr);
2176         layout_image_set_buttons_inactive(lw, i);
2177         image_set_drag_func(lw->split_images[i], layout_image_drag_inactive_cb, lw);
2178
2179         image_attach_window(lw->split_images[i], nullptr, nullptr, nullptr, FALSE);
2180         image_select(lw->split_images[i], FALSE);
2181 }
2182
2183 /* force should be set after change of lw->split_mode */
2184 void layout_image_activate(LayoutWindow *lw, gint i, gboolean force)
2185 {
2186         FileData *fd;
2187
2188         if (!lw->split_images[i]) return;
2189         if (!force && lw->active_split_image == i) return;
2190
2191         /* deactivate currently active */
2192         if (lw->active_split_image != i)
2193                 layout_image_deactivate(lw, lw->active_split_image);
2194
2195         lw->image = lw->split_images[i];
2196         lw->active_split_image = i;
2197
2198         image_set_update_func(lw->image, layout_image_update_cb, lw);
2199         layout_image_set_buttons(lw);
2200         image_set_drag_func(lw->image, layout_image_drag_cb, lw);
2201
2202         image_attach_window(lw->image, lw->window, nullptr, GQ_APPNAME, FALSE);
2203
2204         /* do not highlight selected image in SPLIT_NONE */
2205         /* maybe the image should be selected always and highlight should be controlled by
2206            another image option */
2207         if (lw->split_mode != SPLIT_NONE)
2208                 image_select(lw->split_images[i], TRUE);
2209         else
2210                 image_select(lw->split_images[i], FALSE);
2211
2212         fd = image_get_fd(lw->image);
2213
2214         if (fd)
2215                 {
2216                 layout_set_fd(lw, fd);
2217                 }
2218         layout_status_update_image(lw);
2219 }
2220
2221
2222 static void layout_image_setup_split_common(LayoutWindow *lw, gint n)
2223 {
2224         gboolean frame = (n > 1) || (!lw->options.tools_float && !lw->options.tools_hidden);
2225         gint i;
2226
2227         for (i = 0; i < n; i++)
2228                 if (!lw->split_images[i])
2229                         {
2230                         FileData *img_fd = nullptr;
2231                         double zoom = 0.0;
2232
2233                         layout_image_new(lw, i);
2234                         image_set_frame(lw->split_images[i], frame);
2235                         image_set_selectable(lw->split_images[i], (n > 1));
2236
2237                         if (lw->image)
2238                                 {
2239                                 image_osd_copy_status(lw->image, lw->split_images[i]);
2240                                 }
2241
2242                         if (layout_selection_count(lw, nullptr) > 1)
2243                                 {
2244                                 GList *work = g_list_last(layout_selection_list(lw));
2245                                 gint j = 0;
2246
2247                                 while (work && j < i)
2248                                         {
2249                                         auto fd = static_cast<FileData *>(work->data);
2250                                         work = work->prev;
2251
2252                                         if (!fd || !*fd->path || fd->parent ||
2253                                                                                 fd == lw->split_images[0]->image_fd)
2254                                                 {
2255                                                 continue;
2256                                                 }
2257                                         img_fd = fd;
2258
2259                                         j++;
2260                                         }
2261                                 }
2262
2263                         if (!img_fd && lw->image)
2264                                 {
2265                                 img_fd = image_get_fd(lw->image);
2266                                 zoom = image_zoom_get(lw->image);
2267                                 }
2268
2269                         if (img_fd)
2270                                 {
2271                                 gdouble sx;
2272                                 gdouble sy;
2273                                 image_change_fd(lw->split_images[i], img_fd, zoom);
2274                                 image_get_scroll_center(lw->image, &sx, &sy);
2275                                 image_set_scroll_center(lw->split_images[i], sx, sy);
2276                                 }
2277                         layout_image_deactivate(lw, i);
2278                         }
2279                 else
2280                         {
2281                         image_set_frame(lw->split_images[i], frame);
2282                         image_set_selectable(lw->split_images[i], (n > 1));
2283                         }
2284
2285         for (i = n; i < MAX_SPLIT_IMAGES; i++)
2286                 {
2287                 if (lw->split_images[i])
2288                         {
2289                         g_object_unref(lw->split_images[i]->widget);
2290                         lw->split_images[i] = nullptr;
2291                         }
2292                 }
2293
2294         if (!lw->image || lw->active_split_image < 0 || lw->active_split_image >= n)
2295                 {
2296                 layout_image_activate(lw, 0, TRUE);
2297                 }
2298         else
2299                 {
2300                 /* this will draw the frame around selected image (image_select)
2301                    on switch from single to split images */
2302                 layout_image_activate(lw, lw->active_split_image, TRUE);
2303                 }
2304 }
2305
2306 GtkWidget *layout_image_setup_split_none(LayoutWindow *lw)
2307 {
2308         lw->split_mode = SPLIT_NONE;
2309
2310         layout_image_setup_split_common(lw, 1);
2311
2312         lw->split_image_widget = lw->split_images[0]->widget;
2313
2314         return lw->split_image_widget;
2315 }
2316
2317
2318 GtkWidget *layout_image_setup_split_hv(LayoutWindow *lw, gboolean horizontal)
2319 {
2320         GtkWidget *paned;
2321
2322         lw->split_mode = horizontal ? SPLIT_HOR : SPLIT_VERT;
2323
2324         layout_image_setup_split_common(lw, 2);
2325
2326         /* horizontal split means vpaned and vice versa */
2327         paned = gtk_paned_new(horizontal ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL);
2328         DEBUG_NAME(paned);
2329
2330         gtk_paned_pack1(GTK_PANED(paned), lw->split_images[0]->widget, TRUE, TRUE);
2331         gtk_paned_pack2(GTK_PANED(paned), lw->split_images[1]->widget, TRUE, TRUE);
2332
2333         gtk_widget_show(lw->split_images[0]->widget);
2334         gtk_widget_show(lw->split_images[1]->widget);
2335
2336         lw->split_image_widget = paned;
2337
2338         return lw->split_image_widget;
2339
2340 }
2341
2342 static GtkWidget *layout_image_setup_split_triple(LayoutWindow *lw)
2343 {
2344         GtkWidget *hpaned1;
2345         GtkWidget *hpaned2;
2346         GtkAllocation allocation;
2347         gint i;
2348         gint pane_pos;
2349
2350         lw->split_mode = SPLIT_TRIPLE;
2351
2352         layout_image_setup_split_common(lw, 3);
2353
2354         gtk_widget_get_allocation(lw->utility_paned, &allocation);
2355
2356         hpaned1 = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
2357         DEBUG_NAME(hpaned1);
2358         hpaned2 = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
2359         DEBUG_NAME(hpaned2);
2360
2361         if (lw->bar && gtk_widget_get_visible(lw->bar))
2362                 {
2363                 pane_pos = (gtk_paned_get_position(GTK_PANED(lw->utility_paned))) / 3;
2364                 }
2365         else
2366                 {
2367                 pane_pos = allocation.width / 3;
2368                 }
2369
2370         gtk_paned_set_position(GTK_PANED(hpaned1), pane_pos);
2371         gtk_paned_set_position(GTK_PANED(hpaned2), pane_pos);
2372
2373         gtk_paned_pack1(GTK_PANED(hpaned1), lw->split_images[0]->widget, TRUE, TRUE);
2374         gtk_paned_pack1(GTK_PANED(hpaned2), lw->split_images[1]->widget, TRUE, TRUE);
2375         gtk_paned_pack2(GTK_PANED(hpaned2), lw->split_images[2]->widget, TRUE, TRUE);
2376         gtk_paned_pack2(GTK_PANED(hpaned1), hpaned2, TRUE, TRUE);
2377
2378         for (i = 0; i < 3; i++)
2379                 {
2380                 gtk_widget_show(lw->split_images[i]->widget);
2381                 }
2382
2383         gtk_widget_show(hpaned1);
2384         gtk_widget_show(hpaned2);
2385
2386         lw->split_image_widget = hpaned1;
2387
2388         return lw->split_image_widget;
2389 }
2390
2391 GtkWidget *layout_image_setup_split_quad(LayoutWindow *lw)
2392 {
2393         GtkWidget *hpaned;
2394         GtkWidget *vpaned1;
2395         GtkWidget *vpaned2;
2396         gint i;
2397
2398         lw->split_mode = SPLIT_QUAD;
2399
2400         layout_image_setup_split_common(lw, 4);
2401
2402         hpaned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
2403         DEBUG_NAME(hpaned);
2404         vpaned1 = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
2405         DEBUG_NAME(vpaned1);
2406         vpaned2 = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
2407         DEBUG_NAME(vpaned2);
2408
2409         gtk_paned_pack1(GTK_PANED(vpaned1), lw->split_images[0]->widget, TRUE, TRUE);
2410         gtk_paned_pack2(GTK_PANED(vpaned1), lw->split_images[2]->widget, TRUE, TRUE);
2411
2412         gtk_paned_pack1(GTK_PANED(vpaned2), lw->split_images[1]->widget, TRUE, TRUE);
2413         gtk_paned_pack2(GTK_PANED(vpaned2), lw->split_images[3]->widget, TRUE, TRUE);
2414
2415         gtk_paned_pack1(GTK_PANED(hpaned), vpaned1, TRUE, TRUE);
2416         gtk_paned_pack2(GTK_PANED(hpaned), vpaned2, TRUE, TRUE);
2417
2418         for (i = 0; i < 4; i++)
2419                 gtk_widget_show(lw->split_images[i]->widget);
2420
2421         gtk_widget_show(vpaned1);
2422         gtk_widget_show(vpaned2);
2423
2424         lw->split_image_widget = hpaned;
2425
2426         return lw->split_image_widget;
2427
2428 }
2429
2430 GtkWidget *layout_image_setup_split(LayoutWindow *lw, ImageSplitMode mode)
2431 {
2432         switch (mode)
2433                 {
2434                 case SPLIT_HOR:
2435                         return layout_image_setup_split_hv(lw, TRUE);
2436                 case SPLIT_VERT:
2437                         return layout_image_setup_split_hv(lw, FALSE);
2438                 case SPLIT_TRIPLE:
2439                         return layout_image_setup_split_triple(lw);
2440                 case SPLIT_QUAD:
2441                         return layout_image_setup_split_quad(lw);
2442                 case SPLIT_NONE:
2443                 default:
2444                         return layout_image_setup_split_none(lw);
2445                 }
2446 }
2447
2448
2449 /*
2450  *-----------------------------------------------------------------------------
2451  * maintenance (for rename, move, remove)
2452  *-----------------------------------------------------------------------------
2453  */
2454
2455 static void layout_image_maint_renamed(LayoutWindow *lw, FileData *fd)
2456 {
2457         if (fd == layout_image_get_fd(lw))
2458                 {
2459                 image_set_fd(lw->image, fd);
2460                 }
2461 }
2462
2463 static void layout_image_maint_removed(LayoutWindow *lw, FileData *fd)
2464 {
2465         if (fd == layout_image_get_fd(lw))
2466                 {
2467                 CollectionData *cd;
2468                 CollectInfo *info;
2469
2470                 cd = image_get_collection(lw->image, &info);
2471                 if (cd && info)
2472                         {
2473                         CollectInfo *next_collection;
2474
2475                         next_collection = collection_next_by_info(cd, info);
2476                         if (!next_collection) next_collection = collection_prev_by_info(cd, info);
2477
2478                         if (next_collection)
2479                                 {
2480                                 layout_image_set_collection(lw, cd, next_collection);
2481                                 return;
2482                                 }
2483                         layout_image_set_fd(lw, nullptr);
2484                         }
2485
2486                 /* the image will be set to the next image from the list soon,
2487                    setting it to NULL here is not necessary*/
2488                 }
2489 }
2490
2491
2492 void layout_image_notify_cb(FileData *fd, NotifyType type, gpointer data)
2493 {
2494         auto lw = static_cast<LayoutWindow *>(data);
2495
2496         if (!(type & NOTIFY_CHANGE) || !fd->change) return;
2497
2498         DEBUG_1("Notify layout_image: %s %04x", fd->path, type);
2499
2500         switch (fd->change->type)
2501                 {
2502                 case FILEDATA_CHANGE_MOVE:
2503                 case FILEDATA_CHANGE_RENAME:
2504                         layout_image_maint_renamed(lw, fd);
2505                         break;
2506                 case FILEDATA_CHANGE_DELETE:
2507                         layout_image_maint_removed(lw, fd);
2508                         break;
2509                 case FILEDATA_CHANGE_COPY:
2510                 case FILEDATA_CHANGE_UNSPECIFIED:
2511                 case FILEDATA_CHANGE_WRITE_METADATA:
2512                         break;
2513                 }
2514
2515 }
2516 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */