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