e992f756266a46a60e3a1c62ce39dcfd65831828
[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 static void li_pop_menu_copy_image_cb(GtkWidget *, gpointer data)
599 {
600         auto lw = static_cast<LayoutWindow *>(data);
601         ImageWindow *imd = lw->image;
602
603         GdkPixbuf *pixbuf;
604         pixbuf = image_get_pixbuf(imd);
605         if (!pixbuf) return;
606         gtk_clipboard_set_image(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), pixbuf);
607 }
608
609 static void li_pop_menu_move_cb(GtkWidget *widget, gpointer data)
610 {
611         auto lw = static_cast<LayoutWindow *>(data);
612
613         file_util_move(layout_image_get_fd(lw), nullptr, nullptr,
614                        li_pop_menu_click_parent(widget, lw));
615 }
616
617 static void li_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
618 {
619         auto lw = static_cast<LayoutWindow *>(data);
620
621         file_util_rename(layout_image_get_fd(lw), nullptr,
622                          li_pop_menu_click_parent(widget, lw));
623 }
624
625 static void li_pop_menu_delete_cb(GtkWidget *widget, gpointer data)
626 {
627         auto lw = static_cast<LayoutWindow *>(data);
628
629         options->file_ops.safe_delete_enable = FALSE;
630         file_util_delete(layout_image_get_fd(lw), nullptr,
631                          li_pop_menu_click_parent(widget, lw));
632 }
633
634 static void li_pop_menu_move_to_trash_cb(GtkWidget *widget, gpointer data)
635 {
636         auto lw = static_cast<LayoutWindow *>(data);
637
638         options->file_ops.safe_delete_enable = TRUE;
639         file_util_delete(layout_image_get_fd(lw), nullptr,
640                          li_pop_menu_click_parent(widget, lw));
641 }
642
643 static void li_pop_menu_slide_start_cb(GtkWidget *, gpointer data)
644 {
645         auto lw = static_cast<LayoutWindow *>(data);
646
647         layout_image_slideshow_start(lw);
648 }
649
650 static void li_pop_menu_slide_stop_cb(GtkWidget *, gpointer data)
651 {
652         auto lw = static_cast<LayoutWindow *>(data);
653
654         layout_image_slideshow_stop(lw);
655 }
656
657 static void li_pop_menu_slide_pause_cb(GtkWidget *, gpointer data)
658 {
659         auto lw = static_cast<LayoutWindow *>(data);
660
661         layout_image_slideshow_pause_toggle(lw);
662 }
663
664 static void li_pop_menu_full_screen_cb(GtkWidget *, gpointer data)
665 {
666         auto lw = static_cast<LayoutWindow *>(data);
667
668         layout_image_full_screen_toggle(lw);
669 }
670
671 static void li_pop_menu_animate_cb(GtkWidget *, gpointer data)
672 {
673         auto lw = static_cast<LayoutWindow *>(data);
674
675         layout_image_animate_toggle(lw);
676 }
677
678 static void li_pop_menu_hide_cb(GtkWidget *, gpointer data)
679 {
680         auto lw = static_cast<LayoutWindow *>(data);
681
682         layout_tools_hide_toggle(lw);
683 }
684
685 static void li_set_layout_path_cb(GtkWidget *, gpointer data)
686 {
687         auto lw = static_cast<LayoutWindow *>(data);
688         FileData *fd;
689
690         if (!layout_valid(&lw)) return;
691
692         fd = layout_image_get_fd(lw);
693         if (fd) layout_set_fd(lw, fd);
694 }
695
696 static void li_open_archive_cb(GtkWidget *, gpointer data)
697 {
698         auto lw = static_cast<LayoutWindow *>(data);
699         LayoutWindow *lw_new;
700         gchar *dest_dir;
701
702         if (!layout_valid(&lw)) return;
703
704         dest_dir = open_archive(layout_image_get_fd(lw));
705         if (dest_dir)
706                 {
707                 lw_new = layout_new_from_default();
708                 layout_set_path(lw_new, dest_dir);
709                 g_free(dest_dir);
710                 }
711         else
712                 {
713                 warning_dialog(_("Cannot open archive file"), _("See the Log Window"), GQ_ICON_DIALOG_WARNING, nullptr);
714                 }
715 }
716
717 static gboolean li_check_if_current_path(LayoutWindow *lw, const gchar *path)
718 {
719         gchar *dirname;
720         gboolean ret;
721
722         if (!path || !layout_valid(&lw) || !lw->dir_fd) return FALSE;
723
724         dirname = g_path_get_dirname(path);
725         ret = (strcmp(lw->dir_fd->path, dirname) == 0);
726         g_free(dirname);
727         return ret;
728 }
729
730 static void layout_image_popup_menu_destroy_cb(GtkWidget *, gpointer data)
731 {
732         auto editmenu_fd_list = static_cast<GList *>(data);
733
734         filelist_free(editmenu_fd_list);
735 }
736
737 static GList *layout_image_get_fd_list(LayoutWindow *lw)
738 {
739         GList *list = nullptr;
740         FileData *fd = layout_image_get_fd(lw);
741
742         if (fd)
743                 {
744                 if (lw->vf)
745                         /* optionally include sidecars if the filelist entry is not expanded */
746                         list = vf_selection_get_one(lw->vf, fd);
747                 else
748                         list = g_list_append(nullptr, file_data_ref(fd));
749                 }
750
751         return list;
752 }
753
754 /**
755  * @brief Add file selection list to a collection
756  * @param[in] widget
757  * @param[in] data Index to the collection list menu item selected, or -1 for new collection
758  *
759  *
760  */
761 static void layout_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
762 {
763         LayoutWindow *lw;
764         GList *selection_list = nullptr;
765
766         lw = static_cast<LayoutWindow *>(submenu_item_get_data(widget));
767         selection_list = g_list_append(selection_list, layout_image_get_fd(lw));
768         pop_menu_collections(selection_list, data);
769
770         filelist_free(selection_list);
771 }
772
773 static GtkWidget *layout_image_pop_menu(LayoutWindow *lw)
774 {
775         GtkWidget *menu;
776         GtkWidget *item;
777         GtkWidget *submenu;
778         const gchar *path;
779         gboolean fullscreen;
780         GList *editmenu_fd_list;
781         GtkAccelGroup *accel_group;
782
783         path = layout_image_get_path(lw);
784         fullscreen = layout_image_full_screen_active(lw);
785
786         menu = popup_menu_short_lived();
787
788         accel_group = gtk_accel_group_new();
789         gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
790
791         g_object_set_data(G_OBJECT(menu), "window_keys", nullptr);
792         g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
793
794         menu_item_add_icon(menu, _("Zoom _in"), GQ_ICON_ZOOM_IN, G_CALLBACK(li_pop_menu_zoom_in_cb), lw);
795         menu_item_add_icon(menu, _("Zoom _out"), GQ_ICON_ZOOM_OUT, G_CALLBACK(li_pop_menu_zoom_out_cb), lw);
796         menu_item_add_icon(menu, _("Zoom _1:1"), GQ_ICON_ZOOM_100, G_CALLBACK(li_pop_menu_zoom_1_1_cb), lw);
797         menu_item_add_icon(menu, _("Zoom to fit"), GQ_ICON_ZOOM_FIT, G_CALLBACK(li_pop_menu_zoom_fit_cb), lw);
798         menu_item_add_divider(menu);
799
800         editmenu_fd_list = layout_image_get_fd_list(lw);
801         g_signal_connect(G_OBJECT(menu), "destroy",
802                          G_CALLBACK(layout_image_popup_menu_destroy_cb), editmenu_fd_list);
803         submenu = submenu_add_edit(menu, &item, G_CALLBACK(li_pop_menu_edit_cb), lw, editmenu_fd_list);
804         if (!path) gtk_widget_set_sensitive(item, FALSE);
805         menu_item_add_divider(submenu);
806         item = submenu_add_alter(menu, G_CALLBACK(li_pop_menu_alter_cb), lw);
807
808         item = menu_item_add_icon(menu, _("View in _new window"), GQ_ICON_NEW, G_CALLBACK(li_pop_menu_new_cb), lw);
809         if (!path || fullscreen) gtk_widget_set_sensitive(item, FALSE);
810
811         item = menu_item_add(menu, _("_Go to directory view"), G_CALLBACK(li_set_layout_path_cb), lw);
812         if (!path || li_check_if_current_path(lw, path)) gtk_widget_set_sensitive(item, FALSE);
813
814         item = menu_item_add_icon(menu, _("Open archive"), GQ_ICON_OPEN, G_CALLBACK(li_open_archive_cb), lw);
815         if (!path || lw->image->image_fd->format_class != FORMAT_CLASS_ARCHIVE)
816                 {
817                 gtk_widget_set_sensitive(item, FALSE);
818                 }
819
820         menu_item_add_divider(menu);
821
822         item = menu_item_add_icon(menu, _("_Copy..."), GQ_ICON_COPY, G_CALLBACK(li_pop_menu_copy_cb), lw);
823         if (!path) gtk_widget_set_sensitive(item, FALSE);
824         item = menu_item_add(menu, _("_Move..."), G_CALLBACK(li_pop_menu_move_cb), lw);
825         if (!path) gtk_widget_set_sensitive(item, FALSE);
826         item = menu_item_add(menu, _("_Rename..."), G_CALLBACK(li_pop_menu_rename_cb), lw);
827         if (!path) gtk_widget_set_sensitive(item, FALSE);
828         item = menu_item_add(menu, _("_Copy path to clipboard"), G_CALLBACK(li_pop_menu_copy_path_cb), lw);
829         item = menu_item_add(menu, _("_Copy path unquoted to clipboard"), G_CALLBACK(li_pop_menu_copy_path_unquoted_cb), lw);
830         item = menu_item_add(menu, _("Copy _image to clipboard"), G_CALLBACK(li_pop_menu_copy_image_cb), lw);
831         if (!path) gtk_widget_set_sensitive(item, FALSE);
832         menu_item_add_divider(menu);
833
834         item = menu_item_add_icon(menu,
835                                 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
836                                         _("Move to Trash"), GQ_ICON_DELETE,
837                                                                 G_CALLBACK(li_pop_menu_move_to_trash_cb), lw);
838         if (!path) gtk_widget_set_sensitive(item, FALSE);
839         item = menu_item_add_icon(menu,
840                                 options->file_ops.confirm_delete ? _("_Delete...") :
841                                         _("_Delete"), GQ_ICON_DELETE_SHRED,
842                                                                 G_CALLBACK(li_pop_menu_delete_cb), lw);
843         if (!path) gtk_widget_set_sensitive(item, FALSE);
844         menu_item_add_divider(menu);
845
846         submenu = submenu_add_collections(menu, &item,
847                                 G_CALLBACK(layout_pop_menu_collections_cb), lw);
848         gtk_widget_set_sensitive(item, TRUE);
849         menu_item_add_divider(menu);
850
851         if (layout_image_slideshow_active(lw))
852                 {
853                 menu_item_add(menu, _("Toggle _slideshow"), G_CALLBACK(li_pop_menu_slide_stop_cb), lw);
854                 if (layout_image_slideshow_paused(lw))
855                         {
856                         item = menu_item_add(menu, _("Continue slides_how"),
857                                              G_CALLBACK(li_pop_menu_slide_pause_cb), lw);
858                         }
859                 else
860                         {
861                         item = menu_item_add(menu, _("Pause slides_how"),
862                                              G_CALLBACK(li_pop_menu_slide_pause_cb), lw);
863                         }
864                 }
865         else
866                 {
867                 menu_item_add(menu, _("Toggle _slideshow"), G_CALLBACK(li_pop_menu_slide_start_cb), lw);
868                 item = menu_item_add(menu, _("Pause slides_how"), G_CALLBACK(li_pop_menu_slide_pause_cb), lw);
869                 gtk_widget_set_sensitive(item, FALSE);
870                 }
871
872         if (!fullscreen)
873                 {
874                 menu_item_add_icon(menu, _("_Full screen"), GQ_ICON_FULLSCREEN, G_CALLBACK(li_pop_menu_full_screen_cb), lw);
875                 }
876         else
877                 {
878                 menu_item_add_icon(menu, _("Exit _full screen"), GQ_ICON_LEAVE_FULLSCREEN, G_CALLBACK(li_pop_menu_full_screen_cb), lw);
879                 }
880
881         menu_item_add_check(menu, _("GIF _animation"), lw->options.animate, G_CALLBACK(li_pop_menu_animate_cb), lw);
882
883         menu_item_add_divider(menu);
884
885         item = menu_item_add_check(menu, _("Hide file _list"), lw->options.tools_hidden,
886                                    G_CALLBACK(li_pop_menu_hide_cb), lw);
887         if (fullscreen) gtk_widget_set_sensitive(item, FALSE);
888
889         return menu;
890 }
891
892 void layout_image_menu_popup(LayoutWindow *lw)
893 {
894         GtkWidget *menu;
895
896         menu = layout_image_pop_menu(lw);
897         gtk_menu_popup_at_widget(GTK_MENU(menu), lw->image->widget, GDK_GRAVITY_EAST, GDK_GRAVITY_CENTER, nullptr);
898 }
899
900 /*
901  *----------------------------------------------------------------------------
902  * dnd
903  *----------------------------------------------------------------------------
904  */
905
906 static void layout_image_dnd_receive(GtkWidget *widget, GdkDragContext *,
907                                      gint, gint,
908                                      GtkSelectionData *selection_data, guint info,
909                                      guint, gpointer data)
910 {
911         auto lw = static_cast<LayoutWindow *>(data);
912         gint i;
913         gchar *url;
914
915
916         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
917                 {
918                 if (lw->split_images[i] && lw->split_images[i]->pr == widget)
919                         break;
920                 }
921         if (i < MAX_SPLIT_IMAGES)
922                 {
923                 DEBUG_1("dnd image activate %d", i);
924                 layout_image_activate(lw, i, FALSE);
925                 }
926
927         if (info == TARGET_TEXT_PLAIN)
928                 {
929                 url = g_strdup(reinterpret_cast<const gchar *>(gtk_selection_data_get_data(selection_data)));
930                 download_web_file(url, FALSE, lw);
931                 g_free(url);
932                 }
933         else if (info == TARGET_URI_LIST || info == TARGET_APP_COLLECTION_MEMBER)
934                 {
935                 CollectionData *source;
936                 GList *list;
937                 GList *info_list;
938
939                 if (info == TARGET_URI_LIST)
940                         {
941                         list = uri_filelist_from_gtk_selection_data(selection_data);
942                         source = nullptr;
943                         info_list = nullptr;
944                         }
945                 else
946                         {
947                         source = collection_from_dnd_data(reinterpret_cast<const gchar *>(gtk_selection_data_get_data(selection_data)), &list, &info_list);
948                         }
949
950                 if (list)
951                         {
952                         auto fd = static_cast<FileData *>(list->data);
953
954                         if (isfile(fd->path))
955                                 {
956                                 gchar *base;
957                                 gint row;
958                                 FileData *dir_fd;
959
960                                 base = remove_level_from_path(fd->path);
961                                 dir_fd = file_data_new_dir(base);
962                                 if (dir_fd != lw->dir_fd)
963                                         {
964                                         layout_set_fd(lw, dir_fd);
965                                         }
966                                 file_data_unref(dir_fd);
967                                 g_free(base);
968
969                                 row = layout_list_get_index(lw, fd);
970                                 if (source && info_list)
971                                         {
972                                         layout_image_set_collection(lw, source, static_cast<CollectInfo *>(info_list->data));
973                                         }
974                                 else if (row == -1)
975                                         {
976                                         layout_image_set_fd(lw, fd);
977                                         }
978                                 else
979                                         {
980                                         layout_image_set_index(lw, row);
981                                         }
982                                 }
983                         else if (isdir(fd->path))
984                                 {
985                                 layout_set_fd(lw, fd);
986                                 layout_image_set_fd(lw, nullptr);
987                                 }
988                         }
989
990                 filelist_free(list);
991                 g_list_free(info_list);
992                 }
993 }
994
995 static void layout_image_dnd_get(GtkWidget *widget, GdkDragContext *,
996                                  GtkSelectionData *selection_data, guint,
997                                  guint, gpointer data)
998 {
999         auto lw = static_cast<LayoutWindow *>(data);
1000         FileData *fd;
1001         gint i;
1002
1003
1004         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1005                 {
1006                 if (lw->split_images[i] && lw->split_images[i]->pr == widget)
1007                         break;
1008                 }
1009         if (i < MAX_SPLIT_IMAGES)
1010                 {
1011                 DEBUG_1("dnd get from %d", i);
1012                 fd = image_get_fd(lw->split_images[i]);
1013                 }
1014         else
1015                 fd = layout_image_get_fd(lw);
1016
1017         if (fd)
1018                 {
1019                 GList *list;
1020
1021                 list = g_list_append(nullptr, fd);
1022                 uri_selection_data_set_uris_from_filelist(selection_data, list);
1023                 g_list_free(list);
1024                 }
1025         else
1026                 {
1027                 gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
1028                                        8, nullptr, 0);
1029                 }
1030 }
1031
1032 static void layout_image_dnd_end(GtkWidget *, GdkDragContext *context, gpointer data)
1033 {
1034         auto lw = static_cast<LayoutWindow *>(data);
1035         if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
1036                 {
1037                 FileData *fd;
1038                 gint row;
1039
1040                 fd = layout_image_get_fd(lw);
1041                 row = layout_list_get_index(lw, fd);
1042                 if (row < 0) return;
1043
1044                 if (!isfile(fd->path))
1045                         {
1046                         if (static_cast<guint>(row) < layout_list_count(lw, nullptr) - 1)
1047                                 {
1048                                 layout_image_next(lw);
1049                                 }
1050                         else
1051                                 {
1052                                 layout_image_prev(lw);
1053                                 }
1054                         }
1055                 layout_refresh(lw);
1056                 }
1057 }
1058
1059 static void layout_image_dnd_init(LayoutWindow *lw, gint i)
1060 {
1061         ImageWindow *imd = lw->split_images[i];
1062
1063         gtk_drag_source_set(imd->pr, GDK_BUTTON2_MASK,
1064                             dnd_file_drag_types, dnd_file_drag_types_count,
1065                             static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
1066         g_signal_connect(G_OBJECT(imd->pr), "drag_data_get",
1067                          G_CALLBACK(layout_image_dnd_get), lw);
1068         g_signal_connect(G_OBJECT(imd->pr), "drag_end",
1069                          G_CALLBACK(layout_image_dnd_end), lw);
1070
1071         gtk_drag_dest_set(imd->pr,
1072                           static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP),
1073                           dnd_file_drop_types, dnd_file_drop_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_received",
1076                          G_CALLBACK(layout_image_dnd_receive), lw);
1077 }
1078
1079
1080 /*
1081  *----------------------------------------------------------------------------
1082  * misc
1083  *----------------------------------------------------------------------------
1084  */
1085
1086 void layout_image_to_root(LayoutWindow *lw)
1087 {
1088         image_to_root_window(lw->image, (image_zoom_get(lw->image) == 0));
1089 }
1090
1091 /*
1092  *----------------------------------------------------------------------------
1093  * manipulation + accessors
1094  *----------------------------------------------------------------------------
1095  */
1096
1097 void layout_image_scroll(LayoutWindow *lw, gint x, gint y, gboolean connect_scroll)
1098 {
1099         gint i;
1100         if (!layout_valid(&lw)) return;
1101
1102         image_scroll(lw->image, x, y);
1103
1104         if (lw->full_screen && lw->image != lw->full_screen->imd)
1105                 {
1106                 image_scroll(lw->full_screen->imd, x, y);
1107                 }
1108
1109         if (!connect_scroll) return;
1110
1111         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1112                 {
1113                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
1114                         {
1115                         image_scroll(lw->split_images[i], x, y);
1116                         }
1117                 }
1118
1119 }
1120
1121 void layout_image_zoom_adjust(LayoutWindow *lw, gdouble increment, gboolean connect_zoom)
1122 {
1123         gint i;
1124         if (!layout_valid(&lw)) return;
1125
1126         image_zoom_adjust(lw->image, increment);
1127
1128         if (lw->full_screen && lw->image != lw->full_screen->imd)
1129                 {
1130                 image_zoom_adjust(lw->full_screen->imd, increment);
1131                 }
1132
1133         if (!connect_zoom) return;
1134
1135         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1136                 {
1137                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
1138                         image_zoom_adjust(lw->split_images[i], increment); ;
1139                 }
1140 }
1141
1142 void layout_image_zoom_adjust_at_point(LayoutWindow *lw, gdouble increment, gint x, gint y, gboolean connect_zoom)
1143 {
1144         gint i;
1145         if (!layout_valid(&lw)) return;
1146
1147         image_zoom_adjust_at_point(lw->image, increment, x, y);
1148
1149         if (lw->full_screen && lw->image != lw->full_screen->imd)
1150                 {
1151                 image_zoom_adjust_at_point(lw->full_screen->imd, increment, x, y);
1152                 }
1153         if (!connect_zoom && !lw->split_mode) return;
1154
1155         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1156                 {
1157                 if (lw->split_images[i] && lw->split_images[i] != lw->image &&
1158                                                 lw->split_images[i]->mouse_wheel_mode)
1159                         image_zoom_adjust_at_point(lw->split_images[i], increment, x, y);
1160                 }
1161 }
1162
1163 void layout_image_zoom_set(LayoutWindow *lw, gdouble zoom, gboolean connect_zoom)
1164 {
1165         gint i;
1166         if (!layout_valid(&lw)) return;
1167
1168         image_zoom_set(lw->image, zoom);
1169
1170         if (lw->full_screen && lw->image != lw->full_screen->imd)
1171                 {
1172                 image_zoom_set(lw->full_screen->imd, zoom);
1173                 }
1174
1175         if (!connect_zoom) return;
1176
1177         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1178                 {
1179                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
1180                         image_zoom_set(lw->split_images[i], zoom);
1181                 }
1182 }
1183
1184 void layout_image_zoom_set_fill_geometry(LayoutWindow *lw, gboolean vertical, gboolean connect_zoom)
1185 {
1186         gint i;
1187         if (!layout_valid(&lw)) return;
1188
1189         image_zoom_set_fill_geometry(lw->image, vertical);
1190
1191         if (lw->full_screen && lw->image != lw->full_screen->imd)
1192                 {
1193                 image_zoom_set_fill_geometry(lw->full_screen->imd, vertical);
1194                 }
1195
1196         if (!connect_zoom) return;
1197
1198         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1199                 {
1200                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
1201                         image_zoom_set_fill_geometry(lw->split_images[i], vertical);
1202                 }
1203 }
1204
1205 void layout_image_alter_orientation(LayoutWindow *lw, AlterType type)
1206 {
1207         if (!layout_valid(&lw)) return;
1208
1209         GtkTreeModel *store;
1210         GList *work;
1211         GtkTreeSelection *selection;
1212         GtkTreePath *tpath;
1213         FileData *fd_n;
1214         GtkTreeIter iter;
1215
1216         if (!lw || !lw->vf) return;
1217
1218         if (lw->vf->type == FILEVIEW_ICON)
1219                 {
1220                 if (!VFICON(lw->vf)->selection) return;
1221                 work = VFICON(lw->vf)->selection;
1222                 }
1223         else
1224                 {
1225                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(lw->vf->listview));
1226                 work = gtk_tree_selection_get_selected_rows(selection, &store);
1227                 }
1228
1229         while (work)
1230                 {
1231                 if (lw->vf->type == FILEVIEW_ICON)
1232                         {
1233                         fd_n = static_cast<FileData *>(work->data);
1234                         work = work->next;
1235                         }
1236                 else
1237                         {
1238                         tpath = static_cast<GtkTreePath *>(work->data);
1239                         gtk_tree_model_get_iter(store, &iter, tpath);
1240                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd_n, -1);
1241                         work = work->next;
1242                         }
1243
1244                 image_alter_orientation(lw->image, fd_n, type);
1245                 }
1246 }
1247
1248 static void image_alter_rating(FileData *fd_n, const gchar *rating)
1249 {
1250         metadata_write_string(fd_n, RATING_KEY, rating);
1251         read_rating_data(fd_n);
1252 }
1253
1254 void layout_image_rating(LayoutWindow *lw, const gchar *rating)
1255 {
1256         if (!layout_valid(&lw)) return;
1257
1258         GtkTreeModel *store;
1259         GList *work;
1260         GtkTreeSelection *selection;
1261         GtkTreePath *tpath;
1262         FileData *fd_n;
1263         GtkTreeIter iter;
1264
1265         if (!lw || !lw->vf) return;
1266
1267         if (lw->vf->type == FILEVIEW_ICON)
1268                 {
1269                 if (!VFICON(lw->vf)->selection) return;
1270                 work = VFICON(lw->vf)->selection;
1271                 }
1272         else
1273                 {
1274                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(lw->vf->listview));
1275                 work = gtk_tree_selection_get_selected_rows(selection, &store);
1276                 }
1277
1278         while (work)
1279                 {
1280                 if (lw->vf->type == FILEVIEW_ICON)
1281                         {
1282                         fd_n = static_cast<FileData *>(work->data);
1283                         work = work->next;
1284                         }
1285                 else
1286                         {
1287                         tpath = static_cast<GtkTreePath *>(work->data);
1288                         gtk_tree_model_get_iter(store, &iter, tpath);
1289                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd_n, -1);
1290                         work = work->next;
1291                         }
1292
1293                 image_alter_rating(fd_n, rating);
1294                 }
1295 }
1296
1297 void layout_image_reset_orientation(LayoutWindow *lw)
1298 {
1299         ImageWindow *imd= lw->image;
1300
1301         if (!layout_valid(&lw)) return;
1302         if (!imd || !imd->pr || !imd->image_fd) return;
1303
1304         if (imd->orientation < 1 || imd->orientation > 8) imd->orientation = 1;
1305
1306         if (options->image.exif_rotate_enable)
1307                 {
1308                 if (g_strcmp0(imd->image_fd->format_name, "heif") != 0)
1309                         {
1310                         imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
1311                         }
1312                 else
1313                         {
1314                         imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
1315                         }
1316                 }
1317         else
1318                 {
1319                 imd->orientation = 1;
1320                 }
1321
1322         if (imd->image_fd->user_orientation != 0)
1323                 {
1324                  imd->orientation = imd->image_fd->user_orientation;
1325                 }
1326
1327         pixbuf_renderer_set_orientation(reinterpret_cast<PixbufRenderer *>(imd->pr), imd->orientation);
1328 }
1329
1330 void layout_image_set_desaturate(LayoutWindow *lw, gboolean desaturate)
1331 {
1332         if (!layout_valid(&lw)) return;
1333
1334         image_set_desaturate(lw->image, desaturate);
1335 }
1336
1337 gboolean layout_image_get_desaturate(LayoutWindow *lw)
1338 {
1339         if (!layout_valid(&lw)) return FALSE;
1340
1341         return image_get_desaturate(lw->image);
1342 }
1343
1344 void layout_image_set_overunderexposed(LayoutWindow *lw, gboolean overunderexposed)
1345 {
1346         if (!layout_valid(&lw)) return;
1347
1348         image_set_overunderexposed(lw->image, overunderexposed);
1349 }
1350
1351 #pragma GCC diagnostic push
1352 #pragma GCC diagnostic ignored "-Wunused-function"
1353 gboolean layout_image_get_overunderexposed_unused(LayoutWindow *lw)
1354 {
1355         if (!layout_valid(&lw)) return FALSE;
1356
1357 //      return image_get_overunderexposed(lw->image);
1358         return FALSE;
1359 }
1360 #pragma GCC diagnostic pop
1361
1362 void layout_image_set_ignore_alpha(LayoutWindow *lw, gboolean ignore_alpha)
1363 {
1364    if (!layout_valid(&lw)) return;
1365
1366    lw->options.ignore_alpha = ignore_alpha;
1367    image_set_ignore_alpha(lw->image, ignore_alpha);
1368 }
1369
1370 /* stereo */
1371 #pragma GCC diagnostic push
1372 #pragma GCC diagnostic ignored "-Wunused-function"
1373 gint layout_image_stereo_get_unused(LayoutWindow *lw)
1374 {
1375         if (!layout_valid(&lw)) return 0;
1376
1377 //      return image_stereo_get(lw->image);
1378         return 0;
1379 }
1380
1381 void layout_image_stereo_set_unused(LayoutWindow *lw, gint stereo_mode)
1382 {
1383         if (!layout_valid(&lw)) return;
1384
1385         image_stereo_set(lw->image, stereo_mode);
1386 }
1387 void layout_image_stereo_swap_unused(LayoutWindow *lw)
1388 {
1389         if (!layout_valid(&lw)) return;
1390
1391 //      image_stereo_swap(lw->image);
1392 }
1393 #pragma GCC diagnostic pop
1394
1395 gint layout_image_stereo_pixbuf_get(LayoutWindow *lw)
1396 {
1397         if (!layout_valid(&lw)) return 0;
1398
1399         return image_stereo_pixbuf_get(lw->image);
1400 }
1401
1402 void layout_image_stereo_pixbuf_set(LayoutWindow *lw, gint stereo_mode)
1403 {
1404         if (!layout_valid(&lw)) return;
1405
1406         image_stereo_pixbuf_set(lw->image, static_cast<StereoPixbufData>(stereo_mode));
1407 }
1408
1409 const gchar *layout_image_get_path(LayoutWindow *lw)
1410 {
1411         if (!layout_valid(&lw)) return nullptr;
1412
1413         return image_get_path(lw->image);
1414 }
1415
1416 #pragma GCC diagnostic push
1417 #pragma GCC diagnostic ignored "-Wunused-function"
1418 const gchar *layout_image_get_name_unused(LayoutWindow *lw)
1419 {
1420         if (!layout_valid(&lw)) return NULL;
1421
1422         return image_get_name(lw->image);
1423 }
1424 #pragma GCC diagnostic pop
1425
1426 FileData *layout_image_get_fd(LayoutWindow *lw)
1427 {
1428         if (!layout_valid(&lw)) return nullptr;
1429
1430         return image_get_fd(lw->image);
1431 }
1432
1433 CollectionData *layout_image_get_collection(LayoutWindow *lw, CollectInfo **info)
1434 {
1435         if (!layout_valid(&lw)) return nullptr;
1436
1437         return image_get_collection(lw->image, info);
1438 }
1439
1440 gint layout_image_get_index(LayoutWindow *lw)
1441 {
1442         return layout_list_get_index(lw, image_get_fd(lw->image));
1443 }
1444
1445 /*
1446  *----------------------------------------------------------------------------
1447  * image changers
1448  *----------------------------------------------------------------------------
1449  */
1450
1451 void layout_image_set_fd(LayoutWindow *lw, FileData *fd)
1452 {
1453         if (!layout_valid(&lw)) return;
1454
1455         image_change_fd(lw->image, fd, image_zoom_get_default(lw->image));
1456
1457         if (lw->full_screen && lw->image != lw->full_screen->imd)
1458                 {
1459                 image_change_fd(lw->full_screen->imd, fd, image_zoom_get_default(lw->full_screen->imd));
1460                 }
1461
1462
1463         layout_list_sync_fd(lw, fd);
1464         layout_image_slideshow_continue_check(lw);
1465         layout_bars_new_image(lw);
1466         layout_image_animate_new_file(lw);
1467
1468         if (fd)
1469                 {
1470                 image_chain_append_end(fd->path);
1471                 }
1472 }
1473
1474 void layout_image_set_with_ahead(LayoutWindow *lw, FileData *fd, FileData *read_ahead_fd)
1475 {
1476         if (!layout_valid(&lw)) return;
1477
1478 /** @FIXME This should be handled at the caller: in vflist_select_image
1479         if (path)
1480                 {
1481                 const gchar *old_path;
1482
1483                 old_path = layout_image_get_path(lw);
1484                 if (old_path && strcmp(path, old_path) == 0) return;
1485                 }
1486 */
1487         layout_image_set_fd(lw, fd);
1488         if (options->image.enable_read_ahead) image_prebuffer_set(lw->image, read_ahead_fd);
1489 }
1490
1491 void layout_image_set_index(LayoutWindow *lw, gint index)
1492 {
1493         FileData *fd;
1494         FileData *read_ahead_fd;
1495         gint old;
1496
1497         if (!layout_valid(&lw)) return;
1498
1499         old = layout_list_get_index(lw, layout_image_get_fd(lw));
1500         fd = layout_list_get_fd(lw, index);
1501
1502         if (old > index)
1503                 {
1504                 read_ahead_fd = layout_list_get_fd(lw, index - 1);
1505                 }
1506         else
1507                 {
1508                 read_ahead_fd = layout_list_get_fd(lw, index + 1);
1509                 }
1510
1511         if (layout_selection_count(lw, nullptr) > 1)
1512                 {
1513                 GList *x = layout_selection_list_by_index(lw);
1514                 GList *y;
1515                 GList *last;
1516
1517                 for (last = y = x; y; y = y->next)
1518                         last = y;
1519                 for (y = x; y && (GPOINTER_TO_INT(y->data)) != index; y = y->next)
1520                         ;
1521
1522                 if (y)
1523                         {
1524                         gint newindex;
1525
1526                         if ((index > old && (index != GPOINTER_TO_INT(last->data) || old != GPOINTER_TO_INT(x->data)))
1527                             || (old == GPOINTER_TO_INT(last->data) && index == GPOINTER_TO_INT(x->data)))
1528                                 {
1529                                 if (y->next)
1530                                         newindex = GPOINTER_TO_INT(y->next->data);
1531                                 else
1532                                         newindex = GPOINTER_TO_INT(x->data);
1533                                 }
1534                         else
1535                                 {
1536                                 if (y->prev)
1537                                         newindex = GPOINTER_TO_INT(y->prev->data);
1538                                 else
1539                                         newindex = GPOINTER_TO_INT(last->data);
1540                                 }
1541
1542                         read_ahead_fd = layout_list_get_fd(lw, newindex);
1543                         }
1544
1545                 while (x)
1546                         x = g_list_remove(x, x->data);
1547                 }
1548
1549         layout_image_set_with_ahead(lw, fd, read_ahead_fd);
1550 }
1551
1552 static void layout_image_set_collection_real(LayoutWindow *lw, CollectionData *cd, CollectInfo *info, gboolean forward)
1553 {
1554         if (!layout_valid(&lw)) return;
1555
1556         image_change_from_collection(lw->image, cd, info, image_zoom_get_default(lw->image));
1557         if (options->image.enable_read_ahead)
1558                 {
1559                 CollectInfo *r_info;
1560                 if (forward)
1561                         {
1562                         r_info = collection_next_by_info(cd, info);
1563                         if (!r_info) r_info = collection_prev_by_info(cd, info);
1564                         }
1565                 else
1566                         {
1567                         r_info = collection_prev_by_info(cd, info);
1568                         if (!r_info) r_info = collection_next_by_info(cd, info);
1569                         }
1570                 if (r_info) image_prebuffer_set(lw->image, r_info->fd);
1571                 }
1572
1573         layout_image_slideshow_continue_check(lw);
1574         layout_bars_new_image(lw);
1575 }
1576
1577 void layout_image_set_collection(LayoutWindow *lw, CollectionData *cd, CollectInfo *info)
1578 {
1579         layout_image_set_collection_real(lw, cd, info, TRUE);
1580         layout_list_sync_fd(lw, layout_image_get_fd(lw));
1581 }
1582
1583 void layout_image_refresh(LayoutWindow *lw)
1584 {
1585         if (!layout_valid(&lw)) return;
1586
1587         image_reload(lw->image);
1588 }
1589
1590 void layout_image_color_profile_set(LayoutWindow *lw,
1591                                     gint input_type,
1592                                     gboolean use_image)
1593 {
1594         if (!layout_valid(&lw)) return;
1595
1596         image_color_profile_set(lw->image, input_type, use_image);
1597 }
1598
1599 gboolean layout_image_color_profile_get(LayoutWindow *lw,
1600                                         gint *input_type,
1601                                         gboolean *use_image)
1602 {
1603         if (!layout_valid(&lw)) return FALSE;
1604
1605         return image_color_profile_get(lw->image, input_type, use_image);
1606 }
1607
1608 void layout_image_color_profile_set_use(LayoutWindow *lw, gboolean enable)
1609 {
1610         if (!layout_valid(&lw)) return;
1611
1612         image_color_profile_set_use(lw->image, enable);
1613 }
1614
1615 gboolean layout_image_color_profile_get_use(LayoutWindow *lw)
1616 {
1617         if (!layout_valid(&lw)) return FALSE;
1618
1619         return image_color_profile_get_use(lw->image);
1620 }
1621
1622 gboolean layout_image_color_profile_get_status(LayoutWindow *lw, gchar **image_profile, gchar **screen_profile)
1623 {
1624         if (!layout_valid(&lw)) return FALSE;
1625
1626         return image_color_profile_get_status(lw->image, image_profile, screen_profile);
1627 }
1628
1629 /*
1630  *----------------------------------------------------------------------------
1631  * list walkers
1632  *----------------------------------------------------------------------------
1633  */
1634
1635 void layout_image_next(LayoutWindow *lw)
1636 {
1637         gint current;
1638         CollectionData *cd;
1639         CollectInfo *info;
1640
1641         if (!layout_valid(&lw)) return;
1642
1643         if (layout_image_slideshow_active(lw))
1644                 {
1645                 layout_image_slideshow_next(lw);
1646                 return;
1647                 }
1648
1649         if (layout_selection_count(lw, nullptr) > 1)
1650                 {
1651                 GList *x = layout_selection_list_by_index(lw);
1652                 gint old = layout_list_get_index(lw, layout_image_get_fd(lw));
1653                 GList *y;
1654
1655                 for (y = x; y && (GPOINTER_TO_INT(y->data)) != old; y = y->next)
1656                         ;
1657                 if (y)
1658                         {
1659                         if (y->next)
1660                                 layout_image_set_index(lw, GPOINTER_TO_INT(y->next->data));
1661                         else
1662                                 {
1663                                 if (options->circular_selection_lists)
1664                                         {
1665                                         layout_image_set_index(lw, GPOINTER_TO_INT(x->data));
1666                                         }
1667                                 }
1668                         }
1669                 while (x)
1670                         x = g_list_remove(x, x->data);
1671                 if (y) /* not dereferenced */
1672                         return;
1673                 }
1674
1675         cd = image_get_collection(lw->image, &info);
1676
1677         if (cd && info)
1678                 {
1679                 info = collection_next_by_info(cd, info);
1680                 if (info)
1681                         {
1682                         layout_image_set_collection_real(lw, cd, info, TRUE);
1683                         }
1684                 else
1685                         {
1686                         image_osd_icon(lw->image, IMAGE_OSD_LAST, -1);
1687                         }
1688                 return;
1689                 }
1690
1691         current = layout_image_get_index(lw);
1692
1693         if (current >= 0)
1694                 {
1695                 if (static_cast<guint>(current) < layout_list_count(lw, nullptr) - 1)
1696                         {
1697                         layout_image_set_index(lw, current + 1);
1698                         }
1699                 else
1700                         {
1701                         image_osd_icon(lw->image, IMAGE_OSD_LAST, -1);
1702                         }
1703                 }
1704         else
1705                 {
1706                 layout_image_set_index(lw, 0);
1707                 }
1708 }
1709
1710 void layout_image_prev(LayoutWindow *lw)
1711 {
1712         gint current;
1713         CollectionData *cd;
1714         CollectInfo *info;
1715
1716         if (!layout_valid(&lw)) return;
1717
1718         if (layout_image_slideshow_active(lw))
1719                 {
1720                 layout_image_slideshow_prev(lw);
1721                 return;
1722                 }
1723
1724         if (layout_selection_count(lw, nullptr) > 1)
1725                 {
1726                 GList *x = layout_selection_list_by_index(lw);
1727                 gint old = layout_list_get_index(lw, layout_image_get_fd(lw));
1728                 GList *y;
1729                 GList *last;
1730
1731                 for (last = y = x; y; y = y->next)
1732                         last = y;
1733                 for (y = x; y && (GPOINTER_TO_INT(y->data)) != old; y = y->next)
1734                         ;
1735                 if (y)
1736                         {
1737                         if (y->prev)
1738                                 layout_image_set_index(lw, GPOINTER_TO_INT(y->prev->data));
1739                         else
1740                                 {
1741                                 if (options->circular_selection_lists)
1742                                         {
1743                                         layout_image_set_index(lw, GPOINTER_TO_INT(last->data));
1744                                         }
1745                                 }
1746                         }
1747                 while (x)
1748                         x = g_list_remove(x, x->data);
1749                 if (y) /* not dereferenced */
1750                         return;
1751                 }
1752
1753         cd = image_get_collection(lw->image, &info);
1754
1755         if (cd && info)
1756                 {
1757                 info = collection_prev_by_info(cd, info);
1758                 if (info)
1759                         {
1760                         layout_image_set_collection_real(lw, cd, info, FALSE);
1761                         }
1762                 else
1763                         {
1764                         image_osd_icon(lw->image, IMAGE_OSD_FIRST, -1);
1765                         }
1766                 return;
1767                 }
1768
1769         current = layout_image_get_index(lw);
1770
1771         if (current >= 0)
1772                 {
1773                 if (current > 0)
1774                         {
1775                         layout_image_set_index(lw, current - 1);
1776                         }
1777                 else
1778                         {
1779                         image_osd_icon(lw->image, IMAGE_OSD_FIRST, -1);
1780                         }
1781                 }
1782         else
1783                 {
1784                 layout_image_set_index(lw, layout_list_count(lw, nullptr) - 1);
1785                 }
1786 }
1787
1788 void layout_image_first(LayoutWindow *lw)
1789 {
1790         gint current;
1791         CollectionData *cd;
1792         CollectInfo *info;
1793
1794         if (!layout_valid(&lw)) return;
1795
1796         cd = image_get_collection(lw->image, &info);
1797
1798         if (cd && info)
1799                 {
1800                 CollectInfo *first_collection;
1801                 first_collection = collection_get_first(cd);
1802                 if (first_collection != info)
1803                         {
1804                         layout_image_set_collection_real(lw, cd, first_collection, TRUE);
1805                         }
1806                 return;
1807                 }
1808
1809         current = layout_image_get_index(lw);
1810         if (current != 0 && layout_list_count(lw, nullptr) > 0)
1811                 {
1812                 layout_image_set_index(lw, 0);
1813                 }
1814 }
1815
1816 void layout_image_last(LayoutWindow *lw)
1817 {
1818         gint current;
1819         gint count;
1820         CollectionData *cd;
1821         CollectInfo *info;
1822
1823         if (!layout_valid(&lw)) return;
1824
1825         cd = image_get_collection(lw->image, &info);
1826
1827         if (cd && info)
1828                 {
1829                 CollectInfo *last_collection;
1830                 last_collection = collection_get_last(cd);
1831                 if (last_collection != info)
1832                         {
1833                         layout_image_set_collection_real(lw, cd, last_collection, FALSE);
1834                         }
1835                 return;
1836                 }
1837
1838         current = layout_image_get_index(lw);
1839         count = layout_list_count(lw, nullptr);
1840         if (current != count - 1 && count > 0)
1841                 {
1842                 layout_image_set_index(lw, count - 1);
1843                 }
1844 }
1845
1846 /*
1847  *----------------------------------------------------------------------------
1848  * mouse callbacks
1849  *----------------------------------------------------------------------------
1850  */
1851
1852 static gint image_idx(LayoutWindow *lw, ImageWindow *imd)
1853 {
1854         gint i;
1855
1856         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1857                 {
1858                 if (lw->split_images[i] == imd)
1859                         break;
1860                 }
1861         if (i < MAX_SPLIT_IMAGES)
1862                 {
1863                 return i;
1864                 }
1865         return -1;
1866 }
1867
1868 static void layout_image_focus_in_cb(ImageWindow *imd, gpointer data)
1869 {
1870         auto lw = static_cast<LayoutWindow *>(data);
1871
1872         gint i = image_idx(lw, imd);
1873
1874         if (i != -1)
1875                 {
1876                 DEBUG_1("image activate focus_in %d", i);
1877                 layout_image_activate(lw, i, FALSE);
1878                 }
1879 }
1880
1881
1882 static void layout_image_button_cb(ImageWindow *imd, GdkEventButton *event, gpointer data)
1883 {
1884         auto lw = static_cast<LayoutWindow *>(data);
1885         GtkWidget *menu;
1886         LayoutWindow *lw_new;
1887         gchar *dest_dir;
1888
1889         switch (event->button)
1890                 {
1891                 case MOUSE_BUTTON_LEFT:
1892                         if (event->button == MOUSE_BUTTON_LEFT && event->type == GDK_2BUTTON_PRESS)
1893                                 {
1894                                 layout_image_full_screen_toggle(lw);
1895                                 }
1896
1897                         else if (options->image_l_click_archive && imd-> image_fd && imd->image_fd->format_class == FORMAT_CLASS_ARCHIVE)
1898                                 {
1899                                 dest_dir = open_archive(imd->image_fd);
1900                                 if (dest_dir)
1901                                         {
1902                                         lw_new = layout_new_from_default();
1903                                         layout_set_path(lw_new, dest_dir);
1904                                         g_free(dest_dir);
1905                                         }
1906                                 else
1907                                         {
1908                                         warning_dialog(_("Cannot open archive file"), _("See the Log Window"), GQ_ICON_DIALOG_WARNING, nullptr);
1909                                         }
1910                                 }
1911                         else if (options->image_l_click_video && options->image_l_click_video_editor && imd-> image_fd && imd->image_fd->format_class == FORMAT_CLASS_VIDEO)
1912                                 {
1913                                 start_editor_from_file(options->image_l_click_video_editor, imd->image_fd);
1914                                 }
1915                         else if (options->image_lm_click_nav && lw->split_mode == SPLIT_NONE)
1916                                 layout_image_next(lw);
1917                         break;
1918                 case MOUSE_BUTTON_MIDDLE:
1919                         if (options->image_lm_click_nav && lw->split_mode == SPLIT_NONE)
1920                                 layout_image_prev(lw);
1921                         break;
1922                 case MOUSE_BUTTON_RIGHT:
1923                         menu = layout_image_pop_menu(lw);
1924                         if (imd == lw->image)
1925                                 {
1926                                 g_object_set_data(G_OBJECT(menu), "click_parent", imd->widget);
1927                                 }
1928                         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
1929                         break;
1930                 default:
1931                         break;
1932                 }
1933 }
1934
1935 static void layout_image_scroll_cb(ImageWindow *imd, GdkEventScroll *event, gpointer data)
1936 {
1937         auto lw = static_cast<LayoutWindow *>(data);
1938
1939         gint i = image_idx(lw, imd);
1940
1941         if (i != -1)
1942                 {
1943                 DEBUG_1("image activate scroll %d", i);
1944                 layout_image_activate(lw, i, FALSE);
1945                 }
1946
1947
1948         if ((event->state & GDK_CONTROL_MASK) ||
1949                                 (imd->mouse_wheel_mode && !options->image_lm_click_nav))
1950                 {
1951                 switch (event->direction)
1952                         {
1953                         case GDK_SCROLL_UP:
1954                                 layout_image_zoom_adjust_at_point(lw, get_zoom_increment(), event->x, event->y, event->state & GDK_SHIFT_MASK);
1955                                 break;
1956                         case GDK_SCROLL_DOWN:
1957                                 layout_image_zoom_adjust_at_point(lw, -get_zoom_increment(), event->x, event->y, event->state & GDK_SHIFT_MASK);
1958                                 break;
1959                         default:
1960                                 break;
1961                         }
1962                 }
1963         else if (options->mousewheel_scrolls)
1964                 {
1965                 switch (event->direction)
1966                         {
1967                         case GDK_SCROLL_UP:
1968                                 image_scroll(imd, 0, -MOUSEWHEEL_SCROLL_SIZE);
1969                                 break;
1970                         case GDK_SCROLL_DOWN:
1971                                 image_scroll(imd, 0, MOUSEWHEEL_SCROLL_SIZE);
1972                                 break;
1973                         case GDK_SCROLL_LEFT:
1974                                 image_scroll(imd, -MOUSEWHEEL_SCROLL_SIZE, 0);
1975                                 break;
1976                         case GDK_SCROLL_RIGHT:
1977                                 image_scroll(imd, MOUSEWHEEL_SCROLL_SIZE, 0);
1978                                 break;
1979                         default:
1980                                 break;
1981                         }
1982                 }
1983         else
1984                 {
1985                 switch (event->direction)
1986                         {
1987                         case GDK_SCROLL_UP:
1988                                 layout_image_prev(lw);
1989                                 break;
1990                         case GDK_SCROLL_DOWN:
1991                                 layout_image_next(lw);
1992                                 break;
1993                         default:
1994                                 break;
1995                         }
1996                 }
1997 }
1998
1999 static void layout_image_drag_cb(ImageWindow *imd, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer data)
2000 {
2001         gint i;
2002         auto lw = static_cast<LayoutWindow *>(data);
2003         gdouble sx, sy;
2004
2005         if (lw->full_screen && lw->image != lw->full_screen->imd &&
2006             imd != lw->full_screen->imd)
2007                 {
2008                 if (event->state & GDK_CONTROL_MASK)
2009                         {
2010                         image_get_scroll_center(imd, &sx, &sy);
2011                         }
2012                 else
2013                         {
2014                         image_get_scroll_center(lw->full_screen->imd, &sx, &sy);
2015                         sx += dx;
2016                         sy += dy;
2017                         }
2018                 image_set_scroll_center(lw->full_screen->imd, sx, sy);
2019                 }
2020
2021         if (!(event->state & GDK_SHIFT_MASK)) return;
2022
2023         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
2024                 {
2025                 if (lw->split_images[i] && lw->split_images[i] != imd)
2026                         {
2027
2028                         if (event->state & GDK_CONTROL_MASK)
2029                                 {
2030                                 image_get_scroll_center(imd, &sx, &sy);
2031                                 }
2032                         else
2033                                 {
2034                                 image_get_scroll_center(lw->split_images[i], &sx, &sy);
2035                                 sx += dx;
2036                                 sy += dy;
2037                                 }
2038                         image_set_scroll_center(lw->split_images[i], sx, sy);
2039                         }
2040                 }
2041 }
2042
2043 static void layout_image_button_inactive_cb(ImageWindow *imd, GdkEventButton *event, gpointer data)
2044 {
2045         auto lw = static_cast<LayoutWindow *>(data);
2046         GtkWidget *menu;
2047         gint i = image_idx(lw, imd);
2048
2049         if (i != -1)
2050                 {
2051                 layout_image_activate(lw, i, FALSE);
2052                 }
2053
2054         switch (event->button)
2055                 {
2056                 case MOUSE_BUTTON_RIGHT:
2057                         menu = layout_image_pop_menu(lw);
2058                         if (imd == lw->image)
2059                                 {
2060                                 g_object_set_data(G_OBJECT(menu), "click_parent", imd->widget);
2061                                 }
2062                         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
2063                         break;
2064                 default:
2065                         break;
2066                 }
2067
2068 }
2069
2070 static void layout_image_drag_inactive_cb(ImageWindow *imd, GdkEventMotion *event, gdouble dx, gdouble dy, gpointer data)
2071 {
2072         auto lw = static_cast<LayoutWindow *>(data);
2073         gint i = image_idx(lw, imd);
2074
2075         if (i != -1)
2076                 {
2077                 layout_image_activate(lw, i, FALSE);
2078                 }
2079
2080         /* continue as with active image */
2081         layout_image_drag_cb(imd, event, dx, dy, data);
2082 }
2083
2084
2085 static void layout_image_set_buttons(LayoutWindow *lw)
2086 {
2087         image_set_button_func(lw->image, layout_image_button_cb, lw);
2088         image_set_scroll_func(lw->image, layout_image_scroll_cb, lw);
2089 }
2090
2091 static void layout_image_set_buttons_inactive(LayoutWindow *lw, gint i)
2092 {
2093         image_set_button_func(lw->split_images[i], layout_image_button_inactive_cb, lw);
2094         image_set_scroll_func(lw->split_images[i], layout_image_scroll_cb, lw);
2095 }
2096
2097 /* Returns the length of an integer */
2098 static gint num_length(gint num)
2099 {
2100         gint len = 0;
2101         if (num < 0) num = -num;
2102         while (num)
2103                 {
2104                 num /= 10;
2105                 len++;
2106                 }
2107         return len;
2108 }
2109
2110 void layout_status_update_pixel_cb(PixbufRenderer *pr, gpointer data)
2111 {
2112         auto lw = static_cast<LayoutWindow *>(data);
2113         gint x_pixel, y_pixel;
2114         gint width, height;
2115         gchar *text;
2116         PangoAttrList *attrs;
2117
2118         if (!data || !layout_valid(&lw) || !lw->image
2119             || !lw->options.show_info_pixel || lw->image->unknown) return;
2120
2121         pixbuf_renderer_get_image_size(pr, &width, &height);
2122         if (width < 1 || height < 1) return;
2123
2124         pixbuf_renderer_get_mouse_position(pr, &x_pixel, &y_pixel);
2125
2126         if(x_pixel >= 0 && y_pixel >= 0)
2127                 {
2128                 gint r_mouse, g_mouse, b_mouse;
2129
2130                 pixbuf_renderer_get_pixel_colors(pr, x_pixel, y_pixel,
2131                                                  &r_mouse, &g_mouse, &b_mouse);
2132
2133                 text = g_strdup_printf(_("[%*d,%*d]: RGB(%3d,%3d,%3d)"),
2134                                          num_length(width - 1), x_pixel,
2135                                          num_length(height - 1), y_pixel,
2136                                          r_mouse, g_mouse, b_mouse);
2137
2138                 }
2139         else
2140                 {
2141                 text = g_strdup_printf(_("[%*s,%*s]: RGB(---,---,---)"),
2142                                          num_length(width - 1), " ",
2143                                          num_length(height - 1), " ");
2144                 }
2145
2146         attrs = pango_attr_list_new();
2147         pango_attr_list_insert(attrs, pango_attr_family_new("Monospace"));
2148         gtk_label_set_text(GTK_LABEL(lw->info_pixel), text);
2149         gtk_label_set_attributes(GTK_LABEL(lw->info_pixel), attrs);
2150         pango_attr_list_unref(attrs);
2151         g_free(text);
2152 }
2153
2154
2155 /*
2156  *----------------------------------------------------------------------------
2157  * setup
2158  *----------------------------------------------------------------------------
2159  */
2160
2161 static void layout_image_update_cb(ImageWindow *, gpointer data)
2162 {
2163         auto lw = static_cast<LayoutWindow *>(data);
2164         layout_status_update_image(lw);
2165 }
2166
2167 GtkWidget *layout_image_new(LayoutWindow *lw, gint i)
2168 {
2169         if (!lw->split_image_sizegroup) lw->split_image_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
2170
2171         if (!lw->split_images[i])
2172                 {
2173                 lw->split_images[i] = image_new(TRUE);
2174
2175                 g_object_ref(lw->split_images[i]->widget);
2176
2177                 g_signal_connect(G_OBJECT(lw->split_images[i]->pr), "update-pixel",
2178                                  G_CALLBACK(layout_status_update_pixel_cb), lw);
2179
2180                 image_background_set_color_from_options(lw->split_images[i], FALSE);
2181
2182                 image_auto_refresh_enable(lw->split_images[i], TRUE);
2183
2184                 layout_image_dnd_init(lw, i);
2185                 image_color_profile_set(lw->split_images[i],
2186                                         options->color_profile.input_type,
2187                                         options->color_profile.use_image);
2188                 image_color_profile_set_use(lw->split_images[i], options->color_profile.enabled);
2189
2190                 gtk_size_group_add_widget(lw->split_image_sizegroup, lw->split_images[i]->widget);
2191                 gtk_widget_set_size_request(lw->split_images[i]->widget, IMAGE_MIN_WIDTH, -1);
2192
2193                 image_set_focus_in_func(lw->split_images[i], layout_image_focus_in_cb, lw);
2194
2195                 }
2196
2197         return lw->split_images[i]->widget;
2198 }
2199
2200 void layout_image_deactivate(LayoutWindow *lw, gint i)
2201 {
2202         if (!lw->split_images[i]) return;
2203         image_set_update_func(lw->split_images[i], nullptr, nullptr);
2204         layout_image_set_buttons_inactive(lw, i);
2205         image_set_drag_func(lw->split_images[i], layout_image_drag_inactive_cb, lw);
2206
2207         image_attach_window(lw->split_images[i], nullptr, nullptr, nullptr, FALSE);
2208         image_select(lw->split_images[i], FALSE);
2209 }
2210
2211 /* force should be set after change of lw->split_mode */
2212 void layout_image_activate(LayoutWindow *lw, gint i, gboolean force)
2213 {
2214         FileData *fd;
2215
2216         if (!lw->split_images[i]) return;
2217         if (!force && lw->active_split_image == i) return;
2218
2219         /* deactivate currently active */
2220         if (lw->active_split_image != i)
2221                 layout_image_deactivate(lw, lw->active_split_image);
2222
2223         lw->image = lw->split_images[i];
2224         lw->active_split_image = i;
2225
2226         image_set_update_func(lw->image, layout_image_update_cb, lw);
2227         layout_image_set_buttons(lw);
2228         image_set_drag_func(lw->image, layout_image_drag_cb, lw);
2229
2230         image_attach_window(lw->image, lw->window, nullptr, GQ_APPNAME, FALSE);
2231
2232         /* do not highlight selected image in SPLIT_NONE */
2233         /* maybe the image should be selected always and highlight should be controlled by
2234            another image option */
2235         if (lw->split_mode != SPLIT_NONE)
2236                 image_select(lw->split_images[i], TRUE);
2237         else
2238                 image_select(lw->split_images[i], FALSE);
2239
2240         fd = image_get_fd(lw->image);
2241
2242         if (fd)
2243                 {
2244                 layout_set_fd(lw, fd);
2245                 }
2246         layout_status_update_image(lw);
2247 }
2248
2249
2250 static void layout_image_setup_split_common(LayoutWindow *lw, gint n)
2251 {
2252         gboolean frame = (n > 1) || (!lw->options.tools_float && !lw->options.tools_hidden);
2253         gint i;
2254
2255         for (i = 0; i < n; i++)
2256                 if (!lw->split_images[i])
2257                         {
2258                         FileData *img_fd = nullptr;
2259                         double zoom = 0.0;
2260
2261                         layout_image_new(lw, i);
2262                         image_set_frame(lw->split_images[i], frame);
2263                         image_set_selectable(lw->split_images[i], (n > 1));
2264
2265                         if (lw->image)
2266                                 {
2267                                 image_osd_copy_status(lw->image, lw->split_images[i]);
2268                                 }
2269
2270                         if (layout_selection_count(lw, nullptr) > 1)
2271                                 {
2272                                 GList *work = g_list_last(layout_selection_list(lw));
2273                                 gint j = 0;
2274
2275                                 while (work && j < i)
2276                                         {
2277                                         auto fd = static_cast<FileData *>(work->data);
2278                                         work = work->prev;
2279
2280                                         if (!fd || !*fd->path || fd->parent ||
2281                                                                                 fd == lw->split_images[0]->image_fd)
2282                                                 {
2283                                                 continue;
2284                                                 }
2285                                         img_fd = fd;
2286
2287                                         j++;
2288                                         }
2289                                 }
2290
2291                         if (!img_fd && lw->image)
2292                                 {
2293                                 img_fd = image_get_fd(lw->image);
2294                                 zoom = image_zoom_get(lw->image);
2295                                 }
2296
2297                         if (img_fd)
2298                                 {
2299                                 gdouble sx, sy;
2300                                 image_change_fd(lw->split_images[i], img_fd, zoom);
2301                                 image_get_scroll_center(lw->image, &sx, &sy);
2302                                 image_set_scroll_center(lw->split_images[i], sx, sy);
2303                                 }
2304                         layout_image_deactivate(lw, i);
2305                         }
2306                 else
2307                         {
2308                         image_set_frame(lw->split_images[i], frame);
2309                         image_set_selectable(lw->split_images[i], (n > 1));
2310                         }
2311
2312         for (i = n; i < MAX_SPLIT_IMAGES; i++)
2313                 {
2314                 if (lw->split_images[i])
2315                         {
2316                         g_object_unref(lw->split_images[i]->widget);
2317                         lw->split_images[i] = nullptr;
2318                         }
2319                 }
2320
2321         if (!lw->image || lw->active_split_image < 0 || lw->active_split_image >= n)
2322                 {
2323                 layout_image_activate(lw, 0, TRUE);
2324                 }
2325         else
2326                 {
2327                 /* this will draw the frame around selected image (image_select)
2328                    on switch from single to split images */
2329                 layout_image_activate(lw, lw->active_split_image, TRUE);
2330                 }
2331 }
2332
2333 GtkWidget *layout_image_setup_split_none(LayoutWindow *lw)
2334 {
2335         lw->split_mode = SPLIT_NONE;
2336
2337         layout_image_setup_split_common(lw, 1);
2338
2339         lw->split_image_widget = lw->split_images[0]->widget;
2340
2341         return lw->split_image_widget;
2342 }
2343
2344
2345 GtkWidget *layout_image_setup_split_hv(LayoutWindow *lw, gboolean horizontal)
2346 {
2347         GtkWidget *paned;
2348
2349         lw->split_mode = horizontal ? SPLIT_HOR : SPLIT_VERT;
2350
2351         layout_image_setup_split_common(lw, 2);
2352
2353         /* horizontal split means vpaned and vice versa */
2354         paned = gtk_paned_new(horizontal ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL);
2355         DEBUG_NAME(paned);
2356
2357         gtk_paned_pack1(GTK_PANED(paned), lw->split_images[0]->widget, TRUE, TRUE);
2358         gtk_paned_pack2(GTK_PANED(paned), lw->split_images[1]->widget, TRUE, TRUE);
2359
2360         gtk_widget_show(lw->split_images[0]->widget);
2361         gtk_widget_show(lw->split_images[1]->widget);
2362
2363         lw->split_image_widget = paned;
2364
2365         return lw->split_image_widget;
2366
2367 }
2368
2369 static GtkWidget *layout_image_setup_split_triple(LayoutWindow *lw)
2370 {
2371         GtkWidget *hpaned1;
2372         GtkWidget *hpaned2;
2373         GtkAllocation allocation;
2374         gint i;
2375         gint pane_pos;
2376
2377         lw->split_mode = SPLIT_TRIPLE;
2378
2379         layout_image_setup_split_common(lw, 3);
2380
2381         gtk_widget_get_allocation(lw->utility_paned, &allocation);
2382
2383         hpaned1 = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
2384         DEBUG_NAME(hpaned1);
2385         hpaned2 = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
2386         DEBUG_NAME(hpaned2);
2387
2388         if (lw->bar && gtk_widget_get_visible(lw->bar))
2389                 {
2390                 pane_pos = (gtk_paned_get_position(GTK_PANED(lw->utility_paned))) / 3;
2391                 }
2392         else
2393                 {
2394                 pane_pos = allocation.width / 3;
2395                 }
2396
2397         gtk_paned_set_position(GTK_PANED(hpaned1), pane_pos);
2398         gtk_paned_set_position(GTK_PANED(hpaned2), pane_pos);
2399
2400         gtk_paned_pack1(GTK_PANED(hpaned1), lw->split_images[0]->widget, TRUE, TRUE);
2401         gtk_paned_pack1(GTK_PANED(hpaned2), lw->split_images[1]->widget, TRUE, TRUE);
2402         gtk_paned_pack2(GTK_PANED(hpaned2), lw->split_images[2]->widget, TRUE, TRUE);
2403         gtk_paned_pack2(GTK_PANED(hpaned1), hpaned2, TRUE, TRUE);
2404
2405         for (i = 0; i < 3; i++)
2406                 {
2407                 gtk_widget_show(lw->split_images[i]->widget);
2408                 }
2409
2410         gtk_widget_show(hpaned1);
2411         gtk_widget_show(hpaned2);
2412
2413         lw->split_image_widget = hpaned1;
2414
2415         return lw->split_image_widget;
2416 }
2417
2418 GtkWidget *layout_image_setup_split_quad(LayoutWindow *lw)
2419 {
2420         GtkWidget *hpaned;
2421         GtkWidget *vpaned1;
2422         GtkWidget *vpaned2;
2423         gint i;
2424
2425         lw->split_mode = SPLIT_QUAD;
2426
2427         layout_image_setup_split_common(lw, 4);
2428
2429         hpaned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
2430         DEBUG_NAME(hpaned);
2431         vpaned1 = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
2432         DEBUG_NAME(vpaned1);
2433         vpaned2 = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
2434         DEBUG_NAME(vpaned2);
2435
2436         gtk_paned_pack1(GTK_PANED(vpaned1), lw->split_images[0]->widget, TRUE, TRUE);
2437         gtk_paned_pack2(GTK_PANED(vpaned1), lw->split_images[2]->widget, TRUE, TRUE);
2438
2439         gtk_paned_pack1(GTK_PANED(vpaned2), lw->split_images[1]->widget, TRUE, TRUE);
2440         gtk_paned_pack2(GTK_PANED(vpaned2), lw->split_images[3]->widget, TRUE, TRUE);
2441
2442         gtk_paned_pack1(GTK_PANED(hpaned), vpaned1, TRUE, TRUE);
2443         gtk_paned_pack2(GTK_PANED(hpaned), vpaned2, TRUE, TRUE);
2444
2445         for (i = 0; i < 4; i++)
2446                 gtk_widget_show(lw->split_images[i]->widget);
2447
2448         gtk_widget_show(vpaned1);
2449         gtk_widget_show(vpaned2);
2450
2451         lw->split_image_widget = hpaned;
2452
2453         return lw->split_image_widget;
2454
2455 }
2456
2457 GtkWidget *layout_image_setup_split(LayoutWindow *lw, ImageSplitMode mode)
2458 {
2459         switch (mode)
2460                 {
2461                 case SPLIT_HOR:
2462                         return layout_image_setup_split_hv(lw, TRUE);
2463                 case SPLIT_VERT:
2464                         return layout_image_setup_split_hv(lw, FALSE);
2465                 case SPLIT_TRIPLE:
2466                         return layout_image_setup_split_triple(lw);
2467                 case SPLIT_QUAD:
2468                         return layout_image_setup_split_quad(lw);
2469                 case SPLIT_NONE:
2470                 default:
2471                         return layout_image_setup_split_none(lw);
2472                 }
2473 }
2474
2475
2476 /*
2477  *-----------------------------------------------------------------------------
2478  * maintenance (for rename, move, remove)
2479  *-----------------------------------------------------------------------------
2480  */
2481
2482 static void layout_image_maint_renamed(LayoutWindow *lw, FileData *fd)
2483 {
2484         if (fd == layout_image_get_fd(lw))
2485                 {
2486                 image_set_fd(lw->image, fd);
2487                 }
2488 }
2489
2490 static void layout_image_maint_removed(LayoutWindow *lw, FileData *fd)
2491 {
2492         if (fd == layout_image_get_fd(lw))
2493                 {
2494                 CollectionData *cd;
2495                 CollectInfo *info;
2496
2497                 cd = image_get_collection(lw->image, &info);
2498                 if (cd && info)
2499                         {
2500                         CollectInfo *next_collection;
2501
2502                         next_collection = collection_next_by_info(cd, info);
2503                         if (!next_collection) next_collection = collection_prev_by_info(cd, info);
2504
2505                         if (next_collection)
2506                                 {
2507                                 layout_image_set_collection(lw, cd, next_collection);
2508                                 return;
2509                                 }
2510                         layout_image_set_fd(lw, nullptr);
2511                         }
2512
2513                 /* the image will be set to the next image from the list soon,
2514                    setting it to NULL here is not necessary*/
2515                 }
2516 }
2517
2518
2519 void layout_image_notify_cb(FileData *fd, NotifyType type, gpointer data)
2520 {
2521         auto lw = static_cast<LayoutWindow *>(data);
2522
2523         if (!(type & NOTIFY_CHANGE) || !fd->change) return;
2524
2525         DEBUG_1("Notify layout_image: %s %04x", fd->path, type);
2526
2527         switch (fd->change->type)
2528                 {
2529                 case FILEDATA_CHANGE_MOVE:
2530                 case FILEDATA_CHANGE_RENAME:
2531                         layout_image_maint_renamed(lw, fd);
2532                         break;
2533                 case FILEDATA_CHANGE_DELETE:
2534                         layout_image_maint_removed(lw, fd);
2535                         break;
2536                 case FILEDATA_CHANGE_COPY:
2537                 case FILEDATA_CHANGE_UNSPECIFIED:
2538                 case FILEDATA_CHANGE_WRITE_METADATA:
2539                         break;
2540                 }
2541
2542 }
2543 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */