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