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