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