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