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