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