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