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