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