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