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