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