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