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