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