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