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