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