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