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