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