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