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