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