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