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