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